From 6ad94af0ccf6399dbf75ed82b1a52178fc30ec1b Mon Sep 17 00:00:00 2001 From: Charlie Chen Date: Fri, 19 Apr 2024 12:16:54 -0500 Subject: [PATCH] improved a few error handlling --- x/crosschain/keeper/grpc_query_cctx.go | 11 ++++------- x/crosschain/keeper/grpc_query_cctx_rate_limit.go | 12 ++++-------- .../keeper/grpc_query_cctx_rate_limit_test.go | 4 ++-- x/crosschain/keeper/grpc_query_cctx_test.go | 4 ++-- 4 files changed, 12 insertions(+), 19 deletions(-) diff --git a/x/crosschain/keeper/grpc_query_cctx.go b/x/crosschain/keeper/grpc_query_cctx.go index 5e916beeac..0cc659869c 100644 --- a/x/crosschain/keeper/grpc_query_cctx.go +++ b/x/crosschain/keeper/grpc_query_cctx.go @@ -8,6 +8,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/query" "github.com/zeta-chain/zetacore/x/crosschain/types" + observertypes "github.com/zeta-chain/zetacore/x/observer/types" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" ) @@ -93,21 +94,17 @@ func (k Keeper) CctxListPending(c context.Context, req *types.QueryListCctxPendi return nil, status.Error(codes.InvalidArgument, "invalid request") } - // check limit and use default MaxPendingCctxs if not specified - if req.Limit > MaxPendingCctxs { - return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("limit exceeds max limit of %d", MaxPendingCctxs)) - } + // use default MaxPendingCctxs if not specified or too high limit := req.Limit - if limit == 0 { + if limit == 0 || limit > MaxPendingCctxs { limit = MaxPendingCctxs } - ctx := sdk.UnwrapSDKContext(c) // query the nonces that are pending tss, found := k.zetaObserverKeeper.GetTSS(ctx) if !found { - return nil, status.Error(codes.Internal, "tss not found") + return nil, observertypes.ErrTssNotFound } pendingNonces, found := k.GetObserverKeeper().GetPendingNonces(ctx, tss.TssPubkey, req.ChainId) if !found { diff --git a/x/crosschain/keeper/grpc_query_cctx_rate_limit.go b/x/crosschain/keeper/grpc_query_cctx_rate_limit.go index 9f277cc556..78f73b9d78 100644 --- a/x/crosschain/keeper/grpc_query_cctx_rate_limit.go +++ b/x/crosschain/keeper/grpc_query_cctx_rate_limit.go @@ -2,7 +2,6 @@ package keeper import ( "context" - "fmt" "strings" sdk "github.com/cosmos/cosmos-sdk/types" @@ -21,24 +20,21 @@ func (k Keeper) CctxListPendingWithinRateLimit(c context.Context, req *types.Que return nil, status.Error(codes.InvalidArgument, "invalid request") } - // check limit and use default MaxPendingCctxs if not specified - if req.Limit > MaxPendingCctxs { - return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("limit exceeds max limit of %d", MaxPendingCctxs)) - } + // use default MaxPendingCctxs if not specified or too high limit := req.Limit - if limit == 0 { + if limit == 0 || limit > MaxPendingCctxs { limit = MaxPendingCctxs } + ctx := sdk.UnwrapSDKContext(c) // get current height and tss - ctx := sdk.UnwrapSDKContext(c) height := ctx.BlockHeight() if height <= 0 { return nil, status.Error(codes.OutOfRange, "height out of range") } tss, found := k.zetaObserverKeeper.GetTSS(ctx) if !found { - return nil, status.Error(codes.Internal, "tss not found") + return nil, observertypes.ErrTssNotFound } // check rate limit flags to decide if we should apply rate limit diff --git a/x/crosschain/keeper/grpc_query_cctx_rate_limit_test.go b/x/crosschain/keeper/grpc_query_cctx_rate_limit_test.go index 97f6664b9c..1f994872d8 100644 --- a/x/crosschain/keeper/grpc_query_cctx_rate_limit_test.go +++ b/x/crosschain/keeper/grpc_query_cctx_rate_limit_test.go @@ -16,10 +16,10 @@ func TestKeeper_CctxListPendingWithRateLimit(t *testing.T) { _, err := k.CctxListPendingWithinRateLimit(ctx, nil) require.ErrorContains(t, err, "invalid request") }) - t.Run("should fail if limit is too high", func(t *testing.T) { + t.Run("should use max limit if limit is too high", func(t *testing.T) { k, ctx, _, _ := keepertest.CrosschainKeeper(t) _, err := k.CctxListPendingWithinRateLimit(ctx, &types.QueryListCctxPendingWithRateLimitRequest{Limit: keeper.MaxPendingCctxs + 1}) - require.ErrorContains(t, err, "limit exceeds max limit of") + require.ErrorContains(t, err, "tss not found") }) t.Run("should fail if no TSS", func(t *testing.T) { k, ctx, _, _ := keepertest.CrosschainKeeper(t) diff --git a/x/crosschain/keeper/grpc_query_cctx_test.go b/x/crosschain/keeper/grpc_query_cctx_test.go index 59b0b68863..2387f9dc3a 100644 --- a/x/crosschain/keeper/grpc_query_cctx_test.go +++ b/x/crosschain/keeper/grpc_query_cctx_test.go @@ -70,10 +70,10 @@ func TestKeeper_CctxListPending(t *testing.T) { require.ErrorContains(t, err, "invalid request") }) - t.Run("should fail if limit is too high", func(t *testing.T) { + t.Run("should use max limit if limit is too high", func(t *testing.T) { k, ctx, _, _ := keepertest.CrosschainKeeper(t) _, err := k.CctxListPending(ctx, &types.QueryListCctxPendingRequest{Limit: keeper.MaxPendingCctxs + 1}) - require.ErrorContains(t, err, "limit exceeds max limit of") + require.ErrorContains(t, err, "tss not found") }) t.Run("should fail if no TSS", func(t *testing.T) {