From 399c833a515ce3be176f4e1cf7e7d2d2ecb66703 Mon Sep 17 00:00:00 2001 From: Sergey <83376337+freak12techno@users.noreply.github.com> Date: Thu, 31 Oct 2024 12:49:15 +0300 Subject: [PATCH] fix: fix consumer chains list pagination (#2377) * chore: fix consumer chains list pagination * chore: added CHANGELOG entry * chore: review fixes --- .../2377-fix-list-consumer-chains-pagination.md | 2 ++ x/ccv/provider/keeper/grpc_query.go | 15 +++++++++------ x/ccv/provider/keeper/grpc_query_test.go | 13 ++++++++++++- 3 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 .changelog/unreleased/bug-fixes/2377-fix-list-consumer-chains-pagination.md diff --git a/.changelog/unreleased/bug-fixes/2377-fix-list-consumer-chains-pagination.md b/.changelog/unreleased/bug-fixes/2377-fix-list-consumer-chains-pagination.md new file mode 100644 index 0000000000..28623ee3e2 --- /dev/null +++ b/.changelog/unreleased/bug-fixes/2377-fix-list-consumer-chains-pagination.md @@ -0,0 +1,2 @@ +- `[x/provider]` Fixed pagination in the list consumer chains query. + ([\#2377](https://github.com/cosmos/interchain-security/pull/2377)) \ No newline at end of file diff --git a/x/ccv/provider/keeper/grpc_query.go b/x/ccv/provider/keeper/grpc_query.go index dfc6b06501..2d0c1e1d10 100644 --- a/x/ccv/provider/keeper/grpc_query.go +++ b/x/ccv/provider/keeper/grpc_query.go @@ -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 { diff --git a/x/ccv/provider/keeper/grpc_query_test.go b/x/ccv/provider/keeper/grpc_query_test.go index 5f664a331a..fc9eebe43f 100644 --- a/x/ccv/provider/keeper/grpc_query_test.go +++ b/x/ccv/provider/keeper/grpc_query_test.go @@ -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", @@ -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", @@ -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", @@ -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", @@ -747,6 +753,7 @@ func TestQueryConsumerChains(t *testing.T) { }, phase_filter: types.CONSUMER_PHASE_STOPPED, expConsumers: consumers[3:], + total: 1, }, } @@ -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{ @@ -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) + } }) } }