From 247421392d0785c853ce0ce6ae6fa55281161b84 Mon Sep 17 00:00:00 2001 From: Alex Gartner Date: Thu, 12 Sep 2024 10:15:43 -0700 Subject: [PATCH] add unit tests --- x/crosschain/keeper/grpc_query_cctx.go | 7 ++-- x/crosschain/keeper/grpc_query_cctx_test.go | 38 +++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/x/crosschain/keeper/grpc_query_cctx.go b/x/crosschain/keeper/grpc_query_cctx.go index fed1d3323d..636348ea43 100644 --- a/x/crosschain/keeper/grpc_query_cctx.go +++ b/x/crosschain/keeper/grpc_query_cctx.go @@ -21,7 +21,7 @@ const ( // MaxLookbackNonce is the maximum number of nonces to look back to find missed pending cctxs MaxLookbackNonce = 1000 - defaultPageSize = 100 + DefaultPageSize = 100 ) func (k Keeper) ZetaAccounting( @@ -48,8 +48,11 @@ func (k Keeper) CctxAll(c context.Context, req *types.QueryAllCctxRequest) (*typ store := ctx.KVStore(k.storeKey) sendStore := prefix.NewStore(store, types.KeyPrefix(types.CCTXKey)) + if req.Pagination == nil { + req.Pagination = &query.PageRequest{} + } if req.Pagination.Limit == 0 { - req.Pagination.Limit = defaultPageSize + req.Pagination.Limit = DefaultPageSize } pageRes, err := query.Paginate(sendStore, req.Pagination, func(_ []byte, value []byte) error { diff --git a/x/crosschain/keeper/grpc_query_cctx_test.go b/x/crosschain/keeper/grpc_query_cctx_test.go index 54bffea45d..8a91e9445b 100644 --- a/x/crosschain/keeper/grpc_query_cctx_test.go +++ b/x/crosschain/keeper/grpc_query_cctx_test.go @@ -5,6 +5,7 @@ import ( "testing" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/query" "github.com/stretchr/testify/require" keepertest "github.com/zeta-chain/node/testutil/keeper" @@ -288,3 +289,40 @@ func TestKeeper_CctxByNonce(t *testing.T) { require.Equal(t, res.CrossChainTx.CctxStatus.LastUpdateTimestamp, ctx.BlockTime().Unix()) }) } + +func TestKeeper_CctxAll(t *testing.T) { + t.Run("empty request", func(t *testing.T) { + k, ctx, _, _ := keepertest.CrosschainKeeper(t) + _, err := k.CctxAll(ctx, &types.QueryAllCctxRequest{}) + require.NoError(t, err) + }) + + t.Run("default page size", func(t *testing.T) { + k, ctx, _, zk := keepertest.CrosschainKeeper(t) + chainID := getValidEthChainID() + tss := sample.Tss() + zk.ObserverKeeper.SetTSS(ctx, tss) + _ = createCctxWithNonceRange(t, ctx, *k, 1000, 2000, chainID, tss, zk) + + res, err := k.CctxAll(ctx, &types.QueryAllCctxRequest{}) + require.NoError(t, err) + require.Len(t, res.CrossChainTx, keeper.DefaultPageSize) + }) + + t.Run("page size provided", func(t *testing.T) { + k, ctx, _, zk := keepertest.CrosschainKeeper(t) + chainID := getValidEthChainID() + tss := sample.Tss() + zk.ObserverKeeper.SetTSS(ctx, tss) + _ = createCctxWithNonceRange(t, ctx, *k, 1000, 2000, chainID, tss, zk) + testPageSize := 200 + + res, err := k.CctxAll(ctx, &types.QueryAllCctxRequest{ + Pagination: &query.PageRequest{ + Limit: uint64(testPageSize), + }, + }) + require.NoError(t, err) + require.Len(t, res.CrossChainTx, testPageSize) + }) +}