Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
bc37c15e
Unverified
Commit
bc37c15e
authored
Apr 11, 2023
by
Adrian Sutton
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
op-program: Implement oracle backed L1 client
parent
9adf9b4f
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
283 additions
and
0 deletions
+283
-0
client.go
op-program/client/l1/client.go
+70
-0
client_test.go
op-program/client/l1/client_test.go
+206
-0
fetcher.go
op-program/host/l1/fetcher.go
+7
-0
No files found.
op-program/client/l1/client.go
0 → 100644
View file @
bc37c15e
package
l1
import
(
"context"
"errors"
"fmt"
"github.com/ethereum-optimism/optimism/op-node/eth"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
var
(
ErrNotFound
=
errors
.
New
(
"not found"
)
ErrUnknownLabel
=
errors
.
New
(
"unknown label"
)
)
type
OracleL1Client
struct
{
oracle
Oracle
head
eth
.
L1BlockRef
}
func
NewOracleL1Client
(
oracle
Oracle
,
l1Head
common
.
Hash
)
*
OracleL1Client
{
head
:=
eth
.
InfoToL1BlockRef
(
oracle
.
HeaderByHash
(
l1Head
))
return
&
OracleL1Client
{
oracle
:
oracle
,
head
:
head
,
}
}
func
(
o
OracleL1Client
)
L1BlockRefByLabel
(
ctx
context
.
Context
,
label
eth
.
BlockLabel
)
(
eth
.
L1BlockRef
,
error
)
{
switch
label
{
case
eth
.
Unsafe
:
return
o
.
head
,
nil
case
eth
.
Safe
:
return
o
.
head
,
nil
case
eth
.
Finalized
:
return
o
.
head
,
nil
}
return
eth
.
L1BlockRef
{},
fmt
.
Errorf
(
"%w: %s"
,
ErrUnknownLabel
,
label
)
}
func
(
o
OracleL1Client
)
L1BlockRefByNumber
(
ctx
context
.
Context
,
number
uint64
)
(
eth
.
L1BlockRef
,
error
)
{
if
number
>
o
.
head
.
Number
{
return
eth
.
L1BlockRef
{},
fmt
.
Errorf
(
"%w: block number %d"
,
ErrNotFound
,
number
)
}
head
:=
o
.
head
for
head
.
Number
>
number
{
head
=
eth
.
InfoToL1BlockRef
(
o
.
oracle
.
HeaderByHash
(
head
.
ParentHash
))
}
return
head
,
nil
}
func
(
o
OracleL1Client
)
L1BlockRefByHash
(
ctx
context
.
Context
,
hash
common
.
Hash
)
(
eth
.
L1BlockRef
,
error
)
{
return
eth
.
InfoToL1BlockRef
(
o
.
oracle
.
HeaderByHash
(
hash
)),
nil
}
func
(
o
OracleL1Client
)
InfoByHash
(
ctx
context
.
Context
,
hash
common
.
Hash
)
(
eth
.
BlockInfo
,
error
)
{
return
o
.
oracle
.
HeaderByHash
(
hash
),
nil
}
func
(
o
OracleL1Client
)
FetchReceipts
(
ctx
context
.
Context
,
blockHash
common
.
Hash
)
(
eth
.
BlockInfo
,
types
.
Receipts
,
error
)
{
info
,
rcpts
:=
o
.
oracle
.
ReceiptsByHash
(
blockHash
)
return
info
,
rcpts
,
nil
}
func
(
o
OracleL1Client
)
InfoAndTxsByHash
(
ctx
context
.
Context
,
hash
common
.
Hash
)
(
eth
.
BlockInfo
,
types
.
Transactions
,
error
)
{
info
,
txs
:=
o
.
oracle
.
TransactionsByHash
(
hash
)
return
info
,
txs
,
nil
}
op-program/client/l1/client_test.go
0 → 100644
View file @
bc37c15e
package
l1
import
(
"context"
"math/big"
"testing"
"github.com/ethereum-optimism/optimism/op-node/eth"
"github.com/ethereum-optimism/optimism/op-node/rollup/derive"
"github.com/ethereum-optimism/optimism/op-node/sources"
"github.com/ethereum-optimism/optimism/op-node/testutils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/stretchr/testify/require"
)
var
_
derive
.
L1Fetcher
=
(
*
OracleL1Client
)(
nil
)
var
head
=
blockNum
(
1000
)
func
TestInfoByHash
(
t
*
testing
.
T
)
{
client
,
oracle
:=
newClient
(
t
)
hash
:=
common
.
HexToHash
(
"0xAABBCC"
)
expected
:=
&
sources
.
HeaderInfo
{}
oracle
.
blocks
[
hash
]
=
expected
info
,
err
:=
client
.
InfoByHash
(
context
.
Background
(),
hash
)
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
expected
,
info
)
}
func
TestL1BlockRefByHash
(
t
*
testing
.
T
)
{
client
,
oracle
:=
newClient
(
t
)
hash
:=
common
.
HexToHash
(
"0xAABBCC"
)
header
:=
&
sources
.
HeaderInfo
{}
oracle
.
blocks
[
hash
]
=
header
expected
:=
eth
.
InfoToL1BlockRef
(
header
)
ref
,
err
:=
client
.
L1BlockRefByHash
(
context
.
Background
(),
hash
)
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
expected
,
ref
)
}
func
TestFetchReceipts
(
t
*
testing
.
T
)
{
client
,
oracle
:=
newClient
(
t
)
hash
:=
common
.
HexToHash
(
"0xAABBCC"
)
expectedInfo
:=
&
sources
.
HeaderInfo
{}
expectedReceipts
:=
types
.
Receipts
{
&
types
.
Receipt
{},
}
oracle
.
blocks
[
hash
]
=
expectedInfo
oracle
.
rcpts
[
hash
]
=
expectedReceipts
info
,
rcpts
,
err
:=
client
.
FetchReceipts
(
context
.
Background
(),
hash
)
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
expectedInfo
,
info
)
require
.
Equal
(
t
,
expectedReceipts
,
rcpts
)
}
func
TestInfoAndTxsByHash
(
t
*
testing
.
T
)
{
client
,
oracle
:=
newClient
(
t
)
hash
:=
common
.
HexToHash
(
"0xAABBCC"
)
expectedInfo
:=
&
sources
.
HeaderInfo
{}
expectedTxs
:=
types
.
Transactions
{
&
types
.
Transaction
{},
}
oracle
.
blocks
[
hash
]
=
expectedInfo
oracle
.
txs
[
hash
]
=
expectedTxs
info
,
txs
,
err
:=
client
.
InfoAndTxsByHash
(
context
.
Background
(),
hash
)
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
expectedInfo
,
info
)
require
.
Equal
(
t
,
expectedTxs
,
txs
)
}
func
TestL1BlockRefByLabel
(
t
*
testing
.
T
)
{
t
.
Run
(
"Unsafe"
,
func
(
t
*
testing
.
T
)
{
client
,
_
:=
newClient
(
t
)
ref
,
err
:=
client
.
L1BlockRefByLabel
(
context
.
Background
(),
eth
.
Unsafe
)
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
eth
.
InfoToL1BlockRef
(
head
),
ref
)
})
t
.
Run
(
"Safe"
,
func
(
t
*
testing
.
T
)
{
client
,
_
:=
newClient
(
t
)
ref
,
err
:=
client
.
L1BlockRefByLabel
(
context
.
Background
(),
eth
.
Safe
)
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
eth
.
InfoToL1BlockRef
(
head
),
ref
)
})
t
.
Run
(
"Finalized"
,
func
(
t
*
testing
.
T
)
{
client
,
_
:=
newClient
(
t
)
ref
,
err
:=
client
.
L1BlockRefByLabel
(
context
.
Background
(),
eth
.
Finalized
)
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
eth
.
InfoToL1BlockRef
(
head
),
ref
)
})
t
.
Run
(
"UnknownLabel"
,
func
(
t
*
testing
.
T
)
{
client
,
_
:=
newClient
(
t
)
ref
,
err
:=
client
.
L1BlockRefByLabel
(
context
.
Background
(),
eth
.
BlockLabel
(
"unknown"
))
require
.
ErrorIs
(
t
,
err
,
ErrUnknownLabel
)
require
.
Equal
(
t
,
eth
.
L1BlockRef
{},
ref
)
})
}
func
TestL1BlockRefByNumber
(
t
*
testing
.
T
)
{
t
.
Run
(
"Head"
,
func
(
t
*
testing
.
T
)
{
client
,
_
:=
newClient
(
t
)
ref
,
err
:=
client
.
L1BlockRefByNumber
(
context
.
Background
(),
head
.
NumberU64
())
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
eth
.
InfoToL1BlockRef
(
head
),
ref
)
})
t
.
Run
(
"AfterHead"
,
func
(
t
*
testing
.
T
)
{
client
,
_
:=
newClient
(
t
)
ref
,
err
:=
client
.
L1BlockRefByNumber
(
context
.
Background
(),
head
.
NumberU64
()
+
1
)
require
.
ErrorIs
(
t
,
err
,
ErrNotFound
)
require
.
Equal
(
t
,
eth
.
L1BlockRef
{},
ref
)
})
t
.
Run
(
"ParentOfHead"
,
func
(
t
*
testing
.
T
)
{
client
,
oracle
:=
newClient
(
t
)
parent
:=
blockNum
(
head
.
NumberU64
()
-
1
)
oracle
.
blocks
[
parent
.
Hash
()]
=
parent
ref
,
err
:=
client
.
L1BlockRefByNumber
(
context
.
Background
(),
parent
.
NumberU64
())
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
eth
.
InfoToL1BlockRef
(
parent
),
ref
)
})
t
.
Run
(
"AncestorOfHead"
,
func
(
t
*
testing
.
T
)
{
client
,
oracle
:=
newClient
(
t
)
block
:=
head
blocks
:=
[]
eth
.
BlockInfo
{
block
}
for
i
:=
0
;
i
<
10
;
i
++
{
block
=
blockNum
(
block
.
NumberU64
()
-
1
)
oracle
.
blocks
[
block
.
Hash
()]
=
block
blocks
=
append
(
blocks
,
block
)
}
for
_
,
block
:=
range
blocks
{
ref
,
err
:=
client
.
L1BlockRefByNumber
(
context
.
Background
(),
block
.
NumberU64
())
require
.
NoError
(
t
,
err
)
require
.
Equal
(
t
,
eth
.
InfoToL1BlockRef
(
block
),
ref
)
}
})
}
func
newClient
(
t
*
testing
.
T
)
(
*
OracleL1Client
,
*
stubOracle
)
{
stub
:=
&
stubOracle
{
t
:
t
,
blocks
:
make
(
map
[
common
.
Hash
]
eth
.
BlockInfo
),
txs
:
make
(
map
[
common
.
Hash
]
types
.
Transactions
),
rcpts
:
make
(
map
[
common
.
Hash
]
types
.
Receipts
),
}
stub
.
blocks
[
head
.
Hash
()]
=
head
client
:=
NewOracleL1Client
(
stub
,
head
.
Hash
())
return
client
,
stub
}
type
stubOracle
struct
{
t
*
testing
.
T
// blocks maps block hash to eth.BlockInfo
blocks
map
[
common
.
Hash
]
eth
.
BlockInfo
// txs maps block hash to transactions
txs
map
[
common
.
Hash
]
types
.
Transactions
// rcpts maps Block hash to receipts
rcpts
map
[
common
.
Hash
]
types
.
Receipts
}
func
(
o
stubOracle
)
HeaderByHash
(
blockHash
common
.
Hash
)
eth
.
BlockInfo
{
info
,
ok
:=
o
.
blocks
[
blockHash
]
if
!
ok
{
o
.
t
.
Fatalf
(
"unknown block %s"
,
blockHash
)
}
return
info
}
func
(
o
stubOracle
)
TransactionsByHash
(
blockHash
common
.
Hash
)
(
eth
.
BlockInfo
,
types
.
Transactions
)
{
txs
,
ok
:=
o
.
txs
[
blockHash
]
if
!
ok
{
o
.
t
.
Fatalf
(
"unknown txs %s"
,
blockHash
)
}
return
o
.
HeaderByHash
(
blockHash
),
txs
}
func
(
o
stubOracle
)
ReceiptsByHash
(
blockHash
common
.
Hash
)
(
eth
.
BlockInfo
,
types
.
Receipts
)
{
rcpts
,
ok
:=
o
.
rcpts
[
blockHash
]
if
!
ok
{
o
.
t
.
Fatalf
(
"unknown rcpts %s"
,
blockHash
)
}
return
o
.
HeaderByHash
(
blockHash
),
rcpts
}
func
blockNum
(
num
uint64
)
eth
.
BlockInfo
{
parentNum
:=
num
-
1
return
&
testutils
.
MockBlockInfo
{
InfoHash
:
common
.
BytesToHash
(
big
.
NewInt
(
int64
(
num
))
.
Bytes
()),
InfoParentHash
:
common
.
BytesToHash
(
big
.
NewInt
(
int64
(
parentNum
))
.
Bytes
()),
InfoCoinbase
:
common
.
Address
{},
InfoRoot
:
common
.
Hash
{},
InfoNum
:
num
,
InfoTime
:
num
*
2
,
InfoMixDigest
:
[
32
]
byte
{},
InfoBaseFee
:
nil
,
InfoReceiptRoot
:
common
.
Hash
{},
InfoGasUsed
:
0
,
}
}
op-program/host/l1/fetcher.go
View file @
bc37c15e
...
...
@@ -20,6 +20,13 @@ type FetchingL1Oracle struct {
source
Source
}
func
NewFetchingL1Oracle
(
ctx
context
.
Context
,
source
Source
)
*
FetchingL1Oracle
{
return
&
FetchingL1Oracle
{
ctx
:
ctx
,
source
:
source
,
}
}
func
(
o
FetchingL1Oracle
)
HeaderByHash
(
blockHash
common
.
Hash
)
eth
.
BlockInfo
{
info
,
err
:=
o
.
source
.
InfoByHash
(
o
.
ctx
,
blockHash
)
if
err
!=
nil
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment