Skip to content

Commit 46f325e

Browse files
committed
test: better grpc queries
1 parent 0236910 commit 46f325e

File tree

2 files changed

+48
-17
lines changed

2 files changed

+48
-17
lines changed

e2e/interchaintest/testsuite/grpc_query.go

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,43 @@ package testsuite
33
import (
44
"context"
55
"fmt"
6-
"strings"
76

87
"github.com/cosmos/gogoproto/proto"
98
"google.golang.org/grpc"
109
"google.golang.org/grpc/credentials/insecure"
1110

11+
reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
12+
1213
"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
1314
)
1415

16+
var queryReqToPath = make(map[string]string)
17+
18+
func populateQueryReqToPath(ctx context.Context, chain *cosmos.CosmosChain) error {
19+
resp, err := queryFileDescriptors(ctx, chain)
20+
if err != nil {
21+
return err
22+
}
23+
24+
for _, fileDescriptor := range resp.Files {
25+
for _, service := range fileDescriptor.GetService() {
26+
fmt.Println("Service Uninterpreted Options: ", service.GetOptions().GetUninterpretedOption())
27+
for _, method := range service.GetMethod() {
28+
queryReqToPath[method.GetInputType()] = service.GetName() + "/" + method.GetName()
29+
}
30+
}
31+
}
32+
33+
fmt.Println("queryReqToPath: ", queryReqToPath)
34+
35+
return nil
36+
}
37+
1538
// Queries the chain with a query request and deserializes the response to T
1639
func GRPCQuery[T any](ctx context.Context, chain *cosmos.CosmosChain, req proto.Message, opts ...grpc.CallOption) (*T, error) {
17-
path, err := getProtoPath(req)
18-
if err != nil {
19-
return nil, err
40+
path, ok := queryReqToPath[proto.MessageName(req)]
41+
if !ok {
42+
return nil, fmt.Errorf("no path found for %s", proto.MessageName(req))
2043
}
2144

2245
// Create a connection to the gRPC server.
@@ -39,22 +62,26 @@ func GRPCQuery[T any](ctx context.Context, chain *cosmos.CosmosChain, req proto.
3962
return resp, nil
4063
}
4164

42-
func getProtoPath(req proto.Message) (string, error) {
43-
typeUrl := "/" + proto.MessageName(req)
44-
45-
queryIndex := strings.Index(typeUrl, "Query")
46-
if queryIndex == -1 {
47-
return "", fmt.Errorf("invalid typeUrl: %s", typeUrl)
65+
func queryFileDescriptors(ctx context.Context, chain *cosmos.CosmosChain) (*reflectionv1.FileDescriptorsResponse, error) {
66+
// Create a connection to the gRPC server.
67+
grpcConn, err := grpc.Dial(
68+
chain.GetHostGRPCAddress(),
69+
grpc.WithTransportCredentials(insecure.NewCredentials()),
70+
)
71+
if err != nil {
72+
return nil, err
4873
}
4974

50-
// Add to the index to account for the length of "Query"
51-
queryIndex += len("Query")
75+
defer grpcConn.Close()
5276

53-
// Add a slash before the query
54-
urlWithSlash := typeUrl[:queryIndex] + "/" + typeUrl[queryIndex:]
55-
if !strings.HasSuffix(urlWithSlash, "Request") {
56-
return "", fmt.Errorf("invalid typeUrl: %s", typeUrl)
77+
resp := new(reflectionv1.FileDescriptorsResponse)
78+
err = grpcConn.Invoke(
79+
ctx, reflectionv1.ReflectionService_FileDescriptors_FullMethodName,
80+
&reflectionv1.FileDescriptorsRequest{}, resp,
81+
)
82+
if err != nil {
83+
return nil, err
5784
}
5885

59-
return strings.TrimSuffix(urlWithSlash, "Request"), nil
86+
return resp, nil
6087
}

e2e/interchaintest/testsuite/suite.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/strangelove-ventures/interchaintest/v8/testutil"
1717
)
1818

19+
// TestSuite is a suite of tests that require two chains and a relayer
1920
type TestSuite struct {
2021
suite.Suite
2122

@@ -82,6 +83,9 @@ func (s *TestSuite) SetupSuite(ctx context.Context, chainSpecs []*interchaintest
8283
SkipPathCreation: true,
8384
}))
8485

86+
s.Require().NoError(populateQueryReqToPath(ctx, s.ChainA))
87+
s.Require().NoError(populateQueryReqToPath(ctx, s.ChainB))
88+
8589
// Fund a user account on ChainA and ChainB
8690
const userFunds = int64(10_000_000_000)
8791
users := interchaintest.GetAndFundTestUsers(t, ctx, t.Name(), userFunds, s.ChainA, s.ChainB)

0 commit comments

Comments
 (0)