Skip to content

Commit ad71929

Browse files
committed
[net processing] Extract addr send functionality into MaybeSendAddr()
Reviewer hint: review with `git diff --color-moved=dimmed-zebra --ignore-all-space`
1 parent 4ad4abc commit ad71929

File tree

1 file changed

+71
-65
lines changed

1 file changed

+71
-65
lines changed

src/net_processing.cpp

Lines changed: 71 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,9 @@ class PeerManagerImpl final : public PeerManager
322322
* to time out. */
323323
void MaybeSendPing(CNode& node_to, Peer& peer, std::chrono::microseconds now);
324324

325+
/** Send `addr` messages on a regular schedule. */
326+
void MaybeSendAddr(CNode* pto, std::chrono::microseconds current_time);
327+
325328
const CChainParams& m_chainparams;
326329
CConnman& m_connman;
327330
/** Pointer to this node's banman. May be nullptr - check existence before dereferencing. */
@@ -4138,6 +4141,72 @@ void PeerManagerImpl::MaybeSendPing(CNode& node_to, Peer& peer, std::chrono::mic
41384141
}
41394142
}
41404143

4144+
void PeerManagerImpl::MaybeSendAddr(CNode* pto, std::chrono::microseconds current_time)
4145+
{
4146+
LOCK(pto->m_addr_send_times_mutex);
4147+
const CNetMsgMaker msgMaker(pto->GetCommonVersion());
4148+
4149+
if (fListen && pto->RelayAddrsWithConn() &&
4150+
!m_chainman.ActiveChainstate().IsInitialBlockDownload() &&
4151+
pto->m_next_local_addr_send < current_time) {
4152+
// If we've sent before, clear the bloom filter for the peer, so that our
4153+
// self-announcement will actually go out.
4154+
// This might be unnecessary if the bloom filter has already rolled
4155+
// over since our last self-announcement, but there is only a small
4156+
// bandwidth cost that we can incur by doing this (which happens
4157+
// once a day on average).
4158+
if (pto->m_next_local_addr_send != 0us) {
4159+
pto->m_addr_known->reset();
4160+
}
4161+
if (std::optional<CAddress> local_addr = GetLocalAddrForPeer(pto)) {
4162+
FastRandomContext insecure_rand;
4163+
pto->PushAddress(*local_addr, insecure_rand);
4164+
}
4165+
pto->m_next_local_addr_send = PoissonNextSend(current_time, AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL);
4166+
}
4167+
4168+
//
4169+
// Message: addr
4170+
//
4171+
if (pto->RelayAddrsWithConn() && pto->m_next_addr_send < current_time) {
4172+
pto->m_next_addr_send = PoissonNextSend(current_time, AVG_ADDRESS_BROADCAST_INTERVAL);
4173+
std::vector<CAddress> vAddr;
4174+
vAddr.reserve(pto->vAddrToSend.size());
4175+
assert(pto->m_addr_known);
4176+
4177+
const char* msg_type;
4178+
int make_flags;
4179+
if (pto->m_wants_addrv2) {
4180+
msg_type = NetMsgType::ADDRV2;
4181+
make_flags = ADDRV2_FORMAT;
4182+
} else {
4183+
msg_type = NetMsgType::ADDR;
4184+
make_flags = 0;
4185+
}
4186+
4187+
for (const CAddress& addr : pto->vAddrToSend)
4188+
{
4189+
if (!pto->m_addr_known->contains(addr.GetKey()))
4190+
{
4191+
pto->m_addr_known->insert(addr.GetKey());
4192+
vAddr.push_back(addr);
4193+
// receiver rejects addr messages larger than MAX_ADDR_TO_SEND
4194+
if (vAddr.size() >= MAX_ADDR_TO_SEND)
4195+
{
4196+
m_connman.PushMessage(pto, msgMaker.Make(make_flags, msg_type, vAddr));
4197+
vAddr.clear();
4198+
}
4199+
}
4200+
}
4201+
pto->vAddrToSend.clear();
4202+
if (!vAddr.empty())
4203+
m_connman.PushMessage(pto, msgMaker.Make(make_flags, msg_type, vAddr));
4204+
// we only send the big addr message once
4205+
if (pto->vAddrToSend.capacity() > 40)
4206+
pto->vAddrToSend.shrink_to_fit();
4207+
}
4208+
}
4209+
41414210
namespace {
41424211
class CompareInvMempoolOrder
41434212
{
@@ -4183,76 +4252,13 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
41834252
// MaybeSendPing may have marked peer for disconnection
41844253
if (pto->fDisconnect) return true;
41854254

4255+
MaybeSendAddr(pto, current_time);
4256+
41864257
{
41874258
LOCK(cs_main);
41884259

41894260
CNodeState &state = *State(pto->GetId());
41904261

4191-
// Address refresh broadcast
4192-
{
4193-
LOCK(pto->m_addr_send_times_mutex);
4194-
4195-
if (fListen && pto->RelayAddrsWithConn() &&
4196-
!m_chainman.ActiveChainstate().IsInitialBlockDownload() &&
4197-
pto->m_next_local_addr_send < current_time) {
4198-
// If we've sent before, clear the bloom filter for the peer, so that our
4199-
// self-announcement will actually go out.
4200-
// This might be unnecessary if the bloom filter has already rolled
4201-
// over since our last self-announcement, but there is only a small
4202-
// bandwidth cost that we can incur by doing this (which happens
4203-
// once a day on average).
4204-
if (pto->m_next_local_addr_send != 0us) {
4205-
pto->m_addr_known->reset();
4206-
}
4207-
if (std::optional<CAddress> local_addr = GetLocalAddrForPeer(pto)) {
4208-
FastRandomContext insecure_rand;
4209-
pto->PushAddress(*local_addr, insecure_rand);
4210-
}
4211-
pto->m_next_local_addr_send = PoissonNextSend(current_time, AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL);
4212-
}
4213-
4214-
//
4215-
// Message: addr
4216-
//
4217-
if (pto->RelayAddrsWithConn() && pto->m_next_addr_send < current_time) {
4218-
pto->m_next_addr_send = PoissonNextSend(current_time, AVG_ADDRESS_BROADCAST_INTERVAL);
4219-
std::vector<CAddress> vAddr;
4220-
vAddr.reserve(pto->vAddrToSend.size());
4221-
assert(pto->m_addr_known);
4222-
4223-
const char* msg_type;
4224-
int make_flags;
4225-
if (pto->m_wants_addrv2) {
4226-
msg_type = NetMsgType::ADDRV2;
4227-
make_flags = ADDRV2_FORMAT;
4228-
} else {
4229-
msg_type = NetMsgType::ADDR;
4230-
make_flags = 0;
4231-
}
4232-
4233-
for (const CAddress& addr : pto->vAddrToSend)
4234-
{
4235-
if (!pto->m_addr_known->contains(addr.GetKey()))
4236-
{
4237-
pto->m_addr_known->insert(addr.GetKey());
4238-
vAddr.push_back(addr);
4239-
// receiver rejects addr messages larger than MAX_ADDR_TO_SEND
4240-
if (vAddr.size() >= MAX_ADDR_TO_SEND)
4241-
{
4242-
m_connman.PushMessage(pto, msgMaker.Make(make_flags, msg_type, vAddr));
4243-
vAddr.clear();
4244-
}
4245-
}
4246-
}
4247-
pto->vAddrToSend.clear();
4248-
if (!vAddr.empty())
4249-
m_connman.PushMessage(pto, msgMaker.Make(make_flags, msg_type, vAddr));
4250-
// we only send the big addr message once
4251-
if (pto->vAddrToSend.capacity() > 40)
4252-
pto->vAddrToSend.shrink_to_fit();
4253-
}
4254-
} // pto->m_addr_send_times_mutex
4255-
42564262
// Start block sync
42574263
if (pindexBestHeader == nullptr)
42584264
pindexBestHeader = m_chainman.ActiveChain().Tip();

0 commit comments

Comments
 (0)