diff --git a/src/validation.cpp b/src/validation.cpp index a0638af046..21c4fd5521 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -76,7 +76,7 @@ static const unsigned int EXTRA_DESCENDANT_TX_SIZE_LIMIT = 10000; /** Maximum kilobytes for transactions to store for processing during reorg */ static const unsigned int MAX_DISCONNECTED_TX_POOL_SIZE = 20000; /** Time to wait between writing blocks/block index to disk. */ -static constexpr std::chrono::hours DATABASE_WRITE_INTERVAL{1}; +static constexpr std::chrono::minutes DATABASE_WRITE_INTERVAL{5}; /** Time to wait between flushing chainstate to disk. */ static constexpr std::chrono::hours DATABASE_FLUSH_INTERVAL{24}; /** Maximum age of our tip for us to be considered current for fee estimation */ @@ -2346,6 +2346,7 @@ bool CChainState::FlushStateToDisk( } std::vector vBlocks; vBlocks.reserve(setDirtyBlockIndex.size()); + std::set setTrimmableBlockIndex(setDirtyBlockIndex); for (std::set::iterator it = setDirtyBlockIndex.begin(); it != setDirtyBlockIndex.end(); ) { vBlocks.push_back(*it); setDirtyBlockIndex.erase(it++); @@ -2353,6 +2354,32 @@ bool CChainState::FlushStateToDisk( if (!pblocktree->WriteBatchSync(vFiles, nLastBlockFile, vBlocks)) { return AbortNode(state, "Failed to write to block index database"); } + + if (fTrimHeaders) { + LogPrintf("Flushing block index, trimming headers, setTrimmableBlockIndex.size(): %d\n", setTrimmableBlockIndex.size()); + int trim_height = m_chain.Height() - nMustKeepFullHeaders; + int min_height = std::numeric_limits::max(); + CBlockIndex* min_index = nullptr; + for (std::set::iterator it = setTrimmableBlockIndex.begin(); it != setTrimmableBlockIndex.end(); it++) { + (*it)->assert_untrimmed(); + if ((*it)->nHeight < trim_height) { + (*it)->trim(); + if ((*it)->nHeight < min_height) { + min_height = (*it)->nHeight; + min_index = *it; + } + } + } + + // Handle any remaining untrimmed blocks that were too recent for trimming last time we flushed. + if (min_index) { + min_index = min_index->pprev; + while (min_index && !min_index->trimmed()) { + min_index->trim(); + min_index = min_index->pprev; + } + } + } } // Finally remove any pruned files if (fFlushForPrune) {