Skip to content

Commit

Permalink
feat(eureka): Add gRPC query for Counterparty for a given client iden…
Browse files Browse the repository at this point in the history
…tifier (#7120)

* chore: add grpc query for counterParty

* chpre: made changes to the proto

* chore: updated goDoc

* chore: added creator to the respone

* fix: fixes in the grpc_query

* fixes

* improve implementation and add tests

* trying to fix weird linter error

* fix build

* chore: make counterparty field of response a non pointer.

* chore: simplify logic, remove uneeded ifs

* chore(tests): use expError pattern.

---------

Co-authored-by: Carlos Rodriguez <carlos@interchain.io>
Co-authored-by: DimitrisJim <d.f.hilliard@gmail.com>
  • Loading branch information
3 people authored Aug 26, 2024
1 parent e66223c commit 5d62925
Show file tree
Hide file tree
Showing 5 changed files with 753 additions and 81 deletions.
22 changes: 22 additions & 0 deletions modules/core/02-client/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,3 +410,25 @@ func (q *queryServer) VerifyMembership(c context.Context, req *types.QueryVerify
Success: true,
}, nil
}

// Client implements the Query/Client gRPC method
func (q *queryServer) Client(ctx context.Context, req *types.QueryClientRequest) (*types.QueryClientResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

if err := host.ClientIdentifierValidator(req.ClientId); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}

res := types.QueryClientResponse{}

sdkCtx := sdk.UnwrapSDKContext(ctx)
creator, _ := q.GetCreator(sdkCtx, req.ClientId)
res.Creator = creator

counterparty, _ := q.GetCounterparty(sdkCtx, req.ClientId)
res.Counterparty = counterparty

return &res, nil
}
96 changes: 96 additions & 0 deletions modules/core/02-client/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -954,3 +954,99 @@ func (suite *KeeperTestSuite) TestQueryVerifyMembershipProof() {
})
}
}

func (suite *KeeperTestSuite) TestQueryClient() {
var (
req *types.QueryClientRequest
expCreator string
expCounterparty types.Counterparty
)

testCases := []struct {
msg string
malleate func()
expError error
}{
{
"success",
func() {
ctx := suite.chainA.GetContext()
suite.chainA.App.GetIBCKeeper().ClientKeeper.SetCreator(ctx, ibctesting.FirstClientID, expCreator)
suite.chainA.App.GetIBCKeeper().ClientKeeper.SetCounterparty(ctx, ibctesting.FirstClientID, expCounterparty)

req = &types.QueryClientRequest{
ClientId: ibctesting.FirstClientID,
}
},
nil,
},
{
"success: no creator",
func() {
expCreator = ""

suite.chainA.App.GetIBCKeeper().ClientKeeper.SetCounterparty(suite.chainA.GetContext(), ibctesting.FirstClientID, expCounterparty)

req = &types.QueryClientRequest{
ClientId: ibctesting.FirstClientID,
}
},
nil,
},
{
"success: no counterparty",
func() {
expCounterparty = types.Counterparty{}

suite.chainA.App.GetIBCKeeper().ClientKeeper.SetCreator(suite.chainA.GetContext(), ibctesting.FirstClientID, expCreator)

req = &types.QueryClientRequest{
ClientId: ibctesting.FirstClientID,
}
},
nil,
},
{
"req is nil",
func() {
req = nil
},
status.Error(codes.InvalidArgument, "empty request"),
},
{
"invalid clientID",
func() {
req = &types.QueryClientRequest{}
},
status.Error(codes.InvalidArgument, "identifier cannot be blank: invalid identifier"),
},
}

for _, tc := range testCases {
tc := tc

suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
suite.SetupTest() // reset

expCreator = ibctesting.TestAccAddress
merklePathPrefix := commitmenttypes.NewMerklePath([]byte("prefix"))
expCounterparty = types.Counterparty{ClientId: ibctesting.SecondClientID, MerklePathPrefix: merklePathPrefix}

tc.malleate()

queryServer := keeper.NewQueryServer(suite.chainA.GetSimApp().IBCKeeper.ClientKeeper)
res, err := queryServer.Client(suite.chainA.GetContext(), req)

expPass := tc.expError == nil
if expPass {
suite.Require().NoError(err)
suite.Require().NotNil(res)
suite.Require().Equal(expCreator, res.Creator)
suite.Require().Equal(expCounterparty, res.Counterparty)
} else {
suite.Require().ErrorIs(err, tc.expError)
suite.Require().Nil(res)
}
})
}
}
Loading

0 comments on commit 5d62925

Please sign in to comment.