/
rpc.go
234 lines (214 loc) · 7.63 KB
/
rpc.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
package tendermint_rpc
import (
"crypto/ecdsa"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"github.com/chainpoint/chainpoint-core/types"
"github.com/tendermint/tendermint/libs/log"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
core_types "github.com/tendermint/tendermint/rpc/core/types"
"github.com/chainpoint/chainpoint-core/util"
)
// RPC : hold abstract http client for mocking purposes
type RPC struct {
client *rpchttp.HTTP
logger log.Logger
}
// NewRPCClient : Creates a new client connected to a tendermint instance at web socket "tendermintRPC"
func NewRPCClient(tendermintRPC types.TendermintConfig, logger log.Logger) (rpc *RPC) {
c, _ := rpchttp.NewWithTimeout(fmt.Sprintf("http://%s:%s", tendermintRPC.TMServer, tendermintRPC.TMPort), "/websocket", 2)
return &RPC{
client: c,
logger: logger,
}
}
//LogError : log tendermintRpc errors
func (rpc *RPC) LogError(err error) error {
if err != nil {
rpc.logger.Error(fmt.Sprintf("Error in %s: %s", util.GetCurrentFuncName(2), err.Error()))
}
return err
}
// BroadcastTx : Synchronously broadcasts a transaction to the local Tendermint node
func (rpc *RPC) BroadcastTx(txType string, data string, version int64, time int64, stackID string, privateKey *ecdsa.PrivateKey) (core_types.ResultBroadcastTx, error) {
tx := types.Tx{TxType: txType, Data: data, Version: version, Time: time, CoreID: stackID}
result, err := rpc.client.BroadcastTxSync([]byte(util.EncodeTxWithKey(tx, privateKey)))
if rpc.LogError(err) != nil {
return core_types.ResultBroadcastTx{}, err
}
return *result, nil
}
// BroadcastTx : Synchronously broadcasts a transaction to the local Tendermint node
func (rpc *RPC) BroadcastTxWithMeta(txType string, data string, version int64, time int64, stackID string, meta string, privateKey *ecdsa.PrivateKey) (core_types.ResultBroadcastTx, error) {
tx := types.Tx{TxType: txType, Data: data, Version: version, Time: time, CoreID: stackID, Meta: meta}
result, err := rpc.client.BroadcastTxSync([]byte(util.EncodeTxWithKey(tx, privateKey)))
if rpc.LogError(err) != nil {
return core_types.ResultBroadcastTx{}, err
}
return *result, nil
}
// BroadcastTxCommit : Synchronously broadcasts a transaction to the local Tendermint node THIS IS BLOCKING
func (rpc *RPC) BroadcastTxCommit(txType string, data string, version int64, time int64, stackID string, privateKey *ecdsa.PrivateKey) (core_types.ResultBroadcastTxCommit, error) {
tx := types.Tx{TxType: txType, Data: data, Version: version, Time: time, CoreID: stackID}
result, err := rpc.client.BroadcastTxCommit([]byte(util.EncodeTxWithKey(tx, privateKey)))
if rpc.LogError(err) != nil {
return core_types.ResultBroadcastTxCommit{}, err
}
return *result, nil
}
// GetStatus retrieves status of our node.
func (rpc *RPC) GetStatus() (core_types.ResultStatus, error) {
if rpc == nil {
return core_types.ResultStatus{}, errors.New("tendermintRpc failure")
}
status, err := rpc.client.Status()
if rpc.LogError(err) != nil {
return core_types.ResultStatus{}, err
}
return *status, err
}
// GetNetInfo retrieves known peer information.
func (rpc *RPC) GetNetInfo() (core_types.ResultNetInfo, error) {
if rpc == nil {
return core_types.ResultNetInfo{}, errors.New("tendermintRpc failure")
}
netInfo, err := rpc.client.NetInfo()
if rpc.LogError(err) != nil {
return core_types.ResultNetInfo{}, err
}
return *netInfo, err
}
//GetTxByInt : Retrieves a tx by its unique integer ID (txInt)
func (rpc *RPC) GetTxByInt(txInt int64) (core_types.ResultTxSearch, error) {
txResult, err := rpc.client.TxSearch(fmt.Sprintf("CAL.TxInt=%d", txInt), false, 1, 1, "")
if rpc.LogError(err) != nil {
return core_types.ResultTxSearch{}, err
}
return *txResult, err
}
//GetTxByHash : Retrieves a tx by its unique string ID (txid)
func (rpc *RPC) GetTxByHash(txid string) (core_types.ResultTx, error) {
hash, err := hex.DecodeString(txid)
if rpc.LogError(err) != nil {
return core_types.ResultTx{}, err
}
txResult, err := rpc.client.Tx(hash, false)
if rpc.LogError(err) != nil {
return core_types.ResultTx{}, err
}
return *txResult, err
}
// GetAbciInfo retrieves custom ABCI status struct detailing the state of our application
func (rpc *RPC) GetAbciInfo() (types.AnchorState, error) {
resp, err := rpc.client.ABCIInfo()
if rpc.LogError(err) != nil {
return types.AnchorState{}, err
}
var anchorState types.AnchorState
util.LogError(json.Unmarshal([]byte(resp.Response.Data), &anchorState))
return anchorState, nil
}
//GetValidators : retrieves list of validators at a particular block height
func (rpc *RPC) GetValidators(height int64) (core_types.ResultValidators, error) {
resp, err := rpc.client.Validators(&height, 1, 300)
if rpc.LogError(err) != nil {
return core_types.ResultValidators{}, err
}
return *resp, nil
}
//GetGenesis : retrieves genesis file for initialization
func (rpc *RPC) GetGenesis() (core_types.ResultGenesis, error) {
resp, err := rpc.client.Genesis()
if rpc.LogError(err) != nil {
return core_types.ResultGenesis{}, err
}
return *resp, nil
}
// GetTxRange gets all CAL TXs within a particular range
func (rpc *RPC) GetCalTxRange(minTxInt int64, maxTxInt int64) ([]core_types.ResultTx, error) {
if maxTxInt <= minTxInt {
return nil, errors.New("max of tx range is less than or equal to min")
}
Txs := []core_types.ResultTx{}
for i := minTxInt; i < maxTxInt; i++ {
txResult, err := rpc.client.TxSearch(fmt.Sprintf("CAL.TxInt=%d", i), false, 1, 1, "")
if err != nil {
return nil, err
} else if txResult.TotalCount > 0 {
for _, tx := range txResult.Txs {
Txs = append(Txs, *tx)
}
}
}
return Txs, nil
}
//GetAnchoringCore : gets core to whom last anchor is attributed
func (rpc *RPC) GetAnchoringCore(queryLine string) (string, error) {
txResult, err := rpc.client.TxSearch(queryLine, false, 1, 1, "")
if rpc.LogError(err) == nil {
for _, tx := range txResult.Txs {
decoded, err := util.DecodeTx(tx.Tx)
if rpc.LogError(err) != nil {
continue
}
return decoded.CoreID, nil
}
}
return "", err
}
// GetBTCCTx: retrieves and verifies existence of btcc tx
func (rpc *RPC) GetBTCCTx(btcMonObj types.BtcMonMsg) (hash []byte) {
btccQueryLine := fmt.Sprintf("BTC-C.BTCC='%s'", btcMonObj.BtcHeadRoot)
txResult, err := rpc.client.TxSearch(btccQueryLine, false, 1, 1, "")
if rpc.LogError(err) == nil {
for _, tx := range txResult.Txs {
hash = tx.Hash
rpc.logger.Info(fmt.Sprint("Found BTC-C Hash from confirmation leader: %v", hash))
}
}
return hash
}
// getAllJWKs gets all JWK TXs
func (rpc *RPC) GetAllJWKs() ([]types.Tx, error) {
Txs := []types.Tx{}
endPage := 2
for i := 1; i <= endPage; i++ {
txResult, err := rpc.client.TxSearch("JWK.CORE='NEW'", false, i, 100, "asc")
if err != nil {
return nil, err
} else if txResult.TotalCount > 0 {
rpc.logger.Info(fmt.Sprintf("Found %d JWK tx while loading", txResult.TotalCount))
for _, tx := range txResult.Txs {
decoded, err := util.DecodeTx(tx.Tx)
if rpc.LogError(err) == nil {
Txs = append(Txs, decoded)
}
}
}
endPage = (txResult.TotalCount / 100) + 1
}
return Txs, nil
}
// GetAllCHNGSTK gets all change stake txs
func (rpc *RPC) GetAllCHNGSTK() ([]types.Tx, error) {
Txs := []types.Tx{}
endPage := 2
for i := 1; i <= endPage; i++ {
txResult, err := rpc.client.TxSearch("CHNGSTK.CHANGE='STAKE'", false, i, 100, "")
if err != nil {
return nil, err
} else if txResult.TotalCount > 0 {
rpc.logger.Info(fmt.Sprintf("Found %d CHNGSTK tx while loading", txResult.TotalCount))
for _, tx := range txResult.Txs {
decoded, err := util.DecodeTx(tx.Tx)
if rpc.LogError(err) == nil {
Txs = append(Txs, decoded)
}
}
}
endPage = (txResult.TotalCount / 100) + 1
}
return Txs, nil
}