Skip to content

Commit 06a8e9c

Browse files
committed
merge bitcoin#21845: Don't require locking cs_main before calling RelayTransactions()
1 parent 4e39489 commit 06a8e9c

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

src/net_processing.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <reverse_iterator.h>
3030
#include <scheduler.h>
3131
#include <streams.h>
32+
#include <sync.h>
3233
#include <timedata.h>
3334
#include <tinyformat.h>
3435
#include <index/txindex.h>
@@ -644,6 +645,8 @@ class PeerManagerImpl final : public PeerManager
644645
bool IsInvInFilter(NodeId nodeid, const uint256& hash) const override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
645646
void AskPeersForTransaction(const uint256& txid, bool is_masternode) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
646647
private:
648+
void _RelayTransaction(const uint256& txid) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
649+
647650
/** Helpers to process result of external handlers of message */
648651
void ProcessPeerMsgRet(const PeerMsgRet& ret, CNode& pfrom) EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
649652
void PostProcessMessage(MessageProcessingResult&& ret, NodeId node) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
@@ -1628,7 +1631,8 @@ void PeerManagerImpl::ReattemptInitialBroadcast(CScheduler& scheduler)
16281631
CTransactionRef tx = m_mempool.get(txid);
16291632

16301633
if (tx != nullptr) {
1631-
RelayTransaction(txid);
1634+
LOCK(cs_main);
1635+
_RelayTransaction(txid);
16321636
} else {
16331637
m_mempool.RemoveUnbroadcastTx(txid, true);
16341638
}
@@ -2427,6 +2431,11 @@ void PeerManagerImpl::RelayInvFiltered(CInv &inv, const uint256& relatedTxHash,
24272431
}
24282432

24292433
void PeerManagerImpl::RelayTransaction(const uint256& txid)
2434+
{
2435+
WITH_LOCK(cs_main, _RelayTransaction(txid));
2436+
}
2437+
2438+
void PeerManagerImpl::_RelayTransaction(const uint256& txid)
24302439
{
24312440
const CInv inv{m_cj_ctx->dstxman->GetDSTX(txid) ? MSG_DSTX : MSG_TX, txid};
24322441
LOCK(m_peer_mutex);
@@ -3205,7 +3214,7 @@ void PeerManagerImpl::ProcessOrphanTx(std::set<uint256>& orphan_work_set)
32053214

32063215
if (result.m_result_type == MempoolAcceptResult::ResultType::VALID) {
32073216
LogPrint(BCLog::MEMPOOL, " accepted orphan tx %s\n", orphanHash.ToString());
3208-
RelayTransaction(porphanTx->GetHash());
3217+
_RelayTransaction(porphanTx->GetHash());
32093218
m_orphanage.AddChildrenToWorkSet(*porphanTx, orphan_work_set);
32103219
m_orphanage.EraseTx(orphanHash);
32113220
break;
@@ -3488,7 +3497,7 @@ void PeerManagerImpl::PostProcessMessage(MessageProcessingResult&& result, NodeI
34883497
WITH_LOCK(cs_main, EraseObjectRequest(node, result.m_to_erase.value()));
34893498
}
34903499
for (const auto& tx : result.m_transactions) {
3491-
WITH_LOCK(cs_main, RelayTransaction(tx));
3500+
WITH_LOCK(cs_main, _RelayTransaction(tx));
34923501
}
34933502
if (result.m_inventory) {
34943503
RelayInv(result.m_inventory.value());
@@ -4476,7 +4485,7 @@ void PeerManagerImpl::ProcessMessage(
44764485
LogPrintf("Not relaying non-mempool transaction %s from forcerelay peer=%d\n", tx.GetHash().ToString(), pfrom.GetId());
44774486
} else {
44784487
LogPrintf("Force relaying tx %s from peer=%d\n", tx.GetHash().ToString(), pfrom.GetId());
4479-
RelayTransaction(tx.GetHash());
4488+
_RelayTransaction(tx.GetHash());
44804489
}
44814490
}
44824491
return;
@@ -4493,7 +4502,7 @@ void PeerManagerImpl::ProcessMessage(
44934502
m_cj_ctx->dstxman->AddDSTX(dstx);
44944503
}
44954504

4496-
RelayTransaction(tx.GetHash());
4505+
_RelayTransaction(tx.GetHash());
44974506
m_orphanage.AddChildrenToWorkSet(tx, peer->m_orphan_work_set);
44984507

44994508
pfrom.m_last_tx_time = GetTime<std::chrono::seconds>();

src/net_processing.h

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

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

@@ -29,8 +28,6 @@ class CTransaction;
2928
struct CJContext;
3029
struct LLMQContext;
3130

32-
extern RecursiveMutex cs_main;
33-
3431
/** Default for -maxorphantxsize, maximum size in megabytes the orphan map can grow before entries are removed */
3532
static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS_SIZE = 10; // this allows around 100 TXs of max size (and many more of normal size)
3633
/** Default number of orphan+recently-replaced txn to keep around for block reconstruction */
@@ -114,8 +111,7 @@ class PeerManager : public CValidationInterface, public NetEventsInterface
114111
const int minProtoVersion = MIN_PEER_PROTO_VERSION) = 0;
115112

116113
/** Relay transaction to all peers. */
117-
virtual void RelayTransaction(const uint256& txid)
118-
EXCLUSIVE_LOCKS_REQUIRED(cs_main) = 0;
114+
virtual void RelayTransaction(const uint256& txid) = 0;
119115

120116
/** Relay recovered sigs to all interested peers */
121117
virtual void RelayRecoveredSig(const uint256& sigHash) = 0;

src/node/transaction.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef t
110110
}
111111

112112
if (relay) {
113-
LOCK(cs_main);
114113
node.peerman->RelayTransaction(txid);
115114
}
116115

0 commit comments

Comments
 (0)