Skip to content

Commit 8e31cf9

Browse files
committed
net, net_processing: use existing RNG objects more
PeerManagerImpl, as well as several net functions, already have existing FastRandomContext objects. Reuse them instead of constructing new ones.
1 parent d5fcbe9 commit 8e31cf9

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

src/net.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2481,9 +2481,9 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
24812481
auto start = GetTime<std::chrono::microseconds>();
24822482

24832483
// Minimum time before next feeler connection (in microseconds).
2484-
auto next_feeler = start + FastRandomContext().rand_exp_duration(FEELER_INTERVAL);
2485-
auto next_extra_block_relay = start + FastRandomContext().rand_exp_duration(EXTRA_BLOCK_RELAY_ONLY_PEER_INTERVAL);
2486-
auto next_extra_network_peer{start + FastRandomContext().rand_exp_duration(EXTRA_NETWORK_PEER_INTERVAL)};
2484+
auto next_feeler = start + rng.rand_exp_duration(FEELER_INTERVAL);
2485+
auto next_extra_block_relay = start + rng.rand_exp_duration(EXTRA_BLOCK_RELAY_ONLY_PEER_INTERVAL);
2486+
auto next_extra_network_peer{start + rng.rand_exp_duration(EXTRA_NETWORK_PEER_INTERVAL)};
24872487
const bool dnsseed = gArgs.GetBoolArg("-dnsseed", DEFAULT_DNSSEED);
24882488
bool add_fixed_seeds = gArgs.GetBoolArg("-fixedseeds", DEFAULT_FIXEDSEEDS);
24892489
const bool use_seednodes{gArgs.IsArgSet("-seednode")};
@@ -2642,10 +2642,10 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
26422642
// Because we can promote these connections to block-relay-only
26432643
// connections, they do not get their own ConnectionType enum
26442644
// (similar to how we deal with extra outbound peers).
2645-
next_extra_block_relay = now + FastRandomContext().rand_exp_duration(EXTRA_BLOCK_RELAY_ONLY_PEER_INTERVAL);
2645+
next_extra_block_relay = now + rng.rand_exp_duration(EXTRA_BLOCK_RELAY_ONLY_PEER_INTERVAL);
26462646
conn_type = ConnectionType::BLOCK_RELAY;
26472647
} else if (now > next_feeler) {
2648-
next_feeler = now + FastRandomContext().rand_exp_duration(FEELER_INTERVAL);
2648+
next_feeler = now + rng.rand_exp_duration(FEELER_INTERVAL);
26492649
conn_type = ConnectionType::FEELER;
26502650
fFeeler = true;
26512651
} else if (nOutboundFullRelay == m_max_outbound_full_relay &&
@@ -2658,7 +2658,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
26582658
// This is not attempted if the user changed -maxconnections to a value
26592659
// so low that less than MAX_OUTBOUND_FULL_RELAY_CONNECTIONS are made,
26602660
// to prevent interactions with otherwise protected outbound peers.
2661-
next_extra_network_peer = now + FastRandomContext().rand_exp_duration(EXTRA_NETWORK_PEER_INTERVAL);
2661+
next_extra_network_peer = now + rng.rand_exp_duration(EXTRA_NETWORK_PEER_INTERVAL);
26622662
} else {
26632663
// skip to next iteration of while loop
26642664
continue;

src/net_processing.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ class PeerManagerImpl final : public PeerManager
936936
* accurately determine when we received the transaction (and potentially
937937
* determine the transaction's origin). */
938938
std::chrono::microseconds NextInvToInbounds(std::chrono::microseconds now,
939-
std::chrono::seconds average_interval);
939+
std::chrono::seconds average_interval) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
940940

941941

942942
// All of the following cache a recent block, and are protected by m_most_recent_block_mutex
@@ -1244,7 +1244,7 @@ std::chrono::microseconds PeerManagerImpl::NextInvToInbounds(std::chrono::micros
12441244
// If this function were called from multiple threads simultaneously
12451245
// it would possible that both update the next send variable, and return a different result to their caller.
12461246
// This is not possible in practice as only the net processing thread invokes this function.
1247-
m_next_inv_to_inbounds = now + FastRandomContext().rand_exp_duration(average_interval);
1247+
m_next_inv_to_inbounds = now + m_rng.rand_exp_duration(average_interval);
12481248
}
12491249
return m_next_inv_to_inbounds;
12501250
}
@@ -5654,13 +5654,13 @@ void PeerManagerImpl::MaybeSendAddr(CNode& node, Peer& peer, std::chrono::micros
56545654
CAddress local_addr{*local_service, peer.m_our_services, Now<NodeSeconds>()};
56555655
PushAddress(peer, local_addr);
56565656
}
5657-
peer.m_next_local_addr_send = current_time + FastRandomContext().rand_exp_duration(AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL);
5657+
peer.m_next_local_addr_send = current_time + m_rng.rand_exp_duration(AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL);
56585658
}
56595659

