@@ -3,15 +3,18 @@ package keeper
33import (
44 "bytes"
55 "context"
6+ "encoding/binary"
67 "fmt"
78 "sort"
89
910 "google.golang.org/grpc/codes"
1011 "google.golang.org/grpc/status"
1112
1213 errorsmod "cosmossdk.io/errors"
14+ "cosmossdk.io/store/prefix"
1315
1416 sdk "github.com/cosmos/cosmos-sdk/types"
17+ "github.com/cosmos/cosmos-sdk/types/query"
1518
1619 "github.com/cosmos/interchain-security/v6/x/ccv/provider/types"
1720 ccvtypes "github.com/cosmos/interchain-security/v6/x/ccv/types"
@@ -48,37 +51,35 @@ func (k Keeper) QueryConsumerChains(goCtx context.Context, req *types.QueryConsu
4851 }
4952
5053 ctx := sdk .UnwrapSDKContext (goCtx )
54+ var chains []* types.Chain
5155
52- consumerIds := []string {}
53- for _ , consumerID := range k .GetAllConsumerIds (ctx ) {
54- phase := k .GetConsumerPhase (ctx , consumerID )
55- if req .Phase != types .CONSUMER_PHASE_UNSPECIFIED && req .Phase != phase {
56- // ignore consumer chain
57- continue
56+ store := ctx .KVStore (k .storeKey )
57+ storePrefix := types .ConsumerIdToPhaseKeyPrefix ()
58+ consumerPhaseStore := prefix .NewStore (store , []byte {storePrefix })
59+ pageRes , err := query .Paginate (consumerPhaseStore , req .Pagination , func (key , value []byte ) error {
60+ consumerId , err := types .ParseStringIdWithLenKey (storePrefix , append ([]byte {storePrefix }, key ... ))
61+ if err != nil {
62+ return status .Error (codes .Internal , err .Error ())
5863 }
59- consumerIds = append (consumerIds , consumerID )
60- }
6164
62- // set limit to default value
63- limit := 100
64- if req .Limit != 0 {
65- // update limit if specified
66- limit = int (req .Limit )
67- }
68- if len (consumerIds ) > limit {
69- consumerIds = consumerIds [:limit ]
70- }
65+ phase := types .ConsumerPhase (binary .BigEndian .Uint32 (value ))
66+ if req .Phase != types .CONSUMER_PHASE_UNSPECIFIED && req .Phase != phase {
67+ return nil
68+ }
7169
72- chains := make ([]* types.Chain , len (consumerIds ))
73- for i , cID := range consumerIds {
74- c , err := k .GetConsumerChain (ctx , cID )
70+ c , err := k .GetConsumerChain (ctx , consumerId )
7571 if err != nil {
76- return nil , status .Error (codes .Internal , err .Error ())
72+ return status .Error (codes .Internal , err .Error ())
7773 }
78- chains [i ] = & c
74+ chains = append (chains , & c )
75+ return nil
76+ })
77+
78+ if err != nil {
79+ return nil , status .Error (codes .Internal , err .Error ())
7980 }
8081
81- return & types.QueryConsumerChainsResponse {Chains : chains }, nil
82+ return & types.QueryConsumerChainsResponse {Chains : chains , Pagination : pageRes }, nil
8283}
8384
8485// GetConsumerChain returns a Chain data structure with all the necessary fields
@@ -594,6 +595,7 @@ func (k Keeper) QueryConsumerChain(goCtx context.Context, req *types.QueryConsum
594595
595596 return & types.QueryConsumerChainResponse {
596597 ChainId : chainId ,
598+ ConsumerId : consumerId ,
597599 OwnerAddress : ownerAddress ,
598600 Phase : phase .String (),
599601 Metadata : metadata ,
0 commit comments