MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / MaybeSendAddr

Method MaybeSendAddr

src/net_processing.cpp:5486–5561  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5484}
5485
5486void PeerManagerImpl::MaybeSendAddr(CNode& node, Peer& peer, std::chrono::microseconds current_time)
5487{
5488 // Nothing to do for non-address-relay peers
5489 if (!peer.m_addr_relay_enabled) return;
5490
5491 LOCK(peer.m_addr_send_times_mutex);
5492 // Periodically advertise our local address to the peer.
5493 if (fListen && !m_chainman.IsInitialBlockDownload() &&
5494 peer.m_next_local_addr_send < current_time) {
5495 // If we've sent before, clear the bloom filter for the peer, so that our
5496 // self-announcement will actually go out.
5497 // This might be unnecessary if the bloom filter has already rolled
5498 // over since our last self-announcement, but there is only a small
5499 // bandwidth cost that we can incur by doing this (which happens
5500 // once a day on average).
5501 if (peer.m_next_local_addr_send != 0us) {
5502 peer.m_addr_known->reset();
5503 }
5504 if (std::optional<CService> local_service = GetLocalAddrForPeer(node)) {
5505 CAddress local_addr{*local_service, peer.m_our_services, Now<NodeSeconds>()};
5506 if (peer.m_next_local_addr_send == 0us) {
5507 // Send the initial self-announcement in its own message. This makes sure
5508 // rate-limiting with limited start-tokens doesn't ignore it if the first
5509 // message ends up containing multiple addresses.
5510 if (IsAddrCompatible(peer, local_addr)) {
5511 std::vector<CAddress> self_announcement{local_addr};
5512 if (peer.m_wants_addrv2) {
5513 MakeAndPushMessage(node, NetMsgType::ADDRV2, CAddress::V2_NETWORK(self_announcement));
5514 } else {
5515 MakeAndPushMessage(node, NetMsgType::ADDR, CAddress::V1_NETWORK(self_announcement));
5516 }
5517 }
5518 } else {
5519 // All later self-announcements are sent together with the other addresses.
5520 PushAddress(peer, local_addr);
5521 }
5522 }
5523 peer.m_next_local_addr_send = current_time + m_rng.rand_exp_duration(AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL);
5524 }
5525
5526 // We sent an `addr` message to this peer recently. Nothing more to do.
5527 if (current_time <= peer.m_next_addr_send) return;
5528
5529 peer.m_next_addr_send = current_time + m_rng.rand_exp_duration(AVG_ADDRESS_BROADCAST_INTERVAL);
5530
5531 if (!Assume(peer.m_addrs_to_send.size() <= MAX_ADDR_TO_SEND)) {
5532 // Should be impossible since we always check size before adding to
5533 // m_addrs_to_send. Recover by trimming the vector.
5534 peer.m_addrs_to_send.resize(MAX_ADDR_TO_SEND);
5535 }
5536
5537 // Remove addr records that the peer already knows about, and add new
5538 // addrs to the m_addr_known filter on the same pass.
5539 auto addr_already_known = [&peer](const CAddress& addr) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex) {
5540 bool ret = peer.m_addr_known->contains(addr.GetKey());
5541 if (!ret) peer.m_addr_known->insert(addr.GetKey());
5542 return ret;
5543 };

Callers

nothing calls this directly

Calls 15

GetLocalAddrForPeerFunction · 0.85
IsAddrCompatibleFunction · 0.85
rand_exp_durationMethod · 0.80
EXCLUSIVE_LOCKS_REQUIREDFunction · 0.70
resetMethod · 0.45
sizeMethod · 0.45
resizeMethod · 0.45
containsMethod · 0.45
GetKeyMethod · 0.45
insertMethod · 0.45
eraseMethod · 0.45

Tested by

no test coverage detected