Skip to content

Commit

Permalink
Deduplicate missing parents of orphan transactions
Browse files Browse the repository at this point in the history
Summary:
In the logic for requesting missing parents of orphan transactions, parent
transactions with multiple outputs being spent by the given orphan were being
processed multiple times. Fix this by deduplicating the set of missing parent
txids first.

Co-authored-by: Anthony Towns <aj@erisian.com.au>

This is a backport of [[bitcoin/bitcoin#19596 | core#19596]] [2/2]
bitcoin/bitcoin@4c0731f

Depends on D10066

Test Plan: `ninja all check-all`

Reviewers: #bitcoin_abc, majcosta

Reviewed By: #bitcoin_abc, majcosta

Differential Revision: https://reviews.bitcoinabc.org/D10067
  • Loading branch information
sdaftuar authored and PiRK committed Sep 7, 2021
1 parent 009a334 commit bec895a
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3643,21 +3643,33 @@ void PeerManager::ProcessMessage(const Config &config, CNode &pfrom,
// It may be the case that the orphans parents have all been
// rejected.
bool fRejectedParents = false;

// Deduplicate parent txids, so that we don't have to loop over
// the same parent txid more than once down below.
std::vector<TxId> unique_parents;
unique_parents.reserve(tx.vin.size());
for (const CTxIn &txin : tx.vin) {
if (recentRejects->contains(txin.prevout.GetTxId())) {
// We start with all parents, and then remove duplicates below.
unique_parents.push_back(txin.prevout.GetTxId());
}
std::sort(unique_parents.begin(), unique_parents.end());
unique_parents.erase(
std::unique(unique_parents.begin(), unique_parents.end()),
unique_parents.end());
for (const TxId &parent_txid : unique_parents) {
if (recentRejects->contains(parent_txid)) {
fRejectedParents = true;
break;
}
}
if (!fRejectedParents) {
const auto current_time = GetTime<std::chrono::microseconds>();

for (const CTxIn &txin : tx.vin) {
for (const TxId &parent_txid : unique_parents) {
// FIXME: MSG_TX should use a TxHash, not a TxId.
const TxId _txid = txin.prevout.GetTxId();
pfrom.AddKnownTx(_txid);
if (!AlreadyHaveTx(_txid, m_mempool)) {
AddTxAnnouncement(pfrom, _txid, current_time);
pfrom.AddKnownTx(parent_txid);
if (!AlreadyHaveTx(parent_txid, m_mempool)) {
AddTxAnnouncement(pfrom, parent_txid, current_time);
}
}
AddOrphanTx(ptx, pfrom.GetId());
Expand Down

0 comments on commit bec895a

Please sign in to comment.