Skip to content

Commit 4c0731f

Browse files
sdaftuarajtowns
andcommitted
Deduplicate missing parents of orphan transactions
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 <[email protected]>
1 parent 8196176 commit 4c0731f

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/net_processing.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2990,8 +2990,19 @@ void ProcessMessage(
29902990
else if (state.GetResult() == TxValidationResult::TX_MISSING_INPUTS)
29912991
{
29922992
bool fRejectedParents = false; // It may be the case that the orphans parents have all been rejected
2993+
2994+
// Deduplicate parent txids, so that we don't have to loop over
2995+
// the same parent txid more than once down below.
2996+
std::vector<uint256> unique_parents;
2997+
unique_parents.reserve(tx.vin.size());
29932998
for (const CTxIn& txin : tx.vin) {
2994-
if (recentRejects->contains(txin.prevout.hash)) {
2999+
// We start with all parents, and then remove duplicates below.
3000+
unique_parents.push_back(txin.prevout.hash);
3001+
}
3002+
std::sort(unique_parents.begin(), unique_parents.end());
3003+
unique_parents.erase(std::unique(unique_parents.begin(), unique_parents.end()), unique_parents.end());
3004+
for (const uint256& parent_txid : unique_parents) {
3005+
if (recentRejects->contains(parent_txid)) {
29953006
fRejectedParents = true;
29963007
break;
29973008
}
@@ -3000,14 +3011,14 @@ void ProcessMessage(
30003011
uint32_t nFetchFlags = GetFetchFlags(pfrom);
30013012
const auto current_time = GetTime<std::chrono::microseconds>();
30023013

3003-
for (const CTxIn& txin : tx.vin) {
3014+
for (const uint256& parent_txid : unique_parents) {
30043015
// Here, we only have the txid (and not wtxid) of the
30053016
// inputs, so we only request in txid mode, even for
30063017
// wtxidrelay peers.
30073018
// Eventually we should replace this with an improved
30083019
// protocol for getting all unconfirmed parents.
3009-
CInv _inv(MSG_TX | nFetchFlags, txin.prevout.hash);
3010-
pfrom.AddKnownTx(txin.prevout.hash);
3020+
CInv _inv(MSG_TX | nFetchFlags, parent_txid);
3021+
pfrom.AddKnownTx(parent_txid);
30113022
if (!AlreadyHave(_inv, mempool)) RequestTx(State(pfrom.GetId()), ToGenTxid(_inv), current_time);
30123023
}
30133024
AddOrphanTx(ptx, pfrom.GetId());

0 commit comments

Comments
 (0)