Skip to content

Commit

Permalink
test: better grpc queries
Browse files Browse the repository at this point in the history
  • Loading branch information
srdtrk committed Apr 2, 2024
1 parent 0236910 commit 46f325e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
61 changes: 44 additions & 17 deletions e2e/interchaintest/testsuite/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,43 @@ package testsuite
import (
"context"
"fmt"
"strings"

"github.com/cosmos/gogoproto/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"

"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
)

var queryReqToPath = make(map[string]string)

func populateQueryReqToPath(ctx context.Context, chain *cosmos.CosmosChain) error {
resp, err := queryFileDescriptors(ctx, chain)
if err != nil {
return err
}

for _, fileDescriptor := range resp.Files {
for _, service := range fileDescriptor.GetService() {
fmt.Println("Service Uninterpreted Options: ", service.GetOptions().GetUninterpretedOption())
for _, method := range service.GetMethod() {
queryReqToPath[method.GetInputType()] = service.GetName() + "/" + method.GetName()
}
}
}

fmt.Println("queryReqToPath: ", queryReqToPath)

return nil
}

// Queries the chain with a query request and deserializes the response to T
func GRPCQuery[T any](ctx context.Context, chain *cosmos.CosmosChain, req proto.Message, opts ...grpc.CallOption) (*T, error) {
path, err := getProtoPath(req)
if err != nil {
return nil, err
path, ok := queryReqToPath[proto.MessageName(req)]
if !ok {
return nil, fmt.Errorf("no path found for %s", proto.MessageName(req))
}

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

func getProtoPath(req proto.Message) (string, error) {
typeUrl := "/" + proto.MessageName(req)

queryIndex := strings.Index(typeUrl, "Query")
if queryIndex == -1 {
return "", fmt.Errorf("invalid typeUrl: %s", typeUrl)
func queryFileDescriptors(ctx context.Context, chain *cosmos.CosmosChain) (*reflectionv1.FileDescriptorsResponse, error) {
// Create a connection to the gRPC server.
grpcConn, err := grpc.Dial(
chain.GetHostGRPCAddress(),
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
return nil, err
}

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

// Add a slash before the query
urlWithSlash := typeUrl[:queryIndex] + "/" + typeUrl[queryIndex:]
if !strings.HasSuffix(urlWithSlash, "Request") {
return "", fmt.Errorf("invalid typeUrl: %s", typeUrl)
resp := new(reflectionv1.FileDescriptorsResponse)
err = grpcConn.Invoke(
ctx, reflectionv1.ReflectionService_FileDescriptors_FullMethodName,
&reflectionv1.FileDescriptorsRequest{}, resp,
)
if err != nil {
return nil, err
}

return strings.TrimSuffix(urlWithSlash, "Request"), nil
return resp, nil
}
4 changes: 4 additions & 0 deletions e2e/interchaintest/testsuite/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/strangelove-ventures/interchaintest/v8/testutil"
)

// TestSuite is a suite of tests that require two chains and a relayer
type TestSuite struct {
suite.Suite

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

s.Require().NoError(populateQueryReqToPath(ctx, s.ChainA))
s.Require().NoError(populateQueryReqToPath(ctx, s.ChainB))

// Fund a user account on ChainA and ChainB
const userFunds = int64(10_000_000_000)
users := interchaintest.GetAndFundTestUsers(t, ctx, t.Name(), userFunds, s.ChainA, s.ChainB)
Expand Down

0 comments on commit 46f325e

Please sign in to comment.