@@ -198,9 +198,6 @@ CBlockIndex* FindForkInGlobalIndex(const CChain& chain, const CBlockLocator& loc
198
198
199
199
std::unique_ptr<CBlockTreeDB> pblocktree;
200
200
201
- // See definition for documentation
202
- static void FindFilesToPruneManual (ChainstateManager& chainman, std::set<int >& setFilesToPrune, int nManualPruneHeight);
203
- static void FindFilesToPrune (ChainstateManager& chainman, std::set<int >& setFilesToPrune, uint64_t nPruneAfterHeight);
204
201
bool CheckInputScripts (const CTransaction& tx, TxValidationState &state, const CCoinsViewCache &inputs, unsigned int flags, bool cacheSigStore, bool cacheFullScriptStore, PrecomputedTransactionData& txdata, std::vector<CScriptCheck> *pvChecks = nullptr );
205
202
static FILE* OpenUndoFile (const FlatFilePos &pos, bool fReadOnly = false );
206
203
static FlatFileSeq BlockFileSeq ();
@@ -2299,11 +2296,11 @@ bool CChainState::FlushStateToDisk(
2299
2296
if (nManualPruneHeight > 0 ) {
2300
2297
LOG_TIME_MILLIS_WITH_CATEGORY (" find files to prune (manual)" , BCLog::BENCH);
2301
2298
2302
- FindFilesToPruneManual (g_chainman, setFilesToPrune, nManualPruneHeight);
2299
+ m_blockman. FindFilesToPruneManual (setFilesToPrune, nManualPruneHeight, m_chain. Height () );
2303
2300
} else {
2304
2301
LOG_TIME_MILLIS_WITH_CATEGORY (" find files to prune" , BCLog::BENCH);
2305
2302
2306
- FindFilesToPrune (g_chainman, setFilesToPrune, chainparams.PruneAfterHeight ());
2303
+ m_blockman. FindFilesToPrune (setFilesToPrune, chainparams.PruneAfterHeight (), m_chain. Height (), IsInitialBlockDownload ());
2307
2304
fCheckForPruning = false ;
2308
2305
}
2309
2306
if (!setFilesToPrune.empty ()) {
@@ -3909,12 +3906,12 @@ uint64_t CalculateCurrentUsage()
3909
3906
return retval;
3910
3907
}
3911
3908
3912
- void ChainstateManager ::PruneOneBlockFile (const int fileNumber)
3909
+ void BlockManager ::PruneOneBlockFile (const int fileNumber)
3913
3910
{
3914
3911
AssertLockHeld (cs_main);
3915
3912
LOCK (cs_LastBlockFile);
3916
3913
3917
- for (const auto & entry : m_blockman. m_block_index ) {
3914
+ for (const auto & entry : m_block_index) {
3918
3915
CBlockIndex* pindex = entry.second ;
3919
3916
if (pindex->nFile == fileNumber) {
3920
3917
pindex->nStatus &= ~BLOCK_HAVE_DATA;
@@ -3928,12 +3925,12 @@ void ChainstateManager::PruneOneBlockFile(const int fileNumber)
3928
3925
// to be downloaded again in order to consider its chain, at which
3929
3926
// point it would be considered as a candidate for
3930
3927
// m_blocks_unlinked or setBlockIndexCandidates.
3931
- auto range = m_blockman. m_blocks_unlinked .equal_range (pindex->pprev );
3928
+ auto range = m_blocks_unlinked.equal_range (pindex->pprev );
3932
3929
while (range.first != range.second ) {
3933
3930
std::multimap<CBlockIndex *, CBlockIndex *>::iterator _it = range.first ;
3934
3931
range.first ++;
3935
3932
if (_it->second == pindex) {
3936
- m_blockman. m_blocks_unlinked .erase (_it);
3933
+ m_blocks_unlinked.erase (_it);
3937
3934
}
3938
3935
}
3939
3936
}
@@ -3954,22 +3951,23 @@ void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune)
3954
3951
}
3955
3952
}
3956
3953
3957
- /* Calculate the block/rev files to delete based on height specified by user with RPC command pruneblockchain */
3958
- static void FindFilesToPruneManual (ChainstateManager& chainman, std::set<int >& setFilesToPrune, int nManualPruneHeight)
3954
+ void BlockManager::FindFilesToPruneManual (std::set<int >& setFilesToPrune, int nManualPruneHeight, int chain_tip_height)
3959
3955
{
3960
3956
assert (fPruneMode && nManualPruneHeight > 0 );
3961
3957
3962
3958
LOCK2 (cs_main, cs_LastBlockFile);
3963
- if (:: ChainActive (). Tip () == nullptr )
3959
+ if (chain_tip_height < 0 ) {
3964
3960
return ;
3961
+ }
3965
3962
3966
3963
// last block to prune is the lesser of (user-specified height, MIN_BLOCKS_TO_KEEP from the tip)
3967
- unsigned int nLastBlockWeCanPrune = std::min ((unsigned )nManualPruneHeight, :: ChainActive (). Tip ()-> nHeight - MIN_BLOCKS_TO_KEEP);
3968
- int count= 0 ;
3964
+ unsigned int nLastBlockWeCanPrune = std::min ((unsigned )nManualPruneHeight, chain_tip_height - MIN_BLOCKS_TO_KEEP);
3965
+ int count = 0 ;
3969
3966
for (int fileNumber = 0 ; fileNumber < nLastBlockFile; fileNumber++) {
3970
- if (vinfoBlockFile[fileNumber].nSize == 0 || vinfoBlockFile[fileNumber].nHeightLast > nLastBlockWeCanPrune)
3967
+ if (vinfoBlockFile[fileNumber].nSize == 0 || vinfoBlockFile[fileNumber].nHeightLast > nLastBlockWeCanPrune) {
3971
3968
continue ;
3972
- chainman.PruneOneBlockFile (fileNumber);
3969
+ }
3970
+ PruneOneBlockFile (fileNumber);
3973
3971
setFilesToPrune.insert (fileNumber);
3974
3972
count++;
3975
3973
}
@@ -3987,64 +3985,52 @@ void PruneBlockFilesManual(int nManualPruneHeight)
3987
3985
}
3988
3986
}
3989
3987
3990
- /* *
3991
- * Prune block and undo files (blk???.dat and undo???.dat) so that the disk space used is less than a user-defined target.
3992
- * The user sets the target (in MB) on the command line or in config file. This will be run on startup and whenever new
3993
- * space is allocated in a block or undo file, staying below the target. Changing back to unpruned requires a reindex
3994
- * (which in this case means the blockchain must be re-downloaded.)
3995
- *
3996
- * Pruning functions are called from FlushStateToDisk when the global fCheckForPruning flag has been set.
3997
- * Block and undo files are deleted in lock-step (when blk00003.dat is deleted, so is rev00003.dat.)
3998
- * Pruning cannot take place until the longest chain is at least a certain length (100000 on mainnet, 1000 on testnet, 1000 on regtest).
3999
- * Pruning will never delete a block within a defined distance (currently 288) from the active chain's tip.
4000
- * The block index is updated by unsetting HAVE_DATA and HAVE_UNDO for any blocks that were stored in the deleted files.
4001
- * A db flag records the fact that at least some block files have been pruned.
4002
- *
4003
- * @param[out] setFilesToPrune The set of file indices that can be unlinked will be returned
4004
- */
4005
- static void FindFilesToPrune (ChainstateManager& chainman, std::set<int >& setFilesToPrune, uint64_t nPruneAfterHeight)
3988
+ void BlockManager::FindFilesToPrune (std::set<int >& setFilesToPrune, uint64_t nPruneAfterHeight, int chain_tip_height, bool is_ibd)
4006
3989
{
4007
3990
LOCK2 (cs_main, cs_LastBlockFile);
4008
- if (:: ChainActive (). Tip () == nullptr || nPruneTarget == 0 ) {
3991
+ if (chain_tip_height < 0 || nPruneTarget == 0 ) {
4009
3992
return ;
4010
3993
}
4011
- if ((uint64_t ):: ChainActive (). Tip ()-> nHeight <= nPruneAfterHeight) {
3994
+ if ((uint64_t )chain_tip_height <= nPruneAfterHeight) {
4012
3995
return ;
4013
3996
}
4014
3997
4015
- unsigned int nLastBlockWeCanPrune = :: ChainActive (). Tip ()-> nHeight - MIN_BLOCKS_TO_KEEP;
3998
+ unsigned int nLastBlockWeCanPrune = chain_tip_height - MIN_BLOCKS_TO_KEEP;
4016
3999
uint64_t nCurrentUsage = CalculateCurrentUsage ();
4017
4000
// We don't check to prune until after we've allocated new space for files
4018
4001
// So we should leave a buffer under our target to account for another allocation
4019
4002
// before the next pruning.
4020
4003
uint64_t nBuffer = BLOCKFILE_CHUNK_SIZE + UNDOFILE_CHUNK_SIZE;
4021
4004
uint64_t nBytesToPrune;
4022
- int count= 0 ;
4005
+ int count = 0 ;
4023
4006
4024
4007
if (nCurrentUsage + nBuffer >= nPruneTarget) {
4025
4008
// On a prune event, the chainstate DB is flushed.
4026
4009
// To avoid excessive prune events negating the benefit of high dbcache
4027
4010
// values, we should not prune too rapidly.
4028
4011
// So when pruning in IBD, increase the buffer a bit to avoid a re-prune too soon.
4029
- if (:: ChainstateActive (). IsInitialBlockDownload () ) {
4012
+ if (is_ibd ) {
4030
4013
// Since this is only relevant during IBD, we use a fixed 10%
4031
4014
nBuffer += nPruneTarget / 10 ;
4032
4015
}
4033
4016
4034
4017
for (int fileNumber = 0 ; fileNumber < nLastBlockFile; fileNumber++) {
4035
4018
nBytesToPrune = vinfoBlockFile[fileNumber].nSize + vinfoBlockFile[fileNumber].nUndoSize ;
4036
4019
4037
- if (vinfoBlockFile[fileNumber].nSize == 0 )
4020
+ if (vinfoBlockFile[fileNumber].nSize == 0 ) {
4038
4021
continue ;
4022
+ }
4039
4023
4040
- if (nCurrentUsage + nBuffer < nPruneTarget) // are we below our target?
4024
+ if (nCurrentUsage + nBuffer < nPruneTarget) { // are we below our target?
4041
4025
break ;
4026
+ }
4042
4027
4043
4028
// don't prune files that could have a block within MIN_BLOCKS_TO_KEEP of the main chain's tip but keep scanning
4044
- if (vinfoBlockFile[fileNumber].nHeightLast > nLastBlockWeCanPrune)
4029
+ if (vinfoBlockFile[fileNumber].nHeightLast > nLastBlockWeCanPrune) {
4045
4030
continue ;
4031
+ }
4046
4032
4047
- chainman. PruneOneBlockFile (fileNumber);
4033
+ PruneOneBlockFile (fileNumber);
4048
4034
// Queue up the files for removal
4049
4035
setFilesToPrune.insert (fileNumber);
4050
4036
nCurrentUsage -= nBytesToPrune;
@@ -4602,10 +4588,10 @@ void CChainState::UnloadBlockIndex() {
4602
4588
// May NOT be used after any connections are up as much
4603
4589
// of the peer-processing logic assumes a consistent
4604
4590
// block index state
4605
- void UnloadBlockIndex (CTxMemPool* mempool)
4591
+ void UnloadBlockIndex (CTxMemPool* mempool, ChainstateManager& chainman )
4606
4592
{
4607
4593
LOCK (cs_main);
4608
- g_chainman .Unload ();
4594
+ chainman .Unload ();
4609
4595
pindexBestInvalid = nullptr ;
4610
4596
pindexBestHeader = nullptr ;
4611
4597
if (mempool) mempool->clear ();
@@ -5220,20 +5206,6 @@ double GuessVerificationProgress(const ChainTxData& data, const CBlockIndex *pin
5220
5206
return std::min<double >(pindex->nChainTx / fTxTotal , 1.0 );
5221
5207
}
5222
5208
5223
- class CMainCleanup
5224
- {
5225
- public:
5226
- CMainCleanup () {}
5227
- ~CMainCleanup () {
5228
- // block headers
5229
- BlockMap::iterator it1 = g_chainman.BlockIndex ().begin ();
5230
- for (; it1 != g_chainman.BlockIndex ().end (); it1++)
5231
- delete (*it1).second ;
5232
- g_chainman.BlockIndex ().clear ();
5233
- }
5234
- };
5235
- static CMainCleanup instance_of_cmaincleanup;
5236
-
5237
5209
Optional<uint256> ChainstateManager::SnapshotBlockhash () const {
5238
5210
if (m_active_chainstate != nullptr ) {
5239
5211
// If a snapshot chainstate exists, it will always be our active.
0 commit comments