Skip to content

Commit

Permalink
fix unit test of AddToOutTxTracker
Browse files Browse the repository at this point in the history
  • Loading branch information
brewmaster012 committed Nov 5, 2023
1 parent f181b42 commit 0cfb540
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
24 changes: 12 additions & 12 deletions x/crosschain/keeper/msg_server_add_to_outtx_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,6 @@ func (k msgServer) AddToOutTxTracker(goCtx context.Context, msg *types.MsgAddToO
return nil, observertypes.ErrSupportedChains
}

cctx, err := k.CctxByNonce(ctx, &types.QueryGetCctxByNonceRequest{
ChainID: msg.ChainId,
Nonce: msg.Nonce,
})
if err != nil || cctx == nil || cctx.CrossChainTx == nil {
return nil, cosmoserrors.Wrap(types.ErrCannotFindCctx, "cannot add out tx: no corresponding cctx found")
}
if !IsPending(*cctx.CrossChainTx) {
k.RemoveOutTxTracker(ctx, msg.ChainId, msg.Nonce)
return &types.MsgAddToOutTxTrackerResponse{}, nil
}

if msg.Proof == nil { // without proof, only certain accounts can send this message
adminPolicyAccount := k.zetaObserverKeeper.GetParams(ctx).GetAdminPolicyAccount(observertypes.Policy_Type_group1)
isAdmin := msg.Creator == adminPolicyAccount
Expand All @@ -64,6 +52,18 @@ func (k msgServer) AddToOutTxTracker(goCtx context.Context, msg *types.MsgAddToO
isProven = true
}

cctx, err := k.CctxByNonce(ctx, &types.QueryGetCctxByNonceRequest{
ChainID: msg.ChainId,
Nonce: msg.Nonce,
})
if err != nil || cctx == nil || cctx.CrossChainTx == nil {
return nil, cosmoserrors.Wrap(types.ErrCannotFindCctx, "cannot add out tx: no corresponding cctx found")
}
if !IsPending(*cctx.CrossChainTx) {
k.RemoveOutTxTracker(ctx, msg.ChainId, msg.Nonce)
return &types.MsgAddToOutTxTrackerResponse{}, nil
}

tracker, found := k.GetOutTxTracker(ctx, msg.ChainId, msg.Nonce)
hash := types.TxHashList{
TxHash: msg.TxHash,
Expand Down
28 changes: 21 additions & 7 deletions x/crosschain/keeper/msg_server_add_to_outtx_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestMsgServer_AddToOutTxTracker(t *testing.T) {
txIndex, block, header, headerRLP, _, tx, err := sample.Proof()
require.NoError(t, err)
setupVerificationParams(zk, ctx, txIndex, chainID, header, headerRLP, block)
setupTss(k, ctx)
setupTssAndNonceToCctx(k, ctx, chainID, 0)
msgServer := keeper.NewMsgServerImpl(*k)
_, err = msgServer.AddToOutTxTracker(ctx, &types.MsgAddToOutTxTracker{
Creator: admin,
Expand All @@ -47,7 +47,7 @@ func TestMsgServer_AddToOutTxTracker(t *testing.T) {
txIndex, block, header, headerRLP, proof, tx, err := sample.Proof()
require.NoError(t, err)
setupVerificationParams(zk, ctx, txIndex, chainID, header, headerRLP, block)
setupTss(k, ctx)
setupTssAndNonceToCctx(k, ctx, chainID, int64(tx.Nonce()))
msgServer := keeper.NewMsgServerImpl(*k)
_, err = msgServer.AddToOutTxTracker(ctx, &types.MsgAddToOutTxTracker{
Creator: sample.AccAddress(),
Expand All @@ -69,7 +69,7 @@ func TestMsgServer_AddToOutTxTracker(t *testing.T) {
txIndex, block, header, headerRLP, proof, tx, err := sample.Proof()
require.NoError(t, err)
setupVerificationParams(zk, ctx, txIndex, chainID, header, headerRLP, block)
setupTss(k, ctx)
setupTssAndNonceToCctx(k, ctx, chainID, 1)
msgServer := keeper.NewMsgServerImpl(*k)
_, err = msgServer.AddToOutTxTracker(ctx, &types.MsgAddToOutTxTracker{
Creator: sample.AccAddress(),
Expand All @@ -91,7 +91,7 @@ func TestMsgServer_AddToOutTxTracker(t *testing.T) {
txIndex, block, header, headerRLP, proof, tx, err := sample.Proof()
require.NoError(t, err)
setupVerificationParams(zk, ctx, txIndex, chainID, header, headerRLP, block)
setupTss(k, ctx)
setupTssAndNonceToCctx(k, ctx, chainID, int64(tx.Nonce()))
msgServer := keeper.NewMsgServerImpl(*k)
_, err = msgServer.AddToOutTxTracker(ctx, &types.MsgAddToOutTxTracker{
Creator: sample.AccAddress(),
Expand All @@ -113,7 +113,7 @@ func TestMsgServer_AddToOutTxTracker(t *testing.T) {
txIndex, block, header, headerRLP, _, tx, err := sample.Proof()
require.NoError(t, err)
setupVerificationParams(zk, ctx, txIndex, chainID, header, headerRLP, block)
setupTss(k, ctx)
setupTssAndNonceToCctx(k, ctx, chainID, int64(tx.Nonce()))
msgServer := keeper.NewMsgServerImpl(*k)
_, err = msgServer.AddToOutTxTracker(ctx, &types.MsgAddToOutTxTracker{
Creator: sample.AccAddress(),
Expand All @@ -134,7 +134,7 @@ func TestMsgServer_AddToOutTxTracker(t *testing.T) {
txIndex, block, header, headerRLP, proof, tx, err := sample.Proof()
require.NoError(t, err)
setupVerificationParams(zk, ctx, txIndex, chainID, header, headerRLP, block)
setupTss(k, ctx)
setupTssAndNonceToCctx(k, ctx, chainID, int64(tx.Nonce()))
msgServer := keeper.NewMsgServerImpl(*k)
_, err = msgServer.AddToOutTxTracker(ctx, &types.MsgAddToOutTxTracker{
Creator: sample.AccAddress(),
Expand All @@ -151,8 +151,22 @@ func TestMsgServer_AddToOutTxTracker(t *testing.T) {
})
}

func setupTss(k *keeper.Keeper, ctx sdk.Context) {
func setupTssAndNonceToCctx(k *keeper.Keeper, ctx sdk.Context, chainId, nonce int64) {
k.SetTSS(ctx, types.TSS{
TssPubkey: "zetapub1addwnpepq28c57cvcs0a2htsem5zxr6qnlvq9mzhmm76z3jncsnzz32rclangr2g35p",
})
cctx := types.CrossChainTx{
Creator: "any",
Index: "0x123",
CctxStatus: &types.Status{
Status: types.CctxStatus_PendingOutbound,
},
}
k.SetCrossChainTx(ctx, cctx)
k.SetNonceToCctx(ctx, types.NonceToCctx{
ChainId: chainId,
Nonce: nonce,
CctxIndex: "0x123",
Tss: "zetapub1addwnpepq28c57cvcs0a2htsem5zxr6qnlvq9mzhmm76z3jncsnzz32rclangr2g35p",
})
}

0 comments on commit 0cfb540

Please sign in to comment.