Skip to content

Commit

Permalink
Merge pull request #148 from ethereum-optimism/chain-config-db
Browse files Browse the repository at this point in the history
superchain: beta feature flag for overriding chain-config with superchain-registry
  • Loading branch information
sebastianst authored Oct 4, 2023
2 parents 1e6910b + 80ac0fa commit 1296a2c
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 16 deletions.
1 change: 1 addition & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ var (
utils.RollupDisableTxPoolGossipFlag,
utils.RollupComputePendingBlock,
utils.RollupHaltOnIncompatibleProtocolVersionFlag,
utils.RollupSuperchainUpgradesFlag,
configFileFlag,
}, utils.NetworkFlags, utils.DatabasePathFlags)

Expand Down
6 changes: 6 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,11 @@ var (
Usage: "Opt-in option to halt on incompatible protocol version requirements of the given level (major/minor/patch/none), as signaled through the Engine API by the rollup node",
Category: flags.RollupCategory,
}
RollupSuperchainUpgradesFlag = &cli.BoolFlag{
Name: "beta.rollup.superchain-upgrades",
Usage: "Beta feature: apply superchain-registry config changes to the local chain-configuration",
Category: flags.EthCategory,
}

// Metrics flags
MetricsEnabledFlag = &cli.BoolFlag{
Expand Down Expand Up @@ -1884,6 +1889,7 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
cfg.RollupDisableTxPoolGossip = ctx.Bool(RollupDisableTxPoolGossipFlag.Name)
cfg.RollupDisableTxPoolAdmission = cfg.RollupSequencerHTTP != "" && !ctx.Bool(RollupEnableTxPoolAdmissionFlag.Name)
cfg.RollupHaltOnIncompatibleProtocolVersion = ctx.String(RollupHaltOnIncompatibleProtocolVersionFlag.Name)
cfg.ApplySuperchainUpgrades = ctx.Bool(RollupSuperchainUpgradesFlag.Name)
// Override any default configs for hard coded networks.
switch {
case ctx.Bool(MainnetFlag.Name):
Expand Down
30 changes: 15 additions & 15 deletions core/gen_genesis.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 17 additions & 1 deletion core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"math/big"
"strings"

"github.com/ethereum-optimism/superchain-registry/superchain"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
Expand Down Expand Up @@ -288,7 +290,8 @@ type ChainOverrides struct {
OverrideCancun *uint64
OverrideVerkle *uint64
// optimism
OverrideOptimismCanyon *uint64
OverrideOptimismCanyon *uint64
ApplySuperchainUpgrades bool
}

// SetupGenesisBlock writes or updates the genesis block in db.
Expand All @@ -314,6 +317,19 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, triedb *trie.Database, gen
}
applyOverrides := func(config *params.ChainConfig) {
if config != nil {
// If applying the superchain-registry to a known OP-Stack chain,
// then override the local chain-config with that from the registry.
if overrides != nil && overrides.ApplySuperchainUpgrades && config.IsOptimism() && config.ChainID != nil && genesis.Config.ChainID.IsUint64() {
if _, ok := superchain.OPChains[config.ChainID.Uint64()]; ok {
conf, err := params.LoadOPStackChainConfig(config.ChainID.Uint64())
if err != nil {
log.Warn("failed to load chain config from superchain-registry, skipping override", "err", err, "chain_id", config.ChainID)
} else {
*config = *conf
}
}
}

if config.IsOptimism() && config.ChainID != nil && config.ChainID.Cmp(big.NewInt(params.OPGoerliChainID)) == 0 {
// Apply Optimism Goerli regolith time
config.RegolithTime = &params.OptimismGoerliRegolithTime
Expand Down
37 changes: 37 additions & 0 deletions core/superchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"testing"

"github.com/ethereum-optimism/superchain-registry/superchain"

"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/trie"
)

func TestOPStackGenesis(t *testing.T) {
Expand All @@ -15,3 +18,37 @@ func TestOPStackGenesis(t *testing.T) {
t.Logf("chain: %d, genesis block hash: %s", id, gen.ToBlock().Hash())
}
}

func TestRegistryChainConfigOverride(t *testing.T) {
db := rawdb.NewMemoryDatabase()
genesis, err := LoadOPStackGenesis(10)
if err != nil {
t.Fatal(err)
}
if genesis.Config.RegolithTime == nil {
t.Fatal("expected non-nil regolith time")
}
expectedRegolithTime := *genesis.Config.RegolithTime
genesis.Config.RegolithTime = nil

// initialize the DB
tdb := trie.NewDatabase(db, newDbConfig(rawdb.PathScheme))
genesis.MustCommit(db, tdb)
bl := genesis.ToBlock()
rawdb.WriteCanonicalHash(db, bl.Hash(), 0)
rawdb.WriteBlock(db, bl)

// create chain config, even with incomplete genesis input: the chain config should be corrected
chainConfig, _, err := SetupGenesisBlockWithOverride(db, tdb, genesis, &ChainOverrides{
ApplySuperchainUpgrades: true,
})
if err != nil {
t.Fatal(err)
}
// check if we have a corrected chain config
if chainConfig.RegolithTime == nil {
t.Fatal("expected regolith time to be corrected, but time is still nil")
} else if *chainConfig.RegolithTime != expectedRegolithTime {
t.Fatalf("expected regolith time to be %d, but got %d", expectedRegolithTime, *chainConfig.RegolithTime)
}
}
1 change: 1 addition & 0 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
if config.OverrideOptimismCanyon != nil {
overrides.OverrideOptimismCanyon = config.OverrideOptimismCanyon
}
overrides.ApplySuperchainUpgrades = config.ApplySuperchainUpgrades
eth.blockchain, err = core.NewBlockChain(chainDb, cacheConfig, config.Genesis, &overrides, eth.engine, vmConfig, eth.shouldPreserve, &config.TransactionHistory)
if err != nil {
return nil, err
Expand Down
3 changes: 3 additions & 0 deletions eth/ethconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ type Config struct {

OverrideOptimismCanyon *uint64 `toml:",omitempty"`

// ApplySuperchainUpgrades requests the node to load chain-configuration from the superchain-registry.
ApplySuperchainUpgrades bool `toml:",omitempty"`

RollupSequencerHTTP string
RollupHistoricalRPC string
RollupHistoricalRPCTimeout time.Duration
Expand Down
6 changes: 6 additions & 0 deletions eth/ethconfig/gen_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1296a2c

Please sign in to comment.