Skip to content

Commit

Permalink
fix: fix consumer chains list pagination (#2377)
Browse files Browse the repository at this point in the history
* chore: fix consumer chains list pagination

* chore: added CHANGELOG entry

* chore: review fixes
  • Loading branch information
freak12techno authored Oct 31, 2024
1 parent 956aede commit 399c833
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `[x/provider]` Fixed pagination in the list consumer chains query.
([\#2377](https://github.com/cosmos/interchain-security/pull/2377))
15 changes: 9 additions & 6 deletions x/ccv/provider/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,26 @@ func (k Keeper) QueryConsumerChains(goCtx context.Context, req *types.QueryConsu
store := ctx.KVStore(k.storeKey)
storePrefix := types.ConsumerIdToPhaseKeyPrefix()
consumerPhaseStore := prefix.NewStore(store, []byte{storePrefix})
pageRes, err := query.Paginate(consumerPhaseStore, req.Pagination, func(key, value []byte) error {
pageRes, err := query.FilteredPaginate(consumerPhaseStore, req.Pagination, func(key, value []byte, accumulate bool) (bool, error) {
consumerId, err := types.ParseStringIdWithLenKey(storePrefix, append([]byte{storePrefix}, key...))
if err != nil {
return status.Error(codes.Internal, err.Error())
return false, status.Error(codes.Internal, err.Error())
}

phase := types.ConsumerPhase(binary.BigEndian.Uint32(value))
if req.Phase != types.CONSUMER_PHASE_UNSPECIFIED && req.Phase != phase {
return nil
return false, nil
}

c, err := k.GetConsumerChain(ctx, consumerId)
if err != nil {
return status.Error(codes.Internal, err.Error())
return false, status.Error(codes.Internal, err.Error())
}
chains = append(chains, &c)
return nil

if accumulate {
chains = append(chains, &c)
}
return true, nil
})

if err != nil {
Expand Down
13 changes: 12 additions & 1 deletion x/ccv/provider/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -699,18 +699,21 @@ func TestQueryConsumerChains(t *testing.T) {
setup func(ctx sdk.Context, pk keeper.Keeper)
phase_filter types.ConsumerPhase
limit uint64
total uint64
expConsumers []*types.Chain
}{
{
name: "expect all consumers when phase filter isn't set",
setup: func(ctx sdk.Context, pk keeper.Keeper) {},
expConsumers: consumers,
total: 4,
},
{
name: "expect an amount of consumer equal to the limit",
setup: func(ctx sdk.Context, pk keeper.Keeper) {},
expConsumers: consumers[:3],
limit: 3,
total: 4,
},
{
name: "expect registered consumers when phase filter is set to Registered",
Expand All @@ -720,6 +723,7 @@ func TestQueryConsumerChains(t *testing.T) {
},
phase_filter: types.CONSUMER_PHASE_REGISTERED,
expConsumers: consumers[0:1],
total: 1,
},
{
name: "expect initialized consumers when phase is set to Initialized",
Expand All @@ -729,6 +733,7 @@ func TestQueryConsumerChains(t *testing.T) {
},
phase_filter: types.CONSUMER_PHASE_INITIALIZED,
expConsumers: consumers[1:2],
total: 1,
},
{
name: "expect launched consumers when phase is set to Launched",
Expand All @@ -738,6 +743,7 @@ func TestQueryConsumerChains(t *testing.T) {
},
phase_filter: types.CONSUMER_PHASE_LAUNCHED,
expConsumers: consumers[2:3],
total: 1,
},
{
name: "expect stopped consumers when phase is set to Stopped",
Expand All @@ -747,6 +753,7 @@ func TestQueryConsumerChains(t *testing.T) {
},
phase_filter: types.CONSUMER_PHASE_STOPPED,
expConsumers: consumers[3:],
total: 1,
},
}

Expand All @@ -756,7 +763,8 @@ func TestQueryConsumerChains(t *testing.T) {
req := types.QueryConsumerChainsRequest{
Phase: tc.phase_filter,
Pagination: &sdkquery.PageRequest{
Limit: tc.limit,
Limit: tc.limit,
CountTotal: true,
},
}
expectedResponse := types.QueryConsumerChainsResponse{
Expand All @@ -768,6 +776,9 @@ func TestQueryConsumerChains(t *testing.T) {
if tc.limit != 0 {
require.Len(t, res.GetChains(), int(tc.limit), tc.name)
}
if tc.total != 0 {
require.Equal(t, res.Pagination.Total, tc.total, tc.name)
}
})
}
}

0 comments on commit 399c833

Please sign in to comment.