boot.go 1.73 KB
Newer Older
1 2 3 4 5 6 7
package client

import (
	"encoding/binary"
	"encoding/json"

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

13 14 15 16 17 18 19 20
const (
	L1HeadLocalIndex preimage.LocalIndexKey = iota + 1
	L2HeadLocalIndex
	L2ClaimLocalIndex
	L2ClaimBlockNumberLocalIndex
	L2ChainConfigLocalIndex
	RollupConfigLocalIndex
)
21

22 23 24 25 26 27 28
type BootInfo struct {
	L1Head             common.Hash
	L2Head             common.Hash
	L2Claim            common.Hash
	L2ClaimBlockNumber uint64
	L2ChainConfig      *params.ChainConfig
	RollupConfig       *rollup.Config
29 30
}

31 32
type oracleClient interface {
	Get(key preimage.Key) []byte
33 34
}

35 36
type BootstrapClient struct {
	r oracleClient
37 38
}

39 40
func NewBootstrapClient(r oracleClient) *BootstrapClient {
	return &BootstrapClient{r: r}
41 42
}

43 44 45 46 47 48 49 50 51
func (br *BootstrapClient) BootInfo() *BootInfo {
	l1Head := common.BytesToHash(br.r.Get(L1HeadLocalIndex))
	l2Head := common.BytesToHash(br.r.Get(L2HeadLocalIndex))
	l2Claim := common.BytesToHash(br.r.Get(L2ClaimLocalIndex))
	l2ClaimBlockNumber := binary.BigEndian.Uint64(br.r.Get(L2ClaimBlockNumberLocalIndex))
	l2ChainConfig := new(params.ChainConfig)
	err := json.Unmarshal(br.r.Get(L2ChainConfigLocalIndex), &l2ChainConfig)
	if err != nil {
		panic("failed to bootstrap l2ChainConfig")
52
	}
53 54 55 56
	rollupConfig := new(rollup.Config)
	err = json.Unmarshal(br.r.Get(RollupConfigLocalIndex), rollupConfig)
	if err != nil {
		panic("failed to bootstrap rollup config")
57
	}
58 59 60 61 62 63 64 65

	return &BootInfo{
		L1Head:             l1Head,
		L2Head:             l2Head,
		L2Claim:            l2Claim,
		L2ClaimBlockNumber: l2ClaimBlockNumber,
		L2ChainConfig:      l2ChainConfig,
		RollupConfig:       rollupConfig,
66 67
	}
}