Skip to content

Commit 8e9e2b4

Browse files
committed
Merge bitcoin/bitcoin#27379: net processing: #26140 follow-ups
3fa4c54 [net processing] Pass TxRelay to FindTxForGetData instead of Peer (dergoegge) c85ee76 [net processin] Don't take cs_main in FindTxForGetData (dergoegge) Pull request description: Addresses left over feedback from #26140. * bitcoin/bitcoin#26140 (comment) * bitcoin/bitcoin#26140 (comment) `mapRelay` is only accessed from the message processing thread and does not need to be kept in sync with anything validation specific, it is therfore perfectly fine to have it guarded by `g_msgproc_mutex`. ACKs for top commit: jnewbery: utACK 3fa4c54 hebasto: ACK 3fa4c54, I have reviewed the code and it looks OK, I agree it can be merged. Tree-SHA512: 3ef84bfe4abfa8d991a7e65d9184221294d80e0df0bbb47f0270ab6ca1593266c98abf83c610f9f86b4d16c7a4b62bcf83f8856c68d3c2e10894bff6ed3e88cd
2 parents bc49a47 + 3fa4c54 commit 8e9e2b4

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

src/net_processing.cpp

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,6 @@ struct Peer {
317317
{
318318
return WITH_LOCK(m_tx_relay_mutex, return m_tx_relay.get());
319319
};
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-
};
324320

325321
/** A vector of addresses to send to the peer, limited to MAX_ADDR_TO_SEND. */
326322
std::vector<CAddress> m_addrs_to_send GUARDED_BY(NetEventsInterface::g_msgproc_mutex);
@@ -908,8 +904,8 @@ class PeerManagerImpl final : public PeerManager
908904
std::atomic<std::chrono::seconds> m_last_tip_update{0s};
909905

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

914910
void ProcessGetData(CNode& pfrom, Peer& peer, const std::atomic<bool>& interruptMsgProc)
915911
EXCLUSIVE_LOCKS_REQUIRED(!m_most_recent_block_mutex, peer.m_getdata_requests_mutex, NetEventsInterface::g_msgproc_mutex)
@@ -920,9 +916,9 @@ class PeerManagerImpl final : public PeerManager
920916

921917
/** Relay map (txid or wtxid -> CTransactionRef) */
922918
typedef std::map<uint256, CTransactionRef> MapRelay;
923-
MapRelay mapRelay GUARDED_BY(cs_main);
919+
MapRelay mapRelay GUARDED_BY(NetEventsInterface::g_msgproc_mutex);
924920
/** Expiration-time ordered list of (expire time, relay map entry) pairs. */
925-
std::deque<std::pair<std::chrono::microseconds, MapRelay::iterator>> g_relay_expiration GUARDED_BY(cs_main);
921+
std::deque<std::pair<std::chrono::microseconds, MapRelay::iterator>> g_relay_expiration GUARDED_BY(NetEventsInterface::g_msgproc_mutex);
926922

927923
/**
928924
* When a peer sends us a valid block, instruct it to announce blocks to us
@@ -2258,7 +2254,7 @@ void PeerManagerImpl::ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv&
22582254
}
22592255
}
22602256

2261-
CTransactionRef PeerManagerImpl::FindTxForGetData(const Peer& peer, const GenTxid& gtxid, const std::chrono::seconds mempool_req, const std::chrono::seconds now)
2257+
CTransactionRef PeerManagerImpl::FindTxForGetData(const Peer::TxRelay& tx_relay, const GenTxid& gtxid, const std::chrono::seconds mempool_req, const std::chrono::seconds now)
22622258
{
22632259
auto txinfo = m_mempool.info(gtxid);
22642260
if (txinfo.tx) {
@@ -2270,16 +2266,13 @@ CTransactionRef PeerManagerImpl::FindTxForGetData(const Peer& peer, const GenTxi
22702266
}
22712267
}
22722268

2273-
{
2274-
LOCK(cs_main);
2275-
// Otherwise, the transaction must have been announced recently.
2276-
if (Assume(peer.GetTxRelay())->m_recently_announced_invs.contains(gtxid.GetHash())) {
2277-
// If it was, it can be relayed from either the mempool...
2278-
if (txinfo.tx) return std::move(txinfo.tx);
2279-
// ... or the relay pool.
2280-
auto mi = mapRelay.find(gtxid.GetHash());
2281-
if (mi != mapRelay.end()) return mi->second;
2282-
}
2269+
// Otherwise, the transaction must have been announced recently.
2270+
if (tx_relay.m_recently_announced_invs.contains(gtxid.GetHash())) {
2271+
// If it was, it can be relayed from either the mempool...
2272+
if (txinfo.tx) return std::move(txinfo.tx);
2273+
// ... or the relay pool.
2274+
auto mi = mapRelay.find(gtxid.GetHash());
2275+
if (mi != mapRelay.end()) return mi->second;
22832276
}
22842277

22852278
return {};
@@ -2316,7 +2309,7 @@ void PeerManagerImpl::ProcessGetData(CNode& pfrom, Peer& peer, const std::atomic
23162309
continue;
23172310
}
23182311

2319-
CTransactionRef tx = FindTxForGetData(peer, ToGenTxid(inv), mempool_req, now);
2312+
CTransactionRef tx = FindTxForGetData(*tx_relay, ToGenTxid(inv), mempool_req, now);
23202313
if (tx) {
23212314
// WTX and WITNESS_TX imply we serialize with witness
23222315
int nSendFlags = (inv.IsMsgTx() ? SERIALIZE_TRANSACTION_NO_WITNESS : 0);

0 commit comments

Comments
 (0)