-
Notifications
You must be signed in to change notification settings - Fork 4
/
rpc_client.go
76 lines (63 loc) · 2.32 KB
/
rpc_client.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
package testnode
import (
"context"
"strings"
srvconfig "github.com/cosmos/cosmos-sdk/server/config"
srvgrpc "github.com/cosmos/cosmos-sdk/server/grpc"
srvtypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/tendermint/tendermint/node"
"github.com/tendermint/tendermint/rpc/client/local"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
// StartNode starts the tendermint node along with a local core rpc client. The
// rpc is returned via the client.Context. The function returned should be
// called during cleanup to teardown the node, core client, along with canceling
// the internal context.Context in the returned Context.
func StartNode(tmNode *node.Node, cctx Context) (Context, func() error, error) {
if err := tmNode.Start(); err != nil {
return cctx, func() error { return nil }, err
}
coreClient := local.New(tmNode)
cctx.Context = cctx.WithClient(coreClient)
goCtx, cancel := context.WithCancel(context.Background())
cctx.rootCtx = goCtx
cleanup := func() error {
cancel()
err := tmNode.Stop()
if err != nil {
return err
}
tmNode.Wait()
return nil
}
return cctx, cleanup, nil
}
// StartGRPCServer starts the grpc server using the provided application and
// config. A grpc client connection to that server is also added to the client
// context. The returned function should be used to shutdown the server.
func StartGRPCServer(app srvtypes.Application, appCfg *srvconfig.Config, cctx Context) (Context, func() error, error) {
emptycleanup := func() error { return nil }
// Add the tx service in the gRPC router.
app.RegisterTxService(cctx.Context)
// Add the tendermint queries service in the gRPC router.
app.RegisterTendermintService(cctx.Context)
grpcSrv, err := srvgrpc.StartGRPCServer(cctx.Context, app, appCfg.GRPC)
if err != nil {
return Context{}, emptycleanup, err
}
nodeGRPCAddr := strings.Replace(appCfg.GRPC.Address, "0.0.0.0", "localhost", 1)
conn, err := grpc.Dial(nodeGRPCAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return Context{}, emptycleanup, err
}
cctx.Context = cctx.WithGRPCClient(conn)
return cctx, func() error {
grpcSrv.Stop()
return nil
}, nil
}
// DefaultAppConfig wraps the default config described in the server
func DefaultAppConfig() *srvconfig.Config {
return srvconfig.DefaultConfig()
}