Skip to content

Commit

Permalink
params: set default hardfork times
Browse files Browse the repository at this point in the history
  • Loading branch information
buddh0 committed Dec 12, 2023
1 parent a140a5a commit 9ac2d07
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 24 deletions.
46 changes: 25 additions & 21 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,27 +431,31 @@ func LoadChainConfig(db ethdb.Database, genesis *Genesis) (*params.ChainConfig,
return params.BSCChainConfig, params.BSCGenesisHash, nil
}

// For any block in g.Config which is nil but the same block in defaultConfig is not
// set the block in genesis config to the block in defaultConfig.
// For any block or time in g.Config which is nil but the same field in defaultConfig is not
// set the field in genesis config to the field in defaultConfig.
// Reflection is used to avoid a long series of if statements with hardcoded block names.
func (g *Genesis) setDefaultBlockValues(defaultConfig *params.ChainConfig) {
// Regex to match block names
blockRegex := regexp.MustCompile(`.*Block$`)

// Get reflect values
gConfigElem := reflect.ValueOf(g.Config).Elem()
defaultConfigElem := reflect.ValueOf(defaultConfig).Elem()

// Iterate over fields in config
for i := 0; i < gConfigElem.NumField(); i++ {
gConfigField := gConfigElem.Field(i)
defaultConfigField := defaultConfigElem.Field(i)
fieldName := gConfigElem.Type().Field(i).Name

// Use the regex to check if the field is a Block field
if gConfigField.Kind() == reflect.Ptr && blockRegex.MatchString(fieldName) {
if gConfigField.IsNil() {
gConfigField.Set(defaultConfigField)
func (g *Genesis) setDefaultHardforkValues(defaultConfig *params.ChainConfig) {
// Regex to match block names or time names
hardforkPattern := []string{`.*Block$`, `.*Time$`}

for _, pat := range hardforkPattern {
hardforkRegex := regexp.MustCompile(pat)

// Get reflect values
gConfigElem := reflect.ValueOf(g.Config).Elem()
defaultConfigElem := reflect.ValueOf(defaultConfig).Elem()

// Iterate over fields in config
for i := 0; i < gConfigElem.NumField(); i++ {
gConfigField := gConfigElem.Field(i)
defaultConfigField := defaultConfigElem.Field(i)
fieldName := gConfigElem.Type().Field(i).Name

// Use the regex to check if the field is a Block field
if gConfigField.Kind() == reflect.Ptr && hardforkRegex.MatchString(fieldName) {
if gConfigField.IsNil() {
gConfigField.Set(defaultConfigField)
}
}
}
}
Expand Down Expand Up @@ -481,7 +485,7 @@ func (g *Genesis) configOrDefault(ghash common.Hash) *params.ChainConfig {
return defaultConfig
}

g.setDefaultBlockValues(defaultConfig)
g.setDefaultHardforkValues(defaultConfig)

// BSC Parlia set up
if g.Config.Parlia == nil {
Expand Down
12 changes: 10 additions & 2 deletions core/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,9 @@ func TestConfigOrDefault(t *testing.T) {
}
}

func TestSetDefaultBlockValues(t *testing.T) {
func TestSetDefaultHardforkValues(t *testing.T) {
genesis := &Genesis{Config: &params.ChainConfig{ChainID: big.NewInt(66), HomesteadBlock: big.NewInt(11)}}
genesis.setDefaultBlockValues(params.BSCChainConfig)
genesis.setDefaultHardforkValues(params.BSCChainConfig)

// Make sure the non-nil block was not modified
if genesis.Config.HomesteadBlock.Cmp(big.NewInt(11)) != 0 {
Expand All @@ -280,6 +280,14 @@ func TestSetDefaultBlockValues(t *testing.T) {
t.Errorf("Nano block not matching: in genesis = %v , in defaultConfig = %v", genesis.Config.PlanckBlock, params.BSCChainConfig.PlanckBlock)
}

// Spot check a few times
if *genesis.Config.ShanghaiTime != *params.BSCChainConfig.ShanghaiTime {
t.Errorf("Shanghai Time not matching: in genesis = %d , in defaultConfig = %d", *genesis.Config.ShanghaiTime, *params.BSCChainConfig.ShanghaiTime)
}
if *genesis.Config.KeplerTime != *params.BSCChainConfig.KeplerTime {
t.Errorf("Kepler Time not matching: in genesis = %d , in defaultConfig = %d", *genesis.Config.KeplerTime, *params.BSCChainConfig.KeplerTime)
}

// Lastly make sure non-block fields such as ChainID have not been modified
if genesis.Config.ChainID.Cmp(big.NewInt(66)) != 0 {
t.Errorf("ChainID should not have been modified. ChainID = %v", genesis.Config.ChainID)
Expand Down
2 changes: 1 addition & 1 deletion params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ func (c *ChainConfig) CheckConfigForkOrder() error {
{name: "platoBlock", block: c.PlatoBlock},
{name: "hertzBlock", block: c.HertzBlock},
{name: "hertzfixBlock", block: c.HertzfixBlock},
{name: "shanghaiTime", timestamp: c.ShanghaiTime},
{name: "keplerTime", timestamp: c.KeplerTime},
{name: "cancunTime", timestamp: c.CancunTime, optional: true},
{name: "pragueTime", timestamp: c.PragueTime, optional: true},
{name: "verkleTime", timestamp: c.VerkleTime, optional: true},
Expand Down

0 comments on commit 9ac2d07

Please sign in to comment.