From 83f8821a6f41854edd5c0b11deabba658890cde1 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Tue, 15 Dec 2020 09:56:53 -0800 Subject: [PATCH 1/2] refactor: add IsAddrCompatible() to CNode --- src/net.h | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/net.h b/src/net.h index 20e356562b..d7415d3f9f 100644 --- a/src/net.h +++ b/src/net.h @@ -1176,18 +1176,23 @@ public: m_addr_known->insert(_addr.GetKey()); } + /** + * Whether the peer supports the address. For example, a peer that does not + * implement BIP155 cannot receive Tor v3 addresses because it requires + * ADDRv2 (BIP155) encoding. + */ + bool IsAddrCompatible(const CAddress& addr) const + { + return m_wants_addrv2 || addr.IsAddrV1Compatible(); + } + void PushAddress(const CAddress& _addr, FastRandomContext &insecure_rand) { - // Whether the peer supports the address in `_addr`. For example, - // nodes that do not implement BIP155 cannot receive Tor v3 addresses - // because they require ADDRv2 (BIP155) encoding. - const bool addr_format_supported = m_wants_addrv2 || _addr.IsAddrV1Compatible(); - // Known checking here is only to save space from duplicates. // SendMessages will filter it again for knowns that were added // after addresses were pushed. assert(m_addr_known); - if (_addr.IsValid() && !m_addr_known->contains(_addr.GetKey()) && addr_format_supported) { + if (_addr.IsValid() && !m_addr_known->contains(_addr.GetKey()) && IsAddrCompatible(_addr)) { if (vAddrToSend.size() >= MAX_ADDR_TO_SEND) { vAddrToSend[insecure_rand.randrange(vAddrToSend.size())] = _addr; } else { From 37fe80e6267094f6051ccf9bec0c7f1a6b9e15da Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Tue, 15 Dec 2020 09:57:23 -0800 Subject: [PATCH 2/2] Only consider addrv2 peers for relay of non-addrv1 addresses --- src/net_processing.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 05e5681df3..a33c4a0bd4 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -1443,8 +1443,8 @@ static void RelayAddress(const CNode& originator, std::array,2> best{{{0, nullptr}, {0, nullptr}}}; assert(nRelayNodes <= best.size()); - auto sortfunc = [&best, &hasher, nRelayNodes, &originator](CNode* pnode) { - if (pnode->RelayAddrsWithConn() && pnode != &originator) { + auto sortfunc = [&best, &hasher, nRelayNodes, &originator, &addr](CNode* pnode) { + if (pnode->RelayAddrsWithConn() && pnode != &originator && pnode->IsAddrCompatible(addr)) { uint64_t hashKey = CSipHasher(hasher).Write(pnode->GetId()).Finalize(); for (unsigned int i = 0; i < nRelayNodes; i++) { if (hashKey > best[i].first) {