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

move blackhole check to module registerer #523

Merged
merged 4 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
4 changes: 0 additions & 4 deletions core/vm/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions precompile/modules/registerer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}
Expand Down
17 changes: 16 additions & 1 deletion precompile/modules/registerer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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")
}