Skip to content

Commit 9a61a42

Browse files
authored
refactor(x/marker): cleanup decode/encode for pagination keys (#2005)
Signed-off-by: Artur Troian <[email protected]> Co-authored-by: Artur Troian <[email protected]>
1 parent da8666b commit 9a61a42

File tree

1 file changed

+62
-70
lines changed

1 file changed

+62
-70
lines changed

x/market/keeper/grpc_query.go

Lines changed: 62 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,11 @@ func (k Querier) Orders(c context.Context, req *types.QueryOrdersRequest) (*type
8080

8181
total := uint64(0)
8282

83-
for idx := range states {
84-
state := types.Order_State(states[idx])
85-
var err error
83+
var idx int
84+
var err error
8685

86+
for idx = range states {
87+
state := types.Order_State(states[idx])
8788
if idx > 0 {
8889
req.Pagination.Key = nil
8990
}
@@ -125,34 +126,27 @@ func (k Querier) Orders(c context.Context, req *types.QueryOrdersRequest) (*type
125126
return nil, status.Error(codes.Internal, err.Error())
126127
}
127128

128-
if len(pageRes.NextKey) > 0 {
129-
nextKey := make([]byte, len(searchPrefix)+len(pageRes.NextKey))
130-
copy(nextKey, searchPrefix)
131-
copy(nextKey[len(searchPrefix):], pageRes.NextKey)
132-
133-
pageRes.NextKey = nextKey
134-
}
135-
136129
req.Pagination.Limit -= count
137130
total += count
138131

139132
if req.Pagination.Limit == 0 {
140-
if len(pageRes.NextKey) > 0 {
141-
pageRes.NextKey, err = query.EncodePaginationKey(states[idx:], searchPrefix, pageRes.NextKey, nil)
142-
if err != nil {
143-
pageRes.Total = total
144-
return &types.QueryOrdersResponse{
145-
Orders: orders,
146-
Pagination: pageRes,
147-
}, status.Error(codes.Internal, err.Error())
148-
}
149-
}
150-
151133
break
152134
}
153135
}
154136

155-
pageRes.Total = total
137+
if pageRes != nil {
138+
pageRes.Total = total
139+
140+
if len(pageRes.NextKey) > 0 {
141+
pageRes.NextKey, err = query.EncodePaginationKey(states[idx:], searchPrefix, pageRes.NextKey, nil)
142+
if err != nil {
143+
return &types.QueryOrdersResponse{
144+
Orders: orders,
145+
Pagination: pageRes,
146+
}, status.Error(codes.Internal, err.Error())
147+
}
148+
}
149+
}
156150

157151
return &types.QueryOrdersResponse{
158152
Orders: orders,
@@ -196,7 +190,7 @@ func (k Querier) Bids(c context.Context, req *types.QueryBidsRequest) (*types.Qu
196190
}
197191
req.Pagination.Key = key
198192

199-
if unsolicited[1] == 1 {
193+
if unsolicited[0] == 1 {
200194
reverseSearch = true
201195
}
202196
} else if req.Filters.State != "" {
@@ -220,9 +214,11 @@ func (k Querier) Bids(c context.Context, req *types.QueryBidsRequest) (*types.Qu
220214

221215
total := uint64(0)
222216

223-
for idx := range states {
217+
var idx int
218+
var err error
219+
220+
for idx = range states {
224221
state := types.Bid_State(states[idx])
225-
var err error
226222

227223
if idx > 0 {
228224
req.Pagination.Key = nil
@@ -278,41 +274,34 @@ func (k Querier) Bids(c context.Context, req *types.QueryBidsRequest) (*types.Qu
278274
return nil, status.Error(codes.Internal, err.Error())
279275
}
280276

281-
if len(pageRes.NextKey) > 0 {
282-
nextKey := make([]byte, len(searchPrefix)+len(pageRes.NextKey))
283-
copy(nextKey, searchPrefix)
284-
copy(nextKey[len(searchPrefix):], pageRes.NextKey)
285-
286-
pageRes.NextKey = nextKey
287-
}
288-
289277
req.Pagination.Limit -= count
290278
total += count
291279

292280
if req.Pagination.Limit == 0 {
293-
if len(pageRes.NextKey) > 0 {
294-
unsolicited := make([]byte, 1)
295-
unsolicited[0] = 0
296-
if reverseSearch {
297-
unsolicited[0] = 1
298-
}
281+
break
282+
}
283+
}
299284

300-
pageRes.NextKey, err = query.EncodePaginationKey(states[idx:], searchPrefix, pageRes.NextKey, unsolicited)
301-
if err != nil {
302-
pageRes.Total = total
303-
return &types.QueryBidsResponse{
304-
Bids: bids,
305-
Pagination: pageRes,
306-
}, status.Error(codes.Internal, err.Error())
307-
}
285+
if pageRes != nil {
286+
pageRes.Total = total
287+
if len(pageRes.NextKey) > 0 {
288+
unsolicited := make([]byte, 1)
289+
unsolicited[0] = 0
290+
if reverseSearch {
291+
unsolicited[0] = 1
308292
}
309293

310-
break
294+
pageRes.NextKey, err = query.EncodePaginationKey(states[idx:], searchPrefix, pageRes.NextKey, unsolicited)
295+
if err != nil {
296+
pageRes.Total = total
297+
return &types.QueryBidsResponse{
298+
Bids: bids,
299+
Pagination: pageRes,
300+
}, status.Error(codes.Internal, err.Error())
301+
}
311302
}
312303
}
313304

314-
pageRes.Total = total
315-
316305
return &types.QueryBidsResponse{
317306
Bids: bids,
318307
Pagination: pageRes,
@@ -380,9 +369,10 @@ func (k Querier) Leases(c context.Context, req *types.QueryLeasesRequest) (*type
380369

381370
total := uint64(0)
382371

383-
for idx := range states {
372+
var idx int
373+
var err error
374+
for idx = range states {
384375
state := types.Lease_State(states[idx])
385-
var err error
386376

387377
if idx > 0 {
388378
req.Pagination.Key = nil
@@ -445,29 +435,31 @@ func (k Querier) Leases(c context.Context, req *types.QueryLeasesRequest) (*type
445435
total += count
446436

447437
if req.Pagination.Limit == 0 {
448-
if len(pageRes.NextKey) > 0 {
449-
unsolicited := make([]byte, 1)
450-
unsolicited[0] = 0
451-
if reverseSearch {
452-
unsolicited[0] = 1
453-
}
438+
break
439+
}
440+
}
454441

455-
pageRes.NextKey, err = query.EncodePaginationKey(states[idx:], searchPrefix, pageRes.NextKey, unsolicited)
456-
if err != nil {
457-
pageRes.Total = total
458-
return &types.QueryLeasesResponse{
459-
Leases: leases,
460-
Pagination: pageRes,
461-
}, status.Error(codes.Internal, err.Error())
462-
}
442+
if pageRes != nil {
443+
pageRes.Total = total
444+
445+
if len(pageRes.NextKey) > 0 {
446+
unsolicited := make([]byte, 1)
447+
unsolicited[0] = 0
448+
if reverseSearch {
449+
unsolicited[0] = 1
463450
}
464451

465-
break
452+
pageRes.NextKey, err = query.EncodePaginationKey(states[idx:], searchPrefix, pageRes.NextKey, unsolicited)
453+
if err != nil {
454+
pageRes.Total = total
455+
return &types.QueryLeasesResponse{
456+
Leases: leases,
457+
Pagination: pageRes,
458+
}, status.Error(codes.Internal, err.Error())
459+
}
466460
}
467461
}
468462

469-
pageRes.Total = total
470-
471463
return &types.QueryLeasesResponse{
472464
Leases: leases,
473465
Pagination: pageRes,

0 commit comments

Comments
 (0)