Auto merge of #4997 - str4d:2074-net-setInventoryKnown, r=str4d

Replace setInventoryKnown with a rolling bloom filter

Cherry-picked from bitcoin/bitcoin#7133.
- Excluding for last commit, which needs bitcoin/bitcoin#7129.

Part of #2074.
This commit is contained in:
Homu 2021-03-05 12:17:16 +00:00
commit 2aa9fbbdfe
7 changed files with 15 additions and 165 deletions

View File

@ -172,7 +172,6 @@ BITCOIN_CORE_H = \
merkleblock.h \
metrics.h \
miner.h \
mruset.h \
net.h \
netbase.h \
noui.h \

View File

@ -76,7 +76,6 @@ BITCOIN_TESTS =\
test/main_tests.cpp \
test/mempool_tests.cpp \
test/miner_tests.cpp \
test/mruset_tests.cpp \
test/multisig_tests.cpp \
test/net_tests.cpp \
test/netbase_tests.cpp \

View File

@ -5510,8 +5510,7 @@ void static ProcessGetData(CNode* pfrom, const Consensus::Params& consensusParam
// however we MUST always provide at least what the remote peer needs
typedef std::pair<unsigned int, uint256> PairType;
for (PairType& pair : merkleBlock.vMatchedTxn)
if (!pfrom->setInventoryKnown.count(CInv(MSG_TX, pair.second)))
pfrom->PushMessage("tx", block.vtx[pair.first]);
pfrom->PushMessage("tx", block.vtx[pair.first]);
}
// else
// no response
@ -6793,7 +6792,7 @@ bool SendMessages(const Consensus::Params& params, CNode* pto, bool fSendTrickle
vInvWait.reserve(pto->vInventoryToSend.size());
for (const CInv& inv : pto->vInventoryToSend)
{
if (pto->setInventoryKnown.count(inv))
if (inv.type == MSG_TX && pto->filterInventoryKnown.contains(inv.hash))
continue;
// trickle out tx inv to protect privacy
@ -6814,15 +6813,13 @@ bool SendMessages(const Consensus::Params& params, CNode* pto, bool fSendTrickle
}
}
// returns true if wasn't already contained in the set
if (pto->setInventoryKnown.insert(inv).second)
pto->filterInventoryKnown.insert(inv.hash);
vInv.push_back(inv);
if (vInv.size() >= 1000)
{
vInv.push_back(inv);
if (vInv.size() >= 1000)
{
pto->PushMessage("inv", vInv);
vInv.clear();
}
pto->PushMessage("inv", vInv);
vInv.clear();
}
}
pto->vInventoryToSend = vInvWait;

View File

@ -1,65 +0,0 @@
// Copyright (c) 2012-2015 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or https://www.opensource.org/licenses/mit-license.php .
#ifndef BITCOIN_MRUSET_H
#define BITCOIN_MRUSET_H
#include <set>
#include <vector>
#include <utility>
/** STL-like set container that only keeps the most recent N elements. */
template <typename T>
class mruset
{
public:
typedef T key_type;
typedef T value_type;
typedef typename std::set<T>::iterator iterator;
typedef typename std::set<T>::const_iterator const_iterator;
typedef typename std::set<T>::size_type size_type;
protected:
std::set<T> set;
std::vector<iterator> order;
size_type first_used;
size_type first_unused;
const size_type nMaxSize;
public:
mruset(size_type nMaxSizeIn = 1) : nMaxSize(nMaxSizeIn) { clear(); }
iterator begin() const { return set.begin(); }
iterator end() const { return set.end(); }
size_type size() const { return set.size(); }
bool empty() const { return set.empty(); }
iterator find(const key_type& k) const { return set.find(k); }
size_type count(const key_type& k) const { return set.count(k); }
void clear()
{
set.clear();
order.assign(nMaxSize, set.end());
first_used = 0;
first_unused = 0;
}
bool inline friend operator==(const mruset<T>& a, const mruset<T>& b) { return a.set == b.set; }
bool inline friend operator==(const mruset<T>& a, const std::set<T>& b) { return a.set == b; }
bool inline friend operator<(const mruset<T>& a, const mruset<T>& b) { return a.set < b.set; }
std::pair<iterator, bool> insert(const key_type& x)
{
std::pair<iterator, bool> ret = set.insert(x);
if (ret.second) {
if (set.size() == nMaxSize + 1) {
set.erase(order[first_used]);
order[first_used] = set.end();
if (++first_used == nMaxSize) first_used = 0;
}
order[first_unused] = ret.first;
if (++first_unused == nMaxSize) first_unused = 0;
}
return ret;
}
size_type max_size() const { return nMaxSize; }
};
#endif // BITCOIN_MRUSET_H

