Skip to content

Commit

Permalink
Add bedrock fork block & overrides (ethereum#31)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
trianglesphere authored and protolambda committed Feb 22, 2023
1 parent ca8bbd6 commit 6b41815
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cmd/geth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ var (
utils.SmartCardDaemonPathFlag,
utils.OverrideShanghai,
utils.EnablePersonal,
utils.OverrideOptimismBedrock,
utils.OverrideOptimism,
utils.EthashCacheDirFlag,
utils.EthashCachesInMemoryFlag,
utils.EthashCachesOnDiskFlag,
Expand Down
10 changes: 10 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 14 additions & 0 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 = &params.OptimismConfig{
EIP1559Elasticity: 10,
EIP1559Denominator: 50,
}
}
}
}
}
// Just commit the new block if there is no stored genesis block.
Expand Down
6 changes: 6 additions & 0 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions eth/ethconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package ethconfig

import (
"math/big"
"os"
"os/user"
"path/filepath"
Expand Down Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand All @@ -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),
}
}

0 comments on commit 6b41815

Please sign in to comment.