dont do runtime pruning

This commit is contained in:
Sina Mahmoodi 2026-04-01 15:40:46 +00:00
parent b4156b31cc
commit 43cc92e68a
2 changed files with 39 additions and 32 deletions

View file

@ -764,22 +764,20 @@ func (bc *BlockChain) initializeHistoryPruning(latest uint64) error {
log.Error("Database pruned beyond configured history mode", "tail", freezerTail, "target", target, "mode", policy.Mode) log.Error("Database pruned beyond configured history mode", "tail", freezerTail, "target", target, "mode", policy.Mode)
return fmt.Errorf("database pruned beyond requested history (tail=%d, target=%d)", freezerTail, target) return fmt.Errorf("database pruned beyond requested history (tail=%d, target=%d)", freezerTail, target)
} }
// Need to prune (freezerTail < target). Ensure the target is frozen. // Need to prune (freezerTail < target).
if latest < target+params.FullImmutabilityThreshold { switch policy.Mode {
return fmt.Errorf("chain not far enough past target block %d, need %d more blocks", case history.KeepPostMerge, history.KeepPostPrague:
target, target+params.FullImmutabilityThreshold-latest) // Static modes require the user to run 'geth prune-history' offline
} // rather than blocking startup for hours (tx index pruning is slow).
// For static prune points, verify the canonical hash matches. return fmt.Errorf("history not pruned to target block %d (current tail %d), run 'geth prune-history --history.chain=%s' first", target, freezerTail, policy.Mode)
if policy.Target != nil {
hash := bc.GetCanonicalHash(target) case history.KeepRecent:
if hash != policy.Target.BlockHash { // The rolling pruner will gradually catch up in the background.
return fmt.Errorf("target block hash mismatch at block %d: got %s, want %s", target, hash.Hex(), policy.Target.BlockHash.Hex()) if freezerTail > 0 {
bc.updateHistoryPrunePoint(freezerTail)
} }
log.Warn("Chain history is behind pruning target, rolling pruner will catch up", "tail", freezerTail, "target", target)
} }
if err := bc.pruneChainHistory(target); err != nil {
return fmt.Errorf("failed to prune chain history: %w", err)
}
log.Info("Pruned chain history at startup", "from", freezerTail, "to", target)
return nil return nil
} }

View file

@ -156,36 +156,45 @@ func TestPruneChainHistory(t *testing.T) {
} }
} }
func TestInitHistoryPruningStartupPrune(t *testing.T) { func TestInitHistoryPruningStaticModeRequiresPruneHistory(t *testing.T) {
if testing.Short() { db, gspec, blocks := newTestChain(t, 200)
t.Skip("skipping slow test")
}
db, gspec, blocks := newTestChain(t, 91000)
defer db.Close() defer db.Close()
// Reopen with a static target at block 500. The chain is long enough // Reopen with a static target at block 50. The database is not yet
// (91000 >= 500 + 90000) so initializeHistoryPruning should prune. // pruned to that target, so startup should fail and tell the user to
// run 'geth prune-history'.
policy := history.HistoryPolicy{ policy := history.HistoryPolicy{
Mode: history.KeepPostMerge, Mode: history.KeepPostMerge,
Target: &history.PrunePoint{ Target: &history.PrunePoint{
BlockNumber: 500, BlockNumber: 50,
BlockHash: blocks[499].Hash(), BlockHash: blocks[49].Hash(),
}, },
} }
_, err := reopenChain(db, gspec, policy)
if err == nil {
t.Fatal("expected error when history not pruned to static target, got nil")
}
// Freezer tail should remain at 0 — no pruning happened.
tail, _ := db.Tail()
if tail != 0 {
t.Errorf("freezer tail: got %d, want 0 (startup should not prune)", tail)
}
}
func TestInitHistoryPruningKeepRecentAllowsStartup(t *testing.T) {
db, gspec, _ := newTestChain(t, 200)
defer db.Close()
// Reopen with KeepRecent and a small window. The tail (0) is behind the
// target but KeepRecent should still allow startup — the rolling pruner
// handles catch-up in the background.
policy := history.HistoryPolicy{Mode: history.KeepRecent, Window: 50}
chain, err := reopenChain(db, gspec, policy) chain, err := reopenChain(db, gspec, policy)
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
defer chain.Stop() defer chain.Stop()
tail, _ := db.Tail()
if tail != 500 {
t.Errorf("freezer tail: got %d, want 500", tail)
}
cutoff, _ := chain.HistoryPruningCutoff()
if cutoff != 500 {
t.Errorf("prune cutoff: got %d, want 500", cutoff)
}
} }
func TestInitHistoryPruningStaticModeBeyondTarget(t *testing.T) { func TestInitHistoryPruningStaticModeBeyondTarget(t *testing.T) {