Skip to content

Commit 1e924f2

Browse files
core: fix setHead, panics
1 parent b0bb30c commit 1e924f2

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

core/blockchain.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ func (bc *BlockChain) loadLastState() error {
467467
}
468468
}
469469

470-
// Restore the last known finalized block amd safe block
470+
// Restore the last known finalized block and safe block
471471
// Note: the safe block is not stored on disk and it is set to the last
472472
// known finalized block on startup
473473
if head := rawdb.ReadFinalizedBlockHash(bc.db); head != (common.Hash{}) {
@@ -511,14 +511,23 @@ func (bc *BlockChain) SetHead(head uint64) error {
511511
// SetFinalized sets the finalized block.
512512
func (bc *BlockChain) SetFinalized(block *types.Block) {
513513
bc.currentFinalizedBlock.Store(block)
514-
rawdb.WriteFinalizedBlockHash(bc.db, block.Hash())
515-
headFinalizedBlockGauge.Update(int64(block.NumberU64()))
514+
if block != nil {
515+
rawdb.WriteFinalizedBlockHash(bc.db, block.Hash())
516+
headFinalizedBlockGauge.Update(int64(block.NumberU64()))
517+
} else {
518+
rawdb.WriteFinalizedBlockHash(bc.db, common.Hash{})
519+
headFinalizedBlockGauge.Update(0)
520+
}
516521
}
517522

518523
// SetSafe sets the safe block.
519524
func (bc *BlockChain) SetSafe(block *types.Block) {
520525
bc.currentSafeBlock.Store(block)
521-
headSafeBlockGauge.Update(int64(block.NumberU64()))
526+
if block != nil {
527+
headSafeBlockGauge.Update(int64(block.NumberU64()))
528+
} else {
529+
headSafeBlockGauge.Update(0)
530+
}
522531
}
523532

524533
// setHeadBeyondRoot rewinds the local chain to a new head with the extra condition
@@ -676,6 +685,16 @@ func (bc *BlockChain) setHeadBeyondRoot(head uint64, root common.Hash, repair bo
676685
bc.txLookupCache.Purge()
677686
bc.futureBlocks.Purge()
678687

688+
// Clear safe block, finalized block if needed
689+
if safe := bc.CurrentSafeBlock(); safe != nil && head < safe.NumberU64() {
690+
log.Warn("SetHead invalidated safe block")
691+
bc.SetSafe(nil)
692+
}
693+
if finalized := bc.CurrentFinalizedBlock(); finalized != nil && head < finalized.NumberU64() {
694+
log.Error("SetHead invalidated finalized block")
695+
bc.SetFinalized(nil)
696+
}
697+
679698
return rootNumber, bc.loadLastState()
680699
}
681700

eth/api_backend.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,18 @@ func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumb
7474
return b.eth.blockchain.CurrentBlock().Header(), nil
7575
}
7676
if number == rpc.FinalizedBlockNumber {
77-
return b.eth.blockchain.CurrentFinalizedBlock().Header(), nil
77+
block := b.eth.blockchain.CurrentFinalizedBlock()
78+
if block != nil {
79+
return block.Header(), nil
80+
}
81+
return nil, errors.New("finalized block not found")
7882
}
7983
if number == rpc.SafeBlockNumber {
80-
return b.eth.blockchain.CurrentSafeBlock().Header(), nil
84+
block := b.eth.blockchain.CurrentSafeBlock()
85+
if block != nil {
86+
return block.Header(), nil
87+
}
88+
return nil, errors.New("safe block not found")
8189
}
8290
return b.eth.blockchain.GetHeaderByNumber(uint64(number)), nil
8391
}

0 commit comments

Comments
 (0)