Skip to content

Commit faf7b4f

Browse files
author
MarcoFalke
committed
Add BlockManager::IsPruneMode()
1 parent fae71fe commit faf7b4f

File tree

7 files changed

+23
-22
lines changed

7 files changed

+23
-22
lines changed

src/index/base.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,9 @@ IndexSummary BaseIndex::GetSummary() const
415415
return summary;
416416
}
417417

418-
void BaseIndex::SetBestBlockIndex(const CBlockIndex* block) {
419-
assert(!node::fPruneMode || AllowPrune());
418+
void BaseIndex::SetBestBlockIndex(const CBlockIndex* block)
419+
{
420+
assert(!m_chainstate->m_blockman.IsPruneMode() || AllowPrune());
420421

421422
if (AllowPrune() && block) {
422423
node::PruneLockInfo prune_lock;

src/init.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,7 +1479,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
14791479
options.mempool = Assert(node.mempool.get());
14801480
options.reindex = node::fReindex;
14811481
options.reindex_chainstate = fReindexChainState;
1482-
options.prune = node::fPruneMode;
1482+
options.prune = chainman.m_blockman.IsPruneMode();
14831483
options.check_blocks = args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS);
14841484
options.check_level = args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL);
14851485
options.check_interrupt = ShutdownRequested;
@@ -1589,7 +1589,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
15891589

15901590
// if pruning, perform the initial blockstore prune
15911591
// after any wallet rescanning has taken place.
1592-
if (fPruneMode) {
1592+
if (chainman.m_blockman.IsPruneMode()) {
15931593
if (!fReindex) {
15941594
LOCK(cs_main);
15951595
for (Chainstate* chainstate : chainman.GetAll()) {
@@ -1618,7 +1618,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
16181618
// On first startup, warn on low block storage space
16191619
if (!fReindex && !fReindexChainState && chain_active_height <= 1) {
16201620
uint64_t additional_bytes_needed{
1621-
fPruneMode ?
1621+
chainman.m_blockman.IsPruneMode() ?
16221622
chainman.m_blockman.GetPruneTarget() :
16231623
chainparams.AssumedBlockchainSize() * 1024 * 1024 * 1024};
16241624

src/net_processing.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353

5454
using node::ReadBlockFromDisk;
5555
using node::ReadRawBlockFromDisk;
56-
using node::fPruneMode;
5756

5857
/** How long to cache transactions in mapRelay for normal relay */
5958
static constexpr auto RELAY_TX_CACHE_TIME = 15min;
@@ -3809,8 +3808,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
38093808
// If pruning, don't inv blocks unless we have on disk and are likely to still have
38103809
// for some reasonable time window (1 hour) that block relay might require.
38113810
const int nPrunedBlocksLikelyToHave = MIN_BLOCKS_TO_KEEP - 3600 / m_chainparams.GetConsensus().nPowTargetSpacing;
3812-
if (fPruneMode && (!(pindex->nStatus & BLOCK_HAVE_DATA) || pindex->nHeight <= m_chainman.ActiveChain().Tip()->nHeight - nPrunedBlocksLikelyToHave))
3813-
{
3811+
if (m_chainman.m_blockman.IsPruneMode() && (!(pindex->nStatus & BLOCK_HAVE_DATA) || pindex->nHeight <= m_chainman.ActiveChain().Tip()->nHeight - nPrunedBlocksLikelyToHave)) {
38143812
LogPrint(BCLog::NET, " getblocks stopping, pruned or too old block at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
38153813
break;
38163814
}

src/node/blockstorage.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ static constexpr size_t BLOCK_SERIALIZATION_HEADER_SIZE = CMessageHeader::MESSAG
4848

4949
extern std::atomic_bool fImporting;
5050
extern std::atomic_bool fReindex;
51-
/** Pruning-related variables and constants */
52-
/** True if we're running in -prune mode. */
5351
extern bool fPruneMode;
5452
extern uint64_t nPruneTarget;
5553

@@ -175,6 +173,9 @@ class BlockManager
175173
/** Store block on disk. If dbp is not nullptr, then it provides the known position of the block within a block file on disk. */
176174
FlatFilePos SaveBlockToDisk(const CBlock& block, int nHeight, CChain& active_chain, const CChainParams& chainparams, const FlatFilePos* dbp);
177175

176+
/** Whether running in -prune mode. */
177+
[[nodiscard]] bool IsPruneMode() const { return fPruneMode; }
178+
178179
/** Attempt to stay below this number of bytes of block files. */
179180
[[nodiscard]] uint64_t GetPruneTarget() const { return nPruneTarget; }
180181

src/rpc/blockchain.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ static RPCHelpMan getblockfrompeer()
464464

465465
// Fetching blocks before the node has syncing past their height can prevent block files from
466466
// being pruned, so we avoid it if the node is in prune mode.
467-
if (node::fPruneMode && index->nHeight > WITH_LOCK(chainman.GetMutex(), return chainman.ActiveTip()->nHeight)) {
467+
if (chainman.m_blockman.IsPruneMode() && index->nHeight > WITH_LOCK(chainman.GetMutex(), return chainman.ActiveTip()->nHeight)) {
468468
throw JSONRPCError(RPC_MISC_ERROR, "In prune mode, only blocks that the node has already synced previously can be fetched from a peer");
469469
}
470470

@@ -778,10 +778,11 @@ static RPCHelpMan pruneblockchain()
778778
},
779779
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
780780
{
781-
if (!node::fPruneMode)
781+
ChainstateManager& chainman = EnsureAnyChainman(request.context);
782+
if (!chainman.m_blockman.IsPruneMode()) {
782783
throw JSONRPCError(RPC_MISC_ERROR, "Cannot prune blocks because node is not in prune mode.");
784+
}
783785

784-
ChainstateManager& chainman = EnsureAnyChainman(request.context);
785786
LOCK(cs_main);
786787
Chainstate& active_chainstate = chainman.ActiveChainstate();
787788
CChain& active_chain = active_chainstate.m_chain;
@@ -1265,8 +1266,8 @@ RPCHelpMan getblockchaininfo()
12651266
obj.pushKV("initialblockdownload", active_chainstate.IsInitialBlockDownload());
12661267
obj.pushKV("chainwork", tip.nChainWork.GetHex());
12671268
obj.pushKV("size_on_disk", chainman.m_blockman.CalculateCurrentUsage());
1268-
obj.pushKV("pruned", node::fPruneMode);
1269-
if (node::fPruneMode) {
1269+
obj.pushKV("pruned", chainman.m_blockman.IsPruneMode());
1270+
if (chainman.m_blockman.IsPruneMode()) {
12701271
obj.pushKV("pruneheight", chainman.m_blockman.GetFirstStoredBlock(tip)->nHeight);
12711272

12721273
// if 0, execution bypasses the whole if block.

src/test/util/setup_common.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,23 +208,24 @@ ChainTestingSetup::~ChainTestingSetup()
208208

209209
void TestingSetup::LoadVerifyActivateChainstate()
210210
{
211+
auto& chainman{*Assert(m_node.chainman)};
211212
node::ChainstateLoadOptions options;
212213
options.mempool = Assert(m_node.mempool.get());
213214
options.block_tree_db_in_memory = m_block_tree_db_in_memory;
214215
options.coins_db_in_memory = m_coins_db_in_memory;
215216
options.reindex = node::fReindex;
216217
options.reindex_chainstate = m_args.GetBoolArg("-reindex-chainstate", false);
217-
options.prune = node::fPruneMode;
218+
options.prune = chainman.m_blockman.IsPruneMode();
218219
options.check_blocks = m_args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS);
219220
options.check_level = m_args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL);
220-
auto [status, error] = LoadChainstate(*Assert(m_node.chainman), m_cache_sizes, options);
221+
auto [status, error] = LoadChainstate(chainman, m_cache_sizes, options);
221222
assert(status == node::ChainstateLoadStatus::SUCCESS);
222223

223-
std::tie(status, error) = VerifyLoadedChainstate(*Assert(m_node.chainman), options);
224+
std::tie(status, error) = VerifyLoadedChainstate(chainman, options);
224225
assert(status == node::ChainstateLoadStatus::SUCCESS);
225226

226227
BlockValidationState state;
227-
if (!m_node.chainman->ActiveChainstate().ActivateBestChain(state)) {
228+
if (!chainman.ActiveChainstate().ActivateBestChain(state)) {
228229
throw std::runtime_error(strprintf("ActivateBestChain failed. (%s)", state.ToString()));
229230
}
230231
}

src/validation.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ using node::BlockManager;
7676
using node::BlockMap;
7777
using node::CBlockIndexHeightOnlyComparator;
7878
using node::CBlockIndexWorkComparator;
79-
using node::fPruneMode;
8079
using node::fReindex;
8180
using node::ReadBlockFromDisk;
8281
using node::SnapshotMetadata;
@@ -2411,7 +2410,7 @@ bool Chainstate::FlushStateToDisk(
24112410

24122411
CoinsCacheSizeState cache_state = GetCoinsCacheSizeState();
24132412
LOCK(m_blockman.cs_LastBlockFile);
2414-
if (fPruneMode && (m_blockman.m_check_for_pruning || nManualPruneHeight > 0) && !fReindex) {
2413+
if (m_blockman.IsPruneMode() && (m_blockman.m_check_for_pruning || nManualPruneHeight > 0) && !fReindex) {
24152414
// make sure we don't prune above any of the prune locks bestblocks
24162415
// pruning is height-based
24172416
int last_prune{m_chain.Height()}; // last height we can prune
@@ -4097,7 +4096,7 @@ bool CVerifyDB::VerifyDB(
40974096
if (pindex->nHeight <= chainstate.m_chain.Height() - nCheckDepth) {
40984097
break;
40994098
}
4100-
if ((fPruneMode || is_snapshot_cs) && !(pindex->nStatus & BLOCK_HAVE_DATA)) {
4099+
if ((chainstate.m_blockman.IsPruneMode() || is_snapshot_cs) && !(pindex->nStatus & BLOCK_HAVE_DATA)) {
41014100
// If pruning or running under an assumeutxo snapshot, only go
41024101
// back as far as we have data.
41034102
LogPrintf("VerifyDB(): block verification stopping at height %d (pruning, no data)\n", pindex->nHeight);

0 commit comments

Comments
 (0)