-
Notifications
You must be signed in to change notification settings - Fork 32
/
AuxJobMaker.go
353 lines (302 loc) · 10.6 KB
/
AuxJobMaker.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package main
import (
"container/list"
"encoding/hex"
"errors"
"sync"
"time"
"github.com/golang/glog"
"merkle-tree-and-bitcoin/hash"
"merkle-tree-and-bitcoin/merkle"
zmq "github.com/pebbe/zmq4"
)
// AuxPowInfo 辅助工作量证明的信息
// @see <https://en.bitcoin.it/wiki/Merged_mining_specification#Aux_proof-of-work_block>
type AuxPowInfo struct {
Height uint32
Hash hash.Byte32
Target hash.Byte32
BlockchainBranch merkle.MerklePath
}
// AuxPowJob 合并挖矿的任务
type AuxPowJob struct {
MinBits string
MaxTarget hash.Byte32
MerkleRoot hash.Byte32
MerkleSize uint32
MerkleNonce uint32
AuxPows map[int]AuxPowInfo
// padding to RPC response
PrevBlockHash hash.Byte32
CoinbaseValue uint64
Height uint32
}
// AuxJobMaker 辅助挖矿任务生成器
type AuxJobMaker struct {
chains []ChainRPCInfo
currentAuxBlocks map[int]AuxBlockInfo
auxPowJobs map[hash.Byte32]AuxPowJob
auxPowJobIndex list.List
config AuxJobMakerInfo
lock sync.Mutex
merkleNonce uint32
merkleSize uint32
chainIDIndexSlots map[uint32]uint32
minJobBits string
maxJobTarget hash.Byte32
blockHashChnel chan string
}
// NewAuxJobMaker 创建辅助挖矿任务构造器
func NewAuxJobMaker(config AuxJobMakerInfo, chains []ChainRPCInfo) (maker *AuxJobMaker) {
maker = new(AuxJobMaker)
maker.chains = chains
maker.currentAuxBlocks = make(map[int]AuxBlockInfo)
maker.auxPowJobs = make(map[hash.Byte32]AuxPowJob)
maker.config = config
// set max job target and min job bits
if len(config.MaxJobTarget) != 64 {
// unlimited
config.MaxJobTarget = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
}
hexBytes, _ := hex.DecodeString(config.MaxJobTarget)
maker.maxJobTarget.Assign(hexBytes)
maker.minJobBits, _ = TargetToBits(maker.maxJobTarget.Hex())
glog.Info("Max Job Target: ", maker.maxJobTarget.Hex(), ", Bits: ", maker.minJobBits)
maker.blockHashChnel = make(chan string)
return
}
// Run 运行辅助挖矿任务构造器
func (maker *AuxJobMaker) Run() {
maker.updateAuxBlockAllChains()
}
// GetAuxJob 获取辅助挖矿任务
func (maker *AuxJobMaker) GetAuxJob() (job AuxPowJob, err error) {
job, err = maker.makeAuxJob()
if err != nil {
return
}
maker.lock.Lock()
defer maker.lock.Unlock()
_, exists := maker.auxPowJobs[job.MerkleRoot]
if exists {
if glog.V(2) {
glog.Info("[GetAuxJob] job not changed")
}
return
}
maker.auxPowJobs[job.MerkleRoot] = job
maker.auxPowJobIndex.PushBack(job.MerkleRoot)
if uint(maker.auxPowJobIndex.Len()) > maker.config.AuxPowJobListSize {
oldJobMerkleRoot := maker.auxPowJobIndex.Front()
maker.auxPowJobIndex.Remove(oldJobMerkleRoot)
delete(maker.auxPowJobs, oldJobMerkleRoot.Value.(hash.Byte32))
}
return
}
// FindAuxJob 查找辅助挖矿任务
func (maker *AuxJobMaker) FindAuxJob(merkleRoot hash.Byte32) (job AuxPowJob, err error) {
job, ok := maker.auxPowJobs[merkleRoot]
if !ok {
err = errors.New("AuxJob " + merkleRoot.Hex() + " not found")
}
return
}
// updateAuxBlock 更新辅助区块
func (maker *AuxJobMaker) updateAuxBlock(index int) {
chain := maker.chains[index]
auxBlockInfo, err := RPCCallCreateAuxBlock(chain)
if err != nil {
glog.Warning("CreateAuxBlock for ", chain.Name, " failed: ", err)
return
}
maker.lock.Lock()
// 检查chainID是否更新,如已更新(或oldAuxBlock不存在),则重置chainIDIndex
oldAuxBlock, ok := maker.currentAuxBlocks[index]
if !ok || oldAuxBlock.ChainID != auxBlockInfo.ChainID {
maker.chainIDIndexSlots = nil
}
oldAuxBlockInfo := maker.currentAuxBlocks[index];
maker.currentAuxBlocks[index] = auxBlockInfo
if auxBlockInfo.Height > oldAuxBlockInfo.Height {
glog.Info("send blockhash : ", auxBlockInfo.Hash.Hex())
auxBlockInfo.Hash = auxBlockInfo.Hash.Reverse()
maker.blockHashChnel <- auxBlockInfo.Hash.Hex()
auxBlockInfo.Hash = auxBlockInfo.Hash.Reverse()
}
maker.lock.Unlock()
if glog.V(3) {
glog.Info("[UpdateAuxBlock] <", chain.Name, "> height:", auxBlockInfo.Height, ", bits:", auxBlockInfo.Bits, ", target:", auxBlockInfo.Target.Hex(),
", coinbaseValue:", auxBlockInfo.CoinbaseValue, ", hash:", auxBlockInfo.Hash.Hex(), ", prevHash:", auxBlockInfo.PrevBlockHash.Hex())
}
}
// updateAuxBlockAllChains 持续更新所有链的辅助区块
func (maker *AuxJobMaker) updateAuxBlockAllChains() {
go func () {
txHashChnel := make(chan string)
defer close(txHashChnel)
contextpub, errpubctx := zmq.NewContext()
if errpubctx != nil {
glog.Info("[error] create context failed : ", errpubctx)
}
notifyPublisher, err := contextpub.NewSocket(zmq.PUB)
defer notifyPublisher.Close()
if err != nil {
glog.Info(" create notifyPublisher handle failed!", err)
return
}
address := "tcp://127.0.0.1:" + maker.config.BlockHashPublishPort
glog.Info("notifyPublisher address : ", address)
err = notifyPublisher.Bind(address)
if err != nil {
glog.Info(" bind notifyPublisher handle failed!", err)
return
}
go func (out chan<- string) {
for {
time.Sleep(time.Duration(maker.config.CreateAuxBlockIntervalSeconds) * time.Second)
out <- "connect ok!"
}
}(txHashChnel)
for {
select {
case txhashmsg := <- txHashChnel:
notifyPublisher.Send("hashtx", zmq.SNDMORE)
notifyPublisher.Send(txhashmsg, 0)
case blockhashmsg := <- maker.blockHashChnel:
hashByte, _ := hex.DecodeString(blockhashmsg)
notifyPublisher.Send("hashblock", zmq.SNDMORE)
notifyPublisher.SendBytes(hashByte, 0)
}
}
}()
for i := 0; i < len(maker.chains); i++ {
go func(index int) {
zmqsignalchanel := make(chan string)
timeoutchanel := make(chan string)
go func(out chan<- string) {
chainsupportzmq := maker.chains[index].IsSupportZmq
contextsub, errsubctx := zmq.NewContext()
if errsubctx != nil {
glog.Info("[error] ", maker.chains[index].Name, " create context failed : ", errsubctx)
}
subscriber, fail:= contextsub.NewSocket(zmq.SUB)
if fail != nil {
glog.Info("[error] ", maker.chains[index].Name, " create socket failed : ", fail)
}
connected := true
defer subscriber.Close()
if chainsupportzmq {
ip := maker.chains[index].SubBlockHashAddress
port := maker.chains[index].SubBlockHashPort
address := "tcp://" + ip + ":"+ port
glog.Info("address : ",address)
err := subscriber.Connect(address)
if err != nil {
glog.Info("[error] ", maker.chains[index].Name, " cannot connect to : ", address)
connected = false
}
glog.Info("[OK] ", maker.chains[index].Name, " connected to : ", address)
subscriber.SetSubscribe("hashblock")
}
if chainsupportzmq && connected {
for {
msgtype, err := subscriber.Recv(0)
if err != nil {
glog.Info("[error] when ", maker.chains[index].Name, " recv type msg ", msgtype)
continue
}
glog.Info("[OK] ", maker.chains[index].Name, " receive msgtype : ", msgtype)
content, e := subscriber.Recv(0)
if e != nil {
glog.Info("[error] when ", maker.chains[index].Name, " recv content msg ", content)
continue
}
glog.Info("[OK] ", maker.chains[index].Name, " receive first msgcontent : ", content)
content, e = subscriber.Recv(0)
if e != nil {
glog.Info("[error] when ", maker.chains[index].Name, " recv content msg ", content)
continue
}
glog.Info("[OK] ", maker.chains[index].Name, " receive second msgcontent : ", content)
if msgtype != "hashblock" {
glog.Info("[ERROR] ", maker.chains[index].Name, " receive msgcontent : ", msgtype, "is not hashblock")
continue
}
out <- "ok"
}
}
}(zmqsignalchanel)
go func(out chan<- string) {
for {
time.Sleep(time.Duration(maker.config.CreateAuxBlockIntervalSeconds) * time.Second)
out <- "ok"
}
}(timeoutchanel)
for {
select {
case <- zmqsignalchanel:
glog.Info("[ok] recv msg from zmq chanel ---> ")
maker.updateAuxBlock(index)
case <- timeoutchanel:
//glog.Info("[ok] recv msg from timeout chanel ")
maker.updateAuxBlock(index)
}
}
}(i)
}
}
// makeAuxJob 构造辅助挖矿任务
func (maker *AuxJobMaker) makeAuxJob() (job AuxPowJob, err error) {
maker.lock.Lock()
defer maker.lock.Unlock()
blockNum := len(maker.currentAuxBlocks)
if blockNum < 1 {
err = errors.New("makeAuxJob failed: currentAuxBlocks is empty")
return
}
if maker.chainIDIndexSlots == nil {
chainIDs := make(map[int]uint32)
for index, block := range maker.currentAuxBlocks {
chainIDs[index] = block.ChainID
}
maker.merkleNonce, maker.merkleSize, maker.chainIDIndexSlots, _ = assignChainSlots(chainIDs)
glog.Info("[AssignChainSlots] merkleNonce: ", maker.merkleNonce, ", merkleSize: ", maker.merkleSize,
", chainIDIndexSlots: ", maker.chainIDIndexSlots)
}
job.MerkleNonce = maker.merkleNonce
job.MerkleSize = maker.merkleSize
job.AuxPows = make(map[int]AuxPowInfo)
// set default value of Bits and Target
job.MinBits = maker.currentAuxBlocks[0].Bits
job.MaxTarget = maker.currentAuxBlocks[0].Target
// set fields that padding to response
job.PrevBlockHash = maker.currentAuxBlocks[0].PrevBlockHash
job.Height = maker.currentAuxBlocks[0].Height
job.CoinbaseValue = maker.currentAuxBlocks[0].CoinbaseValue
bottomRow := make(merkle.Row, maker.merkleSize)
for _, block := range maker.currentAuxBlocks {
bottomRow[maker.chainIDIndexSlots[block.ChainID]] = block.Hash
// the hex of the target larger, the difficulty of the job smaller
if block.Target.Hex() > job.MaxTarget.Hex() {
job.MaxTarget = block.Target
job.MinBits = block.Bits
}
}
merkleTree := merkle.NewMerkleTree(bottomRow)
job.MerkleRoot = merkleTree.MerkleRoot()
for index, block := range maker.currentAuxBlocks {
slot := int(maker.chainIDIndexSlots[block.ChainID])
var auxPow AuxPowInfo
auxPow.Height = block.Height
auxPow.Hash = block.Hash
auxPow.Target = block.Target
auxPow.BlockchainBranch = merkleTree.MerklePathForLeaf(slot)
job.AuxPows[index] = auxPow
}
if job.MaxTarget.Hex() > maker.maxJobTarget.Hex() {
glog.Info("Job target ", job.MaxTarget.Hex(), " too high, replaced to ", maker.maxJobTarget.Hex())
job.MaxTarget = maker.maxJobTarget
job.MinBits = maker.minJobBits
}
return
}