View File

@ -2150,7 +2150,7 @@ CNode::CNode(SOCKET hSocketIn, const CAddress& addrIn, const std::string& addrNa
addr(addrIn),
nKeyedNetGroup(CalculateKeyedNetGroup(addrIn)),
addrKnown(5000, 0.001),
setInventoryKnown(SendBufferSize() / 1000)
filterInventoryKnown(50000, 0.000001)
{
nServices = 0;
hSocket = hSocketIn;
@ -2176,6 +2176,7 @@ CNode::CNode(SOCKET hSocketIn, const CAddress& addrIn, const std::string& addrNa
nSendOffset = 0;
hashContinue = uint256();
nStartingHeight = -1;
filterInventoryKnown.reset();
fGetAddr = false;
fRelayTxes = false;
fSentAddr = false;

View File

@ -11,7 +11,6 @@
#include "compat.h"
#include "fs.h"
#include "limitedmap.h"
#include "mruset.h"
#include "netbase.h"
#include "protocol.h"
#include "random.h"
@ -334,7 +333,7 @@ public:
std::set<uint256> setKnown;
// inventory based relay
mruset<CInv> setInventoryKnown;
CRollingBloomFilter filterInventoryKnown;
std::vector<CInv> vInventoryToSend;
CCriticalSection cs_inventory;
std::set<uint256> setAskFor;
@ -446,7 +445,7 @@ public:
{
{
LOCK(cs_inventory);
setInventoryKnown.insert(inv);
filterInventoryKnown.insert(inv.hash);
}
}
@ -454,8 +453,9 @@ public:
{
{
LOCK(cs_inventory);
if (!setInventoryKnown.count(inv))
vInventoryToSend.push_back(inv);
if (inv.type == MSG_TX && filterInventoryKnown.contains(inv.hash))
return;
vInventoryToSend.push_back(inv);
}
}

View File

@ -1,81 +0,0 @@
// Copyright (c) 2012-2013 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or https://www.opensource.org/licenses/mit-license.php .
#include "mruset.h"
#include "random.h"
#include "util.h"
#include "test/test_bitcoin.h"
#include <set>
#include <boost/test/unit_test.hpp>
#define NUM_TESTS 16
#define MAX_SIZE 100
using namespace std;
BOOST_FIXTURE_TEST_SUITE(mruset_tests, BasicTestingSetup)
BOOST_AUTO_TEST_CASE(mruset_test)
{
// The mruset being tested.
mruset<int> mru(5000);
// Run the test 10 times.
for (int test = 0; test < 10; test++) {
// Reset mru.
mru.clear();
// A deque + set to simulate the mruset.
std::deque<int> rep;
std::set<int> all;
// Insert 10000 random integers below 15000.
for (int j=0; j<10000; j++) {
int add = GetRandInt(15000);
mru.insert(add);
// Add the number to rep/all as well.
if (all.count(add) == 0) {
all.insert(add);
rep.push_back(add);
if (all.size() == 5001) {
all.erase(rep.front());
rep.pop_front();
}
}
// Do a full comparison between mru and the simulated mru every 1000 and every 5001 elements.
if (j % 1000 == 0 || j % 5001 == 0) {
mruset<int> mru2 = mru; // Also try making a copy
// Check that all elements that should be in there, are in there.
for (int x : rep) {
BOOST_CHECK(mru.count(x));
BOOST_CHECK(mru2.count(x));
}
// Check that all elements that are in there, should be in there.
for (int x : mru) {
BOOST_CHECK(all.count(x));
}
// Check that all elements that are in there, should be in there.
for (int x : mru2) {
BOOST_CHECK(all.count(x));
}
for (int t = 0; t < 10; t++) {
int r = GetRandInt(15000);
BOOST_CHECK(all.count(r) == mru.count(r));
BOOST_CHECK(all.count(r) == mru2.count(r));
}
}
}
}
}
BOOST_AUTO_TEST_SUITE_END()