From 4f7677b198a27326448693100bfde8fb5841c5f5 Mon Sep 17 00:00:00 2001 From: Darioush Jalali Date: Wed, 11 Dec 2024 14:56:39 -0800 Subject: [PATCH 01/18] refactor: params.ChainConfigExtra -> params/extras --- params/config_extra.go | 257 +-------------------- params/config_libevm.go | 21 +- params/extras/config_extra.go | 264 ++++++++++++++++++++++ params/{ => extras}/config_extra_test.go | 2 +- params/{ => extras}/network_upgrades.go | 4 +- params/{ => extras}/precompile_upgrade.go | 8 +- params/{ => extras}/precompiles.go | 2 +- params/rules_extra.go | 2 +- 8 files changed, 297 insertions(+), 263 deletions(-) create mode 100644 params/extras/config_extra.go rename params/{ => extras}/config_extra_test.go (99%) rename params/{ => extras}/network_upgrades.go (99%) rename params/{ => extras}/precompile_upgrade.go (97%) rename params/{ => extras}/precompiles.go (98%) diff --git a/params/config_extra.go b/params/config_extra.go index be1afcc316..0691607020 100644 --- a/params/config_extra.go +++ b/params/config_extra.go @@ -6,14 +6,11 @@ package params import ( "encoding/json" "errors" - "fmt" "math/big" - "github.com/ava-labs/avalanchego/snow" "github.com/ava-labs/avalanchego/upgrade" + "github.com/ava-labs/coreth/params/extras" "github.com/ava-labs/coreth/utils" - "github.com/ava-labs/libevm/common" - ethparams "github.com/ava-labs/libevm/params" ) const ( @@ -24,18 +21,7 @@ const ( IsMergeTODO = true ) -// UpgradeConfig includes the following configs that may be specified in upgradeBytes: -// - Timestamps that enable avalanche network upgrades, -// - Enabling or disabling precompiles as network upgrades. -type UpgradeConfig struct { - // Config for enabling and disabling precompiles as network upgrades. - PrecompileUpgrades []PrecompileUpgrade `json:"precompileUpgrades,omitempty"` -} - -// AvalancheContext provides Avalanche specific context directly into the EVM. -type AvalancheContext struct { - SnowCtx *snow.Context -} +type ConfigCompatError = extras.ConfigCompatError // SetEthUpgrades enables Etheruem network upgrades using the same time as // the Avalanche network upgrade that enables them. @@ -73,10 +59,10 @@ func SetEthUpgrades(c *ChainConfig) { } func GetExtra(c *ChainConfig) *ChainConfigExtra { - ex := extras.FromChainConfig(c) + ex := payloads.FromChainConfig(c) if ex == nil { ex = &ChainConfigExtra{} - extras.SetOnChainConfig(c, ex) + payloads.SetOnChainConfig(c, ex) } return ex } @@ -89,204 +75,10 @@ func Copy(c *ChainConfig) ChainConfig { // WithExtra sets the extra payload on `c` and returns the modified argument. func WithExtra(c *ChainConfig, extra *ChainConfigExtra) *ChainConfig { - extras.SetOnChainConfig(c, extra) + payloads.SetOnChainConfig(c, extra) return c } -type ChainConfigExtra struct { - NetworkUpgrades // Config for timestamps that enable network upgrades. Skip encoding/decoding directly into ChainConfig. - - AvalancheContext `json:"-"` // Avalanche specific context set during VM initialization. Not serialized. - - UpgradeConfig `json:"-"` // Config specified in upgradeBytes (avalanche network upgrades or enable/disabling precompiles). Skip encoding/decoding directly into ChainConfig. -} - -func (c *ChainConfigExtra) Description() string { - if c == nil { - return "" - } - var banner string - - banner += "Avalanche Upgrades (timestamp based):\n" - banner += c.NetworkUpgrades.Description() - banner += "\n" - - upgradeConfigBytes, err := json.Marshal(c.UpgradeConfig) - if err != nil { - upgradeConfigBytes = []byte("cannot marshal UpgradeConfig") - } - banner += fmt.Sprintf("Upgrade Config: %s", string(upgradeConfigBytes)) - banner += "\n" - return banner -} - -type fork struct { - name string - block *big.Int // some go-ethereum forks use block numbers - timestamp *uint64 // Avalanche forks use timestamps - optional bool // if true, the fork may be nil and next fork is still allowed -} - -func (c *ChainConfigExtra) CheckConfigForkOrder() error { - if c == nil { - return nil - } - // Note: In Avalanche, hard forks must take place via block timestamps instead - // of block numbers since blocks are produced asynchronously. Therefore, we do not - // check that the block timestamps in the same way as for - // the block number forks since it would not be a meaningful comparison. - // Instead, we check only that Phases are enabled in order. - // Note: we do not add the optional stateful precompile configs in here because they are optional - // and independent, such that the ordering they are enabled does not impact the correctness of the - // chain config. - if err := checkForks(c.forkOrder(), false); err != nil { - return err - } - - return nil -} - -// checkForks checks that forks are enabled in order and returns an error if not -// [blockFork] is true if the fork is a block number fork, false if it is a timestamp fork -func checkForks(forks []fork, blockFork bool) error { - lastFork := fork{} - for _, cur := range forks { - if lastFork.name != "" { - switch { - // Non-optional forks must all be present in the chain config up to the last defined fork - case lastFork.block == nil && lastFork.timestamp == nil && (cur.block != nil || cur.timestamp != nil): - if cur.block != nil { - return fmt.Errorf("unsupported fork ordering: %v not enabled, but %v enabled at block %v", - lastFork.name, cur.name, cur.block) - } else { - return fmt.Errorf("unsupported fork ordering: %v not enabled, but %v enabled at timestamp %v", - lastFork.name, cur.name, cur.timestamp) - } - - // Fork (whether defined by block or timestamp) must follow the fork definition sequence - case (lastFork.block != nil && cur.block != nil) || (lastFork.timestamp != nil && cur.timestamp != nil): - if lastFork.block != nil && lastFork.block.Cmp(cur.block) > 0 { - return fmt.Errorf("unsupported fork ordering: %v enabled at block %v, but %v enabled at block %v", - lastFork.name, lastFork.block, cur.name, cur.block) - } else if lastFork.timestamp != nil && *lastFork.timestamp > *cur.timestamp { - return fmt.Errorf("unsupported fork ordering: %v enabled at timestamp %v, but %v enabled at timestamp %v", - lastFork.name, lastFork.timestamp, cur.name, cur.timestamp) - } - - // Timestamp based forks can follow block based ones, but not the other way around - if lastFork.timestamp != nil && cur.block != nil { - return fmt.Errorf("unsupported fork ordering: %v used timestamp ordering, but %v reverted to block ordering", - lastFork.name, cur.name) - } - } - } - // If it was optional and not set, then ignore it - if !cur.optional || (cur.block != nil || cur.timestamp != nil) { - lastFork = cur - } - } - return nil -} - -func (c *ChainConfigExtra) CheckConfigCompatible(newcfg_ *ChainConfig, headNumber *big.Int, headTimestamp uint64) *ConfigCompatError { - if c == nil { - return nil - } - newcfg := GetExtra(newcfg_) - - // Check avalanche network upgrades - if err := c.checkNetworkUpgradesCompatible(&newcfg.NetworkUpgrades, headTimestamp); err != nil { - return err - } - - // Check that the precompiles on the new config are compatible with the existing precompile config. - // XXX: This is missing in master? - // if err := c.checkPrecompilesCompatible(newcfg.PrecompileUpgrades, headTimestamp); err != nil { - // return err - // } - - return nil -} - -// isForkTimestampIncompatible returns true if a fork scheduled at timestamp s1 -// cannot be rescheduled to timestamp s2 because head is already past the fork. -func isForkTimestampIncompatible(s1, s2 *uint64, head uint64) bool { - return (isTimestampForked(s1, head) || isTimestampForked(s2, head)) && !configTimestampEqual(s1, s2) -} - -// isTimestampForked returns whether a fork scheduled at timestamp s is active -// at the given head timestamp. Whilst this method is the same as isBlockForked, -// they are explicitly separate for clearer reading. -func isTimestampForked(s *uint64, head uint64) bool { - if s == nil { - return false - } - return *s <= head -} - -func configTimestampEqual(x, y *uint64) bool { - if x == nil { - return y == nil - } - if y == nil { - return x == nil - } - return *x == *y -} - -// ConfigCompatError is raised if the locally-stored blockchain is initialised with a -// ChainConfig that would alter the past. -type ConfigCompatError = ethparams.ConfigCompatError - -func newTimestampCompatError(what string, storedtime, newtime *uint64) *ConfigCompatError { - var rew *uint64 - switch { - case storedtime == nil: - rew = newtime - case newtime == nil || *storedtime < *newtime: - rew = storedtime - default: - rew = newtime - } - err := &ConfigCompatError{ - What: what, - StoredTime: storedtime, - NewTime: newtime, - RewindToTime: 0, - } - if rew != nil && *rew > 0 { - err.RewindToTime = *rew - 1 - } - return err -} - -// UnmarshalJSON parses the JSON-encoded data and stores the result in the -// object pointed to by c. -// This is a custom unmarshaler to handle the Precompiles field. -// Precompiles was presented as an inline object in the JSON. -// This custom unmarshaler ensures backwards compatibility with the old format. -func (c *ChainConfigExtra) UnmarshalJSON(data []byte) error { - // Alias ChainConfigExtra to avoid recursion - type _ChainConfigExtra ChainConfigExtra - tmp := _ChainConfigExtra{} - if err := json.Unmarshal(data, &tmp); err != nil { - return err - } - - // At this point we have populated all fields except PrecompileUpgrade - *c = ChainConfigExtra(tmp) - - return nil -} - -// MarshalJSON returns the JSON encoding of c. -// This is a custom marshaler to handle the Precompiles field. -func (c *ChainConfigExtra) MarshalJSON() ([]byte, error) { - // Alias ChainConfigExtra to avoid recursion - type _ChainConfigExtra ChainConfigExtra - return json.Marshal(_ChainConfigExtra(*c)) -} - type ChainConfigWithUpgradesJSON struct { ChainConfig UpgradeConfig UpgradeConfig `json:"upgrades,omitempty"` @@ -346,22 +138,6 @@ func (cu *ChainConfigWithUpgradesJSON) UnmarshalJSON(input []byte) error { return nil } -// Verify verifies chain config and returns error -func (c *ChainConfigExtra) Verify() error { - // Verify the precompile upgrades are internally consistent given the existing chainConfig. - if err := c.verifyPrecompileUpgrades(); err != nil { - return fmt.Errorf("invalid precompile upgrades: %w", err) - } - - return nil -} - -// IsPrecompileEnabled returns whether precompile with [address] is enabled at [timestamp]. -func (c *ChainConfigExtra) IsPrecompileEnabled(address common.Address, timestamp uint64) bool { - config := c.getActivePrecompileConfig(address, timestamp) - return config != nil && !config.IsDisabled() -} - // ToWithUpgradesJSON converts the ChainConfig to ChainConfigWithUpgradesJSON with upgrades explicitly displayed. // ChainConfig does not include upgrades in its JSON output. // This is a workaround for showing upgrades in the JSON output. @@ -389,28 +165,7 @@ func GetChainConfig(agoUpgrade upgrade.Config, chainID *big.Int) *ChainConfig { MuirGlacierBlock: big.NewInt(0), }, &ChainConfigExtra{ - NetworkUpgrades: getNetworkUpgrades(agoUpgrade), + NetworkUpgrades: extras.GetNetworkUpgrades(agoUpgrade), }, ) } - -func ptrToString(val *uint64) string { - if val == nil { - return "nil" - } - return fmt.Sprintf("%d", *val) -} - -// IsForkTransition returns true if [fork] activates during the transition from -// [parent] to [current]. -// Taking [parent] as a pointer allows for us to pass nil when checking forks -// that activate during genesis. -// Note: this works for both block number and timestamp activated forks. -func IsForkTransition(fork *uint64, parent *uint64, current uint64) bool { - var parentForked bool - if parent != nil { - parentForked = isTimestampForked(fork, *parent) - } - currentForked := isTimestampForked(fork, current) - return !parentForked && currentForked -} diff --git a/params/config_libevm.go b/params/config_libevm.go index c5db4f9db9..2f99b9896e 100644 --- a/params/config_libevm.go +++ b/params/config_libevm.go @@ -6,23 +6,38 @@ package params import ( "math/big" + "github.com/ava-labs/coreth/params/extras" "github.com/ava-labs/coreth/precompile/modules" "github.com/ava-labs/coreth/precompile/precompileconfig" "github.com/ava-labs/libevm/common" ethparams "github.com/ava-labs/libevm/params" ) +type ( + ChainConfigExtra = extras.ChainConfigExtra + NetworkUpgrades = extras.NetworkUpgrades + AvalancheContext = extras.AvalancheContext + UpgradeConfig = extras.UpgradeConfig + PrecompileUpgrade = extras.PrecompileUpgrade + AvalancheRules = extras.AvalancheRules +) + +func init() { + // XXX: Must fix this + extras.GetExtra = GetExtra +} + // libevmInit would ideally be a regular init() function, but it MUST be run // before any calls to [ChainConfig.Rules]. See `config.go` for its call site. func libevmInit() any { - extras = ethparams.RegisterExtras(ethparams.Extras[*ChainConfigExtra, RulesExtra]{ + payloads = ethparams.RegisterExtras(ethparams.Extras[*ChainConfigExtra, RulesExtra]{ ReuseJSONRoot: true, // Reuse the root JSON input when unmarshalling the extra payload. NewRules: constructRulesExtra, }) return nil } -var extras ethparams.ExtraPayloads[*ChainConfigExtra, RulesExtra] +var payloads ethparams.ExtraPayloads[*ChainConfigExtra, RulesExtra] // constructRulesExtra acts as an adjunct to the [params.ChainConfig.Rules] // method. Its primary purpose is to construct the extra payload for the @@ -39,7 +54,7 @@ func constructRulesExtra(c *ethparams.ChainConfig, r *ethparams.Rules, cEx *Chai rules.Predicaters = make(map[common.Address]precompileconfig.Predicater) rules.AccepterPrecompiles = make(map[common.Address]precompileconfig.Accepter) for _, module := range modules.RegisteredModules() { - if config := cEx.getActivePrecompileConfig(module.Address, timestamp); config != nil && !config.IsDisabled() { + if config := cEx.GetActivePrecompileConfig(module.Address, timestamp); config != nil && !config.IsDisabled() { rules.Precompiles[module.Address] = config if predicater, ok := config.(precompileconfig.Predicater); ok { rules.Predicaters[module.Address] = predicater diff --git a/params/extras/config_extra.go b/params/extras/config_extra.go new file mode 100644 index 0000000000..d94c0b3a2a --- /dev/null +++ b/params/extras/config_extra.go @@ -0,0 +1,264 @@ +// (c) 2024 Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package extras + +import ( + "encoding/json" + "fmt" + "math/big" + + "github.com/ava-labs/avalanchego/snow" + "github.com/ava-labs/libevm/common" + ethparams "github.com/ava-labs/libevm/params" +) + +// XXX: Must fix this +type ChainConfig = ethparams.ChainConfig + +// XXX: Must fix this +var GetExtra func(c *ChainConfig) *ChainConfigExtra + +// UpgradeConfig includes the following configs that may be specified in upgradeBytes: +// - Timestamps that enable avalanche network upgrades, +// - Enabling or disabling precompiles as network upgrades. +type UpgradeConfig struct { + // Config for enabling and disabling precompiles as network upgrades. + PrecompileUpgrades []PrecompileUpgrade `json:"precompileUpgrades,omitempty"` +} + +// AvalancheContext provides Avalanche specific context directly into the EVM. +type AvalancheContext struct { + SnowCtx *snow.Context +} + +type ChainConfigExtra struct { + NetworkUpgrades // Config for timestamps that enable network upgrades. Skip encoding/decoding directly into ChainConfig. + + AvalancheContext `json:"-"` // Avalanche specific context set during VM initialization. Not serialized. + + UpgradeConfig `json:"-"` // Config specified in upgradeBytes (avalanche network upgrades or enable/disabling precompiles). Skip encoding/decoding directly into ChainConfig. +} + +func (c *ChainConfigExtra) CheckConfigCompatible(newcfg_ *ChainConfig, headNumber *big.Int, headTimestamp uint64) *ConfigCompatError { + if c == nil { + return nil + } + newcfg := GetExtra(newcfg_) + + // Check avalanche network upgrades + if err := c.checkNetworkUpgradesCompatible(&newcfg.NetworkUpgrades, headTimestamp); err != nil { + return err + } + + // Check that the precompiles on the new config are compatible with the existing precompile config. + // XXX: This is missing in master? + // if err := c.checkPrecompilesCompatible(newcfg.PrecompileUpgrades, headTimestamp); err != nil { + // return err + // } + + return nil +} + +func (c *ChainConfigExtra) Description() string { + if c == nil { + return "" + } + var banner string + + banner += "Avalanche Upgrades (timestamp based):\n" + banner += c.NetworkUpgrades.Description() + banner += "\n" + + upgradeConfigBytes, err := json.Marshal(c.UpgradeConfig) + if err != nil { + upgradeConfigBytes = []byte("cannot marshal UpgradeConfig") + } + banner += fmt.Sprintf("Upgrade Config: %s", string(upgradeConfigBytes)) + banner += "\n" + return banner +} + +// isForkTimestampIncompatible returns true if a fork scheduled at timestamp s1 +// cannot be rescheduled to timestamp s2 because head is already past the fork. +func isForkTimestampIncompatible(s1, s2 *uint64, head uint64) bool { + return (isTimestampForked(s1, head) || isTimestampForked(s2, head)) && !configTimestampEqual(s1, s2) +} + +// isTimestampForked returns whether a fork scheduled at timestamp s is active +// at the given head timestamp. Whilst this method is the same as isBlockForked, +// they are explicitly separate for clearer reading. +func isTimestampForked(s *uint64, head uint64) bool { + if s == nil { + return false + } + return *s <= head +} + +func configTimestampEqual(x, y *uint64) bool { + if x == nil { + return y == nil + } + if y == nil { + return x == nil + } + return *x == *y +} + +// ConfigCompatError is raised if the locally-stored blockchain is initialised with a +// ChainConfig that would alter the past. +type ConfigCompatError = ethparams.ConfigCompatError + +func newTimestampCompatError(what string, storedtime, newtime *uint64) *ConfigCompatError { + var rew *uint64 + switch { + case storedtime == nil: + rew = newtime + case newtime == nil || *storedtime < *newtime: + rew = storedtime + default: + rew = newtime + } + err := &ConfigCompatError{ + What: what, + StoredTime: storedtime, + NewTime: newtime, + RewindToTime: 0, + } + if rew != nil && *rew > 0 { + err.RewindToTime = *rew - 1 + } + return err +} + +// UnmarshalJSON parses the JSON-encoded data and stores the result in the +// object pointed to by c. +// This is a custom unmarshaler to handle the Precompiles field. +// Precompiles was presented as an inline object in the JSON. +// This custom unmarshaler ensures backwards compatibility with the old format. +func (c *ChainConfigExtra) UnmarshalJSON(data []byte) error { + // Alias ChainConfigExtra to avoid recursion + type _ChainConfigExtra ChainConfigExtra + tmp := _ChainConfigExtra{} + if err := json.Unmarshal(data, &tmp); err != nil { + return err + } + + // At this point we have populated all fields except PrecompileUpgrade + *c = ChainConfigExtra(tmp) + + return nil +} + +// MarshalJSON returns the JSON encoding of c. +// This is a custom marshaler to handle the Precompiles field. +func (c *ChainConfigExtra) MarshalJSON() ([]byte, error) { + // Alias ChainConfigExtra to avoid recursion + type _ChainConfigExtra ChainConfigExtra + return json.Marshal(_ChainConfigExtra(*c)) +} + +type fork struct { + name string + block *big.Int // some go-ethereum forks use block numbers + timestamp *uint64 // Avalanche forks use timestamps + optional bool // if true, the fork may be nil and next fork is still allowed +} + +func (c *ChainConfigExtra) CheckConfigForkOrder() error { + if c == nil { + return nil + } + // Note: In Avalanche, hard forks must take place via block timestamps instead + // of block numbers since blocks are produced asynchronously. Therefore, we do not + // check that the block timestamps in the same way as for + // the block number forks since it would not be a meaningful comparison. + // Instead, we check only that Phases are enabled in order. + // Note: we do not add the optional stateful precompile configs in here because they are optional + // and independent, such that the ordering they are enabled does not impact the correctness of the + // chain config. + if err := checkForks(c.forkOrder(), false); err != nil { + return err + } + + return nil +} + +// checkForks checks that forks are enabled in order and returns an error if not +// [blockFork] is true if the fork is a block number fork, false if it is a timestamp fork +func checkForks(forks []fork, blockFork bool) error { + lastFork := fork{} + for _, cur := range forks { + if lastFork.name != "" { + switch { + // Non-optional forks must all be present in the chain config up to the last defined fork + case lastFork.block == nil && lastFork.timestamp == nil && (cur.block != nil || cur.timestamp != nil): + if cur.block != nil { + return fmt.Errorf("unsupported fork ordering: %v not enabled, but %v enabled at block %v", + lastFork.name, cur.name, cur.block) + } else { + return fmt.Errorf("unsupported fork ordering: %v not enabled, but %v enabled at timestamp %v", + lastFork.name, cur.name, cur.timestamp) + } + + // Fork (whether defined by block or timestamp) must follow the fork definition sequence + case (lastFork.block != nil && cur.block != nil) || (lastFork.timestamp != nil && cur.timestamp != nil): + if lastFork.block != nil && lastFork.block.Cmp(cur.block) > 0 { + return fmt.Errorf("unsupported fork ordering: %v enabled at block %v, but %v enabled at block %v", + lastFork.name, lastFork.block, cur.name, cur.block) + } else if lastFork.timestamp != nil && *lastFork.timestamp > *cur.timestamp { + return fmt.Errorf("unsupported fork ordering: %v enabled at timestamp %v, but %v enabled at timestamp %v", + lastFork.name, lastFork.timestamp, cur.name, cur.timestamp) + } + + // Timestamp based forks can follow block based ones, but not the other way around + if lastFork.timestamp != nil && cur.block != nil { + return fmt.Errorf("unsupported fork ordering: %v used timestamp ordering, but %v reverted to block ordering", + lastFork.name, cur.name) + } + } + } + // If it was optional and not set, then ignore it + if !cur.optional || (cur.block != nil || cur.timestamp != nil) { + lastFork = cur + } + } + return nil +} + +// Verify verifies chain config and returns error +func (c *ChainConfigExtra) Verify() error { + // Verify the precompile upgrades are internally consistent given the existing chainConfig. + if err := c.verifyPrecompileUpgrades(); err != nil { + return fmt.Errorf("invalid precompile upgrades: %w", err) + } + + return nil +} + +// IsPrecompileEnabled returns whether precompile with [address] is enabled at [timestamp]. +func (c *ChainConfigExtra) IsPrecompileEnabled(address common.Address, timestamp uint64) bool { + config := c.GetActivePrecompileConfig(address, timestamp) + return config != nil && !config.IsDisabled() +} + +// IsForkTransition returns true if [fork] activates during the transition from +// [parent] to [current]. +// Taking [parent] as a pointer allows for us to pass nil when checking forks +// that activate during genesis. +// Note: this works for both block number and timestamp activated forks. +func IsForkTransition(fork *uint64, parent *uint64, current uint64) bool { + var parentForked bool + if parent != nil { + parentForked = isTimestampForked(fork, *parent) + } + currentForked := isTimestampForked(fork, current) + return !parentForked && currentForked +} + +func ptrToString(val *uint64) string { + if val == nil { + return "nil" + } + return fmt.Sprintf("%d", *val) +} diff --git a/params/config_extra_test.go b/params/extras/config_extra_test.go similarity index 99% rename from params/config_extra_test.go rename to params/extras/config_extra_test.go index 64438b7f07..a94264cf31 100644 --- a/params/config_extra_test.go +++ b/params/extras/config_extra_test.go @@ -1,7 +1,7 @@ // (c) 2024 Ava Labs, Inc. All rights reserved. // See the file LICENSE for licensing terms. -package params +package extras import ( "testing" diff --git a/params/network_upgrades.go b/params/extras/network_upgrades.go similarity index 99% rename from params/network_upgrades.go rename to params/extras/network_upgrades.go index 72e9b4e29b..6488469d23 100644 --- a/params/network_upgrades.go +++ b/params/extras/network_upgrades.go @@ -1,7 +1,7 @@ // (c) 2022, Ava Labs, Inc. All rights reserved. // See the file LICENSE for licensing terms. -package params +package extras import ( "fmt" @@ -194,7 +194,7 @@ func (n NetworkUpgrades) Description() string { return banner } -func getNetworkUpgrades(agoUpgrade upgrade.Config) NetworkUpgrades { +func GetNetworkUpgrades(agoUpgrade upgrade.Config) NetworkUpgrades { return NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.TimeToNewUint64(agoUpgrade.ApricotPhase1Time), ApricotPhase2BlockTimestamp: utils.TimeToNewUint64(agoUpgrade.ApricotPhase2Time), diff --git a/params/precompile_upgrade.go b/params/extras/precompile_upgrade.go similarity index 97% rename from params/precompile_upgrade.go rename to params/extras/precompile_upgrade.go index 7a7c3b07b5..e27c746632 100644 --- a/params/precompile_upgrade.go +++ b/params/extras/precompile_upgrade.go @@ -1,7 +1,7 @@ // (c) 2023 Ava Labs, Inc. All rights reserved. // See the file LICENSE for licensing terms. -package params +package extras import ( "encoding/json" @@ -131,9 +131,9 @@ func (c *ChainConfigExtra) verifyPrecompileUpgrades() error { return nil } -// getActivePrecompileConfig returns the most recent precompile config corresponding to [address]. +// GetActivePrecompileConfig returns the most recent precompile config corresponding to [address]. // If none have occurred, returns nil. -func (c *ChainConfigExtra) getActivePrecompileConfig(address common.Address, timestamp uint64) precompileconfig.Config { +func (c *ChainConfigExtra) GetActivePrecompileConfig(address common.Address, timestamp uint64) precompileconfig.Config { configs := c.GetActivatingPrecompileConfigs(address, nil, timestamp, c.PrecompileUpgrades) if len(configs) == 0 { return nil @@ -229,7 +229,7 @@ func (c *ChainConfigExtra) checkPrecompileCompatible(address common.Address, pre func (c *ChainConfigExtra) EnabledStatefulPrecompiles(blockTimestamp uint64) Precompiles { statefulPrecompileConfigs := make(Precompiles) for _, module := range modules.RegisteredModules() { - if config := c.getActivePrecompileConfig(module.Address, blockTimestamp); config != nil && !config.IsDisabled() { + if config := c.GetActivePrecompileConfig(module.Address, blockTimestamp); config != nil && !config.IsDisabled() { statefulPrecompileConfigs[module.ConfigKey] = config } } diff --git a/params/precompiles.go b/params/extras/precompiles.go similarity index 98% rename from params/precompiles.go rename to params/extras/precompiles.go index 9b47b219fd..10bbd1161a 100644 --- a/params/precompiles.go +++ b/params/extras/precompiles.go @@ -1,7 +1,7 @@ // (c) 2023 Ava Labs, Inc. All rights reserved. // See the file LICENSE for licensing terms. -package params +package extras import ( "encoding/json" diff --git a/params/rules_extra.go b/params/rules_extra.go index b0e42a62a7..3688ff555b 100644 --- a/params/rules_extra.go +++ b/params/rules_extra.go @@ -9,7 +9,7 @@ import ( ) func GetRulesExtra(r Rules) *RulesExtra { - return extras.PointerFromRules(&r) + return payloads.PointerFromRules(&r) } type RulesExtra struct { From 14c61bdf05363146da2ccb8ac91ef7506193506f Mon Sep 17 00:00:00 2001 From: Darioush Jalali Date: Wed, 11 Dec 2024 15:09:47 -0800 Subject: [PATCH 02/18] remove type alias AvalancheConfig --- params/config.go | 29 +++++++++++++++-------------- params/config_libevm.go | 1 - plugin/evm/vm.go | 3 ++- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/params/config.go b/params/config.go index c064978cd6..3b7c9695c4 100644 --- a/params/config.go +++ b/params/config.go @@ -31,6 +31,7 @@ import ( "github.com/ava-labs/avalanchego/upgrade" "github.com/ava-labs/avalanchego/utils/constants" + "github.com/ava-labs/coreth/params/extras" "github.com/ava-labs/coreth/utils" ethparams "github.com/ava-labs/libevm/params" ) @@ -69,7 +70,7 @@ var ( CancunTime: utils.TimeToNewUint64(upgrade.GetConfig(constants.UnitTestID).EtnaTime), }, &ChainConfigExtra{ - AvalancheContext: AvalancheContext{utils.TestSnowContext()}, + AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), @@ -102,7 +103,7 @@ var ( MuirGlacierBlock: big.NewInt(0), }, &ChainConfigExtra{ - AvalancheContext: AvalancheContext{utils.TestSnowContext()}, + AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: NetworkUpgrades{ ApricotPhase1BlockTimestamp: nil, ApricotPhase2BlockTimestamp: nil, @@ -135,7 +136,7 @@ var ( MuirGlacierBlock: big.NewInt(0), }, &ChainConfigExtra{ - AvalancheContext: AvalancheContext{utils.TestSnowContext()}, + AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: nil, @@ -169,7 +170,7 @@ var ( BerlinBlock: big.NewInt(0), }, &ChainConfigExtra{ - AvalancheContext: AvalancheContext{utils.TestSnowContext()}, + AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), @@ -204,7 +205,7 @@ var ( LondonBlock: big.NewInt(0), }, &ChainConfigExtra{ - AvalancheContext: AvalancheContext{utils.TestSnowContext()}, + AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), @@ -239,7 +240,7 @@ var ( LondonBlock: big.NewInt(0), }, &ChainConfigExtra{ - AvalancheContext: AvalancheContext{utils.TestSnowContext()}, + AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), @@ -274,7 +275,7 @@ var ( LondonBlock: big.NewInt(0), }, &ChainConfigExtra{ - AvalancheContext: AvalancheContext{utils.TestSnowContext()}, + AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), @@ -309,7 +310,7 @@ var ( LondonBlock: big.NewInt(0), }, &ChainConfigExtra{ - AvalancheContext: AvalancheContext{utils.TestSnowContext()}, + AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), @@ -344,7 +345,7 @@ var ( LondonBlock: big.NewInt(0), }, &ChainConfigExtra{ - AvalancheContext: AvalancheContext{utils.TestSnowContext()}, + AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), @@ -379,7 +380,7 @@ var ( LondonBlock: big.NewInt(0), }, &ChainConfigExtra{ - AvalancheContext: AvalancheContext{utils.TestSnowContext()}, + AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), @@ -414,7 +415,7 @@ var ( LondonBlock: big.NewInt(0), }, &ChainConfigExtra{ - AvalancheContext: AvalancheContext{utils.TestSnowContext()}, + AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), @@ -449,7 +450,7 @@ var ( LondonBlock: big.NewInt(0), }, &ChainConfigExtra{ - AvalancheContext: AvalancheContext{utils.TestSnowContext()}, + AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), @@ -485,7 +486,7 @@ var ( ShanghaiTime: utils.NewUint64(0), }, &ChainConfigExtra{ - AvalancheContext: AvalancheContext{utils.TestSnowContext()}, + AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), @@ -522,7 +523,7 @@ var ( CancunTime: utils.NewUint64(0), }, &ChainConfigExtra{ - AvalancheContext: AvalancheContext{utils.TestSnowContext()}, + AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), diff --git a/params/config_libevm.go b/params/config_libevm.go index 2f99b9896e..9bba331f7c 100644 --- a/params/config_libevm.go +++ b/params/config_libevm.go @@ -16,7 +16,6 @@ import ( type ( ChainConfigExtra = extras.ChainConfigExtra NetworkUpgrades = extras.NetworkUpgrades - AvalancheContext = extras.AvalancheContext UpgradeConfig = extras.UpgradeConfig PrecompileUpgrade = extras.PrecompileUpgrade AvalancheRules = extras.AvalancheRules diff --git a/plugin/evm/vm.go b/plugin/evm/vm.go index 1390ea0ab1..61d8df154b 100644 --- a/plugin/evm/vm.go +++ b/plugin/evm/vm.go @@ -38,6 +38,7 @@ import ( "github.com/ava-labs/coreth/miner" "github.com/ava-labs/coreth/node" "github.com/ava-labs/coreth/params" + "github.com/ava-labs/coreth/params/extras" "github.com/ava-labs/coreth/peer" "github.com/ava-labs/coreth/plugin/evm/message" "github.com/ava-labs/coreth/triedb/hashdb" @@ -475,7 +476,7 @@ func (vm *VM) Initialize( } // Set the Avalanche Context on the ChainConfig - configExtra.AvalancheContext = params.AvalancheContext{ + configExtra.AvalancheContext = extras.AvalancheContext{ SnowCtx: chainCtx, } vm.syntacticBlockValidator = NewBlockValidator(extDataHashes) From ba9e9756955e14185df74dd760df6988c1b3e280 Mon Sep 17 00:00:00 2001 From: Darioush Jalali Date: Wed, 11 Dec 2024 15:11:34 -0800 Subject: [PATCH 03/18] remove type alias for PrecompileConfig --- core/genesis_test.go | 3 ++- params/config_libevm.go | 9 ++++----- plugin/evm/vm.go | 2 +- plugin/evm/vm_warp_test.go | 3 ++- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/core/genesis_test.go b/core/genesis_test.go index 94d3435f7d..9fba3f209e 100644 --- a/core/genesis_test.go +++ b/core/genesis_test.go @@ -37,6 +37,7 @@ import ( "github.com/ava-labs/coreth/core/rawdb" "github.com/ava-labs/coreth/core/types" "github.com/ava-labs/coreth/params" + "github.com/ava-labs/coreth/params/extras" "github.com/ava-labs/coreth/precompile/contracts/warp" "github.com/ava-labs/coreth/triedb/pathdb" "github.com/ava-labs/coreth/utils" @@ -257,7 +258,7 @@ func TestGenesisWriteUpgradesRegression(t *testing.T) { _, _, err := SetupGenesisBlock(db, trieDB, genesis, genesisBlock.Hash(), false) require.NoError(err) - params.GetExtra(genesis.Config).UpgradeConfig.PrecompileUpgrades = []params.PrecompileUpgrade{ + params.GetExtra(genesis.Config).UpgradeConfig.PrecompileUpgrades = []extras.PrecompileUpgrade{ { Config: warp.NewConfig(utils.NewUint64(51), 0, false), }, diff --git a/params/config_libevm.go b/params/config_libevm.go index 9bba331f7c..24247664f9 100644 --- a/params/config_libevm.go +++ b/params/config_libevm.go @@ -14,11 +14,10 @@ import ( ) type ( - ChainConfigExtra = extras.ChainConfigExtra - NetworkUpgrades = extras.NetworkUpgrades - UpgradeConfig = extras.UpgradeConfig - PrecompileUpgrade = extras.PrecompileUpgrade - AvalancheRules = extras.AvalancheRules + ChainConfigExtra = extras.ChainConfigExtra + NetworkUpgrades = extras.NetworkUpgrades + UpgradeConfig = extras.UpgradeConfig + AvalancheRules = extras.AvalancheRules ) func init() { diff --git a/plugin/evm/vm.go b/plugin/evm/vm.go index 61d8df154b..c2b1561ea9 100644 --- a/plugin/evm/vm.go +++ b/plugin/evm/vm.go @@ -470,7 +470,7 @@ func (vm *VM) Initialize( // If the Durango is activated, activate the Warp Precompile at the same time configExtra := params.GetExtra(g.Config) if configExtra.DurangoBlockTimestamp != nil { - configExtra.PrecompileUpgrades = append(configExtra.PrecompileUpgrades, params.PrecompileUpgrade{ + configExtra.PrecompileUpgrades = append(configExtra.PrecompileUpgrades, extras.PrecompileUpgrade{ Config: warpcontract.NewDefaultConfig(configExtra.DurangoBlockTimestamp), }) } diff --git a/plugin/evm/vm_warp_test.go b/plugin/evm/vm_warp_test.go index cb99e2221c..4e835fcbb7 100644 --- a/plugin/evm/vm_warp_test.go +++ b/plugin/evm/vm_warp_test.go @@ -29,6 +29,7 @@ import ( "github.com/ava-labs/coreth/core/types" "github.com/ava-labs/coreth/eth/tracers" "github.com/ava-labs/coreth/params" + "github.com/ava-labs/coreth/params/extras" "github.com/ava-labs/coreth/plugin/evm/message" "github.com/ava-labs/coreth/precompile/contract" "github.com/ava-labs/coreth/precompile/contracts/warp" @@ -425,7 +426,7 @@ func TestReceiveWarpMessage(t *testing.T) { ) vm.chainConfigExtra().UpgradeConfig = params.UpgradeConfig{ - PrecompileUpgrades: []params.PrecompileUpgrade{ + PrecompileUpgrades: []extras.PrecompileUpgrade{ {Config: enableConfig}, {Config: disableConfig}, {Config: reEnableConfig}, From f3259c169f63f23613bda9cd7fcb1b1c11b6377a Mon Sep 17 00:00:00 2001 From: Darioush Jalali Date: Wed, 11 Dec 2024 15:12:48 -0800 Subject: [PATCH 04/18] remove type alias UpgradeConfig --- params/config_extra.go | 6 +++--- params/config_libevm.go | 1 - plugin/evm/vm_warp_test.go | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/params/config_extra.go b/params/config_extra.go index 0691607020..44b64e71a9 100644 --- a/params/config_extra.go +++ b/params/config_extra.go @@ -81,7 +81,7 @@ func WithExtra(c *ChainConfig, extra *ChainConfigExtra) *ChainConfig { type ChainConfigWithUpgradesJSON struct { ChainConfig - UpgradeConfig UpgradeConfig `json:"upgrades,omitempty"` + UpgradeConfig extras.UpgradeConfig `json:"upgrades,omitempty"` } // MarshalJSON implements json.Marshaler. This is a workaround for the fact that @@ -100,7 +100,7 @@ func (cu ChainConfigWithUpgradesJSON) MarshalJSON() ([]byte, error) { } type upgrades struct { - UpgradeConfig UpgradeConfig `json:"upgrades"` + UpgradeConfig extras.UpgradeConfig `json:"upgrades"` } upgradeJSON, err := json.Marshal(upgrades{cu.UpgradeConfig}) @@ -126,7 +126,7 @@ func (cu *ChainConfigWithUpgradesJSON) UnmarshalJSON(input []byte) error { } type upgrades struct { - UpgradeConfig UpgradeConfig `json:"upgrades"` + UpgradeConfig extras.UpgradeConfig `json:"upgrades"` } var u upgrades diff --git a/params/config_libevm.go b/params/config_libevm.go index 24247664f9..82657c90a3 100644 --- a/params/config_libevm.go +++ b/params/config_libevm.go @@ -16,7 +16,6 @@ import ( type ( ChainConfigExtra = extras.ChainConfigExtra NetworkUpgrades = extras.NetworkUpgrades - UpgradeConfig = extras.UpgradeConfig AvalancheRules = extras.AvalancheRules ) diff --git a/plugin/evm/vm_warp_test.go b/plugin/evm/vm_warp_test.go index 4e835fcbb7..49984b7f7c 100644 --- a/plugin/evm/vm_warp_test.go +++ b/plugin/evm/vm_warp_test.go @@ -425,7 +425,7 @@ func TestReceiveWarpMessage(t *testing.T) { true, // RequirePrimaryNetworkSigners ) - vm.chainConfigExtra().UpgradeConfig = params.UpgradeConfig{ + vm.chainConfigExtra().UpgradeConfig = extras.UpgradeConfig{ PrecompileUpgrades: []extras.PrecompileUpgrade{ {Config: enableConfig}, {Config: disableConfig}, From a166bacc0dc5b2f154544ec6f06ceb445c4f8323 Mon Sep 17 00:00:00 2001 From: Darioush Jalali Date: Wed, 11 Dec 2024 15:18:16 -0800 Subject: [PATCH 05/18] remove type alias network upgrade --- core/genesis_extra_test.go | 3 ++- core/state_processor_test.go | 3 ++- core/vm/runtime/runtime.go | 3 ++- params/config.go | 28 ++++++++++++++-------------- params/config_libevm.go | 1 - params/config_test.go | 3 ++- tests/init.go | 19 ++++++++++--------- 7 files changed, 32 insertions(+), 28 deletions(-) diff --git a/core/genesis_extra_test.go b/core/genesis_extra_test.go index 7889d6668d..edb915b2e6 100644 --- a/core/genesis_extra_test.go +++ b/core/genesis_extra_test.go @@ -34,6 +34,7 @@ import ( "github.com/ava-labs/coreth/core/rawdb" "github.com/ava-labs/coreth/core/types" "github.com/ava-labs/coreth/params" + "github.com/ava-labs/coreth/params/extras" "github.com/ava-labs/coreth/utils" "github.com/ava-labs/libevm/common" "github.com/ava-labs/libevm/triedb" @@ -58,7 +59,7 @@ func TestGenesisEthUpgrades(t *testing.T) { MuirGlacierBlock: big.NewInt(0), }, ¶ms.ChainConfigExtra{ - NetworkUpgrades: params.NetworkUpgrades{ + NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), }, diff --git a/core/state_processor_test.go b/core/state_processor_test.go index cc05576f25..3b88c84874 100644 --- a/core/state_processor_test.go +++ b/core/state_processor_test.go @@ -38,6 +38,7 @@ import ( "github.com/ava-labs/coreth/core/rawdb" "github.com/ava-labs/coreth/core/types" "github.com/ava-labs/coreth/params" + "github.com/ava-labs/coreth/params/extras" "github.com/ava-labs/coreth/utils" "github.com/ava-labs/libevm/common" "github.com/ava-labs/libevm/core/vm" @@ -271,7 +272,7 @@ func TestStateProcessorErrors(t *testing.T) { MuirGlacierBlock: big.NewInt(0), }, ¶ms.ChainConfigExtra{ - NetworkUpgrades: params.NetworkUpgrades{ + NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), }, diff --git a/core/vm/runtime/runtime.go b/core/vm/runtime/runtime.go index d190e85b1c..8d1c89be4a 100644 --- a/core/vm/runtime/runtime.go +++ b/core/vm/runtime/runtime.go @@ -34,6 +34,7 @@ import ( "github.com/ava-labs/coreth/core/state" "github.com/ava-labs/coreth/core/types" "github.com/ava-labs/coreth/params" + "github.com/ava-labs/coreth/params/extras" "github.com/ava-labs/libevm/common" "github.com/ava-labs/libevm/core/vm" "github.com/ava-labs/libevm/crypto" @@ -85,7 +86,7 @@ func setDefaults(cfg *Config) { BerlinBlock: new(big.Int), }, ¶ms.ChainConfigExtra{ - NetworkUpgrades: params.NetworkUpgrades{ + NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: new(uint64), ApricotPhase2BlockTimestamp: new(uint64), ApricotPhase3BlockTimestamp: new(uint64), diff --git a/params/config.go b/params/config.go index 3b7c9695c4..366c6c63cf 100644 --- a/params/config.go +++ b/params/config.go @@ -71,7 +71,7 @@ var ( }, &ChainConfigExtra{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, - NetworkUpgrades: NetworkUpgrades{ + NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), ApricotPhase3BlockTimestamp: utils.NewUint64(0), @@ -104,7 +104,7 @@ var ( }, &ChainConfigExtra{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, - NetworkUpgrades: NetworkUpgrades{ + NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: nil, ApricotPhase2BlockTimestamp: nil, ApricotPhase3BlockTimestamp: nil, @@ -137,7 +137,7 @@ var ( }, &ChainConfigExtra{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, - NetworkUpgrades: NetworkUpgrades{ + NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: nil, ApricotPhase3BlockTimestamp: nil, @@ -171,7 +171,7 @@ var ( }, &ChainConfigExtra{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, - NetworkUpgrades: NetworkUpgrades{ + NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), ApricotPhase3BlockTimestamp: nil, @@ -206,7 +206,7 @@ var ( }, &ChainConfigExtra{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, - NetworkUpgrades: NetworkUpgrades{ + NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), ApricotPhase3BlockTimestamp: utils.NewUint64(0), @@ -241,7 +241,7 @@ var ( }, &ChainConfigExtra{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, - NetworkUpgrades: NetworkUpgrades{ + NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), ApricotPhase3BlockTimestamp: utils.NewUint64(0), @@ -276,7 +276,7 @@ var ( }, &ChainConfigExtra{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, - NetworkUpgrades: NetworkUpgrades{ + NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), ApricotPhase3BlockTimestamp: utils.NewUint64(0), @@ -311,7 +311,7 @@ var ( }, &ChainConfigExtra{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, - NetworkUpgrades: NetworkUpgrades{ + NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), ApricotPhase3BlockTimestamp: utils.NewUint64(0), @@ -346,7 +346,7 @@ var ( }, &ChainConfigExtra{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, - NetworkUpgrades: NetworkUpgrades{ + NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), ApricotPhase3BlockTimestamp: utils.NewUint64(0), @@ -381,7 +381,7 @@ var ( }, &ChainConfigExtra{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, - NetworkUpgrades: NetworkUpgrades{ + NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), ApricotPhase3BlockTimestamp: utils.NewUint64(0), @@ -416,7 +416,7 @@ var ( }, &ChainConfigExtra{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, - NetworkUpgrades: NetworkUpgrades{ + NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), ApricotPhase3BlockTimestamp: utils.NewUint64(0), @@ -451,7 +451,7 @@ var ( }, &ChainConfigExtra{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, - NetworkUpgrades: NetworkUpgrades{ + NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), ApricotPhase3BlockTimestamp: utils.NewUint64(0), @@ -487,7 +487,7 @@ var ( }, &ChainConfigExtra{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, - NetworkUpgrades: NetworkUpgrades{ + NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), ApricotPhase3BlockTimestamp: utils.NewUint64(0), @@ -524,7 +524,7 @@ var ( }, &ChainConfigExtra{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, - NetworkUpgrades: NetworkUpgrades{ + NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), ApricotPhase3BlockTimestamp: utils.NewUint64(0), diff --git a/params/config_libevm.go b/params/config_libevm.go index 82657c90a3..11c58aadaf 100644 --- a/params/config_libevm.go +++ b/params/config_libevm.go @@ -15,7 +15,6 @@ import ( type ( ChainConfigExtra = extras.ChainConfigExtra - NetworkUpgrades = extras.NetworkUpgrades AvalancheRules = extras.AvalancheRules ) diff --git a/params/config_test.go b/params/config_test.go index e5c7b97bdf..323e95d311 100644 --- a/params/config_test.go +++ b/params/config_test.go @@ -33,6 +33,7 @@ import ( "testing" "time" + "github.com/ava-labs/coreth/params/extras" "github.com/ava-labs/coreth/utils" ) @@ -147,7 +148,7 @@ func TestConfigRules(t *testing.T) { c := WithExtra( &ChainConfig{}, &ChainConfigExtra{ - NetworkUpgrades: NetworkUpgrades{ + NetworkUpgrades: extras.NetworkUpgrades{ CortinaBlockTimestamp: utils.NewUint64(500), }, }, diff --git a/tests/init.go b/tests/init.go index 47af1b63e2..8bb04c4218 100644 --- a/tests/init.go +++ b/tests/init.go @@ -32,6 +32,7 @@ import ( "sort" "github.com/ava-labs/coreth/params" + "github.com/ava-labs/coreth/params/extras" "github.com/ava-labs/coreth/utils" ) @@ -178,7 +179,7 @@ var Forks = map[string]*params.ChainConfig{ IstanbulBlock: big.NewInt(0), }, ¶ms.ChainConfigExtra{ - NetworkUpgrades: params.NetworkUpgrades{ + NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), }, }, @@ -198,7 +199,7 @@ var Forks = map[string]*params.ChainConfig{ BerlinBlock: big.NewInt(0), }, ¶ms.ChainConfigExtra{ - NetworkUpgrades: params.NetworkUpgrades{ + NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), }, @@ -220,7 +221,7 @@ var Forks = map[string]*params.ChainConfig{ LondonBlock: big.NewInt(0), }, ¶ms.ChainConfigExtra{ - NetworkUpgrades: params.NetworkUpgrades{ + NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), ApricotPhase3BlockTimestamp: utils.NewUint64(0), @@ -243,7 +244,7 @@ var Forks = map[string]*params.ChainConfig{ LondonBlock: big.NewInt(0), }, ¶ms.ChainConfigExtra{ - NetworkUpgrades: params.NetworkUpgrades{ + NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), ApricotPhase3BlockTimestamp: utils.NewUint64(0), @@ -266,7 +267,7 @@ var Forks = map[string]*params.ChainConfig{ LondonBlock: big.NewInt(0), }, ¶ms.ChainConfigExtra{ - NetworkUpgrades: params.NetworkUpgrades{ + NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), ApricotPhase3BlockTimestamp: utils.NewUint64(0), @@ -290,7 +291,7 @@ var Forks = map[string]*params.ChainConfig{ LondonBlock: big.NewInt(0), }, ¶ms.ChainConfigExtra{ - NetworkUpgrades: params.NetworkUpgrades{ + NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), ApricotPhase3BlockTimestamp: utils.NewUint64(0), @@ -315,7 +316,7 @@ var Forks = map[string]*params.ChainConfig{ LondonBlock: big.NewInt(0), }, ¶ms.ChainConfigExtra{ - NetworkUpgrades: params.NetworkUpgrades{ + NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), ApricotPhase3BlockTimestamp: utils.NewUint64(0), @@ -341,7 +342,7 @@ var Forks = map[string]*params.ChainConfig{ LondonBlock: big.NewInt(0), }, ¶ms.ChainConfigExtra{ - NetworkUpgrades: params.NetworkUpgrades{ + NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), ApricotPhase3BlockTimestamp: utils.NewUint64(0), @@ -370,7 +371,7 @@ var Forks = map[string]*params.ChainConfig{ CancunTime: utils.NewUint64(0), }, ¶ms.ChainConfigExtra{ - NetworkUpgrades: params.NetworkUpgrades{ + NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), ApricotPhase3BlockTimestamp: utils.NewUint64(0), From 490f9b0077fa8467378d6450815b8c3a7afeb822 Mon Sep 17 00:00:00 2001 From: Darioush Jalali Date: Wed, 11 Dec 2024 15:20:03 -0800 Subject: [PATCH 06/18] seems ok to import ethparams.ChainConfig --- params/extras/config_extra.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/params/extras/config_extra.go b/params/extras/config_extra.go index d94c0b3a2a..06d527005a 100644 --- a/params/extras/config_extra.go +++ b/params/extras/config_extra.go @@ -14,10 +14,7 @@ import ( ) // XXX: Must fix this -type ChainConfig = ethparams.ChainConfig - -// XXX: Must fix this -var GetExtra func(c *ChainConfig) *ChainConfigExtra +var GetExtra func(c *ethparams.ChainConfig) *ChainConfigExtra // UpgradeConfig includes the following configs that may be specified in upgradeBytes: // - Timestamps that enable avalanche network upgrades, @@ -40,7 +37,7 @@ type ChainConfigExtra struct { UpgradeConfig `json:"-"` // Config specified in upgradeBytes (avalanche network upgrades or enable/disabling precompiles). Skip encoding/decoding directly into ChainConfig. } -func (c *ChainConfigExtra) CheckConfigCompatible(newcfg_ *ChainConfig, headNumber *big.Int, headTimestamp uint64) *ConfigCompatError { +func (c *ChainConfigExtra) CheckConfigCompatible(newcfg_ *ethparams.ChainConfig, headNumber *big.Int, headTimestamp uint64) *ConfigCompatError { if c == nil { return nil } From 91cf3e07102debade927518531029a247ffb8606 Mon Sep 17 00:00:00 2001 From: Darioush Jalali Date: Wed, 11 Dec 2024 15:36:39 -0800 Subject: [PATCH 07/18] remove type aliases --- core/blockchain_test.go | 3 ++- core/genesis_extra_test.go | 2 +- core/state_processor_test.go | 2 +- core/vm/runtime/runtime.go | 2 +- params/config.go | 28 ++++++++++++++-------------- params/config_extra.go | 8 ++++---- params/config_libevm.go | 11 +++-------- params/config_test.go | 2 +- params/rules_extra.go | 3 ++- plugin/evm/vm.go | 2 +- tests/init.go | 18 +++++++++--------- 11 files changed, 39 insertions(+), 42 deletions(-) diff --git a/core/blockchain_test.go b/core/blockchain_test.go index d2fa3b62b3..c55ee0e79b 100644 --- a/core/blockchain_test.go +++ b/core/blockchain_test.go @@ -16,6 +16,7 @@ import ( "github.com/ava-labs/coreth/core/state/pruner" "github.com/ava-labs/coreth/core/types" "github.com/ava-labs/coreth/params" + "github.com/ava-labs/coreth/params/extras" "github.com/ava-labs/libevm/common" "github.com/ava-labs/libevm/core/vm" "github.com/ava-labs/libevm/crypto" @@ -425,7 +426,7 @@ func TestUngracefulAsyncShutdown(t *testing.T) { gspec := &Genesis{ Config: params.WithExtra( ¶ms.ChainConfig{HomesteadBlock: new(big.Int)}, - ¶ms.ChainConfigExtra{}, + &extras.ChainConfigExtra{}, ), Alloc: types.GenesisAlloc{addr1: {Balance: genesisBalance}}, } diff --git a/core/genesis_extra_test.go b/core/genesis_extra_test.go index edb915b2e6..119772bfa5 100644 --- a/core/genesis_extra_test.go +++ b/core/genesis_extra_test.go @@ -58,7 +58,7 @@ func TestGenesisEthUpgrades(t *testing.T) { IstanbulBlock: big.NewInt(0), MuirGlacierBlock: big.NewInt(0), }, - ¶ms.ChainConfigExtra{ + &extras.ChainConfigExtra{ NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), diff --git a/core/state_processor_test.go b/core/state_processor_test.go index 3b88c84874..58eb247f8a 100644 --- a/core/state_processor_test.go +++ b/core/state_processor_test.go @@ -271,7 +271,7 @@ func TestStateProcessorErrors(t *testing.T) { IstanbulBlock: big.NewInt(0), MuirGlacierBlock: big.NewInt(0), }, - ¶ms.ChainConfigExtra{ + &extras.ChainConfigExtra{ NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), diff --git a/core/vm/runtime/runtime.go b/core/vm/runtime/runtime.go index 8d1c89be4a..27d3db606b 100644 --- a/core/vm/runtime/runtime.go +++ b/core/vm/runtime/runtime.go @@ -85,7 +85,7 @@ func setDefaults(cfg *Config) { LondonBlock: new(big.Int), BerlinBlock: new(big.Int), }, - ¶ms.ChainConfigExtra{ + &extras.ChainConfigExtra{ NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: new(uint64), ApricotPhase2BlockTimestamp: new(uint64), diff --git a/params/config.go b/params/config.go index 366c6c63cf..bfd14641c2 100644 --- a/params/config.go +++ b/params/config.go @@ -69,7 +69,7 @@ var ( ShanghaiTime: utils.TimeToNewUint64(upgrade.GetConfig(constants.UnitTestID).DurangoTime), CancunTime: utils.TimeToNewUint64(upgrade.GetConfig(constants.UnitTestID).EtnaTime), }, - &ChainConfigExtra{ + &extras.ChainConfigExtra{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), @@ -102,7 +102,7 @@ var ( IstanbulBlock: big.NewInt(0), MuirGlacierBlock: big.NewInt(0), }, - &ChainConfigExtra{ + &extras.ChainConfigExtra{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: nil, @@ -135,7 +135,7 @@ var ( IstanbulBlock: big.NewInt(0), MuirGlacierBlock: big.NewInt(0), }, - &ChainConfigExtra{ + &extras.ChainConfigExtra{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), @@ -169,7 +169,7 @@ var ( MuirGlacierBlock: big.NewInt(0), BerlinBlock: big.NewInt(0), }, - &ChainConfigExtra{ + &extras.ChainConfigExtra{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), @@ -204,7 +204,7 @@ var ( BerlinBlock: big.NewInt(0), LondonBlock: big.NewInt(0), }, - &ChainConfigExtra{ + &extras.ChainConfigExtra{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), @@ -239,7 +239,7 @@ var ( BerlinBlock: big.NewInt(0), LondonBlock: big.NewInt(0), }, - &ChainConfigExtra{ + &extras.ChainConfigExtra{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), @@ -274,7 +274,7 @@ var ( BerlinBlock: big.NewInt(0), LondonBlock: big.NewInt(0), }, - &ChainConfigExtra{ + &extras.ChainConfigExtra{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), @@ -309,7 +309,7 @@ var ( BerlinBlock: big.NewInt(0), LondonBlock: big.NewInt(0), }, - &ChainConfigExtra{ + &extras.ChainConfigExtra{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), @@ -344,7 +344,7 @@ var ( BerlinBlock: big.NewInt(0), LondonBlock: big.NewInt(0), }, - &ChainConfigExtra{ + &extras.ChainConfigExtra{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), @@ -379,7 +379,7 @@ var ( BerlinBlock: big.NewInt(0), LondonBlock: big.NewInt(0), }, - &ChainConfigExtra{ + &extras.ChainConfigExtra{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), @@ -414,7 +414,7 @@ var ( BerlinBlock: big.NewInt(0), LondonBlock: big.NewInt(0), }, - &ChainConfigExtra{ + &extras.ChainConfigExtra{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), @@ -449,7 +449,7 @@ var ( BerlinBlock: big.NewInt(0), LondonBlock: big.NewInt(0), }, - &ChainConfigExtra{ + &extras.ChainConfigExtra{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), @@ -485,7 +485,7 @@ var ( LondonBlock: big.NewInt(0), ShanghaiTime: utils.NewUint64(0), }, - &ChainConfigExtra{ + &extras.ChainConfigExtra{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), @@ -522,7 +522,7 @@ var ( ShanghaiTime: utils.NewUint64(0), CancunTime: utils.NewUint64(0), }, - &ChainConfigExtra{ + &extras.ChainConfigExtra{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), diff --git a/params/config_extra.go b/params/config_extra.go index 44b64e71a9..3357639989 100644 --- a/params/config_extra.go +++ b/params/config_extra.go @@ -58,10 +58,10 @@ func SetEthUpgrades(c *ChainConfig) { } } -func GetExtra(c *ChainConfig) *ChainConfigExtra { +func GetExtra(c *ChainConfig) *extras.ChainConfigExtra { ex := payloads.FromChainConfig(c) if ex == nil { - ex = &ChainConfigExtra{} + ex = &extras.ChainConfigExtra{} payloads.SetOnChainConfig(c, ex) } return ex @@ -74,7 +74,7 @@ func Copy(c *ChainConfig) ChainConfig { } // WithExtra sets the extra payload on `c` and returns the modified argument. -func WithExtra(c *ChainConfig, extra *ChainConfigExtra) *ChainConfig { +func WithExtra(c *ChainConfig, extra *extras.ChainConfigExtra) *ChainConfig { payloads.SetOnChainConfig(c, extra) return c } @@ -164,7 +164,7 @@ func GetChainConfig(agoUpgrade upgrade.Config, chainID *big.Int) *ChainConfig { IstanbulBlock: big.NewInt(0), MuirGlacierBlock: big.NewInt(0), }, - &ChainConfigExtra{ + &extras.ChainConfigExtra{ NetworkUpgrades: extras.GetNetworkUpgrades(agoUpgrade), }, ) diff --git a/params/config_libevm.go b/params/config_libevm.go index 11c58aadaf..909f94be96 100644 --- a/params/config_libevm.go +++ b/params/config_libevm.go @@ -13,11 +13,6 @@ import ( ethparams "github.com/ava-labs/libevm/params" ) -type ( - ChainConfigExtra = extras.ChainConfigExtra - AvalancheRules = extras.AvalancheRules -) - func init() { // XXX: Must fix this extras.GetExtra = GetExtra @@ -26,19 +21,19 @@ func init() { // libevmInit would ideally be a regular init() function, but it MUST be run // before any calls to [ChainConfig.Rules]. See `config.go` for its call site. func libevmInit() any { - payloads = ethparams.RegisterExtras(ethparams.Extras[*ChainConfigExtra, RulesExtra]{ + payloads = ethparams.RegisterExtras(ethparams.Extras[*extras.ChainConfigExtra, RulesExtra]{ ReuseJSONRoot: true, // Reuse the root JSON input when unmarshalling the extra payload. NewRules: constructRulesExtra, }) return nil } -var payloads ethparams.ExtraPayloads[*ChainConfigExtra, RulesExtra] +var payloads ethparams.ExtraPayloads[*extras.ChainConfigExtra, RulesExtra] // constructRulesExtra acts as an adjunct to the [params.ChainConfig.Rules] // method. Its primary purpose is to construct the extra payload for the // [params.Rules] but it MAY also modify the [params.Rules]. -func constructRulesExtra(c *ethparams.ChainConfig, r *ethparams.Rules, cEx *ChainConfigExtra, blockNum *big.Int, isMerge bool, timestamp uint64) RulesExtra { +func constructRulesExtra(c *ethparams.ChainConfig, r *ethparams.Rules, cEx *extras.ChainConfigExtra, blockNum *big.Int, isMerge bool, timestamp uint64) RulesExtra { var rules RulesExtra if cEx == nil { return rules diff --git a/params/config_test.go b/params/config_test.go index 323e95d311..43de160006 100644 --- a/params/config_test.go +++ b/params/config_test.go @@ -147,7 +147,7 @@ func TestCheckCompatible(t *testing.T) { func TestConfigRules(t *testing.T) { c := WithExtra( &ChainConfig{}, - &ChainConfigExtra{ + &extras.ChainConfigExtra{ NetworkUpgrades: extras.NetworkUpgrades{ CortinaBlockTimestamp: utils.NewUint64(500), }, diff --git a/params/rules_extra.go b/params/rules_extra.go index 3688ff555b..a3928b3052 100644 --- a/params/rules_extra.go +++ b/params/rules_extra.go @@ -4,6 +4,7 @@ package params import ( + "github.com/ava-labs/coreth/params/extras" "github.com/ava-labs/coreth/precompile/precompileconfig" "github.com/ava-labs/libevm/common" ) @@ -14,7 +15,7 @@ func GetRulesExtra(r Rules) *RulesExtra { type RulesExtra struct { // Rules for Avalanche releases - AvalancheRules + extras.AvalancheRules // Precompiles maps addresses to stateful precompiled contracts that are enabled // for this rule set. diff --git a/plugin/evm/vm.go b/plugin/evm/vm.go index c2b1561ea9..75760cea75 100644 --- a/plugin/evm/vm.go +++ b/plugin/evm/vm.go @@ -1947,7 +1947,7 @@ func (vm *VM) GetCurrentNonce(address common.Address) (uint64, error) { return state.GetNonce(address), nil } -func (vm *VM) chainConfigExtra() *params.ChainConfigExtra { +func (vm *VM) chainConfigExtra() *extras.ChainConfigExtra { return params.GetExtra(vm.chainConfig) } diff --git a/tests/init.go b/tests/init.go index 8bb04c4218..aaff83aab8 100644 --- a/tests/init.go +++ b/tests/init.go @@ -178,7 +178,7 @@ var Forks = map[string]*params.ChainConfig{ PetersburgBlock: big.NewInt(0), IstanbulBlock: big.NewInt(0), }, - ¶ms.ChainConfigExtra{ + &extras.ChainConfigExtra{ NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), }, @@ -198,7 +198,7 @@ var Forks = map[string]*params.ChainConfig{ MuirGlacierBlock: big.NewInt(0), BerlinBlock: big.NewInt(0), }, - ¶ms.ChainConfigExtra{ + &extras.ChainConfigExtra{ NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), @@ -220,7 +220,7 @@ var Forks = map[string]*params.ChainConfig{ BerlinBlock: big.NewInt(0), LondonBlock: big.NewInt(0), }, - ¶ms.ChainConfigExtra{ + &extras.ChainConfigExtra{ NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), @@ -243,7 +243,7 @@ var Forks = map[string]*params.ChainConfig{ BerlinBlock: big.NewInt(0), LondonBlock: big.NewInt(0), }, - ¶ms.ChainConfigExtra{ + &extras.ChainConfigExtra{ NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), @@ -266,7 +266,7 @@ var Forks = map[string]*params.ChainConfig{ BerlinBlock: big.NewInt(0), LondonBlock: big.NewInt(0), }, - ¶ms.ChainConfigExtra{ + &extras.ChainConfigExtra{ NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), @@ -290,7 +290,7 @@ var Forks = map[string]*params.ChainConfig{ BerlinBlock: big.NewInt(0), LondonBlock: big.NewInt(0), }, - ¶ms.ChainConfigExtra{ + &extras.ChainConfigExtra{ NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), @@ -315,7 +315,7 @@ var Forks = map[string]*params.ChainConfig{ BerlinBlock: big.NewInt(0), LondonBlock: big.NewInt(0), }, - ¶ms.ChainConfigExtra{ + &extras.ChainConfigExtra{ NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), @@ -341,7 +341,7 @@ var Forks = map[string]*params.ChainConfig{ BerlinBlock: big.NewInt(0), LondonBlock: big.NewInt(0), }, - ¶ms.ChainConfigExtra{ + &extras.ChainConfigExtra{ NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), @@ -370,7 +370,7 @@ var Forks = map[string]*params.ChainConfig{ ShanghaiTime: utils.NewUint64(0), CancunTime: utils.NewUint64(0), }, - ¶ms.ChainConfigExtra{ + &extras.ChainConfigExtra{ NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), From 0cd784c27e2f5117199decefea484c27c3052ff3 Mon Sep 17 00:00:00 2001 From: Arran Schlosberg Date: Thu, 12 Dec 2024 18:00:15 +0000 Subject: [PATCH 08/18] refactor: use `ChainConfig.Hooks().(*ChainConfigExtra)` --- params/config_libevm.go | 5 ----- params/extras/config_extra.go | 8 ++++---- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/params/config_libevm.go b/params/config_libevm.go index 909f94be96..16057b5199 100644 --- a/params/config_libevm.go +++ b/params/config_libevm.go @@ -13,11 +13,6 @@ import ( ethparams "github.com/ava-labs/libevm/params" ) -func init() { - // XXX: Must fix this - extras.GetExtra = GetExtra -} - // libevmInit would ideally be a regular init() function, but it MUST be run // before any calls to [ChainConfig.Rules]. See `config.go` for its call site. func libevmInit() any { diff --git a/params/extras/config_extra.go b/params/extras/config_extra.go index 06d527005a..81adeeb1b0 100644 --- a/params/extras/config_extra.go +++ b/params/extras/config_extra.go @@ -13,9 +13,6 @@ import ( ethparams "github.com/ava-labs/libevm/params" ) -// XXX: Must fix this -var GetExtra func(c *ethparams.ChainConfig) *ChainConfigExtra - // UpgradeConfig includes the following configs that may be specified in upgradeBytes: // - Timestamps that enable avalanche network upgrades, // - Enabling or disabling precompiles as network upgrades. @@ -41,7 +38,10 @@ func (c *ChainConfigExtra) CheckConfigCompatible(newcfg_ *ethparams.ChainConfig, if c == nil { return nil } - newcfg := GetExtra(newcfg_) + newcfg, ok := newcfg_.Hooks().(*ChainConfigExtra) + if !ok { + // XXX: if libevm is implemented properly then this can never happen, but that's only a runtime guarantee so shouldn't be relied upon + } // Check avalanche network upgrades if err := c.checkNetworkUpgradesCompatible(&newcfg.NetworkUpgrades, headTimestamp); err != nil { From 409af0f2d9120f046c72336aa19a1eefb7ef765d Mon Sep 17 00:00:00 2001 From: Darioush Jalali Date: Tue, 17 Dec 2024 11:57:12 -0800 Subject: [PATCH 09/18] move RulesExtra (only struct, keep behavior) --- params/{ => extras}/rules_extra.go | 24 ++---------------------- params/hooks_libevm.go | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 22 deletions(-) rename params/{ => extras}/rules_extra.go (61%) diff --git a/params/rules_extra.go b/params/extras/rules_extra.go similarity index 61% rename from params/rules_extra.go rename to params/extras/rules_extra.go index a3928b3052..2636db214e 100644 --- a/params/rules_extra.go +++ b/params/extras/rules_extra.go @@ -1,21 +1,16 @@ // (c) 2024 Ava Labs, Inc. All rights reserved. // See the file LICENSE for licensing terms. -package params +package extras import ( - "github.com/ava-labs/coreth/params/extras" "github.com/ava-labs/coreth/precompile/precompileconfig" "github.com/ava-labs/libevm/common" ) -func GetRulesExtra(r Rules) *RulesExtra { - return payloads.PointerFromRules(&r) -} - type RulesExtra struct { // Rules for Avalanche releases - extras.AvalancheRules + AvalancheRules // Precompiles maps addresses to stateful precompiled contracts that are enabled // for this rule set. @@ -29,18 +24,3 @@ type RulesExtra struct { // that are enabled for this rule set. AccepterPrecompiles map[common.Address]precompileconfig.Accepter } - -func (r *RulesExtra) PredicatersExist() bool { - return len(r.Predicaters) > 0 -} - -func (r *RulesExtra) PredicaterExists(addr common.Address) bool { - _, ok := r.Predicaters[addr] - return ok -} - -// IsPrecompileEnabled returns true if the precompile at [addr] is enabled for this rule set. -func (r *RulesExtra) IsPrecompileEnabled(addr common.Address) bool { - _, ok := r.Precompiles[addr] - return ok -} diff --git a/params/hooks_libevm.go b/params/hooks_libevm.go index 7ad82c67c4..5182e47350 100644 --- a/params/hooks_libevm.go +++ b/params/hooks_libevm.go @@ -8,6 +8,7 @@ import ( "github.com/ava-labs/avalanchego/snow" "github.com/ava-labs/coreth/nativeasset" + "github.com/ava-labs/coreth/params/extras" "github.com/ava-labs/coreth/precompile/contract" "github.com/ava-labs/coreth/precompile/modules" "github.com/ava-labs/coreth/precompile/precompileconfig" @@ -19,6 +20,27 @@ import ( "golang.org/x/exp/maps" ) +type RulesExtra extras.RulesExtra + +func GetRulesExtra(r Rules) *RulesExtra { + return payloads.PointerFromRules(&r) +} + +func (r *RulesExtra) PredicatersExist() bool { + return len(r.Predicaters) > 0 +} + +func (r *RulesExtra) PredicaterExists(addr common.Address) bool { + _, ok := r.Predicaters[addr] + return ok +} + +// IsPrecompileEnabled returns true if the precompile at [addr] is enabled for this rule set. +func (r *RulesExtra) IsPrecompileEnabled(addr common.Address) bool { + _, ok := r.Precompiles[addr] + return ok +} + func (r RulesExtra) CanCreateContract(ac *libevm.AddressContext, gas uint64, state libevm.StateReader) (uint64, error) { return gas, nil } From 146c43e916f16ee81432342d1f63763577b96c72 Mon Sep 17 00:00:00 2001 From: Darioush Jalali Date: Tue, 17 Dec 2024 12:13:40 -0800 Subject: [PATCH 10/18] refactor: return extras.RulesExtra from GetRulesExtra --- params/extras/rules_extra.go | 15 +++++++++++++++ params/hooks_libevm.go | 20 +++----------------- plugin/evm/block.go | 3 ++- plugin/evm/export_tx.go | 5 +++-- plugin/evm/export_tx_test.go | 7 ++++--- plugin/evm/import_tx.go | 5 +++-- plugin/evm/test_tx.go | 6 +++--- plugin/evm/tx.go | 6 +++--- plugin/evm/tx_test.go | 3 ++- plugin/evm/vm.go | 6 +++--- 10 files changed, 41 insertions(+), 35 deletions(-) diff --git a/params/extras/rules_extra.go b/params/extras/rules_extra.go index 2636db214e..7f73be1e08 100644 --- a/params/extras/rules_extra.go +++ b/params/extras/rules_extra.go @@ -24,3 +24,18 @@ type RulesExtra struct { // that are enabled for this rule set. AccepterPrecompiles map[common.Address]precompileconfig.Accepter } + +func (r *RulesExtra) PredicatersExist() bool { + return len(r.Predicaters) > 0 +} + +func (r *RulesExtra) PredicaterExists(addr common.Address) bool { + _, ok := r.Predicaters[addr] + return ok +} + +// IsPrecompileEnabled returns true if the precompile at [addr] is enabled for this rule set. +func (r *RulesExtra) IsPrecompileEnabled(addr common.Address) bool { + _, ok := r.Precompiles[addr] + return ok +} diff --git a/params/hooks_libevm.go b/params/hooks_libevm.go index 5182e47350..3621d7517e 100644 --- a/params/hooks_libevm.go +++ b/params/hooks_libevm.go @@ -22,23 +22,9 @@ import ( type RulesExtra extras.RulesExtra -func GetRulesExtra(r Rules) *RulesExtra { - return payloads.PointerFromRules(&r) -} - -func (r *RulesExtra) PredicatersExist() bool { - return len(r.Predicaters) > 0 -} - -func (r *RulesExtra) PredicaterExists(addr common.Address) bool { - _, ok := r.Predicaters[addr] - return ok -} - -// IsPrecompileEnabled returns true if the precompile at [addr] is enabled for this rule set. -func (r *RulesExtra) IsPrecompileEnabled(addr common.Address) bool { - _, ok := r.Precompiles[addr] - return ok +func GetRulesExtra(r Rules) *extras.RulesExtra { + rules := payloads.PointerFromRules(&r) + return (*extras.RulesExtra)(rules) } func (r RulesExtra) CanCreateContract(ac *libevm.AddressContext, gas uint64, state libevm.StateReader) (uint64, error) { diff --git a/plugin/evm/block.go b/plugin/evm/block.go index d139580b0b..e3d087bd56 100644 --- a/plugin/evm/block.go +++ b/plugin/evm/block.go @@ -18,6 +18,7 @@ import ( "github.com/ava-labs/coreth/core/rawdb" "github.com/ava-labs/coreth/core/types" "github.com/ava-labs/coreth/params" + "github.com/ava-labs/coreth/params/extras" "github.com/ava-labs/coreth/precompile/precompileconfig" "github.com/ava-labs/coreth/predicate" @@ -193,7 +194,7 @@ func (b *Block) Accept(context.Context) error { // contract.Accepter // This function assumes that the Accept function will ONLY operate on state maintained in the VM's versiondb. // This ensures that any DB operations are performed atomically with marking the block as accepted. -func (b *Block) handlePrecompileAccept(rules params.RulesExtra, sharedMemoryWriter *sharedMemoryWriter) error { +func (b *Block) handlePrecompileAccept(rules extras.RulesExtra, sharedMemoryWriter *sharedMemoryWriter) error { // Short circuit early if there are no precompile accepters to execute if len(rules.AccepterPrecompiles) == 0 { return nil diff --git a/plugin/evm/export_tx.go b/plugin/evm/export_tx.go index 5eb16fe495..736dca6c7a 100644 --- a/plugin/evm/export_tx.go +++ b/plugin/evm/export_tx.go @@ -11,6 +11,7 @@ import ( "github.com/ava-labs/coreth/core/state" "github.com/ava-labs/coreth/params" + "github.com/ava-labs/coreth/params/extras" "github.com/holiman/uint256" "github.com/ava-labs/avalanchego/chains/atomic" @@ -69,7 +70,7 @@ func (utx *UnsignedExportTx) InputUTXOs() set.Set[ids.ID] { // Verify this transaction is well-formed func (utx *UnsignedExportTx) Verify( ctx *snow.Context, - rules params.RulesExtra, + rules extras.RulesExtra, ) error { switch { case utx == nil: @@ -180,7 +181,7 @@ func (utx *UnsignedExportTx) SemanticVerify( stx *Tx, _ *Block, baseFee *big.Int, - rules params.RulesExtra, + rules extras.RulesExtra, ) error { if err := utx.Verify(vm.ctx, rules); err != nil { return err diff --git a/plugin/evm/export_tx_test.go b/plugin/evm/export_tx_test.go index 2fcb93b78d..16fe25621d 100644 --- a/plugin/evm/export_tx_test.go +++ b/plugin/evm/export_tx_test.go @@ -18,6 +18,7 @@ import ( "github.com/ava-labs/avalanchego/vms/components/avax" "github.com/ava-labs/avalanchego/vms/secp256k1fx" "github.com/ava-labs/coreth/params" + "github.com/ava-labs/coreth/params/extras" "github.com/ava-labs/libevm/common" "github.com/holiman/uint256" ) @@ -526,7 +527,7 @@ func TestExportTxSemanticVerify(t *testing.T) { tx *Tx signers [][]*secp256k1.PrivateKey baseFee *big.Int - rules params.RulesExtra + rules extras.RulesExtra shouldErr bool }{ { @@ -1633,7 +1634,7 @@ func TestNewExportTx(t *testing.T) { tests := []struct { name string genesis string - rules params.RulesExtra + rules extras.RulesExtra bal uint64 expectedBurnedAVAX uint64 }{ @@ -1806,7 +1807,7 @@ func TestNewExportTxMulticoin(t *testing.T) { tests := []struct { name string genesis string - rules params.RulesExtra + rules extras.RulesExtra bal uint64 balmc uint64 }{ diff --git a/plugin/evm/import_tx.go b/plugin/evm/import_tx.go index 5616a92a1f..a008e9c911 100644 --- a/plugin/evm/import_tx.go +++ b/plugin/evm/import_tx.go @@ -12,6 +12,7 @@ import ( "github.com/ava-labs/coreth/core/state" "github.com/ava-labs/coreth/params" + "github.com/ava-labs/coreth/params/extras" "github.com/holiman/uint256" "github.com/ava-labs/avalanchego/chains/atomic" @@ -62,7 +63,7 @@ func (utx *UnsignedImportTx) InputUTXOs() set.Set[ids.ID] { // Verify this transaction is well-formed func (utx *UnsignedImportTx) Verify( ctx *snow.Context, - rules params.RulesExtra, + rules extras.RulesExtra, ) error { switch { case utx == nil: @@ -181,7 +182,7 @@ func (utx *UnsignedImportTx) SemanticVerify( stx *Tx, parent *Block, baseFee *big.Int, - rules params.RulesExtra, + rules extras.RulesExtra, ) error { if err := utx.Verify(vm.ctx, rules); err != nil { return err diff --git a/plugin/evm/test_tx.go b/plugin/evm/test_tx.go index e3b40164ff..d6e55f19f8 100644 --- a/plugin/evm/test_tx.go +++ b/plugin/evm/test_tx.go @@ -17,7 +17,7 @@ import ( "github.com/ava-labs/avalanchego/utils/set" "github.com/ava-labs/avalanchego/utils/wrappers" "github.com/ava-labs/coreth/core/state" - "github.com/ava-labs/coreth/params" + "github.com/ava-labs/coreth/params/extras" ) type TestUnsignedTx struct { @@ -40,7 +40,7 @@ var _ UnsignedAtomicTx = &TestUnsignedTx{} func (t *TestUnsignedTx) GasUsed(fixedFee bool) (uint64, error) { return t.GasUsedV, nil } // Verify implements the UnsignedAtomicTx interface -func (t *TestUnsignedTx) Verify(ctx *snow.Context, rules params.RulesExtra) error { return t.VerifyV } +func (t *TestUnsignedTx) Verify(ctx *snow.Context, rules extras.RulesExtra) error { return t.VerifyV } // AtomicOps implements the UnsignedAtomicTx interface func (t *TestUnsignedTx) AtomicOps() (ids.ID, *atomic.Requests, error) { @@ -66,7 +66,7 @@ func (t *TestUnsignedTx) SignedBytes() []byte { return t.SignedBytesV } func (t *TestUnsignedTx) InputUTXOs() set.Set[ids.ID] { return t.InputUTXOsV } // SemanticVerify implements the UnsignedAtomicTx interface -func (t *TestUnsignedTx) SemanticVerify(vm *VM, stx *Tx, parent *Block, baseFee *big.Int, rules params.RulesExtra) error { +func (t *TestUnsignedTx) SemanticVerify(vm *VM, stx *Tx, parent *Block, baseFee *big.Int, rules extras.RulesExtra) error { return t.SemanticVerifyV } diff --git a/plugin/evm/tx.go b/plugin/evm/tx.go index b2a5a95991..85ab924713 100644 --- a/plugin/evm/tx.go +++ b/plugin/evm/tx.go @@ -13,7 +13,7 @@ import ( "github.com/ava-labs/libevm/common" "github.com/ava-labs/coreth/core/state" - "github.com/ava-labs/coreth/params" + "github.com/ava-labs/coreth/params/extras" "github.com/ava-labs/avalanchego/chains/atomic" "github.com/ava-labs/avalanchego/codec" @@ -122,9 +122,9 @@ type UnsignedAtomicTx interface { // InputUTXOs returns the UTXOs this tx consumes InputUTXOs() set.Set[ids.ID] // Verify attempts to verify that the transaction is well formed - Verify(ctx *snow.Context, rules params.RulesExtra) error + Verify(ctx *snow.Context, rules extras.RulesExtra) error // Attempts to verify this transaction with the provided state. - SemanticVerify(vm *VM, stx *Tx, parent *Block, baseFee *big.Int, rules params.RulesExtra) error + SemanticVerify(vm *VM, stx *Tx, parent *Block, baseFee *big.Int, rules extras.RulesExtra) error // AtomicOps returns the blockchainID and set of atomic requests that // must be applied to shared memory for this transaction to be accepted. // The set of atomic requests must be returned in a consistent order. diff --git a/plugin/evm/tx_test.go b/plugin/evm/tx_test.go index 32ff051534..d2d29172f6 100644 --- a/plugin/evm/tx_test.go +++ b/plugin/evm/tx_test.go @@ -14,6 +14,7 @@ import ( "github.com/ava-labs/libevm/common" "github.com/ava-labs/coreth/params" + "github.com/ava-labs/coreth/params/extras" "github.com/ava-labs/avalanchego/chains/atomic" "github.com/ava-labs/avalanchego/ids" @@ -60,7 +61,7 @@ func TestCalculateDynamicFee(t *testing.T) { type atomicTxVerifyTest struct { ctx *snow.Context generate func(t *testing.T) UnsignedAtomicTx - rules params.RulesExtra + rules extras.RulesExtra expectedErr string } diff --git a/plugin/evm/vm.go b/plugin/evm/vm.go index 75760cea75..dce1e0f1d0 100644 --- a/plugin/evm/vm.go +++ b/plugin/evm/vm.go @@ -1699,7 +1699,7 @@ func (vm *VM) verifyTxAtTip(tx *Tx) error { // Note: verifyTx may modify [state]. If [state] needs to be properly maintained, the caller is responsible // for reverting to the correct snapshot after calling this function. If this function is called with a // throwaway state, then this is not necessary. -func (vm *VM) verifyTx(tx *Tx, parentHash common.Hash, baseFee *big.Int, state *state.StateDB, rules params.RulesExtra) error { +func (vm *VM) verifyTx(tx *Tx, parentHash common.Hash, baseFee *big.Int, state *state.StateDB, rules extras.RulesExtra) error { parentIntf, err := vm.GetBlockInternal(context.TODO(), ids.ID(parentHash)) if err != nil { return fmt.Errorf("failed to get parent block: %w", err) @@ -1716,7 +1716,7 @@ func (vm *VM) verifyTx(tx *Tx, parentHash common.Hash, baseFee *big.Int, state * // verifyTxs verifies that [txs] are valid to be issued into a block with parent block [parentHash] // using [rules] as the current rule set. -func (vm *VM) verifyTxs(txs []*Tx, parentHash common.Hash, baseFee *big.Int, height uint64, rules params.RulesExtra) error { +func (vm *VM) verifyTxs(txs []*Tx, parentHash common.Hash, baseFee *big.Int, height uint64, rules extras.RulesExtra) error { // Ensure that the parent was verified and inserted correctly. if !vm.blockChain.HasBlock(parentHash, height-1) { return errRejectedParent @@ -1952,7 +1952,7 @@ func (vm *VM) chainConfigExtra() *extras.ChainConfigExtra { } // currentRules returns the chain rules for the current block. -func (vm *VM) currentRules() params.RulesExtra { +func (vm *VM) currentRules() extras.RulesExtra { header := vm.eth.APIBackend.CurrentHeader() rules := vm.chainConfig.Rules(header.Number, params.IsMergeTODO, header.Time) return *params.GetRulesExtra(rules) From acfb22b10f6c2b8feeaeeee6c1f19a12b8cf9b70 Mon Sep 17 00:00:00 2001 From: Darioush Jalali Date: Tue, 17 Dec 2024 12:14:24 -0800 Subject: [PATCH 11/18] rename extras.RulesExtra -> extras.Rules --- params/extras/rules_extra.go | 8 ++++---- params/hooks_libevm.go | 6 +++--- plugin/evm/block.go | 2 +- plugin/evm/export_tx.go | 4 ++-- plugin/evm/export_tx_test.go | 6 +++--- plugin/evm/import_tx.go | 4 ++-- plugin/evm/test_tx.go | 4 ++-- plugin/evm/tx.go | 4 ++-- plugin/evm/tx_test.go | 2 +- plugin/evm/vm.go | 6 +++--- 10 files changed, 23 insertions(+), 23 deletions(-) diff --git a/params/extras/rules_extra.go b/params/extras/rules_extra.go index 7f73be1e08..40b51a3ba2 100644 --- a/params/extras/rules_extra.go +++ b/params/extras/rules_extra.go @@ -8,7 +8,7 @@ import ( "github.com/ava-labs/libevm/common" ) -type RulesExtra struct { +type Rules struct { // Rules for Avalanche releases AvalancheRules @@ -25,17 +25,17 @@ type RulesExtra struct { AccepterPrecompiles map[common.Address]precompileconfig.Accepter } -func (r *RulesExtra) PredicatersExist() bool { +func (r *Rules) PredicatersExist() bool { return len(r.Predicaters) > 0 } -func (r *RulesExtra) PredicaterExists(addr common.Address) bool { +func (r *Rules) PredicaterExists(addr common.Address) bool { _, ok := r.Predicaters[addr] return ok } // IsPrecompileEnabled returns true if the precompile at [addr] is enabled for this rule set. -func (r *RulesExtra) IsPrecompileEnabled(addr common.Address) bool { +func (r *Rules) IsPrecompileEnabled(addr common.Address) bool { _, ok := r.Precompiles[addr] return ok } diff --git a/params/hooks_libevm.go b/params/hooks_libevm.go index 3621d7517e..53e266ed84 100644 --- a/params/hooks_libevm.go +++ b/params/hooks_libevm.go @@ -20,11 +20,11 @@ import ( "golang.org/x/exp/maps" ) -type RulesExtra extras.RulesExtra +type RulesExtra extras.Rules -func GetRulesExtra(r Rules) *extras.RulesExtra { +func GetRulesExtra(r Rules) *extras.Rules { rules := payloads.PointerFromRules(&r) - return (*extras.RulesExtra)(rules) + return (*extras.Rules)(rules) } func (r RulesExtra) CanCreateContract(ac *libevm.AddressContext, gas uint64, state libevm.StateReader) (uint64, error) { diff --git a/plugin/evm/block.go b/plugin/evm/block.go index e3d087bd56..9003b7b7f3 100644 --- a/plugin/evm/block.go +++ b/plugin/evm/block.go @@ -194,7 +194,7 @@ func (b *Block) Accept(context.Context) error { // contract.Accepter // This function assumes that the Accept function will ONLY operate on state maintained in the VM's versiondb. // This ensures that any DB operations are performed atomically with marking the block as accepted. -func (b *Block) handlePrecompileAccept(rules extras.RulesExtra, sharedMemoryWriter *sharedMemoryWriter) error { +func (b *Block) handlePrecompileAccept(rules extras.Rules, sharedMemoryWriter *sharedMemoryWriter) error { // Short circuit early if there are no precompile accepters to execute if len(rules.AccepterPrecompiles) == 0 { return nil diff --git a/plugin/evm/export_tx.go b/plugin/evm/export_tx.go index 736dca6c7a..79f98af435 100644 --- a/plugin/evm/export_tx.go +++ b/plugin/evm/export_tx.go @@ -70,7 +70,7 @@ func (utx *UnsignedExportTx) InputUTXOs() set.Set[ids.ID] { // Verify this transaction is well-formed func (utx *UnsignedExportTx) Verify( ctx *snow.Context, - rules extras.RulesExtra, + rules extras.Rules, ) error { switch { case utx == nil: @@ -181,7 +181,7 @@ func (utx *UnsignedExportTx) SemanticVerify( stx *Tx, _ *Block, baseFee *big.Int, - rules extras.RulesExtra, + rules extras.Rules, ) error { if err := utx.Verify(vm.ctx, rules); err != nil { return err diff --git a/plugin/evm/export_tx_test.go b/plugin/evm/export_tx_test.go index 16fe25621d..84f1d448bb 100644 --- a/plugin/evm/export_tx_test.go +++ b/plugin/evm/export_tx_test.go @@ -527,7 +527,7 @@ func TestExportTxSemanticVerify(t *testing.T) { tx *Tx signers [][]*secp256k1.PrivateKey baseFee *big.Int - rules extras.RulesExtra + rules extras.Rules shouldErr bool }{ { @@ -1634,7 +1634,7 @@ func TestNewExportTx(t *testing.T) { tests := []struct { name string genesis string - rules extras.RulesExtra + rules extras.Rules bal uint64 expectedBurnedAVAX uint64 }{ @@ -1807,7 +1807,7 @@ func TestNewExportTxMulticoin(t *testing.T) { tests := []struct { name string genesis string - rules extras.RulesExtra + rules extras.Rules bal uint64 balmc uint64 }{ diff --git a/plugin/evm/import_tx.go b/plugin/evm/import_tx.go index a008e9c911..c896795706 100644 --- a/plugin/evm/import_tx.go +++ b/plugin/evm/import_tx.go @@ -63,7 +63,7 @@ func (utx *UnsignedImportTx) InputUTXOs() set.Set[ids.ID] { // Verify this transaction is well-formed func (utx *UnsignedImportTx) Verify( ctx *snow.Context, - rules extras.RulesExtra, + rules extras.Rules, ) error { switch { case utx == nil: @@ -182,7 +182,7 @@ func (utx *UnsignedImportTx) SemanticVerify( stx *Tx, parent *Block, baseFee *big.Int, - rules extras.RulesExtra, + rules extras.Rules, ) error { if err := utx.Verify(vm.ctx, rules); err != nil { return err diff --git a/plugin/evm/test_tx.go b/plugin/evm/test_tx.go index d6e55f19f8..134a843431 100644 --- a/plugin/evm/test_tx.go +++ b/plugin/evm/test_tx.go @@ -40,7 +40,7 @@ var _ UnsignedAtomicTx = &TestUnsignedTx{} func (t *TestUnsignedTx) GasUsed(fixedFee bool) (uint64, error) { return t.GasUsedV, nil } // Verify implements the UnsignedAtomicTx interface -func (t *TestUnsignedTx) Verify(ctx *snow.Context, rules extras.RulesExtra) error { return t.VerifyV } +func (t *TestUnsignedTx) Verify(ctx *snow.Context, rules extras.Rules) error { return t.VerifyV } // AtomicOps implements the UnsignedAtomicTx interface func (t *TestUnsignedTx) AtomicOps() (ids.ID, *atomic.Requests, error) { @@ -66,7 +66,7 @@ func (t *TestUnsignedTx) SignedBytes() []byte { return t.SignedBytesV } func (t *TestUnsignedTx) InputUTXOs() set.Set[ids.ID] { return t.InputUTXOsV } // SemanticVerify implements the UnsignedAtomicTx interface -func (t *TestUnsignedTx) SemanticVerify(vm *VM, stx *Tx, parent *Block, baseFee *big.Int, rules extras.RulesExtra) error { +func (t *TestUnsignedTx) SemanticVerify(vm *VM, stx *Tx, parent *Block, baseFee *big.Int, rules extras.Rules) error { return t.SemanticVerifyV } diff --git a/plugin/evm/tx.go b/plugin/evm/tx.go index 85ab924713..54f917da63 100644 --- a/plugin/evm/tx.go +++ b/plugin/evm/tx.go @@ -122,9 +122,9 @@ type UnsignedAtomicTx interface { // InputUTXOs returns the UTXOs this tx consumes InputUTXOs() set.Set[ids.ID] // Verify attempts to verify that the transaction is well formed - Verify(ctx *snow.Context, rules extras.RulesExtra) error + Verify(ctx *snow.Context, rules extras.Rules) error // Attempts to verify this transaction with the provided state. - SemanticVerify(vm *VM, stx *Tx, parent *Block, baseFee *big.Int, rules extras.RulesExtra) error + SemanticVerify(vm *VM, stx *Tx, parent *Block, baseFee *big.Int, rules extras.Rules) error // AtomicOps returns the blockchainID and set of atomic requests that // must be applied to shared memory for this transaction to be accepted. // The set of atomic requests must be returned in a consistent order. diff --git a/plugin/evm/tx_test.go b/plugin/evm/tx_test.go index d2d29172f6..ef22bfa829 100644 --- a/plugin/evm/tx_test.go +++ b/plugin/evm/tx_test.go @@ -61,7 +61,7 @@ func TestCalculateDynamicFee(t *testing.T) { type atomicTxVerifyTest struct { ctx *snow.Context generate func(t *testing.T) UnsignedAtomicTx - rules extras.RulesExtra + rules extras.Rules expectedErr string } diff --git a/plugin/evm/vm.go b/plugin/evm/vm.go index dce1e0f1d0..a254d1d3e3 100644 --- a/plugin/evm/vm.go +++ b/plugin/evm/vm.go @@ -1699,7 +1699,7 @@ func (vm *VM) verifyTxAtTip(tx *Tx) error { // Note: verifyTx may modify [state]. If [state] needs to be properly maintained, the caller is responsible // for reverting to the correct snapshot after calling this function. If this function is called with a // throwaway state, then this is not necessary. -func (vm *VM) verifyTx(tx *Tx, parentHash common.Hash, baseFee *big.Int, state *state.StateDB, rules extras.RulesExtra) error { +func (vm *VM) verifyTx(tx *Tx, parentHash common.Hash, baseFee *big.Int, state *state.StateDB, rules extras.Rules) error { parentIntf, err := vm.GetBlockInternal(context.TODO(), ids.ID(parentHash)) if err != nil { return fmt.Errorf("failed to get parent block: %w", err) @@ -1716,7 +1716,7 @@ func (vm *VM) verifyTx(tx *Tx, parentHash common.Hash, baseFee *big.Int, state * // verifyTxs verifies that [txs] are valid to be issued into a block with parent block [parentHash] // using [rules] as the current rule set. -func (vm *VM) verifyTxs(txs []*Tx, parentHash common.Hash, baseFee *big.Int, height uint64, rules extras.RulesExtra) error { +func (vm *VM) verifyTxs(txs []*Tx, parentHash common.Hash, baseFee *big.Int, height uint64, rules extras.Rules) error { // Ensure that the parent was verified and inserted correctly. if !vm.blockChain.HasBlock(parentHash, height-1) { return errRejectedParent @@ -1952,7 +1952,7 @@ func (vm *VM) chainConfigExtra() *extras.ChainConfigExtra { } // currentRules returns the chain rules for the current block. -func (vm *VM) currentRules() extras.RulesExtra { +func (vm *VM) currentRules() extras.Rules { header := vm.eth.APIBackend.CurrentHeader() rules := vm.chainConfig.Rules(header.Number, params.IsMergeTODO, header.Time) return *params.GetRulesExtra(rules) From c25065d1edb18985832dcdb3feb247798824ae1f Mon Sep 17 00:00:00 2001 From: Darioush Jalali Date: Tue, 17 Dec 2024 12:14:47 -0800 Subject: [PATCH 12/18] rename file --- params/extras/{rules_extra.go => rules.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename params/extras/{rules_extra.go => rules.go} (100%) diff --git a/params/extras/rules_extra.go b/params/extras/rules.go similarity index 100% rename from params/extras/rules_extra.go rename to params/extras/rules.go From f32388aa87376efedc0d403f7ee135358d7653fa Mon Sep 17 00:00:00 2001 From: Darioush Jalali Date: Tue, 17 Dec 2024 12:16:15 -0800 Subject: [PATCH 13/18] refactor: rename extras.ChainConfigExtra -> extras.ChainConfig --- core/blockchain_test.go | 2 +- core/genesis_extra_test.go | 2 +- core/state_processor_test.go | 2 +- core/vm/runtime/runtime.go | 2 +- params/config.go | 28 ++++++++++---------- params/config_extra.go | 8 +++--- params/config_libevm.go | 6 ++--- params/config_test.go | 2 +- params/extras/{config_extra.go => config.go} | 24 ++++++++--------- params/extras/precompile_upgrade.go | 12 ++++----- plugin/evm/vm.go | 2 +- tests/init.go | 18 ++++++------- 12 files changed, 54 insertions(+), 54 deletions(-) rename params/extras/{config_extra.go => config.go} (92%) diff --git a/core/blockchain_test.go b/core/blockchain_test.go index c55ee0e79b..95e703c5ad 100644 --- a/core/blockchain_test.go +++ b/core/blockchain_test.go @@ -426,7 +426,7 @@ func TestUngracefulAsyncShutdown(t *testing.T) { gspec := &Genesis{ Config: params.WithExtra( ¶ms.ChainConfig{HomesteadBlock: new(big.Int)}, - &extras.ChainConfigExtra{}, + &extras.ChainConfig{}, ), Alloc: types.GenesisAlloc{addr1: {Balance: genesisBalance}}, } diff --git a/core/genesis_extra_test.go b/core/genesis_extra_test.go index 119772bfa5..6701fefe0e 100644 --- a/core/genesis_extra_test.go +++ b/core/genesis_extra_test.go @@ -58,7 +58,7 @@ func TestGenesisEthUpgrades(t *testing.T) { IstanbulBlock: big.NewInt(0), MuirGlacierBlock: big.NewInt(0), }, - &extras.ChainConfigExtra{ + &extras.ChainConfig{ NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), diff --git a/core/state_processor_test.go b/core/state_processor_test.go index 58eb247f8a..64179f4290 100644 --- a/core/state_processor_test.go +++ b/core/state_processor_test.go @@ -271,7 +271,7 @@ func TestStateProcessorErrors(t *testing.T) { IstanbulBlock: big.NewInt(0), MuirGlacierBlock: big.NewInt(0), }, - &extras.ChainConfigExtra{ + &extras.ChainConfig{ NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), diff --git a/core/vm/runtime/runtime.go b/core/vm/runtime/runtime.go index 27d3db606b..c66342e918 100644 --- a/core/vm/runtime/runtime.go +++ b/core/vm/runtime/runtime.go @@ -85,7 +85,7 @@ func setDefaults(cfg *Config) { LondonBlock: new(big.Int), BerlinBlock: new(big.Int), }, - &extras.ChainConfigExtra{ + &extras.ChainConfig{ NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: new(uint64), ApricotPhase2BlockTimestamp: new(uint64), diff --git a/params/config.go b/params/config.go index bfd14641c2..e44e41e287 100644 --- a/params/config.go +++ b/params/config.go @@ -69,7 +69,7 @@ var ( ShanghaiTime: utils.TimeToNewUint64(upgrade.GetConfig(constants.UnitTestID).DurangoTime), CancunTime: utils.TimeToNewUint64(upgrade.GetConfig(constants.UnitTestID).EtnaTime), }, - &extras.ChainConfigExtra{ + &extras.ChainConfig{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), @@ -102,7 +102,7 @@ var ( IstanbulBlock: big.NewInt(0), MuirGlacierBlock: big.NewInt(0), }, - &extras.ChainConfigExtra{ + &extras.ChainConfig{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: nil, @@ -135,7 +135,7 @@ var ( IstanbulBlock: big.NewInt(0), MuirGlacierBlock: big.NewInt(0), }, - &extras.ChainConfigExtra{ + &extras.ChainConfig{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), @@ -169,7 +169,7 @@ var ( MuirGlacierBlock: big.NewInt(0), BerlinBlock: big.NewInt(0), }, - &extras.ChainConfigExtra{ + &extras.ChainConfig{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), @@ -204,7 +204,7 @@ var ( BerlinBlock: big.NewInt(0), LondonBlock: big.NewInt(0), }, - &extras.ChainConfigExtra{ + &extras.ChainConfig{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), @@ -239,7 +239,7 @@ var ( BerlinBlock: big.NewInt(0), LondonBlock: big.NewInt(0), }, - &extras.ChainConfigExtra{ + &extras.ChainConfig{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), @@ -274,7 +274,7 @@ var ( BerlinBlock: big.NewInt(0), LondonBlock: big.NewInt(0), }, - &extras.ChainConfigExtra{ + &extras.ChainConfig{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), @@ -309,7 +309,7 @@ var ( BerlinBlock: big.NewInt(0), LondonBlock: big.NewInt(0), }, - &extras.ChainConfigExtra{ + &extras.ChainConfig{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), @@ -344,7 +344,7 @@ var ( BerlinBlock: big.NewInt(0), LondonBlock: big.NewInt(0), }, - &extras.ChainConfigExtra{ + &extras.ChainConfig{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), @@ -379,7 +379,7 @@ var ( BerlinBlock: big.NewInt(0), LondonBlock: big.NewInt(0), }, - &extras.ChainConfigExtra{ + &extras.ChainConfig{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), @@ -414,7 +414,7 @@ var ( BerlinBlock: big.NewInt(0), LondonBlock: big.NewInt(0), }, - &extras.ChainConfigExtra{ + &extras.ChainConfig{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), @@ -449,7 +449,7 @@ var ( BerlinBlock: big.NewInt(0), LondonBlock: big.NewInt(0), }, - &extras.ChainConfigExtra{ + &extras.ChainConfig{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), @@ -485,7 +485,7 @@ var ( LondonBlock: big.NewInt(0), ShanghaiTime: utils.NewUint64(0), }, - &extras.ChainConfigExtra{ + &extras.ChainConfig{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), @@ -522,7 +522,7 @@ var ( ShanghaiTime: utils.NewUint64(0), CancunTime: utils.NewUint64(0), }, - &extras.ChainConfigExtra{ + &extras.ChainConfig{ AvalancheContext: extras.AvalancheContext{SnowCtx: utils.TestSnowContext()}, NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), diff --git a/params/config_extra.go b/params/config_extra.go index 3357639989..a9e5b0bdd9 100644 --- a/params/config_extra.go +++ b/params/config_extra.go @@ -58,10 +58,10 @@ func SetEthUpgrades(c *ChainConfig) { } } -func GetExtra(c *ChainConfig) *extras.ChainConfigExtra { +func GetExtra(c *ChainConfig) *extras.ChainConfig { ex := payloads.FromChainConfig(c) if ex == nil { - ex = &extras.ChainConfigExtra{} + ex = &extras.ChainConfig{} payloads.SetOnChainConfig(c, ex) } return ex @@ -74,7 +74,7 @@ func Copy(c *ChainConfig) ChainConfig { } // WithExtra sets the extra payload on `c` and returns the modified argument. -func WithExtra(c *ChainConfig, extra *extras.ChainConfigExtra) *ChainConfig { +func WithExtra(c *ChainConfig, extra *extras.ChainConfig) *ChainConfig { payloads.SetOnChainConfig(c, extra) return c } @@ -164,7 +164,7 @@ func GetChainConfig(agoUpgrade upgrade.Config, chainID *big.Int) *ChainConfig { IstanbulBlock: big.NewInt(0), MuirGlacierBlock: big.NewInt(0), }, - &extras.ChainConfigExtra{ + &extras.ChainConfig{ NetworkUpgrades: extras.GetNetworkUpgrades(agoUpgrade), }, ) diff --git a/params/config_libevm.go b/params/config_libevm.go index 16057b5199..6ab46dbe7e 100644 --- a/params/config_libevm.go +++ b/params/config_libevm.go @@ -16,19 +16,19 @@ import ( // libevmInit would ideally be a regular init() function, but it MUST be run // before any calls to [ChainConfig.Rules]. See `config.go` for its call site. func libevmInit() any { - payloads = ethparams.RegisterExtras(ethparams.Extras[*extras.ChainConfigExtra, RulesExtra]{ + payloads = ethparams.RegisterExtras(ethparams.Extras[*extras.ChainConfig, RulesExtra]{ ReuseJSONRoot: true, // Reuse the root JSON input when unmarshalling the extra payload. NewRules: constructRulesExtra, }) return nil } -var payloads ethparams.ExtraPayloads[*extras.ChainConfigExtra, RulesExtra] +var payloads ethparams.ExtraPayloads[*extras.ChainConfig, RulesExtra] // constructRulesExtra acts as an adjunct to the [params.ChainConfig.Rules] // method. Its primary purpose is to construct the extra payload for the // [params.Rules] but it MAY also modify the [params.Rules]. -func constructRulesExtra(c *ethparams.ChainConfig, r *ethparams.Rules, cEx *extras.ChainConfigExtra, blockNum *big.Int, isMerge bool, timestamp uint64) RulesExtra { +func constructRulesExtra(c *ethparams.ChainConfig, r *ethparams.Rules, cEx *extras.ChainConfig, blockNum *big.Int, isMerge bool, timestamp uint64) RulesExtra { var rules RulesExtra if cEx == nil { return rules diff --git a/params/config_test.go b/params/config_test.go index 43de160006..3ad2097a07 100644 --- a/params/config_test.go +++ b/params/config_test.go @@ -147,7 +147,7 @@ func TestCheckCompatible(t *testing.T) { func TestConfigRules(t *testing.T) { c := WithExtra( &ChainConfig{}, - &extras.ChainConfigExtra{ + &extras.ChainConfig{ NetworkUpgrades: extras.NetworkUpgrades{ CortinaBlockTimestamp: utils.NewUint64(500), }, diff --git a/params/extras/config_extra.go b/params/extras/config.go similarity index 92% rename from params/extras/config_extra.go rename to params/extras/config.go index 81adeeb1b0..33f03bd1e1 100644 --- a/params/extras/config_extra.go +++ b/params/extras/config.go @@ -26,7 +26,7 @@ type AvalancheContext struct { SnowCtx *snow.Context } -type ChainConfigExtra struct { +type ChainConfig struct { NetworkUpgrades // Config for timestamps that enable network upgrades. Skip encoding/decoding directly into ChainConfig. AvalancheContext `json:"-"` // Avalanche specific context set during VM initialization. Not serialized. @@ -34,11 +34,11 @@ type ChainConfigExtra struct { UpgradeConfig `json:"-"` // Config specified in upgradeBytes (avalanche network upgrades or enable/disabling precompiles). Skip encoding/decoding directly into ChainConfig. } -func (c *ChainConfigExtra) CheckConfigCompatible(newcfg_ *ethparams.ChainConfig, headNumber *big.Int, headTimestamp uint64) *ConfigCompatError { +func (c *ChainConfig) CheckConfigCompatible(newcfg_ *ethparams.ChainConfig, headNumber *big.Int, headTimestamp uint64) *ConfigCompatError { if c == nil { return nil } - newcfg, ok := newcfg_.Hooks().(*ChainConfigExtra) + newcfg, ok := newcfg_.Hooks().(*ChainConfig) if !ok { // XXX: if libevm is implemented properly then this can never happen, but that's only a runtime guarantee so shouldn't be relied upon } @@ -57,7 +57,7 @@ func (c *ChainConfigExtra) CheckConfigCompatible(newcfg_ *ethparams.ChainConfig, return nil } -func (c *ChainConfigExtra) Description() string { +func (c *ChainConfig) Description() string { if c == nil { return "" } @@ -133,25 +133,25 @@ func newTimestampCompatError(what string, storedtime, newtime *uint64) *ConfigCo // This is a custom unmarshaler to handle the Precompiles field. // Precompiles was presented as an inline object in the JSON. // This custom unmarshaler ensures backwards compatibility with the old format. -func (c *ChainConfigExtra) UnmarshalJSON(data []byte) error { +func (c *ChainConfig) UnmarshalJSON(data []byte) error { // Alias ChainConfigExtra to avoid recursion - type _ChainConfigExtra ChainConfigExtra + type _ChainConfigExtra ChainConfig tmp := _ChainConfigExtra{} if err := json.Unmarshal(data, &tmp); err != nil { return err } // At this point we have populated all fields except PrecompileUpgrade - *c = ChainConfigExtra(tmp) + *c = ChainConfig(tmp) return nil } // MarshalJSON returns the JSON encoding of c. // This is a custom marshaler to handle the Precompiles field. -func (c *ChainConfigExtra) MarshalJSON() ([]byte, error) { +func (c *ChainConfig) MarshalJSON() ([]byte, error) { // Alias ChainConfigExtra to avoid recursion - type _ChainConfigExtra ChainConfigExtra + type _ChainConfigExtra ChainConfig return json.Marshal(_ChainConfigExtra(*c)) } @@ -162,7 +162,7 @@ type fork struct { optional bool // if true, the fork may be nil and next fork is still allowed } -func (c *ChainConfigExtra) CheckConfigForkOrder() error { +func (c *ChainConfig) CheckConfigForkOrder() error { if c == nil { return nil } @@ -224,7 +224,7 @@ func checkForks(forks []fork, blockFork bool) error { } // Verify verifies chain config and returns error -func (c *ChainConfigExtra) Verify() error { +func (c *ChainConfig) Verify() error { // Verify the precompile upgrades are internally consistent given the existing chainConfig. if err := c.verifyPrecompileUpgrades(); err != nil { return fmt.Errorf("invalid precompile upgrades: %w", err) @@ -234,7 +234,7 @@ func (c *ChainConfigExtra) Verify() error { } // IsPrecompileEnabled returns whether precompile with [address] is enabled at [timestamp]. -func (c *ChainConfigExtra) IsPrecompileEnabled(address common.Address, timestamp uint64) bool { +func (c *ChainConfig) IsPrecompileEnabled(address common.Address, timestamp uint64) bool { config := c.GetActivePrecompileConfig(address, timestamp) return config != nil && !config.IsDisabled() } diff --git a/params/extras/precompile_upgrade.go b/params/extras/precompile_upgrade.go index e27c746632..6f1522673d 100644 --- a/params/extras/precompile_upgrade.go +++ b/params/extras/precompile_upgrade.go @@ -67,7 +67,7 @@ func (u *PrecompileUpgrade) MarshalJSON() ([]byte, error) { // - the specified blockTimestamps must be compatible with those // specified in the chainConfig by genesis. // - check a precompile is disabled before it is re-enabled -func (c *ChainConfigExtra) verifyPrecompileUpgrades() error { +func (c *ChainConfig) verifyPrecompileUpgrades() error { // Store this struct to keep track of the last upgrade for each precompile key. // Required for timestamp and disabled checks. type lastUpgradeData struct { @@ -133,7 +133,7 @@ func (c *ChainConfigExtra) verifyPrecompileUpgrades() error { // GetActivePrecompileConfig returns the most recent precompile config corresponding to [address]. // If none have occurred, returns nil. -func (c *ChainConfigExtra) GetActivePrecompileConfig(address common.Address, timestamp uint64) precompileconfig.Config { +func (c *ChainConfig) GetActivePrecompileConfig(address common.Address, timestamp uint64) precompileconfig.Config { configs := c.GetActivatingPrecompileConfigs(address, nil, timestamp, c.PrecompileUpgrades) if len(configs) == 0 { return nil @@ -143,7 +143,7 @@ func (c *ChainConfigExtra) GetActivePrecompileConfig(address common.Address, tim // GetActivatingPrecompileConfigs returns all precompile upgrades configured to activate during the // state transition from a block with timestamp [from] to a block with timestamp [to]. -func (c *ChainConfigExtra) GetActivatingPrecompileConfigs(address common.Address, from *uint64, to uint64, upgrades []PrecompileUpgrade) []precompileconfig.Config { +func (c *ChainConfig) GetActivatingPrecompileConfigs(address common.Address, from *uint64, to uint64, upgrades []PrecompileUpgrade) []precompileconfig.Config { // Get key from address. module, ok := modules.GetPrecompileModuleByAddress(address) if !ok { @@ -172,7 +172,7 @@ func (c *ChainConfigExtra) GetActivatingPrecompileConfigs(address common.Address // new upgrade to be applied as long as it activates after the last accepted block. // //nolint:unused -func (c *ChainConfigExtra) checkPrecompilesCompatible(precompileUpgrades []PrecompileUpgrade, time uint64) *ConfigCompatError { +func (c *ChainConfig) checkPrecompilesCompatible(precompileUpgrades []PrecompileUpgrade, time uint64) *ConfigCompatError { for _, module := range modules.RegisteredModules() { if err := c.checkPrecompileCompatible(module.Address, precompileUpgrades, time); err != nil { return err @@ -188,7 +188,7 @@ func (c *ChainConfigExtra) checkPrecompilesCompatible(precompileUpgrades []Preco // Upgrades that have already gone into effect cannot be modified or absent from [precompileUpgrades]. // //nolint:unused -func (c *ChainConfigExtra) checkPrecompileCompatible(address common.Address, precompileUpgrades []PrecompileUpgrade, time uint64) *ConfigCompatError { +func (c *ChainConfig) checkPrecompileCompatible(address common.Address, precompileUpgrades []PrecompileUpgrade, time uint64) *ConfigCompatError { // All active upgrades (from nil to [lastTimestamp]) must match. activeUpgrades := c.GetActivatingPrecompileConfigs(address, nil, time, c.PrecompileUpgrades) newUpgrades := c.GetActivatingPrecompileConfigs(address, nil, time, precompileUpgrades) @@ -226,7 +226,7 @@ func (c *ChainConfigExtra) checkPrecompileCompatible(address common.Address, pre } // EnabledStatefulPrecompiles returns current stateful precompile configs that are enabled at [blockTimestamp]. -func (c *ChainConfigExtra) EnabledStatefulPrecompiles(blockTimestamp uint64) Precompiles { +func (c *ChainConfig) EnabledStatefulPrecompiles(blockTimestamp uint64) Precompiles { statefulPrecompileConfigs := make(Precompiles) for _, module := range modules.RegisteredModules() { if config := c.GetActivePrecompileConfig(module.Address, blockTimestamp); config != nil && !config.IsDisabled() { diff --git a/plugin/evm/vm.go b/plugin/evm/vm.go index a254d1d3e3..efd2617414 100644 --- a/plugin/evm/vm.go +++ b/plugin/evm/vm.go @@ -1947,7 +1947,7 @@ func (vm *VM) GetCurrentNonce(address common.Address) (uint64, error) { return state.GetNonce(address), nil } -func (vm *VM) chainConfigExtra() *extras.ChainConfigExtra { +func (vm *VM) chainConfigExtra() *extras.ChainConfig { return params.GetExtra(vm.chainConfig) } diff --git a/tests/init.go b/tests/init.go index aaff83aab8..5ec587e6d4 100644 --- a/tests/init.go +++ b/tests/init.go @@ -178,7 +178,7 @@ var Forks = map[string]*params.ChainConfig{ PetersburgBlock: big.NewInt(0), IstanbulBlock: big.NewInt(0), }, - &extras.ChainConfigExtra{ + &extras.ChainConfig{ NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), }, @@ -198,7 +198,7 @@ var Forks = map[string]*params.ChainConfig{ MuirGlacierBlock: big.NewInt(0), BerlinBlock: big.NewInt(0), }, - &extras.ChainConfigExtra{ + &extras.ChainConfig{ NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), @@ -220,7 +220,7 @@ var Forks = map[string]*params.ChainConfig{ BerlinBlock: big.NewInt(0), LondonBlock: big.NewInt(0), }, - &extras.ChainConfigExtra{ + &extras.ChainConfig{ NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), @@ -243,7 +243,7 @@ var Forks = map[string]*params.ChainConfig{ BerlinBlock: big.NewInt(0), LondonBlock: big.NewInt(0), }, - &extras.ChainConfigExtra{ + &extras.ChainConfig{ NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), @@ -266,7 +266,7 @@ var Forks = map[string]*params.ChainConfig{ BerlinBlock: big.NewInt(0), LondonBlock: big.NewInt(0), }, - &extras.ChainConfigExtra{ + &extras.ChainConfig{ NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), @@ -290,7 +290,7 @@ var Forks = map[string]*params.ChainConfig{ BerlinBlock: big.NewInt(0), LondonBlock: big.NewInt(0), }, - &extras.ChainConfigExtra{ + &extras.ChainConfig{ NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), @@ -315,7 +315,7 @@ var Forks = map[string]*params.ChainConfig{ BerlinBlock: big.NewInt(0), LondonBlock: big.NewInt(0), }, - &extras.ChainConfigExtra{ + &extras.ChainConfig{ NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), @@ -341,7 +341,7 @@ var Forks = map[string]*params.ChainConfig{ BerlinBlock: big.NewInt(0), LondonBlock: big.NewInt(0), }, - &extras.ChainConfigExtra{ + &extras.ChainConfig{ NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), @@ -370,7 +370,7 @@ var Forks = map[string]*params.ChainConfig{ ShanghaiTime: utils.NewUint64(0), CancunTime: utils.NewUint64(0), }, - &extras.ChainConfigExtra{ + &extras.ChainConfig{ NetworkUpgrades: extras.NetworkUpgrades{ ApricotPhase1BlockTimestamp: utils.NewUint64(0), ApricotPhase2BlockTimestamp: utils.NewUint64(0), From 5d0e959f3500367fc9ed74321167b79641b5e0d6 Mon Sep 17 00:00:00 2001 From: Darioush Jalali Date: Tue, 17 Dec 2024 12:26:43 -0800 Subject: [PATCH 14/18] return error if chain config type does not match --- params/extras/config.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/params/extras/config.go b/params/extras/config.go index 33f03bd1e1..a6bd574c74 100644 --- a/params/extras/config.go +++ b/params/extras/config.go @@ -9,6 +9,7 @@ import ( "math/big" "github.com/ava-labs/avalanchego/snow" + "github.com/ava-labs/coreth/utils" "github.com/ava-labs/libevm/common" ethparams "github.com/ava-labs/libevm/params" ) @@ -40,7 +41,13 @@ func (c *ChainConfig) CheckConfigCompatible(newcfg_ *ethparams.ChainConfig, head } newcfg, ok := newcfg_.Hooks().(*ChainConfig) if !ok { - // XXX: if libevm is implemented properly then this can never happen, but that's only a runtime guarantee so shouldn't be relied upon + // Proper registration of the extras on libevm side should prevent this from happening. + // Return an error to prevent the chain from starting, just in case. + return newTimestampCompatError( + fmt.Sprintf("ChainConfig is not of the expected type *extras.ChainConfig, got %T", newcfg_.Hooks()), + utils.NewUint64(0), + nil, + ) } // Check avalanche network upgrades From a3d4938eae8bca95bd56c404f828748ffb0eec6c Mon Sep 17 00:00:00 2001 From: Darioush Jalali Date: Wed, 18 Dec 2024 06:58:01 -0800 Subject: [PATCH 15/18] nit --- params/extras/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/params/extras/config.go b/params/extras/config.go index a6bd574c74..a69c0a1cf4 100644 --- a/params/extras/config.go +++ b/params/extras/config.go @@ -44,7 +44,7 @@ func (c *ChainConfig) CheckConfigCompatible(newcfg_ *ethparams.ChainConfig, head // Proper registration of the extras on libevm side should prevent this from happening. // Return an error to prevent the chain from starting, just in case. return newTimestampCompatError( - fmt.Sprintf("ChainConfig is not of the expected type *extras.ChainConfig, got %T", newcfg_.Hooks()), + fmt.Sprintf("ChainConfig.Hooks() is not of the expected type *extras.ChainConfig, got %T", newcfg_.Hooks()), utils.NewUint64(0), nil, ) From 7877225f324ba65e3ce20b48a3fb052efd214c70 Mon Sep 17 00:00:00 2001 From: Darioush Jalali Date: Wed, 18 Dec 2024 07:53:21 -0800 Subject: [PATCH 16/18] comment and style improvements --- params/extras/config.go | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/params/extras/config.go b/params/extras/config.go index a69c0a1cf4..fe9e3e1574 100644 --- a/params/extras/config.go +++ b/params/extras/config.go @@ -28,11 +28,11 @@ type AvalancheContext struct { } type ChainConfig struct { - NetworkUpgrades // Config for timestamps that enable network upgrades. Skip encoding/decoding directly into ChainConfig. + NetworkUpgrades // Config for timestamps that enable network upgrades. AvalancheContext `json:"-"` // Avalanche specific context set during VM initialization. Not serialized. - UpgradeConfig `json:"-"` // Config specified in upgradeBytes (avalanche network upgrades or enable/disabling precompiles). Skip encoding/decoding directly into ChainConfig. + UpgradeConfig `json:"-"` // Config specified in upgradeBytes (avalanche network upgrades or enable/disabling precompiles). Not serialized. } func (c *ChainConfig) CheckConfigCompatible(newcfg_ *ethparams.ChainConfig, headNumber *big.Int, headTimestamp uint64) *ConfigCompatError { @@ -50,7 +50,6 @@ func (c *ChainConfig) CheckConfigCompatible(newcfg_ *ethparams.ChainConfig, head ) } - // Check avalanche network upgrades if err := c.checkNetworkUpgradesCompatible(&newcfg.NetworkUpgrades, headTimestamp); err != nil { return err } @@ -113,6 +112,8 @@ func configTimestampEqual(x, y *uint64) bool { // ChainConfig that would alter the past. type ConfigCompatError = ethparams.ConfigCompatError +// newTimestampCompatError is taken verbatim from upstream. +// TODO: export this function from upstream in libevm, so it can be used here. func newTimestampCompatError(what string, storedtime, newtime *uint64) *ConfigCompatError { var rew *uint64 switch { @@ -173,23 +174,20 @@ func (c *ChainConfig) CheckConfigForkOrder() error { if c == nil { return nil } - // Note: In Avalanche, hard forks must take place via block timestamps instead - // of block numbers since blocks are produced asynchronously. Therefore, we do not - // check that the block timestamps in the same way as for - // the block number forks since it would not be a meaningful comparison. - // Instead, we check only that Phases are enabled in order. - // Note: we do not add the optional stateful precompile configs in here because they are optional - // and independent, such that the ordering they are enabled does not impact the correctness of the - // chain config. - if err := checkForks(c.forkOrder(), false); err != nil { - return err - } - - return nil + // Note: In Avalanche, upgrades must take place via block timestamps instead + // of block numbers since blocks are produced asynchronously. Therefore, we do + // not check block timestamp forks in the same way as block number forks since + // it would not be a meaningful comparison. Instead, we check only that + // Phases are enabled in order. + // Note: we do not add the precompile configs here because they are optional + // and independent, i.e. the order in which they are enabled does not impact + // the correctness of the chain config. + return checkForks(c.forkOrder(), false) } -// checkForks checks that forks are enabled in order and returns an error if not +// checkForks checks that forks are enabled in order and returns an error if not. // [blockFork] is true if the fork is a block number fork, false if it is a timestamp fork +// TODO: This code was adapted from CheckConfigForkOrder, consider refactoring to avoid duplication. func checkForks(forks []fork, blockFork bool) error { lastFork := fork{} for _, cur := range forks { @@ -230,7 +228,7 @@ func checkForks(forks []fork, blockFork bool) error { return nil } -// Verify verifies chain config and returns error +// Verify verifies chain config. func (c *ChainConfig) Verify() error { // Verify the precompile upgrades are internally consistent given the existing chainConfig. if err := c.verifyPrecompileUpgrades(); err != nil { @@ -250,7 +248,9 @@ func (c *ChainConfig) IsPrecompileEnabled(address common.Address, timestamp uint // [parent] to [current]. // Taking [parent] as a pointer allows for us to pass nil when checking forks // that activate during genesis. -// Note: this works for both block number and timestamp activated forks. +// Note: [parent] and [current] can be either both timestamp values, or both +// block number values, since this function works for both block number and +// timestamp activated forks. func IsForkTransition(fork *uint64, parent *uint64, current uint64) bool { var parentForked bool if parent != nil { From d88c7477a3ecf6b09be32f4670f5c5c4092069f9 Mon Sep 17 00:00:00 2001 From: Darioush Jalali Date: Wed, 18 Dec 2024 07:58:20 -0800 Subject: [PATCH 17/18] nit --- params/extras/config.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/params/extras/config.go b/params/extras/config.go index fe9e3e1574..c8d346e3c9 100644 --- a/params/extras/config.go +++ b/params/extras/config.go @@ -177,8 +177,8 @@ func (c *ChainConfig) CheckConfigForkOrder() error { // Note: In Avalanche, upgrades must take place via block timestamps instead // of block numbers since blocks are produced asynchronously. Therefore, we do // not check block timestamp forks in the same way as block number forks since - // it would not be a meaningful comparison. Instead, we check only that - // Phases are enabled in order. + // it would not be a meaningful comparison. Instead, we only check that the + // Avalanche upgrades are enabled in order. // Note: we do not add the precompile configs here because they are optional // and independent, i.e. the order in which they are enabled does not impact // the correctness of the chain config. From 917384e999e1507e43a968cf93721a1914e553a8 Mon Sep 17 00:00:00 2001 From: Darioush Jalali Date: Wed, 18 Dec 2024 07:59:07 -0800 Subject: [PATCH 18/18] nit --- params/extras/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/params/extras/config.go b/params/extras/config.go index c8d346e3c9..14b17bc56e 100644 --- a/params/extras/config.go +++ b/params/extras/config.go @@ -249,7 +249,7 @@ func (c *ChainConfig) IsPrecompileEnabled(address common.Address, timestamp uint // Taking [parent] as a pointer allows for us to pass nil when checking forks // that activate during genesis. // Note: [parent] and [current] can be either both timestamp values, or both -// block number values, since this function works for both block number and +// block number values, since this function works for both block number and // timestamp activated forks. func IsForkTransition(fork *uint64, parent *uint64, current uint64) bool { var parentForked bool