Skip to content

Commit

Permalink
An adapted version of btc@b5599147533103efea896a1fc4ff51f2d3ad5808
Browse files Browse the repository at this point in the history
Move bloom and feerate filtering to just prior to tx sending.

This will avoid sending more pointless INVs around updates, and
prevents using filter updates to timetag transactions.

Also adds locking for fRelayTxes.
  • Loading branch information
furszy committed Jan 18, 2021
1 parent cb4fc6c commit d10583b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ class CNode
// a) it allows us to not relay tx invs before receiving the peer's version message
// b) the peer may tell us in their version message that we should not relay tx invs
// until they have initialized their bloom filter.
bool fRelayTxes;
bool fRelayTxes; //protected by cs_filter
CSemaphoreGrant grantOutbound;
RecursiveMutex cs_filter;
CBloomFilter* pfilter;
Expand Down
29 changes: 24 additions & 5 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1053,10 +1053,9 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR
vRecv >> LIMITED_STRING(strSubVer, MAX_SUBVERSION_LENGTH);
cleanSubVer = SanitizeString(strSubVer);
}
if (!vRecv.empty())
if (!vRecv.empty()) {
vRecv >> nStartingHeight;
if (!vRecv.empty())
vRecv >> fRelay;
}

// Disconnect if we connected to ourself
if (pfrom->fInbound && !connman.CheckIncomingNonce(nNonce)) {
Expand Down Expand Up @@ -1084,9 +1083,14 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR
}
pfrom->nStartingHeight = nStartingHeight;
pfrom->fClient = !(nServices & NODE_NETWORK);

{
LOCK(pfrom->cs_filter);
pfrom->fRelayTxes = fRelay; // set to true after we get the first filter* message
if (!vRecv.empty()) {
vRecv >> pfrom->fRelayTxes; // set to true after we get the first filter* message
} else {
pfrom->fRelayTxes = true;
}
}

// Change version
Expand Down Expand Up @@ -1729,12 +1733,13 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR
CBloomFilter filter;
vRecv >> filter;

LOCK(pfrom->cs_filter);

if (!filter.IsWithinSizeConstraints()) {
// There is no excuse for sending a too-large filter
LOCK(cs_main);
Misbehaving(pfrom->GetId(), 100);
} else {
LOCK(pfrom->cs_filter);
delete pfrom->pfilter;
pfrom->pfilter = new CBloomFilter(filter);
pfrom->pfilter->UpdateEmptyFull();
Expand Down Expand Up @@ -2095,6 +2100,12 @@ bool SendMessages(CNode* pto, CConnman& connman, std::atomic<bool>& interruptMsg
pto->nNextInvSend = PoissonNextSend(nNow, INVENTORY_BROADCAST_INTERVAL >> !pto->fInbound);
}

// Time to send but the peer has requested we not relay transactions.
if (fSendTrickle) {
LOCK(pto->cs_filter);
if (!pto->fRelayTxes) pto->setInventoryTxToSend.clear();
}

// Respond to BIP35 mempool requests
if (fSendTrickle && pto->fSendMempool) {
std::vector<uint256> vtxid;
Expand Down Expand Up @@ -2137,6 +2148,7 @@ bool SendMessages(CNode* pto, CConnman& connman, std::atomic<bool>& interruptMsg
// No reason to drain out at many times the network's capacity,
// especially since we have many peers and some will draw much shorter delays.
unsigned int nRelayedTransactions = 0;
LOCK(pto->cs_filter);
while (!vInvTx.empty() && nRelayedTransactions < INVENTORY_BROADCAST_MAX) {
// Fetch the top element from the heap
std::pop_heap(vInvTx.begin(), vInvTx.end(), compareInvMempoolOrder);
Expand All @@ -2149,6 +2161,13 @@ bool SendMessages(CNode* pto, CConnman& connman, std::atomic<bool>& interruptMsg
if (pto->filterInventoryKnown.contains(hash)) {
continue;
}
// Not in the mempool anymore? don't bother sending it.
// todo: back port feerate filter.
if (pto->pfilter) {
CTransaction tx;
if (!mempool.lookup(hash, tx)) continue;
if (!pto->pfilter->IsRelevantAndUpdate(tx)) continue;
}
// Send
vInv.emplace_back(CInv(MSG_TX, hash));
nRelayedTransactions++;
Expand Down

0 comments on commit d10583b

Please sign in to comment.