Skip to content

Commit

Permalink
Merge pull request #84 from bobanetwork/import-superchain
Browse files Browse the repository at this point in the history
Use superchain to load chain configurations
  • Loading branch information
boyuan-chen authored Feb 23, 2024
2 parents 8a4ed7d + 41fc25d commit 55b4885
Show file tree
Hide file tree
Showing 20 changed files with 441 additions and 199 deletions.
4 changes: 2 additions & 2 deletions consensus/misc/create2deployer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestEnsureCreate2Deployer(t *testing.T) {
{
name: "another chain ID",
override: func(cfg *chain.Config) {
cfg.ChainID = params.OptimismMainnetChainConfig.ChainID
cfg.ChainID = big.NewInt(params.OPMainnetChainID)
},
timestamp: canyonTime,
applied: true,
Expand Down Expand Up @@ -72,7 +72,7 @@ func TestEnsureCreate2Deployer(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := chain.Config{
ChainID: params.OptimismSepoliaChainConfig.ChainID,
ChainID: big.NewInt(params.OPMainnetChainID),
Optimism: &chain.OptimismConfig{},
CanyonTime: big.NewInt(int64(canyonTime)),
}
Expand Down
3 changes: 1 addition & 2 deletions consensus/misc/eip1559.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

"github.com/ledgerwatch/erigon-lib/chain"
"github.com/ledgerwatch/erigon-lib/common"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/polygon/bor/borcfg"

Expand Down Expand Up @@ -69,7 +68,7 @@ type eip1559Calculator struct{}
func (f eip1559Calculator) CurrentFees(chainConfig *chain.Config, db kv.Getter) (baseFee uint64, blobFee uint64, minBlobGasPrice uint64, err error) {
hash := rawdb.ReadHeadHeaderHash(db)

if hash == (libcommon.Hash{}) {
if hash == (common.Hash{}) {
return 0, 0, 0, fmt.Errorf("can't get head header hash")
}

Expand Down
1 change: 0 additions & 1 deletion core/allocs/boba_sepolia.json

This file was deleted.

1 change: 0 additions & 1 deletion core/allocs/optimism_mainnet.json

This file was deleted.

1 change: 0 additions & 1 deletion core/allocs/optimism_sepolia.json

This file was deleted.

2 changes: 1 addition & 1 deletion core/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestGenesisBlockHashes(t *testing.T) {
require.Equal(t, block.Hash().Bytes(), expect.Bytes(), network)
}
for _, network := range networkname.All {
if network == "optimism-mainnet" {
if network == "optimism-mainnet" || network == "boba-sepolia" {
t.Skip()
}
check(network)
Expand Down
162 changes: 123 additions & 39 deletions core/genesis_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ import (
"encoding/json"
"fmt"
"math/big"
"reflect"
"sync"

"github.com/c2h5oh/datasize"
"github.com/ethereum-optimism/superchain-registry/superchain"
"github.com/holiman/uint256"
"github.com/ledgerwatch/log/v3"
"golang.org/x/exp/slices"
Expand Down Expand Up @@ -189,6 +191,18 @@ func WriteGenesisBlock(tx kv.RwTx, genesis *types.Genesis, overrideCancunTime, o
newCfg = storedCfg
applyOverrides(newCfg)
}

if newCfg.IsOptimism() {
if !reflect.DeepEqual(newCfg, storedCfg) {
log.Info("Update latest chain config from superchain registry")
}
// rewrite using superchain config just in case
if err := rawdb.WriteChainConfig(tx, storedHash, newCfg); err != nil {
return newCfg, nil, err
}
return newCfg, storedBlock, nil
}

// Check config compatibility and write the config. Compatibility errors
// are returned to the caller unless we're already at block zero.
height := rawdb.ReadHeaderNumber(tx, rawdb.ReadHeadHeaderHash(tx))
Expand Down Expand Up @@ -488,39 +502,6 @@ func ChiadoGenesisBlock() *types.Genesis {
}
}

func OptimismMainnetGenesisBlock() *types.Genesis {
return &types.Genesis{
Config: params.OptimismMainnetChainConfig,
Difficulty: big.NewInt(1),
Mixhash: libcommon.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"),
ExtraData: hexutil.MustDecode("0x000000000000000000000000000000000000000000000000000000000000000000000398232e2064f896018496b4b44b3d62751f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"),
GasLimit: 15000000,
Alloc: readPrealloc("allocs/optimism_mainnet.json"),
}
}

func OptimismSepoliaGenesisBlock() *types.Genesis {
return &types.Genesis{
Config: params.OptimismSepoliaChainConfig,
Difficulty: big.NewInt(1),
Mixhash: libcommon.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"),
ExtraData: hexutil.MustDecode("0x000000000000000000000000000000000000000000000000000000000000000027770a9694e4b4b1e130ab91bc327c36855f612e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"),
GasLimit: 15000000,
Alloc: readPrealloc("allocs/optimism_sepolia.json"),
}
}

func BobaSepoliaGenesisBlock() *types.Genesis {
return &types.Genesis{
Config: params.BobaSepoliaChainConfig,
Difficulty: big.NewInt(1),
Mixhash: libcommon.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"),
ExtraData: hexutil.MustDecode("0x000000000000000000000000000000000000000000000000000000000000000000000398232e2064f896018496b4b44b3d62751f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"),
GasLimit: 11000000,
Alloc: readPrealloc("allocs/boba_sepolia.json"),
}
}

// Pre-calculated version of:
//
// DevnetSignPrivateKey = crypto.HexToECDSA(sha256.Sum256([]byte("erigon devnet key")))
Expand Down Expand Up @@ -683,6 +664,13 @@ func GenesisToBlock(g *types.Genesis, tmpDir string, logger log.Logger) (*types.
return nil, nil, err
}

if g.StateHash != nil {
if len(g.Alloc) > 0 {
panic(fmt.Errorf("cannot both have genesis hash %s "+
"and non-empty state-allocation", *g.StateHash))
}
root = *g.StateHash
}
head.Root = root

return types.NewBlock(head, nil, nil, nil, withdrawals), statedb, nil
Expand Down Expand Up @@ -718,6 +706,13 @@ func readPrealloc(filename string) types.GenesisAlloc {
}

func GenesisBlockByChainName(chain string) *types.Genesis {
genesis, err := loadOPStackGenesisByChainName(chain)
if err != nil {
panic(err)
}
if genesis != nil {
return genesis
}
switch chain {
case networkname.MainnetChainName:
return MainnetGenesisBlock()
Expand All @@ -739,13 +734,102 @@ func GenesisBlockByChainName(chain string) *types.Genesis {
return GnosisGenesisBlock()
case networkname.ChiadoChainName:
return ChiadoGenesisBlock()
case networkname.OptimismMainnetChainName:
return OptimismMainnetGenesisBlock()
case networkname.OptimismSepoliaChainName:
return OptimismSepoliaGenesisBlock()
case networkname.BobaSepoliaChainName:
return BobaSepoliaGenesisBlock()
default:
return nil
}
}

// loadOPStackGenesisByChainName loads genesis block corresponding to the chain name from superchain regsitry.
// This implementation is based on op-geth(https://github.com/ethereum-optimism/op-geth/blob/c7871bc4454ffc924eb128fa492975b30c9c46ad/core/superchain.go#L13)
func loadOPStackGenesisByChainName(name string) (*types.Genesis, error) {
opStackChainCfg := params.OPStackChainConfigByName(name)
if opStackChainCfg == nil {
return nil, nil
}

cfg := params.LoadSuperChainConfig(opStackChainCfg)
if cfg == nil {
return nil, nil
}

gen, err := superchain.LoadGenesis(opStackChainCfg.ChainID)
if err != nil {
return nil, fmt.Errorf("failed to load genesis definition for chain %d: %w", opStackChainCfg.ChainID, err)
}

genesis := &types.Genesis{
Config: cfg,
Nonce: gen.Nonce,
Timestamp: gen.Timestamp,
ExtraData: gen.ExtraData,
GasLimit: gen.GasLimit,
Difficulty: (*big.Int)(gen.Difficulty),
Mixhash: libcommon.Hash(gen.Mixhash),
Coinbase: libcommon.Address(gen.Coinbase),
Alloc: make(types.GenesisAlloc),
Number: gen.Number,
GasUsed: gen.GasUsed,
ParentHash: libcommon.Hash(gen.ParentHash),
BaseFee: (*big.Int)(gen.BaseFee),
}

for addr, acc := range gen.Alloc {
var code []byte
if acc.CodeHash != ([32]byte{}) {
dat, err := superchain.LoadContractBytecode(acc.CodeHash)
if err != nil {
return nil, fmt.Errorf("failed to load bytecode %s of address %s in chain %d: %w", acc.CodeHash, addr, opStackChainCfg.ChainID, err)
}
code = dat
}
var storage map[libcommon.Hash]libcommon.Hash
if len(acc.Storage) > 0 {
storage = make(map[libcommon.Hash]libcommon.Hash)
for k, v := range acc.Storage {
storage[libcommon.Hash(k)] = libcommon.Hash(v)
}
}
bal := libcommon.Big0
if acc.Balance != nil {
bal = (*big.Int)(acc.Balance)
}
genesis.Alloc[libcommon.Address(addr)] = types.GenesisAccount{
Code: code,
Storage: storage,
Balance: bal,
Nonce: acc.Nonce,
}
}
if gen.StateHash != nil {
if len(gen.Alloc) > 0 {
return nil, fmt.Errorf("chain definition unexpectedly contains both allocation (%d) and state-hash %s", len(gen.Alloc), *gen.StateHash)
}
genesis.StateHash = (*libcommon.Hash)(gen.StateHash)
}

genesisBlock, _, err := GenesisToBlock(genesis, "", log.New())
if err != nil {
return nil, fmt.Errorf("failed to build genesis block: %w", err)
}
genesisBlockHash := genesisBlock.Hash()
expectedHash := libcommon.Hash([32]byte(opStackChainCfg.Genesis.L2.Hash))

// Verify we correctly produced the genesis config by recomputing the genesis-block-hash,
// and check the genesis matches the chain genesis definition.
if opStackChainCfg.Genesis.L2.Number != genesisBlock.NumberU64() {
switch opStackChainCfg.ChainID {
case params.OPMainnetChainID:
expectedHash = params.OPMainnetGenesisHash
case params.OPGoerliChainID:
expectedHash = params.OPGoerliGenesisHash
case params.BobaSepoliaChainID:
expectedHash = params.BobaSepoliaGenesisHash
default:
return nil, fmt.Errorf("unknown stateless genesis definition for chain %d", opStackChainCfg.ChainID)
}
}
if expectedHash != genesisBlockHash {
return nil, fmt.Errorf("produced genesis with hash %s but expected %s", genesisBlockHash, expectedHash)
}
return genesis, nil
}
5 changes: 5 additions & 0 deletions core/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ type Genesis struct {
BlobGasUsed *uint64 `json:"blobGasUsed"` // EIP-4844
ExcessBlobGas *uint64 `json:"excessBlobGas"` // EIP-4844
ParentBeaconBlockRoot *common.Hash `json:"parentBeaconBlockRoot"` // EIP-4788

// StateHash represents the genesis state, to allow instantiation of a chain with missing initial state.
// Chains with history pruning, or extraordinarily large genesis allocation (e.g. after a regenesis event)
// may utilize this to get started, and then state-sync the latest state, while still verifying the header chain.
StateHash *common.Hash `json:"stateHash,omitempty"`
}

// GenesisAlloc specifies the initial state that is part of the genesis block.
Expand Down
3 changes: 2 additions & 1 deletion erigon-lib/chain/chain_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ type BorConfig interface {
func (c *Config) String() string {
engine := c.getEngine()

return fmt.Sprintf("{ChainID: %v, Homestead: %v, DAO: %v, Tangerine Whistle: %v, Spurious Dragon: %v, Byzantium: %v, Constantinople: %v, Petersburg: %v, Istanbul: %v, Muir Glacier: %v, Berlin: %v, London: %v, Arrow Glacier: %v, Gray Glacier: %v, Terminal Total Difficulty: %v, Merge Netsplit: %v, Shanghai: %v, Cancun: %v, Prague: %v, BedrockBlock: %v, RegolithTime: %v, CanyonTime: %v, Engine: %v}",
return fmt.Sprintf("{ChainID: %v, Homestead: %v, DAO: %v, Tangerine Whistle: %v, Spurious Dragon: %v, Byzantium: %v, Constantinople: %v, Petersburg: %v, Istanbul: %v, Muir Glacier: %v, Berlin: %v, London: %v, Arrow Glacier: %v, Gray Glacier: %v, Terminal Total Difficulty: %v, Merge Netsplit: %v, Shanghai: %v, Cancun: %v, Prague: %v, BedrockBlock: %v, RegolithTime: %v, CanyonTime: %v, EcotoneTime: %v, Engine: %v}",
c.ChainID,
c.HomesteadBlock,
c.DAOForkBlock,
Expand All @@ -129,6 +129,7 @@ func (c *Config) String() string {
c.BedrockBlock,
c.RegolithTime,
c.CanyonTime,
c.EcotoneTime,
engine,
)
}
Expand Down
42 changes: 37 additions & 5 deletions erigon-lib/chain/networkname/network_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@ const (
BorE2ETestChain2ValName = "bor-e2e-test-2Val"
ChiadoChainName = "chiado"

OptimismMainnetChainName = "optimism-mainnet"
OptimismSepoliaChainName = "optimism-sepolia"
BobaSepoliaChainName = "boba-sepolia"
OPMainnetChainName = "op-mainnet"
OPGoerliChainName = "op-goerli"
OPSepoliaChainName = "op-sepolia"

BobaSepoliaChainName = "boba-sepolia"

LegacyOPMainnetChainName = "optimism-mainnet"
LegacyOPGoerliChainName = "optimism-goerli"
LegacyOPSepoliaChainName = "optimism-sepolia"

BobaSepoliaSuperchainName = "boba-boba-sepolia"
)

var All = []string{
Expand All @@ -31,7 +39,31 @@ var All = []string{
GnosisChainName,
ChiadoChainName,

OptimismMainnetChainName,
OptimismSepoliaChainName,
OPMainnetChainName,
OPSepoliaChainName,
OPGoerliChainName,

BobaSepoliaChainName,
}

func HandleLegacyName(name string) string {
switch name {
case LegacyOPGoerliChainName:
return OPGoerliChainName
case LegacyOPSepoliaChainName:
return OPSepoliaChainName
case LegacyOPMainnetChainName:
return OPMainnetChainName
default:
return name
}
}

func HandleBobaSuperchainName(name string) string {
switch name {
case BobaSepoliaChainName:
return BobaSepoliaSuperchainName
default:
return name
}
}
9 changes: 6 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/ledgerwatch/erigon

go 1.20
go 1.21

require (
github.com/erigontech/mdbx-go v0.27.21
Expand Down Expand Up @@ -33,6 +33,7 @@ require (
github.com/dop251/goja v0.0.0-20220405120441-9037c2b61cbf
github.com/edsrzf/mmap-go v1.1.0
github.com/emicklei/dot v1.6.0
github.com/ethereum-optimism/superchain-registry/superchain v0.0.0-20240201223137-d57c2429e4fc
github.com/fjl/gencodec v0.0.0-20220412091415-8bb9e558978c
github.com/gballet/go-verkle v0.0.0-20221121182333-31427a1f2d35
github.com/gfx-labs/sse v0.0.0-20231226060816-f747e26a9baa
Expand Down Expand Up @@ -87,8 +88,8 @@ require (
github.com/vektah/gqlparser/v2 v2.5.10
github.com/xsleonard/go-merkle v1.1.0
go.uber.org/zap v1.26.0
golang.org/x/crypto v0.17.0
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
golang.org/x/crypto v0.18.0
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa
golang.org/x/net v0.19.0
golang.org/x/sync v0.6.0
golang.org/x/sys v0.16.0
Expand Down Expand Up @@ -300,3 +301,5 @@ require (
replace github.com/ledgerwatch/erigon-lib => ./erigon-lib

replace github.com/tendermint/tendermint => github.com/bnb-chain/tendermint v0.31.12

replace github.com/ethereum-optimism/superchain-registry/superchain => github.com/bobanetwork/superchain-registry/superchain v0.0.0-20240223212802-3cf921f7ab5c
Loading

0 comments on commit 55b4885

Please sign in to comment.