|
| 1 | +package boot |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "math/big" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/ethereum-optimism/optimism/op-node/chaincfg" |
| 10 | + "github.com/ethereum-optimism/optimism/op-node/rollup" |
| 11 | + preimage "github.com/ethereum-optimism/optimism/op-preimage" |
| 12 | + "github.com/ethereum-optimism/optimism/op-program/chainconfig" |
| 13 | + "github.com/ethereum/go-ethereum/common" |
| 14 | + "github.com/ethereum/go-ethereum/params" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | +) |
| 17 | + |
| 18 | +func TestInteropBootstrap_SimpleValues(t *testing.T) { |
| 19 | + expected := &BootInfoInterop{ |
| 20 | + L1Head: common.Hash{0xaa}, |
| 21 | + AgreedPrestate: common.Hash{0xbb}, |
| 22 | + Claim: common.Hash{0xcc}, |
| 23 | + ClaimTimestamp: 49829482, |
| 24 | + } |
| 25 | + mockOracle := newMockInteropBootstrapOracle(expected, false) |
| 26 | + actual := BootstrapInterop(mockOracle) |
| 27 | + require.Equal(t, expected.L1Head, actual.L1Head) |
| 28 | + require.Equal(t, expected.AgreedPrestate, actual.AgreedPrestate) |
| 29 | + require.Equal(t, expected.Claim, actual.Claim) |
| 30 | + require.Equal(t, expected.ClaimTimestamp, actual.ClaimTimestamp) |
| 31 | +} |
| 32 | + |
| 33 | +func TestInteropBootstrap_RollupConfigBuiltIn(t *testing.T) { |
| 34 | + expectedCfg := chaincfg.OPSepolia() |
| 35 | + expected := &BootInfoInterop{ |
| 36 | + L1Head: common.Hash{0xaa}, |
| 37 | + AgreedPrestate: common.Hash{0xbb}, |
| 38 | + Claim: common.Hash{0xcc}, |
| 39 | + ClaimTimestamp: 49829482, |
| 40 | + } |
| 41 | + mockOracle := newMockInteropBootstrapOracle(expected, false) |
| 42 | + actual := BootstrapInterop(mockOracle) |
| 43 | + actualCfg, err := actual.Configs.RollupConfig(expectedCfg.L2ChainID.Uint64()) |
| 44 | + require.NoError(t, err) |
| 45 | + require.Equal(t, expectedCfg, actualCfg) |
| 46 | +} |
| 47 | + |
| 48 | +func TestInteropBootstrap_RollupConfigCustom(t *testing.T) { |
| 49 | + config1 := &rollup.Config{L2ChainID: big.NewInt(1111)} |
| 50 | + config2 := &rollup.Config{L2ChainID: big.NewInt(2222)} |
| 51 | + source := &BootInfoInterop{ |
| 52 | + L1Head: common.Hash{0xaa}, |
| 53 | + AgreedPrestate: common.Hash{0xbb}, |
| 54 | + Claim: common.Hash{0xcc}, |
| 55 | + ClaimTimestamp: 49829482, |
| 56 | + } |
| 57 | + mockOracle := newMockInteropBootstrapOracle(source, true) |
| 58 | + mockOracle.rollupCfgs = []*rollup.Config{config1, config2} |
| 59 | + actual := BootstrapInterop(mockOracle) |
| 60 | + actualCfg, err := actual.Configs.RollupConfig(config1.L2ChainID.Uint64()) |
| 61 | + require.NoError(t, err) |
| 62 | + require.Equal(t, config1, actualCfg) |
| 63 | + |
| 64 | + actualCfg, err = actual.Configs.RollupConfig(config2.L2ChainID.Uint64()) |
| 65 | + require.NoError(t, err) |
| 66 | + require.Equal(t, config2, actualCfg) |
| 67 | +} |
| 68 | + |
| 69 | +func TestInteropBootstrap_ChainConfigBuiltIn(t *testing.T) { |
| 70 | + expectedCfg := chainconfig.OPSepoliaChainConfig() |
| 71 | + expected := &BootInfoInterop{ |
| 72 | + L1Head: common.Hash{0xaa}, |
| 73 | + AgreedPrestate: common.Hash{0xbb}, |
| 74 | + Claim: common.Hash{0xcc}, |
| 75 | + ClaimTimestamp: 49829482, |
| 76 | + } |
| 77 | + mockOracle := newMockInteropBootstrapOracle(expected, false) |
| 78 | + actual := BootstrapInterop(mockOracle) |
| 79 | + actualCfg, err := actual.Configs.ChainConfig(expectedCfg.ChainID.Uint64()) |
| 80 | + require.NoError(t, err) |
| 81 | + require.Equal(t, expectedCfg, actualCfg) |
| 82 | +} |
| 83 | + |
| 84 | +func TestInteropBootstrap_ChainConfigCustom(t *testing.T) { |
| 85 | + config1 := ¶ms.ChainConfig{ChainID: big.NewInt(1111)} |
| 86 | + config2 := ¶ms.ChainConfig{ChainID: big.NewInt(2222)} |
| 87 | + expected := &BootInfoInterop{ |
| 88 | + L1Head: common.Hash{0xaa}, |
| 89 | + AgreedPrestate: common.Hash{0xbb}, |
| 90 | + Claim: common.Hash{0xcc}, |
| 91 | + ClaimTimestamp: 49829482, |
| 92 | + } |
| 93 | + mockOracle := newMockInteropBootstrapOracle(expected, true) |
| 94 | + mockOracle.chainCfgs = []*params.ChainConfig{config1, config2} |
| 95 | + actual := BootstrapInterop(mockOracle) |
| 96 | + |
| 97 | + actualCfg, err := actual.Configs.ChainConfig(config1.ChainID.Uint64()) |
| 98 | + require.NoError(t, err) |
| 99 | + require.Equal(t, config1, actualCfg) |
| 100 | + |
| 101 | + actualCfg, err = actual.Configs.ChainConfig(config2.ChainID.Uint64()) |
| 102 | + require.NoError(t, err) |
| 103 | + require.Equal(t, config2, actualCfg) |
| 104 | +} |
| 105 | + |
| 106 | +func newMockInteropBootstrapOracle(b *BootInfoInterop, custom bool) *mockInteropBootstrapOracle { |
| 107 | + return &mockInteropBootstrapOracle{ |
| 108 | + mockBoostrapOracle: mockBoostrapOracle{ |
| 109 | + l1Head: b.L1Head, |
| 110 | + l2OutputRoot: b.AgreedPrestate, |
| 111 | + l2Claim: b.Claim, |
| 112 | + l2ClaimBlockNumber: b.ClaimTimestamp, |
| 113 | + }, |
| 114 | + custom: custom, |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +type mockInteropBootstrapOracle struct { |
| 119 | + mockBoostrapOracle |
| 120 | + rollupCfgs []*rollup.Config |
| 121 | + chainCfgs []*params.ChainConfig |
| 122 | + custom bool |
| 123 | +} |
| 124 | + |
| 125 | +func (o *mockInteropBootstrapOracle) Get(key preimage.Key) []byte { |
| 126 | + switch key.PreimageKey() { |
| 127 | + case L2ChainConfigLocalIndex.PreimageKey(): |
| 128 | + if !o.custom { |
| 129 | + panic(fmt.Sprintf("unexpected oracle request for preimage key %x", key.PreimageKey())) |
| 130 | + } |
| 131 | + b, _ := json.Marshal(o.chainCfgs) |
| 132 | + return b |
| 133 | + case RollupConfigLocalIndex.PreimageKey(): |
| 134 | + if !o.custom { |
| 135 | + panic(fmt.Sprintf("unexpected oracle request for preimage key %x", key.PreimageKey())) |
| 136 | + } |
| 137 | + b, _ := json.Marshal(o.rollupCfgs) |
| 138 | + return b |
| 139 | + default: |
| 140 | + return o.mockBoostrapOracle.Get(key) |
| 141 | + } |
| 142 | +} |
0 commit comments