forked from forbole/juno
/
node.go
271 lines (222 loc) · 6.88 KB
/
node.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
264
265
266
267
268
269
270
271
package remote
import (
"context"
"encoding/base64"
"fmt"
"net/http"
"strings"
"time"
tmtypes "github.com/tendermint/tendermint/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"google.golang.org/grpc"
constypes "github.com/tendermint/tendermint/consensus/types"
tmjson "github.com/tendermint/tendermint/libs/json"
"github.com/Fueav/juno/node"
"github.com/cosmos/cosmos-sdk/types/tx"
"github.com/Fueav/juno/types"
httpclient "github.com/tendermint/tendermint/rpc/client/http"
tmctypes "github.com/tendermint/tendermint/rpc/core/types"
jsonrpcclient "github.com/tendermint/tendermint/rpc/jsonrpc/client"
)
var (
_ node.Node = &Node{}
)
// Node implements a wrapper around both a Tendermint RPCConfig client and a
// chain SDK REST client that allows for essential data queries.
type Node struct {
ctx context.Context
codec codec.Codec
client *httpclient.HTTP
txServiceClient tx.ServiceClient
grpcConnection *grpc.ClientConn
}
// NewNode allows to build a new Node instance
func NewNode(cfg *Details, codec codec.Codec) (*Node, error) {
httpClient, err := jsonrpcclient.DefaultHTTPClient(cfg.RPC.Address)
if err != nil {
return nil, err
}
// Tweak the transport
httpTransport, ok := (httpClient.Transport).(*http.Transport)
if !ok {
return nil, fmt.Errorf("invalid HTTP Transport: %T", httpTransport)
}
httpTransport.MaxConnsPerHost = cfg.RPC.MaxConnections
rpcClient, err := httpclient.NewWithClient(cfg.RPC.Address, "/websocket", httpClient)
if err != nil {
return nil, err
}
err = rpcClient.Start()
if err != nil {
return nil, err
}
grpcConnection, err := CreateGrpcConnection(cfg.GRPC)
if err != nil {
return nil, err
}
return &Node{
ctx: context.Background(),
codec: codec,
client: rpcClient,
txServiceClient: tx.NewServiceClient(grpcConnection),
grpcConnection: grpcConnection,
}, nil
}
// Genesis implements node.Node
func (cp *Node) Genesis() (*tmctypes.ResultGenesis, error) {
res, err := cp.client.Genesis(cp.ctx)
if err != nil && strings.Contains(err.Error(), "use the genesis_chunked API instead") {
return cp.getGenesisChunked()
}
return res, err
}
// getGenesisChunked gets the genesis data using the chinked API instead
func (cp *Node) getGenesisChunked() (*tmctypes.ResultGenesis, error) {
bz, err := cp.getGenesisChunksStartingFrom(0)
if err != nil {
return nil, err
}
var genDoc *tmtypes.GenesisDoc
err = tmjson.Unmarshal(bz, &genDoc)
if err != nil {
return nil, err
}
return &tmctypes.ResultGenesis{Genesis: genDoc}, nil
}
// getGenesisChunksStartingFrom returns all the genesis chunks data starting from the chunk with the given id
func (cp *Node) getGenesisChunksStartingFrom(id uint) ([]byte, error) {
res, err := cp.client.GenesisChunked(cp.ctx, id)
if err != nil {
return nil, fmt.Errorf("error while getting genesis chunk %d out of %d", id, res.TotalChunks)
}
bz, err := base64.StdEncoding.DecodeString(res.Data)
if err != nil {
return nil, fmt.Errorf("error while decoding genesis chunk %d out of %d", id, res.TotalChunks)
}
if id == uint(res.TotalChunks-1) {
return bz, nil
}
nextChunk, err := cp.getGenesisChunksStartingFrom(id + 1)
if err != nil {
return nil, err
}
return append(bz, nextChunk...), nil
}
// ConsensusState implements node.Node
func (cp *Node) ConsensusState() (*constypes.RoundStateSimple, error) {
state, err := cp.client.ConsensusState(context.Background())
if err != nil {
return nil, err
}
var data constypes.RoundStateSimple
err = tmjson.Unmarshal(state.RoundState, &data)
if err != nil {
return nil, err
}
return &data, nil
}
// LatestHeight implements node.Node
func (cp *Node) LatestHeight() (int64, error) {
status, err := cp.client.Status(cp.ctx)
if err != nil {
return -1, err
}
height := status.SyncInfo.LatestBlockHeight
return height, nil
}
// ChainID implements node.Node
func (cp *Node) ChainID() (string, error) {
status, err := cp.client.Status(cp.ctx)
if err != nil {
return "", err
}
chainID := status.NodeInfo.Network
return chainID, err
}
// Validators implements node.Node
func (cp *Node) Validators(height int64) (*tmctypes.ResultValidators, error) {
vals := &tmctypes.ResultValidators{
BlockHeight: height,
}
page := 1
perPage := 100 // maximum 100 entries per page
stop := false
for !stop {
result, err := cp.client.Validators(cp.ctx, &height, &page, &perPage)
if err != nil {
return nil, err
}
vals.Validators = append(vals.Validators, result.Validators...)
vals.Count += result.Count
vals.Total = result.Total
page += 1
stop = vals.Count == vals.Total
}
return vals, nil
}
// Block implements node.Node
func (cp *Node) Block(height int64) (*tmctypes.ResultBlock, error) {
return cp.client.Block(cp.ctx, &height)
}
// BlockResults implements node.Node
func (cp *Node) BlockResults(height int64) (*tmctypes.ResultBlockResults, error) {
return cp.client.BlockResults(cp.ctx, &height)
}
// Tx implements node.Node
func (cp *Node) Tx(hash string) (*types.Tx, error) {
res, err := cp.txServiceClient.GetTx(context.Background(), &tx.GetTxRequest{Hash: hash})
if err != nil {
return nil, err
}
// Decode messages
for _, msg := range res.Tx.Body.Messages {
var stdMsg sdk.Msg
err = cp.codec.UnpackAny(msg, &stdMsg)
if err != nil {
return nil, fmt.Errorf("error while unpacking message: %s", err)
}
}
convTx, err := types.NewTx(res.TxResponse, res.Tx)
if err != nil {
return nil, fmt.Errorf("error converting transaction: %s", err.Error())
}
return convTx, nil
}
// Txs implements node.Node
func (cp *Node) Txs(block *tmctypes.ResultBlock) ([]*types.Tx, error) {
txResponses := make([]*types.Tx, len(block.Block.Txs))
for i, tmTx := range block.Block.Txs {
txResponse, err := cp.Tx(fmt.Sprintf("%X", tmTx.Hash()))
if err != nil {
return nil, err
}
txResponses[i] = txResponse
}
return txResponses, nil
}
// TxSearch implements node.Node
func (cp *Node) TxSearch(query string, page *int, perPage *int, orderBy string) (*tmctypes.ResultTxSearch, error) {
return cp.client.TxSearch(cp.ctx, query, false, page, perPage, orderBy)
}
// SubscribeEvents implements node.Node
func (cp *Node) SubscribeEvents(subscriber, query string) (<-chan tmctypes.ResultEvent, context.CancelFunc, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
eventCh, err := cp.client.Subscribe(ctx, subscriber, query)
return eventCh, cancel, err
}
// SubscribeNewBlocks implements node.Node
func (cp *Node) SubscribeNewBlocks(subscriber string) (<-chan tmctypes.ResultEvent, context.CancelFunc, error) {
return cp.SubscribeEvents(subscriber, "tm.event = 'NewBlock'")
}
// Stop implements node.Node
func (cp *Node) Stop() {
err := cp.client.Stop()
if err != nil {
panic(fmt.Errorf("error while stopping proxy: %s", err))
}
err = cp.grpcConnection.Close()
if err != nil {
panic(fmt.Errorf("error while closing gRPC connection: %s", err))
}
}