@@ -109,9 +109,9 @@ type ChainManager struct {
109109 transState * state.StateDB
110110 txState * state.ManagedState
111111
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
115115
116116 quit chan struct {}
117117 // procInterrupt must be atomically called
@@ -158,7 +158,7 @@ func NewChainManager(genesis *types.Block, blockDb, stateDb common.Database, pow
158158 // Take ownership of this particular state
159159 bc .txState = state .ManageState (bc .State ().Copy ())
160160
161- bc .futureBlocks = NewBlockCache (maxFutureBlocks )
161+ bc .futureBlocks , _ = lru . New (maxFutureBlocks )
162162 bc .makeCache ()
163163
164164 go bc .update ()
@@ -390,7 +390,7 @@ func (bc *ChainManager) HasBlock(hash common.Hash) bool {
390390 }
391391
392392 if bc .pendingBlocks != nil {
393- if block := bc .pendingBlocks .Get (hash ); block != nil {
393+ if _ , exist := bc .pendingBlocks .Get (hash ); exist {
394394 return true
395395 }
396396 }
@@ -426,8 +426,8 @@ func (self *ChainManager) GetBlock(hash common.Hash) *types.Block {
426426 }
427427
428428 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 )
431431 }
432432 }
433433
@@ -510,32 +510,37 @@ type queueEvent struct {
510510}
511511
512512func (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+ }
517518 if len (blocks ) > 0 {
518519 types .BlockBy (types .Number ).Sort (blocks )
519520 self .InsertChain (blocks )
520521 }
521522}
522523
523524func (self * ChainManager ) enqueueForWrite (block * types.Block ) {
524- self .pendingBlocks .Push ( block )
525+ self .pendingBlocks .Add ( block . Hash (), block )
525526}
526527
527528func (self * ChainManager ) flushQueuedBlocks () {
528529 db , batchWrite := self .blockDb .(* ethdb.LDBDatabase )
529530 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+
531535 enc , _ := rlp .EncodeToBytes ((* types .StorageBlock )(block ))
532536 key := append (blockHashPre , block .Hash ().Bytes ()... )
533537 if batchWrite {
534538 batch .Put (key , rle .Compress (enc ))
535539 } else {
536540 self .blockDb .Put (key , enc )
537541 }
538- })
542+ }
543+
539544 if batchWrite {
540545 db .LDB ().Write (batch , nil )
541546 }
@@ -588,7 +593,7 @@ func (self *ChainManager) WriteBlock(block *types.Block) (status writeStatus, er
588593 self .enqueueForWrite (block )
589594 self .mu .Unlock ()
590595 // Delete from future blocks
591- self .futureBlocks .Delete (block .Hash ())
596+ self .futureBlocks .Remove (block .Hash ())
592597
593598 return
594599}
@@ -602,7 +607,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
602607 self .chainmu .Lock ()
603608 defer self .chainmu .Unlock ()
604609
605- self .pendingBlocks = NewBlockCache (len (chain ))
610+ self .pendingBlocks , _ = lru . New (len (chain ))
606611
607612 // A queued approach to delivering events. This is generally
608613 // faster than direct delivery and requires much less mutex
@@ -669,13 +674,13 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
669674 return i , fmt .Errorf ("%v: BlockFutureErr, %v > %v" , BlockFutureErr , block .Time (), max )
670675 }
671676
672- self .futureBlocks .Push ( block )
677+ self .futureBlocks .Add ( block . Hash (), block )
673678 stats .queued ++
674679 continue
675680 }
676681
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 )
679684 stats .queued ++
680685 continue
681686 }
0 commit comments