Skip to content

Commit c31f148

Browse files
committed
[refactor] TxOrphanage::EraseTx by wtxid
No behavior change right now, as transactions in the orphanage are unique by txid. This makes the next commit easier to review.
1 parent efcc593 commit c31f148

File tree

4 files changed

+22
-20
lines changed

4 files changed

+22
-20
lines changed

src/net_processing.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3232,7 +3232,7 @@ void PeerManagerImpl::ProcessInvalidTx(NodeId nodeid, const CTransactionRef& ptx
32323232

32333233
// If the tx failed in ProcessOrphanTx, it should be removed from the orphanage unless the
32343234
// tx was still missing inputs. If the tx was not in the orphanage, EraseTx does nothing and returns 0.
3235-
if (Assume(state.GetResult() != TxValidationResult::TX_MISSING_INPUTS) && m_orphanage.EraseTx(ptx->GetHash()) > 0) {
3235+
if (Assume(state.GetResult() != TxValidationResult::TX_MISSING_INPUTS) && m_orphanage.EraseTx(ptx->GetWitnessHash()) > 0) {
32363236
LogDebug(BCLog::TXPACKAGES, " removed orphan tx %s (wtxid=%s)\n", ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString());
32373237
}
32383238
}
@@ -3250,7 +3250,7 @@ void PeerManagerImpl::ProcessValidTx(NodeId nodeid, const CTransactionRef& tx, c
32503250

32513251
m_orphanage.AddChildrenToWorkSet(*tx);
32523252
// If it came from the orphanage, remove it. No-op if the tx is not in txorphanage.
3253-
m_orphanage.EraseTx(tx->GetHash());
3253+
m_orphanage.EraseTx(tx->GetWitnessHash());
32543254

32553255
LogDebug(BCLog::MEMPOOL, "AcceptToMemoryPool: peer=%d: accepted %s (wtxid=%s) (poolsz %u txn, %u kB)\n",
32563256
nodeid,

src/test/fuzz/txorphan.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,12 @@ FUZZ_TARGET(txorphan, .init = initialize_orphanage)
137137
bool have_tx = orphanage.HaveTx(tx->GetWitnessHash());
138138
// EraseTx should return 0 if m_orphans doesn't have the tx
139139
{
140-
Assert(have_tx == orphanage.EraseTx(tx->GetHash()));
140+
Assert(have_tx == orphanage.EraseTx(tx->GetWitnessHash()));
141141
}
142142
have_tx = orphanage.HaveTx(tx->GetWitnessHash());
143143
// have_tx should be false and EraseTx should fail
144144
{
145-
Assert(!have_tx && !orphanage.EraseTx(tx->GetHash()));
145+
Assert(!have_tx && !orphanage.EraseTx(tx->GetWitnessHash()));
146146
}
147147
},
148148
[&] {

src/txorphanage.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,19 @@ bool TxOrphanage::AddTx(const CTransactionRef& tx, NodeId peer)
5454
return true;
5555
}
5656

57-
int TxOrphanage::EraseTx(const Txid& txid)
57+
int TxOrphanage::EraseTx(const Wtxid& wtxid)
5858
{
5959
LOCK(m_mutex);
60-
return EraseTxNoLock(txid);
60+
return EraseTxNoLock(wtxid);
6161
}
6262

63-
int TxOrphanage::EraseTxNoLock(const Txid& txid)
63+
int TxOrphanage::EraseTxNoLock(const Wtxid& wtxid)
6464
{
6565
AssertLockHeld(m_mutex);
66-
std::map<Txid, OrphanTx>::iterator it = m_orphans.find(txid);
66+
auto it_by_wtxid = m_wtxid_to_orphan_it.find(wtxid);
67+
if (it_by_wtxid == m_wtxid_to_orphan_it.end()) return 0;
68+
69+
std::map<Txid, OrphanTx>::iterator it = it_by_wtxid->second;
6770
if (it == m_orphans.end())
6871
return 0;
6972
for (const CTxIn& txin : it->second.tx->vin)
@@ -85,10 +88,10 @@ int TxOrphanage::EraseTxNoLock(const Txid& txid)
8588
m_orphan_list[old_pos] = it_last;
8689
it_last->second.list_pos = old_pos;
8790
}
88-
const auto& wtxid = it->second.tx->GetWitnessHash();
91+
const auto& txid = it->second.tx->GetHash();
8992
LogPrint(BCLog::TXPACKAGES, " removed orphan tx %s (wtxid=%s)\n", txid.ToString(), wtxid.ToString());
9093
m_orphan_list.pop_back();
91-
m_wtxid_to_orphan_it.erase(it->second.tx->GetWitnessHash());
94+
m_wtxid_to_orphan_it.erase(wtxid);
9295

9396
m_orphans.erase(it);
9497
return 1;
@@ -107,7 +110,7 @@ void TxOrphanage::EraseForPeer(NodeId peer)
107110
std::map<Txid, OrphanTx>::iterator maybeErase = iter++; // increment to avoid iterator becoming invalid
108111
if (maybeErase->second.fromPeer == peer)
109112
{
110-
nErased += EraseTxNoLock(maybeErase->second.tx->GetHash());
113+
nErased += EraseTxNoLock(maybeErase->second.tx->GetWitnessHash());
111114
}
112115
}
113116
if (nErased > 0) LogPrint(BCLog::TXPACKAGES, "Erased %d orphan tx from peer=%d\n", nErased, peer);
@@ -129,7 +132,7 @@ void TxOrphanage::LimitOrphans(unsigned int max_orphans, FastRandomContext& rng)
129132
{
130133
std::map<Txid, OrphanTx>::iterator maybeErase = iter++;
131134
if (maybeErase->second.nTimeExpire <= nNow) {
132-
nErased += EraseTxNoLock(maybeErase->second.tx->GetHash());
135+
nErased += EraseTxNoLock(maybeErase->second.tx->GetWitnessHash());
133136
} else {
134137
nMinExpTime = std::min(maybeErase->second.nTimeExpire, nMinExpTime);
135138
}
@@ -142,7 +145,7 @@ void TxOrphanage::LimitOrphans(unsigned int max_orphans, FastRandomContext& rng)
142145
{
143146
// Evict a random orphan:
144147
size_t randompos = rng.randrange(m_orphan_list.size());
145-
EraseTxNoLock(m_orphan_list[randompos]->first);
148+
EraseTxNoLock(m_orphan_list[randompos]->second.tx->GetWitnessHash());
146149
++nEvicted;
147150
}
148151
if (nEvicted > 0) LogPrint(BCLog::TXPACKAGES, "orphanage overflow, removed %u tx\n", nEvicted);
@@ -211,7 +214,7 @@ void TxOrphanage::EraseForBlock(const CBlock& block)
211214
{
212215
LOCK(m_mutex);
213216

214-
std::vector<Txid> vOrphanErase;
217+
std::vector<Wtxid> vOrphanErase;
215218

216219
for (const CTransactionRef& ptx : block.vtx) {
217220
const CTransaction& tx = *ptx;
@@ -222,8 +225,7 @@ void TxOrphanage::EraseForBlock(const CBlock& block)
222225
if (itByPrev == m_outpoint_to_orphan_it.end()) continue;
223226
for (auto mi = itByPrev->second.begin(); mi != itByPrev->second.end(); ++mi) {
224227
const CTransaction& orphanTx = *(*mi)->second.tx;
225-
const auto& orphanHash = orphanTx.GetHash();
226-
vOrphanErase.push_back(orphanHash);
228+
vOrphanErase.push_back(orphanTx.GetWitnessHash());
227229
}
228230
}
229231
}

src/txorphanage.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ class TxOrphanage {
3333
*/
3434
CTransactionRef GetTxToReconsider(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
3535

36-
/** Erase an orphan by txid */
37-
int EraseTx(const Txid& txid) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
36+
/** Erase an orphan by wtxid */
37+
int EraseTx(const Wtxid& wtxid) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
3838

3939
/** Erase all orphans announced by a peer (eg, after that peer disconnects) */
4040
void EraseForPeer(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
@@ -106,8 +106,8 @@ class TxOrphanage {
106106
* transactions using their witness ids. */
107107
std::map<Wtxid, OrphanMap::iterator> m_wtxid_to_orphan_it GUARDED_BY(m_mutex);
108108

109-
/** Erase an orphan by txid */
110-
int EraseTxNoLock(const Txid& txid) EXCLUSIVE_LOCKS_REQUIRED(m_mutex);
109+
/** Erase an orphan by wtxid */
110+
int EraseTxNoLock(const Wtxid& wtxid) EXCLUSIVE_LOCKS_REQUIRED(m_mutex);
111111
};
112112

113113
#endif // BITCOIN_TXORPHANAGE_H

0 commit comments

Comments
 (0)