mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-05-02 16:01:58 +02:00
Make blockstorage globals private members of BlockManager
This commit is contained in:
parent
faa8c2d7d7
commit
facd3df21f
@ -27,23 +27,6 @@ bool fHavePruned = false;
|
|||||||
bool fPruneMode = false;
|
bool fPruneMode = false;
|
||||||
uint64_t nPruneTarget = 0;
|
uint64_t nPruneTarget = 0;
|
||||||
|
|
||||||
// TODO make namespace {
|
|
||||||
RecursiveMutex cs_LastBlockFile;
|
|
||||||
std::vector<CBlockFileInfo> vinfoBlockFile;
|
|
||||||
int nLastBlockFile = 0;
|
|
||||||
/** Global flag to indicate we should check to see if there are
|
|
||||||
* block/undo files that should be deleted. Set on startup
|
|
||||||
* or if we allocate more file space when we're in prune mode
|
|
||||||
*/
|
|
||||||
bool fCheckForPruning = false;
|
|
||||||
|
|
||||||
/** Dirty block index entries. */
|
|
||||||
std::set<CBlockIndex*> setDirtyBlockIndex;
|
|
||||||
|
|
||||||
/** Dirty block file entries. */
|
|
||||||
std::set<int> setDirtyFileInfo;
|
|
||||||
// } // namespace
|
|
||||||
|
|
||||||
static FILE* OpenUndoFile(const FlatFilePos& pos, bool fReadOnly = false);
|
static FILE* OpenUndoFile(const FlatFilePos& pos, bool fReadOnly = false);
|
||||||
static FlatFileSeq BlockFileSeq();
|
static FlatFileSeq BlockFileSeq();
|
||||||
static FlatFileSeq UndoFileSeq();
|
static FlatFileSeq UndoFileSeq();
|
||||||
|
@ -64,6 +64,7 @@ struct CBlockIndexWorkComparator {
|
|||||||
class BlockManager
|
class BlockManager
|
||||||
{
|
{
|
||||||
friend CChainState;
|
friend CChainState;
|
||||||
|
friend ChainstateManager;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void FlushBlockFile(bool fFinalize = false, bool finalize_undo = false);
|
void FlushBlockFile(bool fFinalize = false, bool finalize_undo = false);
|
||||||
@ -80,7 +81,7 @@ private:
|
|||||||
* space is allocated in a block or undo file, staying below the target. Changing back to unpruned requires a reindex
|
* space is allocated in a block or undo file, staying below the target. Changing back to unpruned requires a reindex
|
||||||
* (which in this case means the blockchain must be re-downloaded.)
|
* (which in this case means the blockchain must be re-downloaded.)
|
||||||
*
|
*
|
||||||
* Pruning functions are called from FlushStateToDisk when the global fCheckForPruning flag has been set.
|
* Pruning functions are called from FlushStateToDisk when the fCheckForPruning flag has been set.
|
||||||
* Block and undo files are deleted in lock-step (when blk00003.dat is deleted, so is rev00003.dat.)
|
* Block and undo files are deleted in lock-step (when blk00003.dat is deleted, so is rev00003.dat.)
|
||||||
* Pruning cannot take place until the longest chain is at least a certain length (CChainParams::nPruneAfterHeight).
|
* Pruning cannot take place until the longest chain is at least a certain length (CChainParams::nPruneAfterHeight).
|
||||||
* Pruning will never delete a block within a defined distance (currently 288) from the active chain's tip.
|
* Pruning will never delete a block within a defined distance (currently 288) from the active chain's tip.
|
||||||
@ -91,6 +92,21 @@ private:
|
|||||||
*/
|
*/
|
||||||
void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight, int chain_tip_height, int prune_height, bool is_ibd);
|
void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight, int chain_tip_height, int prune_height, bool is_ibd);
|
||||||
|
|
||||||
|
RecursiveMutex cs_LastBlockFile;
|
||||||
|
std::vector<CBlockFileInfo> vinfoBlockFile;
|
||||||
|
int nLastBlockFile = 0;
|
||||||
|
/** Global flag to indicate we should check to see if there are
|
||||||
|
* block/undo files that should be deleted. Set on startup
|
||||||
|
* or if we allocate more file space when we're in prune mode
|
||||||
|
*/
|
||||||
|
bool fCheckForPruning = false;
|
||||||
|
|
||||||
|
/** Dirty block index entries. */
|
||||||
|
std::set<CBlockIndex*> setDirtyBlockIndex;
|
||||||
|
|
||||||
|
/** Dirty block file entries. */
|
||||||
|
std::set<int> setDirtyFileInfo;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
BlockMap m_block_index GUARDED_BY(cs_main);
|
BlockMap m_block_index GUARDED_BY(cs_main);
|
||||||
|
|
||||||
|
@ -133,16 +133,6 @@ arith_uint256 nMinimumChainWork;
|
|||||||
|
|
||||||
CFeeRate minRelayTxFee = CFeeRate(DEFAULT_MIN_RELAY_TX_FEE);
|
CFeeRate minRelayTxFee = CFeeRate(DEFAULT_MIN_RELAY_TX_FEE);
|
||||||
|
|
||||||
// Internal stuff from blockstorage ...
|
|
||||||
extern RecursiveMutex cs_LastBlockFile;
|
|
||||||
extern std::vector<CBlockFileInfo> vinfoBlockFile;
|
|
||||||
extern int nLastBlockFile;
|
|
||||||
extern bool fCheckForPruning;
|
|
||||||
extern std::set<CBlockIndex*> setDirtyBlockIndex;
|
|
||||||
extern std::set<int> setDirtyFileInfo;
|
|
||||||
void FlushBlockFile(bool fFinalize = false, bool finalize_undo = false);
|
|
||||||
// ... TODO move fully to blockstorage
|
|
||||||
|
|
||||||
CBlockIndex* CChainState::FindForkInGlobalIndex(const CBlockLocator& locator) const
|
CBlockIndex* CChainState::FindForkInGlobalIndex(const CBlockLocator& locator) const
|
||||||
{
|
{
|
||||||
AssertLockHeld(cs_main);
|
AssertLockHeld(cs_main);
|
||||||
@ -1505,7 +1495,7 @@ void CChainState::InvalidBlockFound(CBlockIndex* pindex, const BlockValidationSt
|
|||||||
if (state.GetResult() != BlockValidationResult::BLOCK_MUTATED) {
|
if (state.GetResult() != BlockValidationResult::BLOCK_MUTATED) {
|
||||||
pindex->nStatus |= BLOCK_FAILED_VALID;
|
pindex->nStatus |= BLOCK_FAILED_VALID;
|
||||||
m_chainman.m_failed_blocks.insert(pindex);
|
m_chainman.m_failed_blocks.insert(pindex);
|
||||||
setDirtyBlockIndex.insert(pindex);
|
m_blockman.setDirtyBlockIndex.insert(pindex);
|
||||||
setBlockIndexCandidates.erase(pindex);
|
setBlockIndexCandidates.erase(pindex);
|
||||||
InvalidChainFound(pindex);
|
InvalidChainFound(pindex);
|
||||||
}
|
}
|
||||||
@ -2141,7 +2131,7 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
|
|||||||
|
|
||||||
if (!pindex->IsValid(BLOCK_VALID_SCRIPTS)) {
|
if (!pindex->IsValid(BLOCK_VALID_SCRIPTS)) {
|
||||||
pindex->RaiseValidity(BLOCK_VALID_SCRIPTS);
|
pindex->RaiseValidity(BLOCK_VALID_SCRIPTS);
|
||||||
setDirtyBlockIndex.insert(pindex);
|
m_blockman.setDirtyBlockIndex.insert(pindex);
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(pindex->phashBlock);
|
assert(pindex->phashBlock);
|
||||||
@ -2214,8 +2204,8 @@ bool CChainState::FlushStateToDisk(
|
|||||||
bool fDoFullFlush = false;
|
bool fDoFullFlush = false;
|
||||||
|
|
||||||
CoinsCacheSizeState cache_state = GetCoinsCacheSizeState();
|
CoinsCacheSizeState cache_state = GetCoinsCacheSizeState();
|
||||||
LOCK(cs_LastBlockFile);
|
LOCK(m_blockman.cs_LastBlockFile);
|
||||||
if (fPruneMode && (fCheckForPruning || nManualPruneHeight > 0) && !fReindex) {
|
if (fPruneMode && (m_blockman.fCheckForPruning || nManualPruneHeight > 0) && !fReindex) {
|
||||||
// make sure we don't prune above the blockfilterindexes bestblocks
|
// make sure we don't prune above the blockfilterindexes bestblocks
|
||||||
// pruning is height-based
|
// pruning is height-based
|
||||||
int last_prune = m_chain.Height(); // last height we can prune
|
int last_prune = m_chain.Height(); // last height we can prune
|
||||||
@ -2231,7 +2221,7 @@ bool CChainState::FlushStateToDisk(
|
|||||||
LOG_TIME_MILLIS_WITH_CATEGORY("find files to prune", BCLog::BENCH);
|
LOG_TIME_MILLIS_WITH_CATEGORY("find files to prune", BCLog::BENCH);
|
||||||
|
|
||||||
m_blockman.FindFilesToPrune(setFilesToPrune, m_params.PruneAfterHeight(), m_chain.Height(), last_prune, IsInitialBlockDownload());
|
m_blockman.FindFilesToPrune(setFilesToPrune, m_params.PruneAfterHeight(), m_chain.Height(), last_prune, IsInitialBlockDownload());
|
||||||
fCheckForPruning = false;
|
m_blockman.fCheckForPruning = false;
|
||||||
}
|
}
|
||||||
if (!setFilesToPrune.empty()) {
|
if (!setFilesToPrune.empty()) {
|
||||||
fFlushForPrune = true;
|
fFlushForPrune = true;
|
||||||
@ -2336,7 +2326,7 @@ void CChainState::ForceFlushStateToDisk()
|
|||||||
void CChainState::PruneAndFlush()
|
void CChainState::PruneAndFlush()
|
||||||
{
|
{
|
||||||
BlockValidationState state;
|
BlockValidationState state;
|
||||||
fCheckForPruning = true;
|
m_blockman.fCheckForPruning = true;
|
||||||
if (!this->FlushStateToDisk(state, FlushStateMode::NONE)) {
|
if (!this->FlushStateToDisk(state, FlushStateMode::NONE)) {
|
||||||
LogPrintf("%s: failed to flush state (%s)\n", __func__, state.ToString());
|
LogPrintf("%s: failed to flush state (%s)\n", __func__, state.ToString());
|
||||||
}
|
}
|
||||||
@ -3006,14 +2996,14 @@ bool CChainState::InvalidateBlock(BlockValidationState& state, CBlockIndex* pind
|
|||||||
// are no blocks that meet the "have data and are not invalid per
|
// are no blocks that meet the "have data and are not invalid per
|
||||||
// nStatus" criteria for inclusion in setBlockIndexCandidates).
|
// nStatus" criteria for inclusion in setBlockIndexCandidates).
|
||||||
invalid_walk_tip->nStatus |= BLOCK_FAILED_VALID;
|
invalid_walk_tip->nStatus |= BLOCK_FAILED_VALID;
|
||||||
setDirtyBlockIndex.insert(invalid_walk_tip);
|
m_blockman.setDirtyBlockIndex.insert(invalid_walk_tip);
|
||||||
setBlockIndexCandidates.erase(invalid_walk_tip);
|
setBlockIndexCandidates.erase(invalid_walk_tip);
|
||||||
setBlockIndexCandidates.insert(invalid_walk_tip->pprev);
|
setBlockIndexCandidates.insert(invalid_walk_tip->pprev);
|
||||||
if (invalid_walk_tip->pprev == to_mark_failed && (to_mark_failed->nStatus & BLOCK_FAILED_VALID)) {
|
if (invalid_walk_tip->pprev == to_mark_failed && (to_mark_failed->nStatus & BLOCK_FAILED_VALID)) {
|
||||||
// We only want to mark the last disconnected block as BLOCK_FAILED_VALID; its children
|
// We only want to mark the last disconnected block as BLOCK_FAILED_VALID; its children
|
||||||
// need to be BLOCK_FAILED_CHILD instead.
|
// need to be BLOCK_FAILED_CHILD instead.
|
||||||
to_mark_failed->nStatus = (to_mark_failed->nStatus ^ BLOCK_FAILED_VALID) | BLOCK_FAILED_CHILD;
|
to_mark_failed->nStatus = (to_mark_failed->nStatus ^ BLOCK_FAILED_VALID) | BLOCK_FAILED_CHILD;
|
||||||
setDirtyBlockIndex.insert(to_mark_failed);
|
m_blockman.setDirtyBlockIndex.insert(to_mark_failed);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add any equal or more work headers to setBlockIndexCandidates
|
// Add any equal or more work headers to setBlockIndexCandidates
|
||||||
@ -3043,7 +3033,7 @@ bool CChainState::InvalidateBlock(BlockValidationState& state, CBlockIndex* pind
|
|||||||
|
|
||||||
// Mark pindex (or the last disconnected block) as invalid, even when it never was in the main chain
|
// Mark pindex (or the last disconnected block) as invalid, even when it never was in the main chain
|
||||||
to_mark_failed->nStatus |= BLOCK_FAILED_VALID;
|
to_mark_failed->nStatus |= BLOCK_FAILED_VALID;
|
||||||
setDirtyBlockIndex.insert(to_mark_failed);
|
m_blockman.setDirtyBlockIndex.insert(to_mark_failed);
|
||||||
setBlockIndexCandidates.erase(to_mark_failed);
|
setBlockIndexCandidates.erase(to_mark_failed);
|
||||||
m_chainman.m_failed_blocks.insert(to_mark_failed);
|
m_chainman.m_failed_blocks.insert(to_mark_failed);
|
||||||
|
|
||||||
@ -3082,7 +3072,7 @@ void CChainState::ResetBlockFailureFlags(CBlockIndex *pindex) {
|
|||||||
while (it != m_blockman.m_block_index.end()) {
|
while (it != m_blockman.m_block_index.end()) {
|
||||||
if (!it->second->IsValid() && it->second->GetAncestor(nHeight) == pindex) {
|
if (!it->second->IsValid() && it->second->GetAncestor(nHeight) == pindex) {
|
||||||
it->second->nStatus &= ~BLOCK_FAILED_MASK;
|
it->second->nStatus &= ~BLOCK_FAILED_MASK;
|
||||||
setDirtyBlockIndex.insert(it->second);
|
m_blockman.setDirtyBlockIndex.insert(it->second);
|
||||||
if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && it->second->HaveTxsDownloaded() && setBlockIndexCandidates.value_comp()(m_chain.Tip(), it->second)) {
|
if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && it->second->HaveTxsDownloaded() && setBlockIndexCandidates.value_comp()(m_chain.Tip(), it->second)) {
|
||||||
setBlockIndexCandidates.insert(it->second);
|
setBlockIndexCandidates.insert(it->second);
|
||||||
}
|
}
|
||||||
@ -3099,7 +3089,7 @@ void CChainState::ResetBlockFailureFlags(CBlockIndex *pindex) {
|
|||||||
while (pindex != nullptr) {
|
while (pindex != nullptr) {
|
||||||
if (pindex->nStatus & BLOCK_FAILED_MASK) {
|
if (pindex->nStatus & BLOCK_FAILED_MASK) {
|
||||||
pindex->nStatus &= ~BLOCK_FAILED_MASK;
|
pindex->nStatus &= ~BLOCK_FAILED_MASK;
|
||||||
setDirtyBlockIndex.insert(pindex);
|
m_blockman.setDirtyBlockIndex.insert(pindex);
|
||||||
m_chainman.m_failed_blocks.erase(pindex);
|
m_chainman.m_failed_blocks.erase(pindex);
|
||||||
}
|
}
|
||||||
pindex = pindex->pprev;
|
pindex = pindex->pprev;
|
||||||
@ -3119,7 +3109,7 @@ void CChainState::ReceivedBlockTransactions(const CBlock& block, CBlockIndex* pi
|
|||||||
pindexNew->nStatus |= BLOCK_OPT_WITNESS;
|
pindexNew->nStatus |= BLOCK_OPT_WITNESS;
|
||||||
}
|
}
|
||||||
pindexNew->RaiseValidity(BLOCK_VALID_TRANSACTIONS);
|
pindexNew->RaiseValidity(BLOCK_VALID_TRANSACTIONS);
|
||||||
setDirtyBlockIndex.insert(pindexNew);
|
m_blockman.setDirtyBlockIndex.insert(pindexNew);
|
||||||
|
|
||||||
if (pindexNew->pprev == nullptr || pindexNew->pprev->HaveTxsDownloaded()) {
|
if (pindexNew->pprev == nullptr || pindexNew->pprev->HaveTxsDownloaded()) {
|
||||||
// If pindexNew is the genesis block or all parents are BLOCK_VALID_TRANSACTIONS.
|
// If pindexNew is the genesis block or all parents are BLOCK_VALID_TRANSACTIONS.
|
||||||
@ -3481,7 +3471,7 @@ bool ChainstateManager::AcceptBlockHeader(const CBlockHeader& block, BlockValida
|
|||||||
CBlockIndex* invalid_walk = pindexPrev;
|
CBlockIndex* invalid_walk = pindexPrev;
|
||||||
while (invalid_walk != failedit) {
|
while (invalid_walk != failedit) {
|
||||||
invalid_walk->nStatus |= BLOCK_FAILED_CHILD;
|
invalid_walk->nStatus |= BLOCK_FAILED_CHILD;
|
||||||
setDirtyBlockIndex.insert(invalid_walk);
|
m_blockman.setDirtyBlockIndex.insert(invalid_walk);
|
||||||
invalid_walk = invalid_walk->pprev;
|
invalid_walk = invalid_walk->pprev;
|
||||||
}
|
}
|
||||||
LogPrint(BCLog::VALIDATION, "%s: %s prev block invalid\n", __func__, hash.ToString());
|
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
|
|||||||
!ContextualCheckBlock(block, state, m_params.GetConsensus(), pindex->pprev)) {
|
!ContextualCheckBlock(block, state, m_params.GetConsensus(), pindex->pprev)) {
|
||||||
if (state.IsInvalid() && state.GetResult() != BlockValidationResult::BLOCK_MUTATED) {
|
if (state.IsInvalid() && state.GetResult() != BlockValidationResult::BLOCK_MUTATED) {
|
||||||
pindex->nStatus |= BLOCK_FAILED_VALID;
|
pindex->nStatus |= BLOCK_FAILED_VALID;
|
||||||
setDirtyBlockIndex.insert(pindex);
|
m_blockman.setDirtyBlockIndex.insert(pindex);
|
||||||
}
|
}
|
||||||
return error("%s: %s", __func__, state.ToString());
|
return error("%s: %s", __func__, state.ToString());
|
||||||
}
|
}
|
||||||
@ -4914,7 +4904,7 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
|
|||||||
index->nStatus |= BLOCK_OPT_WITNESS;
|
index->nStatus |= BLOCK_OPT_WITNESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
setDirtyBlockIndex.insert(index);
|
m_blockman.setDirtyBlockIndex.insert(index);
|
||||||
// Changes to the block index will be flushed to disk after this call
|
// Changes to the block index will be flushed to disk after this call
|
||||||
// returns in `ActivateSnapshot()`, when `MaybeRebalanceCaches()` is
|
// returns in `ActivateSnapshot()`, when `MaybeRebalanceCaches()` is
|
||||||
// called, since we've added a snapshot chainstate and therefore will
|
// called, since we've added a snapshot chainstate and therefore will
|
||||||
|
Loading…
x
Reference in New Issue
Block a user