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

Rename configs: alternative #520

Merged
merged 5 commits into from
Feb 17, 2023
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
6 changes: 3 additions & 3 deletions params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ import (

"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/subnet-evm/commontype"
"github.com/ava-labs/subnet-evm/precompile/config"
"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 @@ -558,7 +558,7 @@ type Rules struct {
// for this rule set.
// Note: none of these addresses should conflict with the address space used by
// any existing precompiles.
ActivePrecompiles map[common.Address]config.Config
ActivePrecompiles map[common.Address]precompileconfig.Config
}

// IsPrecompileEnabled returns true if the precompile at [addr] is enabled for this rule set.
Expand Down Expand Up @@ -594,7 +594,7 @@ func (c *ChainConfig) AvalancheRules(blockNum, blockTimestamp *big.Int) Rules {
rules.IsSubnetEVM = c.IsSubnetEVM(blockTimestamp)

// Initialize the stateful precompiles that should be enabled at [blockTimestamp].
rules.ActivePrecompiles = make(map[common.Address]config.Config)
rules.ActivePrecompiles = make(map[common.Address]precompileconfig.Config)
for _, module := range modules.RegisteredModules() {
if config := c.GetActivePrecompileConfig(module.Address, blockTimestamp); config != nil && !config.IsDisabled() {
rules.ActivePrecompiles[module.Address] = config
Expand Down
13 changes: 6 additions & 7 deletions params/precompile_upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import (
"fmt"
"math/big"

"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/precompile/precompileconfig"
"github.com/ava-labs/subnet-evm/utils"
"github.com/ethereum/go-ethereum/common"
)
Expand All @@ -23,7 +22,7 @@ var errNoKey = errors.New("PrecompileUpgrade cannot be empty")
// based on the key. Keys are defined in each precompile module, and registered in
// precompile/registry/registry.go.
type PrecompileUpgrade struct {
config.Config
precompileconfig.Config
}

// UnmarshalJSON unmarshals the json into the correct precompile config type
Expand Down Expand Up @@ -59,7 +58,7 @@ func (u *PrecompileUpgrade) UnmarshalJSON(data []byte) error {
// MarshalJSON marshal the precompile config into json based on the precompile key.
// Ex: {"feeManagerConfig": {...}} where "feeManagerConfig" is the key
func (u *PrecompileUpgrade) MarshalJSON() ([]byte, error) {
res := make(map[string]precompileConfig.Config)
res := make(map[string]precompileconfig.Config)
res[u.Key()] = u.Config
return json.Marshal(res)
}
Expand Down Expand Up @@ -152,7 +151,7 @@ func (c *ChainConfig) verifyPrecompileUpgrades() error {

// GetActivePrecompileConfig returns the most recent precompile config corresponding to [address].
// If none have occurred, returns nil.
func (c *ChainConfig) GetActivePrecompileConfig(address common.Address, blockTimestamp *big.Int) config.Config {
func (c *ChainConfig) GetActivePrecompileConfig(address common.Address, blockTimestamp *big.Int) precompileconfig.Config {
configs := c.GetActivatingPrecompileConfigs(address, nil, blockTimestamp, c.PrecompileUpgrades)
if len(configs) == 0 {
return nil
Expand All @@ -162,13 +161,13 @@ func (c *ChainConfig) GetActivePrecompileConfig(address common.Address, blockTim

// GetActivatingPrecompileConfigs returns all upgrades configured to activate during the state transition from a block with timestamp [from]
// to a block with timestamp [to].
func (c *ChainConfig) GetActivatingPrecompileConfigs(address common.Address, from *big.Int, to *big.Int, upgrades []PrecompileUpgrade) []config.Config {
func (c *ChainConfig) GetActivatingPrecompileConfigs(address common.Address, from *big.Int, to *big.Int, upgrades []PrecompileUpgrade) []precompileconfig.Config {
// Get key from address.
module, ok := modules.GetPrecompileModuleByAddress(address)
if !ok {
return nil
}
configs := make([]config.Config, 0)
configs := make([]precompileconfig.Config, 0)
key := module.ConfigKey
// First check the embedded [upgrade] for precompiles configured
// in the genesis chain config.
Expand Down
4 changes: 2 additions & 2 deletions params/precompiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ package params
import (
"encoding/json"

"github.com/ava-labs/subnet-evm/precompile/config"
"github.com/ava-labs/subnet-evm/precompile/modules"
"github.com/ava-labs/subnet-evm/precompile/precompileconfig"
)

type Precompiles map[string]config.Config
type Precompiles map[string]precompileconfig.Config

// UnmarshalJSON parses the JSON-encoded data into the ChainConfigPrecompiles.
// ChainConfigPrecompiles is a map of precompile module keys to their
Expand Down
6 changes: 3 additions & 3 deletions precompile/allowlist/allow_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestAllowListRun(t *testing.T) {
expectedRes []byte
expectedErr string

config *Config
config *AllowListConfig

assertState func(t *testing.T, state *state.StateDB)
}
Expand Down Expand Up @@ -221,7 +221,7 @@ func TestAllowListRun(t *testing.T) {
expectedErr: vmerrs.ErrOutOfGas.Error(),
},
"initial config sets admins": {
config: &Config{
config: &AllowListConfig{
AdminAddresses: []common.Address{noRoleAddr, enabledAddr},
},
suppliedGas: 0,
Expand All @@ -233,7 +233,7 @@ func TestAllowListRun(t *testing.T) {
},
},
"initial config sets enabled": {
config: &Config{
config: &AllowListConfig{
EnabledAddresses: []common.Address{noRoleAddr, adminAddr},
},
suppliedGas: 0,
Expand Down
10 changes: 5 additions & 5 deletions precompile/allowlist/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import (
"github.com/ethereum/go-ethereum/common"
)

// Config specifies the initial set of addresses with Admin or Enabled roles.
type Config struct {
// AllowListConfig specifies the initial set of addresses with Admin or Enabled roles.
type AllowListConfig struct {
AdminAddresses []common.Address `json:"adminAddresses,omitempty"` // initial admin addresses
EnabledAddresses []common.Address `json:"enabledAddresses,omitempty"` // initial enabled addresses
}

// Configure initializes the address space of [precompileAddr] by initializing the role of each of
// the addresses in [AllowListAdmins].
func (c *Config) Configure(state contract.StateDB, precompileAddr common.Address) error {
func (c *AllowListConfig) Configure(state contract.StateDB, precompileAddr common.Address) error {
for _, enabledAddr := range c.EnabledAddresses {
SetAllowListRole(state, precompileAddr, enabledAddr, EnabledRole)
}
Expand All @@ -29,7 +29,7 @@ func (c *Config) Configure(state contract.StateDB, precompileAddr common.Address
}

// Equal returns true iff [other] has the same admins in the same order in its allow list.
func (c *Config) Equal(other *Config) bool {
func (c *AllowListConfig) Equal(other *AllowListConfig) bool {
if other == nil {
return false
}
Expand All @@ -52,7 +52,7 @@ func areEqualAddressLists(current []common.Address, other []common.Address) bool
}

// Verify returns an error if there is an overlapping address between admin and enabled roles
func (c *Config) Verify() error {
func (c *AllowListConfig) Verify() error {
addressMap := make(map[common.Address]Role) // tracks which addresses we have seen and their role

// check for duplicates in enabled list
Expand Down
32 changes: 16 additions & 16 deletions precompile/allowlist/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,32 @@ import (
"github.com/stretchr/testify/require"
)

func TestVerifyAllowlistConfig(t *testing.T) {
func TestVerifyAllowlistAllowList(t *testing.T) {
admins := []common.Address{{1}}
enableds := []common.Address{{2}}
tests := []struct {
name string
config Config
config AllowListConfig
expectedError string
}{
{
name: "invalid allow list config in allowlist",
config: Config{admins, admins},
config: AllowListConfig{admins, admins},
expectedError: "cannot set address",
},
{
name: "nil member allow list config in allowlist",
config: Config{nil, nil},
config: AllowListConfig{nil, nil},
expectedError: "",
},
{
name: "empty member allow list config in allowlist",
config: Config{[]common.Address{}, []common.Address{}},
config: AllowListConfig{[]common.Address{}, []common.Address{}},
expectedError: "",
},
{
name: "valid allow list config in allowlist",
config: Config{admins, enableds},
config: AllowListConfig{admins, enableds},
expectedError: "",
},
}
Expand All @@ -53,37 +53,37 @@ func TestVerifyAllowlistConfig(t *testing.T) {
}
}

func TestEqualAllowListConfig(t *testing.T) {
func TestEqualAllowListAllowList(t *testing.T) {
admins := []common.Address{{1}}
enableds := []common.Address{{2}}
tests := []struct {
name string
config *Config
other *Config
config *AllowListConfig
other *AllowListConfig
expected bool
}{
{
name: "non-nil config and nil other",
config: &Config{admins, enableds},
config: &AllowListConfig{admins, enableds},
other: nil,
expected: false,
},
{
name: "different admin",
config: &Config{admins, enableds},
other: &Config{[]common.Address{{3}}, enableds},
config: &AllowListConfig{admins, enableds},
other: &AllowListConfig{[]common.Address{{3}}, enableds},
expected: false,
},
{
name: "different enabled",
config: &Config{admins, enableds},
other: &Config{admins, []common.Address{{3}}},
config: &AllowListConfig{admins, enableds},
other: &AllowListConfig{admins, []common.Address{{3}}},
expected: false,
},
{
name: "same config",
config: &Config{admins, enableds},
other: &Config{admins, enableds},
config: &AllowListConfig{admins, enableds},
other: &AllowListConfig{admins, enableds},
expected: true,
},
}
Expand Down
6 changes: 3 additions & 3 deletions precompile/contract/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/subnet-evm/commontype"
"github.com/ava-labs/subnet-evm/precompile/config"
"github.com/ava-labs/subnet-evm/precompile/precompileconfig"
"github.com/ethereum/go-ethereum/common"
)

Expand Down Expand Up @@ -69,10 +69,10 @@ type BlockContext interface {
}

type Configurator interface {
NewConfig() config.Config
NewConfig() precompileconfig.Config
Configure(
chainConfig ChainConfig,
precompileConfig config.Config,
precompileconfig precompileconfig.Config,
state StateDB,
blockContext BlockContext,
) error
Expand Down
4 changes: 2 additions & 2 deletions precompile/contract/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"testing"

"github.com/ava-labs/subnet-evm/accounts/abi"
"github.com/ava-labs/subnet-evm/precompile/config"
"github.com/ava-labs/subnet-evm/precompile/precompileconfig"
"github.com/ava-labs/subnet-evm/vmerrs"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
Expand Down Expand Up @@ -39,7 +39,7 @@ type PrecompileTest struct {
// It should be the same precompile config that is used in the
// precompile's configurator.
// If nil, Configure on the Configurator will not be called.
Config config.Config
Config precompileconfig.Config
// BeforeHook is called before the precompile is called.
BeforeHook func(t *testing.T, state StateDB)
// AfterHook is called after the precompile is called.
Expand Down
20 changes: 10 additions & 10 deletions precompile/contracts/deployerallowlist/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,36 @@ import (
"math/big"

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

var _ config.Config = &Config{}
var _ precompileconfig.Config = &Config{}

// Config contains the configuration for the ContractDeployerAllowList precompile,
// consisting of the initial allowlist and the timestamp for the network upgrade.
type Config struct {
allowlist.Config
config.Upgrade
allowlist.AllowListConfig
precompileconfig.Upgrade
}

// NewConfig returns a config for a network upgrade at [blockTimestamp] that enables
// ContractDeployerAllowList with [admins] and [enableds] as members of the allowlist.
func NewConfig(blockTimestamp *big.Int, admins []common.Address, enableds []common.Address) *Config {
return &Config{
Config: allowlist.Config{
AllowListConfig: allowlist.AllowListConfig{
AdminAddresses: admins,
EnabledAddresses: enableds,
},
Upgrade: config.Upgrade{BlockTimestamp: blockTimestamp},
Upgrade: precompileconfig.Upgrade{BlockTimestamp: blockTimestamp},
}
}

// NewDisableConfig returns config for a network upgrade at [blockTimestamp]
// that disables ContractDeployerAllowList.
func NewDisableConfig(blockTimestamp *big.Int) *Config {
return &Config{
Upgrade: config.Upgrade{
Upgrade: precompileconfig.Upgrade{
BlockTimestamp: blockTimestamp,
Disable: true,
},
Expand All @@ -46,13 +46,13 @@ func NewDisableConfig(blockTimestamp *big.Int) *Config {
func (*Config) Key() string { return ConfigKey }

// Equal returns true if [cfg] is a [*ContractDeployerAllowListConfig] and it has been configured identical to [c].
func (c *Config) Equal(cfg config.Config) bool {
func (c *Config) Equal(cfg precompileconfig.Config) bool {
// typecast before comparison
other, ok := (cfg).(*Config)
if !ok {
return false
}
return c.Upgrade.Equal(&other.Upgrade) && c.Config.Equal(&other.Config)
return c.Upgrade.Equal(&other.Upgrade) && c.AllowListConfig.Equal(&other.AllowListConfig)
}

func (c *Config) Verify() error { return c.Config.Verify() }
func (c *Config) Verify() error { return c.AllowListConfig.Verify() }
10 changes: 5 additions & 5 deletions precompile/contracts/deployerallowlist/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"math/big"
"testing"

"github.com/ava-labs/subnet-evm/precompile/config"
"github.com/ava-labs/subnet-evm/precompile/precompileconfig"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
)
Expand All @@ -16,7 +16,7 @@ func TestVerifyContractDeployerConfig(t *testing.T) {
admins := []common.Address{{1}}
tests := []struct {
name string
config config.Config
config precompileconfig.Config
ExpectedError string
}{
{
Expand Down Expand Up @@ -44,8 +44,8 @@ func TestEqualContractDeployerAllowListConfig(t *testing.T) {
enableds := []common.Address{{2}}
tests := []struct {
name string
config config.Config
other config.Config
config precompileconfig.Config
other precompileconfig.Config
expected bool
}{
{
Expand All @@ -57,7 +57,7 @@ func TestEqualContractDeployerAllowListConfig(t *testing.T) {
{
name: "different type",
config: NewConfig(big.NewInt(3), admins, enableds),
other: config.NewNoopStatefulPrecompileConfig(),
other: precompileconfig.NewNoopStatefulPrecompileConfig(),
expected: false,
},
{
Expand Down
Loading