Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2162,7 +2162,7 @@ void PeerManagerImpl::BlockChecked(const CBlock& block, const BlockValidationSta
// the tip yet so we have no way to check this directly here. Instead we
// just check that there are currently no other blocks in flight.
else if (state.IsValid() &&
!m_chainman.ActiveChainstate().IsInitialBlockDownload() &&
!m_chainman.IsInitialBlockDownload() &&
mapBlocksInFlight.count(hash) == mapBlocksInFlight.size()) {
if (it != mapBlockSource.end()) {
MaybeSetPeerAsAnnouncingHeaderAndIDs(it->second.first);
Expand Down Expand Up @@ -3098,7 +3098,7 @@ void PeerManagerImpl::UpdatePeerStateForReceivedHeaders(CNode& pfrom,

// If we're in IBD, we want outbound peers that will serve us a useful
// chain. Disconnect peers that are on chains with insufficient work.
if (m_chainman.ActiveChainstate().IsInitialBlockDownload() && !may_have_more_headers) {
if (m_chainman.IsInitialBlockDownload() && !may_have_more_headers) {
// If the peer has no more headers to give us, then we know we have
// their tip.
if (nodestate->pindexBestKnownBlock && nodestate->pindexBestKnownBlock->nChainWork < nMinimumChainWork) {
Expand Down Expand Up @@ -4149,7 +4149,7 @@ void PeerManagerImpl::ProcessMessage(
return;
}
bool allowWhileInIBD = allowWhileInIBDObjs.count(inv.type);
if (allowWhileInIBD || !m_chainman.ActiveChainstate().IsInitialBlockDownload()) {
if (allowWhileInIBD || !m_chainman.IsInitialBlockDownload()) {
RequestObject(pfrom.GetId(), inv, current_time, is_masternode);
}
}
Expand Down Expand Up @@ -4434,7 +4434,7 @@ void PeerManagerImpl::ProcessMessage(
// Stop processing the transaction early if we are still in IBD since we don't
// have enough information to validate it yet. Sending unsolicited transactions
// is not considered a protocol violation, so don't punish the peer.
if (m_chainman.ActiveChainstate().IsInitialBlockDownload()) return;
if (m_chainman.IsInitialBlockDownload()) return;

CTransactionRef ptx;
CCoinJoinBroadcastTx dstx;
Expand Down Expand Up @@ -4613,7 +4613,7 @@ void PeerManagerImpl::ProcessMessage(

if (!m_chainman.m_blockman.LookupBlockIndex(cmpctblock.header.hashPrevBlock)) {
// Doesn't connect (or is genesis), instead of DoSing in AcceptBlockHeader, request deeper headers
if (!m_chainman.ActiveChainstate().IsInitialBlockDownload()) {
if (!m_chainman.IsInitialBlockDownload()) {
std::string ret_val = UsesCompressedHeaders(*peer) ? NetMsgType::GETHEADERS2 : NetMsgType::GETHEADERS;
MaybeSendGetHeaders(pfrom, ret_val, m_chainman.ActiveChain().GetLocator(m_chainman.m_best_header), *peer);
}
Expand Down Expand Up @@ -5663,7 +5663,7 @@ void PeerManagerImpl::MaybeSendAddr(CNode& node, Peer& peer, std::chrono::micros

LOCK(peer.m_addr_send_times_mutex);
// Periodically advertise our local address to the peer.
if (fListen && !m_chainman.ActiveChainstate().IsInitialBlockDownload() &&
if (fListen && !m_chainman.IsInitialBlockDownload() &&
peer.m_next_local_addr_send < current_time) {
// If we've sent before, clear the bloom filter for the peer, so that our
// self-announcement will actually go out.
Expand Down Expand Up @@ -5723,7 +5723,6 @@ void PeerManagerImpl::MaybeSendAddr(CNode& node, Peer& peer, std::chrono::micros
peer.m_addrs_to_send.shrink_to_fit();
}
}

namespace {
class CompareInvMempoolOrder
{
Expand Down Expand Up @@ -6279,7 +6278,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
// Message: getdata (blocks)
//
std::vector<CInv> vGetData;
if (CanServeBlocks(*peer) && pto->CanRelay() && ((sync_blocks_and_headers_from_peer && !IsLimitedPeer(*peer)) || !m_chainman.ActiveChainstate().IsInitialBlockDownload()) && state.nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
if (CanServeBlocks(*peer) && pto->CanRelay() && ((sync_blocks_and_headers_from_peer && !IsLimitedPeer(*peer)) || !m_chainman.IsInitialBlockDownload()) && state.nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
std::vector<const CBlockIndex*> vToDownload;
NodeId staller = -1;
FindNextBlocksToDownload(*peer, MAX_BLOCKS_IN_TRANSIT_PER_PEER - state.nBlocksInFlight, vToDownload, staller);
Expand Down
10 changes: 6 additions & 4 deletions src/node/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,9 @@ class NodeImpl : public Node
}
return GuessVerificationProgress(Params().TxData(), tip);
}
bool isInitialBlockDownload() override {
return chainman().ActiveChainstate().IsInitialBlockDownload();
bool isInitialBlockDownload() override
{
return chainman().IsInitialBlockDownload();
}
bool isMasternode() override
{
Expand Down Expand Up @@ -961,8 +962,9 @@ class ChainImpl : public Chain
return m_node.chainman->m_blockman.m_have_pruned;
}
bool isReadyToBroadcast() override { return !node::fImporting && !node::fReindex && !isInitialBlockDownload(); }
bool isInitialBlockDownload() override {
return chainman().ActiveChainstate().IsInitialBlockDownload();
bool isInitialBlockDownload() override
{
return chainman().IsInitialBlockDownload();
}
bool shutdownRequested() override { return ShutdownRequested(); }
void initMessage(const std::string& message) override { ::uiInterface.InitMessage(message); }
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1520,7 +1520,7 @@ RPCHelpMan getblockchaininfo()
obj.pushKV("time", tip.GetBlockTime());
obj.pushKV("mediantime", tip.GetMedianTimePast());
obj.pushKV("verificationprogress", GuessVerificationProgress(Params().TxData(), &tip));
obj.pushKV("initialblockdownload", active_chainstate.IsInitialBlockDownload());
obj.pushKV("initialblockdownload", chainman.IsInitialBlockDownload());
obj.pushKV("chainwork", tip.nChainWork.GetHex());
obj.pushKV("size_on_disk", chainman.m_blockman.CalculateCurrentUsage());
obj.pushKV("pruned", node::fPruneMode);
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ static RPCHelpMan getblocktemplate()
throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, PACKAGE_NAME " is not connected!");
}

if (active_chainstate.IsInitialBlockDownload()) {
if (chainman.IsInitialBlockDownload()) {
throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, PACKAGE_NAME " is in initial sync and waiting for blocks...");
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/fuzz/process_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ FUZZ_TARGET(process_message, .init = initialize_process_message)
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());

ConnmanTestMsg& connman = *static_cast<ConnmanTestMsg*>(g_setup->m_node.connman.get());
TestChainState& chainstate = *static_cast<TestChainState*>(&g_setup->m_node.chainman->ActiveChainstate());
auto& chainman = static_cast<TestChainstateManager&>(*g_setup->m_node.chainman);
SetMockTime(1610000000); // any time to successfully reset ibd
chainstate.ResetIbd();
chainman.ResetIbd();

LOCK(NetEventsInterface::g_msgproc_mutex);

Expand Down
4 changes: 2 additions & 2 deletions src/test/fuzz/process_messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ FUZZ_TARGET(process_messages, .init = initialize_process_messages)
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());

ConnmanTestMsg& connman = *static_cast<ConnmanTestMsg*>(g_setup->m_node.connman.get());
TestChainState& chainstate = *static_cast<TestChainState*>(&g_setup->m_node.chainman->ActiveChainstate());
auto& chainman = static_cast<TestChainstateManager&>(*g_setup->m_node.chainman);
SetMockTime(1610000000); // any time to successfully reset ibd
chainstate.ResetIbd();
chainman.ResetIbd();

LOCK(NetEventsInterface::g_msgproc_mutex);

Expand Down
9 changes: 4 additions & 5 deletions src/test/net_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -850,11 +850,10 @@ BOOST_AUTO_TEST_CASE(initial_advertise_from_version_message)
const int64_t time{0};
const CNetMsgMaker msg_maker{PROTOCOL_VERSION};

// Force CChainState::IsInitialBlockDownload() to return false.
// Force ChainstateManager::IsInitialBlockDownload() to return false.
// Otherwise PushAddress() isn't called by PeerManager::ProcessMessage().
TestChainState& chainstate =
*static_cast<TestChainState*>(&m_node.chainman->ActiveChainstate());
chainstate.JumpOutOfIbd();
auto& chainman = static_cast<TestChainstateManager&>(*m_node.chainman);
chainman.JumpOutOfIbd();

m_node.peerman->InitializeNode(peer, NODE_NETWORK);

Expand Down Expand Up @@ -904,7 +903,7 @@ BOOST_AUTO_TEST_CASE(initial_advertise_from_version_message)
BOOST_CHECK(sent);

CaptureMessage = CaptureMessageOrig;
chainstate.ResetIbd();
chainman.ResetIbd();
m_node.args->ForceSetArg("-capturemessages", "0");
m_node.args->ForceSetArg("-bind", "");
// PeerManager::ProcessMessage() calls AddTimeData() which changes the internal state
Expand Down
4 changes: 2 additions & 2 deletions src/test/util/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
#include <validation.h>
#include <validationinterface.h>

void TestChainState::ResetIbd()
void TestChainstateManager::ResetIbd()
{
m_cached_finished_ibd = false;
assert(IsInitialBlockDownload());
}

void TestChainState::JumpOutOfIbd()
void TestChainstateManager::JumpOutOfIbd()
{
Assert(IsInitialBlockDownload());
m_cached_finished_ibd = true;
Expand Down
2 changes: 1 addition & 1 deletion src/test/util/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class CValidationInterface;

struct TestChainState : public CChainState {
struct TestChainstateManager : public ChainstateManager {
/** Reset the ibd cache to its initial state */
void ResetIbd();
/** Toggle IsInitialBlockDownload from true to false */
Expand Down
13 changes: 11 additions & 2 deletions src/test/validation_chainstatemanager_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <sync.h>
#include <test/util/chainstate.h>
#include <test/util/setup_common.h>
#include <test/util/validation.h>
#include <timedata.h>
#include <uint256.h>
#include <validation.h>
#include <validationinterface.h>
Expand Down Expand Up @@ -163,6 +165,15 @@ BOOST_AUTO_TEST_CASE(chainstatemanager_rebalance_caches)
c2.InitCoinsDB(
/*cache_size_bytes=*/1 << 23, /*in_memory=*/true, /*should_wipe=*/false);

// Reset IBD state so IsInitialBlockDownload() returns true and causes
// MaybeRebalancesCaches() to prioritize the snapshot chainstate, giving it
// more cache space than the snapshot chainstate. Calling ResetIbd() is
// necessary because m_cached_finished_ibd is already latched to true before
// the test starts due to the test setup. After ResetIbd() is called.
// IsInitialBlockDownload will return true because at this point the active
// chainstate has a null chain tip.
static_cast<TestChainstateManager&>(manager).ResetIbd();

{
LOCK(::cs_main);
c2.InitCoinsCache(1 << 23);
Expand All @@ -171,8 +182,6 @@ BOOST_AUTO_TEST_CASE(chainstatemanager_rebalance_caches)
manager.MaybeRebalanceCaches();
}

// Since both chainstates are considered to be in initial block download,
// the snapshot chainstate should take priority.
BOOST_CHECK_CLOSE(c1.m_coinstip_cache_size_bytes, max_cache * 0.05, 1);
BOOST_CHECK_CLOSE(c1.m_coinsdb_cache_size_bytes, max_cache * 0.05, 1);
BOOST_CHECK_CLOSE(c2.m_coinstip_cache_size_bytes, max_cache * 0.95, 1);
Expand Down
Loading
Loading