@@ -179,7 +179,9 @@ func (self *ChainManager) Td() *big.Int {
179
179
}
180
180
181
181
func (self * ChainManager ) GasLimit () * big.Int {
182
- // return self.currentGasLimit
182
+ self .mu .RLock ()
183
+ defer self .mu .RUnlock ()
184
+
183
185
return self .currentBlock .GasLimit ()
184
186
}
185
187
@@ -376,6 +378,8 @@ func (self *ChainManager) ExportN(w io.Writer, first uint64, last uint64) error
376
378
return nil
377
379
}
378
380
381
+ // insert injects a block into the current chain block chain. Note, this function
382
+ // assumes that the `mu` mutex is held!
379
383
func (bc * ChainManager ) insert (block * types.Block ) {
380
384
key := append (blockNumPre , block .Number ().Bytes ()... )
381
385
bc .blockDb .Put (key , block .Hash ().Bytes ())
@@ -484,6 +488,8 @@ func (self *ChainManager) GetAncestors(block *types.Block, length int) (blocks [
484
488
return
485
489
}
486
490
491
+ // setTotalDifficulty updates the TD of the chain manager. Note, this function
492
+ // assumes that the `mu` mutex is held!
487
493
func (bc * ChainManager ) setTotalDifficulty (td * big.Int ) {
488
494
bc .td = new (big.Int ).Set (td )
489
495
}
@@ -540,9 +546,6 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
540
546
self .wg .Add (1 )
541
547
defer self .wg .Done ()
542
548
543
- self .mu .Lock ()
544
- defer self .mu .Unlock ()
545
-
546
549
self .chainmu .Lock ()
547
550
defer self .chainmu .Unlock ()
548
551
@@ -625,7 +628,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
625
628
cblock := self .currentBlock
626
629
// Compare the TD of the last known block in the canonical chain to make sure it's greater.
627
630
// At this point it's possible that a different chain (fork) becomes the new canonical chain.
628
- if block .Td .Cmp (self .td ) > 0 {
631
+ if block .Td .Cmp (self .Td () ) > 0 {
629
632
// chain fork
630
633
if block .ParentHash () != cblock .Hash () {
631
634
// during split we merge two different chains and create the new canonical chain
@@ -638,8 +641,10 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
638
641
queueEvent .splitCount ++
639
642
}
640
643
644
+ self .mu .Lock ()
641
645
self .setTotalDifficulty (block .Td )
642
646
self .insert (block )
647
+ self .mu .Unlock ()
643
648
644
649
jsonlogger .LogJson (& logger.EthChainNewHead {
645
650
BlockHash : block .Hash ().Hex (),
@@ -747,9 +752,11 @@ func (self *ChainManager) merge(oldBlock, newBlock *types.Block) error {
747
752
}
748
753
749
754
// insert blocks. Order does not matter. Last block will be written in ImportChain itself which creates the new head properly
755
+ self .mu .Lock ()
750
756
for _ , block := range newChain {
751
757
self .insert (block )
752
758
}
759
+ self .mu .Unlock ()
753
760
754
761
return nil
755
762
}
0 commit comments