Skip to content

Commit ff9500e

Browse files
committed
fix(x/market): improve pagination key handling per CodeRabbit review
1 parent 426eb82 commit ff9500e

File tree

1 file changed

+13
-22
lines changed

1 file changed

+13
-22
lines changed

x/market/keeper/grpc_query.go

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package keeper
33
import (
44
"context"
55
"encoding/base64"
6+
"errors"
67

78
"google.golang.org/grpc/codes"
89
"google.golang.org/grpc/status"
@@ -50,34 +51,36 @@ func (k Querier) Orders(c context.Context, req *types.QueryOrdersRequest) (*type
5051

5152
// setup for case 3 - cross-index search
5253
hasPaginationKey := len(req.Pagination.Key) > 0
53-
if hasPaginationKey { // nolint: gocritic
54+
switch {
55+
case hasPaginationKey:
5456
var key []byte
5557
var err error
5658

57-
// Handle Base64 encoded pagination keys
59+
// Accept both raw and base64-encoded keys: try raw first, then base64.
5860
paginationKeyBytes := req.Pagination.Key
59-
if isBase64String(req.Pagination.Key) {
60-
paginationKeyBytes, err = base64.StdEncoding.DecodeString(string(req.Pagination.Key))
61-
if err != nil {
62-
return nil, status.Error(codes.InvalidArgument, "invalid base64 pagination key")
61+
states, searchPrefix, key, _, err = query.DecodePaginationKey(paginationKeyBytes)
62+
if err != nil {
63+
if decoded, decErr := base64.StdEncoding.DecodeString(string(req.Pagination.Key)); decErr == nil {
64+
states, searchPrefix, key, _, err = query.DecodePaginationKey(decoded)
6365
}
6466
}
65-
66-
states, searchPrefix, key, _, err = query.DecodePaginationKey(paginationKeyBytes)
6767
if err != nil {
68+
if errors.Is(err, query.ErrInvalidPaginationKey) {
69+
return nil, status.Error(codes.InvalidArgument, err.Error())
70+
}
6871
return nil, status.Error(codes.Internal, err.Error())
6972
}
7073

7174
req.Pagination.Key = key
72-
} else if req.Filters.State != "" {
75+
case req.Filters.State != "":
7376
stateVal := types.Order_State(types.Order_State_value[req.Filters.State])
7477

7578
if req.Filters.State != "" && stateVal == types.OrderStateInvalid {
7679
return nil, status.Error(codes.InvalidArgument, "invalid state value")
7780
}
7881

7982
states = append(states, byte(stateVal))
80-
} else {
83+
default:
8184
// request does not have pagination set. Start from open store
8285
states = append(states, byte(types.OrderOpen))
8386
states = append(states, byte(types.OrderActive))
@@ -558,15 +561,3 @@ func (k Querier) Lease(c context.Context, req *types.QueryLeaseRequest) (*types.
558561
EscrowPayment: payment,
559562
}, nil
560563
}
561-
562-
// Helper function to check if bytes represent a Base64 string
563-
func isBase64String(data []byte) bool {
564-
// Check if all bytes are valid Base64 characters
565-
for _, b := range data {
566-
if !((b >= 'A' && b <= 'Z') || (b >= 'a' && b <= 'z') || (b >= '0' && b <= '9') || b == '+' || b == '/' || b == '=') {
567-
return false
568-
}
569-
}
570-
// Additional check: Base64 strings should have length divisible by 4 (with padding)
571-
return len(data) > 0 && len(data)%4 == 0
572-
}

0 commit comments

Comments
 (0)