Skip to content

Commit 8f9df2e

Browse files
author
MarcoFalke
committed
Merge #17164: p2p: Avoid allocating memory for addrKnown where we don't need it
b6d2183 Minor refactoring to remove implied m_addr_relay_peer. (User) a552e84 added asserts to check m_addr_known when it's used (User) 090b75c 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
2 parents bbc9e41 + b6d2183 commit 8f9df2e

File tree

4 files changed

+11
-13
lines changed

4 files changed

+11
-13
lines changed

src/bloom.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,6 @@ class CBloomFilter
115115
class CRollingBloomFilter
116116
{
117117
public:
118-
// A random bloom filter calls GetRand() at creation time.
119-
// Don't create global CRollingBloomFilter objects, as they may be
120-
// constructed before the randomizer is properly initialized.
121118
CRollingBloomFilter(const unsigned int nElements, const double nFPRate);
122119

123120
void insert(const std::vector<unsigned char>& vKey);

src/net.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2666,11 +2666,10 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn
26662666
addrBind(addrBindIn),
26672667
fInbound(fInboundIn),
26682668
nKeyedNetGroup(nKeyedNetGroupIn),
2669-
addrKnown(5000, 0.001),
26702669
// Don't relay addr messages to peers that we connect to as block-relay-only
26712670
// peers (to prevent adversaries from inferring these links from addr
26722671
// traffic).
2673-
m_addr_relay_peer(!block_relay_only),
2672+
m_addr_known{block_relay_only ? nullptr : MakeUnique<CRollingBloomFilter>(5000, 0.001)},
26742673
id(idIn),
26752674
nLocalHostNonce(nLocalHostNonceIn),
26762675
nLocalServices(nLocalServicesIn),

src/net.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -776,13 +776,12 @@ class CNode
776776

777777
// flood relay
778778
std::vector<CAddress> vAddrToSend;
779-
CRollingBloomFilter addrKnown;
779+
const std::unique_ptr<CRollingBloomFilter> m_addr_known;
780780
bool fGetAddr{false};
781781
int64_t nNextAddrSend GUARDED_BY(cs_sendProcessing){0};
782782
int64_t nNextLocalAddrSend GUARDED_BY(cs_sendProcessing){0};
783783

784-
const bool m_addr_relay_peer;
785-
bool IsAddrRelayPeer() const { return m_addr_relay_peer; }
784+
bool IsAddrRelayPeer() const { return m_addr_known != nullptr; }
786785

787786
// List of block ids we still have announce.
788787
// There is no final sorting before sending, as they are always sent immediately
@@ -931,15 +930,17 @@ class CNode
931930

932931
void AddAddressKnown(const CAddress& _addr)
933932
{
934-
addrKnown.insert(_addr.GetKey());
933+
assert(m_addr_known);
934+
m_addr_known->insert(_addr.GetKey());
935935
}
936936

937937
void PushAddress(const CAddress& _addr, FastRandomContext &insecure_rand)
938938
{
939939
// Known checking here is only to save space from duplicates.
940940
// SendMessages will filter it again for knowns that were added
941941
// after addresses were pushed.
942-
if (_addr.IsValid() && !addrKnown.contains(_addr.GetKey())) {
942+
assert(m_addr_known);
943+
if (_addr.IsValid() && !m_addr_known->contains(_addr.GetKey())) {
943944
if (vAddrToSend.size() >= MAX_ADDR_TO_SEND) {
944945
vAddrToSend[insecure_rand.randrange(vAddrToSend.size())] = _addr;
945946
} else {

src/net_processing.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,7 +1340,7 @@ static void RelayAddress(const CAddress& addr, bool fReachable, CConnman* connma
13401340

13411341
// Relay to a limited number of other nodes
13421342
// Use deterministic randomness to send to the same nodes for 24 hours
1343-
// at a time so the addrKnowns of the chosen nodes prevent repeats
1343+
// at a time so the m_addr_knowns of the chosen nodes prevent repeats
13441344
uint64_t hashAddr = addr.GetHash();
13451345
const CSipHasher hasher = connman->GetDeterministicRandomizer(RANDOMIZER_ID_ADDRESS_RELAY).Write(hashAddr << 32).Write((GetTime() + hashAddr) / (24*60*60));
13461346
FastRandomContext insecure_rand;
@@ -3587,11 +3587,12 @@ bool PeerLogicValidation::SendMessages(CNode* pto)
35873587
pto->nNextAddrSend = PoissonNextSend(nNow, AVG_ADDRESS_BROADCAST_INTERVAL);
35883588
std::vector<CAddress> vAddr;
35893589
vAddr.reserve(pto->vAddrToSend.size());
3590+
assert(pto->m_addr_known);
35903591
for (const CAddress& addr : pto->vAddrToSend)
35913592
{
3592-
if (!pto->addrKnown.contains(addr.GetKey()))
3593+
if (!pto->m_addr_known->contains(addr.GetKey()))
35933594
{
3594-
pto->addrKnown.insert(addr.GetKey());
3595+
pto->m_addr_known->insert(addr.GetKey());
35953596
vAddr.push_back(addr);
35963597
// receiver rejects addr messages larger than 1000
35973598
if (vAddr.size() >= 1000)

0 commit comments

Comments
 (0)