@@ -483,13 +483,6 @@ func (bc *BlockChain) Stop() {
483483 glog .V (logger .Info ).Infoln ("Chain manager stopped" )
484484}
485485
486- type queueEvent struct {
487- queue []interface {}
488- canonicalCount int
489- sideCount int
490- splitCount int
491- }
492-
493486func (self * BlockChain ) procFutureBlocks () {
494487 blocks := make ([]* types.Block , self .futureBlocks .Len ())
495488 for i , hash := range self .futureBlocks .Keys () {
@@ -573,10 +566,9 @@ func (self *BlockChain) InsertChain(chain types.Blocks) (int, error) {
573566 // faster than direct delivery and requires much less mutex
574567 // acquiring.
575568 var (
576- queue = make ([]interface {}, len (chain ))
577- queueEvent = queueEvent {queue : queue }
578- stats struct { queued , processed , ignored int }
579- tstart = time .Now ()
569+ stats struct { queued , processed , ignored int }
570+ events = make ([]interface {}, 0 , len (chain ))
571+ tstart = time .Now ()
580572
581573 nonceChecked = make ([]bool , len (chain ))
582574 )
@@ -659,22 +651,21 @@ func (self *BlockChain) InsertChain(chain types.Blocks) (int, error) {
659651 if glog .V (logger .Debug ) {
660652 glog .Infof ("[%v] inserted block #%d (%d TXs %v G %d UNCs) (%x...). Took %v\n " , time .Now ().UnixNano (), block .Number (), len (block .Transactions ()), block .GasUsed (), len (block .Uncles ()), block .Hash ().Bytes ()[0 :4 ], time .Since (bstart ))
661653 }
662- queue [i ] = ChainEvent {block , block .Hash (), logs }
663- queueEvent .canonicalCount ++
654+ events = append (events , ChainEvent {block , block .Hash (), logs })
664655
665656 // This puts transactions in a extra db for rpc
666657 PutTransactions (self .chainDb , block , block .Transactions ())
667658 // store the receipts
668659 PutReceipts (self .chainDb , receipts )
660+
669661 case SideStatTy :
670662 if glog .V (logger .Detail ) {
671663 glog .Infof ("inserted forked block #%d (TD=%v) (%d TXs %d UNCs) (%x...). Took %v\n " , block .Number (), block .Difficulty (), len (block .Transactions ()), len (block .Uncles ()), block .Hash ().Bytes ()[0 :4 ], time .Since (bstart ))
672664 }
673- queue [ i ] = ChainSideEvent {block , logs }
674- queueEvent . sideCount ++
665+ events = append ( events , ChainSideEvent {block , logs })
666+
675667 case SplitStatTy :
676- queue [i ] = ChainSplitEvent {block , logs }
677- queueEvent .splitCount ++
668+ events = append (events , ChainSplitEvent {block , logs })
678669 }
679670 stats .processed ++
680671 }
@@ -684,8 +675,7 @@ func (self *BlockChain) InsertChain(chain types.Blocks) (int, error) {
684675 start , end := chain [0 ], chain [len (chain )- 1 ]
685676 glog .Infof ("imported %d block(s) (%d queued %d ignored) including %d txs in %v. #%v [%x / %x]\n " , stats .processed , stats .queued , stats .ignored , txcount , tend , end .Number (), start .Hash ().Bytes ()[:4 ], end .Hash ().Bytes ()[:4 ])
686677 }
687-
688- go self .eventMux .Post (queueEvent )
678+ go self .postChainEvents (events )
689679
690680 return 0 , nil
691681}
@@ -774,32 +764,31 @@ func (self *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
774764 return nil
775765}
776766
767+ // postChainEvents iterates over the events generated by a chain insertion and
768+ // posts them into the event mux.
769+ func (self * BlockChain ) postChainEvents (events []interface {}) {
770+ for _ , event := range events {
771+ if event , ok := event .(ChainEvent ); ok {
772+ // We need some control over the mining operation. Acquiring locks and waiting for the miner to create new block takes too long
773+ // and in most cases isn't even necessary.
774+ if self .currentBlock .Hash () == event .Hash {
775+ self .currentGasLimit = CalcGasLimit (event .Block )
776+ self .eventMux .Post (ChainHeadEvent {event .Block })
777+ }
778+ }
779+ // Fire the insertion events individually too
780+ self .eventMux .Post (event )
781+ }
782+ }
783+
777784func (self * BlockChain ) update () {
778- events := self .eventMux .Subscribe (queueEvent {})
779785 futureTimer := time .Tick (5 * time .Second )
780- out:
781786 for {
782787 select {
783- case ev := <- events .Chan ():
784- switch ev := ev .(type ) {
785- case queueEvent :
786- for _ , event := range ev .queue {
787- switch event := event .(type ) {
788- case ChainEvent :
789- // We need some control over the mining operation. Acquiring locks and waiting for the miner to create new block takes too long
790- // and in most cases isn't even necessary.
791- if self .currentBlock .Hash () == event .Hash {
792- self .currentGasLimit = CalcGasLimit (event .Block )
793- self .eventMux .Post (ChainHeadEvent {event .Block })
794- }
795- }
796- self .eventMux .Post (event )
797- }
798- }
799788 case <- futureTimer :
800789 self .procFutureBlocks ()
801790 case <- self .quit :
802- break out
791+ return
803792 }
804793 }
805794}
0 commit comments