From 090b75c14be6b9ba2efe38a17d141c6e6af575cb Mon Sep 17 00:00:00 2001 From: User Date: Wed, 16 Oct 2019 17:06:20 -0400 Subject: [PATCH 1/3] p2p: Avoid allocating memory for addrKnown where we don't need it --- src/bloom.h | 3 --- src/net.cpp | 5 ++++- src/net.h | 6 +++--- src/net_processing.cpp | 6 +++--- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/bloom.h b/src/bloom.h index 7d3aa878b0..c3f64ba4bc 100644 --- a/src/bloom.h +++ b/src/bloom.h @@ -115,9 +115,6 @@ public: class CRollingBloomFilter { public: - // A random bloom filter calls GetRand() at creation time. - // Don't create global CRollingBloomFilter objects, as they may be - // constructed before the randomizer is properly initialized. CRollingBloomFilter(const unsigned int nElements, const double nFPRate); void insert(const std::vector& vKey); diff --git a/src/net.cpp b/src/net.cpp index 63b7833822..df52689092 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -2652,7 +2652,6 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn addrBind(addrBindIn), fInbound(fInboundIn), nKeyedNetGroup(nKeyedNetGroupIn), - addrKnown(5000, 0.001), // Don't relay addr messages to peers that we connect to as block-relay-only // peers (to prevent adversaries from inferring these links from addr // traffic). @@ -2669,6 +2668,10 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn m_tx_relay = MakeUnique(); } + if (m_addr_relay_peer) { + m_addr_known = MakeUnique(5000, 0.001); + } + for (const std::string &msg : getAllNetMessageTypes()) mapRecvBytesPerMsgCmd[msg] = 0; mapRecvBytesPerMsgCmd[NET_MESSAGE_COMMAND_OTHER] = 0; diff --git a/src/net.h b/src/net.h index 44655abf80..4a7f7bc093 100644 --- a/src/net.h +++ b/src/net.h @@ -729,7 +729,7 @@ public: // flood relay std::vector vAddrToSend; - CRollingBloomFilter addrKnown; + std::unique_ptr m_addr_known; bool fGetAddr{false}; int64_t nNextAddrSend GUARDED_BY(cs_sendProcessing){0}; int64_t nNextLocalAddrSend GUARDED_BY(cs_sendProcessing){0}; @@ -884,7 +884,7 @@ public: void AddAddressKnown(const CAddress& _addr) { - addrKnown.insert(_addr.GetKey()); + m_addr_known->insert(_addr.GetKey()); } void PushAddress(const CAddress& _addr, FastRandomContext &insecure_rand) @@ -892,7 +892,7 @@ public: // Known checking here is only to save space from duplicates. // SendMessages will filter it again for knowns that were added // after addresses were pushed. - if (_addr.IsValid() && !addrKnown.contains(_addr.GetKey())) { + if (_addr.IsValid() && !m_addr_known->contains(_addr.GetKey())) { if (vAddrToSend.size() >= MAX_ADDR_TO_SEND) { vAddrToSend[insecure_rand.randrange(vAddrToSend.size())] = _addr; } else { diff --git a/src/net_processing.cpp b/src/net_processing.cpp index b6839dcf21..188c3d777e 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -1315,7 +1315,7 @@ static void RelayAddress(const CAddress& addr, bool fReachable, CConnman* connma // Relay to a limited number of other nodes // Use deterministic randomness to send to the same nodes for 24 hours - // at a time so the addrKnowns of the chosen nodes prevent repeats + // at a time so the m_addr_knowns of the chosen nodes prevent repeats uint64_t hashAddr = addr.GetHash(); const CSipHasher hasher = connman->GetDeterministicRandomizer(RANDOMIZER_ID_ADDRESS_RELAY).Write(hashAddr << 32).Write((GetTime() + hashAddr) / (24*60*60)); FastRandomContext insecure_rand; @@ -3563,9 +3563,9 @@ bool PeerLogicValidation::SendMessages(CNode* pto) vAddr.reserve(pto->vAddrToSend.size()); for (const CAddress& addr : pto->vAddrToSend) { - if (!pto->addrKnown.contains(addr.GetKey())) + if (!pto->m_addr_known->contains(addr.GetKey())) { - pto->addrKnown.insert(addr.GetKey()); + pto->m_addr_known->insert(addr.GetKey()); vAddr.push_back(addr); // receiver rejects addr messages larger than 1000 if (vAddr.size() >= 1000) From a552e8477c5bcd22a5457f4f73a2fd6db8acd2c2 Mon Sep 17 00:00:00 2001 From: User Date: Fri, 25 Oct 2019 16:28:14 -0400 Subject: [PATCH 2/3] added asserts to check m_addr_known when it's used --- src/net.h | 2 ++ src/net_processing.cpp | 1 + 2 files changed, 3 insertions(+) diff --git a/src/net.h b/src/net.h index 4a7f7bc093..95193a09db 100644 --- a/src/net.h +++ b/src/net.h @@ -884,6 +884,7 @@ public: void AddAddressKnown(const CAddress& _addr) { + assert(m_addr_known); m_addr_known->insert(_addr.GetKey()); } @@ -892,6 +893,7 @@ public: // 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())) { if (vAddrToSend.size() >= MAX_ADDR_TO_SEND) { vAddrToSend[insecure_rand.randrange(vAddrToSend.size())] = _addr; diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 188c3d777e..c1743c2e32 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -3561,6 +3561,7 @@ bool PeerLogicValidation::SendMessages(CNode* pto) pto->nNextAddrSend = PoissonNextSend(nNow, AVG_ADDRESS_BROADCAST_INTERVAL); std::vector vAddr; vAddr.reserve(pto->vAddrToSend.size()); + assert(pto->m_addr_known); for (const CAddress& addr : pto->vAddrToSend) { if (!pto->m_addr_known->contains(addr.GetKey())) From b6d2183858975abc961207c125c15791e531edcc Mon Sep 17 00:00:00 2001 From: User Date: Thu, 31 Oct 2019 13:42:02 -0400 Subject: [PATCH 3/3] Minor refactoring to remove implied m_addr_relay_peer. Co-authored-by: MarcoFalke --- src/net.cpp | 6 +----- src/net.h | 5 ++--- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/src/net.cpp b/src/net.cpp index df52689092..0415386c0d 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -2655,7 +2655,7 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn // Don't relay addr messages to peers that we connect to as block-relay-only // peers (to prevent adversaries from inferring these links from addr // traffic). - m_addr_relay_peer(!block_relay_only), + m_addr_known{block_relay_only ? nullptr : MakeUnique(5000, 0.001)}, id(idIn), nLocalHostNonce(nLocalHostNonceIn), nLocalServices(nLocalServicesIn), @@ -2668,10 +2668,6 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn m_tx_relay = MakeUnique(); } - if (m_addr_relay_peer) { - m_addr_known = MakeUnique(5000, 0.001); - } - for (const std::string &msg : getAllNetMessageTypes()) mapRecvBytesPerMsgCmd[msg] = 0; mapRecvBytesPerMsgCmd[NET_MESSAGE_COMMAND_OTHER] = 0; diff --git a/src/net.h b/src/net.h index 95193a09db..5bca789219 100644 --- a/src/net.h +++ b/src/net.h @@ -729,13 +729,12 @@ public: // flood relay std::vector vAddrToSend; - std::unique_ptr m_addr_known; + const std::unique_ptr m_addr_known; bool fGetAddr{false}; int64_t nNextAddrSend GUARDED_BY(cs_sendProcessing){0}; int64_t nNextLocalAddrSend GUARDED_BY(cs_sendProcessing){0}; - const bool m_addr_relay_peer; - bool IsAddrRelayPeer() const { return m_addr_relay_peer; } + bool IsAddrRelayPeer() const { return m_addr_known != nullptr; } // List of block ids we still have announce. // There is no final sorting before sending, as they are always sent immediately