Skip to content

Commit

Permalink
Initialize precompiles in InitGenesis
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeniy-scherbina committed May 6, 2024
1 parent 2ada74f commit 9894b45
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
24 changes: 24 additions & 0 deletions x/evm/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ import (
precompile_modules "github.com/ethereum/go-ethereum/precompile/modules"
ethermint "github.com/evmos/ethermint/types"
"github.com/evmos/ethermint/x/evm/keeper"
"github.com/evmos/ethermint/x/evm/statedb"
"github.com/evmos/ethermint/x/evm/types"
)

const PrecompileNonce uint64 = 1

var PrecompileCode = []byte{0x1}

// InitGenesis initializes genesis state based on exported genesis
func InitGenesis(
ctx sdk.Context,
Expand All @@ -48,6 +53,25 @@ func InitGenesis(
panic(err)
}

txConfig := statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash().Bytes()))
stateDB := statedb.New(ctx, k, txConfig)

for _, hexAddr := range data.Params.EnabledPrecompiles {
addr := common.HexToAddress(hexAddr)

// Set the nonce of the precompile's address (as is done when a contract is created) to ensure
// that it is marked as non-empty and will not be cleaned up when the statedb is finalized.
stateDB.SetNonce(addr, PrecompileNonce)
// Set the code of the precompile's address to a non-zero length byte slice to ensure that the precompile
// can be called from within Solidity contracts. Solidity adds a check before invoking a contract to ensure
// that it does not attempt to invoke a non-existent contract.
stateDB.SetCode(addr, PrecompileCode)
}

if err := stateDB.Commit(); err != nil {
panic(err)
}

err = k.SetParams(ctx, data.Params)
if err != nil {
panic(fmt.Errorf("error setting params %s", err))
Expand Down
32 changes: 32 additions & 0 deletions x/evm/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,35 @@ func (suite *EvmTestSuite) TestInitGenesis() {
})
}
}

func (suite *EvmTestSuite) TestInitPrecompiles() {
hexAddr1 := "0x1000000000000000000000000000000000000000"
hexAddr2 := "0x2000000000000000000000000000000000000000"

genState := types.DefaultGenesisState()
genState.Params.EnabledPrecompiles = []string{hexAddr1, hexAddr2}
registeredModules := []precompile_modules.Module{
{Address: common.HexToAddress(hexAddr1)},
{Address: common.HexToAddress(hexAddr2)},
}

vmdb := suite.StateDB()

// check that enabled precompile are uninitialized
for _, hexAddr := range genState.Params.EnabledPrecompiles {
addr := common.HexToAddress(hexAddr)

suite.Require().Equal(uint64(0), vmdb.GetNonce(addr))
suite.Require().Equal([]byte(nil), vmdb.GetCode(addr))
}

evm.InitGenesis(suite.ctx, suite.app.EvmKeeper, suite.app.AccountKeeper, *genState, registeredModules)

// check that enabled precompile are initialized
for _, hexAddr := range genState.Params.EnabledPrecompiles {
addr := common.HexToAddress(hexAddr)

suite.Require().Equal(evm.PrecompileNonce, vmdb.GetNonce(addr))
suite.Require().Equal(evm.PrecompileCode, vmdb.GetCode(addr))
}
}

0 comments on commit 9894b45

Please sign in to comment.