Skip to content

Commit fa81c30

Browse files
author
MarcoFalke
committed
refactor: Move pruning/reindex/importing globals to blockstorage
Can be reviewed with --color-moved=dimmed-zebra
1 parent 19a56d1 commit fa81c30

File tree

6 files changed

+68
-62
lines changed

6 files changed

+68
-62
lines changed

src/init.cpp

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -593,47 +593,6 @@ static void BlockNotifyGenesisWait(const CBlockIndex* pBlockIndex)
593593
}
594594
}
595595

596-
// If we're using -prune with -reindex, then delete block files that will be ignored by the
597-
// reindex. Since reindexing works by starting at block file 0 and looping until a blockfile
598-
// is missing, do the same here to delete any later block files after a gap. Also delete all
599-
// rev files since they'll be rewritten by the reindex anyway. This ensures that vinfoBlockFile
600-
// is in sync with what's actually on disk by the time we start downloading, so that pruning
601-
// works correctly.
602-
static void CleanupBlockRevFiles()
603-
{
604-
std::map<std::string, fs::path> mapBlockFiles;
605-
606-
// Glob all blk?????.dat and rev?????.dat files from the blocks directory.
607-
// Remove the rev files immediately and insert the blk file paths into an
608-
// ordered map keyed by block file index.
609-
LogPrintf("Removing unusable blk?????.dat and rev?????.dat files for -reindex with -prune\n");
610-
fs::path blocksdir = gArgs.GetBlocksDirPath();
611-
for (fs::directory_iterator it(blocksdir); it != fs::directory_iterator(); it++) {
612-
if (fs::is_regular_file(*it) &&
613-
it->path().filename().string().length() == 12 &&
614-
it->path().filename().string().substr(8,4) == ".dat")
615-
{
616-
if (it->path().filename().string().substr(0,3) == "blk")
617-
mapBlockFiles[it->path().filename().string().substr(3,5)] = it->path();
618-
else if (it->path().filename().string().substr(0,3) == "rev")
619-
remove(it->path());
620-
}
621-
}
622-
623-
// Remove all block files that aren't part of a contiguous set starting at
624-
// zero by walking the ordered map (keys are block file indices) by
625-
// keeping a separate counter. Once we hit a gap (or if 0 doesn't exist)
626-
// start removing block files.
627-
int nContigCounter = 0;
628-
for (const std::pair<const std::string, fs::path>& item : mapBlockFiles) {
629-
if (atoi(item.first) == nContigCounter) {
630-
nContigCounter++;
631-
continue;
632-
}
633-
remove(item.second);
634-
}
635-
}
636-
637596
#if HAVE_SYSTEM
638597
static void StartupNotify(const ArgsManager& args)
639598
{

src/node/blockstorage.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,58 @@
1515
#include <util/system.h>
1616
#include <validation.h>
1717

18+
std::atomic_bool fImporting(false);
19+
std::atomic_bool fReindex(false);
20+
bool fHavePruned = false;
21+
bool fPruneMode = false;
22+
uint64_t nPruneTarget = 0;
23+
24+
bool IsBlockPruned(const CBlockIndex* pblockindex)
25+
{
26+
return (fHavePruned && !(pblockindex->nStatus & BLOCK_HAVE_DATA) && pblockindex->nTx > 0);
27+
}
28+
29+
// If we're using -prune with -reindex, then delete block files that will be ignored by the
30+
// reindex. Since reindexing works by starting at block file 0 and looping until a blockfile
31+
// is missing, do the same here to delete any later block files after a gap. Also delete all
32+
// rev files since they'll be rewritten by the reindex anyway. This ensures that vinfoBlockFile
33+
// is in sync with what's actually on disk by the time we start downloading, so that pruning
34+
// works correctly.
35+
void CleanupBlockRevFiles()
36+
{
37+
std::map<std::string, fs::path> mapBlockFiles;
38+
39+
// Glob all blk?????.dat and rev?????.dat files from the blocks directory.
40+
// Remove the rev files immediately and insert the blk file paths into an
41+
// ordered map keyed by block file index.
42+
LogPrintf("Removing unusable blk?????.dat and rev?????.dat files for -reindex with -prune\n");
43+
fs::path blocksdir = gArgs.GetBlocksDirPath();
44+
for (fs::directory_iterator it(blocksdir); it != fs::directory_iterator(); it++) {
45+
if (fs::is_regular_file(*it) &&
46+
it->path().filename().string().length() == 12 &&
47+
it->path().filename().string().substr(8,4) == ".dat")
48+
{
49+
if (it->path().filename().string().substr(0,3) == "blk")
50+
mapBlockFiles[it->path().filename().string().substr(3,5)] = it->path();
51+
else if (it->path().filename().string().substr(0,3) == "rev")
52+
remove(it->path());
53+
}
54+
}
55+
56+
// Remove all block files that aren't part of a contiguous set starting at
57+
// zero by walking the ordered map (keys are block file indices) by
58+
// keeping a separate counter. Once we hit a gap (or if 0 doesn't exist)
59+
// start removing block files.
60+
int nContigCounter = 0;
61+
for (const std::pair<const std::string, fs::path>& item : mapBlockFiles) {
62+
if (atoi(item.first) == nContigCounter) {
63+
nContigCounter++;
64+
continue;
65+
}
66+
remove(item.second);
67+
}
68+
}
69+
1870
// From validation. TODO move here
1971
bool FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigned int nHeight, CChain& active_chain, uint64_t nTime, bool fKnown = false);
2072

