remove obsoleted TxOrphanage::m_mutex

The TxOrphanage is now guarded externally by m_tx_download_mutex.
This commit is contained in:
glozow 2024-05-16 10:32:51 +01:00
parent 61745c7451
commit 6ff84069a5
3 changed files with 20 additions and 48 deletions

View File

@ -21,15 +21,13 @@ BOOST_FIXTURE_TEST_SUITE(orphanage_tests, TestingSetup)
class TxOrphanageTest : public TxOrphanage class TxOrphanageTest : public TxOrphanage
{ {
public: public:
inline size_t CountOrphans() const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex) inline size_t CountOrphans() const
{ {
LOCK(m_mutex);
return m_orphans.size(); return m_orphans.size();
} }
CTransactionRef RandomOrphan() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex) CTransactionRef RandomOrphan()
{ {
LOCK(m_mutex);
std::map<Wtxid, OrphanTx>::iterator it; std::map<Wtxid, OrphanTx>::iterator it;
it = m_orphans.lower_bound(Wtxid::FromUint256(InsecureRand256())); it = m_orphans.lower_bound(Wtxid::FromUint256(InsecureRand256()));
if (it == m_orphans.end()) if (it == m_orphans.end())

View File

@ -20,8 +20,6 @@ static constexpr auto ORPHAN_TX_EXPIRE_INTERVAL{5min};
bool TxOrphanage::AddTx(const CTransactionRef& tx, NodeId peer) bool TxOrphanage::AddTx(const CTransactionRef& tx, NodeId peer)
{ {
LOCK(m_mutex);
const Txid& hash = tx->GetHash(); const Txid& hash = tx->GetHash();
const Wtxid& wtxid = tx->GetWitnessHash(); const Wtxid& wtxid = tx->GetWitnessHash();
if (m_orphans.count(wtxid)) if (m_orphans.count(wtxid))
@ -55,13 +53,11 @@ bool TxOrphanage::AddTx(const CTransactionRef& tx, NodeId peer)
int TxOrphanage::EraseTx(const Wtxid& wtxid) int TxOrphanage::EraseTx(const Wtxid& wtxid)
{ {
LOCK(m_mutex);
return EraseTxNoLock(wtxid); return EraseTxNoLock(wtxid);
} }
int TxOrphanage::EraseTxNoLock(const Wtxid& wtxid) int TxOrphanage::EraseTxNoLock(const Wtxid& wtxid)
{ {
AssertLockHeld(m_mutex);
std::map<Wtxid, OrphanTx>::iterator it = m_orphans.find(wtxid); std::map<Wtxid, OrphanTx>::iterator it = m_orphans.find(wtxid);
if (it == m_orphans.end()) if (it == m_orphans.end())
return 0; return 0;
@ -97,8 +93,6 @@ int TxOrphanage::EraseTxNoLock(const Wtxid& wtxid)
void TxOrphanage::EraseForPeer(NodeId peer) void TxOrphanage::EraseForPeer(NodeId peer)
{ {
LOCK(m_mutex);
m_peer_work_set.erase(peer); m_peer_work_set.erase(peer);
int nErased = 0; int nErased = 0;
@ -116,8 +110,6 @@ void TxOrphanage::EraseForPeer(NodeId peer)
void TxOrphanage::LimitOrphans(unsigned int max_orphans, FastRandomContext& rng) void TxOrphanage::LimitOrphans(unsigned int max_orphans, FastRandomContext& rng)
{ {
LOCK(m_mutex);
unsigned int nEvicted = 0; unsigned int nEvicted = 0;
auto nNow{Now<NodeSeconds>()}; auto nNow{Now<NodeSeconds>()};
if (m_next_sweep <= nNow) { if (m_next_sweep <= nNow) {
@ -150,9 +142,6 @@ void TxOrphanage::LimitOrphans(unsigned int max_orphans, FastRandomContext& rng)
void TxOrphanage::AddChildrenToWorkSet(const CTransaction& tx) void TxOrphanage::AddChildrenToWorkSet(const CTransaction& tx)
{ {
LOCK(m_mutex);
for (unsigned int i = 0; i < tx.vout.size(); i++) { for (unsigned int i = 0; i < tx.vout.size(); i++) {
const auto it_by_prev = m_outpoint_to_orphan_it.find(COutPoint(tx.GetHash(), i)); const auto it_by_prev = m_outpoint_to_orphan_it.find(COutPoint(tx.GetHash(), i));
if (it_by_prev != m_outpoint_to_orphan_it.end()) { if (it_by_prev != m_outpoint_to_orphan_it.end()) {
@ -171,14 +160,11 @@ void TxOrphanage::AddChildrenToWorkSet(const CTransaction& tx)
bool TxOrphanage::HaveTx(const Wtxid& wtxid) const bool TxOrphanage::HaveTx(const Wtxid& wtxid) const
{ {
LOCK(m_mutex);
return m_orphans.count(wtxid); return m_orphans.count(wtxid);
} }
CTransactionRef TxOrphanage::GetTxToReconsider(NodeId peer) CTransactionRef TxOrphanage::GetTxToReconsider(NodeId peer)
{ {
LOCK(m_mutex);
auto work_set_it = m_peer_work_set.find(peer); auto work_set_it = m_peer_work_set.find(peer);
if (work_set_it != m_peer_work_set.end()) { if (work_set_it != m_peer_work_set.end()) {
auto& work_set = work_set_it->second; auto& work_set = work_set_it->second;
@ -197,8 +183,6 @@ CTransactionRef TxOrphanage::GetTxToReconsider(NodeId peer)
bool TxOrphanage::HaveTxToReconsider(NodeId peer) bool TxOrphanage::HaveTxToReconsider(NodeId peer)
{ {
LOCK(m_mutex);
auto work_set_it = m_peer_work_set.find(peer); auto work_set_it = m_peer_work_set.find(peer);
if (work_set_it != m_peer_work_set.end()) { if (work_set_it != m_peer_work_set.end()) {
auto& work_set = work_set_it->second; auto& work_set = work_set_it->second;
@ -209,8 +193,6 @@ bool TxOrphanage::HaveTxToReconsider(NodeId peer)
void TxOrphanage::EraseForBlock(const CBlock& block) void TxOrphanage::EraseForBlock(const CBlock& block)
{ {
LOCK(m_mutex);
std::vector<Wtxid> vOrphanErase; std::vector<Wtxid> vOrphanErase;
for (const CTransactionRef& ptx : block.vtx) { for (const CTransactionRef& ptx : block.vtx) {
@ -239,8 +221,6 @@ void TxOrphanage::EraseForBlock(const CBlock& block)
std::vector<CTransactionRef> TxOrphanage::GetChildrenFromSamePeer(const CTransactionRef& parent, NodeId nodeid) const std::vector<CTransactionRef> TxOrphanage::GetChildrenFromSamePeer(const CTransactionRef& parent, NodeId nodeid) const
{ {
LOCK(m_mutex);
// First construct a vector of iterators to ensure we do not return duplicates of the same tx // First construct a vector of iterators to ensure we do not return duplicates of the same tx
// and so we can sort by nTimeExpire. // and so we can sort by nTimeExpire.
std::vector<OrphanMap::iterator> iters; std::vector<OrphanMap::iterator> iters;
@ -281,8 +261,6 @@ std::vector<CTransactionRef> TxOrphanage::GetChildrenFromSamePeer(const CTransac
std::vector<std::pair<CTransactionRef, NodeId>> TxOrphanage::GetChildrenFromDifferentPeer(const CTransactionRef& parent, NodeId nodeid) const std::vector<std::pair<CTransactionRef, NodeId>> TxOrphanage::GetChildrenFromDifferentPeer(const CTransactionRef& parent, NodeId nodeid) const
{ {
LOCK(m_mutex);
// First construct vector of iterators to ensure we do not return duplicates of the same tx. // First construct vector of iterators to ensure we do not return duplicates of the same tx.
std::vector<OrphanMap::iterator> iters; std::vector<OrphanMap::iterator> iters;

View File

@ -22,55 +22,51 @@
class TxOrphanage { class TxOrphanage {
public: public:
/** Add a new orphan transaction */ /** Add a new orphan transaction */
bool AddTx(const CTransactionRef& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex); bool AddTx(const CTransactionRef& tx, NodeId peer);
/** Check if we already have an orphan transaction (by wtxid only) */ /** Check if we already have an orphan transaction (by wtxid only) */
bool HaveTx(const Wtxid& wtxid) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex); bool HaveTx(const Wtxid& wtxid) const;
/** Extract a transaction from a peer's work set /** Extract a transaction from a peer's work set
* Returns nullptr if there are no transactions to work on. * Returns nullptr if there are no transactions to work on.
* Otherwise returns the transaction reference, and removes * Otherwise returns the transaction reference, and removes
* it from the work set. * it from the work set.
*/ */
CTransactionRef GetTxToReconsider(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex); CTransactionRef GetTxToReconsider(NodeId peer);
/** Erase an orphan by wtxid */ /** Erase an orphan by wtxid */
int EraseTx(const Wtxid& wtxid) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex); int EraseTx(const Wtxid& wtxid);
/** Erase all orphans announced by a peer (eg, after that peer disconnects) */ /** Erase all orphans announced by a peer (eg, after that peer disconnects) */
void EraseForPeer(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex); void EraseForPeer(NodeId peer);
/** Erase all orphans included in or invalidated by a new block */ /** Erase all orphans included in or invalidated by a new block */
void EraseForBlock(const CBlock& block) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex); void EraseForBlock(const CBlock& block);
/** Limit the orphanage to the given maximum */ /** Limit the orphanage to the given maximum */
void LimitOrphans(unsigned int max_orphans, FastRandomContext& rng) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex); void LimitOrphans(unsigned int max_orphans, FastRandomContext& rng);
/** Add any orphans that list a particular tx as a parent into the from peer's work set */ /** Add any orphans that list a particular tx as a parent into the from peer's work set */
void AddChildrenToWorkSet(const CTransaction& tx) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);; void AddChildrenToWorkSet(const CTransaction& tx);
/** Does this peer have any work to do? */ /** Does this peer have any work to do? */
bool HaveTxToReconsider(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);; bool HaveTxToReconsider(NodeId peer);
/** Get all children that spend from this tx and were received from nodeid. Sorted from most /** Get all children that spend from this tx and were received from nodeid. Sorted from most
* recent to least recent. */ * recent to least recent. */
std::vector<CTransactionRef> GetChildrenFromSamePeer(const CTransactionRef& parent, NodeId nodeid) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex); std::vector<CTransactionRef> GetChildrenFromSamePeer(const CTransactionRef& parent, NodeId nodeid) const;
/** Get all children that spend from this tx but were not received from nodeid. Also return /** Get all children that spend from this tx but were not received from nodeid. Also return
* which peer provided each tx. */ * which peer provided each tx. */
std::vector<std::pair<CTransactionRef, NodeId>> GetChildrenFromDifferentPeer(const CTransactionRef& parent, NodeId nodeid) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex); std::vector<std::pair<CTransactionRef, NodeId>> GetChildrenFromDifferentPeer(const CTransactionRef& parent, NodeId nodeid) const;
/** Return how many entries exist in the orphange */ /** Return how many entries exist in the orphange */
size_t Size() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex) size_t Size()
{ {
LOCK(m_mutex);
return m_orphans.size(); return m_orphans.size();
} }
protected: protected:
/** Guards orphan transactions */
mutable Mutex m_mutex;
struct OrphanTx { struct OrphanTx {
CTransactionRef tx; CTransactionRef tx;
NodeId fromPeer; NodeId fromPeer;
@ -80,10 +76,10 @@ protected:
/** Map from wtxid to orphan transaction record. Limited by /** Map from wtxid to orphan transaction record. Limited by
* -maxorphantx/DEFAULT_MAX_ORPHAN_TRANSACTIONS */ * -maxorphantx/DEFAULT_MAX_ORPHAN_TRANSACTIONS */
std::map<Wtxid, OrphanTx> m_orphans GUARDED_BY(m_mutex); std::map<Wtxid, OrphanTx> m_orphans;
/** Which peer provided the orphans that need to be reconsidered */ /** Which peer provided the orphans that need to be reconsidered */
std::map<NodeId, std::set<Wtxid>> m_peer_work_set GUARDED_BY(m_mutex); std::map<NodeId, std::set<Wtxid>> m_peer_work_set;
using OrphanMap = decltype(m_orphans); using OrphanMap = decltype(m_orphans);
@ -98,16 +94,16 @@ protected:
/** Index from the parents' COutPoint into the m_orphans. Used /** Index from the parents' COutPoint into the m_orphans. Used
* to remove orphan transactions from the m_orphans */ * to remove orphan transactions from the m_orphans */
std::map<COutPoint, std::set<OrphanMap::iterator, IteratorComparator>> m_outpoint_to_orphan_it GUARDED_BY(m_mutex); std::map<COutPoint, std::set<OrphanMap::iterator, IteratorComparator>> m_outpoint_to_orphan_it;
/** Orphan transactions in vector for quick random eviction */ /** Orphan transactions in vector for quick random eviction */
std::vector<OrphanMap::iterator> m_orphan_list GUARDED_BY(m_mutex); std::vector<OrphanMap::iterator> m_orphan_list;
/** Erase an orphan by wtxid */ /** Erase an orphan by wtxid */
int EraseTxNoLock(const Wtxid& wtxid) EXCLUSIVE_LOCKS_REQUIRED(m_mutex); int EraseTxNoLock(const Wtxid& wtxid);
/** Timestamp for the next scheduled sweep of expired orphans */ /** Timestamp for the next scheduled sweep of expired orphans */
NodeSeconds m_next_sweep GUARDED_BY(m_mutex){0s}; NodeSeconds m_next_sweep{0s};
}; };
#endif // BITCOIN_TXORPHANAGE_H #endif // BITCOIN_TXORPHANAGE_H