-
Notifications
You must be signed in to change notification settings - Fork 14
/
pov_worker.go
418 lines (326 loc) · 11.6 KB
/
pov_worker.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
package miner
import (
"errors"
"fmt"
"sync"
"time"
"go.uber.org/zap"
"github.com/qlcchain/go-qlc/common"
"github.com/qlcchain/go-qlc/common/merkle"
"github.com/qlcchain/go-qlc/common/types"
"github.com/qlcchain/go-qlc/config"
"github.com/qlcchain/go-qlc/consensus/pov"
"github.com/qlcchain/go-qlc/log"
)
type PovWorker struct {
miner *Miner
logger *zap.SugaredLogger
maxTxPerBlock int
mineBlockPool map[types.Hash]*types.PovMineBlock
minerAlgoBlocks map[types.Address]map[types.PovAlgoType]*PovMinerAlgoBlock
lastMineHeight uint64
muxMineBlock sync.Mutex
handlerIds map[common.TopicType]string //topic->handler id
quitCh chan struct{}
}
type PovMinerAlgoBlock struct {
curMineBlock *types.PovMineBlock
lastMineTime time.Time
}
func NewPovWorker(miner *Miner) *PovWorker {
worker := &PovWorker{
miner: miner,
logger: log.NewLogger("pov_miner"),
handlerIds: make(map[common.TopicType]string),
quitCh: make(chan struct{}),
}
worker.mineBlockPool = make(map[types.Hash]*types.PovMineBlock)
worker.minerAlgoBlocks = make(map[types.Address]map[types.PovAlgoType]*PovMinerAlgoBlock)
return worker
}
func (w *PovWorker) Init() error {
blkHeader := &types.PovHeader{
AuxHdr: types.NewPovAuxHeader(),
CbTx: types.NewPovCoinBaseTx(1, 2),
}
blkHdrSize := blkHeader.Msgsize()
blkHdrSize += common.PovMaxCoinbaseExtraSize // cbtx extra
hash := &types.Hash{}
blkHdrSize += hash.Msgsize() * 32 * 2 // aux merkle branch + coinbase branch
tx := &types.PovTransaction{}
w.maxTxPerBlock = (common.PovChainBlockSize - blkHdrSize) / tx.Msgsize()
w.maxTxPerBlock = w.maxTxPerBlock - 1 // CoinBase TX
w.logger.Infof("MaxBlockSize:%d, MaxHeaderSize:%d, MaxTxSize:%d, MaxTxNum:%d",
common.PovChainBlockSize, blkHdrSize, tx.Msgsize(), w.maxTxPerBlock)
return nil
}
func (w *PovWorker) Start() error {
id, err := w.miner.eb.SubscribeSync(common.EventRpcSyncCall, w.OnEventRpcSyncCall)
if err != nil {
return err
}
w.handlerIds[common.EventRpcSyncCall] = id
return nil
}
func (w *PovWorker) Stop() error {
_ = w.miner.eb.Unsubscribe(common.EventRpcSyncCall, w.handlerIds[common.EventRpcSyncCall])
if w.quitCh != nil {
close(w.quitCh)
}
return nil
}
func (w *PovWorker) GetConfig() *config.Config {
return w.miner.GetConfig()
}
func (w *PovWorker) GetTxPool() *pov.PovTxPool {
return w.miner.GetPovEngine().GetTxPool()
}
func (w *PovWorker) GetChain() *pov.PovBlockChain {
return w.miner.GetPovEngine().GetChain()
}
func (w *PovWorker) GetPovConsensus() pov.ConsensusPov {
return w.miner.GetPovEngine().GetConsensus()
}
func (w *PovWorker) OnEventRpcSyncCall(name string, in interface{}, out interface{}) {
switch name {
case "Miner.GetWork":
w.GetWork(in, out)
case "Miner.SubmitWork":
w.SubmitWork(in, out)
case "Miner.GetMiningInfo":
w.GetMiningInfo(in, out)
}
}
func (w *PovWorker) GetWork(in interface{}, out interface{}) {
inArgs := in.(map[interface{}]interface{})
outArgs := out.(map[interface{}]interface{})
if w.miner.GetSyncState() != common.SyncDone {
outArgs["err"] = fmt.Errorf("miner pausing for sync state %s", w.miner.GetSyncState())
return
}
minerAddr := inArgs["minerAddr"].(types.Address)
algoName := inArgs["algoName"].(string)
algoType := types.NewPoVHashAlgoFromStr(algoName)
err := w.checkMinerPledge(minerAddr)
if err != nil {
outArgs["err"] = err
return
}
mineBlock, err := w.generateBlock(minerAddr, algoType)
if err != nil {
outArgs["err"] = err
return
}
outArgs["err"] = nil
outArgs["mineBlock"] = mineBlock
}
func (w *PovWorker) SubmitWork(in interface{}, out interface{}) {
inArgs := in.(map[interface{}]interface{})
outArgs := out.(map[interface{}]interface{})
result := inArgs["mineResult"].(*types.PovMineResult)
mineBlock := w.findBlockInPool(result.WorkHash)
if mineBlock == nil {
outArgs["err"] = errors.New("failed to find block by WorkHash")
return
}
err := w.checkAndFillBlockByResult(mineBlock, result)
if err != nil {
outArgs["err"] = err
return
}
w.submitBlock(mineBlock)
outArgs["err"] = nil
}
func (w *PovWorker) GetMiningInfo(in interface{}, out interface{}) {
//inArgs := in.(map[interface{}]interface{})
outArgs := out.(map[interface{}]interface{})
outArgs["syncState"] = int(w.miner.GetSyncState())
latestBlock := w.GetChain().LatestBlock()
outArgs["latestBlock"] = latestBlock
outArgs["pooledTx"] = w.GetTxPool().GetPendingTxNum()
outArgs["err"] = nil
}
func (w *PovWorker) newBlockTemplate(minerAddr types.Address, algoType types.PovAlgoType) (*types.PovMineBlock, error) {
latestHeader := w.GetChain().LatestHeader()
if latestHeader == nil {
return nil, fmt.Errorf("failed to get latest header")
}
w.logger.Debugf("make block template after latest %d/%s", latestHeader.GetHeight(), latestHeader.GetHash())
mineBlock := types.NewPovMineBlock()
if mineBlock.Header == nil || mineBlock.Block == nil {
return nil, fmt.Errorf("failed to new block")
}
if mineBlock.Header.CbTx == nil {
return nil, fmt.Errorf("failed to new coinbase tx")
}
// fill base header
header := mineBlock.Header
header.BasHdr.Version = types.POV_VBS_TOPBITS | uint32(algoType)
header.BasHdr.Previous = latestHeader.GetHash()
header.BasHdr.Height = latestHeader.GetHeight() + 1
header.BasHdr.Timestamp = uint32(time.Now().Unix())
prevStateHash := latestHeader.GetStateHash()
prevStateTrie := w.GetChain().GetStateTrie(&prevStateHash)
if prevStateTrie == nil {
return nil, fmt.Errorf("failed to get prev state trie %s", prevStateHash)
}
// coinbase tx
cbtx := header.CbTx
// pack account block txs
accBlocks := w.GetTxPool().SelectPendingTxs(prevStateTrie, w.maxTxPerBlock)
w.logger.Debugf("current block select pending txs %d", len(accBlocks))
var accTxHashes []*types.Hash
var accTxs []*types.PovTransaction
for _, accBlock := range accBlocks {
accTx := &types.PovTransaction{
Hash: accBlock.GetHash(),
Block: accBlock,
}
accTxHashes = append(accTxHashes, &accTx.Hash)
accTxs = append(accTxs, accTx)
}
err := w.GetPovConsensus().PrepareHeader(header)
if err != nil {
return nil, err
}
stateTrie, err := w.GetChain().GenStateTrie(header.GetHeight(), prevStateHash, accTxs)
if err != nil {
return nil, err
}
if stateTrie == nil {
return nil, fmt.Errorf("failed to generate state trie")
}
// build coinbase tx
cbtx = mineBlock.Header.CbTx
cbtx.TxNum = uint32(len(accTxs) + 1)
cbtx.StateHash = *stateTrie.Hash()
minerRwd, repRwd, err := w.GetChain().CalcBlockReward(header)
if err != nil {
return nil, err
}
minerTxOut := cbtx.GetMinerTxOut()
minerTxOut.Address = minerAddr
minerTxOut.Value = minerRwd
repTxOut := cbtx.GetRepTxOut()
repTxOut.Address = types.MinerAddress
repTxOut.Value = repRwd
cbTxHash := cbtx.ComputeHash()
// append all txs to body
body := mineBlock.Body
cbTxPov := &types.PovTransaction{Hash: cbTxHash, CbTx: cbtx}
body.Txs = append(body.Txs, cbTxPov)
body.Txs = append(body.Txs, accTxs...)
// calc merkle root
var mklTxHashList []*types.Hash
mklTxHashList = append(mklTxHashList, &cbTxPov.Hash)
mklTxHashList = append(mklTxHashList, accTxHashes...)
mklHash := merkle.CalcMerkleTreeRootHash(mklTxHashList)
header.BasHdr.MerkleRoot = mklHash
mineBlock.AllTxHashes = mklTxHashList
// calc merkle branch without coinbase tx
mineBlock.CoinbaseBranch = merkle.BuildCoinbaseMerkleBranch(accTxHashes)
mineBlock.WorkHash = mineBlock.Block.ComputeHash()
mineBlock.MinTime = w.GetChain().CalcPastMedianTime(latestHeader)
return mineBlock, nil
}
func (w *PovWorker) generateBlock(minerAddr types.Address, algoType types.PovAlgoType) (*types.PovMineBlock, error) {
w.muxMineBlock.Lock()
defer w.muxMineBlock.Unlock()
var err error
latestHeader := w.GetChain().LatestHeader()
// reset all pending blocks when best chain changed
if w.lastMineHeight != latestHeader.GetHeight() {
w.mineBlockPool = make(map[types.Hash]*types.PovMineBlock)
w.minerAlgoBlocks = make(map[types.Address]map[types.PovAlgoType]*PovMinerAlgoBlock)
w.lastMineHeight = latestHeader.GetHeight()
}
var curMinerAlgoBlk *PovMinerAlgoBlock
curMinerAlgos := w.minerAlgoBlocks[minerAddr]
if curMinerAlgos == nil {
curMinerAlgos = make(map[types.PovAlgoType]*PovMinerAlgoBlock)
w.minerAlgoBlocks[minerAddr] = curMinerAlgos
}
curMinerAlgoBlk = curMinerAlgos[algoType]
if curMinerAlgoBlk == nil {
curMinerAlgoBlk = new(PovMinerAlgoBlock)
curMinerAlgos[algoType] = curMinerAlgoBlk
}
if curMinerAlgoBlk.curMineBlock == nil ||
time.Now().After(curMinerAlgoBlk.lastMineTime.Add(30*time.Second)) {
curMinerAlgoBlk.curMineBlock, err = w.newBlockTemplate(minerAddr, algoType)
if err != nil {
return nil, err
}
curMinerAlgoBlk.lastMineTime = time.Now()
w.mineBlockPool[curMinerAlgoBlk.curMineBlock.WorkHash] = curMinerAlgoBlk.curMineBlock
}
return curMinerAlgoBlk.curMineBlock, nil
}
func (w *PovWorker) findBlockInPool(workHash types.Hash) *types.PovMineBlock {
w.muxMineBlock.Lock()
defer w.muxMineBlock.Unlock()
return w.mineBlockPool[workHash]
}
func (w *PovWorker) checkAndFillBlockByResult(mineBlock *types.PovMineBlock, result *types.PovMineResult) error {
if len(result.CoinbaseExtra) < common.PovMinCoinbaseExtraSize {
return fmt.Errorf("coinbase extra size too small, min size is %d", common.PovMinCoinbaseExtraSize)
}
if len(result.CoinbaseExtra) > common.PovMaxCoinbaseExtraSize {
return fmt.Errorf("coinbase extra size too big, max size is %d", common.PovMaxCoinbaseExtraSize)
}
mineBlock.Header.CbTx.TxIns[0].Extra = result.CoinbaseExtra
cbTxHash := mineBlock.Header.CbTx.ComputeHash()
if cbTxHash.Cmp(result.CoinbaseHash) != 0 {
return fmt.Errorf("coinbase hash not equal, %s != %s", cbTxHash, result.CoinbaseHash)
}
mineBlock.Header.CbTx.Hash = result.CoinbaseHash
cbTxPov := mineBlock.Body.Txs[0]
cbTxPov.Hash = result.CoinbaseHash
mineBlock.AllTxHashes[0] = &cbTxPov.Hash
calcMklRoot := merkle.CalcMerkleTreeRootHash(mineBlock.AllTxHashes)
if calcMklRoot.Cmp(result.MerkleRoot) != 0 {
return fmt.Errorf("merkle root not equal, %s != %s", calcMklRoot, result.MerkleRoot)
}
mineBlock.Header.BasHdr.MerkleRoot = result.MerkleRoot
mineBlock.Header.BasHdr.Timestamp = result.Timestamp
mineBlock.Header.BasHdr.Nonce = result.Nonce
calcBlkHash := mineBlock.Header.ComputeHash()
if calcBlkHash.Cmp(result.BlockHash) != 0 {
return fmt.Errorf("block hash not equal, %s != %s", calcBlkHash, result.BlockHash)
}
mineBlock.Header.BasHdr.Hash = result.BlockHash
if result.AuxPow != nil {
calcParHash := result.AuxPow.ParBlockHeader.ComputeHash()
if calcParHash != result.AuxPow.ParentHash {
return fmt.Errorf("parent block hash not equal, %s != %s", calcParHash, result.AuxPow.ParentHash)
}
mineBlock.Header.AuxHdr = result.AuxPow
}
return nil
}
func (w *PovWorker) checkMinerPledge(minerAddr types.Address) error {
latestBlock := w.GetChain().LatestBlock()
if latestBlock.GetHeight() >= (common.PovMinerVerifyHeightStart - 1) {
prevStateHash := latestBlock.GetStateHash()
prevTrie := w.GetChain().GetStateTrie(&prevStateHash)
if prevTrie == nil {
return errors.New("miner pausing for get previous state tire failed")
}
rs := w.GetChain().GetRepState(prevTrie, minerAddr)
if rs == nil {
return errors.New("miner pausing for account state not exist")
}
if rs.Vote.Compare(common.PovMinerPledgeAmountMin) == types.BalanceCompSmaller {
return errors.New("miner pausing for vote pledge not enough")
}
}
return nil
}
func (w *PovWorker) submitBlock(mineBlock *types.PovMineBlock) {
newBlock := mineBlock.Block.Copy()
w.logger.Infof("submit block %d/%s", newBlock.GetHeight(), newBlock.GetHash())
err := w.miner.GetPovEngine().AddMinedBlock(newBlock)
if err != nil {
w.logger.Infof("failed to submit block %d/%s", newBlock.GetHeight(), newBlock.GetHash())
}
}