Skip to content

Commit 279c53d

Browse files
committed
[net processing] Move m_recently_announced_invs from CNodeState to Peer
1 parent 938a8e2 commit 279c53d

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/net_processing.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@ struct Peer {
278278
/** A bloom filter for which transactions to announce to the peer. See BIP37. */
279279
std::unique_ptr<CBloomFilter> m_bloom_filter PT_GUARDED_BY(m_bloom_filter_mutex) GUARDED_BY(m_bloom_filter_mutex){nullptr};
280280

281+
/** A rolling bloom filter of all announced tx CInvs to this peer */
282+
CRollingBloomFilter m_recently_announced_invs GUARDED_BY(NetEventsInterface::g_msgproc_mutex){INVENTORY_MAX_RECENT_RELAY, 0.000001};
283+
281284
mutable RecursiveMutex m_tx_inventory_mutex;
282285
/** A filter of all the txids and wtxids that the peer has announced to
283286
* us or we have announced to the peer. We use this to avoid announcing
@@ -314,6 +317,10 @@ struct Peer {
314317
{
315318
return WITH_LOCK(m_tx_relay_mutex, return m_tx_relay.get());
316319
};
320+
const TxRelay* GetTxRelay() const EXCLUSIVE_LOCKS_REQUIRED(!m_tx_relay_mutex)
321+
{
322+
return WITH_LOCK(m_tx_relay_mutex, return m_tx_relay.get());
323+
};
317324

318325
/** A vector of addresses to send to the peer, limited to MAX_ADDR_TO_SEND. */
319326
std::vector<CAddress> m_addrs_to_send GUARDED_BY(NetEventsInterface::g_msgproc_mutex);
@@ -400,7 +407,7 @@ struct Peer {
400407
{}
401408

402409
private:
403-
Mutex m_tx_relay_mutex;
410+
mutable Mutex m_tx_relay_mutex;
404411

405412
/** Transaction relay data. May be a nullptr. */
406413
std::unique_ptr<TxRelay> m_tx_relay GUARDED_BY(m_tx_relay_mutex);
@@ -481,9 +488,6 @@ struct CNodeState {
481488
//! Whether this peer is an inbound connection
482489
const bool m_is_inbound;
483490

484-
//! A rolling bloom filter of all announced tx CInvs to this peer.
485-
CRollingBloomFilter m_recently_announced_invs GUARDED_BY(NetEventsInterface::g_msgproc_mutex){INVENTORY_MAX_RECENT_RELAY, 0.000001};
486-
487491
CNodeState(bool is_inbound) : m_is_inbound(is_inbound) {}
488492
};
489493

@@ -904,7 +908,7 @@ class PeerManagerImpl final : public PeerManager
904908
std::atomic<std::chrono::seconds> m_last_tip_update{0s};
905909

906910
/** Determine whether or not a peer can request a transaction, and return it (or nullptr if not found or not allowed). */
907-
CTransactionRef FindTxForGetData(const CNode& peer, const GenTxid& gtxid, const std::chrono::seconds mempool_req, const std::chrono::seconds now)
911+
CTransactionRef FindTxForGetData(const Peer& peer, const GenTxid& gtxid, const std::chrono::seconds mempool_req, const std::chrono::seconds now)
908912
LOCKS_EXCLUDED(cs_main) EXCLUSIVE_LOCKS_REQUIRED(NetEventsInterface::g_msgproc_mutex);
909913

910914
void ProcessGetData(CNode& pfrom, Peer& peer, const std::atomic<bool>& interruptMsgProc)
@@ -2254,7 +2258,7 @@ void PeerManagerImpl::ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv&
22542258
}
22552259
}
22562260

2257-
CTransactionRef PeerManagerImpl::FindTxForGetData(const CNode& peer, const GenTxid& gtxid, const std::chrono::seconds mempool_req, const std::chrono::seconds now)
2261+
CTransactionRef PeerManagerImpl::FindTxForGetData(const Peer& peer, const GenTxid& gtxid, const std::chrono::seconds mempool_req, const std::chrono::seconds now)
22582262
{
22592263
auto txinfo = m_mempool.info(gtxid);
22602264
if (txinfo.tx) {
@@ -2269,7 +2273,7 @@ CTransactionRef PeerManagerImpl::FindTxForGetData(const CNode& peer, const GenTx
22692273
{
22702274
LOCK(cs_main);
22712275
// Otherwise, the transaction must have been announced recently.
2272-
if (State(peer.GetId())->m_recently_announced_invs.contains(gtxid.GetHash())) {
2276+
if (Assume(peer.GetTxRelay())->m_recently_announced_invs.contains(gtxid.GetHash())) {
22732277
// If it was, it can be relayed from either the mempool...
22742278
if (txinfo.tx) return std::move(txinfo.tx);
22752279
// ... or the relay pool.
@@ -2312,7 +2316,7 @@ void PeerManagerImpl::ProcessGetData(CNode& pfrom, Peer& peer, const std::atomic
23122316
continue;
23132317
}
23142318

2315-
CTransactionRef tx = FindTxForGetData(pfrom, ToGenTxid(inv), mempool_req, now);
2319+
CTransactionRef tx = FindTxForGetData(peer, ToGenTxid(inv), mempool_req, now);
23162320
if (tx) {
23172321
// WTX and WITNESS_TX imply we serialize with witness
23182322
int nSendFlags = (inv.IsMsgTx() ? SERIALIZE_TRANSACTION_NO_WITNESS : 0);
@@ -2336,8 +2340,7 @@ void PeerManagerImpl::ProcessGetData(CNode& pfrom, Peer& peer, const std::atomic
23362340
for (const uint256& parent_txid : parent_ids_to_add) {
23372341
// Relaying a transaction with a recent but unconfirmed parent.
23382342
if (WITH_LOCK(tx_relay->m_tx_inventory_mutex, return !tx_relay->m_tx_inventory_known_filter.contains(parent_txid))) {
2339-
LOCK(cs_main);
2340-
State(pfrom.GetId())->m_recently_announced_invs.insert(parent_txid);
2343+
tx_relay->m_recently_announced_invs.insert(parent_txid);
23412344
}
23422345
}
23432346
} else {
@@ -5687,7 +5690,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
56875690
}
56885691
if (tx_relay->m_bloom_filter && !tx_relay->m_bloom_filter->IsRelevantAndUpdate(*txinfo.tx)) continue;
56895692
// Send
5690-
State(pto->GetId())->m_recently_announced_invs.insert(hash);
5693+
tx_relay->m_recently_announced_invs.insert(hash);
56915694
vInv.push_back(inv);
56925695
nRelayedTransactions++;
56935696
{

0 commit comments

Comments
 (0)