Skip to content

Commit

Permalink
Merge pull request #1619 from CosmWasm/queries
Browse files Browse the repository at this point in the history
Restrict pagination on all-state-query
  • Loading branch information
alpe authored Sep 14, 2023
2 parents 09b5008 + 965e28c commit 42f3192
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
5 changes: 4 additions & 1 deletion x/wasm/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,10 @@ func GetCmdGetContractStateAll() *cobra.Command {
SilenceUsage: true,
}
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "contract state")
cmd.Flags().String(flags.FlagPageKey, "", "pagination page-key of contract state to query for")
cmd.Flags().Uint64(flags.FlagLimit, 100, "pagination limit of contract state to query for")
cmd.Flags().Bool(flags.FlagReverse, false, "results are sorted in descending order")

return cmd
}

Expand Down
4 changes: 4 additions & 0 deletions x/wasm/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ func (q GrpcQuerier) AllContractState(c context.Context, req *types.QueryAllCont
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if req.Pagination != nil &&
(req.Pagination.Offset != 0 || req.Pagination.CountTotal) {
return nil, status.Error(codes.InvalidArgument, "offset and count queries not supported anymore")
}
contractAddr, err := sdk.AccAddressFromBech32(req.Address)
if err != nil {
return nil, err
Expand Down
15 changes: 10 additions & 5 deletions x/wasm/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,16 @@ func TestQueryAllContractState(t *testing.T) {
Offset: 1,
},
},
expModelContains: []types.Model{
{Key: []byte("foo"), Value: []byte(`"bar"`)},
},
expModelContainsNot: []types.Model{
{Key: []byte{0x0, 0x1}, Value: []byte(`{"count":8}`)},
expErr: status.Error(codes.InvalidArgument, "offset and count queries not supported anymore"),
},
"with pagination count": {
srcQuery: &types.QueryAllContractStateRequest{
Address: contractAddr.String(),
Pagination: &query.PageRequest{
CountTotal: true,
},
},
expErr: status.Error(codes.InvalidArgument, "offset and count queries not supported anymore"),
},
"with pagination limit": {
srcQuery: &types.QueryAllContractStateRequest{
Expand Down Expand Up @@ -108,6 +112,7 @@ func TestQueryAllContractState(t *testing.T) {
require.Equal(t, spec.expErr.Error(), err.Error())
return
}
require.NoError(t, err)
for _, exp := range spec.expModelContains {
assert.Contains(t, got.Models, exp)
}
Expand Down

0 comments on commit 42f3192

Please sign in to comment.