I did not want to bloat this PR with changes to the interface of ProcessMessages() and SendMessages(). They take a raw pointer. Since they do not take ownership and the object is going to be alive for the duration of the call and there will always be an object (will never pass nullptr), then those functions can take CNode&.
Here is a patch to do that on top of this PR. Could be a followup. I do not want to bloat this PR with this unless reviewers want it.
<details>
<summary>[patch] Change ProcessMessages() and SendMessage() to take CNode&</summary>
It is a mechanical change
11 files changed, 93 insertions(+), 93 deletions(-)
diff --git i/src/net.cpp w/src/net.cpp
index 81ee1eec16..4189cee0df 100644
--- i/src/net.cpp
+++ w/src/net.cpp
@@ -3051,18 +3051,18 @@ void CConnman::ThreadMessageHandler()
for (auto& pnode : nodes) {
if (pnode->fDisconnect)
continue;
// Receive messages
- bool fMoreNodeWork = m_msgproc->ProcessMessages(pnode.get(), flagInterruptMsgProc);
+ bool fMoreNodeWork = m_msgproc->ProcessMessages(*pnode, flagInterruptMsgProc);
fMoreWork |= (fMoreNodeWork && !pnode->fPauseSend);
if (flagInterruptMsgProc)
return;
// Send messages
- m_msgproc->SendMessages(pnode.get());
+ m_msgproc->SendMessages(*pnode);
if (flagInterruptMsgProc)
return;
}
}
diff --git i/src/net.h w/src/net.h
index 051ff50f3b..1e93dfcaa5 100644
--- i/src/net.h
+++ w/src/net.h
@@ -1002,27 +1002,27 @@ public:
* Callback to determine whether the given set of service flags are sufficient
* for a peer to be "relevant".
*/
virtual bool HasAllDesirableServiceFlags(ServiceFlags services) const = 0;
/**
- * Process protocol messages received from a given node
- *
- * [@param](/bitcoin-bitcoin/contributor/param/)[in] pnode The node which we have received messages from.
- * [@param](/bitcoin-bitcoin/contributor/param/)[in] interrupt Interrupt condition for processing threads
- * [@return](/bitcoin-bitcoin/contributor/return/) True if there is more work to be done
- */
- virtual bool ProcessMessages(CNode* pnode, std::atomic<bool>& interrupt) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex) = 0;
+ * Process protocol messages received from a given node
+ *
+ * [@param](/bitcoin-bitcoin/contributor/param/)[in,out] from The node which we have received messages from.
+ * [@param](/bitcoin-bitcoin/contributor/param/)[in] interrupt Interrupt condition for processing threads.
+ * [@return](/bitcoin-bitcoin/contributor/return/) True if there is more work to be done.
+ */
+ virtual bool ProcessMessages(CNode& from, std::atomic<bool>& interrupt) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex) = 0;
/**
- * Send queued protocol messages to a given node.
- *
- * [@param](/bitcoin-bitcoin/contributor/param/)[in] pnode The node which we are sending messages to.
- * [@return](/bitcoin-bitcoin/contributor/return/) True if there is more work to be done
- */
- virtual bool SendMessages(CNode* pnode) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex) = 0;
+ * Send queued protocol messages to a given node.
+ *
+ * [@param](/bitcoin-bitcoin/contributor/param/)[in,out] to The node which we are sending messages to.
+ * [@return](/bitcoin-bitcoin/contributor/return/) True if there is more work to be done.
+ */
+ virtual bool SendMessages(CNode& to) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex) = 0;
protected:
/**
* Protected destructor so that instances can only be deleted by derived classes.
* If that restriction is no longer desired, this should be made public and virtual.
diff --git i/src/net_processing.cpp w/src/net_processing.cpp
index 78e97b6684..7153226635 100644
--- i/src/net_processing.cpp
+++ w/src/net_processing.cpp
@@ -519,15 +519,15 @@ public:
EXCLUSIVE_LOCKS_REQUIRED(!m_most_recent_block_mutex);
/** Implement NetEventsInterface */
void InitializeNode(const CNode& node, ServiceFlags our_services) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, !m_tx_download_mutex);
void FinalizeNode(const CNode& node) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, !m_headers_presync_mutex, !m_tx_download_mutex);
bool HasAllDesirableServiceFlags(ServiceFlags services) const override;
- bool ProcessMessages(CNode* pfrom, std::atomic<bool>& interrupt) override
+ bool ProcessMessages(CNode& from, std::atomic<bool>& interrupt) override
EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, !m_most_recent_block_mutex, !m_headers_presync_mutex, g_msgproc_mutex, !m_tx_download_mutex);
- bool SendMessages(CNode* pto) override
+ bool SendMessages(CNode& to) override
EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, !m_most_recent_block_mutex, g_msgproc_mutex, !m_tx_download_mutex);
/** Implement PeerManager */
void StartScheduledTasks(CScheduler& scheduler) override;
void CheckForStaleTipAndEvictPeers() override;
std::optional<std::string> FetchBlock(NodeId peer_id, const CBlockIndex& block_index) override
@@ -4989,72 +4989,72 @@ bool PeerManagerImpl::MaybeDiscourageAndDisconnect(CNode& pnode, Peer& peer)
LogDebug(BCLog::NET, "Disconnecting and discouraging peer %d!\n", peer.m_id);
if (m_banman) m_banman->Discourage(pnode.addr);
m_connman.DisconnectNode(pnode.addr);
return true;
}
-bool PeerManagerImpl::ProcessMessages(CNode* pfrom, std::atomic<bool>& interruptMsgProc)
+bool PeerManagerImpl::ProcessMessages(CNode& from, std::atomic<bool>& interruptMsgProc)
{
AssertLockNotHeld(m_tx_download_mutex);
AssertLockHeld(g_msgproc_mutex);
- PeerRef peer = GetPeerRef(pfrom->GetId());
+ PeerRef peer = GetPeerRef(from.GetId());
if (peer == nullptr) return false;
// For outbound connections, ensure that the initial VERSION message
// has been sent first before processing any incoming messages
- if (!pfrom->IsInboundConn() && !peer->m_outbound_version_message_sent) return false;
+ if (!from.IsInboundConn() && !peer->m_outbound_version_message_sent) return false;
{
LOCK(peer->m_getdata_requests_mutex);
if (!peer->m_getdata_requests.empty()) {
- ProcessGetData(*pfrom, *peer, interruptMsgProc);
+ ProcessGetData(from, *peer, interruptMsgProc);
}
}
const bool processed_orphan = ProcessOrphanTx(*peer);
- if (pfrom->fDisconnect)
+ if (from.fDisconnect)
return false;
if (processed_orphan) return true;
// this maintains the order of responses
// and prevents m_getdata_requests to grow unbounded
{
LOCK(peer->m_getdata_requests_mutex);
if (!peer->m_getdata_requests.empty()) return true;
}
// Don't bother if send buffer is too full to respond anyway
- if (pfrom->fPauseSend) return false;
+ if (from.fPauseSend) return false;
- auto poll_result{pfrom->PollMessage()};
+ auto poll_result{from.PollMessage()};
if (!poll_result) {
// No message to process
return false;
}
CNetMessage& msg{poll_result->first};
bool fMoreWork = poll_result->second;
TRACEPOINT(net, inbound_message,
- pfrom->GetId(),
- pfrom->m_addr_name.c_str(),
- pfrom->ConnectionTypeAsString().c_str(),
+ from.GetId(),
+ from.m_addr_name.c_str(),
+ from.ConnectionTypeAsString().c_str(),
msg.m_type.c_str(),
msg.m_recv.size(),
msg.m_recv.data()
);
if (m_opts.capture_messages) {
- CaptureMessage(pfrom->addr, msg.m_type, MakeUCharSpan(msg.m_recv), /*is_incoming=*/true);
+ CaptureMessage(from.addr, msg.m_type, MakeUCharSpan(msg.m_recv), /*is_incoming=*/true);
}
try {
- ProcessMessage(*pfrom, msg.m_type, msg.m_recv, msg.m_time, interruptMsgProc);
+ ProcessMessage(from, msg.m_type, msg.m_recv, msg.m_time, interruptMsgProc);
if (interruptMsgProc) return false;
{
LOCK(peer->m_getdata_requests_mutex);
if (!peer->m_getdata_requests.empty()) fMoreWork = true;
}
// Does this peer has an orphan ready to reconsider?
@@ -5481,69 +5481,69 @@ bool PeerManagerImpl::SetupAddressRelay(const CNode& node, Peer& peer)
peer.m_addr_known = std::make_unique<CRollingBloomFilter>(5000, 0.001);
}
return true;
}
-bool PeerManagerImpl::SendMessages(CNode* pto)
+bool PeerManagerImpl::SendMessages(CNode& to)
{
AssertLockNotHeld(m_tx_download_mutex);
AssertLockHeld(g_msgproc_mutex);
- PeerRef peer = GetPeerRef(pto->GetId());
+ PeerRef peer = GetPeerRef(to.GetId());
if (!peer) return false;
const Consensus::Params& consensusParams = m_chainparams.GetConsensus();
// We must call MaybeDiscourageAndDisconnect first, to ensure that we'll
// disconnect misbehaving peers even before the version handshake is complete.
- if (MaybeDiscourageAndDisconnect(*pto, *peer)) return true;
+ if (MaybeDiscourageAndDisconnect(to, *peer)) return true;
// Initiate version handshake for outbound connections
- if (!pto->IsInboundConn() && !peer->m_outbound_version_message_sent) {
- PushNodeVersion(*pto, *peer);
+ if (!to.IsInboundConn() && !peer->m_outbound_version_message_sent) {
+ PushNodeVersion(to, *peer);
peer->m_outbound_version_message_sent = true;
}
// Don't send anything until the version handshake is complete
- if (!pto->fSuccessfullyConnected || pto->fDisconnect)
+ if (!to.fSuccessfullyConnected || to.fDisconnect)
return true;
const auto current_time{GetTime<std::chrono::microseconds>()};
- if (pto->IsAddrFetchConn() && current_time - pto->m_connected > 10 * AVG_ADDRESS_BROADCAST_INTERVAL) {
- LogDebug(BCLog::NET, "addrfetch connection timeout, %s\n", pto->DisconnectMsg(fLogIPs));
- pto->fDisconnect = true;
+ if (to.IsAddrFetchConn() && current_time - to.m_connected > 10 * AVG_ADDRESS_BROADCAST_INTERVAL) {
+ LogDebug(BCLog::NET, "addrfetch connection timeout, %s\n", to.DisconnectMsg(fLogIPs));
+ to.fDisconnect = true;
return true;
}
- MaybeSendPing(*pto, *peer, current_time);
+ MaybeSendPing(to, *peer, current_time);
// MaybeSendPing may have marked peer for disconnection
- if (pto->fDisconnect) return true;
+ if (to.fDisconnect) return true;
- MaybeSendAddr(*pto, *peer, current_time);
+ MaybeSendAddr(to, *peer, current_time);
- MaybeSendSendHeaders(*pto, *peer);
+ MaybeSendSendHeaders(to, *peer);
{
LOCK(cs_main);
- CNodeState &state = *State(pto->GetId());
+ CNodeState &state = *State(to.GetId());
// Start block sync
if (m_chainman.m_best_header == nullptr) {
m_chainman.m_best_header = m_chainman.ActiveChain().Tip();
}
// Determine whether we might try initial headers sync or parallel
// block download from this peer -- this mostly affects behavior while
// in IBD (once out of IBD, we sync from all peers).
bool sync_blocks_and_headers_from_peer = false;
if (state.fPreferredDownload) {
sync_blocks_and_headers_from_peer = true;
- } else if (CanServeBlocks(*peer) && !pto->IsAddrFetchConn()) {
+ } else if (CanServeBlocks(*peer) && !to.IsAddrFetchConn()) {
// Typically this is an inbound peer. If we don't have any outbound
// peers, or if we aren't downloading any blocks from such peers,
// then allow block downloads from this peer, too.
// We prefer downloading blocks from outbound peers to avoid
// putting undue load on (say) some home user who is just making
// outbound connections to the network, but if our only source of
@@ -5565,14 +5565,14 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
is up-to-date. With a non-empty response, we can initialise
the peer's known best block. This wouldn't be possible
if we requested starting at m_chainman.m_best_header and
got back an empty response. */
if (pindexStart->pprev)
pindexStart = pindexStart->pprev;
- if (MaybeSendGetHeaders(*pto, GetLocator(pindexStart), *peer)) {
- LogDebug(BCLog::NET, "initial getheaders (%d) to peer=%d (startheight:%d)\n", pindexStart->nHeight, pto->GetId(), peer->m_starting_height);
+ if (MaybeSendGetHeaders(to, GetLocator(pindexStart), *peer)) {
+ LogDebug(BCLog::NET, "initial getheaders (%d) to peer=%d (startheight:%d)\n", pindexStart->nHeight, to.GetId(), peer->m_starting_height);
state.fSyncStarted = true;
peer->m_headers_sync_timeout = current_time + HEADERS_DOWNLOAD_TIMEOUT_BASE +
(
// Convert HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER to microseconds before scaling
// to maintain precision
@@ -5598,13 +5598,13 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
LOCK(peer->m_block_inv_mutex);
std::vector<CBlock> vHeaders;
bool fRevertToInv = ((!peer->m_prefers_headers &&
(!state.m_requested_hb_cmpctblocks || peer->m_blocks_for_headers_relay.size() > 1)) ||
peer->m_blocks_for_headers_relay.size() > MAX_BLOCKS_TO_ANNOUNCE);
const CBlockIndex *pBestIndex = nullptr; // last header queued for delivery
- ProcessBlockAvailability(pto->GetId()); // ensure pindexBestKnownBlock is up-to-date
+ ProcessBlockAvailability(to.GetId()); // ensure pindexBestKnownBlock is up-to-date
if (!fRevertToInv) {
bool fFoundStartingHeader = false;
// Try to find first header that our peer doesn't have, and
// then send all headers past that one. If we come across any
// headers that aren't on m_chainman.ActiveChain(), give up.
@@ -5652,42 +5652,42 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
}
if (!fRevertToInv && !vHeaders.empty()) {
if (vHeaders.size() == 1 && state.m_requested_hb_cmpctblocks) {
// We only send up to 1 block as header-and-ids, as otherwise
// probably means we're doing an initial-ish-sync or they're slow
LogDebug(BCLog::NET, "%s sending header-and-ids %s to peer=%d\n", __func__,
- vHeaders.front().GetHash().ToString(), pto->GetId());
+ vHeaders.front().GetHash().ToString(), to.GetId());
std::optional<CSerializedNetMsg> cached_cmpctblock_msg;
{
LOCK(m_most_recent_block_mutex);
if (m_most_recent_block_hash == pBestIndex->GetBlockHash()) {
cached_cmpctblock_msg = NetMsg::Make(NetMsgType::CMPCTBLOCK, *m_most_recent_compact_block);
}
}
if (cached_cmpctblock_msg.has_value()) {
- PushMessage(*pto, std::move(cached_cmpctblock_msg.value()));
+ PushMessage(to, std::move(cached_cmpctblock_msg.value()));
} else {
CBlock block;
const bool ret{m_chainman.m_blockman.ReadBlock(block, *pBestIndex)};
assert(ret);
CBlockHeaderAndShortTxIDs cmpctblock{block, m_rng.rand64()};
- MakeAndPushMessage(*pto, NetMsgType::CMPCTBLOCK, cmpctblock);
+ MakeAndPushMessage(to, NetMsgType::CMPCTBLOCK, cmpctblock);
}
state.pindexBestHeaderSent = pBestIndex;
} else if (peer->m_prefers_headers) {
if (vHeaders.size() > 1) {
LogDebug(BCLog::NET, "%s: %u headers, range (%s, %s), to peer=%d\n", __func__,
vHeaders.size(),
vHeaders.front().GetHash().ToString(),
- vHeaders.back().GetHash().ToString(), pto->GetId());
+ vHeaders.back().GetHash().ToString(), to.GetId());
} else {
LogDebug(BCLog::NET, "%s: sending header %s to peer=%d\n", __func__,
- vHeaders.front().GetHash().ToString(), pto->GetId());
+ vHeaders.front().GetHash().ToString(), to.GetId());
}
- MakeAndPushMessage(*pto, NetMsgType::HEADERS, TX_WITH_WITNESS(vHeaders));
+ MakeAndPushMessage(to, NetMsgType::HEADERS, TX_WITH_WITNESS(vHeaders));
state.pindexBestHeaderSent = pBestIndex;
} else
fRevertToInv = true;
}
if (fRevertToInv) {
// If falling back to using an inv, just try to inv the tip.
@@ -5707,13 +5707,13 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
}
// If the peer's chain has this block, don't inv it back.
if (!PeerHasHeader(&state, pindex)) {
peer->m_blocks_for_inv_relay.push_back(hashToAnnounce);
LogDebug(BCLog::NET, "%s: sending inv peer=%d hash=%s\n", __func__,
- pto->GetId(), hashToAnnounce.ToString());
+ to.GetId(), hashToAnnounce.ToString());
}
}
}
peer->m_blocks_for_headers_relay.clear();
}
@@ -5726,26 +5726,26 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
vInv.reserve(std::max<size_t>(peer->m_blocks_for_inv_relay.size(), INVENTORY_BROADCAST_TARGET));
// Add blocks
for (const uint256& hash : peer->m_blocks_for_inv_relay) {
vInv.emplace_back(MSG_BLOCK, hash);
if (vInv.size() == MAX_INV_SZ) {
- MakeAndPushMessage(*pto, NetMsgType::INV, vInv);
+ MakeAndPushMessage(to, NetMsgType::INV, vInv);
vInv.clear();
}
}
peer->m_blocks_for_inv_relay.clear();
}
if (auto tx_relay = peer->GetTxRelay(); tx_relay != nullptr) {
LOCK(tx_relay->m_tx_inventory_mutex);
// Check whether periodic sends should happen
- bool fSendTrickle = pto->HasPermission(NetPermissionFlags::NoBan);
+ bool fSendTrickle = to.HasPermission(NetPermissionFlags::NoBan);
if (tx_relay->m_next_inv_send_time < current_time) {
fSendTrickle = true;
- if (pto->IsInboundConn()) {
+ if (to.IsInboundConn()) {
tx_relay->m_next_inv_send_time = NextInvToInbounds(current_time, INBOUND_INVENTORY_BROADCAST_INTERVAL);
} else {
tx_relay->m_next_inv_send_time = current_time + m_rng.rand_exp_duration(OUTBOUND_INVENTORY_BROADCAST_INTERVAL);
}
}
@@ -5779,13 +5779,13 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
if (tx_relay->m_bloom_filter) {
if (!tx_relay->m_bloom_filter->IsRelevantAndUpdate(*txinfo.tx)) continue;
}
tx_relay->m_tx_inventory_known_filter.insert(inv.hash);
vInv.push_back(inv);
if (vInv.size() == MAX_INV_SZ) {
- MakeAndPushMessage(*pto, NetMsgType::INV, vInv);
+ MakeAndPushMessage(to, NetMsgType::INV, vInv);
vInv.clear();
}
}
}
// Determine transactions to relay
@@ -5831,34 +5831,34 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
}
if (tx_relay->m_bloom_filter && !tx_relay->m_bloom_filter->IsRelevantAndUpdate(*txinfo.tx)) continue;
// Send
vInv.push_back(inv);
nRelayedTransactions++;
if (vInv.size() == MAX_INV_SZ) {
- MakeAndPushMessage(*pto, NetMsgType::INV, vInv);
+ MakeAndPushMessage(to, NetMsgType::INV, vInv);
vInv.clear();
}
tx_relay->m_tx_inventory_known_filter.insert(hash);
}
// Ensure we'll respond to GETDATA requests for anything we've just announced
LOCK(m_mempool.cs);
tx_relay->m_last_inv_sequence = m_mempool.GetSequence();
}
}
if (!vInv.empty())
- MakeAndPushMessage(*pto, NetMsgType::INV, vInv);
+ MakeAndPushMessage(to, NetMsgType::INV, vInv);
// Detect whether we're stalling
auto stalling_timeout = m_block_stalling_timeout.load();
if (state.m_stalling_since.count() && state.m_stalling_since < current_time - stalling_timeout) {
// Stalling only triggers when the block download window cannot move. During normal steady state,
// the download window should be much larger than the to-be-downloaded set of blocks, so disconnection
// should only happen during initial block download.
- LogInfo("Peer is stalling block download, %s\n", pto->DisconnectMsg(fLogIPs));
- pto->fDisconnect = true;
+ LogInfo("Peer is stalling block download, %s\n", to.DisconnectMsg(fLogIPs));
+ to.fDisconnect = true;
// Increase timeout for the next peer so that we don't disconnect multiple peers if our own
// bandwidth is insufficient.
const auto new_timeout = std::min(2 * stalling_timeout, BLOCK_STALLING_TIMEOUT_MAX);
if (stalling_timeout != new_timeout && m_block_stalling_timeout.compare_exchange_strong(stalling_timeout, new_timeout)) {
LogDebug(BCLog::NET, "Increased stalling timeout temporarily to %d seconds\n", count_seconds(new_timeout));
}
@@ -5870,14 +5870,14 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
// being saturated. We only count validated in-flight blocks so peers can't advertise non-existing block hashes
// to unreasonably increase our timeout.
if (state.vBlocksInFlight.size() > 0) {
QueuedBlock &queuedBlock = state.vBlocksInFlight.front();
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)) {
- LogInfo("Timeout downloading block %s, %s\n", queuedBlock.pindex->GetBlockHash().ToString(), pto->DisconnectMsg(fLogIPs));
- pto->fDisconnect = true;
+ LogInfo("Timeout downloading block %s, %s\n", queuedBlock.pindex->GetBlockHash().ToString(), to.DisconnectMsg(fLogIPs));
+ to.fDisconnect = true;
return true;
}
}
// Check for headers sync timeouts
if (state.fSyncStarted && peer->m_headers_sync_timeout < std::chrono::microseconds::max()) {
// Detect whether this is a stalling initial-headers-sync peer
@@ -5885,18 +5885,18 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
if (current_time > peer->m_headers_sync_timeout && nSyncStarted == 1 && (m_num_preferred_download_peers - state.fPreferredDownload >= 1)) {
// Disconnect a peer (without NetPermissionFlags::NoBan permission) if it is our only sync peer,
// and we have others we could be using instead.
// Note: If all our peers are inbound, then we won't
// disconnect our sync peer for stalling; we have bigger
// problems if we can't get any outbound peers.
- if (!pto->HasPermission(NetPermissionFlags::NoBan)) {
- LogInfo("Timeout downloading headers, %s\n", pto->DisconnectMsg(fLogIPs));
- pto->fDisconnect = true;
+ if (!to.HasPermission(NetPermissionFlags::NoBan)) {
+ LogInfo("Timeout downloading headers, %s\n", to.DisconnectMsg(fLogIPs));
+ to.fDisconnect = true;
return true;
} else {
- LogInfo("Timeout downloading headers from noban peer, not %s\n", pto->DisconnectMsg(fLogIPs));
+ LogInfo("Timeout downloading headers from noban peer, not %s\n", to.DisconnectMsg(fLogIPs));
// Reset the headers sync state so that we have a
// chance to try downloading from a different peer.
// Note: this will also result in at least one more
// getheaders message to be sent to
// this peer (eventually).
state.fSyncStarted = false;
@@ -5910,13 +5910,13 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
peer->m_headers_sync_timeout = std::chrono::microseconds::max();
}
}
// Check that outbound peers have reasonable chains
// GetTime() is used by this anti-DoS logic so we can test this using mocktime
- ConsiderEviction(*pto, *peer, GetTime<std::chrono::seconds>());
+ ConsiderEviction(to, *peer, GetTime<std::chrono::seconds>());
//
// Message: getdata (blocks)
//
std::vector<CInv> vGetData;
if (CanServeBlocks(*peer) && ((sync_blocks_and_headers_from_peer && !IsLimitedPeer(*peer)) || !m_chainman.IsInitialBlockDownload()) && state.vBlocksInFlight.size() < MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
@@ -5939,15 +5939,15 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
vToDownload, from_tip,
Assert(m_chainman.GetSnapshotBaseBlock()));
}
for (const CBlockIndex *pindex : vToDownload) {
uint32_t nFetchFlags = GetFetchFlags(*peer);
vGetData.emplace_back(MSG_BLOCK | nFetchFlags, pindex->GetBlockHash());
- BlockRequested(pto->GetId(), *pindex);
+ BlockRequested(to.GetId(), *pindex);
LogDebug(BCLog::NET, "Requesting block %s (%d) peer=%d\n", pindex->GetBlockHash().ToString(),
- pindex->nHeight, pto->GetId());
+ pindex->nHeight, to.GetId());
}
if (state.vBlocksInFlight.empty() && staller != -1) {
if (State(staller)->m_stalling_since == 0us) {
State(staller)->m_stalling_since = current_time;
LogDebug(BCLog::NET, "Stall started peer=%d\n", staller);
}
@@ -5956,21 +5956,21 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
//
// Message: getdata (transactions)
//
{
LOCK(m_tx_download_mutex);
- for (const GenTxid& gtxid : m_txdownloadman.GetRequestsToSend(pto->GetId(), current_time)) {
+ for (const GenTxid& gtxid : m_txdownloadman.GetRequestsToSend(to.GetId(), current_time)) {
vGetData.emplace_back(gtxid.IsWtxid() ? MSG_WTX : (MSG_TX | GetFetchFlags(*peer)), gtxid.GetHash());
if (vGetData.size() >= MAX_GETDATA_SZ) {
- MakeAndPushMessage(*pto, NetMsgType::GETDATA, vGetData);
+ MakeAndPushMessage(to, NetMsgType::GETDATA, vGetData);
vGetData.clear();
}
}
}
if (!vGetData.empty())
- MakeAndPushMessage(*pto, NetMsgType::GETDATA, vGetData);
+ MakeAndPushMessage(to, NetMsgType::GETDATA, vGetData);
} // release cs_main
- MaybeSendFeefilter(*pto, *peer, current_time);
+ MaybeSendFeefilter(to, *peer, current_time);
return true;
}
diff --git i/src/test/denialofservice_tests.cpp w/src/test/denialofservice_tests.cpp
index 9ee7e9c9fe..1d658ac9e0 100644
--- i/src/test/denialofservice_tests.cpp
+++ w/src/test/denialofservice_tests.cpp
@@ -77,33 +77,33 @@ BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
LOCK(cs_main);
BOOST_CHECK(m_node.chainman->ActiveChain().Tip() != nullptr);
BOOST_CHECK(m_node.chainman->ActiveChain().Tip()->nChainWork > 0);
}
// Test starts here
- BOOST_CHECK(peerman.SendMessages(&dummyNode1)); // should result in getheaders
+ BOOST_CHECK(peerman.SendMessages(dummyNode1)); // should result in getheaders
{
LOCK(dummyNode1.cs_vSend);
const auto& [to_send, _more, _msg_type] = dummyNode1.m_transport->GetBytesToSend(false);
BOOST_CHECK(!to_send.empty());
}
connman.FlushSendBuffer(dummyNode1);
int64_t nStartTime = GetTime();
// Wait 21 minutes
SetMockTime(nStartTime+21*60);
- BOOST_CHECK(peerman.SendMessages(&dummyNode1)); // should result in getheaders
+ BOOST_CHECK(peerman.SendMessages(dummyNode1)); // should result in getheaders
{
LOCK(dummyNode1.cs_vSend);
const auto& [to_send, _more, _msg_type] = dummyNode1.m_transport->GetBytesToSend(false);
BOOST_CHECK(!to_send.empty());
}
// Wait 3 more minutes
SetMockTime(nStartTime+24*60);
- BOOST_CHECK(peerman.SendMessages(&dummyNode1)); // should result in disconnect
+ BOOST_CHECK(peerman.SendMessages(dummyNode1)); // should result in disconnect
BOOST_CHECK(dummyNode1.fDisconnect == true);
peerman.FinalizeNode(dummyNode1);
}
struct OutboundTest : TestingSetup {
@@ -330,13 +330,13 @@ BOOST_AUTO_TEST_CASE(peer_discouragement)
/*inbound_onion=*/false};
nodes[0]->SetCommonVersion(PROTOCOL_VERSION);
peerLogic->InitializeNode(*nodes[0], NODE_NETWORK);
nodes[0]->fSuccessfullyConnected = true;
connman->AddTestNode(*nodes[0]);
peerLogic->UnitTestMisbehaving(nodes[0]->GetId()); // Should be discouraged
- BOOST_CHECK(peerLogic->SendMessages(nodes[0]));
+ BOOST_CHECK(peerLogic->SendMessages(*nodes[0]));
BOOST_CHECK(banman->IsDiscouraged(addr[0]));
BOOST_CHECK(nodes[0]->fDisconnect);
BOOST_CHECK(!banman->IsDiscouraged(other_addr)); // Different address, not discouraged
nodes[1] = new CNode{id++,
@@ -349,21 +349,21 @@ BOOST_AUTO_TEST_CASE(peer_discouragement)
ConnectionType::INBOUND,
/*inbound_onion=*/false};
nodes[1]->SetCommonVersion(PROTOCOL_VERSION);
peerLogic->InitializeNode(*nodes[1], NODE_NETWORK);
nodes[1]->fSuccessfullyConnected = true;
connman->AddTestNode(*nodes[1]);
- BOOST_CHECK(peerLogic->SendMessages(nodes[1]));
+ BOOST_CHECK(peerLogic->SendMessages(*nodes[1]));
// [0] is still discouraged/disconnected.
BOOST_CHECK(banman->IsDiscouraged(addr[0]));
BOOST_CHECK(nodes[0]->fDisconnect);
// [1] is not discouraged/disconnected yet.
BOOST_CHECK(!banman->IsDiscouraged(addr[1]));
BOOST_CHECK(!nodes[1]->fDisconnect);
peerLogic->UnitTestMisbehaving(nodes[1]->GetId());
- BOOST_CHECK(peerLogic->SendMessages(nodes[1]));
+ BOOST_CHECK(peerLogic->SendMessages(*nodes[1]));
// Expect both [0] and [1] to be discouraged/disconnected now.
BOOST_CHECK(banman->IsDiscouraged(addr[0]));
BOOST_CHECK(nodes[0]->fDisconnect);
BOOST_CHECK(banman->IsDiscouraged(addr[1]));
BOOST_CHECK(nodes[1]->fDisconnect);
@@ -380,13 +380,13 @@ BOOST_AUTO_TEST_CASE(peer_discouragement)
/*inbound_onion=*/false};
nodes[2]->SetCommonVersion(PROTOCOL_VERSION);
peerLogic->InitializeNode(*nodes[2], NODE_NETWORK);
nodes[2]->fSuccessfullyConnected = true;
connman->AddTestNode(*nodes[2]);
peerLogic->UnitTestMisbehaving(nodes[2]->GetId());
- BOOST_CHECK(peerLogic->SendMessages(nodes[2]));
+ BOOST_CHECK(peerLogic->SendMessages(*nodes[2]));
BOOST_CHECK(banman->IsDiscouraged(addr[0]));
BOOST_CHECK(banman->IsDiscouraged(addr[1]));
BOOST_CHECK(banman->IsDiscouraged(addr[2]));
BOOST_CHECK(nodes[0]->fDisconnect);
BOOST_CHECK(nodes[1]->fDisconnect);
BOOST_CHECK(nodes[2]->fDisconnect);
@@ -422,13 +422,13 @@ BOOST_AUTO_TEST_CASE(DoS_bantime)
/*inbound_onion=*/false};
dummyNode.SetCommonVersion(PROTOCOL_VERSION);
peerLogic->InitializeNode(dummyNode, NODE_NETWORK);
dummyNode.fSuccessfullyConnected = true;
peerLogic->UnitTestMisbehaving(dummyNode.GetId());
- BOOST_CHECK(peerLogic->SendMessages(&dummyNode));
+ BOOST_CHECK(peerLogic->SendMessages(dummyNode));
BOOST_CHECK(banman->IsDiscouraged(addr));
peerLogic->FinalizeNode(dummyNode);
}
BOOST_AUTO_TEST_SUITE_END()
diff --git i/src/test/fuzz/p2p_handshake.cpp w/src/test/fuzz/p2p_handshake.cpp
index d608efd87a..183896bf3c 100644
--- i/src/test/fuzz/p2p_handshake.cpp
+++ w/src/test/fuzz/p2p_handshake.cpp
@@ -97,12 +97,12 @@ FUZZ_TARGET(p2p_handshake, .init = ::initialize)
connection.fPauseSend = false;
try {
more_work = connman.ProcessMessagesOnce(connection);
} catch (const std::ios_base::failure&) {
}
- peerman->SendMessages(&connection);
+ peerman->SendMessages(connection);
}
}
g_setup->m_node.connman->StopNodes();
}
diff --git i/src/test/fuzz/p2p_headers_presync.cpp w/src/test/fuzz/p2p_headers_presync.cpp
index b31b74ee4f..88072c7570 100644
--- i/src/test/fuzz/p2p_headers_presync.cpp
+++ w/src/test/fuzz/p2p_headers_presync.cpp
@@ -94,13 +94,13 @@ void HeadersSyncSetup::SendMessage(FuzzedDataProvider& fuzzed_data_provider, CSe
(void)connman.ReceiveMsgFrom(connection, std::move(msg));
connection.fPauseSend = false;
try {
connman.ProcessMessagesOnce(connection);
} catch (const std::ios_base::failure&) {
}
- m_node.peerman->SendMessages(&connection);
+ m_node.peerman->SendMessages(connection);
}
CBlockHeader ConsumeHeader(FuzzedDataProvider& fuzzed_data_provider, const uint256& prev_hash, uint32_t prev_nbits)
{
CBlockHeader header;
header.nNonce = 0;
diff --git i/src/test/fuzz/process_message.cpp w/src/test/fuzz/process_message.cpp
index 4bd38a1ac6..2a304cbd4b 100644
--- i/src/test/fuzz/process_message.cpp
+++ w/src/test/fuzz/process_message.cpp
@@ -85,11 +85,11 @@ FUZZ_TARGET(process_message, .init = initialize_process_message)
while (more_work) {
p2p_node.fPauseSend = false;
try {
more_work = connman.ProcessMessagesOnce(p2p_node);
} catch (const std::ios_base::failure&) {
}
- g_setup->m_node.peerman->SendMessages(&p2p_node);
+ g_setup->m_node.peerman->SendMessages(p2p_node);
}
g_setup->m_node.validation_signals->SyncWithValidationInterfaceQueue();
g_setup->m_node.connman->StopNodes();
}
diff --git i/src/test/fuzz/process_messages.cpp w/src/test/fuzz/process_messages.cpp
index 0688868c02..342a4038b9 100644
--- i/src/test/fuzz/process_messages.cpp
+++ w/src/test/fuzz/process_messages.cpp
@@ -84,12 +84,12 @@ FUZZ_TARGET(process_messages, .init = initialize_process_messages)
random_node.fPauseSend = false;
try {
more_work = connman.ProcessMessagesOnce(random_node);
} catch (const std::ios_base::failure&) {
}
- g_setup->m_node.peerman->SendMessages(&random_node);
+ g_setup->m_node.peerman->SendMessages(random_node);
}
}
g_setup->m_node.validation_signals->SyncWithValidationInterfaceQueue();
g_setup->m_node.connman->StopNodes();
}
diff --git i/src/test/net_tests.cpp w/src/test/net_tests.cpp
index 0036d94c2f..ce10b50417 100644
--- i/src/test/net_tests.cpp
+++ w/src/test/net_tests.cpp
@@ -875,13 +875,13 @@ BOOST_AUTO_TEST_CASE(initial_advertise_from_version_message)
return;
}
}
}
};
- m_node.peerman->SendMessages(&peer);
+ m_node.peerman->SendMessages(peer);
BOOST_CHECK(sent);
CaptureMessage = CaptureMessageOrig;
chainman.ResetIbd();
m_node.args->ForceSetArg("-capturemessages", "0");
diff --git i/src/test/util/net.cpp w/src/test/util/net.cpp
index 0b8070a88e..4f4496c93e 100644
--- i/src/test/util/net.cpp
+++ w/src/test/util/net.cpp
@@ -28,13 +28,13 @@ void ConnmanTestMsg::Handshake(CNode& node,
bool relay_txs)
{
auto& peerman{static_cast<PeerManager&>(*m_msgproc)};
auto& connman{*this};
peerman.InitializeNode(node, local_services);
- peerman.SendMessages(&node);
+ peerman.SendMessages(node);
FlushSendBuffer(node); // Drop the version message added by SendMessages.
CSerializedNetMsg msg_version{
NetMsg::Make(NetMsgType::VERSION,
version, //
Using<CustomUintFormatter<8>>(remote_services), //
@@ -49,13 +49,13 @@ void ConnmanTestMsg::Handshake(CNode& node,
relay_txs),
};
(void)connman.ReceiveMsgFrom(node, std::move(msg_version));
node.fPauseSend = false;
connman.ProcessMessagesOnce(node);
- peerman.SendMessages(&node);
+ peerman.SendMessages(node);
FlushSendBuffer(node); // Drop the verack message added by SendMessages.
if (node.fDisconnect) return;
assert(node.nVersion == version);
assert(node.GetCommonVersion() == std::min(version, PROTOCOL_VERSION));
CNodeStateStats statestats;
assert(peerman.GetNodeStateStats(node.GetId(), statestats));
@@ -63,13 +63,13 @@ void ConnmanTestMsg::Handshake(CNode& node,
assert(statestats.their_services == remote_services);
if (successfully_connected) {
CSerializedNetMsg msg_verack{NetMsg::Make(NetMsgType::VERACK)};
(void)connman.ReceiveMsgFrom(node, std::move(msg_verack));
node.fPauseSend = false;
connman.ProcessMessagesOnce(node);
- peerman.SendMessages(&node);
+ peerman.SendMessages(node);
assert(node.fSuccessfullyConnected == true);
}
}
void ConnmanTestMsg::NodeReceiveMsgBytes(CNode& node, std::span<const uint8_t> msg_bytes, bool& complete) const
{
diff --git i/src/test/util/net.h w/src/test/util/net.h
index 36c5dc5c17..0a0719c735 100644
--- i/src/test/util/net.h
+++ w/src/test/util/net.h
@@ -70,15 +70,15 @@ struct ConnmanTestMsg : public CConnman {
ServiceFlags remote_services,
ServiceFlags local_services,
int32_t version,
bool relay_txs)
EXCLUSIVE_LOCKS_REQUIRED(NetEventsInterface::g_msgproc_mutex);
- bool ProcessMessagesOnce(CNode& node) EXCLUSIVE_LOCKS_REQUIRED(NetEventsInterface::g_msgproc_mutex)
+ bool ProcessMessagesOnce(CNode& from) EXCLUSIVE_LOCKS_REQUIRED(NetEventsInterface::g_msgproc_mutex)
{
- return m_msgproc->ProcessMessages(&node, flagInterruptMsgProc);
+ return m_msgproc->ProcessMessages(from, flagInterruptMsgProc);
}
void NodeReceiveMsgBytes(CNode& node, std::span<const uint8_t> msg_bytes, bool& complete) const;
bool ReceiveMsgFrom(CNode& node, CSerializedNetMsg&& ser_msg) const;
void FlushSendBuffer(CNode& node) const;
</details>