-
Notifications
You must be signed in to change notification settings - Fork 14
/
processor.go
831 lines (720 loc) · 21.8 KB
/
processor.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
package dpos
import (
"context"
"sync"
"sync/atomic"
"time"
"github.com/bluele/gcache"
"github.com/qlcchain/go-qlc/common"
"github.com/qlcchain/go-qlc/common/types"
"github.com/qlcchain/go-qlc/consensus"
"github.com/qlcchain/go-qlc/ledger"
"github.com/qlcchain/go-qlc/ledger/process"
"github.com/qlcchain/go-qlc/p2p"
cabi "github.com/qlcchain/go-qlc/vm/contract/abi"
)
const ConfirmChainParallelNum = 10
type chainKey struct {
addr types.Address
token types.Hash
}
type chainOrderKey struct {
chainKey
order uint64
}
type Processor struct {
index int
dps *DPoS
uncheckedCache gcache.Cache //gap blocks
quitCh chan bool
blocks chan *consensus.BlockSource
blocksAcked chan types.Hash
syncBlock chan *types.StateBlock
syncBlockAcked chan types.Hash
acks chan *voteInfo
frontiers chan *types.StateBlock
syncStateChange chan common.SyncState
syncState common.SyncState
orderedChain *sync.Map
chainHeight map[chainKey]uint64
doneBlock chan *types.StateBlock
confirmedChain map[types.Hash]bool
confirmParallelNum int32
ctx context.Context
cancel context.CancelFunc
}
func newProcessors(num int) []*Processor {
processors := make([]*Processor, 0)
for i := 0; i < num; i++ {
ctx, cancel := context.WithCancel(context.Background())
p := &Processor{
index: i,
quitCh: make(chan bool, 1),
blocks: make(chan *consensus.BlockSource, common.DPoSMaxBlocks),
blocksAcked: make(chan types.Hash, common.DPoSMaxBlocks),
syncBlock: make(chan *types.StateBlock, common.DPoSMaxBlocks),
syncBlockAcked: make(chan types.Hash, common.DPoSMaxBlocks),
acks: make(chan *voteInfo, common.DPoSMaxBlocks),
frontiers: make(chan *types.StateBlock, common.DPoSMaxBlocks),
syncStateChange: make(chan common.SyncState, 1),
syncState: common.SyncNotStart,
orderedChain: new(sync.Map),
chainHeight: make(map[chainKey]uint64),
doneBlock: make(chan *types.StateBlock, common.DPoSMaxBlocks),
confirmedChain: make(map[types.Hash]bool),
confirmParallelNum: ConfirmChainParallelNum,
ctx: ctx,
cancel: cancel,
}
processors = append(processors, p)
}
return processors
}
func (p *Processor) setDposService(dps *DPoS) {
p.dps = dps
}
func (p *Processor) start() {
go p.processMsg()
}
func (p *Processor) stop() {
p.quitCh <- true
p.cancel()
}
func (p *Processor) syncBlockCheck(block *types.StateBlock) {
dps := p.dps
hash := block.GetHash()
checked := false
if block.IsOpen() {
checked = true
} else {
if has, _ := dps.ledger.HasUnconfirmedSyncBlock(block.Previous); has {
checked = true
} else {
if has, _ := dps.ledger.HasStateBlockConfirmed(block.Previous); has {
checked = true
} else {
//p.enqueueUncheckedSync(block)
}
}
}
if checked {
if err := dps.ledger.AddUnconfirmedSyncBlock(hash, block); err != nil {
dps.logger.Errorf("add unconfirmed sync block err", err)
}
p.processConfirmedSync(hash, block)
}
}
func (p *Processor) processMsg() {
timerRest := time.NewTicker(10 * time.Millisecond)
timerConfirm := time.NewTicker(time.Second)
for {
PriorityOut:
for {
select {
case <-p.quitCh:
return
case p.syncState = <-p.syncStateChange:
p.dps.syncStateNotifyWait.Done()
if p.syncState == common.Syncing {
p.orderedChain = new(sync.Map)
p.chainHeight = make(map[chainKey]uint64)
p.confirmedChain = make(map[types.Hash]bool)
}
case hash := <-p.syncBlockAcked:
if p.dps.isConfirmedFrontier(hash) {
if s, ok := p.dps.frontiersStatus.Load(hash); ok && s == frontierConfirmed {
p.dps.frontiersStatus.Store(hash, frontierChainConfirmed)
}
if _, ok := p.confirmedChain[hash]; !ok {
p.confirmedChain[hash] = false
}
}
//p.dequeueUncheckedSync(hash)
case hash := <-p.blocksAcked:
p.dequeueUnchecked(hash)
case ack := <-p.acks:
p.processAck(ack)
case frontier := <-p.frontiers:
p.processFrontier(frontier)
case block := <-p.doneBlock:
p.dps.updateLastProcessSyncTime()
if err := p.dps.lv.BlockSyncDoneProcess(block); err != nil {
p.dps.logger.Warnf("block(%s) sync done error: %s", block.GetHash(), err)
}
default:
break PriorityOut
}
}
select {
case <-p.quitCh:
return
case bs := <-p.blocks:
p.processMsgDo(bs)
case block := <-p.syncBlock:
p.dps.updateLastProcessSyncTime()
p.syncBlockCheck(block)
case <-timerRest.C:
//
case <-timerConfirm.C:
if p.syncState == common.SyncDone || p.syncState == common.Syncing {
for hash, dealt := range p.confirmedChain {
if dealt {
continue
}
if atomic.LoadInt32(&p.confirmParallelNum) > 0 {
// use gorouting to forbid blocking the thead
p.confirmedChain[hash] = true
go p.confirmChain(hash)
} else {
break
}
}
}
}
}
}
func (p *Processor) processConfirmedSync(hash types.Hash, block *types.StateBlock) {
ck := chainKey{
addr: block.Address,
token: block.Token,
}
if _, ok := p.chainHeight[ck]; ok {
p.chainHeight[ck]++
} else {
p.chainHeight[ck] = 1
}
cok := chainOrderKey{
chainKey: ck,
order: p.chainHeight[ck],
}
p.orderedChain.Store(cok, hash)
p.syncBlockAcked <- hash
}
func (p *Processor) localRepVoteFrontier(block *types.StateBlock) {
hash := block.GetHash()
dps := p.dps
dps.localRepAccount.Range(func(key, value interface{}) bool {
address := key.(types.Address)
weight := dps.ledger.Weight(address)
if weight.Compare(dps.minVoteWeight) == types.BalanceCompSmaller {
return true
}
if block.Type == types.Online {
if !dps.isOnline(block.Address) {
dps.logger.Debugf("block[%s] is not online", hash)
return false
}
}
vi := &voteInfo{
hash: hash,
account: address,
}
select {
case p.acks <- vi:
dps.acTrx.setVoteHash(block)
dps.logger.Debugf("rep [%s] vote for block[%s] type[%s]", address, hash, block.Type)
default:
}
return true
})
}
func (p *Processor) processFrontier(block *types.StateBlock) {
hash := block.GetHash()
dps := p.dps
dps.subAckDo(p.index, hash)
if !p.dps.acTrx.addToRoots(block) {
if el := dps.acTrx.getVoteInfo(block); el != nil {
el.blocks.Store(hash, block)
el.frontier.Store(hash, nil)
dps.hash2el.Store(hash, el)
} else {
dps.logger.Errorf("get election err[%s]", hash)
}
} else {
if el := dps.acTrx.getVoteInfo(block); el != nil {
el.frontier.Store(hash, nil)
}
}
result, _ := dps.lv.BlockCheck(block)
if result == process.Progress {
p.localRepVoteFrontier(block)
}
//p.syncBlock <- block
}
func (p *Processor) confirmChain(hash types.Hash) {
atomic.AddInt32(&p.confirmParallelNum, -1)
defer atomic.AddInt32(&p.confirmParallelNum, 1)
dps := p.dps
dps.logger.Debugf("confirm chain %s", hash)
if block, err := dps.ledger.GetUnconfirmedSyncBlock(hash); err != nil {
dps.logger.Errorf("get unconfirmed sync block err %s", err)
return
} else {
cok := chainOrderKey{
chainKey: chainKey{block.Address, block.Token},
order: 1,
}
for {
if h, ok := p.orderedChain.Load(cok); ok {
bHash := h.(types.Hash)
if blk, _ := dps.ledger.GetUnconfirmedSyncBlock(bHash); blk != nil {
dps.logger.Debugf("confirm chain block %s", bHash)
bs := &consensus.BlockSource{
Block: blk,
BlockFrom: types.Synchronized,
Type: consensus.MsgSync,
}
select {
case <-p.ctx.Done():
return
case p.blocks <- bs:
}
if err := dps.ledger.DeleteUnconfirmedSyncBlock(bHash); err != nil {
dps.logger.Errorf("delete unconfirmed sync block err", err)
}
}
} else {
break
}
cok.order++
}
}
}
func (p *Processor) processAck(vi *voteInfo) {
dps := p.dps
dps.logger.Infof("processor recv confirmAck block[%s]", vi.hash)
if val, ok := dps.hash2el.Load(vi.hash); ok {
el := val.(*Election)
if _, ok := el.frontier.Load(vi.hash); ok {
if dps.acTrx.voteFrontier(vi) {
p.dps.frontiersStatus.Store(vi.hash, frontierConfirmed)
p.syncBlockAcked <- vi.hash
dps.logger.Infof("frontier %s confirmed", vi.hash)
}
} else {
if has, _ := dps.ledger.HasStateBlockConfirmed(vi.hash); !has {
dps.acTrx.vote(vi)
} else {
dps.heartAndVoteInc(vi.hash, vi.account, onlineKindVote)
}
}
}
}
func (p *Processor) processMsgDo(bs *consensus.BlockSource) {
dps := p.dps
hash := bs.Block.GetHash()
var result process.ProcessResult
var err error
if bs.BlockFrom == types.Synchronized {
p.dps.updateLastProcessSyncTime()
}
result, err = dps.lv.BlockCheck(bs.Block)
if err != nil {
dps.logger.Infof("block[%s] check err[%s]", hash, err.Error())
return
}
p.processResult(result, bs)
switch bs.Type {
case consensus.MsgPublishReq:
dps.logger.Infof("dps recv publishReq block[%s]", hash)
case consensus.MsgConfirmReq:
dps.logger.Infof("dps recv confirmReq block[%s]", hash)
// vote if the result is progress and old
if result == process.Progress {
if el := dps.acTrx.getVoteInfo(bs.Block); el != nil {
if el.voteHash == types.ZeroHash {
dps.localRepVote(bs.Block)
} else if hash == el.voteHash {
dps.localRepVote(bs.Block)
}
}
} else if result == process.Old {
dps.localRepVote(bs.Block)
}
case consensus.MsgSync:
// do nothing
case consensus.MsgGenerateBlock:
// cache fork
if result == process.Progress {
el := dps.acTrx.getVoteInfo(bs.Block)
if el != nil && el.status.winner.GetHash() != hash {
_ = dps.lv.Rollback(hash)
return
}
}
dps.acTrx.updatePerfTime(hash, time.Now().UnixNano(), false)
default:
//
}
}
func (p *Processor) processResult(result process.ProcessResult, bs *consensus.BlockSource) {
blk := bs.Block
hash := blk.GetHash()
dps := p.dps
switch result {
case process.Progress:
if bs.BlockFrom == types.Synchronized {
p.confirmBlock(bs.Block)
dps.logger.Infof("Block %s from sync", hash)
} else if bs.BlockFrom == types.UnSynchronized {
dps.logger.Infof("Add block %s to roots", hash)
//make sure we only vote one of the forked blocks
if dps.acTrx.addToRoots(blk) && bs.Type != consensus.MsgConfirmReq {
dps.localRepVote(bs.Block)
}
dps.subAckDo(p.index, hash)
} else {
dps.logger.Errorf("Block %s UnKnow from", hash)
}
case process.BadSignature:
dps.logger.Errorf("Bad signature for block: %s", hash)
case process.BadWork:
dps.logger.Errorf("Bad work for block: %s", hash)
case process.BalanceMismatch:
dps.logger.Errorf("Balance mismatch for block: %s", hash)
case process.Old:
dps.logger.Debugf("Old for block: %s", hash)
case process.UnReceivable:
dps.logger.Errorf("UnReceivable for block: %s", hash)
case process.GapSmartContract:
dps.logger.Errorf("GapSmartContract for block: %s", hash)
//dps.processGapSmartContract(blk)
case process.InvalidData:
dps.logger.Errorf("InvalidData for block: %s", hash)
case process.Other:
dps.logger.Errorf("UnKnow process result for block: %s", hash)
case process.Fork:
dps.logger.Errorf("Fork for block: %s", hash)
p.processFork(bs)
case process.GapPrevious:
dps.logger.Infof("block:[%s] Gap previous:[%s]", hash, blk.Previous.String())
p.enqueueUnchecked(result, bs)
case process.GapSource:
dps.logger.Infof("block:[%s] Gap source:[%s]", hash, blk.Link.String())
p.enqueueUnchecked(result, bs)
case process.GapTokenInfo:
dps.logger.Infof("block:[%s] Gap tokenInfo", hash)
p.enqueueUnchecked(result, bs)
case process.GapPovHeight:
dps.logger.Infof("block:[%s] Gap pov height", hash)
p.dps.gapPovCh <- bs
}
}
func (p *Processor) confirmBlock(blk *types.StateBlock) {
hash := blk.GetHash()
vk := getVoteKey(blk)
dps := p.dps
if v, ok := dps.acTrx.roots.Load(vk); ok {
el := v.(*Election)
loser := make([]*types.StateBlock, 0)
if !el.ifValidAndSetInvalid() {
return
}
dps.acTrx.roots.Delete(el.vote.id)
dps.acTrx.updatePerfTime(hash, time.Now().UnixNano(), true)
el.blocks.Range(func(key, value interface{}) bool {
if key.(types.Hash) != hash {
loser = append(loser, value.(*types.StateBlock))
}
return true
})
el.cleanBlockInfo()
dps.acTrx.rollBack(loser)
dps.acTrx.addSyncBlock2Ledger(blk)
p.blocksAcked <- hash
dps.dispatchAckedBlock(blk, hash, p.index)
dps.eb.Publish(common.EventConfirmedBlock, blk)
} else {
dps.acTrx.addSyncBlock2Ledger(blk)
p.blocksAcked <- hash
dps.dispatchAckedBlock(blk, hash, p.index)
dps.eb.Publish(common.EventConfirmedBlock, blk)
}
}
func (p *Processor) processFork(bs *consensus.BlockSource) {
dps := p.dps
newBlock := bs.Block
confirmedBlock := p.findAnotherForkedBlock(newBlock)
confirmedHash := confirmedBlock.GetHash()
newHash := newBlock.GetHash()
//cache block forked with confirmed block
if bs.Type == consensus.MsgGenerateBlock {
_ = dps.lv.Rollback(bs.Block.GetHash())
dps.logger.Errorf("fork:[new:%s]--[local:%s]--cache forked", newHash, confirmedHash)
return
}
// Block has been packed by pov, can not be rolled back
if dps.ledger.HasPovTxLookup(confirmedHash) {
dps.logger.Errorf("fork:[new:%s]--[local:%s]--pov packed", newHash, confirmedHash)
return
}
dps.logger.Errorf("fork:[new:%s]--[local:%s]--pov not packed", newHash, confirmedHash)
if dps.acTrx.addToRoots(confirmedBlock) {
confirmReqBlocks := make([]*types.StateBlock, 0)
confirmReqBlocks = append(confirmReqBlocks, confirmedBlock)
dps.eb.Publish(common.EventBroadcast, p2p.ConfirmReq, confirmReqBlocks)
dps.subAckDo(p.index, confirmedBlock.GetHash())
dps.subAckDo(p.index, newHash)
if el := dps.acTrx.getVoteInfo(confirmedBlock); el != nil {
el.blocks.Store(newHash, newBlock)
dps.hash2el.Store(newHash, el)
}
dps.localRepVote(confirmedBlock)
} else {
dps.subAckDo(p.index, newHash)
if el := dps.acTrx.getVoteInfo(confirmedBlock); el != nil {
el.blocks.Store(newHash, newBlock)
dps.hash2el.Store(newHash, el)
}
}
}
func (p *Processor) findAnotherForkedBlock(block *types.StateBlock) *types.StateBlock {
dps := p.dps
var forkedHash types.Hash
if block.IsOpen() {
tm, err := dps.ledger.GetTokenMeta(block.GetAddress(), block.GetToken())
if err != nil {
dps.logger.Error(err)
return block
}
forkedHash = tm.OpenBlock
} else {
hash := block.Parent()
var err error
forkedHash, err = dps.ledger.GetChild(hash)
if err != nil {
dps.logger.Error(err)
return block
}
}
forkedBlock, err := dps.ledger.GetStateBlock(forkedHash)
if err != nil {
dps.logger.Error(err)
return block
}
return forkedBlock
}
func (p *Processor) processUncheckedBlock(bs *consensus.BlockSource) bool {
dps := p.dps
var result process.ProcessResult
if has, _ := dps.ledger.HasBlockCache(bs.Block.GetHash()); has {
dps.eb.Publish(common.EventBroadcast, p2p.PublishReq, bs.Block)
}
result, _ = dps.lv.BlockCheck(bs.Block)
if result == bs.Gap {
p.blocksAcked <- bs.GapHash
return false
} else {
if bs.BlockFrom == types.Synchronized {
p.dps.updateLastProcessSyncTime()
}
p.processResult(result, bs)
return true
}
}
func (p *Processor) enqueueUnchecked(result process.ProcessResult, bs *consensus.BlockSource) {
//frontier confirmReq may cause problem
if bs.Type != consensus.MsgConfirmReq {
p.enqueueUncheckedToDb(result, bs)
}
}
func (p *Processor) enqueueUncheckedToDb(result process.ProcessResult, bs *consensus.BlockSource) {
blk := bs.Block
dps := p.dps
switch result {
case process.GapPrevious:
err := dps.ledger.AddUncheckedBlock(blk.Previous, blk, types.UncheckedKindPrevious, bs.BlockFrom)
if err != nil && err != ledger.ErrUncheckedBlockExists {
dps.logger.Errorf("add unchecked block to ledger err %s", err)
}
case process.GapSource:
err := dps.ledger.AddUncheckedBlock(blk.Link, blk, types.UncheckedKindLink, bs.BlockFrom)
if err != nil && err != ledger.ErrUncheckedBlockExists {
dps.logger.Errorf("add unchecked block to ledger err %s", err)
}
case process.GapTokenInfo:
input, err := dps.ledger.GetStateBlock(bs.Block.GetLink())
if err != nil {
dps.logger.Errorf("get contract send block err %s", err)
return
}
tokenId := new(types.Hash)
err = cabi.MintageABI.UnpackMethod(tokenId, cabi.MethodNameMintageWithdraw, input.GetData())
if err != nil {
dps.logger.Errorf("get token info err %s", err)
return
}
err = dps.ledger.AddUncheckedBlock(*tokenId, blk, types.UncheckedKindTokenInfo, bs.BlockFrom)
if err != nil && err != ledger.ErrUncheckedBlockExists {
dps.logger.Errorf("add unchecked block to ledger err %s", err)
}
}
}
func (p *Processor) enqueueUncheckedToMem(hash types.Hash, depHash types.Hash, bs *consensus.BlockSource) {
if !p.uncheckedCache.Has(depHash) {
consensus.GlobalUncheckedBlockNum.Inc()
blocks := new(sync.Map)
blocks.Store(hash, bs)
err := p.uncheckedCache.Set(depHash, blocks)
if err != nil {
p.dps.logger.Errorf("Gap previous set cache err for block:%s", hash)
}
} else {
c, err := p.uncheckedCache.Get(depHash)
if err != nil {
p.dps.logger.Errorf("Gap previous get cache err for block:%s", hash)
}
blocks := c.(*sync.Map)
blocks.Store(hash, bs)
}
}
func (p *Processor) enqueueUncheckedSync(block *types.StateBlock) {
p.enqueueUncheckedSyncToDb(block)
}
func (p *Processor) enqueueUncheckedSyncToDb(block *types.StateBlock) {
if err := p.dps.ledger.AddUncheckedSyncBlock(block.Previous, block); err != nil {
p.dps.logger.Errorf("add unchecked sync block to ledger err %s", err)
}
}
func (p *Processor) dequeueUnchecked(hash types.Hash) {
p.dequeueUncheckedFromDb(hash)
}
func (p *Processor) dequeueUncheckedFromDb(hash types.Hash) {
dps := p.dps
//hash is token id
if blkToken, bf, _ := dps.ledger.GetUncheckedBlock(hash, types.UncheckedKindTokenInfo); blkToken != nil {
if dps.getProcessorIndex(blkToken.Address) == p.index {
dps.logger.Debugf("dequeue gap token info[%s] block[%s]", hash, blkToken.GetHash())
bs := &consensus.BlockSource{
Block: blkToken,
BlockFrom: bf,
Gap: process.GapTokenInfo,
GapHash: hash,
}
if p.processUncheckedBlock(bs) {
err := dps.ledger.DeleteUncheckedBlock(hash, types.UncheckedKindTokenInfo)
if err != nil {
dps.logger.Errorf("Get err [%s] for hash: [%s] when delete UncheckedKindTokenInfo", err, blkToken.GetHash())
}
}
}
return
}
if blkLink, bf, _ := dps.ledger.GetUncheckedBlock(hash, types.UncheckedKindLink); blkLink != nil {
if dps.getProcessorIndex(blkLink.Address) == p.index {
dps.logger.Debugf("dequeue gap link[%s] block[%s]", hash, blkLink.GetHash())
bs := &consensus.BlockSource{
Block: blkLink,
BlockFrom: bf,
Gap: process.GapSource,
GapHash: hash,
}
if p.processUncheckedBlock(bs) {
err := dps.ledger.DeleteUncheckedBlock(hash, types.UncheckedKindLink)
if err != nil {
dps.logger.Errorf("Get err [%s] for hash: [%s] when delete UncheckedKindLink", err, blkLink.GetHash())
}
}
}
}
if blkPre, bf, _ := dps.ledger.GetUncheckedBlock(hash, types.UncheckedKindPrevious); blkPre != nil {
if dps.getProcessorIndex(blkPre.Address) == p.index {
dps.logger.Debugf("dequeue gap previous[%s] block[%s]", hash, blkPre.GetHash())
bs := &consensus.BlockSource{
Block: blkPre,
BlockFrom: bf,
Gap: process.GapPrevious,
GapHash: hash,
}
if p.processUncheckedBlock(bs) {
err := dps.ledger.DeleteUncheckedBlock(hash, types.UncheckedKindPrevious)
if err != nil {
dps.logger.Errorf("Get err [%s] for hash: [%s] when delete UncheckedKindPrevious", err, blkPre.GetHash())
}
}
}
}
}
func (p *Processor) dequeueUncheckedFromMem(hash types.Hash) {
dps := p.dps
dps.logger.Debugf("dequeue gap[%s]", hash.String())
if !p.uncheckedCache.Has(hash) {
return
}
m, err := p.uncheckedCache.Get(hash)
if err != nil {
dps.logger.Errorf("dequeue unchecked err [%s] for hash [%s]", err, hash)
return
}
cm := m.(*sync.Map)
cm.Range(func(key, value interface{}) bool {
bs := value.(*consensus.BlockSource)
dps.logger.Debugf("dequeue gap[%s] block[%s]", hash, bs.Block.GetHash())
result, _ := dps.lv.BlockCheck(bs.Block)
p.processResult(result, bs)
if p.isResultValid(result) {
if bs.BlockFrom == types.Synchronized {
p.confirmBlock(bs.Block)
return true
}
dps.localRepVote(bs.Block)
}
return true
})
r := p.uncheckedCache.Remove(hash)
if !r {
dps.logger.Error("remove cache for unchecked fail")
}
if consensus.GlobalUncheckedBlockNum.Load() > 0 {
consensus.GlobalUncheckedBlockNum.Dec()
}
}
func (p *Processor) dequeueUncheckedSync(hash types.Hash) {
p.dequeueUncheckedSyncFromDb(hash)
}
func (p *Processor) dequeueUncheckedSyncFromDb(hash types.Hash) {
dps := p.dps
if block, err := dps.ledger.GetUncheckedSyncBlock(hash); err != nil {
return
} else {
confirmedHash := block.GetHash()
if err := dps.ledger.AddUnconfirmedSyncBlock(confirmedHash, block); err != nil {
dps.logger.Errorf("add unconfirmed sync block err", err)
}
p.processConfirmedSync(confirmedHash, block)
if err := dps.ledger.DeleteUncheckedSyncBlock(hash); err != nil {
dps.logger.Errorf("del uncheck sync block err", err)
}
}
}
func (p *Processor) rollbackUncheckedFromMem(hash types.Hash) {
if !p.uncheckedCache.Has(hash) {
return
}
_, err := p.uncheckedCache.Get(hash)
if err != nil {
p.dps.logger.Errorf("dequeue unchecked err [%s] for hash [%s]", err, hash)
return
}
r := p.uncheckedCache.Remove(hash)
if !r {
p.dps.logger.Error("remove cache for unchecked fail")
}
if consensus.GlobalUncheckedBlockNum.Load() > 0 {
consensus.GlobalUncheckedBlockNum.Dec()
}
}
func (p *Processor) isResultValid(result process.ProcessResult) bool {
if result == process.Progress || result == process.Old {
return true
} else {
return false
}
}
func (p *Processor) isResultGap(result process.ProcessResult) bool {
if result == process.GapPrevious || result == process.GapSource || result == process.GapTokenInfo {
return true
} else {
return false
}
}