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

wip: precompile config refactor (avoid import modules in params) #1253

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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 params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/version"
"github.com/ava-labs/subnet-evm/commontype"
"github.com/ava-labs/subnet-evm/precompile/modules"
"github.com/ava-labs/subnet-evm/precompile/precompileconfig"
"github.com/ava-labs/subnet-evm/utils"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -374,7 +373,7 @@ func (r *Rules) PredicaterExists(addr common.Address) bool {

// IsPrecompileEnabled returns whether precompile with [address] is enabled at [timestamp].
func (c *ChainConfig) IsPrecompileEnabled(address common.Address, timestamp uint64) bool {
config := c.getActivePrecompileConfig(address, timestamp)
config := c.GetActivePrecompileConfig(address, timestamp)
return config != nil && !config.IsDisabled()
}

Expand Down Expand Up @@ -759,8 +758,8 @@ func (c *ChainConfig) Rules(blockNum *big.Int, timestamp uint64) Rules {
rules.ActivePrecompiles = make(map[common.Address]precompileconfig.Config)
rules.Predicaters = make(map[common.Address]precompileconfig.Predicater)
rules.AccepterPrecompiles = make(map[common.Address]precompileconfig.Accepter)
for _, module := range modules.RegisteredModules() {
if config := c.getActivePrecompileConfig(module.Address, timestamp); config != nil && !config.IsDisabled() {
for _, module := range RegisteredModules() {
if config := c.GetActivePrecompileConfig(module.Address, timestamp); config != nil && !config.IsDisabled() {
rules.ActivePrecompiles[module.Address] = config
if predicater, ok := config.(precompileconfig.Predicater); ok {
rules.Predicaters[module.Address] = predicater
Expand Down
177 changes: 177 additions & 0 deletions params/config_extra_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
// (c) 2024 Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package params_test

import (
"encoding/json"
"math/big"
"testing"

"github.com/ava-labs/subnet-evm/params"
"github.com/ava-labs/subnet-evm/precompile/contracts/nativeminter"
"github.com/ava-labs/subnet-evm/precompile/contracts/rewardmanager"
"github.com/ava-labs/subnet-evm/precompile/contracts/txallowlist"
"github.com/ava-labs/subnet-evm/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
)

func TestConfigUnmarshalJSON(t *testing.T) {
require := require.New(t)

testRewardManagerConfig := rewardmanager.NewConfig(
utils.NewUint64(1671542573),
[]common.Address{common.HexToAddress("0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC")},
nil,
nil,
&rewardmanager.InitialRewardConfig{
AllowFeeRecipients: true,
})

testContractNativeMinterConfig := nativeminter.NewConfig(
utils.NewUint64(0),
[]common.Address{common.HexToAddress("0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC")},
nil,
nil,
nil,
)

config := []byte(`
{
"chainId": 43214,
"allowFeeRecipients": true,
"rewardManagerConfig": {
"blockTimestamp": 1671542573,
"adminAddresses": [
"0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC"
],
"initialRewardConfig": {
"allowFeeRecipients": true
}
},
"contractNativeMinterConfig": {
"blockTimestamp": 0,
"adminAddresses": [
"0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC"
]
}
}
`)
c := params.ChainConfig{}
err := json.Unmarshal(config, &c)
require.NoError(err)

require.Equal(c.ChainID, big.NewInt(43214))
require.Equal(c.AllowFeeRecipients, true)

rewardManagerConfig, ok := c.GenesisPrecompiles[rewardmanager.ConfigKey]
require.True(ok)
require.Equal(rewardManagerConfig.Key(), rewardmanager.ConfigKey)
require.True(rewardManagerConfig.Equal(testRewardManagerConfig))

nativeMinterConfig := c.GenesisPrecompiles[nativeminter.ConfigKey]
require.Equal(nativeMinterConfig.Key(), nativeminter.ConfigKey)
require.True(nativeMinterConfig.Equal(testContractNativeMinterConfig))

// Marshal and unmarshal again and check that the result is the same
marshaled, err := json.Marshal(c)
require.NoError(err)
c2 := params.ChainConfig{}
err = json.Unmarshal(marshaled, &c2)
require.NoError(err)
require.Equal(c, c2)
}

func TestActivePrecompiles(t *testing.T) {
config := params.ChainConfig{
UpgradeConfig: params.UpgradeConfig{
PrecompileUpgrades: []params.PrecompileUpgrade{
{
nativeminter.NewConfig(utils.NewUint64(0), nil, nil, nil, nil), // enable at genesis
},
{
nativeminter.NewDisableConfig(utils.NewUint64(1)), // disable at timestamp 1
},
},
},
}

rules0 := config.Rules(common.Big0, 0)
require.True(t, rules0.IsPrecompileEnabled(nativeminter.Module.Address))

rules1 := config.Rules(common.Big0, 1)
require.False(t, rules1.IsPrecompileEnabled(nativeminter.Module.Address))
}

func TestChainConfigMarshalWithUpgrades(t *testing.T) {
config := params.ChainConfigWithUpgradesJSON{
ChainConfig: params.ChainConfig{
ChainID: big.NewInt(1),
FeeConfig: params.DefaultFeeConfig,
AllowFeeRecipients: false,
HomesteadBlock: big.NewInt(0),
EIP150Block: big.NewInt(0),
EIP155Block: big.NewInt(0),
EIP158Block: big.NewInt(0),
ByzantiumBlock: big.NewInt(0),
ConstantinopleBlock: big.NewInt(0),
PetersburgBlock: big.NewInt(0),
IstanbulBlock: big.NewInt(0),
MuirGlacierBlock: big.NewInt(0),
NetworkUpgrades: params.NetworkUpgrades{
SubnetEVMTimestamp: utils.NewUint64(0),
DurangoTimestamp: utils.NewUint64(0),
},
GenesisPrecompiles: params.Precompiles{},
},
UpgradeConfig: params.UpgradeConfig{
PrecompileUpgrades: []params.PrecompileUpgrade{
{
Config: txallowlist.NewConfig(utils.NewUint64(100), nil, nil, nil),
},
},
},
}
result, err := json.Marshal(&config)
require.NoError(t, err)
expectedJSON := `{
"chainId": 1,
"feeConfig": {
"gasLimit": 8000000,
"targetBlockRate": 2,
"minBaseFee": 25000000000,
"targetGas": 15000000,
"baseFeeChangeDenominator": 36,
"minBlockGasCost": 0,
"maxBlockGasCost": 1000000,
"blockGasCostStep": 200000
},
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"istanbulBlock": 0,
"muirGlacierBlock": 0,
"subnetEVMTimestamp": 0,
"durangoTimestamp": 0,
"upgrades": {
"precompileUpgrades": [
{
"txAllowListConfig": {
"blockTimestamp": 100
}
}
]
}
}`
require.JSONEq(t, expectedJSON, string(result))

var unmarshalled params.ChainConfigWithUpgradesJSON
err = json.Unmarshal(result, &unmarshalled)
require.NoError(t, err)
require.Equal(t, config, unmarshalled)
}
165 changes: 0 additions & 165 deletions params/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,13 @@
package params

import (
"encoding/json"
"math"
"math/big"
"reflect"
"testing"
"time"

"github.com/ava-labs/subnet-evm/precompile/contracts/nativeminter"
"github.com/ava-labs/subnet-evm/precompile/contracts/rewardmanager"
"github.com/ava-labs/subnet-evm/precompile/contracts/txallowlist"
"github.com/ava-labs/subnet-evm/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
)

func TestCheckCompatible(t *testing.T) {
Expand Down Expand Up @@ -169,162 +163,3 @@ func TestConfigRules(t *testing.T) {
t.Errorf("expected %v to be subnet-evm", stamp)
}
}

func TestConfigUnmarshalJSON(t *testing.T) {
require := require.New(t)

testRewardManagerConfig := rewardmanager.NewConfig(
utils.NewUint64(1671542573),
[]common.Address{common.HexToAddress("0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC")},
nil,
nil,
&rewardmanager.InitialRewardConfig{
AllowFeeRecipients: true,
})

testContractNativeMinterConfig := nativeminter.NewConfig(
utils.NewUint64(0),
[]common.Address{common.HexToAddress("0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC")},
nil,
nil,
nil,
)

config := []byte(`
{
"chainId": 43214,
"allowFeeRecipients": true,
"rewardManagerConfig": {
"blockTimestamp": 1671542573,
"adminAddresses": [
"0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC"
],
"initialRewardConfig": {
"allowFeeRecipients": true
}
},
"contractNativeMinterConfig": {
"blockTimestamp": 0,
"adminAddresses": [
"0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC"
]
}
}
`)
c := ChainConfig{}
err := json.Unmarshal(config, &c)
require.NoError(err)

require.Equal(c.ChainID, big.NewInt(43214))
require.Equal(c.AllowFeeRecipients, true)

rewardManagerConfig, ok := c.GenesisPrecompiles[rewardmanager.ConfigKey]
require.True(ok)
require.Equal(rewardManagerConfig.Key(), rewardmanager.ConfigKey)
require.True(rewardManagerConfig.Equal(testRewardManagerConfig))

nativeMinterConfig := c.GenesisPrecompiles[nativeminter.ConfigKey]
require.Equal(nativeMinterConfig.Key(), nativeminter.ConfigKey)
require.True(nativeMinterConfig.Equal(testContractNativeMinterConfig))

// Marshal and unmarshal again and check that the result is the same
marshaled, err := json.Marshal(c)
require.NoError(err)
c2 := ChainConfig{}
err = json.Unmarshal(marshaled, &c2)
require.NoError(err)
require.Equal(c, c2)
}

func TestActivePrecompiles(t *testing.T) {
config := ChainConfig{
UpgradeConfig: UpgradeConfig{
PrecompileUpgrades: []PrecompileUpgrade{
{
nativeminter.NewConfig(utils.NewUint64(0), nil, nil, nil, nil), // enable at genesis
},
{
nativeminter.NewDisableConfig(utils.NewUint64(1)), // disable at timestamp 1
},
},
},
}

rules0 := config.Rules(common.Big0, 0)
require.True(t, rules0.IsPrecompileEnabled(nativeminter.Module.Address))

rules1 := config.Rules(common.Big0, 1)
require.False(t, rules1.IsPrecompileEnabled(nativeminter.Module.Address))
}

func TestChainConfigMarshalWithUpgrades(t *testing.T) {
config := ChainConfigWithUpgradesJSON{
ChainConfig: ChainConfig{
ChainID: big.NewInt(1),
FeeConfig: DefaultFeeConfig,
AllowFeeRecipients: false,
HomesteadBlock: big.NewInt(0),
EIP150Block: big.NewInt(0),
EIP155Block: big.NewInt(0),
EIP158Block: big.NewInt(0),
ByzantiumBlock: big.NewInt(0),
ConstantinopleBlock: big.NewInt(0),
PetersburgBlock: big.NewInt(0),
IstanbulBlock: big.NewInt(0),
MuirGlacierBlock: big.NewInt(0),
NetworkUpgrades: NetworkUpgrades{
SubnetEVMTimestamp: utils.NewUint64(0),
DurangoTimestamp: utils.NewUint64(0),
},
GenesisPrecompiles: Precompiles{},
},
UpgradeConfig: UpgradeConfig{
PrecompileUpgrades: []PrecompileUpgrade{
{
Config: txallowlist.NewConfig(utils.NewUint64(100), nil, nil, nil),
},
},
},
}
result, err := json.Marshal(&config)
require.NoError(t, err)
expectedJSON := `{
"chainId": 1,
"feeConfig": {
"gasLimit": 8000000,
"targetBlockRate": 2,
"minBaseFee": 25000000000,
"targetGas": 15000000,
"baseFeeChangeDenominator": 36,
"minBlockGasCost": 0,
"maxBlockGasCost": 1000000,
"blockGasCostStep": 200000
},
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"istanbulBlock": 0,
"muirGlacierBlock": 0,
"subnetEVMTimestamp": 0,
"durangoTimestamp": 0,
"upgrades": {
"precompileUpgrades": [
{
"txAllowListConfig": {
"blockTimestamp": 100
}
}
]
}
}`
require.JSONEq(t, expectedJSON, string(result))

var unmarshalled ChainConfigWithUpgradesJSON
err = json.Unmarshal(result, &unmarshalled)
require.NoError(t, err)
require.Equal(t, config, unmarshalled)
}
Loading
Loading