Skip to content

Commit

Permalink
refactor: simplify NewEVM() object creation in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yang committed Jan 17, 2025
1 parent ff27306 commit 0d26abc
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 236 deletions.
23 changes: 2 additions & 21 deletions x/evm/evmmodule/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
gethcommon "github.com/ethereum/go-ethereum/common"
gethcore "github.com/ethereum/go-ethereum/core/types"
"github.com/stretchr/testify/suite"

"github.com/NibiruChain/nibiru/v2/eth"
"github.com/NibiruChain/nibiru/v2/x/evm"
"github.com/NibiruChain/nibiru/v2/x/evm/embeds"
"github.com/NibiruChain/nibiru/v2/x/evm/evmmodule"
"github.com/NibiruChain/nibiru/v2/x/evm/evmtest"
"github.com/NibiruChain/nibiru/v2/x/evm/keeper"
)

type Suite struct {
Expand All @@ -25,8 +23,7 @@ type Suite struct {

// TestKeeperSuite: Runs all the tests in the suite.
func TestKeeperSuite(t *testing.T) {
s := new(Suite)
suite.Run(t, s)
suite.Run(t, new(Suite))
}

// TestExportInitGenesis
Expand All @@ -51,23 +48,7 @@ func (s *Suite) TestExportInitGenesis() {
s.Require().NoError(err)
erc20Addr := deployResp.ContractAddr

txConfig := deps.EvmKeeper.TxConfig(deps.Ctx, gethcommon.BigToHash(big.NewInt(0)))
stateDB := deps.EvmKeeper.NewStateDB(deps.Ctx, txConfig)
evmCfg := deps.EvmKeeper.GetEVMConfig(deps.Ctx)
evmMsg := gethcore.NewMessage(
evm.EVM_MODULE_ADDRESS,
&evm.EVM_MODULE_ADDRESS,
deps.EvmKeeper.GetAccNonce(deps.Ctx, evm.EVM_MODULE_ADDRESS),
big.NewInt(0),
keeper.Erc20GasLimitExecute,
big.NewInt(0),
big.NewInt(0),
big.NewInt(0),
[]byte{},
gethcore.AccessList{},
false,
)
evmObj := deps.EvmKeeper.NewEVM(deps.Ctx, evmMsg, evmCfg, nil /*tracer*/, stateDB)
evmObj := deps.NewEVM()
totalSupply, err := deps.EvmKeeper.ERC20().LoadERC20BigInt(
deps.Ctx, evmObj, erc20Contract.ABI, erc20Addr, "totalSupply",
)
Expand Down
12 changes: 8 additions & 4 deletions x/evm/evmtest/test_deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"

gethcommon "github.com/ethereum/go-ethereum/common"

gethcore "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"

"github.com/NibiruChain/nibiru/v2/app"
"github.com/NibiruChain/nibiru/v2/app/codec"
Expand All @@ -34,14 +33,13 @@ func NewTestDeps() TestDeps {
eth.RegisterInterfaces(encCfg.InterfaceRegistry)
app, ctx := testapp.NewNibiruTestAppAndContext()
ctx = ctx.WithChainID(eth.EIP155ChainID_Testnet)
ethAcc := NewEthPrivAcc()
return TestDeps{
App: app,
Ctx: ctx,
EncCfg: encCfg,
EvmKeeper: app.EvmKeeper,
GenState: evm.DefaultGenesisState(),
Sender: ethAcc,
Sender: NewEthPrivAcc(),
}
}

Expand All @@ -54,6 +52,12 @@ func (deps TestDeps) NewStateDB() *statedb.StateDB {
)
}

func (deps TestDeps) NewEVM() *vm.EVM {
stateDB := deps.EvmKeeper.NewStateDB(deps.Ctx, statedb.NewEmptyTxConfig(gethcommon.BytesToHash(deps.Ctx.HeaderHash())))
evmObj := deps.EvmKeeper.NewEVM(deps.Ctx, MOCK_GETH_MESSAGE, deps.EvmKeeper.GetEVMConfig(deps.Ctx), evm.NewNoOpTracer(), stateDB)
return evmObj
}

func (deps *TestDeps) GethSigner() gethcore.Signer {
return gethcore.LatestSignerForChainID(deps.App.EvmKeeper.EthChainID(deps.Ctx))
}
Expand Down
13 changes: 6 additions & 7 deletions x/evm/keeper/erc20_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ package keeper_test
import (
"math/big"

gethcommon "github.com/ethereum/go-ethereum/common"

"github.com/NibiruChain/nibiru/v2/x/evm"
"github.com/NibiruChain/nibiru/v2/x/evm/evmtest"
)
Expand All @@ -16,19 +14,16 @@ func (s *Suite) TestERC20Calls() {
funtoken := evmtest.CreateFunTokenForBankCoin(deps, bankDenom, &s.Suite)
contract := funtoken.Erc20Addr.Address

s.T().Log("create evmObj")
stateDB := deps.EvmKeeper.NewStateDB(deps.Ctx, deps.EvmKeeper.TxConfig(deps.Ctx, gethcommon.Hash{}))
evmObj := deps.EvmKeeper.NewEVM(deps.Ctx, evmtest.MOCK_GETH_MESSAGE, deps.EvmKeeper.GetEVMConfig(deps.Ctx), evm.NewNoOpTracer(), stateDB)

s.Run("Mint tokens - Fail from non-owner", func() {
_, err := deps.EvmKeeper.ERC20().Mint(
contract, deps.Sender.EthAddr, evm.EVM_MODULE_ADDRESS,
big.NewInt(69_420), deps.Ctx, evmObj,
big.NewInt(69_420), deps.Ctx, deps.NewEVM(),
)
s.ErrorContains(err, "Ownable: caller is not the owner")
})

s.Run("Mint tokens - Success", func() {
evmObj := deps.NewEVM()
_, err := deps.EvmKeeper.ERC20().Mint(
contract, /*erc20Addr*/
evm.EVM_MODULE_ADDRESS, /*sender*/
Expand All @@ -44,6 +39,7 @@ func (s *Suite) TestERC20Calls() {
})

s.Run("Transfer - Not enough funds", func() {
evmObj := deps.NewEVM()
_, _, err := deps.EvmKeeper.ERC20().Transfer(
contract, deps.Sender.EthAddr, evm.EVM_MODULE_ADDRESS,
big.NewInt(9_420), deps.Ctx, evmObj,
Expand All @@ -55,6 +51,7 @@ func (s *Suite) TestERC20Calls() {
})

s.Run("Transfer - Success (sanity check)", func() {
evmObj := deps.NewEVM()
sentAmt, _, err := deps.EvmKeeper.ERC20().Transfer(
contract, /*erc20Addr*/
evm.EVM_MODULE_ADDRESS, /*sender*/
Expand All @@ -73,6 +70,7 @@ func (s *Suite) TestERC20Calls() {
})

s.Run("Burn tokens - Allowed as non-owner", func() {
evmObj := deps.NewEVM()
_, err := deps.EvmKeeper.ERC20().Burn(
contract, /*erc20Addr*/
deps.Sender.EthAddr, /*sender*/
Expand All @@ -87,6 +85,7 @@ func (s *Suite) TestERC20Calls() {
})

s.Run("Burn tokens - Allowed as owner", func() {
evmObj := deps.NewEVM()
_, err := deps.EvmKeeper.ERC20().Burn(
contract, /*erc20Addr*/
evm.EVM_MODULE_ADDRESS, /*sender*/
Expand Down
32 changes: 10 additions & 22 deletions x/evm/keeper/funtoken_from_coin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
bank "github.com/cosmos/cosmos-sdk/x/bank/types"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/suite"

Expand All @@ -20,7 +19,6 @@ import (
"github.com/NibiruChain/nibiru/v2/x/evm/evmtest"
"github.com/NibiruChain/nibiru/v2/x/evm/keeper"
"github.com/NibiruChain/nibiru/v2/x/evm/precompile"
"github.com/NibiruChain/nibiru/v2/x/evm/statedb"
)

func (s *FunTokenFromCoinSuite) TestCreateFunTokenFromCoin() {
Expand All @@ -29,8 +27,7 @@ func (s *FunTokenFromCoinSuite) TestCreateFunTokenFromCoin() {
// Compute contract address. FindERC20 should fail
nonce := deps.NewStateDB().GetNonce(deps.Sender.EthAddr)
contractAddress := crypto.CreateAddress(deps.Sender.EthAddr, nonce)
stateDB := deps.EvmKeeper.NewStateDB(deps.Ctx, statedb.NewEmptyTxConfig(gethcommon.BytesToHash(deps.Ctx.HeaderHash())))
evmObj := deps.EvmKeeper.NewEVM(deps.Ctx, evmtest.MOCK_GETH_MESSAGE, deps.EvmKeeper.GetEVMConfig(deps.Ctx), evm.NewNoOpTracer(), stateDB)
evmObj := deps.NewEVM()
metadata, err := deps.EvmKeeper.FindERC20Metadata(deps.Ctx, evmObj, contractAddress)
s.Require().Error(err)
s.Require().Nil(metadata)
Expand Down Expand Up @@ -172,8 +169,7 @@ func (s *FunTokenFromCoinSuite) TestCreateFunTokenFromCoin() {

func (s *FunTokenFromCoinSuite) TestConvertCoinToEvmAndBack() {
deps := evmtest.NewTestDeps()
stateDB := deps.EvmKeeper.NewStateDB(deps.Ctx, statedb.NewEmptyTxConfig(gethcommon.BytesToHash(deps.Ctx.HeaderHash())))
evmObj := deps.EvmKeeper.NewEVM(deps.Ctx, evmtest.MOCK_GETH_MESSAGE, deps.EvmKeeper.GetEVMConfig(deps.Ctx), evm.NewNoOpTracer(), stateDB)
evmObj := deps.NewEVM()
alice := evmtest.NewEthPrivAcc()

// Initial setup
Expand Down Expand Up @@ -240,8 +236,7 @@ func (s *FunTokenFromCoinSuite) TestConvertCoinToEvmAndBack() {
deps.Sender.NibiruAddr.String(),
)
s.Require().NoError(err)
stateDB = deps.EvmKeeper.NewStateDB(deps.Ctx, statedb.NewEmptyTxConfig(gethcommon.BytesToHash(deps.Ctx.HeaderHash())))
evmObj = deps.EvmKeeper.NewEVM(deps.Ctx, evmtest.MOCK_GETH_MESSAGE, deps.EvmKeeper.GetEVMConfig(deps.Ctx), evm.NewNoOpTracer(), stateDB)
evmObj = deps.NewEVM()
_, err = deps.EvmKeeper.CallContractWithInput(
deps.Ctx,
evmObj,
Expand Down Expand Up @@ -269,8 +264,7 @@ func (s *FunTokenFromCoinSuite) TestConvertCoinToEvmAndBack() {
deps.ResetGasMeter()

s.T().Log("sad: Convert more erc-20 to back to bank coin, insufficient funds")
stateDB = deps.EvmKeeper.NewStateDB(deps.Ctx, statedb.NewEmptyTxConfig(gethcommon.BytesToHash(deps.Ctx.HeaderHash())))
evmObj = deps.EvmKeeper.NewEVM(deps.Ctx, evmtest.MOCK_GETH_MESSAGE, deps.EvmKeeper.GetEVMConfig(deps.Ctx), evm.NewNoOpTracer(), stateDB)
evmObj = deps.NewEVM()
_, err = deps.EvmKeeper.CallContractWithInput(
deps.Ctx,
evmObj,
Expand Down Expand Up @@ -303,8 +297,7 @@ func (s *FunTokenFromCoinSuite) TestConvertCoinToEvmAndBack() {
// - Module account: 0 NIBI escrowed
func (s *FunTokenFromCoinSuite) TestNativeSendThenPrecompileSend() {
deps := evmtest.NewTestDeps()
stateDB := deps.EvmKeeper.NewStateDB(deps.Ctx, statedb.NewEmptyTxConfig(gethcommon.BytesToHash(deps.Ctx.HeaderHash())))
evmObj := deps.EvmKeeper.NewEVM(deps.Ctx, evmtest.MOCK_GETH_MESSAGE, deps.EvmKeeper.GetEVMConfig(deps.Ctx), evm.NewNoOpTracer(), stateDB)
evmObj := deps.NewEVM()
bankDenom := evm.EVMBankDenom

// Initial setup
Expand Down Expand Up @@ -382,8 +375,7 @@ func (s *FunTokenFromCoinSuite) TestNativeSendThenPrecompileSend() {
newSendAmtSendToBank, /*amount*/
)
s.Require().NoError(err)
stateDB = deps.EvmKeeper.NewStateDB(deps.Ctx, statedb.NewEmptyTxConfig(gethcommon.BytesToHash(deps.Ctx.HeaderHash())))
evmObj = deps.EvmKeeper.NewEVM(deps.Ctx, evmtest.MOCK_GETH_MESSAGE, deps.EvmKeeper.GetEVMConfig(deps.Ctx), evm.NewNoOpTracer(), stateDB)
evmObj = deps.NewEVM()
evmResp, err := deps.EvmKeeper.CallContractWithInput(
deps.Ctx,
evmObj,
Expand Down Expand Up @@ -426,8 +418,7 @@ func (s *FunTokenFromCoinSuite) TestNativeSendThenPrecompileSend() {
newSendAmtSendToBank, /*amount*/
)
s.Require().NoError(err)
stateDB = deps.EvmKeeper.NewStateDB(deps.Ctx, statedb.NewEmptyTxConfig(gethcommon.BytesToHash(deps.Ctx.HeaderHash())))
evmObj = deps.EvmKeeper.NewEVM(deps.Ctx, evmtest.MOCK_GETH_MESSAGE, deps.EvmKeeper.GetEVMConfig(deps.Ctx), evm.NewNoOpTracer(), stateDB)
evmObj = deps.NewEVM()
evmResp, err = deps.EvmKeeper.CallContractWithInput(
deps.Ctx,
evmObj,
Expand Down Expand Up @@ -482,8 +473,7 @@ func (s *FunTokenFromCoinSuite) TestNativeSendThenPrecompileSend() {
// - Module account: 1 NIBI escrowed (which Alice holds as 1 WNIBI)
func (s *FunTokenFromCoinSuite) TestERC20TransferThenPrecompileSend() {
deps := evmtest.NewTestDeps()
stateDB := deps.EvmKeeper.NewStateDB(deps.Ctx, statedb.NewEmptyTxConfig(gethcommon.BytesToHash(deps.Ctx.HeaderHash())))
evmObj := deps.EvmKeeper.NewEVM(deps.Ctx, evmtest.MOCK_GETH_MESSAGE, deps.EvmKeeper.GetEVMConfig(deps.Ctx), evm.NewNoOpTracer(), stateDB)
evmObj := deps.NewEVM()

funToken := s.fundAndCreateFunToken(deps, 10e6)

Expand Down Expand Up @@ -541,8 +531,7 @@ func (s *FunTokenFromCoinSuite) TestERC20TransferThenPrecompileSend() {
big.NewInt(9e6), /*amount*/
)
s.Require().NoError(err)
stateDB = deps.EvmKeeper.NewStateDB(deps.Ctx, statedb.NewEmptyTxConfig(gethcommon.BytesToHash(deps.Ctx.HeaderHash())))
evmObj = deps.EvmKeeper.NewEVM(deps.Ctx, evmtest.MOCK_GETH_MESSAGE, deps.EvmKeeper.GetEVMConfig(deps.Ctx), evm.NewNoOpTracer(), stateDB)
evmObj = deps.NewEVM()
_, err = deps.EvmKeeper.CallContractWithInput(
deps.Ctx,
evmObj,
Expand Down Expand Up @@ -598,8 +587,7 @@ func (s *FunTokenFromCoinSuite) TestERC20TransferThenPrecompileSend() {
// - Module account: 10 NIBI escrowed (which Test contract holds as 10 WNIBI)
func (s *FunTokenFromCoinSuite) TestPrecompileSelfCallRevert() {
deps := evmtest.NewTestDeps()
stateDB := deps.EvmKeeper.NewStateDB(deps.Ctx, statedb.NewEmptyTxConfig(gethcommon.BytesToHash(deps.Ctx.HeaderHash())))
evmObj := deps.EvmKeeper.NewEVM(deps.Ctx, evmtest.MOCK_GETH_MESSAGE, deps.EvmKeeper.GetEVMConfig(deps.Ctx), evm.NewNoOpTracer(), stateDB)
evmObj := deps.NewEVM()

// Initial setup
funToken := s.fundAndCreateFunToken(deps, 10e6)
Expand Down
Loading

0 comments on commit 0d26abc

Please sign in to comment.