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

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

This commit is contained in:
User 2019-10-16 17:06:20 -04:00
parent c34b88620d
commit 090b75c14b
4 changed files with 10 additions and 10 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

@ -2652,7 +2652,6 @@ 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).
@ -2669,6 +2668,10 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn
m_tx_relay = MakeUnique<TxRelay>(); m_tx_relay = MakeUnique<TxRelay>();
} }
if (m_addr_relay_peer) {
m_addr_known = MakeUnique<CRollingBloomFilter>(5000, 0.001);
}
for (const std::string &msg : getAllNetMessageTypes()) for (const std::string &msg : getAllNetMessageTypes())
mapRecvBytesPerMsgCmd[msg] = 0; mapRecvBytesPerMsgCmd[msg] = 0;
mapRecvBytesPerMsgCmd[NET_MESSAGE_COMMAND_OTHER] = 0; mapRecvBytesPerMsgCmd[NET_MESSAGE_COMMAND_OTHER] = 0;

View file

@ -729,7 +729,7 @@ public:
// flood relay // flood relay
std::vector<CAddress> vAddrToSend; std::vector<CAddress> vAddrToSend;
CRollingBloomFilter addrKnown; 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};
@ -884,7 +884,7 @@ public:
void AddAddressKnown(const CAddress& _addr) void AddAddressKnown(const CAddress& _addr)
{ {
addrKnown.insert(_addr.GetKey()); m_addr_known->insert(_addr.GetKey());
} }
void PushAddress(const CAddress& _addr, FastRandomContext &insecure_rand) void PushAddress(const CAddress& _addr, FastRandomContext &insecure_rand)
@ -892,7 +892,7 @@ 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())) { 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

@ -1315,7 +1315,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;
@ -3563,9 +3563,9 @@ bool PeerLogicValidation::SendMessages(CNode* pto)
vAddr.reserve(pto->vAddrToSend.size()); vAddr.reserve(pto->vAddrToSend.size());
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)