Skip to content

Commit

Permalink
core: fix setHead, panics
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusVanDerWijden committed Jul 18, 2022
1 parent b0bb30c commit 1e924f2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
27 changes: 23 additions & 4 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ func (bc *BlockChain) loadLastState() error {
}
}

// Restore the last known finalized block amd safe block
// Restore the last known finalized block and safe block
// Note: the safe block is not stored on disk and it is set to the last
// known finalized block on startup
if head := rawdb.ReadFinalizedBlockHash(bc.db); head != (common.Hash{}) {
Expand Down Expand Up @@ -511,14 +511,23 @@ func (bc *BlockChain) SetHead(head uint64) error {
// SetFinalized sets the finalized block.
func (bc *BlockChain) SetFinalized(block *types.Block) {
bc.currentFinalizedBlock.Store(block)
rawdb.WriteFinalizedBlockHash(bc.db, block.Hash())
headFinalizedBlockGauge.Update(int64(block.NumberU64()))
if block != nil {
rawdb.WriteFinalizedBlockHash(bc.db, block.Hash())
headFinalizedBlockGauge.Update(int64(block.NumberU64()))
} else {
rawdb.WriteFinalizedBlockHash(bc.db, common.Hash{})
headFinalizedBlockGauge.Update(0)
}
}

// SetSafe sets the safe block.
func (bc *BlockChain) SetSafe(block *types.Block) {
bc.currentSafeBlock.Store(block)
headSafeBlockGauge.Update(int64(block.NumberU64()))
if block != nil {
headSafeBlockGauge.Update(int64(block.NumberU64()))
} else {
headSafeBlockGauge.Update(0)
}
}

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

// Clear safe block, finalized block if needed
if safe := bc.CurrentSafeBlock(); safe != nil && head < safe.NumberU64() {
log.Warn("SetHead invalidated safe block")
bc.SetSafe(nil)
}
if finalized := bc.CurrentFinalizedBlock(); finalized != nil && head < finalized.NumberU64() {
log.Error("SetHead invalidated finalized block")
bc.SetFinalized(nil)
}

return rootNumber, bc.loadLastState()
}

Expand Down
12 changes: 10 additions & 2 deletions eth/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,18 @@ func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumb
return b.eth.blockchain.CurrentBlock().Header(), nil
}
if number == rpc.FinalizedBlockNumber {
return b.eth.blockchain.CurrentFinalizedBlock().Header(), nil
block := b.eth.blockchain.CurrentFinalizedBlock()
if block != nil {
return block.Header(), nil
}
return nil, errors.New("finalized block not found")
}
if number == rpc.SafeBlockNumber {
return b.eth.blockchain.CurrentSafeBlock().Header(), nil
block := b.eth.blockchain.CurrentSafeBlock()
if block != nil {
return block.Header(), nil
}
return nil, errors.New("safe block not found")
}
return b.eth.blockchain.GetHeaderByNumber(uint64(number)), nil
}
Expand Down

0 comments on commit 1e924f2

Please sign in to comment.