aboutsummaryrefslogtreecommitdiff
path: root/src/net_processing.cpp
diff options
context:
space:
mode:
authorJohn Newbery <john@johnnewbery.com>2021-03-03 08:46:55 +0000
committerJohn Newbery <john@johnnewbery.com>2021-03-29 12:15:23 +0100
commitc02fa47baa517e17b5c43bde3902b1e410c1b93f (patch)
tree0d6920d08100011e4aadb178d28cd6cba5719a23 /src/net_processing.cpp
parent3bcd278aa60adf925218a1d59506d525ae639bb3 (diff)
downloadbitcoin-c02fa47baa517e17b5c43bde3902b1e410c1b93f.tar.xz
[net processing] Only call GetTime() once in SendMessages()
We currently call GetTime() 4 times in SendMessages(). Consolidate this to once GetTime() call.
Diffstat (limited to 'src/net_processing.cpp')
-rw-r--r--src/net_processing.cpp20
1 files changed, 9 insertions, 11 deletions
diff --git a/src/net_processing.cpp b/src/net_processing.cpp
index e561f02c4a..9f9af0aa33 100644
--- a/src/net_processing.cpp
+++ b/src/net_processing.cpp
@@ -317,8 +317,10 @@ private:
void PushNodeVersion(CNode& pnode, int64_t nTime);
/** Send a ping message every PING_INTERVAL or if requested via RPC. May
- * mark the peer to be disconnected if a ping has timed out. */
- void MaybeSendPing(CNode& node_to, Peer& peer);
+ * mark the peer to be disconnected if a ping has timed out.
+ * We use mockable time for ping timeouts, so setmocktime may cause pings
+ * to time out. */
+ void MaybeSendPing(CNode& node_to, Peer& peer, std::chrono::microseconds now);
const CChainParams& m_chainparams;
CConnman& m_connman;
@@ -4096,12 +4098,8 @@ void PeerManagerImpl::CheckForStaleTipAndEvictPeers()
}
}
-void PeerManagerImpl::MaybeSendPing(CNode& node_to, Peer& peer)
+void PeerManagerImpl::MaybeSendPing(CNode& node_to, Peer& peer, std::chrono::microseconds now)
{
- // Use mockable time for ping timeouts.
- // This means that setmocktime may cause pings to time out.
- auto now = GetTime<std::chrono::microseconds>();
-
if (m_connman.RunInactivityChecks(node_to) && peer.m_ping_nonce_sent &&
now > peer.m_ping_start.load() + std::chrono::seconds{TIMEOUT_INTERVAL}) {
LogPrint(BCLog::NET, "ping timeout: %fs peer=%d\n", 0.000001 * count_microseconds(now - peer.m_ping_start.load()), peer.m_id);
@@ -4178,7 +4176,9 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
// If we get here, the outgoing message serialization version is set and can't change.
const CNetMsgMaker msgMaker(pto->GetCommonVersion());
- MaybeSendPing(*pto, *peer);
+ const auto current_time = GetTime<std::chrono::microseconds>();
+
+ MaybeSendPing(*pto, *peer, current_time);
// MaybeSendPing may have marked peer for disconnection
if (pto->fDisconnect) return true;
@@ -4189,7 +4189,6 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
CNodeState &state = *State(pto->GetId());
// Address refresh broadcast
- auto current_time = GetTime<std::chrono::microseconds>();
if (fListen && pto->RelayAddrsWithConn() &&
!m_chainman.ActiveChainstate().IsInitialBlockDownload() &&
@@ -4485,7 +4484,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
vInv.clear();
}
}
- pto->m_tx_relay->m_last_mempool_req = GetTime<std::chrono::seconds>();
+ pto->m_tx_relay->m_last_mempool_req = std::chrono::duration_cast<std::chrono::seconds>(current_time);
}
// Determine transactions to relay
@@ -4573,7 +4572,6 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
// Detect whether we're stalling
- current_time = GetTime<std::chrono::microseconds>();
if (state.m_stalling_since.count() && state.m_stalling_since < current_time - BLOCK_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