Skip to content

Commit 8d71796

Browse files
committed
[validation] quit RBF logic earlier and separate loops
No behavior change. While we're looking through the descendants and calculating how many transactions we might replace, quit early, as soon as we hit 100. Since we're failing faster, we can also separate the loops - yes, we loop through more times, but this helps us detangle the different BIP125 rules later.
1 parent badb9b1 commit 8d71796

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

src/validation.cpp

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -782,9 +782,6 @@ bool MemPoolAccept::PreChecks(ATMPArgs& args, Workspace& ws)
782782
}
783783
}
784784

785-
// Check if it's economically rational to mine this transaction rather
786-
// than the ones it replaces.
787-
uint64_t nConflictingCount = 0;
788785

789786
// If we don't hold the lock allConflicting might be incomplete; the
790787
// subsequent RemoveStaged() and addUnchecked() calls don't guarantee
@@ -793,7 +790,6 @@ bool MemPoolAccept::PreChecks(ATMPArgs& args, Workspace& ws)
793790
if (fReplacementTransaction)
794791
{
795792
CFeeRate newFeeRate(nModifiedFees, nSize);
796-
std::set<uint256> setConflictsParents;
797793
for (const auto& mi : setIterConflicting) {
798794
// Don't allow the replacement to reduce the feerate of the
799795
// mempool.
@@ -818,33 +814,40 @@ bool MemPoolAccept::PreChecks(ATMPArgs& args, Workspace& ws)
818814
newFeeRate.ToString(),
819815
oldFeeRate.ToString()));
820816
}
817+
}
818+
819+
uint64_t nConflictingCount = 0;
820+
for (const auto& mi : setIterConflicting) {
821+
nConflictingCount += mi->GetCountWithDescendants();
822+
// This potentially overestimates the number of actual descendants
823+
// but we just want to be conservative to avoid doing too much
824+
// work.
825+
if (nConflictingCount > MAX_BIP125_REPLACEMENT_CANDIDATES) {
826+
return state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "too many potential replacements",
827+
strprintf("rejecting replacement %s; too many potential replacements (%d > %d)\n",
828+
hash.ToString(),
829+
nConflictingCount,
830+
MAX_BIP125_REPLACEMENT_CANDIDATES));
831+
}
832+
}
833+
// If not too many to replace, then calculate the set of
834+
// transactions that would have to be evicted
835+
for (CTxMemPool::txiter it : setIterConflicting) {
836+
m_pool.CalculateDescendants(it, allConflicting);
837+
}
838+
// Check if it's economically rational to mine this transaction rather
839+
// than the ones it replaces.
840+
for (CTxMemPool::txiter it : allConflicting) {
841+
nConflictingFees += it->GetModifiedFee();
842+
nConflictingSize += it->GetTxSize();
843+
}
821844

845+
std::set<uint256> setConflictsParents;
846+
for (const auto& mi : setIterConflicting) {
822847
for (const CTxIn &txin : mi->GetTx().vin)
823848
{
824849
setConflictsParents.insert(txin.prevout.hash);
825850
}
826-
827-
nConflictingCount += mi->GetCountWithDescendants();
828-
}
829-
// This potentially overestimates the number of actual descendants
830-
// but we just want to be conservative to avoid doing too much
831-
// work.
832-
if (nConflictingCount <= MAX_BIP125_REPLACEMENT_CANDIDATES) {
833-
// If not too many to replace, then calculate the set of
834-
// transactions that would have to be evicted
835-
for (CTxMemPool::txiter it : setIterConflicting) {
836-
m_pool.CalculateDescendants(it, allConflicting);
837-
}
838-
for (CTxMemPool::txiter it : allConflicting) {
839-
nConflictingFees += it->GetModifiedFee();
840-
nConflictingSize += it->GetTxSize();
841-
}
842-
} else {
843-
return state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "too many potential replacements",
844-
strprintf("rejecting replacement %s; too many potential replacements (%d > %d)\n",
845-
hash.ToString(),
846-
nConflictingCount,
847-
MAX_BIP125_REPLACEMENT_CANDIDATES));
848851
}
849852

850853
for (unsigned int j = 0; j < tx.vin.size(); j++)

0 commit comments

Comments
 (0)