-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhrpcclient.go
111 lines (90 loc) · 2.8 KB
/
hrpcclient.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
package hivego
import (
"encoding/json"
"errors"
"log"
"strconv"
"github.com/cfoxon/jsonrpc2client"
)
type HiveRpcNode struct {
address string
MaxConn int
MaxBatch int
NoBroadcast bool
}
type globalProps struct {
HeadBlockNumber int `json:"head_block_number"`
HeadBlockId string `json:"head_block_id"`
Time string `json:"time"`
}
type hrpcQuery struct {
method string
params interface{}
}
func NewHiveRpc(addr string) *HiveRpcNode {
return NewHiveRpcWithOpts(addr, 1, 4)
}
func NewHiveRpcWithOpts(addr string, maxConn int, maxBatch int) *HiveRpcNode {
return &HiveRpcNode{address: addr,
MaxConn: maxConn,
MaxBatch: maxBatch,
}
}
func (h *HiveRpcNode) GetDynamicGlobalProps() ([]byte, error) {
q := hrpcQuery{method: "condenser_api.get_dynamic_global_properties", params: []string{}}
res, err := h.rpcExec(h.address, q)
if err != nil {
return nil, err
}
return res, nil
}
func (h *HiveRpcNode) rpcExec(endpoint string, query hrpcQuery) ([]byte, error) {
rpcClient := jsonrpc2client.NewClientWithOpts(endpoint, h.MaxConn, h.MaxBatch)
jr2query := &jsonrpc2client.RpcRequest{Method: query.method, JsonRpc: "2.0", Id: 1, Params: query.params}
resp, err := rpcClient.CallRaw(jr2query)
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, errors.New(strconv.Itoa(resp.Error.Code) + " " + resp.Error.Message)
}
return resp.Result, nil
}
func (h *HiveRpcNode) rpcExecBatch(endpoint string, queries []hrpcQuery) ([]json.RawMessage, error) {
rpcClient := jsonrpc2client.NewClientWithOpts(endpoint, h.MaxConn, h.MaxBatch)
var jr2queries jsonrpc2client.RPCRequests
for i, query := range queries {
jr2query := &jsonrpc2client.RpcRequest{Method: query.method, JsonRpc: "2.0", Id: i, Params: query.params}
jr2queries = append(jr2queries, jr2query)
}
resps, err := rpcClient.CallBatchRaw(jr2queries)
if err != nil {
return nil, err
}
var batchResult []json.RawMessage
for _, resp := range resps {
thisResult := json.RawMessage{}
if err := json.Unmarshal(resp.Result, &thisResult); err != nil {
log.Println("err unmarshalling res.result")
log.Println(err)
log.Println(resp)
}
batchResult = append(batchResult, thisResult)
}
return batchResult, nil
}
func (h *HiveRpcNode) rpcExecBatchFast(endpoint string, queries []hrpcQuery) ([][]byte, error) {
rpcClient := jsonrpc2client.NewClientWithOpts(endpoint, h.MaxConn, h.MaxBatch)
var jr2queries jsonrpc2client.RPCRequests
for i, query := range queries {
jr2query := &jsonrpc2client.RpcRequest{Method: query.method, JsonRpc: "2.0", Id: i, Params: query.params}
jr2queries = append(jr2queries, jr2query)
}
resps, err := rpcClient.CallBatchFast(jr2queries)
if err != nil {
return nil, err
}
var batchResult [][]byte
batchResult = append(batchResult, resps...)
return batchResult, nil
}