-
Notifications
You must be signed in to change notification settings - Fork 72
/
utils.go
45 lines (36 loc) · 1.27 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package client
import (
"crypto/tls"
"strconv"
"google.golang.org/grpc/credentials"
"github.com/desmos-labs/juno/types"
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
// GetHeightRequestHeader returns the grpc.CallOption to query the state at a given height
func GetHeightRequestHeader(height int64) grpc.CallOption {
header := metadata.New(map[string]string{
grpctypes.GRPCBlockHeightHeader: strconv.FormatInt(height, 10),
})
return grpc.Header(&header)
}
// MustCreateGrpcConnection creates a new gRPC connection using the provided configuration and panics on error
func MustCreateGrpcConnection(cfg types.Config) *grpc.ClientConn {
grpConnection, err := CreateGrpcConnection(cfg)
if err != nil {
panic(err)
}
return grpConnection
}
// CreateGrpcConnection creates a new gRPC client connection from the given configuration
func CreateGrpcConnection(cfg types.Config) (*grpc.ClientConn, error) {
gprConfig := cfg.GetGrpcConfig()
var grpcOpts []grpc.DialOption
if gprConfig.IsInsecure() {
grpcOpts = append(grpcOpts, grpc.WithInsecure())
} else {
grpcOpts = append(grpcOpts, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})))
}
return grpc.Dial(gprConfig.GetAddress(), grpcOpts...)
}