2023-12-13 17:17:37 +00:00
|
|
|
package query
|
2023-02-22 23:40:56 +00:00
|
|
|
|
|
|
|
import (
|
2023-03-28 22:32:36 +00:00
|
|
|
"context"
|
2023-02-22 23:40:56 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
|
2024-07-19 17:24:23 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
"github.com/kava-labs/kava/app"
|
2023-02-22 23:40:56 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials"
|
2023-10-31 23:37:40 +00:00
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
2023-02-22 23:40:56 +00:00
|
|
|
)
|
|
|
|
|
2023-12-13 17:17:37 +00:00
|
|
|
// newGrpcConnection parses a GRPC endpoint and creates a connection to it
|
|
|
|
func newGrpcConnection(ctx context.Context, endpoint string) (*grpc.ClientConn, error) {
|
2023-02-22 23:40:56 +00:00
|
|
|
grpcUrl, err := url.Parse(endpoint)
|
|
|
|
if err != nil {
|
2023-12-13 17:17:37 +00:00
|
|
|
return nil, fmt.Errorf("failed to parse grpc connection \"%s\": %v", endpoint, err)
|
2023-02-22 23:40:56 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 23:37:40 +00:00
|
|
|
var creds credentials.TransportCredentials
|
2023-02-22 23:40:56 +00:00
|
|
|
switch grpcUrl.Scheme {
|
|
|
|
case "http":
|
2023-10-31 23:37:40 +00:00
|
|
|
creds = insecure.NewCredentials()
|
2023-02-22 23:40:56 +00:00
|
|
|
case "https":
|
2023-10-31 23:37:40 +00:00
|
|
|
creds = credentials.NewTLS(&tls.Config{})
|
2023-02-22 23:40:56 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown grpc url scheme: %s", grpcUrl.Scheme)
|
|
|
|
}
|
|
|
|
|
2024-07-19 17:24:23 +00:00
|
|
|
// Ensure the encoding config is set up correctly with the query client
|
|
|
|
// otherwise it will produce panics like:
|
|
|
|
// invalid Go type math.Int for field ...
|
|
|
|
encodingConfig := app.MakeEncodingConfig()
|
|
|
|
protoCodec := codec.NewProtoCodec(encodingConfig.InterfaceRegistry)
|
|
|
|
grpcCodec := protoCodec.GRPCCodec()
|
|
|
|
|
2023-10-31 23:37:40 +00:00
|
|
|
secureOpt := grpc.WithTransportCredentials(creds)
|
2024-07-19 17:24:23 +00:00
|
|
|
grpcConn, err := grpc.DialContext(
|
|
|
|
ctx,
|
|
|
|
grpcUrl.Host,
|
|
|
|
secureOpt,
|
|
|
|
grpc.WithDefaultCallOptions(grpc.ForceCodec(grpcCodec)),
|
|
|
|
)
|
2023-02-22 23:40:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return grpcConn, nil
|
|
|
|
}
|