Skip to content

Commit a0d1d48

Browse files
committed
Merge bitcoin#21845: net processing: Don't require locking cs_main before calling RelayTransactions()
39e1971 [net processing] Add internal _RelayTransactions() (John Newbery) Pull request description: As part of the general effort to reduce cs_main usage in net_processing, this removes the need to be holding `cs_main` when calling `RelayTransactions()` from outside net_processing. Internally, we lock `cs_main` and call an internal `_RelayTransactions()` function that _does_ require `cs_main`. ACKs for top commit: MarcoFalke: re-unsigned-code-review ACK 39e1971 promag: Code review ACK 39e1971, just included sync.h since last review. ajtowns: ACK 39e1971 Tree-SHA512: dc08441233adfb8eaac501cf497cb4bad029eb723bd3fa8a3d8b7e49cc984c98859b95780ad15f5701d62ac745a8223beb0df405e3d49d95a8c86c8be17c9543
2 parents 06d573f + 39e1971 commit a0d1d48

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

src/net_processing.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <reverse_iterator.h>
2626
#include <scheduler.h>
2727
#include <streams.h>
28+
#include <sync.h>
2829
#include <tinyformat.h>
2930
#include <txmempool.h>
3031
#include <txorphanage.h>
@@ -256,6 +257,9 @@ class PeerManagerImpl final : public PeerManager
256257
const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) override;
257258

258259
private:
260+
void _RelayTransaction(const uint256& txid, const uint256& wtxid)
261+
EXCLUSIVE_LOCKS_REQUIRED(cs_main);
262+
259263
/** Consider evicting an outbound peer based on the amount of time they've been behind our tip */
260264
void ConsiderEviction(CNode& pto, int64_t time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
261265

@@ -1015,7 +1019,7 @@ void PeerManagerImpl::ReattemptInitialBroadcast(CScheduler& scheduler)
10151019

10161020
if (tx != nullptr) {
10171021
LOCK(cs_main);
1018-
RelayTransaction(txid, tx->GetWitnessHash());
1022+
_RelayTransaction(txid, tx->GetWitnessHash());
10191023
} else {
10201024
m_mempool.RemoveUnbroadcastTx(txid, true);
10211025
}
@@ -1511,6 +1515,11 @@ void PeerManagerImpl::SendPings()
15111515
}
15121516

15131517
void PeerManagerImpl::RelayTransaction(const uint256& txid, const uint256& wtxid)
1518+
{
1519+
WITH_LOCK(cs_main, _RelayTransaction(txid, wtxid););
1520+
}
1521+
1522+
void PeerManagerImpl::_RelayTransaction(const uint256& txid, const uint256& wtxid)
15141523
{
15151524
m_connman.ForEachNode([&txid, &wtxid](CNode* pnode) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
15161525
AssertLockHeld(::cs_main);
@@ -2087,7 +2096,7 @@ void PeerManagerImpl::ProcessOrphanTx(std::set<uint256>& orphan_work_set)
20872096

20882097
if (result.m_result_type == MempoolAcceptResult::ResultType::VALID) {
20892098
LogPrint(BCLog::MEMPOOL, " accepted orphan tx %s\n", orphanHash.ToString());
2090-
RelayTransaction(orphanHash, porphanTx->GetWitnessHash());
2099+
_RelayTransaction(orphanHash, porphanTx->GetWitnessHash());
20912100
m_orphanage.AddChildrenToWorkSet(*porphanTx, orphan_work_set);
20922101
m_orphanage.EraseTx(orphanHash);
20932102
for (const CTransactionRef& removedTx : result.m_replaced_transactions.value()) {
@@ -3055,7 +3064,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
30553064
LogPrintf("Not relaying non-mempool transaction %s from forcerelay peer=%d\n", tx.GetHash().ToString(), pfrom.GetId());
30563065
} else {
30573066
LogPrintf("Force relaying tx %s from peer=%d\n", tx.GetHash().ToString(), pfrom.GetId());
3058-
RelayTransaction(tx.GetHash(), tx.GetWitnessHash());
3067+
_RelayTransaction(tx.GetHash(), tx.GetWitnessHash());
30593068
}
30603069
}
30613070
return;
@@ -3070,7 +3079,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
30703079
// requests for it.
30713080
m_txrequest.ForgetTxHash(tx.GetHash());
30723081
m_txrequest.ForgetTxHash(tx.GetWitnessHash());
3073-
RelayTransaction(tx.GetHash(), tx.GetWitnessHash());
3082+
_RelayTransaction(tx.GetHash(), tx.GetWitnessHash());
30743083
m_orphanage.AddChildrenToWorkSet(tx, peer->m_orphan_work_set);
30753084

30763085
pfrom.nLastTXTime = GetTime();

src/net_processing.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@
77
#define BITCOIN_NET_PROCESSING_H
88

99
#include <net.h>
10-
#include <sync.h>
1110
#include <validationinterface.h>
1211

1312
class CAddrMan;
1413
class CChainParams;
1514
class CTxMemPool;
1615
class ChainstateManager;
1716

18-
extern RecursiveMutex cs_main;
19-
2017
/** Default for -maxorphantx, maximum number of orphan transactions kept in memory */
2118
static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS = 100;
2219
/** Default number of orphan+recently-replaced txn to keep around for block reconstruction */
@@ -49,8 +46,7 @@ class PeerManager : public CValidationInterface, public NetEventsInterface
4946
virtual bool IgnoresIncomingTxs() = 0;
5047

5148
/** Relay transaction to all peers. */
52-
virtual void RelayTransaction(const uint256& txid, const uint256& wtxid)
53-
EXCLUSIVE_LOCKS_REQUIRED(cs_main) = 0;
49+
virtual void RelayTransaction(const uint256& txid, const uint256& wtxid) = 0;
5450

5551
/** Send ping message to all peers */
5652
virtual void SendPings() = 0;

src/node/transaction.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,6 @@ TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef t
100100
// the mempool tracks locally submitted transactions to make a
101101
// best-effort of initial broadcast
102102
node.mempool->AddUnbroadcastTx(hashTx);
103-
104-
LOCK(cs_main);
105103
node.peerman->RelayTransaction(hashTx, tx->GetWitnessHash());
106104
}
107105

0 commit comments

Comments
 (0)