core: use reflection to set default block values

This commit is contained in:
Mister-EA 2023-06-15 10:31:42 +02:00
parent 3dca640303
commit 73397217e2
2 changed files with 82 additions and 75 deletions

@ -23,6 +23,8 @@ import (
"errors" "errors"
"fmt" "fmt"
"math/big" "math/big"
"reflect"
"regexp"
"strings" "strings"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -246,6 +248,32 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, genesis *Genesis, override
return newcfg, stored, nil return newcfg, stored, 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.
// 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)
}
}
}
}
// Hard fork block height specified in config.toml has higher priority, but // Hard fork block height specified in config.toml has higher priority, but
// if it is not specified in config.toml, use the default height in code. // if it is not specified in config.toml, use the default height in code.
func (g *Genesis) configOrDefault(ghash common.Hash) *params.ChainConfig { func (g *Genesis) configOrDefault(ghash common.Hash) *params.ChainConfig {
@ -270,81 +298,7 @@ func (g *Genesis) configOrDefault(ghash common.Hash) *params.ChainConfig {
return defaultConfig return defaultConfig
} }
// if not set in config.toml, use the default value of each network g.setDefaultBlockValues(defaultConfig)
if g.Config.ChainID == nil {
g.Config.ChainID = defaultConfig.ChainID
}
if g.Config.HomesteadBlock == nil {
g.Config.HomesteadBlock = defaultConfig.HomesteadBlock
}
if g.Config.EIP150Block == nil {
g.Config.EIP150Block = defaultConfig.EIP150Block
}
if g.Config.EIP155Block == nil {
g.Config.EIP155Block = defaultConfig.EIP155Block
}
if g.Config.EIP158Block == nil {
g.Config.EIP158Block = defaultConfig.EIP158Block
}
if g.Config.ByzantiumBlock == nil {
g.Config.ByzantiumBlock = defaultConfig.ByzantiumBlock
}
if g.Config.ConstantinopleBlock == nil {
g.Config.ConstantinopleBlock = defaultConfig.ConstantinopleBlock
}
if g.Config.PetersburgBlock == nil {
g.Config.PetersburgBlock = defaultConfig.PetersburgBlock
}
if g.Config.IstanbulBlock == nil {
g.Config.IstanbulBlock = defaultConfig.IstanbulBlock
}
if g.Config.MuirGlacierBlock == nil {
g.Config.MuirGlacierBlock = defaultConfig.MuirGlacierBlock
}
// BSC dedicated start
if g.Config.RamanujanBlock == nil {
g.Config.RamanujanBlock = defaultConfig.RamanujanBlock
}
if g.Config.NielsBlock == nil {
g.Config.NielsBlock = defaultConfig.NielsBlock
}
if g.Config.MirrorSyncBlock == nil {
g.Config.MirrorSyncBlock = defaultConfig.MirrorSyncBlock
}
if g.Config.BrunoBlock == nil {
g.Config.BrunoBlock = defaultConfig.BrunoBlock
}
if g.Config.EulerBlock == nil {
g.Config.EulerBlock = defaultConfig.EulerBlock
}
if g.Config.NanoBlock == nil {
g.Config.NanoBlock = defaultConfig.NanoBlock
}
if g.Config.MoranBlock == nil {
g.Config.MoranBlock = defaultConfig.MoranBlock
}
if g.Config.GibbsBlock == nil {
g.Config.GibbsBlock = defaultConfig.GibbsBlock
}
if g.Config.PlanckBlock == nil {
g.Config.PlanckBlock = defaultConfig.PlanckBlock
}
if g.Config.LubanBlock == nil {
g.Config.LubanBlock = defaultConfig.LubanBlock
}
if g.Config.PlatoBlock == nil {
g.Config.PlatoBlock = defaultConfig.PlatoBlock
}
if g.Config.BerlinBlock == nil {
g.Config.BerlinBlock = defaultConfig.BerlinBlock
}
if g.Config.LondonBlock == nil {
g.Config.LondonBlock = defaultConfig.LondonBlock
}
if g.Config.HertzBlock == nil {
g.Config.HertzBlock = defaultConfig.HertzBlock
}
// BSC Parlia set up // BSC Parlia set up
if g.Config.Parlia == nil { if g.Config.Parlia == nil {

@ -182,3 +182,56 @@ func TestGenesis_Commit(t *testing.T) {
t.Errorf("inequal difficulty; stored: %v, genesisBlock: %v", stored, genesisBlock.Difficulty()) t.Errorf("inequal difficulty; stored: %v, genesisBlock: %v", stored, genesisBlock.Difficulty())
} }
} }
func TestConfigOrDefault(t *testing.T) {
defaultGenesis := DefaultGenesisBlock()
if defaultGenesis.Config.PlanckBlock != nil {
t.Errorf("initial config should have PlanckBlock = nil, but instead PlanckBlock = %v", defaultGenesis.Config.PlanckBlock)
}
gHash := params.BSCGenesisHash
config := defaultGenesis.configOrDefault(gHash)
if config.ChainID.Cmp(params.MainnetChainConfig.ChainID) != 0 {
t.Errorf("ChainID of resulting config should be %v, but is %v instead", params.BSCChainConfig.ChainID, config.ChainID)
}
if config.HomesteadBlock.Cmp(params.MainnetChainConfig.HomesteadBlock) != 0 {
t.Errorf("resulting config should have HomesteadBlock = %v, but instead is %v", params.MainnetChainConfig, config.HomesteadBlock)
}
if config.PlanckBlock == nil {
t.Errorf("resulting config should have PlanckBlock = %v , but instead is nil", params.BSCChainConfig.PlanckBlock)
}
if config.PlanckBlock.Cmp(params.BSCChainConfig.PlanckBlock) != 0 {
t.Errorf("resulting config should have PlanckBlock = %v , but instead is %v", params.BSCChainConfig.PlanckBlock, config.PlanckBlock)
}
}
func TestSetDefaultBlockValues(t *testing.T) {
genesis := &Genesis{Config: &params.ChainConfig{ChainID: big.NewInt(66), HomesteadBlock: big.NewInt(11)}}
genesis.setDefaultBlockValues(params.BSCChainConfig)
// Make sure the non-nil block was not modified
if genesis.Config.HomesteadBlock.Cmp(big.NewInt(11)) != 0 {
t.Errorf("Homestead block should not have been modified. HomesteadBlock = %v", genesis.Config.HomesteadBlock)
}
// Spot check a few blocks
if genesis.Config.NielsBlock.Cmp(params.BSCChainConfig.NielsBlock) != 0 {
t.Errorf("Niels block not matching: in genesis = %v , in defaultConfig = %v", genesis.Config.NielsBlock, params.BSCChainConfig.NielsBlock)
}
if genesis.Config.NanoBlock.Cmp(params.BSCChainConfig.NanoBlock) != 0 {
t.Errorf("Nano block not matching: in genesis = %v , in defaultConfig = %v", genesis.Config.NanoBlock, params.BSCChainConfig.NanoBlock)
}
if genesis.Config.PlanckBlock.Cmp(params.BSCChainConfig.PlanckBlock) != 0 {
t.Errorf("Nano block not matching: in genesis = %v , in defaultConfig = %v", genesis.Config.PlanckBlock, params.BSCChainConfig.PlanckBlock)
}
// 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)
}
}