Skip to content

Commit

Permalink
Added TestInitPrecompiles unit-test
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeniy-scherbina committed May 6, 2024
1 parent 2ada74f commit c4fc436
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions x/evm/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@ package keeper_test
import (
"math/big"

sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/ethereum/go-ethereum/common"

ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
"github.com/evmos/ethermint/x/evm/statedb"
"github.com/evmos/ethermint/x/evm/types"
)

const PrecompileNonce uint64 = 1

var PrecompileCode = []byte{0x1}

func (suite *KeeperTestSuite) TestEthereumTx() {
var (
err error
Expand Down Expand Up @@ -115,3 +121,73 @@ func (suite *KeeperTestSuite) TestUpdateParams() {
})
}
}

func (suite *KeeperTestSuite) TestInitPrecompiles() {
addr1 := "0x1000000000000000000000000000000000000000"
addr2 := "0x2000000000000000000000000000000000000000"
addr3 := "0x3000000000000000000000000000000000000000"

testCases := []struct {
name string
enabledPrecompiles []string
// precompiles which must be uninitialized after corresponding test case
uninitialized []string
}{
{
name: "enable addr1 and addr2",
enabledPrecompiles: []string{addr1, addr2},
uninitialized: []string{addr3},
},
{
name: "enable addr3, and disable the rest",
enabledPrecompiles: []string{addr3},
uninitialized: []string{addr1, addr2},
},
{
name: "no changes",
enabledPrecompiles: []string{addr3},
uninitialized: []string{addr1, addr2},
},
{
name: "enable all precompiles",
enabledPrecompiles: []string{addr1, addr2, addr3},
uninitialized: []string{},
},
{
name: "disable all precompiles",
enabledPrecompiles: []string{},
uninitialized: []string{addr1, addr2, addr3},
},
}

for _, tc := range testCases {
suite.Run(tc.name, func() {
params := suite.app.EvmKeeper.GetParams(suite.ctx)
params.EnabledPrecompiles = tc.enabledPrecompiles

_, err := suite.app.EvmKeeper.UpdateParams(sdk.WrapSDKContext(suite.ctx), &types.MsgUpdateParams{
Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(),
Params: params,
})
suite.Require().NoError(err)

vmdb := suite.StateDB()

// check that precompiles are initialized
for _, hexAddr := range tc.enabledPrecompiles {
addr := common.HexToAddress(hexAddr)

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

// check that precompiles are uninitialized
for _, hexAddr := range tc.uninitialized {
addr := common.HexToAddress(hexAddr)

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

0 comments on commit c4fc436

Please sign in to comment.