From a3549bcf16b540cd294395e247bc12a1d79f2a9d Mon Sep 17 00:00:00 2001 From: Tanmay Date: Thu, 11 Apr 2024 00:01:58 -0400 Subject: [PATCH] fix unit tests for ZEVMdeposit --- .../keeper/msg_server_vote_inbound_tx_test.go | 15 ++++- x/crosschain/keeper/process_inbound_test.go | 9 +-- x/fungible/keeper/deposits_test.go | 57 ++++++++++--------- .../keeper/zevm_message_passing_test.go | 28 +++++++++ 4 files changed, 77 insertions(+), 32 deletions(-) diff --git a/x/crosschain/keeper/msg_server_vote_inbound_tx_test.go b/x/crosschain/keeper/msg_server_vote_inbound_tx_test.go index 87ff60e729..69af8bb5ee 100644 --- a/x/crosschain/keeper/msg_server_vote_inbound_tx_test.go +++ b/x/crosschain/keeper/msg_server_vote_inbound_tx_test.go @@ -3,6 +3,7 @@ package keeper_test import ( "encoding/hex" "errors" + "fmt" "math/big" "math/rand" "testing" @@ -10,6 +11,8 @@ import ( sdkmath "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" ethcommon "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" + "github.com/evmos/ethermint/x/evm/statedb" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" "github.com/zeta-chain/zetacore/pkg/chains" @@ -45,7 +48,7 @@ func setObservers(t *testing.T, k *keeper.Keeper, ctx sdk.Context, zk keepertest // https://github.com/zeta-chain/node/issues/1542 func TestKeeper_VoteOnObservedInboundTx(t *testing.T) { t.Run("successfully vote on evm deposit", func(t *testing.T) { - k, ctx, _, zk := keepertest.CrosschainKeeper(t) + k, ctx, sdkk, zk := keepertest.CrosschainKeeper(t) msgServer := keeper.NewMsgServerImpl(*k) validatorList := setObservers(t, k, ctx, zk) to, from := int64(1337), int64(101) @@ -61,6 +64,13 @@ func TestKeeper_VoteOnObservedInboundTx(t *testing.T) { zk.ObserverKeeper.SetTSS(ctx, sample.Tss()) msg := sample.InboundVote(0, from, to) + + err := sdkk.EvmKeeper.SetAccount(ctx, ethcommon.HexToAddress(msg.Receiver), statedb.Account{ + Nonce: 0, + Balance: big.NewInt(0), + CodeHash: crypto.Keccak256(nil), + }) + require.NoError(t, err) for _, validatorAddr := range validatorList { msg.Creator = validatorAddr _, err := msgServer.VoteOnObservedInboundTx( @@ -78,7 +88,8 @@ func TestKeeper_VoteOnObservedInboundTx(t *testing.T) { require.Equal(t, ballot.BallotStatus, observertypes.BallotStatus_BallotFinalized_SuccessObservation) cctx, found := k.GetCrossChainTx(ctx, msg.Digest()) require.True(t, found) - require.Equal(t, cctx.CctxStatus.Status, types.CctxStatus_OutboundMined) + fmt.Println(cctx.CctxStatus.StatusMessage) + require.Equal(t, types.CctxStatus_OutboundMined, cctx.CctxStatus.Status) require.Equal(t, cctx.InboundTxParams.TxFinalizationStatus, types.TxFinalizationStatus_Executed) }) diff --git a/x/crosschain/keeper/process_inbound_test.go b/x/crosschain/keeper/process_inbound_test.go index 491e362f78..dcde32ac6f 100644 --- a/x/crosschain/keeper/process_inbound_test.go +++ b/x/crosschain/keeper/process_inbound_test.go @@ -29,8 +29,9 @@ func TestKeeper_ProcessInboundZEVMDeposit(t *testing.T) { amount := big.NewInt(42) // expect DepositCoinZeta to be called - fungibleMock.On("DepositCoinZeta", mock.Anything, receiver, amount). - Return(nil) + fungibleMock.On("ZEVMDepositAndCallContract", mock.Anything, + mock.Anything, + receiver, int64(0), amount, mock.Anything, mock.Anything).Return(nil, nil) // call ProcessInbound cctx := sample.CrossChainTx(t, "test") @@ -55,8 +56,8 @@ func TestKeeper_ProcessInboundZEVMDeposit(t *testing.T) { amount := big.NewInt(42) // mock unsuccessful HandleEVMDeposit which does not revert - fungibleMock.On("DepositCoinZeta", mock.Anything, receiver, amount). - Return(fmt.Errorf("deposit error"), false) + + fungibleMock.On("ZEVMDepositAndCallContract", mock.Anything, mock.Anything, receiver, int64(0), amount, mock.Anything, mock.Anything).Return(nil, fmt.Errorf("deposit error")) // call ProcessInbound cctx := sample.CrossChainTx(t, "test") diff --git a/x/fungible/keeper/deposits_test.go b/x/fungible/keeper/deposits_test.go index 31dd1b3471..0e6f245173 100644 --- a/x/fungible/keeper/deposits_test.go +++ b/x/fungible/keeper/deposits_test.go @@ -4,10 +4,10 @@ import ( "math/big" "testing" + "cosmossdk.io/errors" "cosmossdk.io/math" - ethcommon "github.com/ethereum/go-ethereum/common" - sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" "github.com/zeta-chain/zetacore/cmd/zetacored/config" "github.com/zeta-chain/zetacore/pkg/chains" @@ -357,29 +357,34 @@ func TestKeeper_ZRC20DepositAndCallContract(t *testing.T) { } func TestKeeper_DepositCoinZeta(t *testing.T) { - k, ctx, sdkk, _ := keepertest.FungibleKeeper(t) - to := sample.EthAddress() - amount := big.NewInt(1) - zetaToAddress := sdk.AccAddress(to.Bytes()) - - b := sdkk.BankKeeper.GetBalance(ctx, zetaToAddress, config.BaseDenom) - require.Equal(t, int64(0), b.Amount.Int64()) - - err := k.DepositCoinZeta(ctx, to, amount) - require.NoError(t, err) - b = sdkk.BankKeeper.GetBalance(ctx, zetaToAddress, config.BaseDenom) - require.Equal(t, amount.Int64(), b.Amount.Int64()) -} + t.Run("successfully deposit coin", func(t *testing.T) { + k, ctx, sdkk, _ := keepertest.FungibleKeeper(t) + to := sample.EthAddress() + amount := big.NewInt(1) + zetaToAddress := sdk.AccAddress(to.Bytes()) + + b := sdkk.BankKeeper.GetBalance(ctx, zetaToAddress, config.BaseDenom) + require.Equal(t, int64(0), b.Amount.Int64()) + + err := k.DepositCoinZeta(ctx, to, amount) + require.NoError(t, err) + b = sdkk.BankKeeper.GetBalance(ctx, zetaToAddress, config.BaseDenom) + require.Equal(t, amount.Int64(), b.Amount.Int64()) + }) + + t.Run("should fail if MintZetaToEVMAccount fails", func(t *testing.T) { + k, ctx, sdkk, _ := keepertest.FungibleKeeperWithMocks(t, keepertest.FungibleMockOptions{UseBankMock: true}) + bankMock := keepertest.GetFungibleBankMock(t, k) + to := sample.EthAddress() + amount := big.NewInt(1) + zetaToAddress := sdk.AccAddress(to.Bytes()) + + b := sdkk.BankKeeper.GetBalance(ctx, zetaToAddress, config.BaseDenom) + require.Equal(t, int64(0), b.Amount.Int64()) + errorMint := errors.New("", 1, "error minting coins") + bankMock.On("MintCoins", ctx, types.ModuleName, mock.Anything).Return(errorMint).Once() + err := k.DepositCoinZeta(ctx, to, amount) + require.ErrorIs(t, err, errorMint) -func Test_AddressConvertion(t *testing.T) { - addressCosmosString := sample.AccAddress() - addressCosmsosAccAddress := sdk.MustAccAddressFromBech32(addressCosmosString) - // Logic used in depositCoins function - addressEth := ethcommon.HexToAddress(addressCosmosString) - //https://github.com/zeta-chain/zeta-node/blob/zevm-message-passing/x/fungible/keeper/deposits.go#L17-L17 - depositAddress := sdk.AccAddress(addressEth.Bytes()) - depositAddressString := depositAddress.String() - - require.Equal(t, addressCosmsosAccAddress, depositAddress) - require.Equal(t, addressCosmosString, depositAddressString) + }) } diff --git a/x/fungible/keeper/zevm_message_passing_test.go b/x/fungible/keeper/zevm_message_passing_test.go index 467e578b8f..e15e9d25fd 100644 --- a/x/fungible/keeper/zevm_message_passing_test.go +++ b/x/fungible/keeper/zevm_message_passing_test.go @@ -4,9 +4,11 @@ import ( "math/big" "testing" + "cosmossdk.io/errors" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/ethereum/go-ethereum/crypto" "github.com/evmos/ethermint/x/evm/statedb" + "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" "github.com/zeta-chain/zetacore/cmd/zetacored/config" "github.com/zeta-chain/zetacore/testutil/contracts" @@ -97,6 +99,32 @@ func TestKeeper_ZEVMDepositAndCallContract(t *testing.T) { require.ErrorIs(t, err, types.ErrAccountNotFound) require.ErrorContains(t, err, "account not found") }) + + t.Run("fail ZEVMDepositAndCallContract id Deposit Fails", func(t *testing.T) { + k, ctx, sdkk, _ := keepertest.FungibleKeeperWithMocks(t, keepertest.FungibleMockOptions{UseBankMock: true}) + _ = k.GetAuthKeeper().GetModuleAccount(ctx, types.ModuleName) + + bankMock := keepertest.GetFungibleBankMock(t, k) + deploySystemContracts(t, ctx, k, sdkk.EvmKeeper) + zetaTxSender := sample.EthAddress() + zetaTxReceiver := sample.EthAddress() + inboundSenderChainID := int64(1) + inboundAmount := big.NewInt(45) + data := []byte("message") + cctxIndexBytes := [32]byte{} + + err := sdkk.EvmKeeper.SetAccount(ctx, zetaTxReceiver, statedb.Account{ + Nonce: 0, + Balance: big.NewInt(0), + CodeHash: crypto.Keccak256(nil), + }) + require.NoError(t, err) + errorMint := errors.New("", 10, "error minting coins") + bankMock.On("MintCoins", ctx, types.ModuleName, mock.Anything).Return(errorMint).Once() + + _, err = k.ZEVMDepositAndCallContract(ctx, zetaTxSender, zetaTxReceiver, inboundSenderChainID, inboundAmount, data, cctxIndexBytes) + require.ErrorIs(t, err, errorMint) + }) } func TestKeeper_ZevmOnReceive(t *testing.T) { t.Run("successfully call ZevmOnReceive on connector contract ", func(t *testing.T) {