forked from forbole/juno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
205 lines (167 loc) · 5.14 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
package remote
import (
"context"
"fmt"
"net/http"
"time"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
constypes "github.com/tendermint/tendermint/consensus/types"
tmjson "github.com/tendermint/tendermint/libs/json"
"github.com/pylons-tech/juno/node"
"github.com/cosmos/cosmos-sdk/types/tx"
"github.com/pylons-tech/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
}
// 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),
}, nil
}
// Genesis implements node.Node
func (cp *Node) Genesis() (*tmctypes.ResultGenesis, error) {
return cp.client.Genesis(cp.ctx)
}
// 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
}
// Validators implements node.Node
func (cp *Node) Validators(height int64) (*tmctypes.ResultValidators, error) {
vals := &tmctypes.ResultValidators{
BlockHeight: height,
}
page := 1
perPage := 100
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 == len(vals.Validators)
}
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))
}
}