diff --git a/core/vm/contracts.go b/core/vm/contracts.go index bdeb189fb5..2d8ec6b79d 100644 --- a/core/vm/contracts.go +++ b/core/vm/contracts.go @@ -33,7 +33,6 @@ import ( "fmt" "math/big" - "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/modules" @@ -164,9 +163,6 @@ func init() { if _, ok := PrecompileAllNativeAddresses[address]; ok { panic(fmt.Errorf("precompile address collides with existing native address: %s", address)) } - if address == constants.BlackholeAddr { - panic(fmt.Errorf("cannot use address %s for stateful precompile - overlaps with blackhole address", address)) - } } } diff --git a/precompile/modules/registerer.go b/precompile/modules/registerer.go index 5de73ec657..3ab469ed06 100644 --- a/precompile/modules/registerer.go +++ b/precompile/modules/registerer.go @@ -7,6 +7,7 @@ import ( "fmt" "sort" + "github.com/ava-labs/subnet-evm/constants" "github.com/ava-labs/subnet-evm/utils" "github.com/ethereum/go-ethereum/common" ) @@ -47,6 +48,10 @@ func ReservedAddress(addr common.Address) bool { func RegisterModule(stm Module) error { address := stm.Address key := stm.ConfigKey + + if address == constants.BlackholeAddr { + return fmt.Errorf("address %s overlaps with blackhole address", address) + } if !ReservedAddress(address) { return fmt.Errorf("address %s not in a reserved range", address) } diff --git a/precompile/modules/registerer_test.go b/precompile/modules/registerer_test.go index b91e0b01ed..c0e4feb711 100644 --- a/precompile/modules/registerer_test.go +++ b/precompile/modules/registerer_test.go @@ -7,11 +7,12 @@ import ( "math/big" "testing" + "github.com/ava-labs/subnet-evm/constants" "github.com/ethereum/go-ethereum/common" "github.com/stretchr/testify/require" ) -func TestRegisterModule(t *testing.T) { +func TestInsertSortedByAddress(t *testing.T) { data := make([]Module, 0) // test that the module is registered in sorted order module1 := Module{ @@ -42,3 +43,17 @@ func TestRegisterModule(t *testing.T) { data = insertSortedByAddress(data, module2) require.Equal(t, []Module{module0, module1, module2, module3}, data) } + +func TestRegisterModuleInvalidAddresses(t *testing.T) { + // Test the blockhole address cannot be registered + m := Module{ + Address: constants.BlackholeAddr, + } + err := RegisterModule(m) + require.ErrorContains(t, err, "overlaps with blackhole address") + + // Test an address outside of the reserved ranges cannot be registered + m.Address = common.BigToAddress(big.NewInt(1)) + err = RegisterModule(m) + require.ErrorContains(t, err, "not in a reserved range") +}