Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1105,11 +1105,12 @@ func (bc *BlockChain) setHeadBeyondRoot(head uint64, time uint64, root common.Ha
bc.txLookupCache.Purge()

// Clear safe block, finalized block if needed
if safe := bc.CurrentSafeBlock(); safe != nil && head < safe.Number.Uint64() {
headBlock := bc.CurrentBlock()
if safe := bc.CurrentSafeBlock(); safe != nil && headBlock.Number.Uint64() < safe.Number.Uint64() {
log.Warn("SetHead invalidated safe block")
bc.SetSafe(nil)
}
if finalized := bc.CurrentFinalBlock(); finalized != nil && head < finalized.Number.Uint64() {
if finalized := bc.CurrentFinalBlock(); finalized != nil && headBlock.Number.Uint64() < finalized.Number.Uint64() {
log.Error("SetHead invalidated finalized block")
bc.SetFinalized(nil)
}
Expand Down
42 changes: 42 additions & 0 deletions core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4515,3 +4515,45 @@ func TestGetCanonicalReceipt(t *testing.T) {
}
}
}

// TestSetHeadBeyondRootFinalizedBug tests the issue where the finalized block
// is not cleared when rewinding past it using setHeadBeyondRoot.
func TestSetHeadBeyondRootFinalizedBug(t *testing.T) {
// Create a clean blockchain with 100 blocks using PathScheme (PBSS)
_, _, blockchain, err := newCanonical(ethash.NewFaker(), 100, true, rawdb.PathScheme)
if err != nil {
t.Fatalf("failed to create pristine chain: %v", err)
}
defer blockchain.Stop()

// Set the "Finalized" marker to the current Head (Block 100)
headBlock := blockchain.CurrentBlock()
if headBlock.Number.Uint64() != 100 {
t.Fatalf("Setup failed: expected head 100, got %d", headBlock.Number.Uint64())
}
blockchain.SetFinalized(headBlock)

// Verify setup
if blockchain.CurrentFinalBlock().Number.Uint64() != 100 {
t.Fatalf("Setup failed: Finalized block should be 100")
}
targetBlock := blockchain.GetBlockByNumber(50)

// Call setHeadBeyondRoot with:
// head = 100
// repair = true
if _, err := blockchain.setHeadBeyondRoot(100, 0, targetBlock.Root(), true); err != nil {
t.Fatalf("Failed to rewind: %v", err)
}

currentFinal := blockchain.CurrentFinalBlock()
currentHead := blockchain.CurrentBlock().Number.Uint64()

// The previous finalized block (100) is now invalid because we rewound to 50.
// The function should have cleared the finalized marker (set to nil).
if currentFinal != nil && currentFinal.Number.Uint64() > currentHead {
t.Errorf("Chain Head: %d , Finalized Block: %d , Finalized block was >= head block.",
currentHead,
currentFinal.Number.Uint64())
}
}