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 Dec 5, 2022
1 parent 92c0e4e commit 1f64f84
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 18 deletions.
7 changes: 7 additions & 0 deletions cmd/geth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) {
if ctx.IsSet(utils.OverrideTerminalTotalDifficulty.Name) {
cfg.Eth.OverrideTerminalTotalDifficulty = flags.GlobalBig(ctx, utils.OverrideTerminalTotalDifficulty.Name)
}
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
}
if ctx.IsSet(utils.OverrideTerminalTotalDifficultyPassed.Name) {
override := ctx.Bool(utils.OverrideTerminalTotalDifficultyPassed.Name)
cfg.Eth.OverrideTerminalTotalDifficultyPassed = &override
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.OverrideTerminalTotalDifficulty,
utils.OverrideTerminalTotalDifficultyPassed,
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 @@ -282,6 +282,16 @@ var (
Usage: "Manually specify TerminalTotalDifficultyPassed, 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
13 changes: 13 additions & 0 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ func (e *GenesisMismatchError) Error() string {
type ChainOverrides struct {
OverrideTerminalTotalDifficulty *big.Int
OverrideTerminalTotalDifficultyPassed *bool
OverrideOptimismBedrock *big.Int
OverrideOptimism *bool
}

// SetupGenesisBlock writes or updates the genesis block in db.
Expand Down Expand Up @@ -301,6 +303,17 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, genesis *Genesis, override
if overrides != nil && overrides.OverrideTerminalTotalDifficultyPassed != nil {
config.TerminalTotalDifficultyPassed = *overrides.OverrideTerminalTotalDifficultyPassed
}
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,
}
}
}
}
}

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.OverrideTerminalTotalDifficulty != nil {
overrides.OverrideTerminalTotalDifficulty = config.OverrideTerminalTotalDifficulty
}
if config.OverrideOptimismBedrock != nil {
overrides.OverrideOptimismBedrock = config.OverrideOptimismBedrock
}
if config.OverrideOptimism != nil {
overrides.OverrideOptimism = config.OverrideOptimism
}
if config.OverrideTerminalTotalDifficultyPassed != nil {
overrides.OverrideTerminalTotalDifficultyPassed = config.OverrideTerminalTotalDifficultyPassed
}
Expand Down
3 changes: 3 additions & 0 deletions eth/ethconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ type Config struct {
// OverrideTerminalTotalDifficultyPassed (TODO: remove after the fork)
OverrideTerminalTotalDifficultyPassed *bool `toml:",omitempty"`

OverrideOptimismBedrock *big.Int
OverrideOptimism *bool

// SyncTarget defines the target block of sync. It's only used for
// development purposes.
SyncTarget *types.Block
Expand Down
62 changes: 44 additions & 18 deletions params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,17 +271,20 @@ var (
//
// This configuration is intentionally not using keyed fields to force anyone
// adding flags to the config to also have to set these fields.
AllEthashProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, nil, false, new(EthashConfig), nil, nil}
AllEthashProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, big.NewInt(0), nil, false, new(EthashConfig), nil, nil}

// AllCliqueProtocolChanges contains every protocol change (EIPs) introduced
// and accepted by the Ethereum core developers into the Clique consensus.
//
// This configuration is intentionally not using keyed fields to force anyone
// adding flags to the config to also have to set these fields.
AllCliqueProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, nil, nil, nil, false, nil, &CliqueConfig{Period: 0, Epoch: 30000}, nil}
AllCliqueProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, nil, nil, big.NewInt(0), nil, false, nil, &CliqueConfig{Period: 0, Epoch: 30000}, nil}

TestChainConfig = &ChainConfig{big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, nil, false, new(EthashConfig), nil, nil}
NonActivatedConfig = &ChainConfig{big.NewInt(1), nil, nil, false, nil, common.Hash{}, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, false, new(EthashConfig), nil, nil}
// This is a optimism config with bedrock starting a block 5
AllOptimismProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, big.NewInt(5), nil, false, nil, nil, &OptimismConfig{EIP1559Elasticity: 50, EIP1559Denominator: 10}}

TestChainConfig = &ChainConfig{big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, big.NewInt(0), nil, false, new(EthashConfig), nil, nil}
NonActivatedConfig = &ChainConfig{big.NewInt(1), nil, nil, false, nil, common.Hash{}, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, big.NewInt(0), nil, false, new(EthashConfig), nil, nil}
TestRules = TestChainConfig.Rules(new(big.Int), false)
)

Expand Down Expand Up @@ -374,6 +377,7 @@ type ChainConfig struct {
MergeNetsplitBlock *big.Int `json:"mergeNetsplitBlock,omitempty"` // Virtual fork after The Merge to use as a network splitter
ShanghaiBlock *big.Int `json:"shanghaiBlock,omitempty"` // Shanghai switch block (nil = no fork, 0 = already on shanghai)
CancunBlock *big.Int `json:"cancunBlock,omitempty"` // Cancun switch block (nil = no fork, 0 = already on cancun)
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.
Expand Down Expand Up @@ -594,6 +598,26 @@ func (c *ChainConfig) IsCancun(num *big.Int) bool {
return isForked(c.CancunBlock, num)
}

// IsBedrock returns whether num is either equal to the Bedrock fork block or greater.
func (c *ChainConfig) IsBedrock(num *big.Int) bool {
return isForked(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) *ConfigCompatError {
Expand Down Expand Up @@ -808,6 +832,7 @@ type Rules struct {
IsByzantium, IsConstantinople, IsPetersburg, IsIstanbul bool
IsBerlin, IsLondon bool
IsMerge, IsShanghai, isCancun bool
IsOptimismBedrock bool
}

// Rules ensures c's ChainID is not nil.
Expand All @@ -817,19 +842,20 @@ func (c *ChainConfig) Rules(num *big.Int, isMerge bool) Rules {
chainID = new(big.Int)
}
return Rules{
ChainID: new(big.Int).Set(chainID),
IsHomestead: c.IsHomestead(num),
IsEIP150: c.IsEIP150(num),
IsEIP155: c.IsEIP155(num),
IsEIP158: c.IsEIP158(num),
IsByzantium: c.IsByzantium(num),
IsConstantinople: c.IsConstantinople(num),
IsPetersburg: c.IsPetersburg(num),
IsIstanbul: c.IsIstanbul(num),
IsBerlin: c.IsBerlin(num),
IsLondon: c.IsLondon(num),
IsMerge: isMerge,
IsShanghai: c.IsShanghai(num),
isCancun: c.IsCancun(num),
ChainID: new(big.Int).Set(chainID),
IsHomestead: c.IsHomestead(num),
IsEIP150: c.IsEIP150(num),
IsEIP155: c.IsEIP155(num),
IsEIP158: c.IsEIP158(num),
IsByzantium: c.IsByzantium(num),
IsConstantinople: c.IsConstantinople(num),
IsPetersburg: c.IsPetersburg(num),
IsIstanbul: c.IsIstanbul(num),
IsBerlin: c.IsBerlin(num),
IsLondon: c.IsLondon(num),
IsMerge: isMerge,
IsShanghai: c.IsShanghai(num),
isCancun: c.IsCancun(num),
IsOptimismBedrock: c.IsOptimismBedrock(num),
}
}

0 comments on commit 1f64f84

Please sign in to comment.