boot_test.go 2.86 KB
Newer Older
1 2 3
package client

import (
4 5
	"encoding/binary"
	"encoding/json"
6
	"fmt"
7 8
	"testing"

9
	"github.com/ethereum-optimism/optimism/op-node/chaincfg"
10
	preimage "github.com/ethereum-optimism/optimism/op-preimage"
11
	"github.com/ethereum-optimism/optimism/op-program/chainconfig"
12 13 14 15
	"github.com/ethereum/go-ethereum/common"
	"github.com/stretchr/testify/require"
)

16 17 18
func TestBootstrapClient(t *testing.T) {
	bootInfo := &BootInfo{
		L1Head:             common.HexToHash("0x1111"),
19
		L2OutputRoot:       common.HexToHash("0x2222"),
20
		L2Claim:            common.HexToHash("0x3333"),
21
		L2ClaimBlockNumber: 1,
22 23
		L2ChainID:          chaincfg.Goerli.L2ChainID.Uint64(),
		L2ChainConfig:      chainconfig.OPGoerliChainConfig,
24
		RollupConfig:       chaincfg.Goerli,
25
	}
26
	mockOracle := &mockBoostrapOracle{bootInfo, false}
27 28 29
	readBootInfo := NewBootstrapClient(mockOracle).BootInfo()
	require.EqualValues(t, bootInfo, readBootInfo)
}
30

31 32 33 34 35 36 37 38
func TestBootstrapClient_CustomChain(t *testing.T) {
	bootInfo := &BootInfo{
		L1Head:             common.HexToHash("0x1111"),
		L2OutputRoot:       common.HexToHash("0x2222"),
		L2Claim:            common.HexToHash("0x3333"),
		L2ClaimBlockNumber: 1,
		L2ChainID:          CustomChainIDIndicator,
		L2ChainConfig:      chainconfig.OPGoerliChainConfig,
39
		RollupConfig:       chaincfg.Goerli,
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
	}
	mockOracle := &mockBoostrapOracle{bootInfo, true}
	readBootInfo := NewBootstrapClient(mockOracle).BootInfo()
	require.EqualValues(t, bootInfo, readBootInfo)
}

func TestBootstrapClient_UnknownChainPanics(t *testing.T) {
	bootInfo := &BootInfo{
		L1Head:             common.HexToHash("0x1111"),
		L2OutputRoot:       common.HexToHash("0x2222"),
		L2Claim:            common.HexToHash("0x3333"),
		L2ClaimBlockNumber: 1,
		L2ChainID:          uint64(0xdead),
	}
	mockOracle := &mockBoostrapOracle{bootInfo, false}
	client := NewBootstrapClient(mockOracle)
	require.Panics(t, func() { client.BootInfo() })
}

59
type mockBoostrapOracle struct {
60 61
	b      *BootInfo
	custom bool
62
}
63

64 65 66 67
func (o *mockBoostrapOracle) Get(key preimage.Key) []byte {
	switch key.PreimageKey() {
	case L1HeadLocalIndex.PreimageKey():
		return o.b.L1Head[:]
68 69
	case L2OutputRootLocalIndex.PreimageKey():
		return o.b.L2OutputRoot[:]
70 71 72 73
	case L2ClaimLocalIndex.PreimageKey():
		return o.b.L2Claim[:]
	case L2ClaimBlockNumberLocalIndex.PreimageKey():
		return binary.BigEndian.AppendUint64(nil, o.b.L2ClaimBlockNumber)
74 75
	case L2ChainIDLocalIndex.PreimageKey():
		return binary.BigEndian.AppendUint64(nil, o.b.L2ChainID)
76
	case L2ChainConfigLocalIndex.PreimageKey():
77
		if !o.custom {
78
			panic(fmt.Sprintf("unexpected oracle request for preimage key %x", key.PreimageKey()))
79
		}
80 81 82
		b, _ := json.Marshal(o.b.L2ChainConfig)
		return b
	case RollupConfigLocalIndex.PreimageKey():
83
		if !o.custom {
84
			panic(fmt.Sprintf("unexpected oracle request for preimage key %x", key.PreimageKey()))
85
		}
86 87 88 89
		b, _ := json.Marshal(o.b.RollupConfig)
		return b
	default:
		panic("unknown key")
90 91
	}
}