Skip to content

Commit facd3df

Browse files
author
MarcoFalke
committed
Make blockstorage globals private members of BlockManager
1 parent faa8c2d commit facd3df

File tree

3 files changed

+32
-43
lines changed

3 files changed

+32
-43
lines changed

src/node/blockstorage.cpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,6 @@ bool fHavePruned = false;
2727
bool fPruneMode = false;
2828
uint64_t nPruneTarget = 0;
2929

30-
// TODO make namespace {
31-
RecursiveMutex cs_LastBlockFile;
32-
std::vector<CBlockFileInfo> vinfoBlockFile;
33-
int nLastBlockFile = 0;
34-
/** Global flag to indicate we should check to see if there are
35-
* block/undo files that should be deleted. Set on startup
36-
* or if we allocate more file space when we're in prune mode
37-
*/
38-
bool fCheckForPruning = false;
39-
40-
/** Dirty block index entries. */
41-
std::set<CBlockIndex*> setDirtyBlockIndex;
42-
43-
/** Dirty block file entries. */
44-
std::set<int> setDirtyFileInfo;
45-
// } // namespace
46-
4730
static FILE* OpenUndoFile(const FlatFilePos& pos, bool fReadOnly = false);
4831
static FlatFileSeq BlockFileSeq();
4932
static FlatFileSeq UndoFileSeq();