56605660
// We sent an `addr` message to this peer recently. Nothing more to do.
56615661
if (current_time <= peer.m_next_addr_send) return;
56625662

5663-
peer.m_next_addr_send = current_time + FastRandomContext().rand_exp_duration(AVG_ADDRESS_BROADCAST_INTERVAL);
5663+
peer.m_next_addr_send = current_time + m_rng.rand_exp_duration(AVG_ADDRESS_BROADCAST_INTERVAL);
56645664

56655665
if (!Assume(peer.m_addrs_to_send.size() <= MAX_ADDR_TO_SEND)) {
56665666
// Should be impossible since we always check size before adding to
@@ -5747,13 +5747,13 @@ void PeerManagerImpl::MaybeSendFeefilter(CNode& pto, Peer& peer, std::chrono::mi
57475747
MakeAndPushMessage(pto, NetMsgType::FEEFILTER, filterToSend);
57485748
peer.m_fee_filter_sent = filterToSend;
57495749
}
5750-
peer.m_next_send_feefilter = current_time + FastRandomContext().rand_exp_duration(AVG_FEEFILTER_BROADCAST_INTERVAL);
5750+
peer.m_next_send_feefilter = current_time + m_rng.rand_exp_duration(AVG_FEEFILTER_BROADCAST_INTERVAL);
57515751
}
57525752
// If the fee filter has changed substantially and it's still more than MAX_FEEFILTER_CHANGE_DELAY
57535753
// until scheduled broadcast, then move the broadcast to within MAX_FEEFILTER_CHANGE_DELAY.
57545754
else if (current_time + MAX_FEEFILTER_CHANGE_DELAY < peer.m_next_send_feefilter &&
57555755
(currentFilter < 3 * peer.m_fee_filter_sent / 4 || currentFilter > 4 * peer.m_fee_filter_sent / 3)) {
5756-
peer.m_next_send_feefilter = current_time + FastRandomContext().randrange<std::chrono::microseconds>(MAX_FEEFILTER_CHANGE_DELAY);
5756+
peer.m_next_send_feefilter = current_time + m_rng.randrange<std::chrono::microseconds>(MAX_FEEFILTER_CHANGE_DELAY);
57575757
}
57585758
}
57595759

@@ -6059,7 +6059,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
60596059
if (pto->IsInboundConn()) {
60606060
tx_relay->m_next_inv_send_time = NextInvToInbounds(current_time, INBOUND_INVENTORY_BROADCAST_INTERVAL);
60616061
} else {
6062-
tx_relay->m_next_inv_send_time = current_time + FastRandomContext().rand_exp_duration(OUTBOUND_INVENTORY_BROADCAST_INTERVAL);
6062+
tx_relay->m_next_inv_send_time = current_time + m_rng.rand_exp_duration(OUTBOUND_INVENTORY_BROADCAST_INTERVAL);
60636063
}
60646064
}
60656065

0 commit comments

Comments
 (0)