Merge bitcoin/bitcoin#21845: net processing: Don't require locking cs_main before calling RelayTransactions()

39e19713cd [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 39e19713cd
  promag:
    Code review ACK 39e19713cd, just included sync.h since last review.
  ajtowns:
    ACK 39e19713cd

Tree-SHA512: dc08441233adfb8eaac501cf497cb4bad029eb723bd3fa8a3d8b7e49cc984c98859b95780ad15f5701d62ac745a8223beb0df405e3d49d95a8c86c8be17c9543
This commit is contained in:
fanquake 2021-05-07 10:58:26 +08:00
commit a0d1d487e9
No known key found for this signature in database
GPG key ID: 2EEB9F5CC09526C1
3 changed files with 14 additions and 11 deletions

View file

@ -25,6 +25,7 @@
#include <reverse_iterator.h>
#include <scheduler.h>
#include <streams.h>
#include <sync.h>
#include <tinyformat.h>
#include <txmempool.h>
#include <txorphanage.h>
@ -256,6 +257,9 @@ public:
const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) override;
private:
void _RelayTransaction(const uint256& txid, const uint256& wtxid)
EXCLUSIVE_LOCKS_REQUIRED(cs_main);
/** Consider evicting an outbound peer based on the amount of time they've been behind our tip */
void ConsiderEviction(CNode& pto, int64_t time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
@ -1015,7 +1019,7 @@ void PeerManagerImpl::ReattemptInitialBroadcast(CScheduler& scheduler)
if (tx != nullptr) {
LOCK(cs_main);
RelayTransaction(txid, tx->GetWitnessHash());
_RelayTransaction(txid, tx->GetWitnessHash());
} else {
m_mempool.RemoveUnbroadcastTx(txid, true);
}
@ -1511,6 +1515,11 @@ void PeerManagerImpl::SendPings()
}
void PeerManagerImpl::RelayTransaction(const uint256& txid, const uint256& wtxid)
{
WITH_LOCK(cs_main, _RelayTransaction(txid, wtxid););
}
void PeerManagerImpl::_RelayTransaction(const uint256& txid, const uint256& wtxid)
{
m_connman.ForEachNode([&txid, &wtxid](CNode* pnode) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
AssertLockHeld(::cs_main);
@ -2087,7 +2096,7 @@ void PeerManagerImpl::ProcessOrphanTx(std::set<uint256>& orphan_work_set)
if (result.m_result_type == MempoolAcceptResult::ResultType::VALID) {
LogPrint(BCLog::MEMPOOL, " accepted orphan tx %s\n", orphanHash.ToString());
RelayTransaction(orphanHash, porphanTx->GetWitnessHash());
_RelayTransaction(orphanHash, porphanTx->GetWitnessHash());
m_orphanage.AddChildrenToWorkSet(*porphanTx, orphan_work_set);
m_orphanage.EraseTx(orphanHash);
for (const CTransactionRef& removedTx : result.m_replaced_transactions.value()) {
@ -3055,7 +3064,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
LogPrintf("Not relaying non-mempool transaction %s from forcerelay peer=%d\n", tx.GetHash().ToString(), pfrom.GetId());
} else {
LogPrintf("Force relaying tx %s from peer=%d\n", tx.GetHash().ToString(), pfrom.GetId());
RelayTransaction(tx.GetHash(), tx.GetWitnessHash());
_RelayTransaction(tx.GetHash(), tx.GetWitnessHash());
}
}
return;
@ -3070,7 +3079,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
// requests for it.
m_txrequest.ForgetTxHash(tx.GetHash());
m_txrequest.ForgetTxHash(tx.GetWitnessHash());
RelayTransaction(tx.GetHash(), tx.GetWitnessHash());
_RelayTransaction(tx.GetHash(), tx.GetWitnessHash());
m_orphanage.AddChildrenToWorkSet(tx, peer->m_orphan_work_set);
pfrom.nLastTXTime = GetTime();

View file

@ -7,7 +7,6 @@
#define BITCOIN_NET_PROCESSING_H
#include <net.h>
#include <sync.h>
#include <validationinterface.h>
class CAddrMan;
@ -15,8 +14,6 @@ class CChainParams;
class CTxMemPool;
class ChainstateManager;
extern RecursiveMutex cs_main;
/** Default for -maxorphantx, maximum number of orphan transactions kept in memory */
static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS = 100;
/** Default number of orphan+recently-replaced txn to keep around for block reconstruction */
@ -49,8 +46,7 @@ public:
virtual bool IgnoresIncomingTxs() = 0;
/** Relay transaction to all peers. */
virtual void RelayTransaction(const uint256& txid, const uint256& wtxid)
EXCLUSIVE_LOCKS_REQUIRED(cs_main) = 0;
virtual void RelayTransaction(const uint256& txid, const uint256& wtxid) = 0;
/** Send ping message to all peers */
virtual void SendPings() = 0;

View file

@ -100,8 +100,6 @@ TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef t
// the mempool tracks locally submitted transactions to make a
// best-effort of initial broadcast
node.mempool->AddUnbroadcastTx(hashTx);
LOCK(cs_main);
node.peerman->RelayTransaction(hashTx, tx->GetWitnessHash());
}