MCPcopy Create free account
hub / github.com/ElementsProject/elements / RelayAddress

Method RelayAddress

src/net_processing.cpp:1742–1787  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1740}
1741
1742void PeerManagerImpl::RelayAddress(NodeId originator,
1743 const CAddress& addr,
1744 bool fReachable)
1745{
1746 // We choose the same nodes within a given 24h window (if the list of connected
1747 // nodes does not change) and we don't relay to nodes that already know an
1748 // address. So within 24h we will likely relay a given address once. This is to
1749 // prevent a peer from unjustly giving their address better propagation by sending
1750 // it to us repeatedly.
1751
1752 if (!fReachable && !addr.IsRelayable()) return;
1753
1754 // Relay to a limited number of other nodes
1755 // Use deterministic randomness to send to the same nodes for 24 hours
1756 // at a time so the m_addr_knowns of the chosen nodes prevent repeats
1757 const uint64_t hash_addr{CServiceHash(0, 0)(addr)};
1758 const CSipHasher hasher{m_connman.GetDeterministicRandomizer(RANDOMIZER_ID_ADDRESS_RELAY)
1759 .Write(hash_addr)
1760 .Write((GetTime() + hash_addr) / (24 * 60 * 60))};
1761 FastRandomContext insecure_rand;
1762
1763 // Relay reachable addresses to 2 peers. Unreachable addresses are relayed randomly to 1 or 2 peers.
1764 unsigned int nRelayNodes = (fReachable || (hasher.Finalize() & 1)) ? 2 : 1;
1765
1766 std::array<std::pair<uint64_t, Peer*>, 2> best{{{0, nullptr}, {0, nullptr}}};
1767 assert(nRelayNodes <= best.size());
1768
1769 LOCK(m_peer_mutex);
1770
1771 for (auto& [id, peer] : m_peer_map) {
1772 if (peer->m_addr_relay_enabled && id != originator && IsAddrCompatible(*peer, addr)) {
1773 uint64_t hashKey = CSipHasher(hasher).Write(id).Finalize();
1774 for (unsigned int i = 0; i < nRelayNodes; i++) {
1775 if (hashKey > best[i].first) {
1776 std::copy(best.begin() + i, best.begin() + nRelayNodes - 1, best.begin() + i + 1);
1777 best[i] = std::make_pair(hashKey, peer.get());
1778 break;
1779 }
1780 }
1781 }
1782 };
1783
1784 for (unsigned int i = 0; i < nRelayNodes && best[i].first != 0; i++) {
1785 PushAddress(*best[i].second, addr, insecure_rand);
1786 }
1787}
1788
1789void PeerManagerImpl::ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv& inv)
1790{

Callers

nothing calls this directly

Calls 12

CServiceHashClass · 0.85
GetTimeFunction · 0.85
IsAddrCompatibleFunction · 0.85
CSipHasherClass · 0.85
PushAddressFunction · 0.85
IsRelayableMethod · 0.80
WriteMethod · 0.45
FinalizeMethod · 0.45
sizeMethod · 0.45
beginMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected