validation: randomly compact chainstate

Full chainstate flushes are convenient maintenance points for long-term LevelDB cleanup because the chainstate was just written.
Randomize the trigger so nodes that flush near the same height do not compact together.

Add blocking chainstate compaction through `CCoinsViewDB::CompactFull()` and give each post-IBD full flush on the normal chainstate a 1/320 chance to start compaction.
With hourly flushes this averages roughly every two weeks and makes a six-month miss about one in a million.
This keeps the schedule stateless and leaves last-compaction height or timestamp bookkeeping out of chainstate metadata.

Co-authored-by: Andrew Toth <andrewstoth@gmail.com>
This commit is contained in:
Lőrinc
2026-06-08 15:46:45 +02:00
parent b10889d107
commit aa021b26f3
6 changed files with 37 additions and 4 deletions

View File

@@ -256,7 +256,7 @@ CDBWrapper::CDBWrapper(const DBParams& params)
if (params.options.force_compact) {
LogInfo("Starting database compaction of %s", fs::PathToString(params.path));
DBContext().pdb->CompactRange(nullptr, nullptr);
CompactFull();
LogInfo("Finished database compaction of %s", fs::PathToString(params.path));
}
@@ -307,6 +307,8 @@ std::optional<std::string> CDBWrapper::GetProperty(const std::string& property)
return std::nullopt;
}
void CDBWrapper::CompactFull() { DBContext().pdb->CompactRange(nullptr, nullptr); }
size_t CDBWrapper::DynamicMemoryUsage() const
{
std::optional<size_t> parsed;