Skip to content

Commit 3308ecd

Browse files
committed
move-mostly: Make fHavePruned a BlockMan member
[META] In the next commit, we move the clearing of fHavePruned to BlockManager::Unload()
1 parent c965241 commit 3308ecd

File tree

10 files changed

+35
-38
lines changed

10 files changed

+35
-38
lines changed

src/bench/rpc_blockchain.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static void BlockToJsonVerbose(benchmark::Bench& bench)
4040
{
4141
TestBlockAndIndex data;
4242
bench.run([&] {
43-
auto univalue = blockToJSON(data.block, &data.blockindex, &data.blockindex, TxVerbosity::SHOW_DETAILS_AND_PREVOUT);
43+
auto univalue = blockToJSON(data.testing_setup->m_node.chainman->m_blockman, data.block, &data.blockindex, &data.blockindex, TxVerbosity::SHOW_DETAILS_AND_PREVOUT);
4444
ankerl::nanobench::doNotOptimizeAway(univalue);
4545
});
4646
}
@@ -50,7 +50,7 @@ BENCHMARK(BlockToJsonVerbose);
5050
static void BlockToJsonVerboseWrite(benchmark::Bench& bench)
5151
{
5252
TestBlockAndIndex data;
53-
auto univalue = blockToJSON(data.block, &data.blockindex, &data.blockindex, TxVerbosity::SHOW_DETAILS_AND_PREVOUT);
53+
auto univalue = blockToJSON(data.testing_setup->m_node.chainman->m_blockman, data.block, &data.blockindex, &data.blockindex, TxVerbosity::SHOW_DETAILS_AND_PREVOUT);
5454
bench.run([&] {
5555
auto str = univalue.write();
5656
ankerl::nanobench::doNotOptimizeAway(str);

src/init.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ using node::LoadChainstate;
109109
using node::NodeContext;
110110
using node::ThreadImport;
111111
using node::VerifyLoadedChainstate;
112-
using node::fHavePruned;
113112
using node::fPruneMode;
114113
using node::fReindex;
115114
using node::nPruneTarget;
@@ -1480,7 +1479,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
14801479
try {
14811480
uiInterface.InitMessage(_("Verifying blocks…").translated);
14821481
auto check_blocks = args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS);
1483-
if (fHavePruned && check_blocks > MIN_BLOCKS_TO_KEEP) {
1482+
if (chainman.m_blockman.fHavePruned && check_blocks > MIN_BLOCKS_TO_KEEP) {
14841483
LogPrintf("Prune: pruned datadir may not have more than %d blocks; only checking available blocks\n",
14851484
MIN_BLOCKS_TO_KEEP);
14861485
}

src/node/blockstorage.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
namespace node {
2525
std::atomic_bool fImporting(false);
2626
std::atomic_bool fReindex(false);
27-
bool fHavePruned = false;
2827
bool fPruneMode = false;
2928
uint64_t nPruneTarget = 0;
3029

@@ -390,7 +389,7 @@ const CBlockIndex* BlockManager::GetLastCheckpoint(const CCheckpointData& data)
390389
return nullptr;
391390
}
392391

393-
bool IsBlockPruned(const CBlockIndex* pblockindex)
392+
bool BlockManager::IsBlockPruned(const CBlockIndex* pblockindex)
394393
{
395394
AssertLockHeld(::cs_main);
396395
return (fHavePruned && !(pblockindex->nStatus & BLOCK_HAVE_DATA) && pblockindex->nTx > 0);

src/node/blockstorage.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ static const unsigned int MAX_BLOCKFILE_SIZE = 0x8000000; // 128 MiB
4545
extern std::atomic_bool fImporting;
4646
extern std::atomic_bool fReindex;
4747
/** Pruning-related variables and constants */
48-
/** True if any block files have ever been pruned. */
49-
extern bool fHavePruned;
5048
/** True if we're running in -prune mode. */
5149
extern bool fPruneMode;
5250
/** Number of MiB of block files that we're trying to stay below. */
@@ -171,15 +169,18 @@ class BlockManager
171169
//! Returns last CBlockIndex* that is a checkpoint
172170
const CBlockIndex* GetLastCheckpoint(const CCheckpointData& data) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
173171

172+
/** True if any block files have ever been pruned. */
173+
bool fHavePruned = false;
174+
175+
//! Check whether the block associated with this index entry is pruned or not.
176+
bool IsBlockPruned(const CBlockIndex* pblockindex) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
177+
174178
~BlockManager()
175179
{
176180
Unload();
177181
}
178182
};
179183

180-
//! Check whether the block associated with this index entry is pruned or not.
181-
bool IsBlockPruned(const CBlockIndex* pblockindex) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
182-
183184
void CleanupBlockRevFiles();
184185

185186
/** Open a block file (blk?????.dat) */

src/node/chainstate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
6565

6666
// Check for changed -prune state. What we are concerned about is a user who has pruned blocks
6767
// in the past, but is now trying to run unpruned.
68-
if (fHavePruned && !fPruneMode) {
68+
if (chainman.m_blockman.fHavePruned && !fPruneMode) {
6969
return ChainstateLoadingError::ERROR_PRUNED_NEEDS_REINDEX;
7070
}
7171

src/node/interfaces.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ class ChainImpl : public Chain
645645
bool havePruned() override
646646
{
647647
LOCK(cs_main);
648-
return node::fHavePruned;
648+
return m_node.chainman->m_blockman.fHavePruned;
649649
}
650650
bool isReadyToBroadcast() override { return !node::fImporting && !node::fReindex && !isInitialBlockDownload(); }
651651
bool isInitialBlockDownload() override {

src/rest.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
#include <univalue.h>
3838

3939
using node::GetTransaction;
40-
using node::IsBlockPruned;
4140
using node::NodeContext;
4241
using node::ReadBlockFromDisk;
4342

@@ -295,18 +294,18 @@ static bool rest_block(const std::any& context,
295294
CBlock block;
296295
const CBlockIndex* pblockindex = nullptr;
297296
const CBlockIndex* tip = nullptr;
297+
ChainstateManager* maybe_chainman = GetChainman(context, req);
298+
if (!maybe_chainman) return false;
299+
ChainstateManager& chainman = *maybe_chainman;
298300
{
299-
ChainstateManager* maybe_chainman = GetChainman(context, req);
300-
if (!maybe_chainman) return false;
301-
ChainstateManager& chainman = *maybe_chainman;
302301
LOCK(cs_main);
303302
tip = chainman.ActiveChain().Tip();
304303
pblockindex = chainman.m_blockman.LookupBlockIndex(hash);
305304
if (!pblockindex) {
306305
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
307306
}
308307

309-
if (IsBlockPruned(pblockindex))
308+
if (chainman.m_blockman.IsBlockPruned(pblockindex))
310309
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not available (pruned data)");
311310

312311
if (!ReadBlockFromDisk(block, pblockindex, Params().GetConsensus()))
@@ -333,7 +332,7 @@ static bool rest_block(const std::any& context,
333332
}
334333

335334
case RESTResponseFormat::JSON: {
336-
UniValue objBlock = blockToJSON(block, tip, pblockindex, tx_verbosity);
335+
UniValue objBlock = blockToJSON(chainman.m_blockman, block, tip, pblockindex, tx_verbosity);
337336
std::string strJSON = objBlock.write() + "\n";
338337
req->WriteHeader("Content-Type", "application/json");
339338
req->WriteReply(HTTP_OK, strJSON);

src/rpc/blockchain.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ using node::BlockManager;
5454
using node::CCoinsStats;
5555
using node::CoinStatsHashType;
5656
using node::GetUTXOStats;
57-
using node::IsBlockPruned;
5857
using node::NodeContext;
5958
using node::ReadBlockFromDisk;
6059
using node::SnapshotMetadata;
@@ -161,7 +160,7 @@ UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex
161160
return result;
162161
}
163162

164-
UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, TxVerbosity verbosity)
163+
UniValue blockToJSON(BlockManager& blockman, const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, TxVerbosity verbosity)
165164
{
166165
UniValue result = blockheaderToJSON(tip, blockindex);
167166

@@ -180,7 +179,7 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIn
180179
case TxVerbosity::SHOW_DETAILS:
181180
case TxVerbosity::SHOW_DETAILS_AND_PREVOUT:
182181
CBlockUndo blockUndo;
183-
const bool have_undo{WITH_LOCK(::cs_main, return !IsBlockPruned(blockindex) && UndoReadFromDisk(blockUndo, blockindex))};
182+
const bool have_undo{WITH_LOCK(::cs_main, return !blockman.IsBlockPruned(blockindex) && UndoReadFromDisk(blockUndo, blockindex))};
184183

185184
for (size_t i = 0; i < block.vtx.size(); ++i) {
186185
const CTransactionRef& tx = block.vtx.at(i);
@@ -565,11 +564,11 @@ static RPCHelpMan getblockheader()
565564
};
566565
}
567566

568-
static CBlock GetBlockChecked(const CBlockIndex* pblockindex) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
567+
static CBlock GetBlockChecked(BlockManager& blockman, const CBlockIndex* pblockindex) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
569568
{
570569
AssertLockHeld(::cs_main);
571570
CBlock block;
572-
if (IsBlockPruned(pblockindex)) {
571+
if (blockman.IsBlockPruned(pblockindex)) {
573572
throw JSONRPCError(RPC_MISC_ERROR, "Block not available (pruned data)");
574573
}
575574

@@ -583,11 +582,11 @@ static CBlock GetBlockChecked(const CBlockIndex* pblockindex) EXCLUSIVE_LOCKS_RE
583582
return block;
584583
}
585584

586-
static CBlockUndo GetUndoChecked(const CBlockIndex* pblockindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
585+
static CBlockUndo GetUndoChecked(BlockManager& blockman, const CBlockIndex* pblockindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
587586
{
588587
AssertLockHeld(::cs_main);
589588
CBlockUndo blockUndo;
590-
if (IsBlockPruned(pblockindex)) {
589+
if (blockman.IsBlockPruned(pblockindex)) {
591590
throw JSONRPCError(RPC_MISC_ERROR, "Undo data not available (pruned data)");
592591
}
593592

@@ -701,8 +700,8 @@ static RPCHelpMan getblock()
701700
CBlock block;
702701
const CBlockIndex* pblockindex;
703702
const CBlockIndex* tip;
703+
ChainstateManager& chainman = EnsureAnyChainman(request.context);
704704
{
705-
ChainstateManager& chainman = EnsureAnyChainman(request.context);
706705
LOCK(cs_main);
707706
pblockindex = chainman.m_blockman.LookupBlockIndex(hash);
708707
tip = chainman.ActiveChain().Tip();
@@ -711,7 +710,7 @@ static RPCHelpMan getblock()
711710
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
712711
}
713712

714-
block = GetBlockChecked(pblockindex);
713+
block = GetBlockChecked(chainman.m_blockman, pblockindex);
715714
}
716715

717716
if (verbosity <= 0)
@@ -731,7 +730,7 @@ static RPCHelpMan getblock()
731730
tx_verbosity = TxVerbosity::SHOW_DETAILS_AND_PREVOUT;
732731
}
733732

734-
return blockToJSON(block, tip, pblockindex, tx_verbosity);
733+
return blockToJSON(chainman.m_blockman, block, tip, pblockindex, tx_verbosity);
735734
},
736735
};
737736
}
@@ -1777,8 +1776,8 @@ static RPCHelpMan getblockstats()
17771776
}
17781777
}
17791778

1780-
const CBlock block = GetBlockChecked(pindex);
1781-
const CBlockUndo blockUndo = GetUndoChecked(pindex);
1779+
const CBlock block = GetBlockChecked(chainman.m_blockman, pindex);
1780+
const CBlockUndo blockUndo = GetUndoChecked(chainman.m_blockman, pindex);
17821781

17831782
const bool do_all = stats.size() == 0; // Calculate everything if nothing selected (default)
17841783
const bool do_mediantxsize = do_all || stats.count("mediantxsize") != 0;

src/rpc/blockchain.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <fs.h>
1111
#include <streams.h>
1212
#include <sync.h>
13+
#include <validation.h>
1314

1415
#include <any>
1516
#include <stdint.h>
@@ -39,7 +40,7 @@ double GetDifficulty(const CBlockIndex* blockindex);
3940
void RPCNotifyBlockChange(const CBlockIndex*);
4041

4142
/** Block description to JSON */
42-
UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, TxVerbosity verbosity) LOCKS_EXCLUDED(cs_main);
43+
UniValue blockToJSON(node::BlockManager& blockman, const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, TxVerbosity verbosity) LOCKS_EXCLUDED(cs_main);
4344

4445
/** Block header to JSON */
4546
UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex) LOCKS_EXCLUDED(cs_main);

src/validation.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ using node::CBlockIndexHeightOnlyComparator;
6969
using node::CBlockIndexWorkComparator;
7070
using node::CCoinsStats;
7171
using node::CoinStatsHashType;
72-
using node::fHavePruned;
7372
using node::fImporting;
7473
using node::fPruneMode;
7574
using node::fReindex;
@@ -2358,9 +2357,9 @@ bool CChainState::FlushStateToDisk(
23582357
}
23592358
if (!setFilesToPrune.empty()) {
23602359
fFlushForPrune = true;
2361-
if (!fHavePruned) {
2360+
if (!m_blockman.fHavePruned) {
23622361
m_blockman.m_block_tree_db->WriteFlag("prunedblockfiles", true);
2363-
fHavePruned = true;
2362+
m_blockman.fHavePruned = true;
23642363
}
23652364
}
23662365
}
@@ -4127,7 +4126,7 @@ void UnloadBlockIndex(CTxMemPool* mempool, ChainstateManager& chainman)
41274126
for (int b = 0; b < VERSIONBITS_NUM_BITS; b++) {
41284127
warningcache[b].clear();
41294128
}
4130-
fHavePruned = false;
4129+
chainman.m_blockman.fHavePruned = false;
41314130
}
41324131

41334132
bool ChainstateManager::LoadBlockIndex()
@@ -4449,7 +4448,7 @@ void CChainState::CheckBlockIndex()
44494448
// HAVE_DATA is only equivalent to nTx > 0 (or VALID_TRANSACTIONS) if no pruning has occurred.
44504449
// Unless these indexes are assumed valid and pending block download on a
44514450
// background chainstate.
4452-
if (!fHavePruned && !pindex->IsAssumedValid()) {
4451+
if (!m_blockman.fHavePruned && !pindex->IsAssumedValid()) {
44534452
// If we've never pruned, then HAVE_DATA should be equivalent to nTx > 0
44544453
assert(!(pindex->nStatus & BLOCK_HAVE_DATA) == (pindex->nTx == 0));
44554454
assert(pindexFirstMissing == pindexFirstNeverProcessed);
@@ -4523,7 +4522,7 @@ void CChainState::CheckBlockIndex()
45234522
if (pindexFirstMissing == nullptr) assert(!foundInUnlinked); // We aren't missing data for any parent -- cannot be in m_blocks_unlinked.
45244523
if (pindex->pprev && (pindex->nStatus & BLOCK_HAVE_DATA) && pindexFirstNeverProcessed == nullptr && pindexFirstMissing != nullptr) {
45254524
// We HAVE_DATA for this block, have received data for all parents at some point, but we're currently missing data for some parent.
4526-
assert(fHavePruned); // We must have pruned.
4525+
assert(m_blockman.fHavePruned); // We must have pruned.
45274526
// This block may have entered m_blocks_unlinked if:
45284527
// - it has a descendant that at some point had more work than the
45294528
// tip, and

0 commit comments

Comments
 (0)