Skip to content

Commit

Permalink
add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Mar 7, 2024
1 parent 822d4d4 commit b228a28
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 1 deletion.
2 changes: 1 addition & 1 deletion x/crosschain/keeper/cctx_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func CreateNewCCTX(
OutboundTxGasPrice: "",
OutboundTxBallotIndex: "",
OutboundTxObservedExternalHeight: 0,
Amount: sdk.NewUint(0),
Amount: sdkmath.ZeroUint(),
TssPubkey: tssPubkey,
}
status := &types.Status{
Expand Down
110 changes: 110 additions & 0 deletions x/crosschain/keeper/cctx_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,116 @@ func TestKeeper_ProcessCrosschainMsgPassing(t *testing.T) {
})
}

func TestKeeper_GetInbound(t *testing.T) {
t.Run("should return a cctx with correct values", func(t *testing.T) {
k, ctx, _, zk := keepertest.CrosschainKeeper(t)
senderChain := getValidEthChain(t)
sender := sample.EthAddress()
receiverChain := getValidEthChain(t)
receiver := sample.EthAddress()
creator := sample.AccAddress()
amount := sdkmath.NewUint(42)
message := "test"
intxBlockHeight := uint64(420)
intxHash := sample.Hash()
gasLimit := uint64(100)
asset := "test-asset"
eventIndex := uint64(1)
cointType := common.CoinType_ERC20
tss := sample.Tss()
msg := types.MsgVoteOnObservedInboundTx{
Creator: creator,
Sender: sender.String(),
SenderChainId: senderChain.ChainId,
Receiver: receiver.String(),
ReceiverChain: receiverChain.ChainId,
Amount: amount,
Message: message,
InTxHash: intxHash.String(),
InBlockHeight: intxBlockHeight,
GasLimit: gasLimit,
CoinType: cointType,
TxOrigin: sender.String(),
Asset: asset,
EventIndex: eventIndex,
}
zk.ObserverKeeper.SetTSS(ctx, tss)
cctx := k.GetInbound(ctx, &msg)
require.Equal(t, receiver.String(), cctx.GetCurrentOutTxParam().Receiver)
require.Equal(t, receiverChain.ChainId, cctx.GetCurrentOutTxParam().ReceiverChainId)
require.Equal(t, sender.String(), cctx.GetInboundTxParams().Sender)
require.Equal(t, senderChain.ChainId, cctx.GetInboundTxParams().SenderChainId)
require.Equal(t, amount, cctx.GetInboundTxParams().Amount)
require.Equal(t, message, cctx.RelayedMessage)
require.Equal(t, intxHash.String(), cctx.GetInboundTxParams().InboundTxObservedHash)
require.Equal(t, intxBlockHeight, cctx.GetInboundTxParams().InboundTxObservedExternalHeight)
require.Equal(t, gasLimit, cctx.GetCurrentOutTxParam().OutboundTxGasLimit)
require.Equal(t, asset, cctx.GetInboundTxParams().Asset)
require.Equal(t, eventIndex, cctx.EventIndex)
require.Equal(t, cointType, cctx.CoinType)
require.Equal(t, uint64(0), cctx.GetCurrentOutTxParam().OutboundTxTssNonce)
require.Equal(t, sdkmath.ZeroUint(), cctx.GetCurrentOutTxParam().Amount)
require.Equal(t, types.CctxStatus_PendingInbound, cctx.CctxStatus.Status)
require.Equal(t, false, cctx.CctxStatus.IsAbortRefunded)
})
}

func Test_IsPending(t *testing.T) {
tt := []struct {
status types.CctxStatus
expected bool
}{
{types.CctxStatus_PendingInbound, false},
{types.CctxStatus_PendingOutbound, true},
{types.CctxStatus_PendingRevert, true},
{types.CctxStatus_Reverted, false},
{types.CctxStatus_Aborted, false},
{types.CctxStatus_OutboundMined, false},
}
for _, tc := range tt {
t.Run(fmt.Sprintf("status %s", tc.status), func(t *testing.T) {
require.Equal(t, tc.expected, crosschainkeeper.IsPending(types.CrossChainTx{CctxStatus: &types.Status{Status: tc.status}}))
})
}
}

func TestKeeper_SaveInbound(t *testing.T) {
t.Run("should save the cctx", func(t *testing.T) {
k, ctx, _, _ := keepertest.CrosschainKeeper(t)
receiver := sample.EthAddress()
amount := big.NewInt(42)
senderChain := getValidEthChain(t)
cctx := GetERC20Cctx(t, receiver, *senderChain, "", amount)
k.SaveInbound(ctx, cctx)
require.Equal(t, types.TxFinalizationStatus_Executed, cctx.InboundTxParams.TxFinalizationStatus)
require.True(t, k.IsFinalizedInbound(ctx, cctx.GetInboundTxParams().InboundTxObservedHash, cctx.GetInboundTxParams().SenderChainId, cctx.EventIndex))
_, found := k.GetCrossChainTx(ctx, cctx.Index)
require.True(t, found)
})

t.Run("should save the cctx and remove tracker", func(t *testing.T) {
k, ctx, _, _ := keepertest.CrosschainKeeper(t)
receiver := sample.EthAddress()
amount := big.NewInt(42)
senderChain := getValidEthChain(t)
cctx := GetERC20Cctx(t, receiver, *senderChain, "", amount)
hash := sample.Hash()
cctx.InboundTxParams.InboundTxObservedHash = hash.String()
k.SetInTxTracker(ctx, types.InTxTracker{
ChainId: senderChain.ChainId,
TxHash: hash.String(),
CoinType: 0,
})
k.SaveInbound(ctx, cctx)
require.Equal(t, types.TxFinalizationStatus_Executed, cctx.InboundTxParams.TxFinalizationStatus)
require.True(t, k.IsFinalizedInbound(ctx, cctx.GetInboundTxParams().InboundTxObservedHash, cctx.GetInboundTxParams().SenderChainId, cctx.EventIndex))
_, found := k.GetCrossChainTx(ctx, cctx.Index)
require.True(t, found)
_, found = k.GetInTxTracker(ctx, senderChain.ChainId, hash.String())
require.False(t, found)
})
}

func GetERC20Cctx(t *testing.T, receiver ethcommon.Address, senderChain common.Chain, asset string, amount *big.Int) *types.CrossChainTx {
cctx := sample.CrossChainTx(t, "test")
cctx.CctxStatus = &types.Status{Status: types.CctxStatus_PendingInbound}
Expand Down

0 comments on commit b228a28

Please sign in to comment.