0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-03 09:56:38 -05:00

Merge #17164: p2p: Avoid allocating memory for addrKnown where we don't need it

b6d2183858 Minor refactoring to remove implied m_addr_relay_peer. (User)
a552e8477c added asserts to check m_addr_known when it's used (User)
090b75c14b p2p: Avoid allocating memory for addrKnown where we don't need it (User)

Pull request description:

  We should allocate memory for addrKnown filter only for those peers which are expected to participate in address relay.

  Currently, we do it for all peers (including SPV and block-relay-only),  which results in extra RAM where it's not needed.

  Upd:
  In future, we would still allow SPVs to ask for addrs, so allocation still will be done by default.
  However, they will be able to opt-out via [this proposal](https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2019-October/017428.html) and then we could save some more memory.
  This PR still saves memory for block-relay-only peers immediately after merging.

Top commit has no ACKs.

Tree-SHA512: e84d93b2615556d466f5ca0e543580fde763911a3bfea3127c493ddfaba8f05c8605cb94ff795d165af542b594400995a2c51338185c298581408687e7812463
This commit is contained in:
MarcoFalke 2019-11-04 11:17:02 -05:00
commit 8f9df2ed88
No known key found for this signature in database
GPG key ID: D2EA4850E7528B25
4 changed files with 11 additions and 13 deletions

View file

@ -115,9 +115,6 @@ public:
class CRollingBloomFilter class CRollingBloomFilter
{ {
public: 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); CRollingBloomFilter(const unsigned int nElements, const double nFPRate);
void insert(const std::vector<unsigned char>& vKey); void insert(const std::vector<unsigned char>& vKey);

View file

@ -2666,11 +2666,10 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn
addrBind(addrBindIn), addrBind(addrBindIn),
fInbound(fInboundIn), fInbound(fInboundIn),
nKeyedNetGroup(nKeyedNetGroupIn), nKeyedNetGroup(nKeyedNetGroupIn),
addrKnown(5000, 0.001),
// Don't relay addr messages to peers that we connect to as block-relay-only // 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 // peers (to prevent adversaries from inferring these links from addr
// traffic). // traffic).
m_addr_relay_peer(!block_relay_only), m_addr_known{block_relay_only ? nullptr : MakeUnique<CRollingBloomFilter>(5000, 0.001)},
id(idIn), id(idIn),
nLocalHostNonce(nLocalHostNonceIn), nLocalHostNonce(nLocalHostNonceIn),
nLocalServices(nLocalServicesIn), nLocalServices(nLocalServicesIn),

View file

@ -776,13 +776,12 @@ public:
// flood relay // flood relay
std::vector<CAddress> vAddrToSend; std::vector<CAddress> vAddrToSend;
CRollingBloomFilter addrKnown; const std::unique_ptr<CRollingBloomFilter> m_addr_known;
bool fGetAddr{false}; bool fGetAddr{false};
int64_t nNextAddrSend GUARDED_BY(cs_sendProcessing){0}; int64_t nNextAddrSend GUARDED_BY(cs_sendProcessing){0};
int64_t nNextLocalAddrSend 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_known != nullptr; }
bool IsAddrRelayPeer() const { return m_addr_relay_peer; }
// List of block ids we still have announce. // List of block ids we still have announce.
// There is no final sorting before sending, as they are always sent immediately // There is no final sorting before sending, as they are always sent immediately
@ -931,7 +930,8 @@ public:
void AddAddressKnown(const CAddress& _addr) void AddAddressKnown(const CAddress& _addr)
{ {
addrKnown.insert(_addr.GetKey()); assert(m_addr_known);
m_addr_known->insert(_addr.GetKey());
} }
void PushAddress(const CAddress& _addr, FastRandomContext &insecure_rand) void PushAddress(const CAddress& _addr, FastRandomContext &insecure_rand)
@ -939,7 +939,8 @@ public:
// Known checking here is only to save space from duplicates. // Known checking here is only to save space from duplicates.
// SendMessages will filter it again for knowns that were added // SendMessages will filter it again for knowns that were added
// after addresses were pushed. // after addresses were pushed.
if (_addr.IsValid() && !addrKnown.contains(_addr.GetKey())) { assert(m_addr_known);
if (_addr.IsValid() && !m_addr_known->contains(_addr.GetKey())) {
if (vAddrToSend.size() >= MAX_ADDR_TO_SEND) { if (vAddrToSend.size() >= MAX_ADDR_TO_SEND) {
vAddrToSend[insecure_rand.randrange(vAddrToSend.size())] = _addr; vAddrToSend[insecure_rand.randrange(vAddrToSend.size())] = _addr;
} else { } else {

View file

@ -1340,7 +1340,7 @@ static void RelayAddress(const CAddress& addr, bool fReachable, CConnman* connma
// Relay to a limited number of other nodes // Relay to a limited number of other nodes
// Use deterministic randomness to send to the same nodes for 24 hours // 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(); uint64_t hashAddr = addr.GetHash();
const CSipHasher hasher = connman->GetDeterministicRandomizer(RANDOMIZER_ID_ADDRESS_RELAY).Write(hashAddr << 32).Write((GetTime() + hashAddr) / (24*60*60)); const CSipHasher hasher = connman->GetDeterministicRandomizer(RANDOMIZER_ID_ADDRESS_RELAY).Write(hashAddr << 32).Write((GetTime() + hashAddr) / (24*60*60));
FastRandomContext insecure_rand; FastRandomContext insecure_rand;
@ -3587,11 +3587,12 @@ bool PeerLogicValidation::SendMessages(CNode* pto)
pto->nNextAddrSend = PoissonNextSend(nNow, AVG_ADDRESS_BROADCAST_INTERVAL); pto->nNextAddrSend = PoissonNextSend(nNow, AVG_ADDRESS_BROADCAST_INTERVAL);
std::vector<CAddress> vAddr; std::vector<CAddress> vAddr;
vAddr.reserve(pto->vAddrToSend.size()); vAddr.reserve(pto->vAddrToSend.size());
assert(pto->m_addr_known);
for (const CAddress& addr : pto->vAddrToSend) 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); vAddr.push_back(addr);
// receiver rejects addr messages larger than 1000 // receiver rejects addr messages larger than 1000
if (vAddr.size() >= 1000) if (vAddr.size() >= 1000)