-
Notifications
You must be signed in to change notification settings - Fork 22
/
client.go
263 lines (222 loc) · 6.96 KB
/
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package abci
import (
"context"
"encoding/base64"
"errors"
"os"
"sync"
"time"
cmtjson "github.com/tendermint/tendermint/libs/json"
tmlog "github.com/tendermint/tendermint/libs/log"
tmquery "github.com/tendermint/tendermint/libs/pubsub/query"
tmclihttp "github.com/tendermint/tendermint/rpc/client/http"
tmctypes "github.com/tendermint/tendermint/rpc/core/types"
"github.com/tendermint/tendermint/types"
tmtypes "github.com/tendermint/tendermint/types"
)
var ErrEmptyClientAddr = errors.New("abci client addr is empty in config")
type Client struct {
tmclt *tmclihttp.HTTP
genesisDoc *cachedGenesisDoc
}
func NewClient(addr string) (*Client, error) {
if len(addr) <= 0 {
return nil, ErrEmptyClientAddr
}
clt, err := tmclihttp.New(addr, "/websocket")
if err != nil {
return nil, err
}
// log errors only
clt.Logger = tmlog.NewFilter(
tmlog.NewTMLogger(os.Stdout),
tmlog.AllowError(),
)
return &Client{
tmclt: clt,
genesisDoc: newCachedGenesisDoc(),
}, nil
}
func (c *Client) SendTransactionAsync(ctx context.Context, bytes []byte) (*tmctypes.ResultBroadcastTx, error) {
// Fire off the transaction for consensus
return c.tmclt.BroadcastTxAsync(ctx, bytes)
}
func (c *Client) CheckTransaction(ctx context.Context, bytes []byte) (*tmctypes.ResultCheckTx, error) {
return c.tmclt.CheckTx(ctx, bytes)
}
func (c *Client) SendTransactionSync(ctx context.Context, bytes []byte) (*tmctypes.ResultBroadcastTx, error) {
// Fire off the transaction for consensus
return c.tmclt.BroadcastTxSync(ctx, bytes)
}
func (c *Client) SendTransactionCommit(ctx context.Context, bytes []byte) (*tmctypes.ResultBroadcastTxCommit, error) {
// Fire off the transaction for consensus
return c.tmclt.BroadcastTxCommit(ctx, bytes)
}
// GetGenesisTime retrieves the genesis time from the blockchain.
func (c *Client) GetGenesisTime(ctx context.Context) (genesisTime time.Time, err error) {
genDoc, err := c.genesisDoc.Get(ctx, c.tmclt)
if err != nil {
return time.Time{}, err
}
return genDoc.GenesisTime.UTC(), nil
}
// GetChainID retrieves the chainID from the blockchain.
func (c *Client) GetChainID(ctx context.Context) (chainID string, err error) {
genDoc, err := c.genesisDoc.Get(ctx, c.tmclt)
if err != nil {
return "", err
}
return genDoc.ChainID, nil
}
// GetStatus returns the current status of the chain.
func (c *Client) GetStatus(ctx context.Context) (status *tmctypes.ResultStatus, err error) {
return c.tmclt.Status(ctx)
}
// GetNetworkInfo return information of the current network.
func (c *Client) GetNetworkInfo(ctx context.Context) (netInfo *tmctypes.ResultNetInfo, err error) {
return c.tmclt.NetInfo(ctx)
}
// GetUnconfirmedTxCount return the current count of unconfirmed transactions.
func (c *Client) GetUnconfirmedTxCount(ctx context.Context) (count int, err error) {
res, err := c.tmclt.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 *Client) Health(ctx context.Context) (*tmctypes.ResultHealth, error) {
return c.tmclt.Health(ctx)
}
func (c *Client) Validators(ctx context.Context, height *int64) ([]*tmtypes.Validator, error) {
res, err := c.tmclt.Validators(ctx, height, nil, nil)
if err != nil {
return nil, err
}
return res.Validators, nil
}
func (c *Client) Genesis(ctx context.Context) (*tmtypes.GenesisDoc, error) {
genDoc, err := c.genesisDoc.Get(ctx, c.tmclt)
if err != nil {
return nil, err
}
return genDoc, nil
}
func (c *Client) GenesisValidators(ctx context.Context) ([]*tmtypes.Validator, error) {
gen, err := c.genesisDoc.Get(ctx, c.tmclt)
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 *Client) Subscribe(ctx context.Context, fn func(tmctypes.ResultEvent) error, queries ...string) error {
if err := c.tmclt.Start(); err != nil {
return err
}
defer c.tmclt.Stop()
errCh := make(chan error)
for _, query := range queries {
q, err := tmquery.New(query)
if err != nil {
return err
}
// For subscription we use "vega" as the client name but it's ignored by the implementation.
// 10 is the channel capacity which is absolutely arbitraty.
out, err := c.tmclt.Subscribe(ctx, "vega", 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.tmclt.UnsubscribeAll(context.Background(), "vega")
return <-errCh
}
func (c *Client) Start() error {
return nil // Nothing to do for this client type.
}
type cachedGenesisDoc struct {
mu sync.Mutex
genesisCache *tmtypes.GenesisDoc
}
func newCachedGenesisDoc() *cachedGenesisDoc {
return &cachedGenesisDoc{}
}
func (c *cachedGenesisDoc) Get(
ctx context.Context,
clt interface {
GenesisChunked(context.Context, uint) (*tmctypes.ResultGenesisChunk, error)
},
) (*tmtypes.GenesisDoc, error) {
c.mu.Lock()
defer c.mu.Unlock()
if c.genesisCache == nil {
var err error
if c.genesisCache, err = c.cacheGenesis(ctx, clt); err != nil {
return nil, err
}
}
return c.genesisCache, nil
}
func (c *cachedGenesisDoc) cacheGenesis(
ctx context.Context,
clt interface {
GenesisChunked(context.Context, uint) (*tmctypes.ResultGenesisChunk, error)
},
) (*tmtypes.GenesisDoc, error) {
var (
res = &tmctypes.ResultGenesisChunk{
TotalChunks: 1, // just default to startup our for loop
}
buf []byte
err error
)
for i := 0; i < res.TotalChunks; i++ {
res, err = clt.GenesisChunked(ctx, uint(i))
if err != nil {
return nil, err
}
decoded, err := base64.StdEncoding.DecodeString(res.Data)
if err != nil {
return nil, err
}
buf = append(buf, decoded...)
}
genDoc := types.GenesisDoc{}
err = cmtjson.Unmarshal(buf, &genDoc)
if err != nil {
return nil, err
}
return &genDoc, nil
}