@@ -541,6 +541,58 @@ func (self *ChainManager) flushQueuedBlocks() {
541541 }
542542}
543543
544+ type writeStatus byte
545+
546+ const (
547+ nonStatTy writeStatus = iota
548+ canonStatTy
549+ splitStatTy
550+ sideStatTy
551+ )
552+
553+ func (self * ChainManager ) WriteBlock (block * types.Block ) (status writeStatus , err error ) {
554+ self .wg .Add (1 )
555+ defer self .wg .Done ()
556+
557+ cblock := self .currentBlock
558+ // Compare the TD of the last known block in the canonical chain to make sure it's greater.
559+ // At this point it's possible that a different chain (fork) becomes the new canonical chain.
560+ if block .Td .Cmp (self .Td ()) > 0 {
561+ // chain fork
562+ if block .ParentHash () != cblock .Hash () {
563+ // during split we merge two different chains and create the new canonical chain
564+ err := self .merge (cblock , block )
565+ if err != nil {
566+ return nonStatTy , err
567+ }
568+
569+ status = splitStatTy
570+ }
571+
572+ self .mu .Lock ()
573+ self .setTotalDifficulty (block .Td )
574+ self .insert (block )
575+ self .mu .Unlock ()
576+
577+ self .setTransState (state .New (block .Root (), self .stateDb ))
578+ self .txState .SetState (state .New (block .Root (), self .stateDb ))
579+
580+ status = canonStatTy
581+ } else {
582+ status = sideStatTy
583+ }
584+
585+ // Write block to database. Eventually we'll have to improve on this and throw away blocks that are
586+ // not in the canonical chain.
587+ self .mu .Lock ()
588+ self .enqueueForWrite (block )
589+ self .mu .Unlock ()
590+ // Delete from future blocks
591+ self .futureBlocks .Delete (block .Hash ())
592+
593+ return
594+ }
595+
544596// InsertChain will attempt to insert the given chain in to the canonical chain or, otherwise, create a fork. It an error is returned
545597// it will return the index number of the failing block as well an error describing what went wrong (for possible errors see core/errors.go).
546598func (self * ChainManager ) InsertChain (chain types.Blocks ) (int , error ) {
@@ -635,57 +687,29 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
635687
636688 txcount += len (block .Transactions ())
637689
638- cblock := self .currentBlock
639- // Compare the TD of the last known block in the canonical chain to make sure it's greater.
640- // At this point it's possible that a different chain (fork) becomes the new canonical chain.
641- if block .Td .Cmp (self .Td ()) > 0 {
642- // chain fork
643- if block .ParentHash () != cblock .Hash () {
644- // during split we merge two different chains and create the new canonical chain
645- err := self .merge (cblock , block )
646- if err != nil {
647- return i , err
648- }
649-
650- queue [i ] = ChainSplitEvent {block , logs }
651- queueEvent .splitCount ++
652- }
653-
654- self .mu .Lock ()
655- self .setTotalDifficulty (block .Td )
656- self .insert (block )
657- self .mu .Unlock ()
658-
659- jsonlogger .LogJson (& logger.EthChainNewHead {
660- BlockHash : block .Hash ().Hex (),
661- BlockNumber : block .Number (),
662- ChainHeadHash : cblock .Hash ().Hex (),
663- BlockPrevHash : block .ParentHash ().Hex (),
664- })
665-
666- self .setTransState (state .New (block .Root (), self .stateDb ))
667- self .txState .SetState (state .New (block .Root (), self .stateDb ))
668-
669- queue [i ] = ChainEvent {block , block .Hash (), logs }
670- queueEvent .canonicalCount ++
671-
690+ // write the block to the chain and get the status
691+ status , err := self .WriteBlock (block )
692+ if err != nil {
693+ return i , err
694+ }
695+ switch status {
696+ case canonStatTy :
672697 if glog .V (logger .Debug ) {
673698 glog .Infof ("[%v] inserted block #%d (%d TXs %d UNCs) (%x...). Took %v\n " , time .Now ().UnixNano (), block .Number (), len (block .Transactions ()), len (block .Uncles ()), block .Hash ().Bytes ()[0 :4 ], time .Since (bstart ))
674699 }
675- } else {
700+ queue [i ] = ChainEvent {block , block .Hash (), logs }
701+ queueEvent .canonicalCount ++
702+ case sideStatTy :
676703 if glog .V (logger .Detail ) {
677704 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 ))
678705 }
679-
680706 queue [i ] = ChainSideEvent {block , logs }
681707 queueEvent .sideCount ++
708+ case splitStatTy :
709+ queue [i ] = ChainSplitEvent {block , logs }
710+ queueEvent .splitCount ++
682711 }
683- self .enqueueForWrite (block )
684- // Delete from future blocks
685- self .futureBlocks .Delete (block .Hash ())
686-
687712 stats .processed ++
688- blockInsertTimer .UpdateSince (bstart )
689713 }
690714
691715 if (stats .queued > 0 || stats .processed > 0 || stats .ignored > 0 ) && bool (glog .V (logger .Info )) {
@@ -744,9 +768,9 @@ func (self *ChainManager) diff(oldBlock, newBlock *types.Block) (types.Blocks, e
744768 }
745769 }
746770
747- if glog .V (logger .Info ) {
771+ if glog .V (logger .Debug ) {
748772 commonHash := commonBlock .Hash ()
749- glog .Infof ("Fork detected @ %x. Reorganising chain from #%v %x to %x" , commonHash [:4 ], numSplit , oldStart .Hash ().Bytes ()[:4 ], newStart .Hash ().Bytes ()[:4 ])
773+ glog .Infof ("Chain split detected @ %x. Reorganising chain from #%v %x to %x" , commonHash [:4 ], numSplit , oldStart .Hash ().Bytes ()[:4 ], newStart .Hash ().Bytes ()[:4 ])
750774 }
751775
752776 return newChain , nil
0 commit comments