Skip to content

Commit fa0f043

Browse files
author
MarcoFalke
committed
Add BlockManager::LoadingBlocks()
1 parent 599e941 commit fa0f043

File tree

4 files changed

+19
-16
lines changed

4 files changed

+19
-16
lines changed

src/net_processing.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@
5353

5454
using node::ReadBlockFromDisk;
5555
using node::ReadRawBlockFromDisk;
56-
using node::fImporting;
5756
using node::fPruneMode;
58-
using node::fReindex;
5957

6058
/** How long to cache transactions in mapRelay for normal relay */
6159
static constexpr auto RELAY_TX_CACHE_TIME = 15min;
@@ -1730,8 +1728,7 @@ bool PeerManagerImpl::BlockRequestAllowed(const CBlockIndex* pindex)
17301728

17311729
std::optional<std::string> PeerManagerImpl::FetchBlock(NodeId peer_id, const CBlockIndex& block_index)
17321730
{
1733-
if (fImporting) return "Importing...";
1734-
if (fReindex) return "Reindexing...";
1731+
if (m_chainman.m_blockman.LoadingBlocks()) return "Loading blocks ...";
17351732

17361733
// Ensure this peer exists and hasn't been disconnected
17371734
PeerRef peer = GetPeerRef(peer_id);
@@ -3679,7 +3676,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
36793676
LogPrint(BCLog::NET, "got inv: %s %s peer=%d\n", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom.GetId());
36803677

36813678
UpdateBlockAvailability(pfrom.GetId(), inv.hash);
3682-
if (!fAlreadyHave && !fImporting && !fReindex && !IsBlockRequested(inv.hash)) {
3679+
if (!fAlreadyHave && !m_chainman.m_blockman.LoadingBlocks() && !IsBlockRequested(inv.hash)) {
36833680
// Headers-first is the primary method of announcement on
36843681
// the network. If a node fell back to sending blocks by
36853682
// inv, it may be for a re-org, or because we haven't
@@ -3889,7 +3886,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
38893886
return;
38903887
}
38913888

3892-
if (fImporting || fReindex) {
3889+
if (m_chainman.m_blockman.LoadingBlocks()) {
38933890
LogPrint(BCLog::NET, "Ignoring getheaders from peer=%d while importing/reindexing\n", pfrom.GetId());
38943891
return;
38953892
}
@@ -4171,7 +4168,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
41714168
if (msg_type == NetMsgType::CMPCTBLOCK)
41724169
{
41734170
// Ignore cmpctblock received while importing
4174-
if (fImporting || fReindex) {
4171+
if (m_chainman.m_blockman.LoadingBlocks()) {
41754172
LogPrint(BCLog::NET, "Unexpected cmpctblock message received from peer %d\n", pfrom.GetId());
41764173
return;
41774174
}
@@ -4387,7 +4384,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
43874384
if (msg_type == NetMsgType::BLOCKTXN)
43884385
{
43894386
// Ignore blocktxn received while importing
4390-
if (fImporting || fReindex) {
4387+
if (m_chainman.m_blockman.LoadingBlocks()) {
43914388
LogPrint(BCLog::NET, "Unexpected blocktxn message received from peer %d\n", pfrom.GetId());
43924389
return;
43934390
}
@@ -4462,7 +4459,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
44624459
if (msg_type == NetMsgType::HEADERS)
44634460
{
44644461
// Ignore headers received while importing
4465-
if (fImporting || fReindex) {
4462+
if (m_chainman.m_blockman.LoadingBlocks()) {
44664463
LogPrint(BCLog::NET, "Unexpected headers message received from peer %d\n", pfrom.GetId());
44674464
return;
44684465
}
@@ -4507,7 +4504,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
45074504
if (msg_type == NetMsgType::BLOCK)
45084505
{
45094506
// Ignore block received while importing
4510-
if (fImporting || fReindex) {
4507+
if (m_chainman.m_blockman.LoadingBlocks()) {
45114508
LogPrint(BCLog::NET, "Unexpected block message received from peer %d\n", pfrom.GetId());
45124509
return;
45134510
}
@@ -5092,7 +5089,7 @@ void PeerManagerImpl::CheckForStaleTipAndEvictPeers()
50925089
if (now > m_stale_tip_check_time) {
50935090
// Check whether our tip is stale, and if so, allow using an extra
50945091
// outbound peer
5095-
if (!fImporting && !fReindex && m_connman.GetNetworkActive() && m_connman.GetUseAddrmanOutgoing() && TipMayBeStale()) {
5092+
if (!m_chainman.m_blockman.LoadingBlocks() && m_connman.GetNetworkActive() && m_connman.GetUseAddrmanOutgoing() && TipMayBeStale()) {
50965093
LogPrintf("Potential stale tip detected, will try using extra outbound peer (last tip update: %d seconds ago)\n",
50975094
count_seconds(now - m_last_tip_update.load()));
50985095
m_connman.SetTryNewOutboundPeer(true);
@@ -5399,7 +5396,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
53995396
}
54005397
}
54015398

5402-
if (!state.fSyncStarted && CanServeBlocks(*peer) && !fImporting && !fReindex) {
5399+
if (!state.fSyncStarted && CanServeBlocks(*peer) && !m_chainman.m_blockman.LoadingBlocks()) {
54035400
// Only actively request headers from a single peer, unless we're close to today.
54045401
if ((nSyncStarted == 0 && sync_blocks_and_headers_from_peer) || m_chainman.m_best_header->Time() > GetAdjustedTime() - 24h) {
54055402
const CBlockIndex* pindexStart = m_chainman.m_best_header;

src/node/blockstorage.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@ class BlockManager
176176
/** Store block on disk. If dbp is not nullptr, then it provides the known position of the block within a block file on disk. */
177177
FlatFilePos SaveBlockToDisk(const CBlock& block, int nHeight, CChain& active_chain, const CChainParams& chainparams, const FlatFilePos* dbp);
178178

179+
[[nodiscard]] bool LoadingBlocks() const
180+
{
181+
return fImporting || fReindex;
182+
}
183+
179184
/** Calculate the amount of disk space the block & undo files currently use */
180185
uint64_t CalculateCurrentUsage();
181186

src/node/interfaces.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -711,8 +711,9 @@ class ChainImpl : public Chain
711711
LOCK(::cs_main);
712712
return chainman().m_blockman.m_have_pruned;
713713
}
714-
bool isReadyToBroadcast() override { return !node::fImporting && !node::fReindex && !isInitialBlockDownload(); }
715-
bool isInitialBlockDownload() override {
714+
bool isReadyToBroadcast() override { return !chainman().m_blockman.LoadingBlocks() && !isInitialBlockDownload(); }
715+
bool isInitialBlockDownload() override
716+
{
716717
return chainman().ActiveChainstate().IsInitialBlockDownload();
717718
}
718719
bool shutdownRequested() override { return ShutdownRequested(); }

src/validation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ using node::BlockManager;
7676
using node::BlockMap;
7777
using node::CBlockIndexHeightOnlyComparator;
7878
using node::CBlockIndexWorkComparator;
79-
using node::fImporting;
8079
using node::fPruneMode;
8180
using node::fReindex;
8281
using node::ReadBlockFromDisk;
@@ -1573,8 +1572,9 @@ bool Chainstate::IsInitialBlockDownload() const
15731572
LOCK(cs_main);
15741573
if (m_cached_finished_ibd.load(std::memory_order_relaxed))
15751574
return false;
1576-
if (fImporting || fReindex)
1575+
if (m_chainman.m_blockman.LoadingBlocks()) {
15771576
return true;
1577+
}
15781578
if (m_chain.Tip() == nullptr)
15791579
return true;
15801580
if (m_chain.Tip()->nChainWork < m_chainman.MinimumChainWork()) {

0 commit comments

Comments
 (0)