-
Notifications
You must be signed in to change notification settings - Fork 3
/
blockchain.go
142 lines (111 loc) · 3.56 KB
/
blockchain.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
package hiveenginego
import (
"encoding/json"
)
type EngineStatus struct {
LastBlockNumber int `json:"lastBlockNumber"`
LastBlockRefHiveBlockNumber int `json:"lastBlockRefHiveBlockNumber"`
LastParsedHiveBlockNumber int `json:"lastParsedHiveBlockNumber"`
LastVerifiedBlockNumber int `json:"lastVerifiedBlockNumber"`
SSCnodeVersion string `json:"SSCnodeVersion"`
ChainId string `json:"chainId"`
Domain string `json:"domain"`
Lightnode bool `json:"lightnode"`
LastHash string `json:"lastHash"`
}
type EngineBlock struct {
BlockNumber int `json:"blockNumber"`
RefHiveBlockNumber int `json:"refHiveBlockNumber"`
Timestamp string `json:"timestamp"`
Transactions json.RawMessage `json:"transactions"`
VirtualTransactions json.RawMessage `json:"virtualTransactions,omitempty"`
Hash string `json:"hash"`
DatabaseHash string `json:"databasehash"`
MerkleRoot string `json:"merkleRoot"`
Round int `json:"round"`
RoundHash string `json:"roundHash"`
Witness string `json:"witness"`
SigningKey string `json:"signingKey"`
RoundSignature string `json:"roundSignature"`
}
type blockChainQueryParams struct {
BlockNumber int `json:"blockNumber"`
}
func (h HiveEngineRpcNode) GetStatus() (*EngineStatus, error) {
if len(h.Endpoints.Blockchain) == 0 {
h.Endpoints.Blockchain = "/blockchain"
}
endpoint := h.Endpoints.Blockchain
query := herpcQuery{method: "getStatus"}
res, err := h.rpcExec(endpoint, query)
if err != nil {
return nil, err
}
status := &EngineStatus{}
if err := json.Unmarshal(res, &status); err != nil {
return nil, err
}
return status, nil
}
func (h HiveEngineRpcNode) GetLatestBlockInfo() (*EngineBlock, error) {
if len(h.Endpoints.Blockchain) == 0 {
h.Endpoints.Blockchain = "/blockchain"
}
endpoint := h.Endpoints.Blockchain
query := herpcQuery{method: "getLatestBlockInfo"}
res, err := h.rpcExec(endpoint, query)
if err != nil {
return nil, err
}
block := &EngineBlock{}
if err := json.Unmarshal(res, &block); err != nil {
return nil, err
}
return block, nil
}
func (h HiveEngineRpcNode) GetBlockRange(startBlock int, endBlock int) ([][]byte, error) {
if len(h.Endpoints.Blockchain) == 0 {
h.Endpoints.Blockchain = "/blockchain"
}
if h.RpcNode.MaxConn == 0 {
h.RpcNode.MaxConn = 1
}
if h.RpcNode.MaxBatch == 0 {
h.RpcNode.MaxBatch = 2
}
var queries []herpcQuery
for i := startBlock; i <= endBlock; i++ {
params := blockChainQueryParams{BlockNumber: i}
query := herpcQuery{method: "getBlockInfo", params: params}
queries = append(queries, query)
}
endpoint := h.Endpoints.Blockchain
res, err := h.rpcExecBatch(endpoint, queries)
if err != nil {
return nil, err
}
return res, nil
}
func (h HiveEngineRpcNode) GetBlockRangeFast(startBlock int, endBlock int) ([][]byte, error) {
if len(h.Endpoints.Blockchain) == 0 {
h.Endpoints.Blockchain = "/blockchain"
}
if h.RpcNode.MaxConn == 0 {
h.RpcNode.MaxConn = 1
}
if h.RpcNode.MaxBatch == 0 {
h.RpcNode.MaxBatch = 2
}
var queries []herpcQuery
for i := startBlock; i <= endBlock; i++ {
params := blockChainQueryParams{BlockNumber: i}
query := herpcQuery{method: "getBlockInfo", params: params}
queries = append(queries, query)
}
endpoint := h.Endpoints.Blockchain
res, err := h.rpcExecBatchFast(endpoint, queries)
if err != nil {
return nil, err
}
return res, nil
}