-
Notifications
You must be signed in to change notification settings - Fork 3
/
herpc.go
108 lines (91 loc) · 2.8 KB
/
herpc.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
package hiveenginego
import (
"errors"
"github.com/cfoxon/jrc"
"strconv"
)
type HiveEngineRpcNode struct {
Endpoints engineApiEndpoints
RpcNode rpcServer
}
type rpcServer struct {
address string
MaxConn int
MaxBatch int
UseFast bool
}
type herpcQuery struct {
method string
params interface{}
}
type engineApiEndpoints struct {
Blockchain string
Contracts string
}
func NewHiveEngineRpc(addr string) *HiveEngineRpcNode {
return NewHiveEngineRpcWithOpts(addr, "/blockchain", "/contracts", 1, 4)
}
func NewHiveEngineRpcWithOpts(addr string, blockchain string, contracts string, maxConn int, maxBatch int) *HiveEngineRpcNode {
return &HiveEngineRpcNode{
Endpoints: engineApiEndpoints{
Blockchain: blockchain,
Contracts: contracts,
},
RpcNode: rpcServer{
address: addr,
MaxConn: maxConn,
MaxBatch: maxBatch},
}
}
func (h *HiveEngineRpcNode) rpcExec(endpoint string, query herpcQuery) ([]byte, error) {
rpcClient, err := jrc.NewServer(h.RpcNode.address+endpoint, jrc.MaxCon(h.RpcNode.MaxConn), jrc.MaxBatch(h.RpcNode.MaxBatch))
if err != nil {
return nil, err
}
jr2query := jrc.RpcRequest{Method: query.method, JsonRpc: "2.0", Id: 1, Params: query.params}
resp, err := rpcClient.Exec(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 *HiveEngineRpcNode) rpcExecBatch(endpoint string, queries []herpcQuery) ([][]byte, error) {
rpcClient, err := jrc.NewServer(h.RpcNode.address+endpoint, jrc.MaxCon(h.RpcNode.MaxConn), jrc.MaxBatch(h.RpcNode.MaxBatch))
var jr2queries jrc.RPCRequests
for i, query := range queries {
jr2query := &jrc.RpcRequest{Method: query.method, JsonRpc: "2.0", Id: i, Params: query.params}
jr2queries = append(jr2queries, jr2query)
}
resps, err := rpcClient.ExecBatch(jr2queries)
if err != nil {
return nil, err
}
var batchResult [][]byte
for _, resp := range resps {
if resp.Error != nil {
return nil, errors.New(strconv.Itoa(resp.Error.Code) + " " + resp.Error.Message)
}
batchResult = append(batchResult, resp.Result)
}
return batchResult, nil
}
func (h *HiveEngineRpcNode) rpcExecBatchFast(endpoint string, queries []herpcQuery) ([][]byte, error) {
rpcClient, err := jrc.NewServer(h.RpcNode.address+endpoint, jrc.MaxCon(h.RpcNode.MaxConn), jrc.MaxBatch(h.RpcNode.MaxBatch))
var jr2queries jrc.RPCRequests
for i, query := range queries {
jr2query := &jrc.RpcRequest{Method: query.method, JsonRpc: "2.0", Id: i + 1, Params: query.params}
jr2queries = append(jr2queries, jr2query)
}
resps, err := rpcClient.ExecBatchFast(jr2queries)
if err != nil {
return nil, err
}
var batchResult [][]byte
for _, resp := range resps {
batchResult = append(batchResult, resp)
}
return batchResult, nil
}