@@ -3,20 +3,43 @@ package testsuite
3
3
import (
4
4
"context"
5
5
"fmt"
6
- "strings"
7
6
8
7
"github.com/cosmos/gogoproto/proto"
9
8
"google.golang.org/grpc"
10
9
"google.golang.org/grpc/credentials/insecure"
11
10
11
+ reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
12
+
12
13
"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
13
14
)
14
15
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
+
15
38
// Queries the chain with a query request and deserializes the response to T
16
39
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 ))
20
43
}
21
44
22
45
// Create a connection to the gRPC server.
@@ -39,22 +62,26 @@ func GRPCQuery[T any](ctx context.Context, chain *cosmos.CosmosChain, req proto.
39
62
return resp , nil
40
63
}
41
64
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
48
73
}
49
74
50
- // Add to the index to account for the length of "Query"
51
- queryIndex += len ("Query" )
75
+ defer grpcConn .Close ()
52
76
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
57
84
}
58
85
59
- return strings . TrimSuffix ( urlWithSlash , "Request" ) , nil
86
+ return resp , nil
60
87
}
0 commit comments