@@ -411,28 +411,35 @@ func (self *BlockChain) ExportN(w io.Writer, first uint64, last uint64) error {
411
411
412
412
// insert injects a new head block into the current block chain. This method
413
413
// assumes that the block is indeed a true head. It will also reset the head
414
- // header and the head fast sync block to this very same block to prevent them
415
- // from pointing to a possibly old canonical chain (i.e. side chain by now) .
414
+ // header and the head fast sync block to this very same block if they are older
415
+ // or if they are on a different side chain.
416
416
//
417
417
// Note, this function assumes that the `mu` mutex is held!
418
418
func (bc * BlockChain ) insert (block * types.Block ) {
419
+ // If the block is on a side chain or an unknown one, force other heads onto it too
420
+ updateHeads := GetCanonicalHash (bc .chainDb , block .NumberU64 ()) != block .Hash ()
421
+
419
422
// Add the block to the canonical chain number scheme and mark as the head
420
423
if err := WriteCanonicalHash (bc .chainDb , block .Hash (), block .NumberU64 ()); err != nil {
421
424
glog .Fatalf ("failed to insert block number: %v" , err )
422
425
}
423
426
if err := WriteHeadBlockHash (bc .chainDb , block .Hash ()); err != nil {
424
427
glog .Fatalf ("failed to insert head block hash: %v" , err )
425
428
}
426
- if err := WriteHeadHeaderHash (bc .chainDb , block .Hash ()); err != nil {
427
- glog .Fatalf ("failed to insert head header hash: %v" , err )
428
- }
429
- if err := WriteHeadFastBlockHash (bc .chainDb , block .Hash ()); err != nil {
430
- glog .Fatalf ("failed to insert head fast block hash: %v" , err )
431
- }
432
- // Update the internal state with the head block
433
429
bc .currentBlock = block
434
- bc .currentHeader = block .Header ()
435
- bc .currentFastBlock = block
430
+
431
+ // If the block is better than out head or is on a different chain, force update heads
432
+ if updateHeads {
433
+ if err := WriteHeadHeaderHash (bc .chainDb , block .Hash ()); err != nil {
434
+ glog .Fatalf ("failed to insert head header hash: %v" , err )
435
+ }
436
+ bc .currentHeader = block .Header ()
437
+
438
+ if err := WriteHeadFastBlockHash (bc .chainDb , block .Hash ()); err != nil {
439
+ glog .Fatalf ("failed to insert head fast block hash: %v" , err )
440
+ }
441
+ bc .currentFastBlock = block
442
+ }
436
443
}
437
444
438
445
// Accessors
0 commit comments