Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito committed Apr 2, 2024
1 parent c8de223 commit 238789a
Show file tree
Hide file tree
Showing 8 changed files with 308 additions and 28 deletions.
4 changes: 2 additions & 2 deletions testutil/keeper/crosschain.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,13 @@ func MockGetSupportedChainFromChainID(m *crosschainmocks.CrosschainObserverKeepe
Return(senderChain).Once()

}
func MockGetRevertGasLimitForERC20(m *crosschainmocks.CrosschainFungibleKeeper, asset string, senderChain chains.Chain) {
func MockGetRevertGasLimitForERC20(m *crosschainmocks.CrosschainFungibleKeeper, asset string, senderChain chains.Chain, returnVal int64) {
m.On("GetForeignCoinFromAsset", mock.Anything, asset, senderChain.ChainId).
Return(fungibletypes.ForeignCoins{
Zrc20ContractAddress: sample.EthAddress().String(),
}, true).Once()
m.On("QueryGasLimit", mock.Anything, mock.Anything).
Return(big.NewInt(100), nil).Once()
Return(big.NewInt(returnVal), nil).Once()

}
func MockPayGasAndUpdateCCTX(m *crosschainmocks.CrosschainFungibleKeeper, m2 *crosschainmocks.CrosschainObserverKeeper, ctx sdk.Context, k keeper.Keeper, senderChain chains.Chain, asset string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

func getEthereumChainID() int64 {
return 5 // Goerli

}

// setEnabledChain sets the chain as enabled in chain params
Expand Down
5 changes: 1 addition & 4 deletions x/crosschain/keeper/msg_server_gas_price_voter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,11 @@ func (k msgServer) GasPriceVoter(goCtx context.Context, msg *types.MsgGasPriceVo

chain := k.zetaObserverKeeper.GetSupportedChainFromChainID(ctx, msg.ChainId)
if chain == nil {
return nil, observertypes.ErrSupportedChains
return nil, cosmoserrors.Wrap(types.ErrUnsupportedChain, fmt.Sprintf("ChainID : %d ", msg.ChainId))
}
if ok := k.zetaObserverKeeper.IsNonTombstonedObserver(ctx, msg.Creator); !ok {
return nil, observertypes.ErrNotObserver
}
if chain == nil {
return nil, cosmoserrors.Wrap(types.ErrUnsupportedChain, fmt.Sprintf("ChainID : %d ", msg.ChainId))
}

gasPrice, isFound := k.GetGasPrice(ctx, chain.ChainId)
if !isFound {
Expand Down
204 changes: 204 additions & 0 deletions x/crosschain/keeper/msg_server_gas_price_voter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
package keeper_test

import (
"errors"
"testing"

"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/zetacore/pkg/chains"
keepertest "github.com/zeta-chain/zetacore/testutil/keeper"
"github.com/zeta-chain/zetacore/testutil/sample"
"github.com/zeta-chain/zetacore/x/crosschain/keeper"
"github.com/zeta-chain/zetacore/x/crosschain/types"
)

func TestMsgServer_GasPriceVoter(t *testing.T) {
t.Run("should error if unsupported chain", func(t *testing.T) {
k, ctx, _, _ := keepertest.CrosschainKeeperWithMocks(t, keepertest.CrosschainMockOptions{
UseObserverMock: true,
})

observerMock := keepertest.GetCrosschainObserverMock(t, k)
observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(nil)

msgServer := keeper.NewMsgServerImpl(*k)

res, err := msgServer.GasPriceVoter(ctx, &types.MsgGasPriceVoter{
ChainId: 5,
})
require.Error(t, err)
require.Nil(t, res)
})

t.Run("should error if not non tombstoned observer", func(t *testing.T) {
k, ctx, _, _ := keepertest.CrosschainKeeperWithMocks(t, keepertest.CrosschainMockOptions{
UseObserverMock: true,
})

observerMock := keepertest.GetCrosschainObserverMock(t, k)
observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(&chains.Chain{})
observerMock.On("IsNonTombstonedObserver", mock.Anything, mock.Anything).Return(false)

msgServer := keeper.NewMsgServerImpl(*k)

res, err := msgServer.GasPriceVoter(ctx, &types.MsgGasPriceVoter{
ChainId: 5,
})
require.Error(t, err)
require.Nil(t, res)
})

t.Run("should error if gas price not found and set gas price in fungible keeper fails", func(t *testing.T) {
k, ctx, _, _ := keepertest.CrosschainKeeperWithMocks(t, keepertest.CrosschainMockOptions{
UseObserverMock: true,
UseFungibleMock: true,
})

observerMock := keepertest.GetCrosschainObserverMock(t, k)
observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(&chains.Chain{
ChainId: 5,
})
observerMock.On("IsNonTombstonedObserver", mock.Anything, mock.Anything).Return(true)

fungibleMock := keepertest.GetCrosschainFungibleMock(t, k)
fungibleMock.On("SetGasPrice", mock.Anything, mock.Anything, mock.Anything).Return(uint64(0), errors.New("err"))
msgServer := keeper.NewMsgServerImpl(*k)

res, err := msgServer.GasPriceVoter(ctx, &types.MsgGasPriceVoter{
ChainId: 5,
})
require.Error(t, err)
require.Nil(t, res)
_, found := k.GetGasPrice(ctx, 5)
require.True(t, found)
})

t.Run("should not error if gas price not found and set gas price in fungible keeper succeeds", func(t *testing.T) {
k, ctx, _, _ := keepertest.CrosschainKeeperWithMocks(t, keepertest.CrosschainMockOptions{
UseObserverMock: true,
UseFungibleMock: true,
})

observerMock := keepertest.GetCrosschainObserverMock(t, k)
observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(&chains.Chain{
ChainId: 5,
})
observerMock.On("IsNonTombstonedObserver", mock.Anything, mock.Anything).Return(true)

fungibleMock := keepertest.GetCrosschainFungibleMock(t, k)
fungibleMock.On("SetGasPrice", mock.Anything, mock.Anything, mock.Anything).Return(uint64(1), nil)
msgServer := keeper.NewMsgServerImpl(*k)
creator := sample.AccAddress()
res, err := msgServer.GasPriceVoter(ctx, &types.MsgGasPriceVoter{
Creator: creator,
ChainId: 5,
Price: 1,
BlockNumber: 1,
})
require.NoError(t, err)
require.Empty(t, res)
gp, found := k.GetGasPrice(ctx, 5)
require.True(t, found)
require.Equal(t, types.GasPrice{
Creator: creator,
Index: "5",
ChainId: 5,
Signers: []string{creator},
BlockNums: []uint64{1},
Prices: []uint64{1},
MedianIndex: 0,
}, gp)
})

t.Run("should not error if gas price found and msg.creator in signers", func(t *testing.T) {
k, ctx, _, _ := keepertest.CrosschainKeeperWithMocks(t, keepertest.CrosschainMockOptions{
UseObserverMock: true,
UseFungibleMock: true,
})

observerMock := keepertest.GetCrosschainObserverMock(t, k)
observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(&chains.Chain{
ChainId: 5,
})
observerMock.On("IsNonTombstonedObserver", mock.Anything, mock.Anything).Return(true)

fungibleMock := keepertest.GetCrosschainFungibleMock(t, k)
fungibleMock.On("SetGasPrice", mock.Anything, mock.Anything, mock.Anything).Return(uint64(1), nil)
msgServer := keeper.NewMsgServerImpl(*k)

creator := sample.AccAddress()
k.SetGasPrice(ctx, types.GasPrice{
Creator: creator,
ChainId: 5,
Signers: []string{creator},
BlockNums: []uint64{1},
Prices: []uint64{1},
})

res, err := msgServer.GasPriceVoter(ctx, &types.MsgGasPriceVoter{
Creator: creator,
ChainId: 5,
BlockNumber: 2,
Price: 2,
})
require.NoError(t, err)
require.Empty(t, res)
gp, found := k.GetGasPrice(ctx, 5)
require.True(t, found)
require.Equal(t, types.GasPrice{
Creator: creator,
Index: "",
ChainId: 5,
Signers: []string{creator},
BlockNums: []uint64{2},
Prices: []uint64{2},
MedianIndex: 0,
}, gp)
})

t.Run("should not error if gas price found and msg.creator not in signers", func(t *testing.T) {
k, ctx, _, _ := keepertest.CrosschainKeeperWithMocks(t, keepertest.CrosschainMockOptions{
UseObserverMock: true,
UseFungibleMock: true,
})

observerMock := keepertest.GetCrosschainObserverMock(t, k)
observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(&chains.Chain{
ChainId: 5,
})
observerMock.On("IsNonTombstonedObserver", mock.Anything, mock.Anything).Return(true)

fungibleMock := keepertest.GetCrosschainFungibleMock(t, k)
fungibleMock.On("SetGasPrice", mock.Anything, mock.Anything, mock.Anything).Return(uint64(1), nil)
msgServer := keeper.NewMsgServerImpl(*k)

creator := sample.AccAddress()
k.SetGasPrice(ctx, types.GasPrice{
Creator: creator,
ChainId: 5,
BlockNums: []uint64{1},
Prices: []uint64{1},
})

res, err := msgServer.GasPriceVoter(ctx, &types.MsgGasPriceVoter{
Creator: creator,
ChainId: 5,
BlockNumber: 2,
Price: 2,
})
require.NoError(t, err)
require.Empty(t, res)
gp, found := k.GetGasPrice(ctx, 5)
require.True(t, found)
require.Equal(t, types.GasPrice{
Creator: creator,
Index: "",
ChainId: 5,
Signers: []string{creator},
BlockNums: []uint64{1, 2},
Prices: []uint64{1, 2},
MedianIndex: 1,
}, gp)
})
}
4 changes: 2 additions & 2 deletions x/crosschain/keeper/msg_server_vote_outbound_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func TestKeeper_VoteOnObservedOutboundTx(t *testing.T) {
keepertest.MockGetOutBound(observerMock, ctx)

// Successfully mock ProcessOutbound
keepertest.MockGetRevertGasLimitForERC20(fungibleMock, asset, *senderChain)
keepertest.MockGetRevertGasLimitForERC20(fungibleMock, asset, *senderChain, 100)
keepertest.MockPayGasAndUpdateCCTX(fungibleMock, observerMock, ctx, *k, *senderChain, asset)
_ = keepertest.MockUpdateNonce(observerMock, *senderChain)

Expand Down Expand Up @@ -257,7 +257,7 @@ func TestKeeper_VoteOnObservedOutboundTx(t *testing.T) {
keepertest.MockGetOutBound(observerMock, ctx)

// Mock Failed ProcessOutbound
keepertest.MockGetRevertGasLimitForERC20(fungibleMock, asset, *senderChain)
keepertest.MockGetRevertGasLimitForERC20(fungibleMock, asset, *senderChain, 100)
keepertest.MockPayGasAndUpdateCCTX(fungibleMock, observerMock, ctx, *k, *senderChain, asset)
observerMock.On("GetChainNonces", mock.Anything, senderChain.ChainName.String()).
Return(observertypes.ChainNonces{}, false)
Expand Down
52 changes: 44 additions & 8 deletions x/crosschain/keeper/process_inbound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,43 @@ func TestKeeper_ProcessZEVMDeposit(t *testing.T) {
keepertest.MockGetSupportedChainFromChainID(observerMock, senderChain)

// mock successful GetRevertGasLimit for ERC20
keepertest.MockGetRevertGasLimitForERC20(fungibleMock, asset, *senderChain)
keepertest.MockGetRevertGasLimitForERC20(fungibleMock, asset, *senderChain, 100)

// mock unsuccessful PayGasInERC20AndUpdateCctx
observerMock.On("GetSupportedChainFromChainID", mock.Anything, senderChain.ChainId).
Return(nil).Once()

// call ProcessZEVMDeposit
cctx := GetERC20Cctx(t, receiver, *senderChain, asset, amount)
k.ProcessZEVMDeposit(ctx, cctx)
require.Equal(t, types.CctxStatus_Aborted, cctx.CctxStatus.Status)
require.Equal(t, fmt.Sprintf("deposit revert message: %s err : %s", errDeposit, observertypes.ErrSupportedChains), cctx.CctxStatus.StatusMessage)
})

t.Run("uunable to process zevm deposit HandleEVMDeposit revert fails at PayGasInERC20AndUpdateCctx with gas limit is 0", func(t *testing.T) {
k, ctx, _, _ := keepertest.CrosschainKeeperWithMocks(t, keepertest.CrosschainMockOptions{
UseFungibleMock: true,
UseObserverMock: true,
})

// Setup mock data
fungibleMock := keepertest.GetCrosschainFungibleMock(t, k)
receiver := sample.EthAddress()
amount := big.NewInt(42)
senderChain := getValidEthChain(t)
asset := ""

// Setup expected calls
errDeposit := fmt.Errorf("deposit failed")
keepertest.MockRevertForHandleEVMDeposit(fungibleMock, receiver, amount, senderChain.ChainId, errDeposit)

observerMock := keepertest.GetCrosschainObserverMock(t, k)

// Mock successful GetSupportedChainFromChainID
keepertest.MockGetSupportedChainFromChainID(observerMock, senderChain)

// mock successful GetRevertGasLimit for ERC20
keepertest.MockGetRevertGasLimitForERC20(fungibleMock, asset, *senderChain, 0)

// mock unsuccessful PayGasInERC20AndUpdateCctx
observerMock.On("GetSupportedChainFromChainID", mock.Anything, senderChain.ChainId).
Expand Down Expand Up @@ -188,7 +224,7 @@ func TestKeeper_ProcessZEVMDeposit(t *testing.T) {
keepertest.MockGetSupportedChainFromChainID(observerMock, senderChain)

// mock successful GetRevertGasLimit for ERC20
keepertest.MockGetRevertGasLimitForERC20(fungibleMock, asset, *senderChain)
keepertest.MockGetRevertGasLimitForERC20(fungibleMock, asset, *senderChain, 100)

// mock successful PayGasAndUpdateCctx
keepertest.MockPayGasAndUpdateCCTX(fungibleMock, observerMock, ctx, *k, *senderChain, asset)
Expand Down Expand Up @@ -227,7 +263,7 @@ func TestKeeper_ProcessZEVMDeposit(t *testing.T) {
keepertest.MockGetSupportedChainFromChainID(observerMock, senderChain)

// mock successful GetRevertGasLimit for ERC20
keepertest.MockGetRevertGasLimitForERC20(fungibleMock, asset, *senderChain)
keepertest.MockGetRevertGasLimitForERC20(fungibleMock, asset, *senderChain, 100)

// mock successful PayGasAndUpdateCctx
keepertest.MockPayGasAndUpdateCCTX(fungibleMock, observerMock, ctx, *k, *senderChain, asset)
Expand Down Expand Up @@ -265,7 +301,7 @@ func TestKeeper_ProcessZEVMDeposit(t *testing.T) {
keepertest.MockGetSupportedChainFromChainID(observerMock, senderChain)

// mock successful GetRevertGasLimit for ERC20
keepertest.MockGetRevertGasLimitForERC20(fungibleMock, asset, *senderChain)
keepertest.MockGetRevertGasLimitForERC20(fungibleMock, asset, *senderChain, 100)

// call ProcessZEVMDeposit
cctx := GetERC20Cctx(t, receiver, *senderChain, asset, amount)
Expand Down Expand Up @@ -296,9 +332,9 @@ func TestKeeper_ProcessCrosschainMsgPassing(t *testing.T) {
// mock successful UpdateNonce
updatedNonce := keepertest.MockUpdateNonce(observerMock, *receiverChain)

// call ProcessCrosschainMsgPassing
// call ProcessInbound
cctx := GetERC20Cctx(t, receiver, *receiverChain, "", amount)
k.ProcessCrosschainMsgPassing(ctx, cctx)
k.ProcessInbound(ctx, cctx)
require.Equal(t, types.CctxStatus_PendingOutbound, cctx.CctxStatus.Status)
require.Equal(t, updatedNonce, cctx.GetCurrentOutTxParam().OutboundTxTssNonce)
})
Expand Down Expand Up @@ -346,9 +382,9 @@ func TestKeeper_ProcessCrosschainMsgPassing(t *testing.T) {
observerMock.On("GetChainNonces", mock.Anything, receiverChain.ChainName.String()).
Return(observertypes.ChainNonces{}, false)

// call ProcessCrosschainMsgPassing
// call ProcessInbound
cctx := GetERC20Cctx(t, receiver, *receiverChain, "", amount)
k.ProcessCrosschainMsgPassing(ctx, cctx)
k.ProcessInbound(ctx, cctx)
require.Equal(t, types.CctxStatus_Aborted, cctx.CctxStatus.Status)
require.Contains(t, cctx.CctxStatus.StatusMessage, "cannot find receiver chain nonce")
})
Expand Down
Loading

0 comments on commit 238789a

Please sign in to comment.