Skip to content

Commit

Permalink
feat: Add support for Regolith in chain configuration (ethereum#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajsutton authored Feb 23, 2023
1 parent aea0402 commit 5ff481f
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 6 deletions.
4 changes: 4 additions & 0 deletions cmd/geth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) {
if ctx.IsSet(utils.OverrideOptimismBedrock.Name) {
cfg.Eth.OverrideOptimismBedrock = flags.GlobalBig(ctx, utils.OverrideOptimismBedrock.Name)
}
if ctx.IsSet(utils.OverrideOptimismRegolith.Name) {
v := ctx.Uint64(utils.OverrideOptimismRegolith.Name)
cfg.Eth.OverrideOptimismRegolith = &v
}
if ctx.IsSet(utils.OverrideOptimism.Name) {
override := ctx.Bool(utils.OverrideOptimism.Name)
cfg.Eth.OverrideOptimism = &override
Expand Down
1 change: 1 addition & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ var (
utils.OverrideShanghai,
utils.EnablePersonal,
utils.OverrideOptimismBedrock,
utils.OverrideOptimismRegolith,
utils.OverrideOptimism,
utils.EthashCacheDirFlag,
utils.EthashCachesInMemoryFlag,
Expand Down
5 changes: 5 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,11 @@ var (
Usage: "Manually specify OptimsimBedrock, overriding the bundled setting",
Category: flags.EthCategory,
}
OverrideOptimismRegolith = &flags.BigFlag{
Name: "override.regolith",
Usage: "Manually specify the OptimsimRegolith fork timestamp, overriding the bundled setting",
Category: flags.EthCategory,
}
OverrideOptimism = &cli.BoolFlag{
Name: "override.optimism",
Usage: "Manually specify optimism",
Expand Down
8 changes: 6 additions & 2 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,9 @@ func (e *GenesisMismatchError) Error() string {
type ChainOverrides struct {
OverrideShanghai *uint64
// optimism
OverrideOptimismBedrock *big.Int
OverrideOptimism *bool
OverrideOptimismBedrock *big.Int
OverrideOptimismRegolith *uint64
OverrideOptimism *bool
}

// SetupGenesisBlock writes or updates the genesis block in db.
Expand Down Expand Up @@ -302,6 +303,9 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, triedb *trie.Database, gen
if overrides != nil && overrides.OverrideOptimismBedrock != nil {
config.BedrockBlock = overrides.OverrideOptimismBedrock
}
if overrides != nil && overrides.OverrideOptimismRegolith != nil {
config.RegolithTime = overrides.OverrideOptimismRegolith
}
if overrides != nil && overrides.OverrideOptimism != nil {
if *overrides.OverrideOptimism {
config.Optimism = &params.OptimismConfig{
Expand Down
3 changes: 3 additions & 0 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
if config.OverrideOptimismBedrock != nil {
overrides.OverrideOptimismBedrock = config.OverrideOptimismBedrock
}
if config.OverrideOptimismRegolith != nil {
overrides.OverrideOptimismRegolith = config.OverrideOptimismRegolith
}
if config.OverrideOptimism != nil {
overrides.OverrideOptimism = config.OverrideOptimism
}
Expand Down
5 changes: 3 additions & 2 deletions eth/ethconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,9 @@ type Config struct {
// OverrideShanghai (TODO: remove after the fork)
OverrideShanghai *uint64 `toml:",omitempty"`

OverrideOptimismBedrock *big.Int
OverrideOptimism *bool
OverrideOptimismBedrock *big.Int
OverrideOptimismRegolith *uint64 `toml:",omitempty"`
OverrideOptimism *bool

RollupSequencerHTTP string
RollupHistoricalRPC string
Expand Down
17 changes: 15 additions & 2 deletions params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ type ChainConfig struct {
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)
RegolithTime *uint64 `json:"regolithTime,omitempty"` // Regolith switch time (nil = no fork, 0 = already on optimism regolith)

// TerminalTotalDifficulty is the amount of total difficulty reached by
// the network that triggers the consensus upgrade.
Expand Down Expand Up @@ -583,6 +584,9 @@ func (c *ChainConfig) Description() string {
if c.PragueTime != nil {
banner += fmt.Sprintf(" - Prague: @%-10v\n", *c.PragueTime)
}
if c.RegolithTime != nil {
banner += fmt.Sprintf(" - Regolith: @%-10v\n", *c.RegolithTime)
}
return banner
}

Expand Down Expand Up @@ -686,6 +690,10 @@ func (c *ChainConfig) IsBedrock(num *big.Int) bool {
return isBlockForked(c.BedrockBlock, num)
}

func (c *ChainConfig) IsRegolith(time uint64) bool {
return isTimestampForked(c.RegolithTime, time)
}

// IsOptimism returns whether the node is an optimism node or not.
func (c *ChainConfig) IsOptimism() bool {
return c.Optimism != nil
Expand All @@ -696,6 +704,10 @@ func (c *ChainConfig) IsOptimismBedrock(num *big.Int) bool {
return c.IsOptimism() && c.IsBedrock(num)
}

func (c *ChainConfig) IsOptimismRegolith(time uint64) bool {
return c.IsOptimism() && c.IsRegolith(time)
}

// 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)
Expand Down Expand Up @@ -1009,7 +1021,7 @@ type Rules struct {
IsByzantium, IsConstantinople, IsPetersburg, IsIstanbul bool
IsBerlin, IsLondon bool
IsMerge, IsShanghai, isCancun, isPrague bool
IsOptimismBedrock bool
IsOptimismBedrock, IsOptimismRegolith bool
}

// Rules ensures c's ChainID is not nil.
Expand All @@ -1035,6 +1047,7 @@ func (c *ChainConfig) Rules(num *big.Int, isMerge bool, timestamp uint64) Rules
isCancun: c.IsCancun(timestamp),
isPrague: c.IsPrague(timestamp),
// Optimism
IsOptimismBedrock: c.IsOptimismBedrock(num),
IsOptimismBedrock: c.IsOptimismBedrock(num),
IsOptimismRegolith: c.IsOptimismRegolith(timestamp),
}
}
19 changes: 19 additions & 0 deletions params/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,22 @@ func TestConfigRules(t *testing.T) {
t.Errorf("expected %v to be shanghai", stamp)
}
}

func TestConfigRulesReglith(t *testing.T) {
c := &ChainConfig{
RegolithTime: newUint64(500),
Optimism: &OptimismConfig{},
}
var stamp uint64
if r := c.Rules(big.NewInt(0), true, stamp); r.IsOptimismRegolith {
t.Errorf("expected %v to not be regolith", stamp)
}
stamp = 500
if r := c.Rules(big.NewInt(0), true, stamp); !r.IsOptimismRegolith {
t.Errorf("expected %v to be regolith", stamp)
}
stamp = math.MaxInt64
if r := c.Rules(big.NewInt(0), true, stamp); !r.IsOptimismRegolith {
t.Errorf("expected %v to be regolith", stamp)
}
}

0 comments on commit 5ff481f

Please sign in to comment.