Skip to content

Commit f497414

Browse files
committed
[refactor] put peerman tasks at the end of ProcessInvalidTx
1 parent 6797bc4 commit f497414

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

src/net_processing.cpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ class PeerManagerImpl final : public PeerManager
573573
NodeId parent_sender,
574574
NodeId child_sender) :
575575
m_txns{parent, child},
576-
m_senders {parent_sender, child_sender}
576+
m_senders{parent_sender, child_sender}
577577
{}
578578

579579
// Move ctor
@@ -3033,6 +3033,11 @@ std::optional<PeerManagerImpl::PackageToValidate> PeerManagerImpl::ProcessInvali
30333033
ptx->GetWitnessHash().ToString(),
30343034
nodeid,
30353035
state.ToString());
3036+
3037+
// Whether we should call AddToCompactExtraTransactions at the end
3038+
bool add_extra_compact_tx{first_time_failure};
3039+
// Hashes to pass to AddKnownTx later
3040+
std::vector<uint256> unique_parents;
30363041
// Populated if failure is reconsiderable and eligible package is found.
30373042
std::optional<PackageToValidate> package_to_validate;
30383043

@@ -3044,7 +3049,6 @@ std::optional<PeerManagerImpl::PackageToValidate> PeerManagerImpl::ProcessInvali
30443049

30453050
// Deduplicate parent txids, so that we don't have to loop over
30463051
// the same parent txid more than once down below.
3047-
std::vector<uint256> unique_parents;
30483052
unique_parents.reserve(tx.vin.size());
30493053
for (const CTxIn& txin : tx.vin) {
30503054
// We start with all parents, and then remove duplicates below.
@@ -3081,17 +3085,15 @@ std::optional<PeerManagerImpl::PackageToValidate> PeerManagerImpl::ProcessInvali
30813085
// Eventually we should replace this with an improved
30823086
// protocol for getting all unconfirmed parents.
30833087
const auto gtxid{GenTxid::Txid(parent_txid)};
3084-
if (peer) AddKnownTx(*peer, parent_txid);
30853088
// Exclude m_lazy_recent_rejects_reconsiderable: the missing parent may have been
30863089
// previously rejected for being too low feerate. This orphan might CPFP it.
30873090
if (!m_txdownloadman.AlreadyHaveTx(gtxid, /*include_reconsiderable=*/false)) {
30883091
m_txdownloadman.AddTxAnnouncement(nodeid, gtxid, current_time, /*p2p_inv=*/false);
30893092
}
30903093
}
30913094

3092-
if (m_orphanage.AddTx(ptx, nodeid) && RecursiveDynamicUsage(*ptx) < 100000) {
3093-
AddToCompactExtraTransactions(ptx);
3094-
}
3095+
// Potentially flip add_extra_compact_tx to false if AddTx returns false because the tx was already there
3096+
add_extra_compact_tx &= m_orphanage.AddTx(ptx, nodeid);
30953097

30963098
// Once added to the orphan pool, a tx is considered AlreadyHave, and we shouldn't request it anymore.
30973099
m_txrequest.ForgetTxHash(tx.GetHash());
@@ -3100,6 +3102,7 @@ std::optional<PeerManagerImpl::PackageToValidate> PeerManagerImpl::ProcessInvali
31003102
// DoS prevention: do not allow m_orphanage to grow unbounded (see CVE-2012-3789)
31013103
m_orphanage.LimitOrphans(m_opts.max_orphan_txs, m_rng);
31023104
} else {
3105+
unique_parents.clear();
31033106
LogDebug(BCLog::MEMPOOL, "not keeping orphan with rejected parents %s (wtxid=%s)\n",
31043107
tx.GetHash().ToString(),
31053108
tx.GetWitnessHash().ToString());
@@ -3115,8 +3118,9 @@ std::optional<PeerManagerImpl::PackageToValidate> PeerManagerImpl::ProcessInvali
31153118
m_txrequest.ForgetTxHash(tx.GetWitnessHash());
31163119
}
31173120
}
3118-
return std::nullopt;
3119-
} else if (state.GetResult() != TxValidationResult::TX_WITNESS_STRIPPED) {
3121+
} else if (state.GetResult() == TxValidationResult::TX_WITNESS_STRIPPED) {
3122+
add_extra_compact_tx = false;
3123+
} else {
31203124
// We can add the wtxid of this transaction to our reject filter.
31213125
// Do not add txids of witness transactions or witness-stripped
31223126
// transactions to the filter, as they can have been malleated;
@@ -3161,16 +3165,19 @@ std::optional<PeerManagerImpl::PackageToValidate> PeerManagerImpl::ProcessInvali
31613165
RecentRejectsFilter().insert(ptx->GetHash().ToUint256());
31623166
m_txrequest.ForgetTxHash(ptx->GetHash());
31633167
}
3164-
if (first_time_failure && RecursiveDynamicUsage(*ptx) < 100000) {
3165-
AddToCompactExtraTransactions(ptx);
3166-
}
3168+
}
3169+
if (add_extra_compact_tx && RecursiveDynamicUsage(*ptx) < 100000) {
3170+
AddToCompactExtraTransactions(ptx);
3171+
}
3172+
for (const uint256& parent_txid : unique_parents) {
3173+
if (peer) AddKnownTx(*peer, parent_txid);
31673174
}
31683175

31693176
MaybePunishNodeForTx(nodeid, state);
31703177

31713178
// If the tx failed in ProcessOrphanTx, it should be removed from the orphanage unless the
31723179
// tx was still missing inputs. If the tx was not in the orphanage, EraseTx does nothing and returns 0.
3173-
if (Assume(state.GetResult() != TxValidationResult::TX_MISSING_INPUTS) && m_orphanage.EraseTx(ptx->GetWitnessHash()) > 0) {
3180+
if (state.GetResult() != TxValidationResult::TX_MISSING_INPUTS && m_orphanage.EraseTx(ptx->GetWitnessHash()) > 0) {
31743181
LogDebug(BCLog::TXPACKAGES, " removed orphan tx %s (wtxid=%s)\n", ptx->GetHash().ToString(), ptx->GetWitnessHash().ToString());
31753182
}
31763183

0 commit comments

Comments
 (0)