@@ -2,6 +2,7 @@ package interop
22
33import (
44 "fmt"
5+ "math/big"
56 "testing"
67
78 "github.com/ethereum-optimism/optimism/op-node/chaincfg"
@@ -21,46 +22,114 @@ import (
2122 "github.com/stretchr/testify/require"
2223)
2324
24- func TestDeriveBlockForFirstChainFromSuperchainRoot (t * testing.T ) {
25- logger := testlog .Logger (t , log .LevelError )
26- rollupCfg := chaincfg .OPSepolia ()
27- chainCfg := chainconfig .OPSepoliaChainConfig ()
28- chain1Output := & eth.OutputV0 {}
25+ func setupTwoChains () (* staticConfigSource , * eth.SuperV1 , stubTasks ) {
26+ rollupCfg1 := chaincfg .OPSepolia ()
27+ chainCfg1 := chainconfig .OPSepoliaChainConfig ()
28+
29+ rollupCfg2 := * chaincfg .OPSepolia ()
30+ rollupCfg2 .L2ChainID = new (big.Int ).SetUint64 (42 )
31+ chainCfg2 := * chainconfig .OPSepoliaChainConfig ()
32+ chainCfg2 .ChainID = rollupCfg2 .L2ChainID
33+
2934 agreedSuperRoot := & eth.SuperV1 {
30- Timestamp : rollupCfg .Genesis .L2Time + 1234 ,
31- Chains : []eth.ChainIDAndOutput {{ChainID : rollupCfg .L2ChainID .Uint64 (), Output : eth .OutputRoot (chain1Output )}},
35+ Timestamp : rollupCfg1 .Genesis .L2Time + 1234 ,
36+ Chains : []eth.ChainIDAndOutput {
37+ {ChainID : rollupCfg1 .L2ChainID .Uint64 (), Output : eth .OutputRoot (& eth.OutputV0 {BlockHash : common.Hash {0x11 }})},
38+ {ChainID : rollupCfg2 .L2ChainID .Uint64 (), Output : eth .OutputRoot (& eth.OutputV0 {BlockHash : common.Hash {0x22 }})},
39+ },
40+ }
41+ configSource := & staticConfigSource {
42+ rollupCfgs : []* rollup.Config {rollupCfg1 , & rollupCfg2 },
43+ chainConfigs : []* params.ChainConfig {chainCfg1 , & chainCfg2 },
3244 }
45+ tasksStub := stubTasks {
46+ blockHash : common.Hash {0x22 },
47+ outputRoot : eth.Bytes32 {0x66 },
48+ }
49+ return configSource , agreedSuperRoot , tasksStub
50+ }
51+
52+ func TestDeriveBlockForFirstChainFromSuperchainRoot (t * testing.T ) {
53+ logger := testlog .Logger (t , log .LevelError )
54+ configSource , agreedSuperRoot , tasksStub := setupTwoChains ()
55+
3356 outputRootHash := common .Hash (eth .SuperRoot (agreedSuperRoot ))
3457 l2PreimageOracle , _ := test .NewStubOracle (t )
3558 l2PreimageOracle .TransitionStates [outputRootHash ] = & types.TransitionState {SuperRoot : agreedSuperRoot .Marshal ()}
36- tasks := stubTasks {
37- l2SafeHead : eth.L2BlockRef {
38- Number : 56 ,
39- Hash : common.Hash {0x11 },
59+
60+ expectedIntermediateRoot := & types.TransitionState {
61+ SuperRoot : agreedSuperRoot .Marshal (),
62+ PendingProgress : []types.OptimisticBlock {
63+ {BlockHash : tasksStub .blockHash , OutputRoot : tasksStub .outputRoot },
4064 },
41- blockHash : common.Hash {0x22 },
42- outputRoot : eth.Bytes32 {0x66 },
65+ Step : 1 ,
4366 }
44- expectedIntermediateRoot := & types.TransitionState {
67+
68+ expectedClaim , err := expectedIntermediateRoot .Hash ()
69+ require .NoError (t , err )
70+
71+ verifyResult (t , logger , tasksStub , configSource , l2PreimageOracle , agreedSuperRoot , outputRootHash , expectedClaim )
72+ }
73+
74+ func TestDeriveBlockForSecondChainFromTransitionState (t * testing.T ) {
75+ logger := testlog .Logger (t , log .LevelError )
76+ configSource , agreedSuperRoot , tasksStub := setupTwoChains ()
77+ agreedTransitionState := & types.TransitionState {
4578 SuperRoot : agreedSuperRoot .Marshal (),
4679 PendingProgress : []types.OptimisticBlock {
47- {BlockHash : tasks . blockHash , OutputRoot : tasks . outputRoot },
80+ {BlockHash : common. Hash { 0xaa } , OutputRoot : eth. Bytes32 { 6 : 22 } },
4881 },
4982 Step : 1 ,
5083 }
84+ outputRootHash , err := agreedTransitionState .Hash ()
85+ require .NoError (t , err )
86+ l2PreimageOracle , _ := test .NewStubOracle (t )
87+ l2PreimageOracle .TransitionStates [outputRootHash ] = agreedTransitionState
88+ expectedIntermediateRoot := & types.TransitionState {
89+ SuperRoot : agreedSuperRoot .Marshal (),
90+ PendingProgress : []types.OptimisticBlock {
91+ {BlockHash : common.Hash {0xaa }, OutputRoot : eth.Bytes32 {6 : 22 }},
92+ {BlockHash : tasksStub .blockHash , OutputRoot : tasksStub .outputRoot },
93+ },
94+ Step : 2 ,
95+ }
5196
5297 expectedClaim , err := expectedIntermediateRoot .Hash ()
5398 require .NoError (t , err )
99+ verifyResult (t , logger , tasksStub , configSource , l2PreimageOracle , agreedSuperRoot , outputRootHash , expectedClaim )
100+ }
101+
102+ func TestNoOpStep (t * testing.T ) {
103+ logger := testlog .Logger (t , log .LevelError )
104+ configSource , agreedSuperRoot , tasksStub := setupTwoChains ()
105+ agreedTransitionState := & types.TransitionState {
106+ SuperRoot : agreedSuperRoot .Marshal (),
107+ PendingProgress : []types.OptimisticBlock {
108+ {BlockHash : common.Hash {0xaa }, OutputRoot : eth.Bytes32 {6 : 22 }},
109+ {BlockHash : tasksStub .blockHash , OutputRoot : tasksStub .outputRoot },
110+ },
111+ Step : 2 ,
112+ }
113+ outputRootHash , err := agreedTransitionState .Hash ()
114+ require .NoError (t , err )
115+ l2PreimageOracle , _ := test .NewStubOracle (t )
116+ l2PreimageOracle .TransitionStates [outputRootHash ] = agreedTransitionState
117+ expectedIntermediateRoot := * agreedTransitionState // Copy agreed state
118+ expectedIntermediateRoot .Step = 3
119+
120+ expectedClaim , err := expectedIntermediateRoot .Hash ()
121+ require .NoError (t , err )
122+ verifyResult (t , logger , tasksStub , configSource , l2PreimageOracle , agreedSuperRoot , outputRootHash , expectedClaim )
123+ }
124+
125+ func verifyResult (t * testing.T , logger log.Logger , tasks stubTasks , configSource * staticConfigSource , l2PreimageOracle * test.StubBlockOracle , agreedSuperRoot * eth.SuperV1 , agreedPrestate common.Hash , expectedClaim common.Hash ) {
54126 bootInfo := & boot.BootInfoInterop {
55- AgreedPrestate : outputRootHash ,
127+ AgreedPrestate : agreedPrestate ,
56128 ClaimTimestamp : agreedSuperRoot .Timestamp + 1 ,
57129 Claim : expectedClaim ,
58- Configs : & staticConfigSource {
59- rollupCfgs : []* rollup.Config {rollupCfg },
60- chainConfigs : []* params.ChainConfig {chainCfg },
61- },
130+ Configs : configSource ,
62131 }
63- err = runInteropProgram (logger , bootInfo , nil , l2PreimageOracle , true , & tasks )
132+ err : = runInteropProgram (logger , bootInfo , nil , l2PreimageOracle , true , & tasks )
64133 require .NoError (t , err )
65134}
66135
0 commit comments