Skip to content

Commit

Permalink
testutil/keeper/crosschain.go
Browse files Browse the repository at this point in the history
  • Loading branch information
lumtis committed Apr 17, 2024
1 parent 447dd0c commit a5f9653
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 42 deletions.
28 changes: 28 additions & 0 deletions testutil/keeper/crosschain.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,31 @@ func MockSaveOutBoundNewRevertCreated(m *crosschainmocks.CrosschainObserverKeepe
m.On("GetTSS", ctx).Return(observertypes.TSS{}, true)
m.On("SetNonceToCctx", mock.Anything, mock.Anything).Return().Once()
}

// MockCctxByNonce is a utility function using observer mock to returns a cctx of the given status from crosschain keeper
// mocks the methods called by CctxByNonce to directly return the given cctx or error
func MockCctxByNonce(
t *testing.T,
ctx sdk.Context,
k keeper.Keeper,
observerKeeper *crosschainmocks.CrosschainObserverKeeper,
cctxStatus types.CctxStatus,
isErr bool,
) {
if isErr {
// return error on GetTSS to make CctxByNonce return error
observerKeeper.On("GetTSS", mock.Anything).Return(observertypes.TSS{}, false).Once()
return
}

cctx := sample.CrossChainTx(t, sample.StringRandom(sample.Rand(), 10))
cctx.CctxStatus = &types.Status{
Status: cctxStatus,
}
k.SetCrossChainTx(ctx, *cctx)

observerKeeper.On("GetTSS", mock.Anything).Return(observertypes.TSS{}, true).Once()
observerKeeper.On("GetNonceToCctx", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(observertypes.NonceToCctx{
CctxIndex: cctx.Index,
}, true).Once()
}
54 changes: 12 additions & 42 deletions x/crosschain/keeper/msg_server_add_to_outtx_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import (
"errors"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/zetacore/pkg/chains"
"github.com/zeta-chain/zetacore/pkg/proofs"
keepertest "github.com/zeta-chain/zetacore/testutil/keeper"
mocks "github.com/zeta-chain/zetacore/testutil/keeper/mocks/crosschain"
"github.com/zeta-chain/zetacore/testutil/sample"
authoritytypes "github.com/zeta-chain/zetacore/x/authority/types"
"github.com/zeta-chain/zetacore/x/crosschain/keeper"
Expand All @@ -26,34 +24,6 @@ func getEthereumChainID() int64 {
// https://github.com/zeta-chain/node/issues/1994

func TestMsgServer_AddToOutTxTracker(t *testing.T) {

// mockCctxByNonce mocks the methods called by CctxByNonce to directly return the given cctx or error
mockCctxByNonce := func(
t *testing.T,
ctx sdk.Context,
k keeper.Keeper,
observerKeeper *mocks.CrosschainObserverKeeper,
cctxStatus types.CctxStatus,
isErr bool,
) {
if isErr {
// return error on GetTSS to make CctxByNonce return error
observerKeeper.On("GetTSS", mock.Anything).Return(observertypes.TSS{}, false).Once()
return
}

cctx := sample.CrossChainTx(t, sample.StringRandom(sample.Rand(), 10))
cctx.CctxStatus = &types.Status{
Status: cctxStatus,
}
k.SetCrossChainTx(ctx, *cctx)

observerKeeper.On("GetTSS", mock.Anything).Return(observertypes.TSS{}, true).Once()
observerKeeper.On("GetNonceToCctx", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(observertypes.NonceToCctx{
CctxIndex: cctx.Index,
}, true).Once()
}

t.Run("admin can add tracker", func(t *testing.T) {
k, ctx, _, _ := keepertest.CrosschainKeeperWithMocks(t, keepertest.CrosschainMockOptions{
UseAuthorityMock: true,
Expand All @@ -69,7 +39,7 @@ func TestMsgServer_AddToOutTxTracker(t *testing.T) {
keepertest.MockIsAuthorized(&authorityMock.Mock, admin, authoritytypes.PolicyType_groupEmergency, true)
observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(&chains.Chain{})
observerMock.On("IsNonTombstonedObserver", mock.Anything, mock.Anything).Return(false)
mockCctxByNonce(t, ctx, *k, observerMock, types.CctxStatus_PendingOutbound, false)
keepertest.MockCctxByNonce(t, ctx, *k, observerMock, types.CctxStatus_PendingOutbound, false)

chainID := getEthereumChainID()
hash := sample.Hash().Hex()
Expand Down Expand Up @@ -104,7 +74,7 @@ func TestMsgServer_AddToOutTxTracker(t *testing.T) {
keepertest.MockIsAuthorized(&authorityMock.Mock, admin, authoritytypes.PolicyType_groupEmergency, false)
observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(&chains.Chain{})
observerMock.On("IsNonTombstonedObserver", mock.Anything, mock.Anything).Return(true)
mockCctxByNonce(t, ctx, *k, observerMock, types.CctxStatus_PendingOutbound, false)
keepertest.MockCctxByNonce(t, ctx, *k, observerMock, types.CctxStatus_PendingOutbound, false)

chainID := getEthereumChainID()
hash := sample.Hash().Hex()
Expand Down Expand Up @@ -139,7 +109,7 @@ func TestMsgServer_AddToOutTxTracker(t *testing.T) {
keepertest.MockIsAuthorized(&authorityMock.Mock, admin, authoritytypes.PolicyType_groupEmergency, true)
observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(&chains.Chain{})
observerMock.On("IsNonTombstonedObserver", mock.Anything, mock.Anything).Return(false)
mockCctxByNonce(t, ctx, *k, observerMock, types.CctxStatus_PendingOutbound, false)
keepertest.MockCctxByNonce(t, ctx, *k, observerMock, types.CctxStatus_PendingOutbound, false)

chainID := getEthereumChainID()
existinghHash := sample.Hash().Hex()
Expand Down Expand Up @@ -185,7 +155,7 @@ func TestMsgServer_AddToOutTxTracker(t *testing.T) {
observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(&chains.Chain{})

// set cctx status to outbound mined
mockCctxByNonce(t, ctx, *k, observerMock, types.CctxStatus_OutboundMined, false)
keepertest.MockCctxByNonce(t, ctx, *k, observerMock, types.CctxStatus_OutboundMined, false)

chainID := getEthereumChainID()

Expand Down Expand Up @@ -244,7 +214,7 @@ func TestMsgServer_AddToOutTxTracker(t *testing.T) {
observerMock := keepertest.GetCrosschainObserverMock(t, k)

observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(&chains.Chain{})
mockCctxByNonce(t, ctx, *k, observerMock, types.CctxStatus_PendingOutbound, true)
keepertest.MockCctxByNonce(t, ctx, *k, observerMock, types.CctxStatus_PendingOutbound, true)

chainID := getEthereumChainID()

Expand Down Expand Up @@ -275,7 +245,7 @@ func TestMsgServer_AddToOutTxTracker(t *testing.T) {
keepertest.MockIsAuthorized(&authorityMock.Mock, admin, authoritytypes.PolicyType_groupEmergency, true)
observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(&chains.Chain{})
observerMock.On("IsNonTombstonedObserver", mock.Anything, mock.Anything).Return(false)
mockCctxByNonce(t, ctx, *k, observerMock, types.CctxStatus_PendingOutbound, false)
keepertest.MockCctxByNonce(t, ctx, *k, observerMock, types.CctxStatus_PendingOutbound, false)

hashes := make([]*types.TxHashList, keeper.MaxOutTxTrackerHashes)
for i := 0; i < keeper.MaxOutTxTrackerHashes; i++ {
Expand Down Expand Up @@ -320,7 +290,7 @@ func TestMsgServer_AddToOutTxTracker(t *testing.T) {
keepertest.MockIsAuthorized(&authorityMock.Mock, admin, authoritytypes.PolicyType_groupEmergency, true)
observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(&chains.Chain{})
observerMock.On("IsNonTombstonedObserver", mock.Anything, mock.Anything).Return(false)
mockCctxByNonce(t, ctx, *k, observerMock, types.CctxStatus_PendingOutbound, false)
keepertest.MockCctxByNonce(t, ctx, *k, observerMock, types.CctxStatus_PendingOutbound, false)

chainID := getEthereumChainID()
existinghHash := sample.Hash().Hex()
Expand Down Expand Up @@ -371,7 +341,7 @@ func TestMsgServer_AddToOutTxTracker(t *testing.T) {
keepertest.MockIsAuthorized(&authorityMock.Mock, admin, authoritytypes.PolicyType_groupEmergency, false)
observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(&chains.Chain{})
observerMock.On("IsNonTombstonedObserver", mock.Anything, mock.Anything).Return(false)
mockCctxByNonce(t, ctx, *k, observerMock, types.CctxStatus_PendingOutbound, false)
keepertest.MockCctxByNonce(t, ctx, *k, observerMock, types.CctxStatus_PendingOutbound, false)
observerMock.On("GetTssAddress", mock.Anything, mock.Anything).Return(&observertypes.QueryGetTssAddressResponse{
Eth: tssAddress.Hex(),
}, nil)
Expand Down Expand Up @@ -413,7 +383,7 @@ func TestMsgServer_AddToOutTxTracker(t *testing.T) {
keepertest.MockIsAuthorized(&authorityMock.Mock, admin, authoritytypes.PolicyType_groupEmergency, false)
observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(&chains.Chain{})
observerMock.On("IsNonTombstonedObserver", mock.Anything, mock.Anything).Return(false)
mockCctxByNonce(t, ctx, *k, observerMock, types.CctxStatus_PendingOutbound, false)
keepertest.MockCctxByNonce(t, ctx, *k, observerMock, types.CctxStatus_PendingOutbound, false)
observerMock.On("GetTssAddress", mock.Anything, mock.Anything).Return(&observertypes.QueryGetTssAddressResponse{
Eth: tssAddress.Hex(),
}, nil)
Expand Down Expand Up @@ -471,7 +441,7 @@ func TestMsgServer_AddToOutTxTracker(t *testing.T) {
keepertest.MockIsAuthorized(&authorityMock.Mock, admin, authoritytypes.PolicyType_groupEmergency, false)
observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(&chains.Chain{})
observerMock.On("IsNonTombstonedObserver", mock.Anything, mock.Anything).Return(false)
mockCctxByNonce(t, ctx, *k, observerMock, types.CctxStatus_PendingOutbound, false)
keepertest.MockCctxByNonce(t, ctx, *k, observerMock, types.CctxStatus_PendingOutbound, false)
lightclientMock.On("VerifyProof", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(ethTxBytes, errors.New("error"))

_, err := msgServer.AddToOutTxTracker(ctx, &types.MsgAddToOutTxTracker{
Expand Down Expand Up @@ -506,7 +476,7 @@ func TestMsgServer_AddToOutTxTracker(t *testing.T) {
keepertest.MockIsAuthorized(&authorityMock.Mock, admin, authoritytypes.PolicyType_groupEmergency, false)
observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(&chains.Chain{})
observerMock.On("IsNonTombstonedObserver", mock.Anything, mock.Anything).Return(false)
mockCctxByNonce(t, ctx, *k, observerMock, types.CctxStatus_PendingOutbound, false)
keepertest.MockCctxByNonce(t, ctx, *k, observerMock, types.CctxStatus_PendingOutbound, false)
lightclientMock.On("VerifyProof", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(ethTxBytes, nil)
observerMock.On("GetTssAddress", mock.Anything, mock.Anything).Return(&observertypes.QueryGetTssAddressResponse{
Eth: tssAddress.Hex(),
Expand Down Expand Up @@ -544,7 +514,7 @@ func TestMsgServer_AddToOutTxTracker(t *testing.T) {
keepertest.MockIsAuthorized(&authorityMock.Mock, admin, authoritytypes.PolicyType_groupEmergency, false)
observerMock.On("GetSupportedChainFromChainID", mock.Anything, mock.Anything).Return(&chains.Chain{})
observerMock.On("IsNonTombstonedObserver", mock.Anything, mock.Anything).Return(false)
mockCctxByNonce(t, ctx, *k, observerMock, types.CctxStatus_PendingOutbound, false)
keepertest.MockCctxByNonce(t, ctx, *k, observerMock, types.CctxStatus_PendingOutbound, false)
observerMock.On("GetTssAddress", mock.Anything, mock.Anything).Return(&observertypes.QueryGetTssAddressResponse{
Eth: tssAddress.Hex(),
}, nil)
Expand Down

0 comments on commit a5f9653

Please sign in to comment.