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

Review of params package in #662 #674

Merged
merged 7 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ var (
AvalancheLocalChainID = big.NewInt(43112)
)

var (
_ = do_init() // XXX: is (temporarily) here because type registration must proceed the call to .Rules()
// Guarantees extras initialisation before a call to [ChainConfig.Rules].
var _ = libevmInit()

var (
TestChainConfig = WithExtra(
&ChainConfig{
ChainID: big.NewInt(1),
Expand Down
20 changes: 11 additions & 9 deletions params/config_extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,29 @@ func SetEthUpgrades(c *ChainConfig) {
// number of 0 as well.
initiallyActive := uint64(upgrade.InitiallyActiveTime.Unix())
extra := GetExtra(c)
if extra != nil && extra.ApricotPhase2BlockTimestamp != nil && *extra.ApricotPhase2BlockTimestamp <= initiallyActive && c.BerlinBlock == nil {
if extra.ApricotPhase2BlockTimestamp != nil && *extra.ApricotPhase2BlockTimestamp <= initiallyActive && c.BerlinBlock == nil {
c.BerlinBlock = big.NewInt(0)
}
if extra != nil && extra.ApricotPhase3BlockTimestamp != nil && *extra.ApricotPhase3BlockTimestamp <= initiallyActive && c.LondonBlock == nil {
if extra.ApricotPhase3BlockTimestamp != nil && *extra.ApricotPhase3BlockTimestamp <= initiallyActive && c.LondonBlock == nil {
c.LondonBlock = big.NewInt(0)
}
}
extra := GetExtra(c)
if extra != nil && extra.DurangoBlockTimestamp != nil {
if extra.DurangoBlockTimestamp != nil {
c.ShanghaiTime = utils.NewUint64(*extra.DurangoBlockTimestamp)
}
if extra != nil && extra.EtnaTimestamp != nil {
if extra.EtnaTimestamp != nil {
c.CancunTime = utils.NewUint64(*extra.EtnaTimestamp)
}
}

func GetExtra(c *ChainConfig) *ChainConfigExtra {
if extra := FromChainConfig(c); extra != nil {
return extra
ex := extras.FromChainConfig(c)
if ex == nil {
ex = &ChainConfigExtra{}
extras.SetOnChainConfig(c, ex)
}
return &ChainConfigExtra{}
return ex
}

func Copy(c *ChainConfig) ChainConfig {
Expand All @@ -85,6 +87,7 @@ func Copy(c *ChainConfig) ChainConfig {
return *WithExtra(&cpy, &extraCpy)
}

// WithExtra sets the extra payload on `c` and returns the modified argument.
func WithExtra(c *ChainConfig, extra *ChainConfigExtra) *ChainConfig {
extras.SetOnChainConfig(c, extra)
return c
Expand Down Expand Up @@ -370,7 +373,7 @@ func ToWithUpgradesJSON(c *ChainConfig) *ChainConfigWithUpgradesJSON {
}

func GetChainConfig(agoUpgrade upgrade.Config, chainID *big.Int) *ChainConfig {
c := WithExtra(
return WithExtra(
&ChainConfig{
ChainID: chainID,
HomesteadBlock: big.NewInt(0),
Expand All @@ -389,7 +392,6 @@ func GetChainConfig(agoUpgrade upgrade.Config, chainID *big.Int) *ChainConfig {
NetworkUpgrades: getNetworkUpgrades(agoUpgrade),
},
)
return c
}

func ptrToString(val *uint64) string {
Expand Down
16 changes: 3 additions & 13 deletions params/config_libevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
gethparams "github.com/ethereum/go-ethereum/params"
)

func do_init() any {
// 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 = gethparams.RegisterExtras(gethparams.Extras[*ChainConfigExtra, RulesExtra]{
ReuseJSONRoot: true, // Reuse the root JSON input when unmarshalling the extra payload.
NewRules: constructRulesExtra,
Expand All @@ -31,8 +33,6 @@ func constructRulesExtra(c *gethparams.ChainConfig, r *gethparams.Rules, cEx *Ch
return rules
}
rules.AvalancheRules = cEx.GetAvalancheRules(timestamp)
rules.chainConfig = c
rules.gethrules = *r

// Initialize the stateful precompiles that should be enabled at [blockTimestamp].
rules.Precompiles = make(map[common.Address]precompileconfig.Config)
Expand All @@ -52,13 +52,3 @@ func constructRulesExtra(c *gethparams.ChainConfig, r *gethparams.Rules, cEx *Ch

return rules
}

// FromChainConfig returns the extra payload carried by the ChainConfig.
func FromChainConfig(c *gethparams.ChainConfig) *ChainConfigExtra {
return extras.FromChainConfig(c)
}

// FromRules returns the extra payload carried by the Rules.
func FromRules(r *gethparams.Rules) RulesExtra {
return extras.FromRules(r)
}
10 changes: 3 additions & 7 deletions params/hooks_libevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/libevm"
gethparams "github.com/ethereum/go-ethereum/params"
"github.com/holiman/uint256"
"golang.org/x/exp/maps"
)
Expand Down Expand Up @@ -112,8 +111,7 @@ func makePrecompile(contract contract.StatefulPrecompiledContract) libevm.Precom
predicateResultsBytes = header.Extra[DynamicFeeExtraDataSize:]
}
accessableState := accessableState{
env: env,
chainConfig: GetRulesExtra(env.Rules()).chainConfig,
env: env,
blockContext: &BlockContext{
number: env.BlockNumber(),
time: env.BlockTime(),
Expand Down Expand Up @@ -142,7 +140,6 @@ func (r RulesExtra) PrecompileOverride(addr common.Address) (libevm.PrecompiledC

type accessableState struct {
env vm.PrecompileEnvironment
chainConfig *gethparams.ChainConfig
blockContext *BlockContext
}

Expand All @@ -162,12 +159,11 @@ func (a accessableState) GetBlockContext() contract.BlockContext {
}

func (a accessableState) GetChainConfig() precompileconfig.ChainConfig {
extra := GetExtra(a.chainConfig)
return extra
return GetExtra(a.env.ChainConfig())
}

func (a accessableState) GetSnowContext() *snow.Context {
return GetExtra(a.chainConfig).SnowCtx
return GetExtra(a.env.ChainConfig()).SnowCtx
}

func (a accessableState) NativeAssetCall(caller common.Address, input []byte, suppliedGas uint64, gasCost uint64, readOnly bool) (ret []byte, remainingGas uint64, err error) {
Expand Down
11 changes: 3 additions & 8 deletions params/rules_extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,13 @@ package params
import (
"github.com/ava-labs/coreth/precompile/precompileconfig"
"github.com/ethereum/go-ethereum/common"
gethparams "github.com/ethereum/go-ethereum/params"
)

func GetRulesExtra(r Rules) *RulesExtra {
extra := FromRules(&r)
return &extra
return extras.PointerFromRules(&r)
}

type RulesExtra struct {
chainConfig *ChainConfig
gethrules gethparams.Rules

// Rules for Avalanche releases
AvalancheRules

Expand All @@ -39,8 +34,8 @@ func (r *RulesExtra) PredicatersExist() bool {
}

func (r *RulesExtra) PredicaterExists(addr common.Address) bool {
_, PredicaterExists := r.Predicaters[addr]
return PredicaterExists
_, ok := r.Predicaters[addr]
return ok
}

// IsPrecompileEnabled returns true if the precompile at [addr] is enabled for this rule set.
Expand Down
Loading