From 6b41815cb5d7c2386ac95952bacea756546a18bf Mon Sep 17 00:00:00 2001 From: Joshua Gutow Date: Mon, 5 Dec 2022 10:52:29 -0800 Subject: [PATCH] Add bedrock fork block & overrides (#31) This enables two overrides: the bedrock fork block & the optimism config being set. The optimism override uses the default EIP 1559 parameters. These make it easy to setup a node in optimism mode & have a configurable bedrock block. All of the rules accessors are also helpful for checking which mode the node is running in. --- cmd/geth/config.go | 9 +++++++++ cmd/geth/main.go | 2 ++ cmd/utils/flags.go | 10 ++++++++++ core/genesis.go | 14 ++++++++++++++ eth/backend.go | 6 ++++++ eth/ethconfig/config.go | 4 ++++ params/config.go | 34 ++++++++++++++++++++++++++++++++++ 7 files changed, 79 insertions(+) diff --git a/cmd/geth/config.go b/cmd/geth/config.go index 0b856d1c1b7a..546aa4412c5b 100644 --- a/cmd/geth/config.go +++ b/cmd/geth/config.go @@ -162,6 +162,15 @@ func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) { v := ctx.Uint64(utils.OverrideShanghai.Name) cfg.Eth.OverrideShanghai = &v } + + if ctx.IsSet(utils.OverrideOptimismBedrock.Name) { + cfg.Eth.OverrideOptimismBedrock = flags.GlobalBig(ctx, utils.OverrideOptimismBedrock.Name) + } + if ctx.IsSet(utils.OverrideOptimism.Name) { + override := ctx.Bool(utils.OverrideOptimism.Name) + cfg.Eth.OverrideOptimism = &override + } + backend, eth := utils.RegisterEthService(stack, &cfg.Eth) // Configure log filter RPC API. diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 757526add7bd..47b354b22180 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -66,6 +66,8 @@ var ( utils.SmartCardDaemonPathFlag, utils.OverrideShanghai, utils.EnablePersonal, + utils.OverrideOptimismBedrock, + utils.OverrideOptimism, utils.EthashCacheDirFlag, utils.EthashCachesInMemoryFlag, utils.EthashCachesOnDiskFlag, diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 95873327078b..e5ed68f218bc 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -274,6 +274,16 @@ var ( Usage: "Manually specify the Shanghai fork timestamp, overriding the bundled setting", Category: flags.EthCategory, } + OverrideOptimismBedrock = &flags.BigFlag{ + Name: "override.bedrock", + Usage: "Manually specify OptimsimBedrock, overriding the bundled setting", + Category: flags.EthCategory, + } + OverrideOptimism = &cli.BoolFlag{ + Name: "override.optimism", + Usage: "Manually specify optimism", + Category: flags.EthCategory, + } // Light server and client settings LightServeFlag = &cli.IntFlag{ Name: "light.serve", diff --git a/core/genesis.go b/core/genesis.go index 269c1486d31d..f31ffd974e9f 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -268,6 +268,9 @@ func (e *GenesisMismatchError) Error() string { // ChainOverrides contains the changes to chain config. type ChainOverrides struct { OverrideShanghai *uint64 + // optimism + OverrideOptimismBedrock *big.Int + OverrideOptimism *bool } // SetupGenesisBlock writes or updates the genesis block in db. @@ -296,6 +299,17 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, triedb *trie.Database, gen if overrides != nil && overrides.OverrideShanghai != nil { config.ShanghaiTime = overrides.OverrideShanghai } + if overrides != nil && overrides.OverrideOptimismBedrock != nil { + config.BedrockBlock = overrides.OverrideOptimismBedrock + } + if overrides != nil && overrides.OverrideOptimism != nil { + if *overrides.OverrideOptimism { + config.Optimism = ¶ms.OptimismConfig{ + EIP1559Elasticity: 10, + EIP1559Denominator: 50, + } + } + } } } // Just commit the new block if there is no stored genesis block. diff --git a/eth/backend.go b/eth/backend.go index fc4e9a1e015d..35274e390728 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -203,6 +203,12 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { if config.OverrideShanghai != nil { overrides.OverrideShanghai = config.OverrideShanghai } + if config.OverrideOptimismBedrock != nil { + overrides.OverrideOptimismBedrock = config.OverrideOptimismBedrock + } + if config.OverrideOptimism != nil { + overrides.OverrideOptimism = config.OverrideOptimism + } eth.blockchain, err = core.NewBlockChain(chainDb, cacheConfig, config.Genesis, &overrides, eth.engine, vmConfig, eth.shouldPreserve, &config.TxLookupLimit) if err != nil { return nil, err diff --git a/eth/ethconfig/config.go b/eth/ethconfig/config.go index 722ffd109711..98b1af2fdb86 100644 --- a/eth/ethconfig/config.go +++ b/eth/ethconfig/config.go @@ -18,6 +18,7 @@ package ethconfig import ( + "math/big" "os" "os/user" "path/filepath" @@ -208,6 +209,9 @@ type Config struct { // OverrideShanghai (TODO: remove after the fork) OverrideShanghai *uint64 `toml:",omitempty"` + OverrideOptimismBedrock *big.Int + OverrideOptimism *bool + RollupSequencerHTTP string RollupHistoricalRPC string RollupDisableTxPoolGossip bool diff --git a/params/config.go b/params/config.go index b65ef0203231..00dfd32fce17 100644 --- a/params/config.go +++ b/params/config.go @@ -342,6 +342,15 @@ var ( Clique: nil, } TestRules = TestChainConfig.Rules(new(big.Int), false, 0) + + // This is an Optimism chain config with bedrock starting a block 5, introduced for historical endpoint testing, largely based on the clique config + AllOptimismProtocolChanges = func() *ChainConfig { + conf := *AllCliqueProtocolChanges // copy the config + conf.Clique = nil + conf.BedrockBlock = big.NewInt(5) + conf.Optimism = &OptimismConfig{EIP1559Elasticity: 50, EIP1559Denominator: 10} + return &conf + }() ) // NetworkNames are user friendly names to use in the chain spec banner. @@ -437,6 +446,8 @@ type ChainConfig struct { CancunTime *uint64 `json:"cancunTime,omitempty"` // Cancun switch time (nil = no fork, 0 = already on cancun) PragueTime *uint64 `json:"pragueTime,omitempty"` // Prague switch time (nil = no fork, 0 = already on prague) + BedrockBlock *big.Int `json:"bedrockBlock,omitempty"` // Bedrock switch block (nil = no fork, 0 = already on optimism bedrock) + // TerminalTotalDifficulty is the amount of total difficulty reached by // the network that triggers the consensus upgrade. TerminalTotalDifficulty *big.Int `json:"terminalTotalDifficulty,omitempty"` @@ -670,6 +681,26 @@ func (c *ChainConfig) IsPrague(time uint64) bool { return isTimestampForked(c.PragueTime, time) } +// IsBedrock returns whether num is either equal to the Bedrock fork block or greater. +func (c *ChainConfig) IsBedrock(num *big.Int) bool { + return isBlockForked(c.BedrockBlock, num) +} + +// IsOptimism returns whether the node is an optimism node or not. +func (c *ChainConfig) IsOptimism() bool { + return c.Optimism != nil +} + +// IsOptimismBedrock returns true iff this is an optimism node & bedrock is active +func (c *ChainConfig) IsOptimismBedrock(num *big.Int) bool { + return c.IsOptimism() && c.IsBedrock(num) +} + +// IsOptimismPreBedrock returns true iff this is an optimism node & bedrock is not yet active +func (c *ChainConfig) IsOptimismPreBedrock(num *big.Int) bool { + return c.IsOptimism() && !c.IsBedrock(num) +} + // CheckCompatible checks whether scheduled fork transitions have been imported // with a mismatching chain configuration. func (c *ChainConfig) CheckCompatible(newcfg *ChainConfig, height uint64, time uint64) *ConfigCompatError { @@ -978,6 +1009,7 @@ type Rules struct { IsByzantium, IsConstantinople, IsPetersburg, IsIstanbul bool IsBerlin, IsLondon bool IsMerge, IsShanghai, isCancun, isPrague bool + IsOptimismBedrock bool } // Rules ensures c's ChainID is not nil. @@ -1002,5 +1034,7 @@ func (c *ChainConfig) Rules(num *big.Int, isMerge bool, timestamp uint64) Rules IsShanghai: c.IsShanghai(timestamp), isCancun: c.IsCancun(timestamp), isPrague: c.IsPrague(timestamp), + // Optimism + IsOptimismBedrock: c.IsOptimismBedrock(num), } }