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
91817423
Unverified
Commit
91817423
authored
Apr 17, 2023
by
Adrian Sutton
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
op-program: Extract stub oracles to a reusable test package
parent
98dced7a
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
212 additions
and
49 deletions
+212
-49
cache_test.go
op-program/client/l1/cache_test.go
+14
-13
client_test.go
op-program/client/l1/client_test.go
+12
-11
stub_oracle.go
op-program/client/l1/test/stub_oracle.go
+54
-0
cache_test.go
op-program/client/l2/cache_test.go
+10
-9
db_test.go
op-program/client/l2/db_test.go
+12
-14
engine_backend_test.go
op-program/client/l2/engine_backend_test.go
+3
-2
stub_oracle.go
op-program/client/l2/test/stub_oracle.go
+107
-0
No files found.
op-program/client/l1/cache_test.go
View file @
91817423
...
...
@@ -6,6 +6,7 @@ import (
"github.com/ethereum-optimism/optimism/op-node/eth"
"github.com/ethereum-optimism/optimism/op-node/testutils"
"github.com/ethereum-optimism/optimism/op-program/client/l1/test"
"github.com/stretchr/testify/require"
)
...
...
@@ -14,37 +15,37 @@ var _ Oracle = (*CachingOracle)(nil)
func
TestCachingOracle_HeaderByBlockHash
(
t
*
testing
.
T
)
{
rng
:=
rand
.
New
(
rand
.
NewSource
(
1
))
stub
:=
n
ewStubOracle
(
t
)
stub
:=
test
.
N
ewStubOracle
(
t
)
oracle
:=
NewCachingOracle
(
stub
)
block
:=
testutils
.
RandomBlockInfo
(
rng
)
// Initial call retrieves from the stub
stub
.
b
locks
[
block
.
Hash
()]
=
block
stub
.
B
locks
[
block
.
Hash
()]
=
block
result
:=
oracle
.
HeaderByBlockHash
(
block
.
Hash
())
require
.
Equal
(
t
,
block
,
result
)
// Later calls should retrieve from cache
delete
(
stub
.
b
locks
,
block
.
Hash
())
delete
(
stub
.
B
locks
,
block
.
Hash
())
result
=
oracle
.
HeaderByBlockHash
(
block
.
Hash
())
require
.
Equal
(
t
,
block
,
result
)
}
func
TestCachingOracle_TransactionsByBlockHash
(
t
*
testing
.
T
)
{
rng
:=
rand
.
New
(
rand
.
NewSource
(
1
))
stub
:=
n
ewStubOracle
(
t
)
stub
:=
test
.
N
ewStubOracle
(
t
)
oracle
:=
NewCachingOracle
(
stub
)
block
,
_
:=
testutils
.
RandomBlock
(
rng
,
3
)
// Initial call retrieves from the stub
stub
.
b
locks
[
block
.
Hash
()]
=
eth
.
BlockToInfo
(
block
)
stub
.
t
xs
[
block
.
Hash
()]
=
block
.
Transactions
()
stub
.
B
locks
[
block
.
Hash
()]
=
eth
.
BlockToInfo
(
block
)
stub
.
T
xs
[
block
.
Hash
()]
=
block
.
Transactions
()
actualBlock
,
actualTxs
:=
oracle
.
TransactionsByBlockHash
(
block
.
Hash
())
require
.
Equal
(
t
,
eth
.
BlockToInfo
(
block
),
actualBlock
)
require
.
Equal
(
t
,
block
.
Transactions
(),
actualTxs
)
// Later calls should retrieve from cache
delete
(
stub
.
b
locks
,
block
.
Hash
())
delete
(
stub
.
t
xs
,
block
.
Hash
())
delete
(
stub
.
B
locks
,
block
.
Hash
())
delete
(
stub
.
T
xs
,
block
.
Hash
())
actualBlock
,
actualTxs
=
oracle
.
TransactionsByBlockHash
(
block
.
Hash
())
require
.
Equal
(
t
,
eth
.
BlockToInfo
(
block
),
actualBlock
)
require
.
Equal
(
t
,
block
.
Transactions
(),
actualTxs
)
...
...
@@ -52,20 +53,20 @@ func TestCachingOracle_TransactionsByBlockHash(t *testing.T) {
func
TestCachingOracle_ReceiptsByBlockHash
(
t
*
testing
.
T
)
{
rng
:=
rand
.
New
(
rand
.
NewSource
(
1
))
stub
:=
n
ewStubOracle
(
t
)
stub
:=
test
.
N
ewStubOracle
(
t
)
oracle
:=
NewCachingOracle
(
stub
)
block
,
rcpts
:=
testutils
.
RandomBlock
(
rng
,
3
)
// Initial call retrieves from the stub
stub
.
b
locks
[
block
.
Hash
()]
=
eth
.
BlockToInfo
(
block
)
stub
.
r
cpts
[
block
.
Hash
()]
=
rcpts
stub
.
B
locks
[
block
.
Hash
()]
=
eth
.
BlockToInfo
(
block
)
stub
.
R
cpts
[
block
.
Hash
()]
=
rcpts
actualBlock
,
actualRcpts
:=
oracle
.
ReceiptsByBlockHash
(
block
.
Hash
())
require
.
Equal
(
t
,
eth
.
BlockToInfo
(
block
),
actualBlock
)
require
.
EqualValues
(
t
,
rcpts
,
actualRcpts
)
// Later calls should retrieve from cache
delete
(
stub
.
b
locks
,
block
.
Hash
())
delete
(
stub
.
r
cpts
,
block
.
Hash
())
delete
(
stub
.
B
locks
,
block
.
Hash
())
delete
(
stub
.
R
cpts
,
block
.
Hash
())
actualBlock
,
actualRcpts
=
oracle
.
ReceiptsByBlockHash
(
block
.
Hash
())
require
.
Equal
(
t
,
eth
.
BlockToInfo
(
block
),
actualBlock
)
require
.
EqualValues
(
t
,
rcpts
,
actualRcpts
)
...
...
op-program/client/l1/client_test.go
View file @
91817423
...
...
@@ -9,6 +9,7 @@ import (
"github.com/ethereum-optimism/optimism/op-node/rollup/derive"
"github.com/ethereum-optimism/optimism/op-node/testlog"
"github.com/ethereum-optimism/optimism/op-node/testutils"
"github.com/ethereum-optimism/optimism/op-program/client/l1/test"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
...
...
@@ -24,7 +25,7 @@ func TestInfoByHash(t *testing.T) {
client
,
oracle
:=
newClient
(
t
)
hash
:=
common
.
HexToHash
(
"0xAABBCC"
)
expected
:=
&
testutils
.
MockBlockInfo
{}
oracle
.
b
locks
[
hash
]
=
expected
oracle
.
B
locks
[
hash
]
=
expected
info
,
err
:=
client
.
InfoByHash
(
context
.
Background
(),
hash
)
require
.
NoError
(
t
,
err
)
...
...
@@ -35,7 +36,7 @@ func TestL1BlockRefByHash(t *testing.T) {
client
,
oracle
:=
newClient
(
t
)
hash
:=
common
.
HexToHash
(
"0xAABBCC"
)
header
:=
&
testutils
.
MockBlockInfo
{}
oracle
.
b
locks
[
hash
]
=
header
oracle
.
B
locks
[
hash
]
=
header
expected
:=
eth
.
InfoToL1BlockRef
(
header
)
ref
,
err
:=
client
.
L1BlockRefByHash
(
context
.
Background
(),
hash
)
...
...
@@ -50,8 +51,8 @@ func TestFetchReceipts(t *testing.T) {
expectedReceipts
:=
types
.
Receipts
{
&
types
.
Receipt
{},
}
oracle
.
b
locks
[
hash
]
=
expectedInfo
oracle
.
r
cpts
[
hash
]
=
expectedReceipts
oracle
.
B
locks
[
hash
]
=
expectedInfo
oracle
.
R
cpts
[
hash
]
=
expectedReceipts
info
,
rcpts
,
err
:=
client
.
FetchReceipts
(
context
.
Background
(),
hash
)
require
.
NoError
(
t
,
err
)
...
...
@@ -66,8 +67,8 @@ func TestInfoAndTxsByHash(t *testing.T) {
expectedTxs
:=
types
.
Transactions
{
&
types
.
Transaction
{},
}
oracle
.
b
locks
[
hash
]
=
expectedInfo
oracle
.
t
xs
[
hash
]
=
expectedTxs
oracle
.
B
locks
[
hash
]
=
expectedInfo
oracle
.
T
xs
[
hash
]
=
expectedTxs
info
,
txs
,
err
:=
client
.
InfoAndTxsByHash
(
context
.
Background
(),
hash
)
require
.
NoError
(
t
,
err
)
...
...
@@ -119,7 +120,7 @@ func TestL1BlockRefByNumber(t *testing.T) {
t
.
Run
(
"ParentOfHead"
,
func
(
t
*
testing
.
T
)
{
client
,
oracle
:=
newClient
(
t
)
parent
:=
blockNum
(
head
.
NumberU64
()
-
1
)
oracle
.
b
locks
[
parent
.
Hash
()]
=
parent
oracle
.
B
locks
[
parent
.
Hash
()]
=
parent
ref
,
err
:=
client
.
L1BlockRefByNumber
(
context
.
Background
(),
parent
.
NumberU64
())
require
.
NoError
(
t
,
err
)
...
...
@@ -131,7 +132,7 @@ func TestL1BlockRefByNumber(t *testing.T) {
blocks
:=
[]
eth
.
BlockInfo
{
block
}
for
i
:=
0
;
i
<
10
;
i
++
{
block
=
blockNum
(
block
.
NumberU64
()
-
1
)
oracle
.
b
locks
[
block
.
Hash
()]
=
block
oracle
.
B
locks
[
block
.
Hash
()]
=
block
blocks
=
append
(
blocks
,
block
)
}
...
...
@@ -143,9 +144,9 @@ func TestL1BlockRefByNumber(t *testing.T) {
})
}
func
newClient
(
t
*
testing
.
T
)
(
*
OracleL1Client
,
*
s
tubOracle
)
{
stub
:=
n
ewStubOracle
(
t
)
stub
.
b
locks
[
head
.
Hash
()]
=
head
func
newClient
(
t
*
testing
.
T
)
(
*
OracleL1Client
,
*
test
.
S
tubOracle
)
{
stub
:=
test
.
N
ewStubOracle
(
t
)
stub
.
B
locks
[
head
.
Hash
()]
=
head
client
:=
NewOracleL1Client
(
testlog
.
Logger
(
t
,
log
.
LvlDebug
),
stub
,
head
.
Hash
())
return
client
,
stub
}
...
...
op-program/client/l1/
stub_oracle_test
.go
→
op-program/client/l1/
test/stub_oracle
.go
View file @
91817423
package
l1
package
test
import
(
"testing"
...
...
@@ -8,45 +8,45 @@ import (
"github.com/ethereum/go-ethereum/core/types"
)
type
s
tubOracle
struct
{
type
S
tubOracle
struct
{
t
*
testing
.
T
//
b
locks maps block hash to eth.BlockInfo
b
locks
map
[
common
.
Hash
]
eth
.
BlockInfo
//
B
locks maps block hash to eth.BlockInfo
B
locks
map
[
common
.
Hash
]
eth
.
BlockInfo
//
t
xs maps block hash to transactions
t
xs
map
[
common
.
Hash
]
types
.
Transactions
//
T
xs maps block hash to transactions
T
xs
map
[
common
.
Hash
]
types
.
Transactions
//
r
cpts maps Block hash to receipts
r
cpts
map
[
common
.
Hash
]
types
.
Receipts
//
R
cpts maps Block hash to receipts
R
cpts
map
[
common
.
Hash
]
types
.
Receipts
}
func
newStubOracle
(
t
*
testing
.
T
)
*
s
tubOracle
{
return
&
s
tubOracle
{
func
NewStubOracle
(
t
*
testing
.
T
)
*
S
tubOracle
{
return
&
S
tubOracle
{
t
:
t
,
b
locks
:
make
(
map
[
common
.
Hash
]
eth
.
BlockInfo
),
t
xs
:
make
(
map
[
common
.
Hash
]
types
.
Transactions
),
r
cpts
:
make
(
map
[
common
.
Hash
]
types
.
Receipts
),
B
locks
:
make
(
map
[
common
.
Hash
]
eth
.
BlockInfo
),
T
xs
:
make
(
map
[
common
.
Hash
]
types
.
Transactions
),
R
cpts
:
make
(
map
[
common
.
Hash
]
types
.
Receipts
),
}
}
func
(
o
s
tubOracle
)
HeaderByBlockHash
(
blockHash
common
.
Hash
)
eth
.
BlockInfo
{
info
,
ok
:=
o
.
b
locks
[
blockHash
]
func
(
o
S
tubOracle
)
HeaderByBlockHash
(
blockHash
common
.
Hash
)
eth
.
BlockInfo
{
info
,
ok
:=
o
.
B
locks
[
blockHash
]
if
!
ok
{
o
.
t
.
Fatalf
(
"unknown block %s"
,
blockHash
)
}
return
info
}
func
(
o
s
tubOracle
)
TransactionsByBlockHash
(
blockHash
common
.
Hash
)
(
eth
.
BlockInfo
,
types
.
Transactions
)
{
txs
,
ok
:=
o
.
t
xs
[
blockHash
]
func
(
o
S
tubOracle
)
TransactionsByBlockHash
(
blockHash
common
.
Hash
)
(
eth
.
BlockInfo
,
types
.
Transactions
)
{
txs
,
ok
:=
o
.
T
xs
[
blockHash
]
if
!
ok
{
o
.
t
.
Fatalf
(
"unknown txs %s"
,
blockHash
)
}
return
o
.
HeaderByBlockHash
(
blockHash
),
txs
}
func
(
o
s
tubOracle
)
ReceiptsByBlockHash
(
blockHash
common
.
Hash
)
(
eth
.
BlockInfo
,
types
.
Receipts
)
{
rcpts
,
ok
:=
o
.
r
cpts
[
blockHash
]
func
(
o
S
tubOracle
)
ReceiptsByBlockHash
(
blockHash
common
.
Hash
)
(
eth
.
BlockInfo
,
types
.
Receipts
)
{
rcpts
,
ok
:=
o
.
R
cpts
[
blockHash
]
if
!
ok
{
o
.
t
.
Fatalf
(
"unknown rcpts %s"
,
blockHash
)
}
...
...
op-program/client/l2/cache_test.go
View file @
91817423
...
...
@@ -5,6 +5,7 @@ import (
"testing"
"github.com/ethereum-optimism/optimism/op-node/testutils"
"github.com/ethereum-optimism/optimism/op-program/client/l2/test"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
)
...
...
@@ -13,55 +14,55 @@ import (
var
_
Oracle
=
(
*
CachingOracle
)(
nil
)
func
TestBlockByHash
(
t
*
testing
.
T
)
{
stub
,
_
:=
n
ewStubOracle
(
t
)
stub
,
_
:=
test
.
N
ewStubOracle
(
t
)
oracle
:=
NewCachingOracle
(
stub
)
rng
:=
rand
.
New
(
rand
.
NewSource
(
1
))
block
,
_
:=
testutils
.
RandomBlock
(
rng
,
1
)
// Initial call retrieves from the stub
stub
.
b
locks
[
block
.
Hash
()]
=
block
stub
.
B
locks
[
block
.
Hash
()]
=
block
actual
:=
oracle
.
BlockByHash
(
block
.
Hash
())
require
.
Equal
(
t
,
block
,
actual
)
// Later calls should retrieve from cache
delete
(
stub
.
b
locks
,
block
.
Hash
())
delete
(
stub
.
B
locks
,
block
.
Hash
())
actual
=
oracle
.
BlockByHash
(
block
.
Hash
())
require
.
Equal
(
t
,
block
,
actual
)
}
func
TestNodeByHash
(
t
*
testing
.
T
)
{
stub
,
stateStub
:=
n
ewStubOracle
(
t
)
stub
,
stateStub
:=
test
.
N
ewStubOracle
(
t
)
oracle
:=
NewCachingOracle
(
stub
)
node
:=
[]
byte
{
12
,
3
,
4
}
hash
:=
common
.
Hash
{
0xaa
}
// Initial call retrieves from the stub
stateStub
.
d
ata
[
hash
]
=
node
stateStub
.
D
ata
[
hash
]
=
node
actual
:=
oracle
.
NodeByHash
(
hash
)
require
.
Equal
(
t
,
node
,
actual
)
// Later calls should retrieve from cache
delete
(
stateStub
.
d
ata
,
hash
)
delete
(
stateStub
.
D
ata
,
hash
)
actual
=
oracle
.
NodeByHash
(
hash
)
require
.
Equal
(
t
,
node
,
actual
)
}
func
TestCodeByHash
(
t
*
testing
.
T
)
{
stub
,
stateStub
:=
n
ewStubOracle
(
t
)
stub
,
stateStub
:=
test
.
N
ewStubOracle
(
t
)
oracle
:=
NewCachingOracle
(
stub
)
node
:=
[]
byte
{
12
,
3
,
4
}
hash
:=
common
.
Hash
{
0xaa
}
// Initial call retrieves from the stub
stateStub
.
c
ode
[
hash
]
=
node
stateStub
.
C
ode
[
hash
]
=
node
actual
:=
oracle
.
CodeByHash
(
hash
)
require
.
Equal
(
t
,
node
,
actual
)
// Later calls should retrieve from cache
delete
(
stateStub
.
c
ode
,
hash
)
delete
(
stateStub
.
C
ode
,
hash
)
actual
=
oracle
.
CodeByHash
(
hash
)
require
.
Equal
(
t
,
node
,
actual
)
}
op-program/client/l2/db_test.go
View file @
91817423
...
...
@@ -4,6 +4,7 @@ import (
"math/big"
"testing"
"github.com/ethereum-optimism/optimism/op-program/client/l2/test"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/rawdb"
...
...
@@ -27,7 +28,7 @@ var _ ethdb.KeyValueStore = (*OracleKeyValueStore)(nil)
func
TestGet
(
t
*
testing
.
T
)
{
t
.
Run
(
"IncorrectLengthKey"
,
func
(
t
*
testing
.
T
)
{
oracle
:=
n
ewStubStateOracle
(
t
)
oracle
:=
test
.
N
ewStubStateOracle
(
t
)
db
:=
NewOracleBackedDB
(
oracle
)
val
,
err
:=
db
.
Get
([]
byte
{
1
,
2
,
3
})
require
.
ErrorIs
(
t
,
err
,
ErrInvalidKeyLength
)
...
...
@@ -35,13 +36,13 @@ func TestGet(t *testing.T) {
})
t
.
Run
(
"KeyWithCodePrefix"
,
func
(
t
*
testing
.
T
)
{
oracle
:=
n
ewStubStateOracle
(
t
)
oracle
:=
test
.
N
ewStubStateOracle
(
t
)
db
:=
NewOracleBackedDB
(
oracle
)
key
:=
common
.
HexToHash
(
"0x12345678"
)
prefixedKey
:=
append
(
rawdb
.
CodePrefix
,
key
.
Bytes
()
...
)
expected
:=
[]
byte
{
1
,
2
,
3
}
oracle
.
c
ode
[
key
]
=
expected
oracle
.
C
ode
[
key
]
=
expected
val
,
err
:=
db
.
Get
(
prefixedKey
)
require
.
NoError
(
t
,
err
)
...
...
@@ -49,13 +50,13 @@ func TestGet(t *testing.T) {
})
t
.
Run
(
"NormalKeyThatHappensToStartWithCodePrefix"
,
func
(
t
*
testing
.
T
)
{
oracle
:=
n
ewStubStateOracle
(
t
)
oracle
:=
test
.
N
ewStubStateOracle
(
t
)
db
:=
NewOracleBackedDB
(
oracle
)
key
:=
make
([]
byte
,
common
.
HashLength
)
copy
(
rawdb
.
CodePrefix
,
key
)
println
(
key
[
0
])
expected
:=
[]
byte
{
1
,
2
,
3
}
oracle
.
d
ata
[
common
.
BytesToHash
(
key
)]
=
expected
oracle
.
D
ata
[
common
.
BytesToHash
(
key
)]
=
expected
val
,
err
:=
db
.
Get
(
key
)
require
.
NoError
(
t
,
err
)
...
...
@@ -65,8 +66,8 @@ func TestGet(t *testing.T) {
t
.
Run
(
"KnownKey"
,
func
(
t
*
testing
.
T
)
{
key
:=
common
.
HexToHash
(
"0xAA4488"
)
expected
:=
[]
byte
{
2
,
6
,
3
,
8
}
oracle
:=
n
ewStubStateOracle
(
t
)
oracle
.
d
ata
[
key
]
=
expected
oracle
:=
test
.
N
ewStubStateOracle
(
t
)
oracle
.
D
ata
[
key
]
=
expected
db
:=
NewOracleBackedDB
(
oracle
)
val
,
err
:=
db
.
Get
(
key
.
Bytes
())
require
.
NoError
(
t
,
err
)
...
...
@@ -76,7 +77,7 @@ func TestGet(t *testing.T) {
func
TestPut
(
t
*
testing
.
T
)
{
t
.
Run
(
"NewKey"
,
func
(
t
*
testing
.
T
)
{
oracle
:=
n
ewStubStateOracle
(
t
)
oracle
:=
test
.
N
ewStubStateOracle
(
t
)
db
:=
NewOracleBackedDB
(
oracle
)
key
:=
common
.
HexToHash
(
"0xAA4488"
)
value
:=
[]
byte
{
2
,
6
,
3
,
8
}
...
...
@@ -88,7 +89,7 @@ func TestPut(t *testing.T) {
require
.
Equal
(
t
,
value
,
actual
)
})
t
.
Run
(
"ReplaceKey"
,
func
(
t
*
testing
.
T
)
{
oracle
:=
n
ewStubStateOracle
(
t
)
oracle
:=
test
.
N
ewStubStateOracle
(
t
)
db
:=
NewOracleBackedDB
(
oracle
)
key
:=
common
.
HexToHash
(
"0xAA4488"
)
value1
:=
[]
byte
{
2
,
6
,
3
,
8
}
...
...
@@ -109,16 +110,13 @@ func TestSupportsStateDBOperations(t *testing.T) {
realDb
:=
rawdb
.
NewDatabase
(
memorydb
.
New
())
genesisBlock
:=
l2Genesis
.
MustCommit
(
realDb
)
loader
:=
&
kvStateOracle
{
t
:
t
,
source
:
realDb
,
}
loader
:=
test
.
NewKvStateOracle
(
t
,
realDb
)
assertStateDataAvailable
(
t
,
NewOracleBackedDB
(
loader
),
l2Genesis
,
genesisBlock
)
}
func
TestUpdateState
(
t
*
testing
.
T
)
{
l2Genesis
:=
createGenesis
()
oracle
:=
n
ewStubStateOracle
(
t
)
oracle
:=
test
.
N
ewStubStateOracle
(
t
)
db
:=
rawdb
.
NewDatabase
(
NewOracleBackedDB
(
oracle
))
genesisBlock
:=
l2Genesis
.
MustCommit
(
db
)
...
...
op-program/client/l2/engine_backend_test.go
View file @
91817423
...
...
@@ -8,6 +8,7 @@ import (
"github.com/ethereum-optimism/optimism/op-node/testlog"
"github.com/ethereum-optimism/optimism/op-program/client/l2/engineapi"
"github.com/ethereum-optimism/optimism/op-program/client/l2/engineapi/test"
l2test
"github.com/ethereum-optimism/optimism/op-program/client/l2/test"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/consensus/beacon"
...
...
@@ -143,7 +144,7 @@ func setupOracleBackedChainWithLowerHead(t *testing.T, blockCount int, headBlock
return
blocks
,
chain
}
func
setupOracle
(
t
*
testing
.
T
,
blockCount
int
,
headBlockNumber
int
)
(
*
params
.
ChainConfig
,
[]
*
types
.
Block
,
*
s
tubBlockOracle
)
{
func
setupOracle
(
t
*
testing
.
T
,
blockCount
int
,
headBlockNumber
int
)
(
*
params
.
ChainConfig
,
[]
*
types
.
Block
,
*
l2test
.
S
tubBlockOracle
)
{
deployConfig
:=
&
genesis
.
DeployConfig
{
L1ChainID
:
900
,
L2ChainID
:
901
,
...
...
@@ -171,7 +172,7 @@ func setupOracle(t *testing.T, blockCount int, headBlockNumber int) (*params.Cha
genesisBlock
:=
l2Genesis
.
MustCommit
(
db
)
blocks
,
_
:=
core
.
GenerateChain
(
chainCfg
,
genesisBlock
,
consensus
,
db
,
blockCount
,
func
(
i
int
,
gen
*
core
.
BlockGen
)
{})
blocks
=
append
([]
*
types
.
Block
{
genesisBlock
},
blocks
...
)
oracle
:=
n
ewStubOracleWithBlocks
(
t
,
blocks
[
:
headBlockNumber
+
1
],
db
)
oracle
:=
l2test
.
N
ewStubOracleWithBlocks
(
t
,
blocks
[
:
headBlockNumber
+
1
],
db
)
return
chainCfg
,
blocks
,
oracle
}
...
...
op-program/client/l2/
stub_oracle_test
.go
→
op-program/client/l2/
test/stub_oracle
.go
View file @
91817423
package
l2
package
test
import
(
"testing"
...
...
@@ -9,84 +9,97 @@ import (
"github.com/ethereum/go-ethereum/ethdb"
)
type
stubBlockOracle
struct
{
// Same as l2.StateOracle but need to use our own copy to avoid dependency loops
type
stateOracle
interface
{
NodeByHash
(
nodeHash
common
.
Hash
)
[]
byte
CodeByHash
(
codeHash
common
.
Hash
)
[]
byte
}
type
StubBlockOracle
struct
{
t
*
testing
.
T
b
locks
map
[
common
.
Hash
]
*
types
.
Block
S
tateOracle
B
locks
map
[
common
.
Hash
]
*
types
.
Block
s
tateOracle
}
func
newStubOracle
(
t
*
testing
.
T
)
(
*
stubBlockOracle
,
*
s
tubStateOracle
)
{
stateOracle
:=
n
ewStubStateOracle
(
t
)
blockOracle
:=
s
tubBlockOracle
{
func
NewStubOracle
(
t
*
testing
.
T
)
(
*
StubBlockOracle
,
*
S
tubStateOracle
)
{
stateOracle
:=
N
ewStubStateOracle
(
t
)
blockOracle
:=
S
tubBlockOracle
{
t
:
t
,
b
locks
:
make
(
map
[
common
.
Hash
]
*
types
.
Block
),
S
tateOracle
:
stateOracle
,
B
locks
:
make
(
map
[
common
.
Hash
]
*
types
.
Block
),
s
tateOracle
:
stateOracle
,
}
return
&
blockOracle
,
stateOracle
}
func
newStubOracleWithBlocks
(
t
*
testing
.
T
,
chain
[]
*
types
.
Block
,
db
ethdb
.
Database
)
*
s
tubBlockOracle
{
func
NewStubOracleWithBlocks
(
t
*
testing
.
T
,
chain
[]
*
types
.
Block
,
db
ethdb
.
Database
)
*
S
tubBlockOracle
{
blocks
:=
make
(
map
[
common
.
Hash
]
*
types
.
Block
,
len
(
chain
))
for
_
,
block
:=
range
chain
{
blocks
[
block
.
Hash
()]
=
block
}
return
&
s
tubBlockOracle
{
b
locks
:
blocks
,
StateOracle
:
&
kvStateOracle
{
t
:
t
,
s
ource
:
db
},
return
&
S
tubBlockOracle
{
B
locks
:
blocks
,
stateOracle
:
&
KvStateOracle
{
t
:
t
,
S
ource
:
db
},
}
}
func
(
o
s
tubBlockOracle
)
BlockByHash
(
blockHash
common
.
Hash
)
*
types
.
Block
{
block
,
ok
:=
o
.
b
locks
[
blockHash
]
func
(
o
S
tubBlockOracle
)
BlockByHash
(
blockHash
common
.
Hash
)
*
types
.
Block
{
block
,
ok
:=
o
.
B
locks
[
blockHash
]
if
!
ok
{
o
.
t
.
Fatalf
(
"requested unknown block %s"
,
blockHash
)
}
return
block
}
//
k
vStateOracle loads data from a source ethdb.KeyValueStore
type
k
vStateOracle
struct
{
//
K
vStateOracle loads data from a source ethdb.KeyValueStore
type
K
vStateOracle
struct
{
t
*
testing
.
T
source
ethdb
.
KeyValueStore
Source
ethdb
.
KeyValueStore
}
func
NewKvStateOracle
(
t
*
testing
.
T
,
db
ethdb
.
KeyValueStore
)
*
KvStateOracle
{
return
&
KvStateOracle
{
t
:
t
,
Source
:
db
,
}
}
func
(
o
*
k
vStateOracle
)
NodeByHash
(
nodeHash
common
.
Hash
)
[]
byte
{
val
,
err
:=
o
.
s
ource
.
Get
(
nodeHash
.
Bytes
())
func
(
o
*
K
vStateOracle
)
NodeByHash
(
nodeHash
common
.
Hash
)
[]
byte
{
val
,
err
:=
o
.
S
ource
.
Get
(
nodeHash
.
Bytes
())
if
err
!=
nil
{
o
.
t
.
Fatalf
(
"error retrieving node %v: %v"
,
nodeHash
,
err
)
}
return
val
}
func
(
o
*
k
vStateOracle
)
CodeByHash
(
hash
common
.
Hash
)
[]
byte
{
return
rawdb
.
ReadCode
(
o
.
s
ource
,
hash
)
func
(
o
*
K
vStateOracle
)
CodeByHash
(
hash
common
.
Hash
)
[]
byte
{
return
rawdb
.
ReadCode
(
o
.
S
ource
,
hash
)
}
func
newStubStateOracle
(
t
*
testing
.
T
)
*
s
tubStateOracle
{
return
&
s
tubStateOracle
{
func
NewStubStateOracle
(
t
*
testing
.
T
)
*
S
tubStateOracle
{
return
&
S
tubStateOracle
{
t
:
t
,
d
ata
:
make
(
map
[
common
.
Hash
][]
byte
),
c
ode
:
make
(
map
[
common
.
Hash
][]
byte
),
D
ata
:
make
(
map
[
common
.
Hash
][]
byte
),
C
ode
:
make
(
map
[
common
.
Hash
][]
byte
),
}
}
// Stub StateOracle implementation that reads from simple maps
type
s
tubStateOracle
struct
{
// Stub
StateOracle is a
StateOracle implementation that reads from simple maps
type
S
tubStateOracle
struct
{
t
*
testing
.
T
d
ata
map
[
common
.
Hash
][]
byte
c
ode
map
[
common
.
Hash
][]
byte
D
ata
map
[
common
.
Hash
][]
byte
C
ode
map
[
common
.
Hash
][]
byte
}
func
(
o
*
s
tubStateOracle
)
NodeByHash
(
nodeHash
common
.
Hash
)
[]
byte
{
data
,
ok
:=
o
.
d
ata
[
nodeHash
]
func
(
o
*
S
tubStateOracle
)
NodeByHash
(
nodeHash
common
.
Hash
)
[]
byte
{
data
,
ok
:=
o
.
D
ata
[
nodeHash
]
if
!
ok
{
o
.
t
.
Fatalf
(
"no value for node %v"
,
nodeHash
)
}
return
data
}
func
(
o
*
s
tubStateOracle
)
CodeByHash
(
hash
common
.
Hash
)
[]
byte
{
data
,
ok
:=
o
.
c
ode
[
hash
]
func
(
o
*
S
tubStateOracle
)
CodeByHash
(
hash
common
.
Hash
)
[]
byte
{
data
,
ok
:=
o
.
C
ode
[
hash
]
if
!
ok
{
o
.
t
.
Fatalf
(
"no value for code %v"
,
hash
)
}
...
...
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