Skip to content

Commit

Permalink
[net processing] Add internal _RelayTransactions()
Browse files Browse the repository at this point in the history
Callers of the external RelayTransactions() no longer need to lock cs_main.
  • Loading branch information
jnewbery committed May 4, 2021
1 parent b859361 commit 39e1971
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
17 changes: 13 additions & 4 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -256,6 +257,9 @@ class PeerManagerImpl final : public PeerManager
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);

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand Down
6 changes: 1 addition & 5 deletions src/net_processing.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@
#define BITCOIN_NET_PROCESSING_H

#include <net.h>
#include <sync.h>
#include <validationinterface.h>

class CAddrMan;
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 */
Expand Down Expand Up @@ -49,8 +46,7 @@ class PeerManager : public CValidationInterface, public NetEventsInterface
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;
Expand Down
2 changes: 0 additions & 2 deletions src/node/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down

0 comments on commit 39e1971

Please sign in to comment.