/
local_client.go
165 lines (139 loc) · 4.65 KB
/
local_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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package abci
import (
"context"
"time"
tmquery "github.com/tendermint/tendermint/libs/pubsub/query"
"github.com/tendermint/tendermint/libs/service"
nm "github.com/tendermint/tendermint/node"
"github.com/tendermint/tendermint/rpc/client/local"
tmctypes "github.com/tendermint/tendermint/rpc/core/types"
tmtypes "github.com/tendermint/tendermint/types"
)
type LocalClient struct {
node *local.Local
}
func newLocalClient(node service.Service) (*LocalClient, error) {
localNode := local.New(node.(*nm.Node))
return &LocalClient{
node: localNode,
}, nil
}
func (c *LocalClient) SendTransactionAsync(ctx context.Context, bytes []byte) (*tmctypes.ResultBroadcastTx, error) {
// Fire off the transaction for consensus
res, err := c.node.BroadcastTxAsync(ctx, bytes)
if err != nil {
return nil, err
}
return res, nil
}
func (c *LocalClient) SendTransactionSync(ctx context.Context, bytes []byte) (*tmctypes.ResultBroadcastTx, error) {
// Fire off the transaction for consensus
return c.node.BroadcastTxSync(ctx, bytes)
}
func (c *LocalClient) SendTransactionCommit(ctx context.Context, bytes []byte) (*tmctypes.ResultBroadcastTxCommit, error) {
// Fire off the transaction for consensus
return c.node.BroadcastTxCommit(ctx, bytes)
}
func (c *LocalClient) CheckTransaction(ctx context.Context, bytes []byte) (*tmctypes.ResultCheckTx, error) {
return c.node.CheckTx(ctx, bytes)
}
// GetGenesisTime retrieves the genesis time from the blockchain.
func (c *LocalClient) GetGenesisTime(ctx context.Context) (genesisTime time.Time, err error) {
res, err := c.node.Genesis(ctx)
if err != nil {
return time.Time{}, err
}
return res.Genesis.GenesisTime.UTC(), nil
}
// GetChainID retrieves the chainID from the blockchain.
func (c *LocalClient) GetChainID(ctx context.Context) (chainID string, err error) {
res, err := c.node.Genesis(ctx)
if err != nil {
return "", err
}
return res.Genesis.ChainID, nil
}
// GetStatus returns the current status of the chain.
func (c *LocalClient) GetStatus(ctx context.Context) (status *tmctypes.ResultStatus, err error) {
return c.node.Status(ctx)
}
// GetNetworkInfo return information of the current network.
func (c *LocalClient) GetNetworkInfo(ctx context.Context) (netInfo *tmctypes.ResultNetInfo, err error) {
return c.node.NetInfo(ctx)
}
// GetUnconfirmedTxCount return the current count of unconfirmed transactions.
func (c *LocalClient) GetUnconfirmedTxCount(ctx context.Context) (count int, err error) {
res, err := c.node.NumUnconfirmedTxs(ctx)
if err != nil {
return 0, err
}
return res.Count, err
}
// Health returns the result of the health endpoint of the chain.
func (c *LocalClient) Health(ctx context.Context) (*tmctypes.ResultHealth, error) {
return c.node.Health(ctx)
}
func (c *LocalClient) Validators(ctx context.Context, height *int64) ([]*tmtypes.Validator, error) {
res, err := c.node.Validators(ctx, height, nil, nil)
if err != nil {
return nil, err
}
return res.Validators, nil
}
func (c *LocalClient) Genesis(ctx context.Context) (*tmtypes.GenesisDoc, error) {
res, err := c.node.Genesis(ctx)
if err != nil {
return nil, err
}
return res.Genesis, nil
}
func (c *LocalClient) GenesisValidators(ctx context.Context) ([]*tmtypes.Validator, error) {
gen, err := c.Genesis(ctx)
if err != nil {
return nil, err
}
validators := make([]*tmtypes.Validator, 0, len(gen.Validators))
for _, v := range gen.Validators {
validators = append(validators, &tmtypes.Validator{
Address: v.Address,
PubKey: v.PubKey,
VotingPower: v.Power,
})
}
return validators, nil
}
// Subscribe subscribes to any event matching query (https://godoc.org/github.com/tendermint/tendermint/types#pkg-constants).
// Subscribe will call fn each time it receives an event from the node.
// The function returns nil when the context is canceled or when fn returns an error.
func (c *LocalClient) Subscribe(ctx context.Context, fn func(tmctypes.ResultEvent) error, queries ...string) error {
if err := c.node.Start(); err != nil {
return err
}
defer c.node.Stop()
errCh := make(chan error)
for _, query := range queries {
q, err := tmquery.New(query)
if err != nil {
return err
}
// For subscription we use "zeta" as the client name but it's ignored by the implementation.
// 10 is the channel capacity which is absolutely arbitraty.
out, err := c.node.Subscribe(ctx, "zeta", q.String(), 10)
if err != nil {
return err
}
go func() {
for res := range out {
if err := fn(res); err != nil {
errCh <- err
return
}
}
}()
}
defer c.node.UnsubscribeAll(context.Background(), "zeta")
return <-errCh
}
func (c *LocalClient) Start() error {
return nil // Nothing to do for this client type.
}