Skip to content

Commit c02fa47

Browse files
committed
[net processing] Only call GetTime() once in SendMessages()
We currently call GetTime() 4 times in SendMessages(). Consolidate this to once GetTime() call.
1 parent 3bcd278 commit c02fa47

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

src/net_processing.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,10 @@ class PeerManagerImpl final : public PeerManager
317317
void PushNodeVersion(CNode& pnode, int64_t nTime);
318318

319319
/** Send a ping message every PING_INTERVAL or if requested via RPC. May
320-
* mark the peer to be disconnected if a ping has timed out. */
321-
void MaybeSendPing(CNode& node_to, Peer& peer);
320+
* mark the peer to be disconnected if a ping has timed out.
321+
* We use mockable time for ping timeouts, so setmocktime may cause pings
322+
* to time out. */
323+
void MaybeSendPing(CNode& node_to, Peer& peer, std::chrono::microseconds now);
322324

323325
const CChainParams& m_chainparams;
324326
CConnman& m_connman;
@@ -4096,12 +4098,8 @@ void PeerManagerImpl::CheckForStaleTipAndEvictPeers()
40964098
}
40974099
}
40984100

4099-
void PeerManagerImpl::MaybeSendPing(CNode& node_to, Peer& peer)
4101+
void PeerManagerImpl::MaybeSendPing(CNode& node_to, Peer& peer, std::chrono::microseconds now)
41004102
{
4101-
// Use mockable time for ping timeouts.
4102-
// This means that setmocktime may cause pings to time out.
4103-
auto now = GetTime<std::chrono::microseconds>();
4104-
41054103
if (m_connman.RunInactivityChecks(node_to) && peer.m_ping_nonce_sent &&
41064104
now > peer.m_ping_start.load() + std::chrono::seconds{TIMEOUT_INTERVAL}) {
41074105
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)
41784176
// If we get here, the outgoing message serialization version is set and can't change.
41794177
const CNetMsgMaker msgMaker(pto->GetCommonVersion());
41804178

4181-
MaybeSendPing(*pto, *peer);
4179+
const auto current_time = GetTime<std::chrono::microseconds>();
4180+
4181+
MaybeSendPing(*pto, *peer, current_time);
41824182

41834183
// MaybeSendPing may have marked peer for disconnection
41844184
if (pto->fDisconnect) return true;
@@ -4189,7 +4189,6 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
41894189
CNodeState &state = *State(pto->GetId());
41904190

41914191
// Address refresh broadcast
4192-
auto current_time = GetTime<std::chrono::microseconds>();
41934192

41944193
if (fListen && pto->RelayAddrsWithConn() &&
41954194
!m_chainman.ActiveChainstate().IsInitialBlockDownload() &&
@@ -4485,7 +4484,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
44854484
vInv.clear();
44864485
}
44874486
}
4488-
pto->m_tx_relay->m_last_mempool_req = GetTime<std::chrono::seconds>();
4487+
pto->m_tx_relay->m_last_mempool_req = std::chrono::duration_cast<std::chrono::seconds>(current_time);
44894488
}
44904489

44914490
// Determine transactions to relay
@@ -4573,7 +4572,6 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
45734572
m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
45744573

45754574
// Detect whether we're stalling
4576-
current_time = GetTime<std::chrono::microseconds>();
45774575
if (state.m_stalling_since.count() && state.m_stalling_since < current_time - BLOCK_STALLING_TIMEOUT) {
45784576
// Stalling only triggers when the block download window cannot move. During normal steady state,
45794577
// the download window should be much larger than the to-be-downloaded set of blocks, so disconnection

0 commit comments

Comments
 (0)