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