boot.go 2.51 KB
Newer Older
1 2 3 4 5
package client

import (
	"encoding/binary"
	"encoding/json"
6
	"math"
7 8

	"github.com/ethereum-optimism/optimism/op-node/rollup"
9
	preimage "github.com/ethereum-optimism/optimism/op-preimage"
10
	"github.com/ethereum-optimism/optimism/op-program/chainconfig"
11 12 13 14
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/params"
)

15 16
const (
	L1HeadLocalIndex preimage.LocalIndexKey = iota + 1
17
	L2OutputRootLocalIndex
18 19
	L2ClaimLocalIndex
	L2ClaimBlockNumberLocalIndex
20 21 22
	L2ChainIDLocalIndex

	// These local keys are only used for custom chains
23 24 25
	L2ChainConfigLocalIndex
	RollupConfigLocalIndex
)
26

27 28 29
// CustomChainIDIndicator is used to detect when the program should load custom chain configuration
const CustomChainIDIndicator = uint64(math.MaxUint64)

30 31
type BootInfo struct {
	L1Head             common.Hash
32
	L2OutputRoot       common.Hash
33 34
	L2Claim            common.Hash
	L2ClaimBlockNumber uint64
35 36 37 38
	L2ChainID          uint64

	L2ChainConfig *params.ChainConfig
	RollupConfig  *rollup.Config
39 40
}

41 42
type oracleClient interface {
	Get(key preimage.Key) []byte
43 44
}

45 46
type BootstrapClient struct {
	r oracleClient
47 48
}

49 50
func NewBootstrapClient(r oracleClient) *BootstrapClient {
	return &BootstrapClient{r: r}
51 52
}

53 54
func (br *BootstrapClient) BootInfo() *BootInfo {
	l1Head := common.BytesToHash(br.r.Get(L1HeadLocalIndex))
55
	l2OutputRoot := common.BytesToHash(br.r.Get(L2OutputRootLocalIndex))
56 57
	l2Claim := common.BytesToHash(br.r.Get(L2ClaimLocalIndex))
	l2ClaimBlockNumber := binary.BigEndian.Uint64(br.r.Get(L2ClaimBlockNumberLocalIndex))
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
	l2ChainID := binary.BigEndian.Uint64(br.r.Get(L2ChainIDLocalIndex))

	var l2ChainConfig *params.ChainConfig
	var rollupConfig *rollup.Config
	if l2ChainID == CustomChainIDIndicator {
		l2ChainConfig = new(params.ChainConfig)
		err := json.Unmarshal(br.r.Get(L2ChainConfigLocalIndex), &l2ChainConfig)
		if err != nil {
			panic("failed to bootstrap l2ChainConfig")
		}
		rollupConfig = new(rollup.Config)
		err = json.Unmarshal(br.r.Get(RollupConfigLocalIndex), rollupConfig)
		if err != nil {
			panic("failed to bootstrap rollup config")
		}
	} else {
		var err error
		rollupConfig, err = chainconfig.RollupConfigByChainID(l2ChainID)
		if err != nil {
			panic(err)
		}
		l2ChainConfig, err = chainconfig.ChainConfigByChainID(l2ChainID)
		if err != nil {
			panic(err)
		}
83
	}
84 85 86

	return &BootInfo{
		L1Head:             l1Head,
87
		L2OutputRoot:       l2OutputRoot,
88 89
		L2Claim:            l2Claim,
		L2ClaimBlockNumber: l2ClaimBlockNumber,
90
		L2ChainID:          l2ChainID,
91 92
		L2ChainConfig:      l2ChainConfig,
		RollupConfig:       rollupConfig,
93 94
	}
}