Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: params.ChainConfigExtra & RulesExtra -> params/extras #702

Merged
merged 19 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ func TestUngracefulAsyncShutdown(t *testing.T) {
gspec := &Genesis{
Config: params.WithExtra(
&params.ChainConfig{HomesteadBlock: new(big.Int)},
&extras.ChainConfigExtra{},
&extras.ChainConfig{},
),
Alloc: types.GenesisAlloc{addr1: {Balance: genesisBalance}},
}
Expand Down
2 changes: 1 addition & 1 deletion core/genesis_extra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion core/state_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion core/vm/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
28 changes: 14 additions & 14 deletions params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down
8 changes: 4 additions & 4 deletions params/config_extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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),
},
)
Expand Down
11 changes: 3 additions & 8 deletions params/config_libevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,22 @@ 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 {
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
Expand Down
2 changes: 1 addition & 1 deletion params/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
Expand Down
37 changes: 22 additions & 15 deletions params/extras/config_extra.go → params/extras/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ 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"
)

// 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.
Expand All @@ -29,19 +27,28 @@ 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.

UpgradeConfig `json:"-"` // Config specified in upgradeBytes (avalanche network upgrades or enable/disabling precompiles). Skip encoding/decoding directly into ChainConfig.
darioush marked this conversation as resolved.
Show resolved Hide resolved
}

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 := GetExtra(newcfg_)
newcfg, ok := newcfg_.Hooks().(*ChainConfig)
if !ok {
// 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()),
darioush marked this conversation as resolved.
Show resolved Hide resolved
utils.NewUint64(0),
nil,
)
}

// Check avalanche network upgrades
if err := c.checkNetworkUpgradesCompatible(&newcfg.NetworkUpgrades, headTimestamp); err != nil {
Expand All @@ -57,7 +64,7 @@ func (c *ChainConfigExtra) CheckConfigCompatible(newcfg_ *ethparams.ChainConfig,
return nil
}

func (c *ChainConfigExtra) Description() string {
func (c *ChainConfig) Description() string {
if c == nil {
return ""
}
Expand Down Expand Up @@ -133,25 +140,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))
}

Expand All @@ -162,7 +169,7 @@ type fork struct {
optional bool // if true, the fork may be nil and next fork is still allowed
qdm12 marked this conversation as resolved.
Show resolved Hide resolved
}

func (c *ChainConfigExtra) CheckConfigForkOrder() error {
func (c *ChainConfig) CheckConfigForkOrder() error {
if c == nil {
return nil
}
Expand Down Expand Up @@ -224,7 +231,7 @@ func checkForks(forks []fork, blockFork bool) error {
}

// Verify verifies chain config and returns error
qdm12 marked this conversation as resolved.
Show resolved Hide resolved
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)
Expand All @@ -234,7 +241,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()
}
Expand Down
Loading
Loading