From f67eac6840b33f1ef1145f82650f8ac1d3968543 Mon Sep 17 00:00:00 2001 From: Aditya Date: Mon, 4 Jan 2021 15:03:11 +0000 Subject: [PATCH] Fix ConsensusStates grpc query (#8246) * fix consensus states grpc query * Update x/ibc/core/02-client/keeper/grpc_query.go * simplify --- x/ibc/core/02-client/keeper/grpc_query.go | 13 +++++++++---- x/ibc/core/02-client/keeper/grpc_query_test.go | 12 +++++++++--- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/x/ibc/core/02-client/keeper/grpc_query.go b/x/ibc/core/02-client/keeper/grpc_query.go index f14507d8ce..6328e5de3b 100644 --- a/x/ibc/core/02-client/keeper/grpc_query.go +++ b/x/ibc/core/02-client/keeper/grpc_query.go @@ -157,19 +157,24 @@ func (q Keeper) ConsensusStates(c context.Context, req *types.QueryConsensusStat consensusStates := []types.ConsensusStateWithHeight{} store := prefix.NewStore(ctx.KVStore(q.storeKey), host.FullClientKey(req.ClientId, []byte(fmt.Sprintf("%s/", host.KeyConsensusStatePrefix)))) - pageRes, err := query.Paginate(store, req.Pagination, func(key, value []byte) error { + pageRes, err := query.FilteredPaginate(store, req.Pagination, func(key, value []byte, accumulate bool) (bool, error) { + // filter any metadata stored under consensus state key + if strings.Contains(string(key), "/") { + return false, nil + } + height, err := types.ParseHeight(string(key)) if err != nil { - return err + return false, err } consensusState, err := q.UnmarshalConsensusState(value) if err != nil { - return err + return false, err } consensusStates = append(consensusStates, types.NewConsensusStateWithHeight(height, consensusState)) - return nil + return true, nil }) if err != nil { diff --git a/x/ibc/core/02-client/keeper/grpc_query_test.go b/x/ibc/core/02-client/keeper/grpc_query_test.go index 9fc8f975cf..35b0691ad2 100644 --- a/x/ibc/core/02-client/keeper/grpc_query_test.go +++ b/x/ibc/core/02-client/keeper/grpc_query_test.go @@ -315,8 +315,14 @@ func (suite *KeeperTestSuite) TestQueryConsensusStates() { suite.consensusState.Timestamp.Add(time.Second), commitmenttypes.NewMerkleRoot([]byte("hash2")), nil, ) - suite.keeper.SetClientConsensusState(suite.ctx, testClientID, testClientHeight, cs) - suite.keeper.SetClientConsensusState(suite.ctx, testClientID, testClientHeight.Increment(), cs2) + clientState := ibctmtypes.NewClientState( + testChainID, ibctmtypes.DefaultTrustLevel, trustingPeriod, ubdPeriod, maxClockDrift, testClientHeight, commitmenttypes.GetSDKSpecs(), ibctesting.UpgradePath, false, false, + ) + + // Use CreateClient to ensure that processedTime metadata gets stored. + clientId, err := suite.keeper.CreateClient(suite.ctx, clientState, cs) + suite.Require().NoError(err) + suite.keeper.SetClientConsensusState(suite.ctx, clientId, testClientHeight.Increment(), cs2) // order is swapped because the res is sorted by client id expConsensusStates = []types.ConsensusStateWithHeight{ @@ -324,7 +330,7 @@ func (suite *KeeperTestSuite) TestQueryConsensusStates() { types.NewConsensusStateWithHeight(testClientHeight.Increment().(types.Height), cs2), } req = &types.QueryConsensusStatesRequest{ - ClientId: testClientID, + ClientId: clientId, Pagination: &query.PageRequest{ Limit: 3, CountTotal: true,