Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update Paginate to use FilterPaginate in ClientStates and ConnectionChannels grpc queries #3010

Merged
merged 21 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion e2e/testsuite/testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ func (b *broadcastUser) FormattedAddressWithPrefix(prefix string) string {
// BroadcastMessages broadcasts the provided messages to the given chain and signs them on behalf of the provided user.
// Once the broadcast response is returned, we wait for a few blocks to be created on both chain A and chain B.
func (s *E2ETestSuite) BroadcastMessages(ctx context.Context, chain *cosmos.CosmosChain, user ibc.Wallet, msgs ...sdk.Msg) (sdk.TxResponse, error) {

// wrap the user so it is a valid implementation of broadcast user.
b := broadcastUser{Wallet: user}

Expand Down
11 changes: 6 additions & 5 deletions modules/core/02-client/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,26 @@ func (q Keeper) ClientStates(c context.Context, req *types.QueryClientStatesRequ
clientStates := types.IdentifiedClientStates{}
store := prefix.NewStore(ctx.KVStore(q.storeKey), host.KeyClientStorePrefix)

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) {
crodriguezvega marked this conversation as resolved.
Show resolved Hide resolved
// filter any metadata stored under client state key
keySplit := strings.Split(string(key), "/")
if keySplit[len(keySplit)-1] != "clientState" {
return nil
return false, nil
}

clientState, err := q.UnmarshalClientState(value)
if err != nil {
return err
return false, err
}

clientID := keySplit[1]
if err := host.ClientIdentifierValidator(clientID); err != nil {
return err
return false, err
}

identifiedClient := types.NewIdentifiedClientState(clientID, clientState)
clientStates = append(clientStates, identifiedClient)
return nil
return true, nil
})
if err != nil {
return nil, err
Expand Down
17 changes: 12 additions & 5 deletions modules/core/02-client/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,26 +97,30 @@ func (suite *KeeperTestSuite) TestQueryClientStates() {
)

testCases := []struct {
msg string
malleate func()
expPass bool
msg string
expectedClientStates uint64
malleate func()
expPass bool
}{
{
"req is nil",
0,
func() {
req = nil
},
false,
},
{
"empty pagination",
0,
func() {
req = &types.QueryClientStatesRequest{}
},
true,
},
{
"success, no results",
0,
func() {
req = &types.QueryClientStatesRequest{
Pagination: &query.PageRequest{
Expand All @@ -129,6 +133,7 @@ func (suite *KeeperTestSuite) TestQueryClientStates() {
},
{
"success",
2,
func() {
path1 := ibctesting.NewPath(suite.chainA, suite.chainB)
suite.coordinator.SetupClients(path1)
Expand Down Expand Up @@ -163,10 +168,10 @@ func (suite *KeeperTestSuite) TestQueryClientStates() {
ctx := sdk.WrapSDKContext(suite.chainA.GetContext())

res, err := suite.chainA.QueryServer.ClientStates(ctx, req)

if tc.expPass {
suite.Require().NoError(err)
suite.Require().NotNil(res)
suite.Require().Equal(tc.expectedClientStates, res.Pagination.Total)
suite.Require().Equal(expClientStates.Sort(), res.ClientStates)
} else {
suite.Require().Error(err)
Expand Down Expand Up @@ -328,6 +333,9 @@ func (suite *KeeperTestSuite) TestQueryConsensusStates() {
path := ibctesting.NewPath(suite.chainA, suite.chainB)
suite.coordinator.SetupClients(path)

path1 := ibctesting.NewPath(suite.chainA, suite.chainB)
Copy link
Contributor

@crodriguezvega crodriguezvega Jan 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this extra path in this test?

suite.coordinator.SetupClients(path1)

height1 := path.EndpointA.GetClientState().GetLatestHeight().(types.Height)
expConsensusStates = append(
expConsensusStates,
Expand Down Expand Up @@ -381,7 +389,6 @@ func (suite *KeeperTestSuite) TestQueryConsensusStates() {
for i := range expConsensusStates {
suite.Require().NotNil(res.ConsensusStates[i])
suite.Require().Equal(expConsensusStates[i], res.ConsensusStates[i])

// ensure UnpackInterfaces is defined
cachedValue := res.ConsensusStates[i].ConsensusState.GetCachedValue()
suite.Require().NotNil(cachedValue)
Expand Down
11 changes: 6 additions & 5 deletions modules/core/04-channel/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,26 +96,27 @@ func (q Keeper) ConnectionChannels(c context.Context, req *types.QueryConnection
channels := []*types.IdentifiedChannel{}
store := prefix.NewStore(ctx.KVStore(q.storeKey), []byte(host.KeyChannelEndPrefix))

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) {
crodriguezvega marked this conversation as resolved.
Show resolved Hide resolved
// filter any metadata stored under channel key
var result types.Channel
if err := q.cdc.Unmarshal(value, &result); err != nil {
return err
return false, err
}

// ignore channel and continue to the next item if the connection is
// different than the requested one
if result.ConnectionHops[0] != req.Connection {
return nil
return false, nil
}

portID, channelID, err := host.ParseChannelPath(string(key))
if err != nil {
return err
return false, err
}

identifiedChannel := types.NewIdentifiedChannel(portID, channelID, result)
channels = append(channels, &identifiedChannel)
return nil
return true, nil
})
if err != nil {
return nil, err
Expand Down
11 changes: 8 additions & 3 deletions modules/core/04-channel/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,26 +112,30 @@ func (suite *KeeperTestSuite) TestQueryChannels() {
)

testCases := []struct {
msg string
malleate func()
expPass bool
msg string
expectedChannels uint64
malleate func()
expPass bool
}{
{
"empty request",
0,
func() {
req = nil
},
false,
},
{
"empty pagination",
0,
func() {
req = &types.QueryChannelsRequest{}
},
true,
},
{
"success",
2,
func() {
path := ibctesting.NewPath(suite.chainA, suite.chainB)
suite.coordinator.Setup(path)
Expand Down Expand Up @@ -193,6 +197,7 @@ func (suite *KeeperTestSuite) TestQueryChannels() {
if tc.expPass {
suite.Require().NoError(err)
suite.Require().NotNil(res)
suite.Require().Equal(tc.expectedChannels, res.Pagination.Total)
suite.Require().Equal(expChannels, res.Channels)
suite.Require().Equal(len(expChannels), int(res.Pagination.Total))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for slowing this down but I think this pretty much the same check as below too, right?
suite.Require().Equal(len(expChannels), int(res.Pagination.Total))

We can probably remove the tc.expectedChannels.

I think as long as the scenario from the original issue has been tested and covered then I'm happy to get this merged.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree with @damiannolan and I think we can remove tc.expectedChannels.

} else {
Expand Down