@@ -61,12 +61,15 @@ func (q GrpcQuerier) ContractHistory(c context.Context, req *types.QueryContract
61
61
if err != nil {
62
62
return nil , err
63
63
}
64
+ paginationParams , err := ensurePaginationParams (req .Pagination )
65
+ if err != nil {
66
+ return nil , err
67
+ }
64
68
65
69
ctx := sdk .UnwrapSDKContext (c )
66
70
r := make ([]types.ContractCodeHistoryEntry , 0 )
67
-
68
71
prefixStore := prefix .NewStore (ctx .KVStore (q .storeKey ), types .GetContractCodeHistoryElementPrefix (contractAddr ))
69
- pageRes , err := query .FilteredPaginate (prefixStore , req . Pagination , func (key , value []byte , accumulate bool ) (bool , error ) {
72
+ pageRes , err := query .FilteredPaginate (prefixStore , paginationParams , func (key , value []byte , accumulate bool ) (bool , error ) {
70
73
if accumulate {
71
74
var e types.ContractCodeHistoryEntry
72
75
if err := q .cdc .Unmarshal (value , & e ); err != nil {
@@ -93,11 +96,15 @@ func (q GrpcQuerier) ContractsByCode(c context.Context, req *types.QueryContract
93
96
if req .CodeId == 0 {
94
97
return nil , errorsmod .Wrap (types .ErrInvalid , "code id" )
95
98
}
99
+ paginationParams , err := ensurePaginationParams (req .Pagination )
100
+ if err != nil {
101
+ return nil , err
102
+ }
103
+
96
104
ctx := sdk .UnwrapSDKContext (c )
97
105
r := make ([]string , 0 )
98
-
99
106
prefixStore := prefix .NewStore (ctx .KVStore (q .storeKey ), types .GetContractByCodeIDSecondaryIndexPrefix (req .CodeId ))
100
- pageRes , err := query .FilteredPaginate (prefixStore , req . Pagination , func (key , value []byte , accumulate bool ) (bool , error ) {
107
+ pageRes , err := query .FilteredPaginate (prefixStore , paginationParams , func (key , value []byte , accumulate bool ) (bool , error ) {
101
108
if accumulate {
102
109
var contractAddr sdk.AccAddress = key [types .AbsoluteTxPositionLen :]
103
110
r = append (r , contractAddr .String ())
@@ -117,14 +124,16 @@ func (q GrpcQuerier) AllContractState(c context.Context, req *types.QueryAllCont
117
124
if req == nil {
118
125
return nil , status .Error (codes .InvalidArgument , "empty request" )
119
126
}
120
- if req .Pagination != nil &&
121
- (req .Pagination .Offset != 0 || req .Pagination .CountTotal ) {
122
- return nil , status .Error (codes .InvalidArgument , "offset and count queries not supported anymore" )
123
- }
127
+
124
128
contractAddr , err := sdk .AccAddressFromBech32 (req .Address )
125
129
if err != nil {
126
130
return nil , err
127
131
}
132
+ paginationParams , err := ensurePaginationParams (req .Pagination )
133
+ if err != nil {
134
+ return nil , err
135
+ }
136
+
128
137
ctx := sdk .UnwrapSDKContext (c )
129
138
if ! q .keeper .HasContractInfo (ctx , contractAddr ) {
130
139
return nil , types .ErrNoSuchContractFn (contractAddr .String ()).
@@ -133,7 +142,7 @@ func (q GrpcQuerier) AllContractState(c context.Context, req *types.QueryAllCont
133
142
134
143
r := make ([]types.Model , 0 )
135
144
prefixStore := prefix .NewStore (ctx .KVStore (q .storeKey ), types .GetContractStorePrefix (contractAddr ))
136
- pageRes , err := query .FilteredPaginate (prefixStore , req . Pagination , func (key , value []byte , accumulate bool ) (bool , error ) {
145
+ pageRes , err := query .FilteredPaginate (prefixStore , paginationParams , func (key , value []byte , accumulate bool ) (bool , error ) {
137
146
if accumulate {
138
147
r = append (r , types.Model {
139
148
Key : key ,
@@ -238,10 +247,15 @@ func (q GrpcQuerier) Codes(c context.Context, req *types.QueryCodesRequest) (*ty
238
247
if req == nil {
239
248
return nil , status .Error (codes .InvalidArgument , "empty request" )
240
249
}
250
+ paginationParams , err := ensurePaginationParams (req .Pagination )
251
+ if err != nil {
252
+ return nil , err
253
+ }
254
+
241
255
ctx := sdk .UnwrapSDKContext (c )
242
256
r := make ([]types.CodeInfoResponse , 0 )
243
257
prefixStore := prefix .NewStore (ctx .KVStore (q .storeKey ), types .CodeKeyPrefix )
244
- pageRes , err := query .FilteredPaginate (prefixStore , req . Pagination , func (key , value []byte , accumulate bool ) (bool , error ) {
258
+ pageRes , err := query .FilteredPaginate (prefixStore , paginationParams , func (key , value []byte , accumulate bool ) (bool , error ) {
245
259
if accumulate {
246
260
var c types.CodeInfo
247
261
if err := q .cdc .Unmarshal (value , & c ); err != nil {
@@ -302,11 +316,15 @@ func (q GrpcQuerier) PinnedCodes(c context.Context, req *types.QueryPinnedCodesR
302
316
if req == nil {
303
317
return nil , status .Error (codes .InvalidArgument , "empty request" )
304
318
}
319
+ paginationParams , err := ensurePaginationParams (req .Pagination )
320
+ if err != nil {
321
+ return nil , err
322
+ }
323
+
305
324
ctx := sdk .UnwrapSDKContext (c )
306
325
r := make ([]uint64 , 0 )
307
-
308
326
prefixStore := prefix .NewStore (ctx .KVStore (q .storeKey ), types .PinnedCodeIndexPrefix )
309
- pageRes , err := query .FilteredPaginate (prefixStore , req . Pagination , func (key , _ []byte , accumulate bool ) (bool , error ) {
327
+ pageRes , err := query .FilteredPaginate (prefixStore , paginationParams , func (key , _ []byte , accumulate bool ) (bool , error ) {
310
328
if accumulate {
311
329
r = append (r , sdk .BigEndianToUint64 (key ))
312
330
}
@@ -332,6 +350,11 @@ func (q GrpcQuerier) ContractsByCreator(c context.Context, req *types.QueryContr
332
350
if req == nil {
333
351
return nil , status .Error (codes .InvalidArgument , "empty request" )
334
352
}
353
+ paginationParams , err := ensurePaginationParams (req .Pagination )
354
+ if err != nil {
355
+ return nil , err
356
+ }
357
+
335
358
ctx := sdk .UnwrapSDKContext (c )
336
359
contracts := make ([]string , 0 )
337
360
@@ -340,7 +363,7 @@ func (q GrpcQuerier) ContractsByCreator(c context.Context, req *types.QueryContr
340
363
return nil , err
341
364
}
342
365
prefixStore := prefix .NewStore (ctx .KVStore (q .storeKey ), types .GetContractsByCreatorPrefix (creatorAddress ))
343
- pageRes , err := query .FilteredPaginate (prefixStore , req . Pagination , func (key , _ []byte , accumulate bool ) (bool , error ) {
366
+ pageRes , err := query .FilteredPaginate (prefixStore , paginationParams , func (key , _ []byte , accumulate bool ) (bool , error ) {
344
367
if accumulate {
345
368
accAddres := sdk .AccAddress (key [types .AbsoluteTxPositionLen :])
346
369
contracts = append (contracts , accAddres .String ())
@@ -356,3 +379,25 @@ func (q GrpcQuerier) ContractsByCreator(c context.Context, req *types.QueryContr
356
379
Pagination : pageRes ,
357
380
}, nil
358
381
}
382
+
383
+ // max limit to pagination queries
384
+ const maxResultEntries = 100
385
+
386
+ var errLegacyPaginationUnsupported = status .Error (codes .InvalidArgument , "offset and count queries not supported" )
387
+
388
+ // ensure that pagination is done via key iterator with reasonable limit
389
+ func ensurePaginationParams (req * query.PageRequest ) (* query.PageRequest , error ) {
390
+ if req == nil {
391
+ return & query.PageRequest {
392
+ Key : nil ,
393
+ Limit : query .DefaultLimit ,
394
+ }, nil
395
+ }
396
+ if req .Offset != 0 || req .CountTotal {
397
+ return nil , errLegacyPaginationUnsupported
398
+ }
399
+ if req .Limit > maxResultEntries || req .Limit <= 0 {
400
+ req .Limit = maxResultEntries
401
+ }
402
+ return req , nil
403
+ }
0 commit comments