@@ -76,7 +76,7 @@ static const unsigned int EXTRA_DESCENDANT_TX_SIZE_LIMIT = 10000;
7676/* * Maximum kilobytes for transactions to store for processing during reorg */
7777static const unsigned int MAX_DISCONNECTED_TX_POOL_SIZE = 20000 ;
7878/* * Time to wait between writing blocks/block index to disk. */
79- static constexpr std::chrono::hours DATABASE_WRITE_INTERVAL{1 };
79+ static constexpr std::chrono::minutes DATABASE_WRITE_INTERVAL{5 };
8080/* * Time to wait between flushing chainstate to disk. */
8181static constexpr std::chrono::hours DATABASE_FLUSH_INTERVAL{24 };
8282/* * Maximum age of our tip for us to be considered current for fee estimation */
@@ -2346,13 +2346,40 @@ bool CChainState::FlushStateToDisk(
23462346 }
23472347 std::vector<const CBlockIndex*> vBlocks;
23482348 vBlocks.reserve (setDirtyBlockIndex.size ());
2349+ std::set<CBlockIndex*> setTrimmableBlockIndex (setDirtyBlockIndex);
23492350 for (std::set<CBlockIndex*>::iterator it = setDirtyBlockIndex.begin (); it != setDirtyBlockIndex.end (); ) {
23502351 vBlocks.push_back (*it);
23512352 setDirtyBlockIndex.erase (it++);
23522353 }
23532354 if (!pblocktree->WriteBatchSync (vFiles, nLastBlockFile, vBlocks)) {
23542355 return AbortNode (state, " Failed to write to block index database" );
23552356 }
2357+
2358+ if (fTrimHeaders ) {
2359+ LogPrintf (" Flushing block index, trimming headers, setTrimmableBlockIndex.size(): %d\n " , setTrimmableBlockIndex.size ());
2360+ int trim_height = m_chain.Height () - nMustKeepFullHeaders;
2361+ int min_height = std::numeric_limits<int >::max ();
2362+ CBlockIndex* min_index = nullptr ;
2363+ for (std::set<CBlockIndex*>::iterator it = setTrimmableBlockIndex.begin (); it != setTrimmableBlockIndex.end (); it++) {
2364+ (*it)->assert_untrimmed ();
2365+ if ((*it)->nHeight < trim_height) {
2366+ (*it)->trim ();
2367+ if ((*it)->nHeight < min_height) {
2368+ min_height = (*it)->nHeight ;
2369+ min_index = *it;
2370+ }
2371+ }
2372+ }
2373+
2374+ // Handle any remaining untrimmed blocks that were too recent for trimming last time we flushed.
2375+ if (min_index) {
2376+ min_index = min_index->pprev ;
2377+ while (min_index && !min_index->trimmed ()) {
2378+ min_index->trim ();
2379+ min_index = min_index->pprev ;
2380+ }
2381+ }
2382+ }
23562383 }
23572384 // Finally remove any pruned files
23582385 if (fFlushForPrune ) {
0 commit comments