@@ -197,9 +197,6 @@ CBlockIndex* FindForkInGlobalIndex(const CChain& chain, const CBlockLocator& loc
197
197
198
198
std::unique_ptr<CBlockTreeDB> pblocktree;
199
199
200
- // See definition for documentation
201
- static void FindFilesToPruneManual (ChainstateManager& chainman, std::set<int >& setFilesToPrune, int nManualPruneHeight);
202
- static void FindFilesToPrune (ChainstateManager& chainman, std::set<int >& setFilesToPrune, uint64_t nPruneAfterHeight);
203
200
bool CheckInputScripts (const CTransaction& tx, TxValidationState &state, const CCoinsViewCache &inputs, unsigned int flags, bool cacheSigStore, bool cacheFullScriptStore, PrecomputedTransactionData& txdata, std::vector<CScriptCheck> *pvChecks = nullptr );
204
201
static FILE* OpenUndoFile (const FlatFilePos &pos, bool fReadOnly = false );
205
202
static FlatFileSeq BlockFileSeq ();
@@ -2284,14 +2281,53 @@ bool CChainState::FlushStateToDisk(
2284
2281
CoinsCacheSizeState cache_state = GetCoinsCacheSizeState (&m_mempool);
2285
2282
LOCK (cs_LastBlockFile);
2286
2283
if (fPruneMode && (fCheckForPruning || nManualPruneHeight > 0 ) && !fReindex ) {
2284
+ // Previously, we called the global function ::ChainActive() in
2285
+ // FindFilesToPrune{,Manual} to get the tip height and to determine
2286
+ // whether or not a tip even exists. Now, we are simply passing in
2287
+ // m_chain.Height() (which returns -1 if the tip doesn't exist). To
2288
+ // make sure we're not changing behaviour, let's check that
2289
+ // ::ChainActive() is the same object as m_chain (not just
2290
+ // identical).
2291
+ //
2292
+ // This comment and the following assert will be removed in a
2293
+ // subsequent commit, as they're just meant to demonstrate
2294
+ // correctness (you can run tests against it and see that nothing
2295
+ // exit unexpectedly).
2296
+ assert (std::addressof (::ChainActive ()) == std::addressof (m_chain));
2287
2297
if (nManualPruneHeight > 0 ) {
2288
2298
LOG_TIME_MILLIS_WITH_CATEGORY (" find files to prune (manual)" , BCLog::BENCH);
2289
2299
2290
- FindFilesToPruneManual (g_chainman, setFilesToPrune, nManualPruneHeight);
2300
+ m_blockman. FindFilesToPruneManual (setFilesToPrune, nManualPruneHeight, m_chain. Height () );
2291
2301
} else {
2292
2302
LOG_TIME_MILLIS_WITH_CATEGORY (" find files to prune" , BCLog::BENCH);
2293
2303
2294
- FindFilesToPrune (g_chainman, setFilesToPrune, chainparams.PruneAfterHeight ());
2304
+ // Previously, we called the global function
2305
+ // ::ChainstateActive() in FindFilesToPrune{,Manual} to get the
2306
+ // IBD status. Now, we are simply passing in
2307
+ // IsInitialBlockDownload(). To make sure we're not changing
2308
+ // behaviour, let's check that ::ChainstateActive() is the same
2309
+ // object as *this (not just identical).
2310
+ //
2311
+ // This comment and the following assert will be removed in a
2312
+ // subsequent commit, as they're just meant to demonstrate
2313
+ // correctness (you can run tests against it and see that
2314
+ // nothing exit unexpectedly).
2315
+ assert (std::addressof (::ChainstateActive ()) == std::addressof (*this ));
2316
+
2317
+ // Previously, we called PruneOneBlockFile on g_chainman's
2318
+ // m_blockman in FindFilesToPrune{,Manual}. Now, we are instead
2319
+ // calling PruneOneBlockFile on _our_ m_blockman. To make sure
2320
+ // we're not changing behaviour, let's check that
2321
+ // g_chainman.m_blockman is the same object as _our_ m_blockman
2322
+ // (not just identical).
2323
+ //
2324
+ // This comment and the following assert will be removed in a
2325
+ // subsequent commit, as they're just meant to demonstrate
2326
+ // correctness (you can run tests against it and see that
2327
+ // nothing exit unexpectedly).
2328
+ assert (std::addressof (g_chainman.m_blockman ) == std::addressof (m_blockman));
2329
+
2330
+ m_blockman.FindFilesToPrune (setFilesToPrune, chainparams.PruneAfterHeight (), m_chain.Height (), IsInitialBlockDownload ());
2295
2331
fCheckForPruning = false ;
2296
2332
}
2297
2333
if (!setFilesToPrune.empty ()) {
@@ -3958,21 +3994,21 @@ void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune)
3958
3994
}
3959
3995
3960
3996
/* Calculate the block/rev files to delete based on height specified by user with RPC command pruneblockchain */
3961
- static void FindFilesToPruneManual (ChainstateManager& chainman, std::set<int >& setFilesToPrune, int nManualPruneHeight)
3997
+ void BlockManager:: FindFilesToPruneManual (std::set<int >& setFilesToPrune, int nManualPruneHeight, int chain_tip_height )
3962
3998
{
3963
3999
assert (fPruneMode && nManualPruneHeight > 0 );
3964
4000
3965
4001
LOCK2 (cs_main, cs_LastBlockFile);
3966
- if (:: ChainActive (). Tip () == nullptr )
4002
+ if (chain_tip_height < 0 )
3967
4003
return ;
3968
4004
3969
4005
// last block to prune is the lesser of (user-specified height, MIN_BLOCKS_TO_KEEP from the tip)
3970
- unsigned int nLastBlockWeCanPrune = std::min ((unsigned )nManualPruneHeight, :: ChainActive (). Tip ()-> nHeight - MIN_BLOCKS_TO_KEEP);
4006
+ unsigned int nLastBlockWeCanPrune = std::min ((unsigned )nManualPruneHeight, chain_tip_height - MIN_BLOCKS_TO_KEEP);
3971
4007
int count=0 ;
3972
4008
for (int fileNumber = 0 ; fileNumber < nLastBlockFile; fileNumber++) {
3973
4009
if (vinfoBlockFile[fileNumber].nSize == 0 || vinfoBlockFile[fileNumber].nHeightLast > nLastBlockWeCanPrune)
3974
4010
continue ;
3975
- chainman. m_blockman . PruneOneBlockFile (fileNumber);
4011
+ PruneOneBlockFile (fileNumber);
3976
4012
setFilesToPrune.insert (fileNumber);
3977
4013
count++;
3978
4014
}
@@ -4005,17 +4041,17 @@ void PruneBlockFilesManual(int nManualPruneHeight)
4005
4041
*
4006
4042
* @param[out] setFilesToPrune The set of file indices that can be unlinked will be returned
4007
4043
*/
4008
- static void FindFilesToPrune (ChainstateManager& chainman, std::set<int >& setFilesToPrune, uint64_t nPruneAfterHeight)
4044
+ void BlockManager:: FindFilesToPrune (std::set<int >& setFilesToPrune, uint64_t nPruneAfterHeight, int chain_tip_height, bool is_ibd )
4009
4045
{
4010
4046
LOCK2 (cs_main, cs_LastBlockFile);
4011
- if (:: ChainActive (). Tip () == nullptr || nPruneTarget == 0 ) {
4047
+ if (chain_tip_height < 0 || nPruneTarget == 0 ) {
4012
4048
return ;
4013
4049
}
4014
- if ((uint64_t ):: ChainActive (). Tip ()-> nHeight <= nPruneAfterHeight) {
4050
+ if ((uint64_t )chain_tip_height <= nPruneAfterHeight) {
4015
4051
return ;
4016
4052
}
4017
4053
4018
- unsigned int nLastBlockWeCanPrune = :: ChainActive (). Tip ()-> nHeight - MIN_BLOCKS_TO_KEEP;
4054
+ unsigned int nLastBlockWeCanPrune = chain_tip_height - MIN_BLOCKS_TO_KEEP;
4019
4055
uint64_t nCurrentUsage = CalculateCurrentUsage ();
4020
4056
// We don't check to prune until after we've allocated new space for files
4021
4057
// So we should leave a buffer under our target to account for another allocation
@@ -4029,7 +4065,7 @@ static void FindFilesToPrune(ChainstateManager& chainman, std::set<int>& setFile
4029
4065
// To avoid excessive prune events negating the benefit of high dbcache
4030
4066
// values, we should not prune too rapidly.
4031
4067
// So when pruning in IBD, increase the buffer a bit to avoid a re-prune too soon.
4032
- if (:: ChainstateActive (). IsInitialBlockDownload () ) {
4068
+ if (is_ibd ) {
4033
4069
// Since this is only relevant during IBD, we use a fixed 10%
4034
4070
nBuffer += nPruneTarget / 10 ;
4035
4071
}
@@ -4047,7 +4083,7 @@ static void FindFilesToPrune(ChainstateManager& chainman, std::set<int>& setFile
4047
4083
if (vinfoBlockFile[fileNumber].nHeightLast > nLastBlockWeCanPrune)
4048
4084
continue ;
4049
4085
4050
- chainman. m_blockman . PruneOneBlockFile (fileNumber);
4086
+ PruneOneBlockFile (fileNumber);
4051
4087
// Queue up the files for removal
4052
4088
setFilesToPrune.insert (fileNumber);
4053
4089
nCurrentUsage -= nBytesToPrune;
0 commit comments