Skip to content
Merged

wip #509

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
7 changes: 3 additions & 4 deletions core/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import (
"github.com/ava-labs/subnet-evm/ethdb"
"github.com/ava-labs/subnet-evm/params"
"github.com/ava-labs/subnet-evm/precompile/allowlist"
precompileConfig "github.com/ava-labs/subnet-evm/precompile/config"
"github.com/ava-labs/subnet-evm/precompile/contracts/deployerallowlist"
"github.com/davecgh/go-spew/spew"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -192,8 +191,8 @@ func TestStatefulPrecompilesConfigure(t *testing.T) {
"allow list enabled in genesis": {
getConfig: func() *params.ChainConfig {
config := *params.TestChainConfig
config.GenesisPrecompiles = precompileConfig.Configs{
deployerallowlist.ConfigKey: deployerallowlist.NewContractDeployerAllowListConfig(big.NewInt(0), []common.Address{addr}, nil),
config.GenesisPrecompiles = params.ChainConfigPrecompiles{
deployerallowlist.ConfigKey: deployerallowlist.NewConfig(big.NewInt(0), []common.Address{addr}, nil),
}
return &config
},
Expand Down Expand Up @@ -269,7 +268,7 @@ func TestPrecompileActivationAfterHeaderBlock(t *testing.T) {
require.Greater(block.Time(), bc.lastAccepted.Time())

activatedGenesis := customg
contractDeployerConfig := deployerallowlist.NewContractDeployerAllowListConfig(big.NewInt(51), nil, nil)
contractDeployerConfig := deployerallowlist.NewConfig(big.NewInt(51), nil, nil)
activatedGenesis.Config.UpgradeConfig.PrecompileUpgrades = []params.PrecompileUpgrade{
{
Config: contractDeployerConfig,
Expand Down
19 changes: 9 additions & 10 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ import (
"github.com/ava-labs/subnet-evm/core/types"
"github.com/ava-labs/subnet-evm/core/vm"
"github.com/ava-labs/subnet-evm/params"
"github.com/ava-labs/subnet-evm/precompile/config"
"github.com/ava-labs/subnet-evm/precompile/contract"
"github.com/ava-labs/subnet-evm/precompile/registry"
"github.com/ava-labs/subnet-evm/precompile/modules"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
Expand Down Expand Up @@ -181,35 +180,35 @@ func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *commo
// - during block producing to apply the precompile upgrades before producing the block.
func ApplyPrecompileActivations(c *params.ChainConfig, parentTimestamp *big.Int, blockContext contract.BlockContext, statedb *state.StateDB) error {
blockTimestamp := blockContext.Timestamp()
// Note: RegisteredModules returns precompiles in order they are registered.
// Note: RegisteredModules returns precompiles sorted by module addresses.
// This is important because we want to configure precompiles in the same order
// so that the state is deterministic.
for _, config := range config.GetConfigs() {
key := config.Key()
for _, activatingConfig := range c.GetActivatingPrecompileConfigs(config.Address(), parentTimestamp, blockTimestamp, c.PrecompileUpgrades) {
for _, module := range modules.RegisteredModules() {
key := module.ConfigKey
for _, activatingConfig := range c.GetActivatingPrecompileConfigs(module.Address, parentTimestamp, blockTimestamp, c.PrecompileUpgrades) {
// If this transition activates the upgrade, configure the stateful precompile.
// (or deconfigure it if it is being disabled.)
if activatingConfig.IsDisabled() {
log.Info("Disabling precompile", "name", key)
statedb.Suicide(config.Address())
statedb.Suicide(module.Address)
// Calling Finalise here effectively commits Suicide call and wipes the contract state.
// This enables re-configuration of the same contract state in the same block.
// Without an immediate Finalise call after the Suicide, a reconfigured precompiled state can be wiped out
// since Suicide will be committed after the reconfiguration.
statedb.Finalise(true)
} else {
module, ok := registry.GetPrecompileModule(key)
module, ok := modules.GetPrecompileModule(key)
if !ok {
return fmt.Errorf("could not find module for activating precompile, name: %s", key)
}
log.Info("Activating new precompile", "name", key, "config", activatingConfig)
// Set the nonce of the precompile's address (as is done when a contract is created) to ensure
// that it is marked as non-empty and will not be cleaned up when the statedb is finalized.
statedb.SetNonce(activatingConfig.Address(), 1)
statedb.SetNonce(module.Address, 1)
// Set the code of the precompile's address to a non-zero length byte slice to ensure that the precompile
// can be called from within Solidity contracts. Solidity adds a check before invoking a contract to ensure
// that it does not attempt to invoke a non-existent contract.
statedb.SetCode(activatingConfig.Address(), []byte{0x1})
statedb.SetCode(module.Address, []byte{0x1})
if err := module.Configure(c, activatingConfig, statedb, blockContext); err != nil {
return fmt.Errorf("could not configure precompile, name: %s, reason: %w", key, err)
}
Expand Down
5 changes: 2 additions & 3 deletions core/state_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
"github.com/ava-labs/subnet-evm/core/types"
"github.com/ava-labs/subnet-evm/core/vm"
"github.com/ava-labs/subnet-evm/params"
precompileConfig "github.com/ava-labs/subnet-evm/precompile/config"
"github.com/ava-labs/subnet-evm/precompile/contracts/txallowlist"
"github.com/ava-labs/subnet-evm/trie"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -316,8 +315,8 @@ func TestBadTxAllowListBlock(t *testing.T) {
NetworkUpgrades: params.NetworkUpgrades{
SubnetEVMTimestamp: big.NewInt(0),
},
GenesisPrecompiles: precompileConfig.Configs{
txallowlist.ConfigKey: txallowlist.NewTxAllowListConfig(big.NewInt(0), nil, nil),
GenesisPrecompiles: params.ChainConfigPrecompiles{
txallowlist.ConfigKey: txallowlist.NewConfig(big.NewInt(0), nil, nil),
},
}
signer = types.LatestSigner(config)
Expand Down
7 changes: 3 additions & 4 deletions core/test_blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/ava-labs/subnet-evm/ethdb"
"github.com/ava-labs/subnet-evm/params"
"github.com/ava-labs/subnet-evm/precompile/allowlist"
precompileConfig "github.com/ava-labs/subnet-evm/precompile/config"
"github.com/ava-labs/subnet-evm/precompile/contracts/deployerallowlist"
"github.com/ava-labs/subnet-evm/precompile/contracts/feemanager"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -1549,9 +1548,9 @@ func TestStatefulPrecompiles(t *testing.T, create func(db ethdb.Database, chainC
genesisBalance := new(big.Int).Mul(big.NewInt(1000000), big.NewInt(params.Ether))
config := *params.TestChainConfig
// Set all of the required config parameters
config.GenesisPrecompiles = precompileConfig.Configs{
deployerallowlist.ConfigKey: deployerallowlist.NewContractDeployerAllowListConfig(big.NewInt(0), []common.Address{addr1}, nil),
feemanager.ConfigKey: feemanager.NewFeeManagerConfig(big.NewInt(0), []common.Address{addr1}, nil, nil),
config.GenesisPrecompiles = params.ChainConfigPrecompiles{
deployerallowlist.ConfigKey: deployerallowlist.NewConfig(big.NewInt(0), []common.Address{addr1}, nil),
feemanager.ConfigKey: feemanager.NewConfig(big.NewInt(0), []common.Address{addr1}, nil, nil),
}
gspec := &Genesis{
Config: &config,
Expand Down
6 changes: 3 additions & 3 deletions core/vm/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
"github.com/ava-labs/subnet-evm/constants"
"github.com/ava-labs/subnet-evm/params"
"github.com/ava-labs/subnet-evm/precompile/contract"
"github.com/ava-labs/subnet-evm/precompile/registry"
"github.com/ava-labs/subnet-evm/precompile/modules"
"github.com/ava-labs/subnet-evm/vmerrs"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
Expand Down Expand Up @@ -159,8 +159,8 @@ func init() {

// Ensure that this package will panic during init if there is a conflict present with the declared
// precompile addresses.
for _, module := range registry.RegisteredModules() {
address := module.Address()
for _, module := range modules.RegisteredModules() {
address := module.Address
if _, ok := PrecompileAllNativeAddresses[address]; ok {
panic(fmt.Errorf("precompile address collides with existing native address: %s", address))
}
Expand Down
14 changes: 6 additions & 8 deletions core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
"github.com/ava-labs/subnet-evm/params"
"github.com/ava-labs/subnet-evm/precompile/contract"
"github.com/ava-labs/subnet-evm/precompile/contracts/deployerallowlist"
"github.com/ava-labs/subnet-evm/precompile/registry"
"github.com/ava-labs/subnet-evm/precompile/modules"
"github.com/ava-labs/subnet-evm/vmerrs"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
Expand All @@ -56,7 +56,7 @@ func IsProhibited(addr common.Address) bool {
return true
}

return registry.ReservedAddress(addr)
return modules.ReservedAddress(addr)
}

// emptyCodeHash is used by create to ensure deployment is disallowed to already
Expand Down Expand Up @@ -93,13 +93,11 @@ func (evm *EVM) precompile(addr common.Address) (contract.StatefulPrecompiledCon
}

// Otherwise, check the chain rules for the additionally configured precompiles.
config, ok := evm.chainRules.ActivePrecompiles[addr]
if ok {
key := config.Key()
if module, ok := registry.GetPrecompileModule(key); ok {
return module.Contract(), true
}
if _, ok = evm.chainRules.ActivePrecompiles[addr]; ok {
module, ok := modules.GetPrecompileModuleByAddress(addr)
return module.Contract, ok
}

return nil, false
}

Expand Down
5 changes: 2 additions & 3 deletions eth/gasprice/gasprice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import (
"github.com/ava-labs/subnet-evm/core/vm"
"github.com/ava-labs/subnet-evm/ethdb"
"github.com/ava-labs/subnet-evm/params"
precompileConfig "github.com/ava-labs/subnet-evm/precompile/config"
"github.com/ava-labs/subnet-evm/precompile/contracts/feemanager"
"github.com/ava-labs/subnet-evm/rpc"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -435,8 +434,8 @@ func TestSuggestGasPriceAfterFeeConfigUpdate(t *testing.T) {

// create a chain config with fee manager enabled at genesis with [addr] as the admin
chainConfig := *params.TestChainConfig
chainConfig.GenesisPrecompiles = precompileConfig.Configs{
feemanager.ConfigKey: feemanager.NewFeeManagerConfig(big.NewInt(0), []common.Address{addr}, nil, nil),
chainConfig.GenesisPrecompiles = params.ChainConfigPrecompiles{
feemanager.ConfigKey: feemanager.NewConfig(big.NewInt(0), []common.Address{addr}, nil, nil),
}

// create a fee config with higher MinBaseFee and prepare it for inclusion in a tx
Expand Down
13 changes: 6 additions & 7 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ import (
"github.com/ava-labs/subnet-evm/core/vm"
"github.com/ava-labs/subnet-evm/eth/tracers/logger"
"github.com/ava-labs/subnet-evm/params"
precompileConfig "github.com/ava-labs/subnet-evm/precompile/config"
"github.com/ava-labs/subnet-evm/precompile/registry"
"github.com/ava-labs/subnet-evm/precompile/modules"
"github.com/ava-labs/subnet-evm/rpc"
"github.com/ava-labs/subnet-evm/vmerrs"
"github.com/davecgh/go-spew/spew"
Expand Down Expand Up @@ -628,15 +627,15 @@ func (api *BlockChainAPI) ChainId() *hexutil.Big {
}

// GetActivePrecompilesAt returns the active precompile configs at the given block timestamp.
func (s *BlockChainAPI) GetActivePrecompilesAt(ctx context.Context, blockTimestamp *big.Int) precompileConfig.Configs {
func (s *BlockChainAPI) GetActivePrecompilesAt(ctx context.Context, blockTimestamp *big.Int) params.ChainConfigPrecompiles {
if blockTimestamp == nil {
blockTimestampInt := s.b.CurrentHeader().Time
blockTimestamp = new(big.Int).SetUint64(blockTimestampInt)
}
res := make(precompileConfig.Configs)
for _, module := range registry.RegisteredModules() {
if config := s.b.ChainConfig().GetActivePrecompileConfig(module.Address(), blockTimestamp); config != nil && !config.IsDisabled() {
res[module.Key()] = config
res := make(params.ChainConfigPrecompiles)
for _, module := range modules.RegisteredModules() {
if config := s.b.ChainConfig().GetActivePrecompileConfig(module.Address, blockTimestamp); config != nil && !config.IsDisabled() {
res[module.ConfigKey] = config
}
}

Expand Down
48 changes: 48 additions & 0 deletions params/chain_config_precompiles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// (c) 2023 Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package params

import (
"encoding/json"

"github.com/ava-labs/subnet-evm/precompile/config"
"github.com/ava-labs/subnet-evm/precompile/modules"
"github.com/ethereum/go-ethereum/common"
)

type ChainConfigPrecompiles map[string]config.Config

func (ccp *ChainConfigPrecompiles) GetConfigByAddress(address common.Address) (config.Config, bool) {
module, ok := modules.GetPrecompileModuleByAddress(address)
if !ok {
return nil, false
}
key := module.ConfigKey
config, ok := (*ccp)[key]
return config, ok
}

// UnmarshalJSON parses the JSON-encoded data into the ChainConfigPrecompiles.
// ChainConfigPrecompiles is a map of precompile module keys to their
// configuration.
func (ccp *ChainConfigPrecompiles) UnmarshalJSON(data []byte) error {
raw := make(map[string]json.RawMessage)
if err := json.Unmarshal(data, &raw); err != nil {
return err
}

*ccp = make(ChainConfigPrecompiles)
for _, module := range modules.RegisteredModules() {
key := module.ConfigKey
if value, ok := raw[key]; ok {
conf := module.NewConfig()
err := json.Unmarshal(value, conf)
if err != nil {
return err
}
(*ccp)[key] = conf
}
}
return nil
}
21 changes: 10 additions & 11 deletions params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/subnet-evm/commontype"
"github.com/ava-labs/subnet-evm/precompile/config"
precompileConfig "github.com/ava-labs/subnet-evm/precompile/config"
"github.com/ava-labs/subnet-evm/precompile/modules"
"github.com/ava-labs/subnet-evm/utils"
"github.com/ethereum/go-ethereum/common"
)
Expand Down Expand Up @@ -84,7 +84,7 @@ var (
PetersburgBlock: big.NewInt(0),
IstanbulBlock: big.NewInt(0),
MuirGlacierBlock: big.NewInt(0),
GenesisPrecompiles: precompileConfig.Configs{},
GenesisPrecompiles: ChainConfigPrecompiles{},
NetworkUpgrades: NetworkUpgrades{
SubnetEVMTimestamp: big.NewInt(0),
},
Expand All @@ -106,7 +106,7 @@ var (
IstanbulBlock: big.NewInt(0),
MuirGlacierBlock: big.NewInt(0),
NetworkUpgrades: NetworkUpgrades{big.NewInt(0)},
GenesisPrecompiles: precompileConfig.Configs{},
GenesisPrecompiles: ChainConfigPrecompiles{},
UpgradeConfig: UpgradeConfig{},
}

Expand All @@ -126,7 +126,7 @@ var (
IstanbulBlock: big.NewInt(0),
MuirGlacierBlock: big.NewInt(0),
NetworkUpgrades: NetworkUpgrades{},
GenesisPrecompiles: precompileConfig.Configs{},
GenesisPrecompiles: ChainConfigPrecompiles{},
UpgradeConfig: UpgradeConfig{},
}
)
Expand Down Expand Up @@ -158,9 +158,9 @@ type ChainConfig struct {
IstanbulBlock *big.Int `json:"istanbulBlock,omitempty"` // Istanbul switch block (nil = no fork, 0 = already on istanbul)
MuirGlacierBlock *big.Int `json:"muirGlacierBlock,omitempty"` // Eip-2384 (bomb delay) switch block (nil = no fork, 0 = already activated)

NetworkUpgrades // Config for timestamps that enable avalanche network upgrades
GenesisPrecompiles precompileConfig.Configs `json:"-"` // Config for enabling precompiles from genesis. JSON encode/decode will be handled by the custom marshaler/unmarshaler.
UpgradeConfig `json:"-"` // Config specified in upgradeBytes (avalanche network upgrades or enable/disabling precompiles). Skip encoding/decoding directly into ChainConfig.
NetworkUpgrades // Config for timestamps that enable avalanche network upgrades
GenesisPrecompiles ChainConfigPrecompiles `json:"-"` // Config for enabling precompiles from genesis. JSON encode/decode will be handled by the custom marshaler/unmarshaler.
UpgradeConfig `json:"-"` // Config specified in upgradeBytes (avalanche network upgrades or enable/disabling precompiles). Skip encoding/decoding directly into ChainConfig.
}

// UnmarshalJSON parses the JSON-encoded data and stores the result in the
Expand Down Expand Up @@ -596,11 +596,10 @@ func (c *ChainConfig) AvalancheRules(blockNum, blockTimestamp *big.Int) Rules {

// Initialize the stateful precompiles that should be enabled at [blockTimestamp].
rules.ActivePrecompiles = make(map[common.Address]config.Config)
for _, config := range c.EnabledStatefulPrecompiles(blockTimestamp) {
if config.IsDisabled() {
continue
for _, module := range modules.RegisteredModules() {
if config := c.GetActivePrecompileConfig(module.Address, blockTimestamp); config != nil {
rules.ActivePrecompiles[module.Address] = config
}
rules.ActivePrecompiles[config.Address()] = config
}

return rules
Expand Down
Loading