mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-06-01 00:34:01 +02:00
Move all g_cs_orphans locking to txorphanage
This commit is contained in:
@@ -15,11 +15,11 @@ static constexpr int64_t ORPHAN_TX_EXPIRE_TIME = 20 * 60;
|
||||
/** Minimum time between orphan transactions expire time checks in seconds */
|
||||
static constexpr int64_t ORPHAN_TX_EXPIRE_INTERVAL = 5 * 60;
|
||||
|
||||
RecursiveMutex g_cs_orphans;
|
||||
RecursiveMutex TxOrphanage::g_cs_orphans;
|
||||
|
||||
bool TxOrphanage::AddTx(const CTransactionRef& tx, NodeId peer)
|
||||
{
|
||||
AssertLockHeld(g_cs_orphans);
|
||||
LOCK(g_cs_orphans);
|
||||
|
||||
const uint256& hash = tx->GetHash();
|
||||
if (m_orphans.count(hash))
|
||||
@@ -54,6 +54,12 @@ bool TxOrphanage::AddTx(const CTransactionRef& tx, NodeId peer)
|
||||
}
|
||||
|
||||
int TxOrphanage::EraseTx(const uint256& txid)
|
||||
{
|
||||
LOCK(g_cs_orphans);
|
||||
return _EraseTx(txid);
|
||||
}
|
||||
|
||||
int TxOrphanage::_EraseTx(const uint256& txid)
|
||||
{
|
||||
AssertLockHeld(g_cs_orphans);
|
||||
std::map<uint256, OrphanTx>::iterator it = m_orphans.find(txid);
|
||||
@@ -87,7 +93,7 @@ int TxOrphanage::EraseTx(const uint256& txid)
|
||||
|
||||
void TxOrphanage::EraseForPeer(NodeId peer)
|
||||
{
|
||||
AssertLockHeld(g_cs_orphans);
|
||||
LOCK(g_cs_orphans);
|
||||
|
||||
m_peer_work_set.erase(peer);
|
||||
|
||||
@@ -98,7 +104,7 @@ void TxOrphanage::EraseForPeer(NodeId peer)
|
||||
std::map<uint256, OrphanTx>::iterator maybeErase = iter++; // increment to avoid iterator becoming invalid
|
||||
if (maybeErase->second.fromPeer == peer)
|
||||
{
|
||||
nErased += EraseTx(maybeErase->second.tx->GetHash());
|
||||
nErased += _EraseTx(maybeErase->second.tx->GetHash());
|
||||
}
|
||||
}
|
||||
if (nErased > 0) LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx from peer=%d\n", nErased, peer);
|
||||
@@ -106,7 +112,7 @@ void TxOrphanage::EraseForPeer(NodeId peer)
|
||||
|
||||
void TxOrphanage::LimitOrphans(unsigned int max_orphans)
|
||||
{
|
||||
AssertLockHeld(g_cs_orphans);
|
||||
LOCK(g_cs_orphans);
|
||||
|
||||
unsigned int nEvicted = 0;
|
||||
static int64_t nNextSweep;
|
||||
@@ -120,7 +126,7 @@ void TxOrphanage::LimitOrphans(unsigned int max_orphans)
|
||||
{
|
||||
std::map<uint256, OrphanTx>::iterator maybeErase = iter++;
|
||||
if (maybeErase->second.nTimeExpire <= nNow) {
|
||||
nErased += EraseTx(maybeErase->second.tx->GetHash());
|
||||
nErased += _EraseTx(maybeErase->second.tx->GetHash());
|
||||
} else {
|
||||
nMinExpTime = std::min(maybeErase->second.nTimeExpire, nMinExpTime);
|
||||
}
|
||||
@@ -134,7 +140,7 @@ void TxOrphanage::LimitOrphans(unsigned int max_orphans)
|
||||
{
|
||||
// Evict a random orphan:
|
||||
size_t randompos = rng.randrange(m_orphan_list.size());
|
||||
EraseTx(m_orphan_list[randompos]->first);
|
||||
_EraseTx(m_orphan_list[randompos]->first);
|
||||
++nEvicted;
|
||||
}
|
||||
if (nEvicted > 0) LogPrint(BCLog::MEMPOOL, "orphanage overflow, removed %u tx\n", nEvicted);
|
||||
@@ -142,7 +148,7 @@ void TxOrphanage::LimitOrphans(unsigned int max_orphans)
|
||||
|
||||
void TxOrphanage::AddChildrenToWorkSet(const CTransaction& tx, NodeId peer)
|
||||
{
|
||||
AssertLockHeld(g_cs_orphans);
|
||||
LOCK(g_cs_orphans);
|
||||
|
||||
// Get this peer's work set, emplacing an empty set it didn't exist
|
||||
std::set<uint256>& orphan_work_set = m_peer_work_set.try_emplace(peer).first->second;
|
||||
@@ -169,7 +175,7 @@ bool TxOrphanage::HaveTx(const GenTxid& gtxid) const
|
||||
|
||||
CTransactionRef TxOrphanage::GetTxToReconsider(NodeId peer, NodeId& originator, bool& more)
|
||||
{
|
||||
AssertLockHeld(g_cs_orphans);
|
||||
LOCK(g_cs_orphans);
|
||||
|
||||
auto work_set_it = m_peer_work_set.find(peer);
|
||||
if (work_set_it != m_peer_work_set.end()) {
|
||||
@@ -215,7 +221,7 @@ void TxOrphanage::EraseForBlock(const CBlock& block)
|
||||
if (vOrphanErase.size()) {
|
||||
int nErased = 0;
|
||||
for (const uint256& orphanHash : vOrphanErase) {
|
||||
nErased += EraseTx(orphanHash);
|
||||
nErased += _EraseTx(orphanHash);
|
||||
}
|
||||
LogPrint(BCLog::MEMPOOL, "Erased %d orphan tx included or conflicted by block\n", nErased);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user