Skip to content
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
3 changes: 2 additions & 1 deletion consensus/dummy/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/ava-labs/subnet-evm/core/state"
"github.com/ava-labs/subnet-evm/core/types"
"github.com/ava-labs/subnet-evm/params"
"github.com/ava-labs/subnet-evm/precompile"
"github.com/ava-labs/subnet-evm/trie"
"github.com/ava-labs/subnet-evm/vmerrs"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -198,7 +199,7 @@ func (self *DummyEngine) verifyHeader(chain consensus.ChainHeaderReader, header
}
// Ensure that coinbase is valid if reward manager is enabled
// If reward manager is disabled, this will be handled in syntactic verification
if config.IsRewardManager(timestamp) {
if config.IsPrecompileEnabled(precompile.RewardManagerAddress, timestamp) {
if err := self.verifyCoinbase(config, header, parent, chain); err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions core/blockchain_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ func (bc *BlockChain) SubscribeAcceptedTransactionEvent(ch chan<- NewTxsEvent) e
func (bc *BlockChain) GetFeeConfigAt(parent *types.Header) (commontype.FeeConfig, *big.Int, error) {
config := bc.Config()
bigTime := new(big.Int).SetUint64(parent.Time)
if !config.IsFeeConfigManager(bigTime) {
if !config.IsPrecompileEnabled(precompile.FeeConfigManagerAddress, bigTime) {
return config.FeeConfig, common.Big0, nil
}

Expand Down Expand Up @@ -390,7 +390,7 @@ func (bc *BlockChain) GetCoinbaseAt(parent *types.Header) (common.Address, bool,
return constants.BlackholeAddr, false, nil
}

if !config.IsRewardManager(bigTime) {
if !config.IsPrecompileEnabled(precompile.RewardManagerAddress, bigTime) {
if bc.chainConfig.AllowFeeRecipients {
return common.Address{}, true, nil
} else {
Expand Down
2 changes: 1 addition & 1 deletion core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func (st *StateTransition) preCheck() error {
}

// Check that the sender is on the tx allow list if enabled
if st.evm.ChainConfig().IsTxAllowList(st.evm.Context.Time) {
if st.evm.ChainConfig().IsPrecompileEnabled(precompile.TxAllowListAddress, st.evm.Context.Time) {
txAllowListRole := precompile.GetTxAllowListStatus(st.state, st.msg.From())
if !txAllowListRole.IsEnabled() {
return fmt.Errorf("%w: %s", precompile.ErrSenderAddressNotAllowListed, st.msg.From())
Expand Down
4 changes: 2 additions & 2 deletions core/tx_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ func (pool *TxPool) checkTxState(from common.Address, tx *types.Transaction) err

// If the tx allow list is enabled, return an error if the from address is not allow listed.
headTimestamp := big.NewInt(int64(pool.currentHead.Time))
if pool.chainconfig.IsTxAllowList(headTimestamp) {
if pool.chainconfig.IsPrecompileEnabled(precompile.TxAllowListAddress, headTimestamp) {
txAllowListRole := precompile.GetTxAllowListStatus(pool.currentState, from)
if !txAllowListRole.IsEnabled() {
return fmt.Errorf("%w: %s", precompile.ErrSenderAddressNotAllowListed, from)
Expand Down Expand Up @@ -1441,7 +1441,7 @@ func (pool *TxPool) reset(oldHead, newHead *types.Header) {

// when we reset txPool we should explicitly check if fee struct for min base fee has changed
// so that we can correctly drop txs with < minBaseFee from tx pool.
if pool.chainconfig.IsFeeConfigManager(new(big.Int).SetUint64(newHead.Time)) {
if pool.chainconfig.IsPrecompileEnabled(precompile.FeeConfigManagerAddress, new(big.Int).SetUint64(newHead.Time)) {
feeConfig, _, err := pool.chain.GetFeeConfigAt(newHead)
if err != nil {
log.Error("Failed to get fee config state", "err", err, "root", newHead.Root)
Expand Down
2 changes: 1 addition & 1 deletion core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64,
return nil, common.Address{}, 0, vmerrs.ErrContractAddressCollision
}
// If the allow list is enabled, check that [evm.TxContext.Origin] has permission to deploy a contract.
if evm.chainRules.IsContractDeployerAllowListEnabled {
if evm.chainRules.IsPrecompileEnabled(precompile.ContractDeployerAllowListAddress) {
allowListRole := precompile.GetContractDeployerAllowListStatus(evm.StateDB, evm.TxContext.Origin)
if !allowListRole.IsEnabled() {
return nil, common.Address{}, 0, fmt.Errorf("tx.origin %s is not authorized to deploy a contract", evm.TxContext.Origin)
Expand Down
3 changes: 2 additions & 1 deletion eth/gasprice/gasprice.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/ava-labs/subnet-evm/core"
"github.com/ava-labs/subnet-evm/core/types"
"github.com/ava-labs/subnet-evm/params"
"github.com/ava-labs/subnet-evm/precompile"
"github.com/ava-labs/subnet-evm/rpc"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
Expand Down Expand Up @@ -317,7 +318,7 @@ func (oracle *Oracle) suggestDynamicFees(ctx context.Context) (*big.Int, *big.In
feeLastChangedAt *big.Int
feeConfig commontype.FeeConfig
)
if oracle.backend.ChainConfig().IsFeeConfigManager(new(big.Int).SetUint64(head.Time)) {
if oracle.backend.ChainConfig().IsPrecompileEnabled(precompile.FeeConfigManagerAddress, new(big.Int).SetUint64(head.Time)) {
feeConfig, feeLastChangedAt, err = oracle.backend.GetFeeConfigAt(head)
if err != nil {
return nil, nil, err
Expand Down
62 changes: 8 additions & 54 deletions params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,47 +272,12 @@ func (c *ChainConfig) IsSubnetEVM(blockTimestamp *big.Int) bool {
return utils.IsForked(c.getNetworkUpgrades().SubnetEVMTimestamp, blockTimestamp)
}

// PRECOMPILE UPGRADES START HERE

// TODO: generalize these functions with IsPrecompileEnabled(address, blockTimestamp)
// IsContractDeployerAllowList returns whether [blockTimestamp] is either equal to the ContractDeployerAllowList fork block timestamp or greater.
func (c *ChainConfig) IsContractDeployerAllowList(blockTimestamp *big.Int) bool {
config := c.GetPrecompileConfig(precompile.ContractDeployerAllowListAddress, blockTimestamp)
return config != nil && !config.IsDisabled()
}

// IsContractNativeMinter returns whether [blockTimestamp] is either equal to the NativeMinter fork block timestamp or greater.
func (c *ChainConfig) IsContractNativeMinter(blockTimestamp *big.Int) bool {
config := c.GetPrecompileConfig(precompile.ContractNativeMinterAddress, blockTimestamp)
return config != nil && !config.IsDisabled()
}

// IsTxAllowList returns whether [blockTimestamp] is either equal to the TxAllowList fork block timestamp or greater.
func (c *ChainConfig) IsTxAllowList(blockTimestamp *big.Int) bool {
config := c.GetPrecompileConfig(precompile.TxAllowListAddress, blockTimestamp)
return config != nil && !config.IsDisabled()
}

// IsFeeConfigManager returns whether [blockTimestamp] is either equal to the FeeConfigManager fork block timestamp or greater.
func (c *ChainConfig) IsFeeConfigManager(blockTimestamp *big.Int) bool {
config := c.GetPrecompileConfig(precompile.FeeConfigManagerAddress, blockTimestamp)
// IsPrecompileEnabled returns whether precompile with [address] is enabled at [blockTimestamp].
func (c *ChainConfig) IsPrecompileEnabled(address common.Address, blockTimestamp *big.Int) bool {
config := c.GetPrecompileConfig(address, blockTimestamp)
return config != nil && !config.IsDisabled()
}

// IsRewardManager returns whether [blockTimestamp] is either equal to the RewardManager fork block timestamp or greater.
func (c *ChainConfig) IsRewardManager(blockTimestamp *big.Int) bool {
config := c.GetPrecompileConfig(precompile.RewardManagerAddress, blockTimestamp)
return config != nil && !config.IsDisabled()
}

// ADD YOUR PRECOMPILE HERE
/*
func (c *ChainConfig) Is{YourPrecompile}(blockTimestamp *big.Int) bool {
config := c.GetPrecompileConfig(precompile.{YourPrecompile}Address, blockTimestamp)
return config != nil && !config.IsDisabled()
}
*/

// CheckCompatible checks whether scheduled fork transitions have been imported
// with a mismatching chain configuration.
func (c *ChainConfig) CheckCompatible(newcfg *ChainConfig, height uint64, timestamp uint64) *ConfigCompatError {
Expand Down Expand Up @@ -540,22 +505,18 @@ type Rules struct {
// Rules for Avalanche releases
IsSubnetEVM bool

// Optional stateful precompile rules
IsContractDeployerAllowListEnabled bool
IsContractNativeMinterEnabled bool
IsTxAllowListEnabled bool
IsFeeConfigManagerEnabled bool
IsRewardManagerEnabled bool
// ADD YOUR PRECOMPILE HERE
// Is{YourPrecompile}Enabled bool

// Precompiles maps addresses to stateful precompiled contracts that are enabled
// for this rule set.
// Note: none of these addresses should conflict with the address space used by
// any existing precompiles.
Precompiles map[common.Address]precompile.StatefulPrecompiledContract
}

func (r *Rules) IsPrecompileEnabled(addr common.Address) bool {
_, ok := r.Precompiles[addr]
return ok
}

// Rules ensures c's ChainID is not nil.
func (c *ChainConfig) rules(num *big.Int) Rules {
chainID := c.ChainID
Expand All @@ -581,13 +542,6 @@ func (c *ChainConfig) AvalancheRules(blockNum, blockTimestamp *big.Int) Rules {
rules := c.rules(blockNum)

rules.IsSubnetEVM = c.IsSubnetEVM(blockTimestamp)
rules.IsContractDeployerAllowListEnabled = c.IsContractDeployerAllowList(blockTimestamp)
rules.IsContractNativeMinterEnabled = c.IsContractNativeMinter(blockTimestamp)
rules.IsTxAllowListEnabled = c.IsTxAllowList(blockTimestamp)
rules.IsFeeConfigManagerEnabled = c.IsFeeConfigManager(blockTimestamp)
rules.IsRewardManagerEnabled = c.IsRewardManager(blockTimestamp)
// ADD YOUR PRECOMPILE HERE
// rules.Is{YourPrecompile}Enabled = c.{IsYourPrecompile}(blockTimestamp)

// Initialize the stateful precompiles that should be enabled at [blockTimestamp].
rules.Precompiles = make(map[common.Address]precompile.StatefulPrecompiledContract)
Expand Down
3 changes: 2 additions & 1 deletion plugin/evm/block_verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/ava-labs/subnet-evm/constants"
"github.com/ava-labs/subnet-evm/core/types"
"github.com/ava-labs/subnet-evm/params"
"github.com/ava-labs/subnet-evm/precompile"
"github.com/ava-labs/subnet-evm/trie"
"github.com/ava-labs/subnet-evm/vmerrs"
)
Expand Down Expand Up @@ -98,7 +99,7 @@ func (v blockValidator) SyntacticVerify(b *Block, rules params.Rules) error {
}

switch {
case rules.IsRewardManagerEnabled:
case rules.IsPrecompileEnabled(precompile.RewardManagerAddress):
// if reward manager is enabled, Coinbase depends on state.
// State is not available here, so skip checking the coinbase here.
case rules.IsSubnetEVM && b.vm.chainConfig.AllowFeeRecipients:
Expand Down