mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-25 10:43:19 -03:00
Merge bitcoin/bitcoin#26900: refactor: Add BlockManager getters
faf7b4f1fc
Add BlockManager::IsPruneMode() (MarcoFalke)fae71fe27e
Add BlockManager::GetPruneTarget() (MarcoFalke)fa0f0436d8
Add BlockManager::LoadingBlocks() (MarcoFalke) Pull request description: Requested in https://github.com/bitcoin/bitcoin/pull/25781#discussion_r1061323795, but adding getters seems unrelated from removing globals, so I split it out for now. ACKs for top commit: dergoegge: Code review ACKfaf7b4f1fc
brunoerg: crACKfaf7b4f1fc
Tree-SHA512: 204d0e9a0e8b78175482f89b4ce620fba0e65d8e49ad845d187af44d3843f4c733a01bac1ffe5a5319f524d8346123693a456778b69d6c75268c447eb8839642
This commit is contained in:
commit
9a288430df
9 changed files with 53 additions and 45 deletions
|
@ -415,8 +415,9 @@ IndexSummary BaseIndex::GetSummary() const
|
|||
return summary;
|
||||
}
|
||||
|
||||
void BaseIndex::SetBestBlockIndex(const CBlockIndex* block) {
|
||||
assert(!node::fPruneMode || AllowPrune());
|
||||
void BaseIndex::SetBestBlockIndex(const CBlockIndex* block)
|
||||
{
|
||||
assert(!m_chainstate->m_blockman.IsPruneMode() || AllowPrune());
|
||||
|
||||
if (AllowPrune() && block) {
|
||||
node::PruneLockInfo prune_lock;
|
||||
|
|
10
src/init.cpp
10
src/init.cpp
|
@ -1499,7 +1499,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
|||
options.mempool = Assert(node.mempool.get());
|
||||
options.reindex = node::fReindex;
|
||||
options.reindex_chainstate = fReindexChainState;
|
||||
options.prune = node::fPruneMode;
|
||||
options.prune = chainman.m_blockman.IsPruneMode();
|
||||
options.check_blocks = args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS);
|
||||
options.check_level = args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL);
|
||||
options.check_interrupt = ShutdownRequested;
|
||||
|
@ -1609,7 +1609,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
|||
|
||||
// if pruning, perform the initial blockstore prune
|
||||
// after any wallet rescanning has taken place.
|
||||
if (fPruneMode) {
|
||||
if (chainman.m_blockman.IsPruneMode()) {
|
||||
if (!fReindex) {
|
||||
LOCK(cs_main);
|
||||
for (Chainstate* chainstate : chainman.GetAll()) {
|
||||
|
@ -1637,8 +1637,10 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
|||
|
||||
// On first startup, warn on low block storage space
|
||||
if (!fReindex && !fReindexChainState && chain_active_height <= 1) {
|
||||
uint64_t additional_bytes_needed = fPruneMode ? nPruneTarget
|
||||
: chainparams.AssumedBlockchainSize() * 1024 * 1024 * 1024;
|
||||
uint64_t additional_bytes_needed{
|
||||
chainman.m_blockman.IsPruneMode() ?
|
||||
chainman.m_blockman.GetPruneTarget() :
|
||||
chainparams.AssumedBlockchainSize() * 1024 * 1024 * 1024};
|
||||
|
||||
if (!CheckDiskSpace(args.GetBlocksDirPath(), additional_bytes_needed)) {
|
||||
InitWarning(strprintf(_(
|
||||
|
|
|
@ -53,9 +53,6 @@
|
|||
|
||||
using node::ReadBlockFromDisk;
|
||||
using node::ReadRawBlockFromDisk;
|
||||
using node::fImporting;
|
||||
using node::fPruneMode;
|
||||
using node::fReindex;
|
||||
|
||||
/** How long to cache transactions in mapRelay for normal relay */
|
||||
static constexpr auto RELAY_TX_CACHE_TIME = 15min;
|
||||
|
@ -1739,8 +1736,7 @@ bool PeerManagerImpl::BlockRequestAllowed(const CBlockIndex* pindex)
|
|||
|
||||
std::optional<std::string> PeerManagerImpl::FetchBlock(NodeId peer_id, const CBlockIndex& block_index)
|
||||
{
|
||||
if (fImporting) return "Importing...";
|
||||
if (fReindex) return "Reindexing...";
|
||||
if (m_chainman.m_blockman.LoadingBlocks()) return "Loading blocks ...";
|
||||
|
||||
// Ensure this peer exists and hasn't been disconnected
|
||||
PeerRef peer = GetPeerRef(peer_id);
|
||||
|
@ -3697,7 +3693,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
|
|||
LogPrint(BCLog::NET, "got inv: %s %s peer=%d\n", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom.GetId());
|
||||
|
||||
UpdateBlockAvailability(pfrom.GetId(), inv.hash);
|
||||
if (!fAlreadyHave && !fImporting && !fReindex && !IsBlockRequested(inv.hash)) {
|
||||
if (!fAlreadyHave && !m_chainman.m_blockman.LoadingBlocks() && !IsBlockRequested(inv.hash)) {
|
||||
// Headers-first is the primary method of announcement on
|
||||
// the network. If a node fell back to sending blocks by
|
||||
// inv, it may be for a re-org, or because we haven't
|
||||
|
@ -3830,8 +3826,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
|
|||
// If pruning, don't inv blocks unless we have on disk and are likely to still have
|
||||
// for some reasonable time window (1 hour) that block relay might require.
|
||||
const int nPrunedBlocksLikelyToHave = MIN_BLOCKS_TO_KEEP - 3600 / m_chainparams.GetConsensus().nPowTargetSpacing;
|
||||
if (fPruneMode && (!(pindex->nStatus & BLOCK_HAVE_DATA) || pindex->nHeight <= m_chainman.ActiveChain().Tip()->nHeight - nPrunedBlocksLikelyToHave))
|
||||
{
|
||||
if (m_chainman.m_blockman.IsPruneMode() && (!(pindex->nStatus & BLOCK_HAVE_DATA) || pindex->nHeight <= m_chainman.ActiveChain().Tip()->nHeight - nPrunedBlocksLikelyToHave)) {
|
||||
LogPrint(BCLog::NET, " getblocks stopping, pruned or too old block at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
|
||||
break;
|
||||
}
|
||||
|
@ -3907,7 +3902,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
|
|||
return;
|
||||
}
|
||||
|
||||
if (fImporting || fReindex) {
|
||||
if (m_chainman.m_blockman.LoadingBlocks()) {
|
||||
LogPrint(BCLog::NET, "Ignoring getheaders from peer=%d while importing/reindexing\n", pfrom.GetId());
|
||||
return;
|
||||
}
|
||||
|
@ -4186,7 +4181,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
|
|||
if (msg_type == NetMsgType::CMPCTBLOCK)
|
||||
{
|
||||
// Ignore cmpctblock received while importing
|
||||
if (fImporting || fReindex) {
|
||||
if (m_chainman.m_blockman.LoadingBlocks()) {
|
||||
LogPrint(BCLog::NET, "Unexpected cmpctblock message received from peer %d\n", pfrom.GetId());
|
||||
return;
|
||||
}
|
||||
|
@ -4402,7 +4397,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
|
|||
if (msg_type == NetMsgType::BLOCKTXN)
|
||||
{
|
||||
// Ignore blocktxn received while importing
|
||||
if (fImporting || fReindex) {
|
||||
if (m_chainman.m_blockman.LoadingBlocks()) {
|
||||
LogPrint(BCLog::NET, "Unexpected blocktxn message received from peer %d\n", pfrom.GetId());
|
||||
return;
|
||||
}
|
||||
|
@ -4477,7 +4472,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
|
|||
if (msg_type == NetMsgType::HEADERS)
|
||||
{
|
||||
// Ignore headers received while importing
|
||||
if (fImporting || fReindex) {
|
||||
if (m_chainman.m_blockman.LoadingBlocks()) {
|
||||
LogPrint(BCLog::NET, "Unexpected headers message received from peer %d\n", pfrom.GetId());
|
||||
return;
|
||||
}
|
||||
|
@ -4522,7 +4517,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
|
|||
if (msg_type == NetMsgType::BLOCK)
|
||||
{
|
||||
// Ignore block received while importing
|
||||
if (fImporting || fReindex) {
|
||||
if (m_chainman.m_blockman.LoadingBlocks()) {
|
||||
LogPrint(BCLog::NET, "Unexpected block message received from peer %d\n", pfrom.GetId());
|
||||
return;
|
||||
}
|
||||
|
@ -5109,7 +5104,7 @@ void PeerManagerImpl::CheckForStaleTipAndEvictPeers()
|
|||
if (now > m_stale_tip_check_time) {
|
||||
// Check whether our tip is stale, and if so, allow using an extra
|
||||
// outbound peer
|
||||
if (!fImporting && !fReindex && m_connman.GetNetworkActive() && m_connman.GetUseAddrmanOutgoing() && TipMayBeStale()) {
|
||||
if (!m_chainman.m_blockman.LoadingBlocks() && m_connman.GetNetworkActive() && m_connman.GetUseAddrmanOutgoing() && TipMayBeStale()) {
|
||||
LogPrintf("Potential stale tip detected, will try using extra outbound peer (last tip update: %d seconds ago)\n",
|
||||
count_seconds(now - m_last_tip_update.load()));
|
||||
m_connman.SetTryNewOutboundPeer(true);
|
||||
|
@ -5416,7 +5411,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
|
|||
}
|
||||
}
|
||||
|
||||
if (!state.fSyncStarted && CanServeBlocks(*peer) && !fImporting && !fReindex) {
|
||||
if (!state.fSyncStarted && CanServeBlocks(*peer) && !m_chainman.m_blockman.LoadingBlocks()) {
|
||||
// Only actively request headers from a single peer, unless we're close to today.
|
||||
if ((nSyncStarted == 0 && sync_blocks_and_headers_from_peer) || m_chainman.m_best_header->Time() > GetAdjustedTime() - 24h) {
|
||||
const CBlockIndex* pindexStart = m_chainman.m_best_header;
|
||||
|
|
|
@ -48,10 +48,7 @@ static constexpr size_t BLOCK_SERIALIZATION_HEADER_SIZE = CMessageHeader::MESSAG
|
|||
|
||||
extern std::atomic_bool fImporting;
|
||||
extern std::atomic_bool fReindex;
|
||||
/** Pruning-related variables and constants */
|
||||
/** True if we're running in -prune mode. */
|
||||
extern bool fPruneMode;
|
||||
/** Number of bytes of block files that we're trying to stay below. */
|
||||
extern uint64_t nPruneTarget;
|
||||
|
||||
// Because validation code takes pointers to the map's CBlockIndex objects, if
|
||||
|
@ -176,6 +173,17 @@ public:
|
|||
/** Store block on disk. If dbp is not nullptr, then it provides the known position of the block within a block file on disk. */
|
||||
FlatFilePos SaveBlockToDisk(const CBlock& block, int nHeight, CChain& active_chain, const CChainParams& chainparams, const FlatFilePos* dbp);
|
||||
|
||||
/** Whether running in -prune mode. */
|
||||
[[nodiscard]] bool IsPruneMode() const { return fPruneMode; }
|
||||
|
||||
/** Attempt to stay below this number of bytes of block files. */
|
||||
[[nodiscard]] uint64_t GetPruneTarget() const { return nPruneTarget; }
|
||||
|
||||
[[nodiscard]] bool LoadingBlocks() const
|
||||
{
|
||||
return fImporting || fReindex;
|
||||
}
|
||||
|
||||
/** Calculate the amount of disk space the block & undo files currently use */
|
||||
uint64_t CalculateCurrentUsage();
|
||||
|
||||
|
|
|
@ -44,10 +44,10 @@ ChainstateLoadResult LoadChainstate(ChainstateManager& chainman, const CacheSize
|
|||
if (chainman.MinimumChainWork() < UintToArith256(chainman.GetConsensus().nMinimumChainWork)) {
|
||||
LogPrintf("Warning: nMinimumChainWork set below default value of %s\n", chainman.GetConsensus().nMinimumChainWork.GetHex());
|
||||
}
|
||||
if (nPruneTarget == std::numeric_limits<uint64_t>::max()) {
|
||||
if (chainman.m_blockman.GetPruneTarget() == std::numeric_limits<uint64_t>::max()) {
|
||||
LogPrintf("Block pruning enabled. Use RPC call pruneblockchain(height) to manually prune block and undo files.\n");
|
||||
} else if (nPruneTarget) {
|
||||
LogPrintf("Prune configured to target %u MiB on disk for block and undo files.\n", nPruneTarget / 1024 / 1024);
|
||||
} else if (chainman.m_blockman.GetPruneTarget()) {
|
||||
LogPrintf("Prune configured to target %u MiB on disk for block and undo files.\n", chainman.m_blockman.GetPruneTarget() / 1024 / 1024);
|
||||
}
|
||||
|
||||
LOCK(cs_main);
|
||||
|
|
|
@ -711,8 +711,9 @@ public:
|
|||
LOCK(::cs_main);
|
||||
return chainman().m_blockman.m_have_pruned;
|
||||
}
|
||||
bool isReadyToBroadcast() override { return !node::fImporting && !node::fReindex && !isInitialBlockDownload(); }
|
||||
bool isInitialBlockDownload() override {
|
||||
bool isReadyToBroadcast() override { return !chainman().m_blockman.LoadingBlocks() && !isInitialBlockDownload(); }
|
||||
bool isInitialBlockDownload() override
|
||||
{
|
||||
return chainman().ActiveChainstate().IsInitialBlockDownload();
|
||||
}
|
||||
bool shutdownRequested() override { return ShutdownRequested(); }
|
||||
|
|
|
@ -460,7 +460,7 @@ static RPCHelpMan getblockfrompeer()
|
|||
|
||||
// Fetching blocks before the node has syncing past their height can prevent block files from
|
||||
// being pruned, so we avoid it if the node is in prune mode.
|
||||
if (node::fPruneMode && index->nHeight > WITH_LOCK(chainman.GetMutex(), return chainman.ActiveTip()->nHeight)) {
|
||||
if (chainman.m_blockman.IsPruneMode() && index->nHeight > WITH_LOCK(chainman.GetMutex(), return chainman.ActiveTip()->nHeight)) {
|
||||
throw JSONRPCError(RPC_MISC_ERROR, "In prune mode, only blocks that the node has already synced previously can be fetched from a peer");
|
||||
}
|
||||
|
||||
|
@ -775,10 +775,11 @@ static RPCHelpMan pruneblockchain()
|
|||
},
|
||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
||||
{
|
||||
if (!node::fPruneMode)
|
||||
throw JSONRPCError(RPC_MISC_ERROR, "Cannot prune blocks because node is not in prune mode.");
|
||||
|
||||
ChainstateManager& chainman = EnsureAnyChainman(request.context);
|
||||
if (!chainman.m_blockman.IsPruneMode()) {
|
||||
throw JSONRPCError(RPC_MISC_ERROR, "Cannot prune blocks because node is not in prune mode.");
|
||||
}
|
||||
|
||||
LOCK(cs_main);
|
||||
Chainstate& active_chainstate = chainman.ActiveChainstate();
|
||||
CChain& active_chain = active_chainstate.m_chain;
|
||||
|
@ -1266,15 +1267,15 @@ RPCHelpMan getblockchaininfo()
|
|||
obj.pushKV("initialblockdownload", active_chainstate.IsInitialBlockDownload());
|
||||
obj.pushKV("chainwork", tip.nChainWork.GetHex());
|
||||
obj.pushKV("size_on_disk", chainman.m_blockman.CalculateCurrentUsage());
|
||||
obj.pushKV("pruned", node::fPruneMode);
|
||||
if (node::fPruneMode) {
|
||||
obj.pushKV("pruned", chainman.m_blockman.IsPruneMode());
|
||||
if (chainman.m_blockman.IsPruneMode()) {
|
||||
obj.pushKV("pruneheight", chainman.m_blockman.GetFirstStoredBlock(tip)->nHeight);
|
||||
|
||||
// if 0, execution bypasses the whole if block.
|
||||
bool automatic_pruning{args.GetIntArg("-prune", 0) != 1};
|
||||
obj.pushKV("automatic_pruning", automatic_pruning);
|
||||
if (automatic_pruning) {
|
||||
obj.pushKV("prune_target_size", node::nPruneTarget);
|
||||
obj.pushKV("prune_target_size", chainman.m_blockman.GetPruneTarget());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -208,23 +208,24 @@ ChainTestingSetup::~ChainTestingSetup()
|
|||
|
||||
void TestingSetup::LoadVerifyActivateChainstate()
|
||||
{
|
||||
auto& chainman{*Assert(m_node.chainman)};
|
||||
node::ChainstateLoadOptions options;
|
||||
options.mempool = Assert(m_node.mempool.get());
|
||||
options.block_tree_db_in_memory = m_block_tree_db_in_memory;
|
||||
options.coins_db_in_memory = m_coins_db_in_memory;
|
||||
options.reindex = node::fReindex;
|
||||
options.reindex_chainstate = m_args.GetBoolArg("-reindex-chainstate", false);
|
||||
options.prune = node::fPruneMode;
|
||||
options.prune = chainman.m_blockman.IsPruneMode();
|
||||
options.check_blocks = m_args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS);
|
||||
options.check_level = m_args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL);
|
||||
auto [status, error] = LoadChainstate(*Assert(m_node.chainman), m_cache_sizes, options);
|
||||
auto [status, error] = LoadChainstate(chainman, m_cache_sizes, options);
|
||||
assert(status == node::ChainstateLoadStatus::SUCCESS);
|
||||
|
||||
std::tie(status, error) = VerifyLoadedChainstate(*Assert(m_node.chainman), options);
|
||||
std::tie(status, error) = VerifyLoadedChainstate(chainman, options);
|
||||
assert(status == node::ChainstateLoadStatus::SUCCESS);
|
||||
|
||||
BlockValidationState state;
|
||||
if (!m_node.chainman->ActiveChainstate().ActivateBestChain(state)) {
|
||||
if (!chainman.ActiveChainstate().ActivateBestChain(state)) {
|
||||
throw std::runtime_error(strprintf("ActivateBestChain failed. (%s)", state.ToString()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -76,8 +76,6 @@ using node::BlockManager;
|
|||
using node::BlockMap;
|
||||
using node::CBlockIndexHeightOnlyComparator;
|
||||
using node::CBlockIndexWorkComparator;
|
||||
using node::fImporting;
|
||||
using node::fPruneMode;
|
||||
using node::fReindex;
|
||||
using node::ReadBlockFromDisk;
|
||||
using node::SnapshotMetadata;
|
||||
|
@ -1573,8 +1571,9 @@ bool Chainstate::IsInitialBlockDownload() const
|
|||
LOCK(cs_main);
|
||||
if (m_cached_finished_ibd.load(std::memory_order_relaxed))
|
||||
return false;
|
||||
if (fImporting || fReindex)
|
||||
if (m_chainman.m_blockman.LoadingBlocks()) {
|
||||
return true;
|
||||
}
|
||||
if (m_chain.Tip() == nullptr)
|
||||
return true;
|
||||
if (m_chain.Tip()->nChainWork < m_chainman.MinimumChainWork()) {
|
||||
|
@ -2411,7 +2410,7 @@ bool Chainstate::FlushStateToDisk(
|
|||
|
||||
CoinsCacheSizeState cache_state = GetCoinsCacheSizeState();
|
||||
LOCK(m_blockman.cs_LastBlockFile);
|
||||
if (fPruneMode && (m_blockman.m_check_for_pruning || nManualPruneHeight > 0) && !fReindex) {
|
||||
if (m_blockman.IsPruneMode() && (m_blockman.m_check_for_pruning || nManualPruneHeight > 0) && !fReindex) {
|
||||
// make sure we don't prune above any of the prune locks bestblocks
|
||||
// pruning is height-based
|
||||
int last_prune{m_chain.Height()}; // last height we can prune
|
||||
|
@ -4097,7 +4096,7 @@ bool CVerifyDB::VerifyDB(
|
|||
if (pindex->nHeight <= chainstate.m_chain.Height() - nCheckDepth) {
|
||||
break;
|
||||
}
|
||||
if ((fPruneMode || is_snapshot_cs) && !(pindex->nStatus & BLOCK_HAVE_DATA)) {
|
||||
if ((chainstate.m_blockman.IsPruneMode() || is_snapshot_cs) && !(pindex->nStatus & BLOCK_HAVE_DATA)) {
|
||||
// If pruning or running under an assumeutxo snapshot, only go
|
||||
// back as far as we have data.
|
||||
LogPrintf("VerifyDB(): block verification stopping at height %d (pruning, no data)\n", pindex->nHeight);
|
||||
|
|
Loading…
Add table
Reference in a new issue