Skip to content

Commit 781e100

Browse files
committed
MOVEONLY: fee checks (Rules 3 and 4) to policy/rbf
1 parent 5210b29 commit 781e100

File tree

3 files changed

+41
-22
lines changed

3 files changed

+41
-22
lines changed

src/policy/rbf.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,29 @@ bool PaysMoreThanConflicts(const CTxMemPool::setEntries& setIterConflicting, CFe
165165
return true;
166166
}
167167

168+
bool PaysForRBF(CAmount nConflictingFees, CAmount nModifiedFees, size_t nSize,
169+
const uint256& hash, std::string& err_string)
170+
{
171+
// The replacement must pay greater fees than the transactions it
172+
// replaces - if we did the bandwidth used by those conflicting
173+
// transactions would not be paid for.
174+
if (nModifiedFees < nConflictingFees)
175+
{
176+
err_string = strprintf("rejecting replacement %s, less fees than conflicting txs; %s < %s",
177+
hash.ToString(), FormatMoney(nModifiedFees), FormatMoney(nConflictingFees));
178+
return false;
179+
}
180+
181+
// Finally in addition to paying more fees than the conflicts the
182+
// new transaction must pay for its own bandwidth.
183+
CAmount nDeltaFees = nModifiedFees - nConflictingFees;
184+
if (nDeltaFees < ::incrementalRelayFee.GetFee(nSize))
185+
{
186+
err_string = strprintf("rejecting replacement %s, not enough additional fees to relay; %s < %s",
187+
hash.ToString(),
188+
FormatMoney(nDeltaFees),
189+
FormatMoney(::incrementalRelayFee.GetFee(nSize)));
190+
return false;
191+
}
192+
return true;
193+
}

src/policy/rbf.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,16 @@ bool EntriesAndTxidsDisjoint(const CTxMemPool::setEntries& setAncestors,
7676
bool PaysMoreThanConflicts(const CTxMemPool::setEntries& setIterConflicting, CFeeRate newFeeRate,
7777
const uint256& hash, std::string& err_string);
7878

79+
/** Enforce BIP125 Rule #3 "The replacement transaction pays an absolute fee of at least the sum
80+
* paid by the original transactions." Enforce BIP125 Rule #4 "The replacement transaction must also
81+
* pay for its own bandwidth at or above the rate set by the node's minimum relay fee setting."
82+
* @param[in] nConflictingFees Total modified fees of original transaction(s).
83+
* @param[in] nModifiedFees Total modified fees of replacement transaction(s).
84+
* @param[in] nSize Total virtual size of replacement transaction(s).
85+
* @param[in] hash Transaction ID, included in the error message if violation occurs.
86+
* returns true if fees are sufficient, false if otherwise.
87+
*/
88+
bool PaysForRBF(CAmount nConflictingFees, CAmount nModifiedFees, size_t nSize,
89+
const uint256& hash, std::string& err_string);
90+
7991
#endif // BITCOIN_POLICY_RBF_H

src/validation.cpp

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -797,32 +797,13 @@ bool MemPoolAccept::PreChecks(ATMPArgs& args, Workspace& ws)
797797
}
798798

799799
// Check if it's economically rational to mine this transaction rather
800-
// than the ones it replaces.
800+
// than the ones it replaces. Enforce Rules #3 and #4.
801801
for (CTxMemPool::txiter it : allConflicting) {
802802
nConflictingFees += it->GetModifiedFee();
803803
nConflictingSize += it->GetTxSize();
804804
}
805-
806-
// The replacement must pay greater fees than the transactions it
807-
// replaces - if we did the bandwidth used by those conflicting
808-
// transactions would not be paid for.
809-
if (nModifiedFees < nConflictingFees)
810-
{
811-
return state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "insufficient fee",
812-
strprintf("rejecting replacement %s, less fees than conflicting txs; %s < %s",
813-
hash.ToString(), FormatMoney(nModifiedFees), FormatMoney(nConflictingFees)));
814-
}
815-
816-
// Finally in addition to paying more fees than the conflicts the
817-
// new transaction must pay for its own bandwidth.
818-
CAmount nDeltaFees = nModifiedFees - nConflictingFees;
819-
if (nDeltaFees < ::incrementalRelayFee.GetFee(nSize))
820-
{
821-
return state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "insufficient fee",
822-
strprintf("rejecting replacement %s, not enough additional fees to relay; %s < %s",
823-
hash.ToString(),
824-
FormatMoney(nDeltaFees),
825-
FormatMoney(::incrementalRelayFee.GetFee(nSize))));
805+
if (!PaysForRBF(nConflictingFees, nModifiedFees, nSize, hash, err_string)) {
806+
return state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "insufficient fee", err_string);
826807
}
827808
}
828809
return true;

0 commit comments

Comments
 (0)