@@ -109,9 +109,9 @@ type ChainManager struct {
109
109
transState * state.StateDB
110
110
txState * state.ManagedState
111
111
112
- cache * lru.Cache // cache is the LRU caching
113
- futureBlocks * BlockCache // future blocks are blocks added for later processing
114
- pendingBlocks * BlockCache // pending blocks contain blocks not yet written to the db
112
+ cache * lru.Cache // cache is the LRU caching
113
+ futureBlocks * lru. Cache // future blocks are blocks added for later processing
114
+ pendingBlocks * lru. Cache // pending blocks contain blocks not yet written to the db
115
115
116
116
quit chan struct {}
117
117
// procInterrupt must be atomically called
@@ -158,7 +158,7 @@ func NewChainManager(genesis *types.Block, blockDb, stateDb common.Database, pow
158
158
// Take ownership of this particular state
159
159
bc .txState = state .ManageState (bc .State ().Copy ())
160
160
161
- bc .futureBlocks = NewBlockCache (maxFutureBlocks )
161
+ bc .futureBlocks , _ = lru . New (maxFutureBlocks )
162
162
bc .makeCache ()
163
163
164
164
go bc .update ()
@@ -390,7 +390,7 @@ func (bc *ChainManager) HasBlock(hash common.Hash) bool {
390
390
}
391
391
392
392
if bc .pendingBlocks != nil {
393
- if block := bc .pendingBlocks .Get (hash ); block != nil {
393
+ if _ , exist := bc .pendingBlocks .Get (hash ); exist {
394
394
return true
395
395
}
396
396
}
@@ -426,8 +426,8 @@ func (self *ChainManager) GetBlock(hash common.Hash) *types.Block {
426
426
}
427
427
428
428
if self .pendingBlocks != nil {
429
- if block := self .pendingBlocks .Get (hash ); block != nil {
430
- return block
429
+ if block , _ := self .pendingBlocks .Get (hash ); block != nil {
430
+ return block .( * types. Block )
431
431
}
432
432
}
433
433
@@ -510,32 +510,37 @@ type queueEvent struct {
510
510
}
511
511
512
512
func (self * ChainManager ) procFutureBlocks () {
513
- var blocks []* types.Block
514
- self .futureBlocks .Each (func (i int , block * types.Block ) {
515
- blocks = append (blocks , block )
516
- })
513
+ blocks := make ([]* types.Block , self .futureBlocks .Len ())
514
+ for i , hash := range self .futureBlocks .Keys () {
515
+ block , _ := self .futureBlocks .Get (hash )
516
+ blocks [i ] = block .(* types.Block )
517
+ }
517
518
if len (blocks ) > 0 {
518
519
types .BlockBy (types .Number ).Sort (blocks )
519
520
self .InsertChain (blocks )
520
521
}
521
522
}
522
523
523
524
func (self * ChainManager ) enqueueForWrite (block * types.Block ) {
524
- self .pendingBlocks .Push ( block )
525
+ self .pendingBlocks .Add ( block . Hash (), block )
525
526
}
526
527
527
528
func (self * ChainManager ) flushQueuedBlocks () {
528
529
db , batchWrite := self .blockDb .(* ethdb.LDBDatabase )
529
530
batch := new (leveldb.Batch )
530
- self .pendingBlocks .Each (func (i int , block * types.Block ) {
531
+ for _ , key := range self .pendingBlocks .Keys () {
532
+ b , _ := self .pendingBlocks .Get (key )
533
+ block := b .(* types.Block )
534
+
531
535
enc , _ := rlp .EncodeToBytes ((* types .StorageBlock )(block ))
532
536
key := append (blockHashPre , block .Hash ().Bytes ()... )
533
537
if batchWrite {
534
538
batch .Put (key , rle .Compress (enc ))
535
539
} else {
536
540
self .blockDb .Put (key , enc )
537
541
}
538
- })
542
+ }
543
+
539
544
if batchWrite {
540
545
db .LDB ().Write (batch , nil )
541
546
}
@@ -588,7 +593,7 @@ func (self *ChainManager) WriteBlock(block *types.Block) (status writeStatus, er
588
593
self .enqueueForWrite (block )
589
594
self .mu .Unlock ()
590
595
// Delete from future blocks
591
- self .futureBlocks .Delete (block .Hash ())
596
+ self .futureBlocks .Remove (block .Hash ())
592
597
593
598
return
594
599
}
@@ -602,7 +607,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
602
607
self .chainmu .Lock ()
603
608
defer self .chainmu .Unlock ()
604
609
605
- self .pendingBlocks = NewBlockCache (len (chain ))
610
+ self .pendingBlocks , _ = lru . New (len (chain ))
606
611
607
612
// A queued approach to delivering events. This is generally
608
613
// faster than direct delivery and requires much less mutex
@@ -669,13 +674,13 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
669
674
return i , fmt .Errorf ("%v: BlockFutureErr, %v > %v" , BlockFutureErr , block .Time (), max )
670
675
}
671
676
672
- self .futureBlocks .Push ( block )
677
+ self .futureBlocks .Add ( block . Hash (), block )
673
678
stats .queued ++
674
679
continue
675
680
}
676
681
677
- if IsParentErr (err ) && self .futureBlocks .Has (block .ParentHash ()) {
678
- self .futureBlocks .Push ( block )
682
+ if IsParentErr (err ) && self .futureBlocks .Contains (block .ParentHash ()) {
683
+ self .futureBlocks .Add ( block . Hash (), block )
679
684
stats .queued ++
680
685
continue
681
686
}
0 commit comments