mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-25 10:43:19 -03:00
Merge bitcoin/bitcoin#22141: net processing: Remove hash and fValidatedHeaders from QueuedBlock
2f4ad6b7ef
scripted-diff: rename MarkBlockAs functions (John Newbery)2c45f832e8
[net processing] Tidy up MarkBlockAsReceived() (John Newbery)6299350733
[net processing] Add IsBlockRequested() function (John Newbery)4e90d2dd0e
[net processing] Remove QueuedBlock.hash (John Newbery)156a19ee6a
scripted-diff: rename nPeersWithValidatedDownloads (John Newbery)b03de9c753
[net processing] Remove CNodeState.nBlocksInFlightValidHeaders (John Newbery)b4e29f2436
[net processing] Remove QueuedBlock.fValidatedHeaders (John Newbery)85e058b191
[net processing] Remove unnecessary hash arg from MarkBlockAsInFlight() (John Newbery) Pull request description: The QueuedBlock struct contains a `fValidatedHeaders` field that indicates whether we have already validated a header for the requested block. Since headers-first syncing, we only request blocks where the header is already validated, so `fValidatedHeaders` is always true. Remove it and clean up the logic that uses that field. Likewise, QueuedBlock contains a `hash` field that is set to the block hash. Since headers-first syncing, we always have a CBlockIndex, which contains the block hash, so remove the redundant `hash` field. Tidy up the logic and rename functions to better indicate what they're doing. ACKs for top commit: mjdietzx: crACK2f4ad6b7ef
sipa: utACK2f4ad6b7ef
MarcoFalke: review ACK2f4ad6b7ef
📊 Tree-SHA512: 3d31d2bcb4d35d0fdb7c1da624c2878203218026445e8f76c4a2df68cc7183ce0e7d0c47c7c0a3242e55efaca7c9f5532b683cf6ec7c03d23fa83764fdb82fd2
This commit is contained in:
commit
1704bbf226
1 changed files with 69 additions and 57 deletions
|
@ -159,10 +159,10 @@ static constexpr size_t MAX_ADDR_TO_SEND{1000};
|
|||
namespace {
|
||||
/** Blocks that are in flight, and that are in the queue to be downloaded. */
|
||||
struct QueuedBlock {
|
||||
uint256 hash;
|
||||
const CBlockIndex* pindex; //!< Optional.
|
||||
bool fValidatedHeaders; //!< Whether this block has validated headers at the time of request.
|
||||
std::unique_ptr<PartiallyDownloadedBlock> partialBlock; //!< Optional, used for CMPCTBLOCK downloads
|
||||
/** BlockIndex. We must have this since we only request blocks when we've already validated the header. */
|
||||
const CBlockIndex* pindex;
|
||||
/** Optional, used for CMPCTBLOCK downloads */
|
||||
std::unique_ptr<PartiallyDownloadedBlock> partialBlock;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -463,16 +463,20 @@ private:
|
|||
Mutex m_recent_confirmed_transactions_mutex;
|
||||
std::unique_ptr<CRollingBloomFilter> m_recent_confirmed_transactions GUARDED_BY(m_recent_confirmed_transactions_mutex);
|
||||
|
||||
/* Returns a bool indicating whether we requested this block.
|
||||
* Also used if a block was /not/ received and timed out or started with another peer
|
||||
/** Have we requested this block from a peer */
|
||||
bool IsBlockRequested(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
|
||||
|
||||
/** Remove this block from our tracked requested blocks. Called if:
|
||||
* - the block has been recieved from a peer
|
||||
* - the request for the block has timed out
|
||||
*/
|
||||
bool MarkBlockAsReceived(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
|
||||
void RemoveBlockRequest(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
|
||||
|
||||
/* Mark a block as in flight
|
||||
* Returns false, still setting pit, if the block was already in flight from the same peer
|
||||
* pit will only be valid as long as the same cs_main lock is being held
|
||||
*/
|
||||
bool MarkBlockAsInFlight(NodeId nodeid, const uint256& hash, const CBlockIndex* pindex = nullptr, std::list<QueuedBlock>::iterator** pit = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
|
||||
bool BlockRequested(NodeId nodeid, const CBlockIndex* pindex, std::list<QueuedBlock>::iterator** pit = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
|
||||
|
||||
bool TipMayBeStale() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
|
||||
|
||||
|
@ -512,7 +516,7 @@ private:
|
|||
std::list<NodeId> lNodesAnnouncingHeaderAndIDs GUARDED_BY(cs_main);
|
||||
|
||||
/** Number of peers from which we're downloading blocks. */
|
||||
int nPeersWithValidatedDownloads GUARDED_BY(cs_main) = 0;
|
||||
int m_peers_downloading_from GUARDED_BY(cs_main) = 0;
|
||||
|
||||
/** Storage for orphan information */
|
||||
TxOrphanage m_orphanage;
|
||||
|
@ -627,7 +631,6 @@ struct CNodeState {
|
|||
//! When the first entry in vBlocksInFlight started downloading. Don't care when vBlocksInFlight is empty.
|
||||
std::chrono::microseconds m_downloading_since{0us};
|
||||
int nBlocksInFlight{0};
|
||||
int nBlocksInFlightValidHeaders{0};
|
||||
//! Whether we consider this a preferred download peer.
|
||||
bool fPreferredDownload{false};
|
||||
//! Whether this peer wants invs or headers (when possible) for block announcements.
|
||||
|
@ -758,32 +761,43 @@ static void UpdatePreferredDownload(const CNode& node, CNodeState* state) EXCLUS
|
|||
nPreferredDownload += state->fPreferredDownload;
|
||||
}
|
||||
|
||||
bool PeerManagerImpl::MarkBlockAsReceived(const uint256& hash)
|
||||
bool PeerManagerImpl::IsBlockRequested(const uint256& hash)
|
||||
{
|
||||
std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find(hash);
|
||||
if (itInFlight != mapBlocksInFlight.end()) {
|
||||
CNodeState *state = State(itInFlight->second.first);
|
||||
assert(state != nullptr);
|
||||
state->nBlocksInFlightValidHeaders -= itInFlight->second.second->fValidatedHeaders;
|
||||
if (state->nBlocksInFlightValidHeaders == 0 && itInFlight->second.second->fValidatedHeaders) {
|
||||
// Last validated block on the queue was received.
|
||||
nPeersWithValidatedDownloads--;
|
||||
}
|
||||
if (state->vBlocksInFlight.begin() == itInFlight->second.second) {
|
||||
// First block on the queue was received, update the start download time for the next one
|
||||
state->m_downloading_since = std::max(state->m_downloading_since, GetTime<std::chrono::microseconds>());
|
||||
}
|
||||
state->vBlocksInFlight.erase(itInFlight->second.second);
|
||||
state->nBlocksInFlight--;
|
||||
state->m_stalling_since = 0us;
|
||||
mapBlocksInFlight.erase(itInFlight);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return mapBlocksInFlight.find(hash) != mapBlocksInFlight.end();
|
||||
}
|
||||
|
||||
bool PeerManagerImpl::MarkBlockAsInFlight(NodeId nodeid, const uint256& hash, const CBlockIndex* pindex, std::list<QueuedBlock>::iterator** pit)
|
||||
void PeerManagerImpl::RemoveBlockRequest(const uint256& hash)
|
||||
{
|
||||
auto it = mapBlocksInFlight.find(hash);
|
||||
if (it == mapBlocksInFlight.end()) {
|
||||
// Block was not requested
|
||||
return;
|
||||
}
|
||||
|
||||
auto [node_id, list_it] = it->second;
|
||||
CNodeState *state = State(node_id);
|
||||
assert(state != nullptr);
|
||||
|
||||
if (state->vBlocksInFlight.begin() == list_it) {
|
||||
// First block on the queue was received, update the start download time for the next one
|
||||
state->m_downloading_since = std::max(state->m_downloading_since, GetTime<std::chrono::microseconds>());
|
||||
}
|
||||
state->vBlocksInFlight.erase(list_it);
|
||||
|
||||
state->nBlocksInFlight--;
|
||||
if (state->nBlocksInFlight == 0) {
|
||||
// Last validated block on the queue was received.
|
||||
m_peers_downloading_from--;
|
||||
}
|
||||
state->m_stalling_since = 0us;
|
||||
mapBlocksInFlight.erase(it);
|
||||
}
|
||||
|
||||
bool PeerManagerImpl::BlockRequested(NodeId nodeid, const CBlockIndex* pindex, std::list<QueuedBlock>::iterator** pit)
|
||||
{
|
||||
assert(pindex);
|
||||
const uint256& hash{pindex->GetBlockHash()};
|
||||
|
||||
CNodeState *state = State(nodeid);
|
||||
assert(state != nullptr);
|
||||
|
||||
|
@ -797,18 +811,15 @@ bool PeerManagerImpl::MarkBlockAsInFlight(NodeId nodeid, const uint256& hash, co
|
|||
}
|
||||
|
||||
// Make sure it's not listed somewhere already.
|
||||
MarkBlockAsReceived(hash);
|
||||
RemoveBlockRequest(hash);
|
||||
|
||||
std::list<QueuedBlock>::iterator it = state->vBlocksInFlight.insert(state->vBlocksInFlight.end(),
|
||||
{hash, pindex, pindex != nullptr, std::unique_ptr<PartiallyDownloadedBlock>(pit ? new PartiallyDownloadedBlock(&m_mempool) : nullptr)});
|
||||
{pindex, std::unique_ptr<PartiallyDownloadedBlock>(pit ? new PartiallyDownloadedBlock(&m_mempool) : nullptr)});
|
||||
state->nBlocksInFlight++;
|
||||
state->nBlocksInFlightValidHeaders += it->fValidatedHeaders;
|
||||
if (state->nBlocksInFlight == 1) {
|
||||
// We're starting a block download (batch) from this peer.
|
||||
state->m_downloading_since = GetTime<std::chrono::microseconds>();
|
||||
}
|
||||
if (state->nBlocksInFlightValidHeaders == 1 && pindex != nullptr) {
|
||||
nPeersWithValidatedDownloads++;
|
||||
m_peers_downloading_from++;
|
||||
}
|
||||
itInFlight = mapBlocksInFlight.insert(std::make_pair(hash, std::make_pair(nodeid, it))).first;
|
||||
if (pit)
|
||||
|
@ -978,7 +989,7 @@ void PeerManagerImpl::FindNextBlocksToDownload(NodeId nodeid, unsigned int count
|
|||
if (pindex->nStatus & BLOCK_HAVE_DATA || m_chainman.ActiveChain().Contains(pindex)) {
|
||||
if (pindex->HaveTxsDownloaded())
|
||||
state->pindexLastCommonBlock = pindex;
|
||||
} else if (mapBlocksInFlight.count(pindex->GetBlockHash()) == 0) {
|
||||
} else if (!IsBlockRequested(pindex->GetBlockHash())) {
|
||||
// The block is not already downloaded, and not yet in flight.
|
||||
if (pindex->nHeight > nWindowEnd) {
|
||||
// We reached the end of the window.
|
||||
|
@ -1129,13 +1140,13 @@ void PeerManagerImpl::FinalizeNode(const CNode& node)
|
|||
nSyncStarted--;
|
||||
|
||||
for (const QueuedBlock& entry : state->vBlocksInFlight) {
|
||||
mapBlocksInFlight.erase(entry.hash);
|
||||
mapBlocksInFlight.erase(entry.pindex->GetBlockHash());
|
||||
}
|
||||
WITH_LOCK(g_cs_orphans, m_orphanage.EraseForPeer(nodeid));
|
||||
m_txrequest.DisconnectedPeer(nodeid);
|
||||
nPreferredDownload -= state->fPreferredDownload;
|
||||
nPeersWithValidatedDownloads -= (state->nBlocksInFlightValidHeaders != 0);
|
||||
assert(nPeersWithValidatedDownloads >= 0);
|
||||
m_peers_downloading_from -= (state->nBlocksInFlight != 0);
|
||||
assert(m_peers_downloading_from >= 0);
|
||||
m_outbound_peers_with_protect_from_disconnect -= state->m_chain_sync.m_protect;
|
||||
assert(m_outbound_peers_with_protect_from_disconnect >= 0);
|
||||
m_wtxid_relay_peers -= state->m_wtxid_relay;
|
||||
|
@ -1147,7 +1158,7 @@ void PeerManagerImpl::FinalizeNode(const CNode& node)
|
|||
// Do a consistency check after the last peer is removed.
|
||||
assert(mapBlocksInFlight.empty());
|
||||
assert(nPreferredDownload == 0);
|
||||
assert(nPeersWithValidatedDownloads == 0);
|
||||
assert(m_peers_downloading_from == 0);
|
||||
assert(m_outbound_peers_with_protect_from_disconnect == 0);
|
||||
assert(m_wtxid_relay_peers == 0);
|
||||
assert(m_txrequest.Size() == 0);
|
||||
|
@ -2056,7 +2067,7 @@ void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, const Peer& peer,
|
|||
// Calculate all the blocks we'd need to switch to pindexLast, up to a limit.
|
||||
while (pindexWalk && !m_chainman.ActiveChain().Contains(pindexWalk) && vToFetch.size() <= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
|
||||
if (!(pindexWalk->nStatus & BLOCK_HAVE_DATA) &&
|
||||
!mapBlocksInFlight.count(pindexWalk->GetBlockHash()) &&
|
||||
!IsBlockRequested(pindexWalk->GetBlockHash()) &&
|
||||
(!IsWitnessEnabled(pindexWalk->pprev, m_chainparams.GetConsensus()) || State(pfrom.GetId())->fHaveWitness)) {
|
||||
// We don't have this block, and it's not yet in flight.
|
||||
vToFetch.push_back(pindexWalk);
|
||||
|
@ -2081,7 +2092,7 @@ void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, const Peer& peer,
|
|||
}
|
||||
uint32_t nFetchFlags = GetFetchFlags(pfrom);
|
||||
vGetData.push_back(CInv(MSG_BLOCK | nFetchFlags, pindex->GetBlockHash()));
|
||||
MarkBlockAsInFlight(pfrom.GetId(), pindex->GetBlockHash(), pindex);
|
||||
BlockRequested(pfrom.GetId(), pindex);
|
||||
LogPrint(BCLog::NET, "Requesting block %s from peer=%d\n",
|
||||
pindex->GetBlockHash().ToString(), pfrom.GetId());
|
||||
}
|
||||
|
@ -2827,7 +2838,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 && !mapBlocksInFlight.count(inv.hash)) {
|
||||
if (!fAlreadyHave && !fImporting && !fReindex && !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's probably for a re-org. The final block hash
|
||||
|
@ -3384,7 +3395,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
|
|||
if ((!fAlreadyInFlight && nodestate->nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) ||
|
||||
(fAlreadyInFlight && blockInFlightIt->second.first == pfrom.GetId())) {
|
||||
std::list<QueuedBlock>::iterator* queuedBlockIt = nullptr;
|
||||
if (!MarkBlockAsInFlight(pfrom.GetId(), pindex->GetBlockHash(), pindex, &queuedBlockIt)) {
|
||||
if (!BlockRequested(pfrom.GetId(), pindex, &queuedBlockIt)) {
|
||||
if (!(*queuedBlockIt)->partialBlock)
|
||||
(*queuedBlockIt)->partialBlock.reset(new PartiallyDownloadedBlock(&m_mempool));
|
||||
else {
|
||||
|
@ -3397,7 +3408,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
|
|||
PartiallyDownloadedBlock& partialBlock = *(*queuedBlockIt)->partialBlock;
|
||||
ReadStatus status = partialBlock.InitData(cmpctblock, vExtraTxnForCompact);
|
||||
if (status == READ_STATUS_INVALID) {
|
||||
MarkBlockAsReceived(pindex->GetBlockHash()); // Reset in-flight state in case Misbehaving does not result in a disconnect
|
||||
RemoveBlockRequest(pindex->GetBlockHash()); // Reset in-flight state in case Misbehaving does not result in a disconnect
|
||||
Misbehaving(pfrom.GetId(), 100, "invalid compact block");
|
||||
return;
|
||||
} else if (status == READ_STATUS_FAILED) {
|
||||
|
@ -3492,7 +3503,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
|
|||
// process from some other peer. We do this after calling
|
||||
// ProcessNewBlock so that a malleated cmpctblock announcement
|
||||
// can't be used to interfere with block relay.
|
||||
MarkBlockAsReceived(pblock->GetHash());
|
||||
RemoveBlockRequest(pblock->GetHash());
|
||||
}
|
||||
}
|
||||
return;
|
||||
|
@ -3524,7 +3535,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
|
|||
PartiallyDownloadedBlock& partialBlock = *it->second.second->partialBlock;
|
||||
ReadStatus status = partialBlock.FillBlock(*pblock, resp.txn);
|
||||
if (status == READ_STATUS_INVALID) {
|
||||
MarkBlockAsReceived(resp.blockhash); // Reset in-flight state in case Misbehaving does not result in a disconnect
|
||||
RemoveBlockRequest(resp.blockhash); // Reset in-flight state in case Misbehaving does not result in a disconnect
|
||||
Misbehaving(pfrom.GetId(), 100, "invalid compact block/non-matching block transactions");
|
||||
return;
|
||||
} else if (status == READ_STATUS_FAILED) {
|
||||
|
@ -3550,7 +3561,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
|
|||
// though the block was successfully read, and rely on the
|
||||
// handling in ProcessNewBlock to ensure the block index is
|
||||
// updated, etc.
|
||||
MarkBlockAsReceived(resp.blockhash); // it is now an empty pointer
|
||||
RemoveBlockRequest(resp.blockhash); // it is now an empty pointer
|
||||
fBlockRead = true;
|
||||
// mapBlockSource is used for potentially punishing peers and
|
||||
// updating which peers send us compact blocks, so the race
|
||||
|
@ -3615,9 +3626,10 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
|
|||
const uint256 hash(pblock->GetHash());
|
||||
{
|
||||
LOCK(cs_main);
|
||||
// Also always process if we requested the block explicitly, as we may
|
||||
// need it even though it is not a candidate for a new best tip.
|
||||
forceProcessing |= MarkBlockAsReceived(hash);
|
||||
// Always process the block if we requested it, since we may
|
||||
// need it even when it's not a candidate for a new best tip.
|
||||
forceProcessing = IsBlockRequested(hash);
|
||||
RemoveBlockRequest(hash);
|
||||
// mapBlockSource is only used for punishing peers and setting
|
||||
// which peers send us compact blocks, so the race between here and
|
||||
// cs_main in ProcessNewBlock is fine.
|
||||
|
@ -4712,9 +4724,9 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
|
|||
// to unreasonably increase our timeout.
|
||||
if (state.vBlocksInFlight.size() > 0) {
|
||||
QueuedBlock &queuedBlock = state.vBlocksInFlight.front();
|
||||
int nOtherPeersWithValidatedDownloads = nPeersWithValidatedDownloads - (state.nBlocksInFlightValidHeaders > 0);
|
||||
int nOtherPeersWithValidatedDownloads = m_peers_downloading_from - 1;
|
||||
if (current_time > state.m_downloading_since + std::chrono::seconds{consensusParams.nPowTargetSpacing} * (BLOCK_DOWNLOAD_TIMEOUT_BASE + BLOCK_DOWNLOAD_TIMEOUT_PER_PEER * nOtherPeersWithValidatedDownloads)) {
|
||||
LogPrintf("Timeout downloading block %s from peer=%d, disconnecting\n", queuedBlock.hash.ToString(), pto->GetId());
|
||||
LogPrintf("Timeout downloading block %s from peer=%d, disconnecting\n", queuedBlock.pindex->GetBlockHash().ToString(), pto->GetId());
|
||||
pto->fDisconnect = true;
|
||||
return true;
|
||||
}
|
||||
|
@ -4767,7 +4779,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
|
|||
for (const CBlockIndex *pindex : vToDownload) {
|
||||
uint32_t nFetchFlags = GetFetchFlags(*pto);
|
||||
vGetData.push_back(CInv(MSG_BLOCK | nFetchFlags, pindex->GetBlockHash()));
|
||||
MarkBlockAsInFlight(pto->GetId(), pindex->GetBlockHash(), pindex);
|
||||
BlockRequested(pto->GetId(), pindex);
|
||||
LogPrint(BCLog::NET, "Requesting block %s (%d) peer=%d\n", pindex->GetBlockHash().ToString(),
|
||||
pindex->nHeight, pto->GetId());
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue