Skip to content

Commit 9a51319

Browse files
committed
Sort mempool by min(feerate, ancestor_feerate)
This more closely approximates the desirability of a given transaction for mining.
1 parent 6773f92 commit 9a51319

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

src/txmempool.h

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -266,27 +266,46 @@ class CompareTxMemPoolEntryByEntryTime
266266
}
267267
};
268268

269+
/** \class CompareTxMemPoolEntryByAncestorScore
270+
*
271+
* Sort an entry by min(score/size of entry's tx, score/size with all ancestors).
272+
*/
269273
class CompareTxMemPoolEntryByAncestorFee
270274
{
271275
public:
272276
bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
273277
{
274-
double aFees = a.GetModFeesWithAncestors();
275-
double aSize = a.GetSizeWithAncestors();
278+
double a_mod_fee, a_size, b_mod_fee, b_size;
276279

277-
double bFees = b.GetModFeesWithAncestors();
278-
double bSize = b.GetSizeWithAncestors();
280+
GetModFeeAndSize(a, a_mod_fee, a_size);
281+
GetModFeeAndSize(b, b_mod_fee, b_size);
279282

280283
// Avoid division by rewriting (a/b > c/d) as (a*d > c*b).
281-
double f1 = aFees * bSize;
282-
double f2 = aSize * bFees;
284+
double f1 = a_mod_fee * b_size;
285+
double f2 = a_size * b_mod_fee;
283286

284287
if (f1 == f2) {
285288
return a.GetTx().GetHash() < b.GetTx().GetHash();
286289
}
287-
288290
return f1 > f2;
289291
}
292+
293+
// Return the fee/size we're using for sorting this entry.
294+
void GetModFeeAndSize(const CTxMemPoolEntry &a, double &mod_fee, double &size) const
295+
{
296+
// Compare feerate with ancestors to feerate of the transaction, and
297+
// return the fee/size for the min.
298+
double f1 = (double)a.GetModifiedFee() * a.GetSizeWithAncestors();
299+
double f2 = (double)a.GetModFeesWithAncestors() * a.GetTxSize();
300+
301+
if (f1 > f2) {
302+
mod_fee = a.GetModFeesWithAncestors();
303+
size = a.GetSizeWithAncestors();
304+
} else {
305+
mod_fee = a.GetModifiedFee();
306+
size = a.GetTxSize();
307+
}
308+
}
290309
};
291310

292311
// Multi_index tag names

0 commit comments

Comments
 (0)