From 9a7cd75f84a3ea444cf9a77559619cd12e33569f Mon Sep 17 00:00:00 2001 From: Tanmay Date: Wed, 27 Nov 2024 18:57:31 -0500 Subject: [PATCH] add check for pending status --- .../keeper/msg_server_abort_stuck_cctx.go | 5 +-- x/crosschain/types/status.go | 8 ++++ x/crosschain/types/status_test.go | 42 +++++++++++++++++++ 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/x/crosschain/keeper/msg_server_abort_stuck_cctx.go b/x/crosschain/keeper/msg_server_abort_stuck_cctx.go index f61cf30f71..6dbb5ac4dd 100644 --- a/x/crosschain/keeper/msg_server_abort_stuck_cctx.go +++ b/x/crosschain/keeper/msg_server_abort_stuck_cctx.go @@ -36,10 +36,7 @@ func (k msgServer) AbortStuckCCTX( } // check if the cctx is pending - isPending := cctx.CctxStatus.Status == types.CctxStatus_PendingOutbound || - cctx.CctxStatus.Status == types.CctxStatus_PendingInbound || - cctx.CctxStatus.Status == types.CctxStatus_PendingRevert - if !isPending { + if !cctx.CctxStatus.Status.IsPendingStatus() { return nil, types.ErrStatusNotPending } diff --git a/x/crosschain/types/status.go b/x/crosschain/types/status.go index 00c08bf6f7..d5eb303ab9 100644 --- a/x/crosschain/types/status.go +++ b/x/crosschain/types/status.go @@ -86,3 +86,11 @@ func stateTransitionMap() map[CctxStatus][]CctxStatus { } return stateTransitionMap } + +func (c CctxStatus) IsTerminalStatus() bool { + return c == CctxStatus_Aborted || c == CctxStatus_Reverted || c == CctxStatus_OutboundMined +} + +func (c CctxStatus) IsPendingStatus() bool { + return c == CctxStatus_PendingInbound || c == CctxStatus_PendingOutbound || c == CctxStatus_PendingRevert +} diff --git a/x/crosschain/types/status_test.go b/x/crosschain/types/status_test.go index 5bb272d522..526a4e74a4 100644 --- a/x/crosschain/types/status_test.go +++ b/x/crosschain/types/status_test.go @@ -174,3 +174,45 @@ func TestStatus_ChangeStatus(t *testing.T) { ) }) } + +func TestCctxStatus_IsTerminalStatus(t *testing.T) { + tests := []struct { + name string + status types.CctxStatus + expected bool + }{ + {"PendingInbound", types.CctxStatus_PendingInbound, false}, + {"PendingOutbound", types.CctxStatus_PendingOutbound, false}, + {"OutboundMined", types.CctxStatus_OutboundMined, true}, + {"Reverted", types.CctxStatus_Reverted, true}, + {"Aborted", types.CctxStatus_Aborted, true}, + {"PendingRevert", types.CctxStatus_PendingRevert, false}, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + assert.Equal(t, tc.expected, tc.status.IsTerminalStatus()) + }) + } +} + +func TestCctxStatus_IsPendingStatus(t *testing.T) { + tests := []struct { + name string + status types.CctxStatus + expected bool + }{ + {"PendingInbound", types.CctxStatus_PendingInbound, true}, + {"PendingOutbound", types.CctxStatus_PendingOutbound, true}, + {"OutboundMined", types.CctxStatus_OutboundMined, false}, + {"Reverted", types.CctxStatus_Reverted, false}, + {"Aborted", types.CctxStatus_Aborted, false}, + {"PendingRevert", types.CctxStatus_PendingRevert, true}, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + assert.Equal(t, tc.expected, tc.status.IsPendingStatus()) + }) + } +}