Skip to content

Commit 090b75c

Browse files
committed
p2p: Avoid allocating memory for addrKnown where we don't need it
1 parent c34b886 commit 090b75c

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2652,7 +2652,6 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn
26522652
addrBind(addrBindIn),
26532653
fInbound(fInboundIn),
26542654
nKeyedNetGroup(nKeyedNetGroupIn),
2655-
addrKnown(5000, 0.001),
26562655
// Don't relay addr messages to peers that we connect to as block-relay-only
26572656
// peers (to prevent adversaries from inferring these links from addr
26582657
// traffic).
@@ -2669,6 +2668,10 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn
26692668
m_tx_relay = MakeUnique<TxRelay>();
26702669
}
26712670

2671+
if (m_addr_relay_peer) {
2672+
m_addr_known = MakeUnique<CRollingBloomFilter>(5000, 0.001);
2673+
}
2674+
26722675
for (const std::string &msg : getAllNetMessageTypes())
26732676
mapRecvBytesPerMsgCmd[msg] = 0;
26742677
mapRecvBytesPerMsgCmd[NET_MESSAGE_COMMAND_OTHER] = 0;

src/net.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ class CNode
729729

730730
// flood relay
731731
std::vector<CAddress> vAddrToSend;
732-
CRollingBloomFilter addrKnown;
732+
std::unique_ptr<CRollingBloomFilter> m_addr_known;
733733
bool fGetAddr{false};
734734
int64_t nNextAddrSend GUARDED_BY(cs_sendProcessing){0};
735735
int64_t nNextLocalAddrSend GUARDED_BY(cs_sendProcessing){0};
@@ -884,15 +884,15 @@ class CNode
884884

885885
void AddAddressKnown(const CAddress& _addr)
886886
{
887-
addrKnown.insert(_addr.GetKey());
887+
m_addr_known->insert(_addr.GetKey());
888888
}
889889

890890
void PushAddress(const CAddress& _addr, FastRandomContext &insecure_rand)
891891
{
892892
// Known checking here is only to save space from duplicates.
893893
// SendMessages will filter it again for knowns that were added
894894
// after addresses were pushed.
895-
if (_addr.IsValid() && !addrKnown.contains(_addr.GetKey())) {
895+
if (_addr.IsValid() && !m_addr_known->contains(_addr.GetKey())) {
896896
if (vAddrToSend.size() >= MAX_ADDR_TO_SEND) {
897897
vAddrToSend[insecure_rand.randrange(vAddrToSend.size())] = _addr;
898898
} else {

src/net_processing.cpp

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

13161316
// Relay to a limited number of other nodes
13171317
// Use deterministic randomness to send to the same nodes for 24 hours
1318-
// at a time so the addrKnowns of the chosen nodes prevent repeats
1318+
// at a time so the m_addr_knowns of the chosen nodes prevent repeats
13191319
uint64_t hashAddr = addr.GetHash();
13201320
const CSipHasher hasher = connman->GetDeterministicRandomizer(RANDOMIZER_ID_ADDRESS_RELAY).Write(hashAddr << 32).Write((GetTime() + hashAddr) / (24*60*60));
13211321
FastRandomContext insecure_rand;
@@ -3563,9 +3563,9 @@ bool PeerLogicValidation::SendMessages(CNode* pto)
35633563
vAddr.reserve(pto->vAddrToSend.size());
35643564
for (const CAddress& addr : pto->vAddrToSend)
35653565
{
3566-
if (!pto->addrKnown.contains(addr.GetKey()))
3566+
if (!pto->m_addr_known->contains(addr.GetKey()))
35673567
{
3568-
pto->addrKnown.insert(addr.GetKey());
3568+
pto->m_addr_known->insert(addr.GetKey());
35693569
vAddr.push_back(addr);
35703570
// receiver rejects addr messages larger than 1000
35713571
if (vAddr.size() >= 1000)

0 commit comments

Comments
 (0)