Skip to content

Commit

Permalink
fix unit tests for ZEVMdeposit
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Apr 11, 2024
1 parent ef33edc commit a3549bc
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 32 deletions.
15 changes: 13 additions & 2 deletions x/crosschain/keeper/msg_server_vote_inbound_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package keeper_test
import (
"encoding/hex"
"errors"
"fmt"
"math/big"
"math/rand"
"testing"

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"
Expand Down Expand Up @@ -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)
Expand All @@ -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(
Expand All @@ -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)
})

Expand Down
9 changes: 5 additions & 4 deletions x/crosschain/keeper/process_inbound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand Down
57 changes: 31 additions & 26 deletions x/fungible/keeper/deposits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
})
}
28 changes: 28 additions & 0 deletions x/fungible/keeper/zevm_message_passing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit a3549bc

Please sign in to comment.