@@ -548,6 +548,58 @@ func (self *ChainManager) procFutureBlocks() {
548
548
}
549
549
}
550
550
551
+ type writeStatus byte
552
+
553
+ const (
554
+ nonStatTy writeStatus = iota
555
+ canonStatTy
556
+ splitStatTy
557
+ sideStatTy
558
+ )
559
+
560
+ func (self * ChainManager ) WriteBlock (block * types.Block ) (status writeStatus , err error ) {
561
+ self .wg .Add (1 )
562
+ defer self .wg .Done ()
563
+
564
+ cblock := self .currentBlock
565
+ // Compare the TD of the last known block in the canonical chain to make sure it's greater.
566
+ // At this point it's possible that a different chain (fork) becomes the new canonical chain.
567
+ if block .Td .Cmp (self .Td ()) > 0 {
568
+ // chain fork
569
+ if block .ParentHash () != cblock .Hash () {
570
+ // during split we merge two different chains and create the new canonical chain
571
+ err := self .merge (cblock , block )
572
+ if err != nil {
573
+ return nonStatTy , err
574
+ }
575
+
576
+ status = splitStatTy
577
+ }
578
+
579
+ self .mu .Lock ()
580
+ self .setTotalDifficulty (block .Td )
581
+ self .insert (block )
582
+ self .mu .Unlock ()
583
+
584
+ self .setTransState (state .New (block .Root (), self .stateDb ))
585
+ self .txState .SetState (state .New (block .Root (), self .stateDb ))
586
+
587
+ status = canonStatTy
588
+ } else {
589
+ status = sideStatTy
590
+ }
591
+
592
+ // Write block to database. Eventually we'll have to improve on this and throw away blocks that are
593
+ // not in the canonical chain.
594
+ self .mu .Lock ()
595
+ self .write (block )
596
+ self .mu .Unlock ()
597
+ // Delete from future blocks
598
+ self .futureBlocks .Delete (block .Hash ())
599
+
600
+ return
601
+ }
602
+
551
603
// InsertChain will attempt to insert the given chain in to the canonical chain or, otherwise, create a fork. It an error is returned
552
604
// 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).
553
605
func (self * ChainManager ) InsertChain (chain types.Blocks ) (int , error ) {
@@ -641,59 +693,29 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
641
693
642
694
txcount += len (block .Transactions ())
643
695
644
- cblock := self .currentBlock
645
- // Compare the TD of the last known block in the canonical chain to make sure it's greater.
646
- // At this point it's possible that a different chain (fork) becomes the new canonical chain.
647
- if block .Td .Cmp (self .Td ()) > 0 {
648
- // chain fork
649
- if block .ParentHash () != cblock .Hash () {
650
- // during split we merge two different chains and create the new canonical chain
651
- err := self .merge (cblock , block )
652
- if err != nil {
653
- return i , err
654
- }
655
-
656
- queue [i ] = ChainSplitEvent {block , logs }
657
- queueEvent .splitCount ++
658
- }
659
-
660
- self .mu .Lock ()
661
- self .setTotalDifficulty (block .Td )
662
- self .insert (block )
663
- self .mu .Unlock ()
664
-
665
- jsonlogger .LogJson (& logger.EthChainNewHead {
666
- BlockHash : block .Hash ().Hex (),
667
- BlockNumber : block .Number (),
668
- ChainHeadHash : cblock .Hash ().Hex (),
669
- BlockPrevHash : block .ParentHash ().Hex (),
670
- })
671
-
672
- self .setTransState (state .New (block .Root (), self .stateDb ))
673
- self .txState .SetState (state .New (block .Root (), self .stateDb ))
674
-
675
- queue [i ] = ChainEvent {block , block .Hash (), logs }
676
- queueEvent .canonicalCount ++
677
-
696
+ // write the block to the chain and get the status
697
+ status , err := self .WriteBlock (block )
698
+ if err != nil {
699
+ return i , err
700
+ }
701
+ switch status {
702
+ case canonStatTy :
678
703
if glog .V (logger .Debug ) {
679
704
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 ))
680
705
}
681
- } else {
706
+ queue [i ] = ChainEvent {block , block .Hash (), logs }
707
+ queueEvent .canonicalCount ++
708
+ case sideStatTy :
682
709
if glog .V (logger .Detail ) {
683
710
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 ))
684
711
}
685
-
686
712
queue [i ] = ChainSideEvent {block , logs }
687
713
queueEvent .sideCount ++
714
+ case splitStatTy :
715
+ queue [i ] = ChainSplitEvent {block , logs }
716
+ queueEvent .splitCount ++
688
717
}
689
- // Write block to database. Eventually we'll have to improve on this and throw away blocks that are
690
- // not in the canonical chain.
691
- self .write (block )
692
- // Delete from future blocks
693
- self .futureBlocks .Delete (block .Hash ())
694
-
695
718
stats .processed ++
696
- blockInsertTimer .UpdateSince (bstart )
697
719
}
698
720
699
721
if (stats .queued > 0 || stats .processed > 0 || stats .ignored > 0 ) && bool (glog .V (logger .Info )) {
@@ -752,9 +774,9 @@ func (self *ChainManager) diff(oldBlock, newBlock *types.Block) (types.Blocks, e
752
774
}
753
775
}
754
776
755
- if glog .V (logger .Info ) {
777
+ if glog .V (logger .Debug ) {
756
778
commonHash := commonBlock .Hash ()
757
- glog .Infof ("Fork detected @ %x. Reorganising chain from #%v %x to %x" , commonHash [:4 ], numSplit , oldStart .Hash ().Bytes ()[:4 ], newStart .Hash ().Bytes ()[:4 ])
779
+ glog .Infof ("Chain split detected @ %x. Reorganising chain from #%v %x to %x" , commonHash [:4 ], numSplit , oldStart .Hash ().Bytes ()[:4 ], newStart .Hash ().Bytes ()[:4 ])
758
780
}
759
781
760
782
return newChain , nil
0 commit comments