2023-02-22 23:40:56 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2023-03-28 22:32:36 +00:00
|
|
|
"context"
|
2023-02-22 23:40:56 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2023-03-28 22:32:36 +00:00
|
|
|
"strconv"
|
2023-02-22 23:40:56 +00:00
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials"
|
2023-03-28 22:32:36 +00:00
|
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
|
|
|
|
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
|
2023-02-22 23:40:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewGrpcConnection parses a GRPC endpoint and creates a connection to it
|
|
|
|
func NewGrpcConnection(endpoint string) (*grpc.ClientConn, error) {
|
|
|
|
grpcUrl, err := url.Parse(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var secureOpt grpc.DialOption
|
|
|
|
switch grpcUrl.Scheme {
|
|
|
|
case "http":
|
|
|
|
secureOpt = grpc.WithInsecure()
|
|
|
|
case "https":
|
|
|
|
creds := credentials.NewTLS(&tls.Config{})
|
|
|
|
secureOpt = grpc.WithTransportCredentials(creds)
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown grpc url scheme: %s", grpcUrl.Scheme)
|
|
|
|
}
|
|
|
|
|
|
|
|
grpcConn, err := grpc.Dial(grpcUrl.Host, secureOpt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return grpcConn, nil
|
|
|
|
}
|
2023-03-28 22:32:36 +00:00
|
|
|
|
|
|
|
func CtxAtHeight(height int64) context.Context {
|
|
|
|
heightStr := strconv.FormatInt(height, 10)
|
|
|
|
return metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, heightStr)
|
|
|
|
}
|