Skip to content

Commit

Permalink
Replace setInventoryKnown with a rolling bloom filter.
Browse files Browse the repository at this point in the history
Mruset setInventoryKnown was reduced to a remarkably small 1000
 entries as a side effect of sendbuffer size reductions in 2012.

This removes setInventoryKnown filtering from merkleBlock responses
 because false positives there are especially unattractive and
 also because I'm not sure if there aren't race conditions around
 the relay pool that would cause some transactions there to
 be suppressed. (Also, ProcessGetData was accessing
 setInventoryKnown without taking the required lock.)
  • Loading branch information
gmaxwell authored and Fuzzbawls committed Jun 23, 2020
1 parent 409aa83 commit 2e3b05c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
9 changes: 4 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5159,8 +5159,7 @@ void static ProcessGetData(CNode* pfrom)
// 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(NetMsgType::TX, block.vtx[pair.first]);
pfrom->PushMessage(NetMsgType::TX, block.vtx[pair.first]);
}
// else
// no response
Expand Down Expand Up @@ -6398,7 +6397,7 @@ bool SendMessages(CNode* pto)
vInv.reserve(pto->vInventoryToSend.size());
vInvWait.reserve(pto->vInventoryToSend.size());
for (const CInv& inv : pto->vInventoryToSend) {
if (pto->setInventoryKnown.count(inv))
if (pto->setInventoryKnown.contains(inv.hash))
continue;

// trickle out tx inv to protect privacy
Expand All @@ -6417,8 +6416,8 @@ bool SendMessages(CNode* pto)
}
}

// returns true if wasn't already contained in the set
if (pto->setInventoryKnown.insert(inv).second) {
if (!pto->setInventoryKnown.contains(inv.hash)) {
pto->setInventoryKnown.insert(inv.hash);
vInv.push_back(inv);
if (vInv.size() >= 1000) {
pto->PushMessage(NetMsgType::INV, vInv);
Expand Down
3 changes: 2 additions & 1 deletion src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2319,7 +2319,7 @@ unsigned int SendBufferSize() { return 1000 * GetArg("-maxsendbuffer", 1 * 1000)
CNode::CNode(SOCKET hSocketIn, CAddress addrIn, std::string addrNameIn, bool fInboundIn) :
ssSend(SER_NETWORK, INIT_PROTO_VERSION),
addrKnown(5000, 0.001),
setInventoryKnown(SendBufferSize() / 1000)
setInventoryKnown(50000, 0.000001)
{
nServices = NODE_NONE;
nServicesExpected = NODE_NONE;
Expand Down Expand Up @@ -2347,6 +2347,7 @@ CNode::CNode(SOCKET hSocketIn, CAddress addrIn, std::string addrNameIn, bool fIn
nSendOffset = 0;
hashContinue = UINT256_ZERO;
nStartingHeight = -1;
setInventoryKnown.reset();
fGetAddr = false;
nNextLocalAddrSend = 0;
nNextAddrSend = 0;
Expand Down
6 changes: 3 additions & 3 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ class CNode
int64_t nNextLocalAddrSend;

// inventory based relay
mruset<CInv> setInventoryKnown;
CRollingBloomFilter setInventoryKnown;
std::vector<CInv> vInventoryToSend;
RecursiveMutex cs_inventory;
std::multimap<int64_t, CInv> mapAskFor;
Expand Down Expand Up @@ -487,15 +487,15 @@ class CNode
{
{
LOCK(cs_inventory);
setInventoryKnown.insert(inv);
setInventoryKnown.insert(inv.hash);
}
}

void PushInventory(const CInv& inv)
{
{
LOCK(cs_inventory);
if (!setInventoryKnown.count(inv))
if (!setInventoryKnown.contains(inv.hash))
vInventoryToSend.push_back(inv);
}
}
Expand Down

0 comments on commit 2e3b05c

Please sign in to comment.