Skip to content

Commit ed70683

Browse files
committed
Handle mempool requests in send loop, subject to trickle
By eliminating queued entries from the mempool response and responding only at trickle time, this makes the mempool no longer leak transaction arrival order information (as the mempool itself is also sorted)-- at least no more than relay itself leaks it.
1 parent dc13dcd commit ed70683

File tree

3 files changed

+49
-28
lines changed

3 files changed

+49
-28
lines changed

src/main.cpp

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5235,34 +5235,9 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
52355235
pfrom->fDisconnect = true;
52365236
return true;
52375237
}
5238-
LOCK2(cs_main, pfrom->cs_filter);
52395238

5240-
std::vector<uint256> vtxid;
5241-
mempool.queryHashes(vtxid);
5242-
vector<CInv> vInv;
5243-
BOOST_FOREACH(uint256& hash, vtxid) {
5244-
CInv inv(MSG_TX, hash);
5245-
if (pfrom->pfilter) {
5246-
CTransaction tx;
5247-
bool fInMemPool = mempool.lookup(hash, tx);
5248-
if (!fInMemPool) continue; // another thread removed since queryHashes, maybe...
5249-
if (!pfrom->pfilter->IsRelevantAndUpdate(tx)) continue;
5250-
}
5251-
if (pfrom->minFeeFilter) {
5252-
CFeeRate feeRate;
5253-
mempool.lookupFeeRate(hash, feeRate);
5254-
LOCK(pfrom->cs_feeFilter);
5255-
if (feeRate.GetFeePerK() < pfrom->minFeeFilter)
5256-
continue;
5257-
}
5258-
vInv.push_back(inv);
5259-
if (vInv.size() == MAX_INV_SZ) {
5260-
pfrom->PushMessage(NetMsgType::INV, vInv);
5261-
vInv.clear();
5262-
}
5263-
}
5264-
if (vInv.size() > 0)
5265-
pfrom->PushMessage(NetMsgType::INV, vInv);
5239+
LOCK(pfrom->cs_inventory);
5240+
pfrom->fSendMempool = true;
52665241
}
52675242

52685243

@@ -5815,13 +5790,52 @@ bool SendMessages(CNode* pto)
58155790
}
58165791
pto->vInventoryBlockToSend.clear();
58175792

5818-
// Determine transactions to relay
5793+
// Check whether periodic sends should happen
58195794
bool fSendTrickle = pto->fWhitelisted;
58205795
if (pto->nNextInvSend < nNow) {
58215796
fSendTrickle = true;
58225797
// Use half the delay for outbound peers, as there is less privacy concern for them.
58235798
pto->nNextInvSend = PoissonNextSend(nNow, INVENTORY_BROADCAST_INTERVAL >> !pto->fInbound);
58245799
}
5800+
5801+
// Respond to BIP35 mempool requests
5802+
if (fSendTrickle && pto->fSendMempool) {
5803+
std::vector<uint256> vtxid;
5804+
mempool.queryHashes(vtxid);
5805+
pto->fSendMempool = false;
5806+
CAmount filterrate = 0;
5807+
{
5808+
LOCK(pto->cs_feeFilter);
5809+
filterrate = pto->minFeeFilter;
5810+
}
5811+
5812+
LOCK(pto->cs_filter);
5813+
5814+
BOOST_FOREACH(const uint256& hash, vtxid) {
5815+
CInv inv(MSG_TX, hash);
5816+
pto->setInventoryTxToSend.erase(hash);
5817+
if (filterrate) {
5818+
CFeeRate feeRate;
5819+
mempool.lookupFeeRate(hash, feeRate);
5820+
if (feeRate.GetFeePerK() < filterrate)
5821+
continue;
5822+
}
5823+
if (pto->pfilter) {
5824+
CTransaction tx;
5825+
bool fInMemPool = mempool.lookup(hash, tx);
5826+
if (!fInMemPool) continue; // another thread removed since queryHashes, maybe...
5827+
if (!pto->pfilter->IsRelevantAndUpdate(tx)) continue;
5828+
}
5829+
pto->filterInventoryKnown.insert(hash);
5830+
vInv.push_back(inv);
5831+
if (vInv.size() == MAX_INV_SZ) {
5832+
pto->PushMessage(NetMsgType::INV, vInv);
5833+
vInv.clear();
5834+
}
5835+
}
5836+
}
5837+
5838+
// Determine transactions to relay
58255839
if (fSendTrickle) {
58265840
// Produce a vector with all candidates for sending
58275841
vector<std::set<uint256>::iterator> vInvTx;
@@ -5851,6 +5865,10 @@ bool SendMessages(CNode* pto)
58515865
// Send
58525866
vInv.push_back(CInv(MSG_TX, hash));
58535867
nRelayedTransactions++;
5868+
if (vInv.size() == MAX_INV_SZ) {
5869+
pto->PushMessage(NetMsgType::INV, vInv);
5870+
vInv.clear();
5871+
}
58545872
pto->filterInventoryKnown.insert(hash);
58555873
}
58565874
}

src/net.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2370,6 +2370,7 @@ CNode::CNode(SOCKET hSocketIn, const CAddress& addrIn, const std::string& addrNa
23702370
hashContinue = uint256();
23712371
nStartingHeight = -1;
23722372
filterInventoryKnown.reset();
2373+
fSendMempool = false;
23732374
fGetAddr = false;
23742375
nNextLocalAddrSend = 0;
23752376
nNextAddrSend = 0;

src/net.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,8 @@ class CNode
411411
// Used for headers announcements - unfiltered blocks to relay
412412
// Also protected by cs_inventory
413413
std::vector<uint256> vBlockHashesToAnnounce;
414+
// Used for BIP35 mempool sending, also protected by cs_inventory
415+
bool fSendMempool;
414416

415417
// Ping time measurement:
416418
// The pong reply we're expecting, or 0 if no pong expected.

0 commit comments

Comments
 (0)