Skip to content

Commit

Permalink
feat: op-program supports custom chain config
Browse files Browse the repository at this point in the history
  • Loading branch information
Qi Zhou committed Oct 4, 2024
1 parent 711bc7c commit 15703b7
Show file tree
Hide file tree
Showing 8 changed files with 15,626 additions and 30 deletions.
6 changes: 6 additions & 0 deletions op-program/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ test:
verify-sepolia: op-program-host op-program-client
env GO111MODULE=on go run ./verify/sepolia/cmd/sepolia.go --l1 $$SEPOLIA_L1URL --l1.beacon $$SEPOLIA_BEACON_URL --l2 $$SEPOLIA_L2URL --datadir /tmp/test-sepolia

verify-devnet:
cp ../.devnet/rollup.json ./chainconfig/configs
cp ../.devnet/genesis-l2.json ./chainconfig/configs
env GO111MODULE=on go run ./verify/devnet/cmd/devnet.go --l1 http://localhost:8545 --l1.beacon http://localhost:5052 --l2 http://localhost:9545 --datadir /tmp/test-devnet

capture-mainnet-genesis: op-program-host op-program-client
rm -rf "$(COMPAT_DIR)/mainnet-genesis" "$(COMPAT_DIR)/mainnet-genesis.tar.bz"
env GO111MODULE=on go run ./verify/mainnet/cmd/mainnet.go --l1 $$MAINNET_L1URL --l1.beacon $$MAINNET_BEACON_URL --l2 $$MAINNET_L2URL --datadir "$(COMPAT_DIR)/mainnet-genesis" --l1.head "0x4903424f6cc2cfba7c2bf8c8f48ca46721c963fa64b411cfee3697b781e3e5f1" --l2.start "105235063" --l2.end "105235064"
Expand Down Expand Up @@ -97,6 +102,7 @@ verify-compat: verify-sepolia-delta verify-sepolia-ecotone verify-mainnet-genesi
test \
capture-goerli-verify \
verify-sepolia \
verify-devnet \
capture-mainnet-genesis \
capture-sepolia-delta \
capture-sepolia-ecotone \
Expand Down
46 changes: 45 additions & 1 deletion op-program/chainconfig/chaincfg.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package chainconfig

import (
"embed"
"encoding/json"
"fmt"

"github.com/ethereum-optimism/optimism/op-node/rollup"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/params"
)

var OPSepoliaChainConfig, OPMainnetChainConfig *params.ChainConfig
//go:embed configs/*json
var customChainConfigFS embed.FS

var OPSepoliaChainConfig, OPMainnetChainConfig, CustomChainConfig *params.ChainConfig
var CustomRollupConfig rollup.Config

func init() {
mustLoadConfig := func(chainID uint64) *params.ChainConfig {
Expand All @@ -19,6 +26,37 @@ func init() {
}
OPSepoliaChainConfig = mustLoadConfig(11155420)
OPMainnetChainConfig = mustLoadConfig(10)

// Load custom l2 genesis and rollup config from embed FS
data, err := customChainConfigFS.ReadFile("configs/genesis-l2.json")
if err != nil {
panic(err)
}
var genesis core.Genesis
err = json.Unmarshal(data, &genesis)
if err != nil {
panic(err)
}
CustomChainConfig = genesis.Config

file, err := customChainConfigFS.Open("configs/rollup.json")
if err != nil {
panic(err)
}
dec := json.NewDecoder(file)
dec.DisallowUnknownFields()
if err := dec.Decode(&CustomRollupConfig); err != nil {
panic(err)
}
// Both configs must have the same L2 chainID
if CustomChainConfig.ChainID.Uint64() != CustomRollupConfig.L2ChainID.Uint64() {
panic(fmt.Errorf("mismatched genesis-l2.json chainid %d vs rollup.json chainid %d", CustomChainConfig.ChainID.Uint64(), CustomRollupConfig.L2ChainID.Uint64()))
}
// Do not override existing superchain registered configs
if _, err := params.LoadOPStackChainConfig(CustomChainConfig.ChainID.Uint64()); err == nil {
panic(fmt.Errorf("cannot override existing superchain registered config"))
}
L2ChainConfigsByChainID[CustomChainConfig.ChainID.Uint64()] = CustomChainConfig
}

var L2ChainConfigsByChainID = map[uint64]*params.ChainConfig{
Expand All @@ -27,6 +65,9 @@ var L2ChainConfigsByChainID = map[uint64]*params.ChainConfig{
}

func RollupConfigByChainID(chainID uint64) (*rollup.Config, error) {
if chainID == CustomRollupConfig.L2ChainID.Uint64() {
return &CustomRollupConfig, nil
}
config, err := rollup.LoadOPStackRollupConfig(chainID)
if err != nil {
return nil, fmt.Errorf("failed to get rollup config for chain ID %d: %w", chainID, err)
Expand All @@ -35,5 +76,8 @@ func RollupConfigByChainID(chainID uint64) (*rollup.Config, error) {
}

func ChainConfigByChainID(chainID uint64) (*params.ChainConfig, error) {
if chainID == CustomChainConfig.ChainID.Uint64() {
return CustomChainConfig, nil
}
return params.LoadOPStackChainConfig(chainID)
}
Loading

0 comments on commit 15703b7

Please sign in to comment.