-
Notifications
You must be signed in to change notification settings - Fork 1
/
grpc.go
108 lines (86 loc) · 3.12 KB
/
grpc.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package grpc
import (
"fmt"
"net"
"time"
"github.com/FiboChain/fbc/libs/tendermint/node"
app2 "github.com/FiboChain/fbc/libs/cosmos-sdk/server/types"
"github.com/FiboChain/fbc/libs/cosmos-sdk/client/context"
"github.com/FiboChain/fbc/libs/cosmos-sdk/client/flags"
interfacetypes "github.com/FiboChain/fbc/libs/cosmos-sdk/codec/types"
sdk "github.com/FiboChain/fbc/libs/cosmos-sdk/types"
"github.com/FiboChain/fbc/libs/cosmos-sdk/x/auth/client/utils"
"github.com/spf13/viper"
reflection "github.com/FiboChain/fbc/libs/cosmos-sdk/server/grpc/reflection/v2alpha1"
"github.com/FiboChain/fbc/libs/cosmos-sdk/codec"
"github.com/gogo/protobuf/jsonpb"
"github.com/FiboChain/fbc/libs/cosmos-sdk/server/grpc/gogoreflection"
"google.golang.org/grpc"
"github.com/FiboChain/fbc/libs/tendermint/config"
)
// ServerStartTime defines the time duration that the server need to stay running after startup
// for the startup be considered successful
const ServerStartTime = 5 * time.Second
// StartGRPCServer starts a gRPC server on the given address.
func StartGRPCServer(cdc *codec.CodecProxy, interfaceReg jsonpb.AnyResolver, app app2.ApplicationAdapter, cfg config.GRPCConfig, tmNode *node.Node) (*grpc.Server, error) {
txCfg := utils.NewPbTxConfig(interfaceReg.(interfacetypes.InterfaceRegistry))
cliCtx := context.NewCLIContext().WithProxy(cdc).WithInterfaceRegistry(interfaceReg.(interfacetypes.InterfaceRegistry)).WithTrustNode(true)
if tmNode != nil {
cliCtx = cliCtx.WithChainID(tmNode.ConsensusState().GetState().ChainID)
} else {
cliCtx = cliCtx.WithChainID(viper.GetString(flags.FlagChainID))
}
maxSendMsgSize := cfg.MaxSendMsgSize
if maxSendMsgSize == 0 {
maxSendMsgSize = config.DefaultGRPCMaxSendMsgSize
}
maxRecvMsgSize := cfg.MaxRecvMsgSize
if maxRecvMsgSize == 0 {
maxRecvMsgSize = config.DefaultGRPCMaxRecvMsgSize
}
grpcSrv := grpc.NewServer(
grpc.MaxSendMsgSize(maxSendMsgSize),
grpc.MaxRecvMsgSize(maxRecvMsgSize),
)
app.RegisterTxService(cliCtx)
app.RegisterGRPCServer(grpcSrv)
// Reflection allows consumers to build dynamic clients that can write to any
// Cosmos SDK application without relying on application packages at compile
// time.
err := reflection.Register(grpcSrv, reflection.Config{
SigningModes: func() map[string]int32 {
modes := make(map[string]int32, len(txCfg.SignModeHandler().Modes()))
for _, m := range txCfg.SignModeHandler().Modes() {
modes[m.String()] = (int32)(m)
}
return modes
}(),
ChainID: cliCtx.ChainID,
SdkConfig: sdk.GetConfig(),
InterfaceRegistry: cliCtx.InterfaceRegistry,
})
if err != nil {
return nil, err
}
// Reflection allows external clients to see what services and methods
// the gRPC server exposes.
gogoreflection.Register(grpcSrv)
listener, err := net.Listen("tcp", cfg.Address)
if err != nil {
return nil, err
}
errCh := make(chan error)
go func() {
err = grpcSrv.Serve(listener)
if err != nil {
errCh <- fmt.Errorf("failed to serve: %w", err)
}
}()
select {
case err := <-errCh:
return nil, err
case <-time.After(ServerStartTime):
// assume server started successfully
return grpcSrv, nil
}
}