-
Notifications
You must be signed in to change notification settings - Fork 32
/
ChainRPC.go
221 lines (183 loc) · 5.57 KB
/
ChainRPC.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
package main
import (
"encoding/hex"
"encoding/json"
"errors"
"merkle-tree-and-bitcoin/hash"
)
// AuxBlockInfo 合并挖矿的区块信息
type AuxBlockInfo struct {
Hash hash.Byte32
ChainID uint32
Bits string
Target hash.Byte32
Height uint32
PrevBlockHash hash.Byte32
CoinbaseValue uint64
}
// RPCCallCreateAuxBlock 调用CreateAuxBlock方法
func RPCCallCreateAuxBlock(rpcInfo ChainRPCInfo) (auxBlockInfo AuxBlockInfo, err error) {
responseJSON, err := RPCCall(rpcInfo.RPCServer, rpcInfo.CreateAuxBlock.Method, rpcInfo.CreateAuxBlock.Params)
if err != nil {
return
}
response, err := ParseRPCResponse(responseJSON)
if response.Error != nil {
errBytes, _ := json.Marshal(response.Error)
err = errors.New(string(errBytes))
}
rpcRawResult, ok := response.Result.(map[string]interface{})
if !ok {
err = errors.New("RPC result is not a JSON object: " + string(responseJSON))
return
}
// ------------ Hash ------------
hashKey := rpcInfo.CreateAuxBlock.ResponseKeys.Hash
hash, ok := rpcRawResult[hashKey]
if !ok {
err = errors.New("rpc result: missing " + hashKey)
return
}
hashStr, ok := hash.(string)
if !ok {
err = errors.New("rpc result: " + hashKey + " is not a string")
return
}
hashByte, err := hex.DecodeString(hashStr)
if err != nil {
err = errors.New("rpc result: " + hashKey + " decode failed: " + err.Error())
return
}
if len(hashByte) != 32 {
err = errors.New("rpc result: " + hashKey + " is not a hex of 32 bytes")
return
}
auxBlockInfo.Hash.Assign(hashByte)
auxBlockInfo.Hash = auxBlockInfo.Hash.Reverse()
// ------------ ChainID ------------
chainIDKey := rpcInfo.CreateAuxBlock.ResponseKeys.ChainID
if len(chainIDKey) < 1 {
auxBlockInfo.ChainID = rpcInfo.ChainID
} else {
chainID, ok := rpcRawResult[chainIDKey]
if !ok {
err = errors.New("rpc result: missing " + chainIDKey)
return
}
chainIDFloat, ok := chainID.(float64)
if !ok {
err = errors.New("rpc result: " + chainIDKey + " is not a number")
return
}
auxBlockInfo.ChainID = uint32(chainIDFloat)
}
// ------------ Bits ------------
bitsKey := rpcInfo.CreateAuxBlock.ResponseKeys.Bits
if len(bitsKey) >= 1 {
bits, ok := rpcRawResult[bitsKey]
if !ok {
err = errors.New("rpc result: missing " + bitsKey)
return
}
auxBlockInfo.Bits, ok = bits.(string)
if !ok {
err = errors.New("rpc result: " + bitsKey + " is not a string")
return
}
}
// ------------ Target ------------
var targetStr string
targetKey := rpcInfo.CreateAuxBlock.ResponseKeys.Target
if len(targetKey) < 1 {
targetStr, err = BitsToTarget(auxBlockInfo.Bits)
if err != nil {
err = errors.New("rpc result: cannot convert bits (" + auxBlockInfo.Bits + ") to target: " + err.Error())
}
} else {
target, ok := rpcRawResult[targetKey]
if !ok {
err = errors.New("rpc result: missing " + targetKey)
return
}
targetStr, ok = target.(string)
if !ok {
err = errors.New("rpc result: target is not a string")
return
}
}
targetByte, err := hex.DecodeString(targetStr)
if err != nil {
err = errors.New("rpc result: targetStr (" + targetStr + ") decode failed: " + err.Error())
return
}
if len(targetByte) != 32 {
err = errors.New("rpc result: targetStr (" + targetStr + ") is not a hex of 32 bytes")
return
}
auxBlockInfo.Target.Assign(targetByte)
//----------Bits not exist-------
if len(bitsKey) < 1 {
auxBlockInfo.Bits, err = TargetToBits(targetStr)
if err != nil {
err = errors.New("rpc result: cannot convert Target ( " + targetStr + ") to bits: " + err.Error())
return
}
}
// ------------ Height ------------
heightKey := rpcInfo.CreateAuxBlock.ResponseKeys.Height
if len(heightKey) >= 1 {
height, ok := rpcRawResult[heightKey]
if !ok {
err = errors.New("rpc result: missing " + heightKey)
return
}
heightFloat, ok := height.(float64)
if !ok {
err = errors.New("rpc result: " + heightKey + " is not a number")
return
}
auxBlockInfo.Height = uint32(heightFloat)
}
// ------------ PrevBlockHash ------------
prevBlockHashKey := rpcInfo.CreateAuxBlock.ResponseKeys.PrevBlockHash
if len(prevBlockHashKey) >= 1 {
prevBlockHash, ok := rpcRawResult[prevBlockHashKey]
if !ok {
err = errors.New("rpc result: missing " + prevBlockHashKey)
return
}
prevBlockHashStr, ok := prevBlockHash.(string)
if !ok {
err = errors.New("rpc result: " + prevBlockHashKey + " is not a string")
return
}
var prevBlockHashByte []byte
prevBlockHashByte, err = hex.DecodeString(prevBlockHashStr)
if err != nil {
err = errors.New("rpc result: " + prevBlockHashKey + " decode failed: " + err.Error())
return
}
if len(prevBlockHashByte) != 32 {
err = errors.New("rpc result: " + prevBlockHashKey + " is not a hex of 32 bytes")
return
}
auxBlockInfo.PrevBlockHash.Assign(prevBlockHashByte)
}
// ------------ CoinbaseValue ------------
coinbaseValueKey := rpcInfo.CreateAuxBlock.ResponseKeys.CoinbaseValue
if len(coinbaseValueKey) >= 1 {
coinbaseValue, ok := rpcRawResult[coinbaseValueKey]
if !ok {
err = errors.New("rpc result: missing " + coinbaseValueKey)
return
}
coinbaseValueFloat, ok := coinbaseValue.(float64)
if !ok {
err = errors.New("rpc result: " + coinbaseValueKey + " is not a number")
return
}
auxBlockInfo.CoinbaseValue = uint64(coinbaseValueFloat)
}
// ------------ Finished ------------
return
}