Skip to content

Commit

Permalink
Revert "Add custom registered precompiles to the active precompile li…
Browse files Browse the repository at this point in the history
…st (#21)" (#30)

This reverts commit 031b717.
  • Loading branch information
nddeluca authored May 1, 2024
1 parent fef649a commit 25f547b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 88 deletions.
23 changes: 6 additions & 17 deletions core/vm/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"github.com/ethereum/go-ethereum/crypto/kzg4844"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/precompile/contract"
"github.com/ethereum/go-ethereum/precompile/modules"
"golang.org/x/crypto/ripemd160"
)

Expand Down Expand Up @@ -152,29 +151,19 @@ func init() {
}

// ActivePrecompiles returns the precompiles enabled with the current configuration.
func ActivePrecompiles(rules params.Rules) (precompiles []common.Address) {
func ActivePrecompiles(rules params.Rules) []common.Address {
switch {
case rules.IsCancun:
precompiles = PrecompiledAddressesCancun
return PrecompiledAddressesCancun
case rules.IsBerlin:
precompiles = PrecompiledAddressesBerlin

return PrecompiledAddressesBerlin
case rules.IsIstanbul:
precompiles = PrecompiledAddressesIstanbul
return PrecompiledAddressesIstanbul
case rules.IsByzantium:
precompiles = PrecompiledAddressesByzantium
return PrecompiledAddressesByzantium
default:
precompiles = PrecompiledAddressesHomestead
}

// TODO: Consider performance improvements here to prevent iteration & allocations on every call.
// NOTE: If using init to cache addresses, then some care should be taken to ensure all precompiles are
// registered before being cached here.
for _, precompile := range modules.RegisteredModules() {
precompiles = append(precompiles, precompile.Address)
return PrecompiledAddressesHomestead
}

return precompiles
}

// RunPrecompiledContract runs and evaluates the output of a precompiled contract.
Expand Down
71 changes: 0 additions & 71 deletions core/vm/precompile_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package vm

import (
"math/big"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -138,71 +135,3 @@ func TestEvmIsPrecompileMethod(t *testing.T) {
})
}
}

func TestActivePrecompiles(t *testing.T) {
genesisTime := time.Now()
getBlockTime := func(height *big.Int) *uint64 {
if !height.IsInt64() {
t.Fatalf("expected height bounded to int64")
}
totalBlockSeconds := time.Duration(10*height.Int64()) * time.Second
blockTimeUnix := uint64(genesisTime.Add(totalBlockSeconds).Unix())

return &blockTimeUnix
}
chainConfig := params.TestChainConfig
chainConfig.HomesteadBlock = big.NewInt(1)
chainConfig.ByzantiumBlock = big.NewInt(2)
chainConfig.IstanbulBlock = big.NewInt(3)
chainConfig.BerlinBlock = big.NewInt(4)
chainConfig.CancunTime = getBlockTime(big.NewInt(5))

testCases := []struct {
name string
block *big.Int
}{
{"homestead", chainConfig.HomesteadBlock},
{"byzantium", chainConfig.ByzantiumBlock},
{"istanbul", chainConfig.IstanbulBlock},
{"berlin", chainConfig.BerlinBlock},
{"cancun", new(big.Int).Add(chainConfig.BerlinBlock, big.NewInt(1))},
}

// custom precompile address used for test
contractAddress := common.HexToAddress("0x0400000000000000000000000000000000000000")

// ensure we are not being shadowed by a core preompile address
for _, tc := range testCases {
rules := chainConfig.Rules(tc.block, false, *getBlockTime(tc.block))

for _, precompileAddr := range ActivePrecompiles(rules) {
if precompileAddr == contractAddress {
t.Fatalf("expected precompile %s to not be returned in %s block", contractAddress, tc.name)
}
}
}

// register the precompile
module := modules.Module{
Address: contractAddress,
Contract: new(mockStatefulPrecompiledContract),
}

// TODO: should we allow dynamic registration to update ActivePrecompiles?
// Or should we enforce registration only at init?
err := modules.RegisterModule(module)
require.NoError(t, err, "could not register precompile for test")

for _, tc := range testCases {
rules := chainConfig.Rules(tc.block, false, *getBlockTime(tc.block))

exists := false
for _, precompileAddr := range ActivePrecompiles(rules) {
if precompileAddr == contractAddress {
exists = true
}
}

assert.True(t, exists, "expected %s block to include active stateful precompile %s", tc.name, contractAddress)
}
}

0 comments on commit 25f547b

Please sign in to comment.