src/node/blockstorage.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ struct CBlockIndexWorkComparator {
6464
class BlockManager
6565
{
6666
friend CChainState;
67+
friend ChainstateManager;
6768

6869
private:
6970
void FlushBlockFile(bool fFinalize = false, bool finalize_undo = false);
@@ -80,7 +81,7 @@ class BlockManager
8081
* space is allocated in a block or undo file, staying below the target. Changing back to unpruned requires a reindex
8182
* (which in this case means the blockchain must be re-downloaded.)
8283
*
83-
* Pruning functions are called from FlushStateToDisk when the global fCheckForPruning flag has been set.
84+
* Pruning functions are called from FlushStateToDisk when the fCheckForPruning flag has been set.
8485
* Block and undo files are deleted in lock-step (when blk00003.dat is deleted, so is rev00003.dat.)
8586
* Pruning cannot take place until the longest chain is at least a certain length (CChainParams::nPruneAfterHeight).
8687
* Pruning will never delete a block within a defined distance (currently 288) from the active chain's tip.
@@ -91,6 +92,21 @@ class BlockManager
9192
*/
9293
void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight, int chain_tip_height, int prune_height, bool is_ibd);
9394

95+
RecursiveMutex cs_LastBlockFile;
96+
std::vector<CBlockFileInfo> vinfoBlockFile;
97+
int nLastBlockFile = 0;
98+
/** Global flag to indicate we should check to see if there are
99+
* block/undo files that should be deleted. Set on startup
100+
* or if we allocate more file space when we're in prune mode
101+
*/
102+
bool fCheckForPruning = false;
103+
104+
/** Dirty block index entries. */
105+
std::set<CBlockIndex*> setDirtyBlockIndex;
106+
107+
/** Dirty block file entries. */
108+
std::set<int> setDirtyFileInfo;
109+
94110
public:
95111
BlockMap m_block_index GUARDED_BY(cs_main);
96112

src/validation.cpp

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,6 @@ arith_uint256 nMinimumChainWork;
133133

134134
CFeeRate minRelayTxFee = CFeeRate(DEFAULT_MIN_RELAY_TX_FEE);
135135

136-
// Internal stuff from blockstorage ...
137-
extern RecursiveMutex cs_LastBlockFile;
138-
extern std::vector<CBlockFileInfo> vinfoBlockFile;
139-
extern int nLastBlockFile;
140-
extern bool fCheckForPruning;
141-
extern std::set<CBlockIndex*> setDirtyBlockIndex;
142-
extern std::set<int> setDirtyFileInfo;
143-
void FlushBlockFile(bool fFinalize = false, bool finalize_undo = false);
144-
// ... TODO move fully to blockstorage
145-
146136
CBlockIndex* CChainState::FindForkInGlobalIndex(const CBlockLocator& locator) const
147137
{
148138
AssertLockHeld(cs_main);
@@ -1505,7 +1495,7 @@ void CChainState::InvalidBlockFound(CBlockIndex* pindex, const BlockValidationSt
15051495
if (state.GetResult() != BlockValidationResult::BLOCK_MUTATED) {
15061496
pindex->nStatus |= BLOCK_FAILED_VALID;
15071497
m_chainman.m_failed_blocks.insert(pindex);
1508-
setDirtyBlockIndex.insert(pindex);
1498+
m_blockman.setDirtyBlockIndex.insert(pindex);
15091499
setBlockIndexCandidates.erase(pindex);
15101500
InvalidChainFound(pindex);
15111501
}
@@ -2141,7 +2131,7 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
21412131

21422132
if (!pindex->IsValid(BLOCK_VALID_SCRIPTS)) {
21432133
pindex->RaiseValidity(BLOCK_VALID_SCRIPTS);
2144-
setDirtyBlockIndex.insert(pindex);
2134+
m_blockman.setDirtyBlockIndex.insert(pindex);
21452135
}
21462136

21472137
assert(pindex->phashBlock);
@@ -2214,8 +2204,8 @@ bool CChainState::FlushStateToDisk(
22142204
bool fDoFullFlush = false;
22152205

22162206
CoinsCacheSizeState cache_state = GetCoinsCacheSizeState();
2217-
LOCK(cs_LastBlockFile);
2218-
if (fPruneMode && (fCheckForPruning || nManualPruneHeight > 0) && !fReindex) {
2207+
LOCK(m_blockman.cs_LastBlockFile);
2208+
if (fPruneMode && (m_blockman.fCheckForPruning || nManualPruneHeight > 0) && !fReindex) {
22192209
// make sure we don't prune above the blockfilterindexes bestblocks
22202210
// pruning is height-based
22212211
int last_prune = m_chain.Height(); // last height we can prune
@@ -2231,7 +2221,7 @@ bool CChainState::FlushStateToDisk(
22312221
LOG_TIME_MILLIS_WITH_CATEGORY("find files to prune", BCLog::BENCH);
22322222

22332223
m_blockman.FindFilesToPrune(setFilesToPrune, m_params.PruneAfterHeight(), m_chain.Height(), last_prune, IsInitialBlockDownload());
2234-
fCheckForPruning = false;
2224+
m_blockman.fCheckForPruning = false;
22352225
}
22362226
if (!setFilesToPrune.empty()) {
22372227
fFlushForPrune = true;
@@ -2336,7 +2326,7 @@ void CChainState::ForceFlushStateToDisk()
23362326
void CChainState::PruneAndFlush()
23372327
{
23382328
BlockValidationState state;
2339-
fCheckForPruning = true;
2329+
m_blockman.fCheckForPruning = true;
23402330
if (!this->FlushStateToDisk(state, FlushStateMode::NONE)) {
23412331
LogPrintf("%s: failed to flush state (%s)\n", __func__, state.ToString());
23422332
}
@@ -3006,14 +2996,14 @@ bool CChainState::InvalidateBlock(BlockValidationState& state, CBlockIndex* pind
30062996
// are no blocks that meet the "have data and are not invalid per
30072997
// nStatus" criteria for inclusion in setBlockIndexCandidates).
30082998
invalid_walk_tip->nStatus |= BLOCK_FAILED_VALID;
3009-
setDirtyBlockIndex.insert(invalid_walk_tip);
2999+
m_blockman.setDirtyBlockIndex.insert(invalid_walk_tip);
30103000
setBlockIndexCandidates.erase(invalid_walk_tip);
30113001
setBlockIndexCandidates.insert(invalid_walk_tip->pprev);
30123002
if (invalid_walk_tip->pprev == to_mark_failed && (to_mark_failed->nStatus & BLOCK_FAILED_VALID)) {
30133003
// We only want to mark the last disconnected block as BLOCK_FAILED_VALID; its children
30143004
// need to be BLOCK_FAILED_CHILD instead.
30153005
to_mark_failed->nStatus = (to_mark_failed->nStatus ^ BLOCK_FAILED_VALID) | BLOCK_FAILED_CHILD;
3016-
setDirtyBlockIndex.insert(to_mark_failed);
3006+
m_blockman.setDirtyBlockIndex.insert(to_mark_failed);
30173007
}
30183008

30193009
// Add any equal or more work headers to setBlockIndexCandidates
@@ -3043,7 +3033,7 @@ bool CChainState::InvalidateBlock(BlockValidationState& state, CBlockIndex* pind
30433033

30443034
// Mark pindex (or the last disconnected block) as invalid, even when it never was in the main chain
30453035
to_mark_failed->nStatus |= BLOCK_FAILED_VALID;
3046-
setDirtyBlockIndex.insert(to_mark_failed);
3036+
m_blockman.setDirtyBlockIndex.insert(to_mark_failed);
30473037
setBlockIndexCandidates.erase(to_mark_failed);
30483038
m_chainman.m_failed_blocks.insert(to_mark_failed);
30493039

@@ -3082,7 +3072,7 @@ void CChainState::ResetBlockFailureFlags(CBlockIndex *pindex) {
30823072
while (it != m_blockman.m_block_index.end()) {
30833073
if (!it->second->IsValid() && it->second->GetAncestor(nHeight) == pindex) {
30843074
it->second->nStatus &= ~BLOCK_FAILED_MASK;
3085-
setDirtyBlockIndex.insert(it->second);
3075+
m_blockman.setDirtyBlockIndex.insert(it->second);
30863076
if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && it->second->HaveTxsDownloaded() && setBlockIndexCandidates.value_comp()(m_chain.Tip(), it->second)) {
30873077
setBlockIndexCandidates.insert(it->second);
30883078
}
@@ -3099,7 +3089,7 @@ void CChainState::ResetBlockFailureFlags(CBlockIndex *pindex) {
30993089
while (pindex != nullptr) {
31003090
if (pindex->nStatus & BLOCK_FAILED_MASK) {
31013091
pindex->nStatus &= ~BLOCK_FAILED_MASK;
3102-
setDirtyBlockIndex.insert(pindex);
3092+
m_blockman.setDirtyBlockIndex.insert(pindex);
31033093
m_chainman.m_failed_blocks.erase(pindex);
31043094
}
31053095
pindex = pindex->pprev;
@@ -3119,7 +3109,7 @@ void CChainState::ReceivedBlockTransactions(const CBlock& block, CBlockIndex* pi
31193109
pindexNew->nStatus |= BLOCK_OPT_WITNESS;
31203110
}
31213111
pindexNew->RaiseValidity(BLOCK_VALID_TRANSACTIONS);
3122-
setDirtyBlockIndex.insert(pindexNew);
3112+
m_blockman.setDirtyBlockIndex.insert(pindexNew);
31233113

31243114
if (pindexNew->pprev == nullptr || pindexNew->pprev->HaveTxsDownloaded()) {
31253115
// If pindexNew is the genesis block or all parents are BLOCK_VALID_TRANSACTIONS.
@@ -3481,7 +3471,7 @@ bool ChainstateManager::AcceptBlockHeader(const CBlockHeader& block, BlockValida
34813471
CBlockIndex* invalid_walk = pindexPrev;
34823472
while (invalid_walk != failedit) {
34833473
invalid_walk->nStatus |= BLOCK_FAILED_CHILD;
3484-
setDirtyBlockIndex.insert(invalid_walk);
3474+
m_blockman.setDirtyBlockIndex.insert(invalid_walk);
34853475
invalid_walk = invalid_walk->pprev;
34863476
}
34873477
LogPrint(BCLog::VALIDATION, "%s: %s prev block invalid\n", __func__, hash.ToString());
@@ -3579,7 +3569,7 @@ bool CChainState::AcceptBlock(const std::shared_ptr<const CBlock>& pblock, Block
35793569
!ContextualCheckBlock(block, state, m_params.GetConsensus(), pindex->pprev)) {
35803570
if (state.IsInvalid() && state.GetResult() != BlockValidationResult::BLOCK_MUTATED) {
35813571
pindex->nStatus |= BLOCK_FAILED_VALID;
3582-
setDirtyBlockIndex.insert(pindex);
3572+
m_blockman.setDirtyBlockIndex.insert(pindex);
35833573
}
35843574
return error("%s: %s", __func__, state.ToString());
35853575
}
@@ -4914,7 +4904,7 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
49144904
index->nStatus |= BLOCK_OPT_WITNESS;
49154905
}
49164906

4917-
setDirtyBlockIndex.insert(index);
4907+
m_blockman.setDirtyBlockIndex.insert(index);
49184908
// Changes to the block index will be flushed to disk after this call
49194909
// returns in `ActivateSnapshot()`, when `MaybeRebalanceCaches()` is
49204910
// called, since we've added a snapshot chainstate and therefore will

0 commit comments

Comments
 (0)