src/node/blockstorage.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ struct Params;
2525

2626
static constexpr bool DEFAULT_STOPAFTERBLOCKIMPORT{false};
2727

28+
extern std::atomic_bool fImporting;
29+
extern std::atomic_bool fReindex;
30+
/** Pruning-related variables and constants */
31+
/** True if any block files have ever been pruned. */
32+
extern bool fHavePruned;
33+
/** True if we're running in -prune mode. */
34+
extern bool fPruneMode;
35+
/** Number of MiB of block files that we're trying to stay below. */
36+
extern uint64_t nPruneTarget;
37+
38+
//! Check whether the block associated with this index entry is pruned or not.
39+
bool IsBlockPruned(const CBlockIndex* pblockindex);
40+
41+
void CleanupBlockRevFiles();
42+
2843
/** Functions for disk access for blocks */
2944
bool ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos, const Consensus::Params& consensusParams);
3045
bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams);

src/validation.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,9 @@ Mutex g_best_block_mutex;
135135
std::condition_variable g_best_block_cv;
136136
uint256 g_best_block;
137137
bool g_parallel_script_checks{false};
138-
std::atomic_bool fImporting(false);
139-
std::atomic_bool fReindex(false);
140-
bool fHavePruned = false;
141-
bool fPruneMode = false;
142138
bool fRequireStandard = true;
143139
bool fCheckBlockIndex = false;
144140
bool fCheckpointsEnabled = DEFAULT_CHECKPOINTS_ENABLED;
145-
uint64_t nPruneTarget = 0;
146141
int64_t nMaxTipAge = DEFAULT_MAX_TIP_AGE;
147142

148143
uint256 hashAssumeValid;

src/validation.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,6 @@ typedef std::unordered_map<uint256, CBlockIndex*, BlockHasher> BlockMap;
112112
extern Mutex g_best_block_mutex;
113113
extern std::condition_variable g_best_block_cv;
114114
extern uint256 g_best_block;
115-
extern std::atomic_bool fImporting;
116-
extern std::atomic_bool fReindex;
117115
/** Whether there are dedicated script-checking threads running.
118116
* False indicates all script checking is done on the main threadMessageHandler thread.
119117
*/
@@ -135,13 +133,6 @@ extern arith_uint256 nMinimumChainWork;
135133
/** Best header we've seen so far (used for getheaders queries' starting points). */
136134
extern CBlockIndex *pindexBestHeader;
137135

138-
/** Pruning-related variables and constants */
139-
/** True if any block files have ever been pruned. */
140-
extern bool fHavePruned;
141-
/** True if we're running in -prune mode. */
142-
extern bool fPruneMode;
143-
/** Number of MiB of block files that we're trying to stay below. */
144-
extern uint64_t nPruneTarget;
145136
/** Documentation for argument 'checklevel'. */
146137
extern const std::vector<std::string> CHECKLEVEL_DOC;
147138

@@ -1012,12 +1003,6 @@ bool DumpMempool(const CTxMemPool& pool, FopenFn mockable_fopen_function = fsbri
10121003
/** Load the mempool from disk. */
10131004
bool LoadMempool(CTxMemPool& pool, CChainState& active_chainstate, FopenFn mockable_fopen_function = fsbridge::fopen);
10141005

1015-
//! Check whether the block associated with this index entry is pruned or not.
1016-
inline bool IsBlockPruned(const CBlockIndex* pblockindex)
1017-
{
1018-
return (fHavePruned && !(pblockindex->nStatus & BLOCK_HAVE_DATA) && pblockindex->nTx > 0);
1019-
}
1020-
10211006
/**
10221007
* Return the expected assumeutxo value for a given height, if one exists.
10231008
*

test/lint/lint-locale-dependence.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ KNOWN_VIOLATIONS=(
4343
"src/dbwrapper.cpp.*stoul"
4444
"src/dbwrapper.cpp:.*vsnprintf"
4545
"src/httprpc.cpp.*trim"
46-
"src/init.cpp:.*atoi"
46+
"src/node/blockstorage.cpp:.*atoi"
4747
"src/qt/rpcconsole.cpp:.*atoi"
4848
"src/rest.cpp:.*strtol"
4949
"src/test/dbwrapper_tests.cpp:.*snprintf"

0 commit comments

Comments
 (0)