Skip to content

Commit 0a22a52

Browse files
committed
Use mempool's ancestor sort in transaction selection
Transaction selection for mining tracks ancestor feerates that are modified based on transactions that have already been selected. This commit de-duplicates the code so that the ancestor feerate sorting used by the mempool can also be directly applied to the miner.
1 parent 7abfa53 commit 0a22a52

File tree

3 files changed

+12
-19
lines changed

3 files changed

+12
-19
lines changed

src/miner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ void BlockAssembler::addPackageTxs(int &nPackagesSelected, int &nDescendantsUpda
352352
// Try to compare the mapTx entry to the mapModifiedTx entry
353353
iter = mempool.mapTx.project<0>(mi);
354354
if (modit != mapModifiedTx.get<ancestor_score>().end() &&
355-
CompareModifiedEntry()(*modit, CTxMemPoolModifiedEntry(iter))) {
355+
CompareTxMemPoolEntryByAncestorFee()(*modit, CTxMemPoolModifiedEntry(iter))) {
356356
// The best entry in mapModifiedTx has higher score
357357
// than the one from mapTx.
358358
// Switch which transaction (package) to consider

src/miner.h

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ struct CTxMemPoolModifiedEntry {
4141
nSigOpCostWithAncestors = entry->GetSigOpCostWithAncestors();
4242
}
4343

44+
int64_t GetModifiedFee() const { return iter->GetModifiedFee(); }
45+
uint64_t GetSizeWithAncestors() const { return nSizeWithAncestors; }
46+
CAmount GetModFeesWithAncestors() const { return nModFeesWithAncestors; }
47+
size_t GetTxSize() const { return iter->GetTxSize(); }
48+
const CTransaction& GetTx() const { return iter->GetTx(); }
49+
4450
CTxMemPool::txiter iter;
4551
uint64_t nSizeWithAncestors;
4652
CAmount nModFeesWithAncestors;
@@ -67,21 +73,6 @@ struct modifiedentry_iter {
6773
}
6874
};
6975

70-
// This matches the calculation in CompareTxMemPoolEntryByAncestorFee,
71-
// except operating on CTxMemPoolModifiedEntry.
72-
// TODO: refactor to avoid duplication of this logic.
73-
struct CompareModifiedEntry {
74-
bool operator()(const CTxMemPoolModifiedEntry &a, const CTxMemPoolModifiedEntry &b) const
75-
{
76-
double f1 = (double)a.nModFeesWithAncestors * b.nSizeWithAncestors;
77-
double f2 = (double)b.nModFeesWithAncestors * a.nSizeWithAncestors;
78-
if (f1 == f2) {
79-
return CTxMemPool::CompareIteratorByHash()(a.iter, b.iter);
80-
}
81-
return f1 > f2;
82-
}
83-
};
84-
8576
// A comparator that sorts transactions based on number of ancestors.
8677
// This is sufficient to sort an ancestor package in an order that is valid
8778
// to appear in a block.
@@ -106,7 +97,7 @@ typedef boost::multi_index_container<
10697
// Reuse same tag from CTxMemPool's similar index
10798
boost::multi_index::tag<ancestor_score>,
10899
boost::multi_index::identity<CTxMemPoolModifiedEntry>,
109-
CompareModifiedEntry
100+
CompareTxMemPoolEntryByAncestorFee
110101
>
111102
>
112103
> indexed_modified_transaction_set;

src/txmempool.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ class CompareTxMemPoolEntryByEntryTime
273273
class CompareTxMemPoolEntryByAncestorFee
274274
{
275275
public:
276-
bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
276+
template<typename T>
277+
bool operator()(const T& a, const T& b) const
277278
{
278279
double a_mod_fee, a_size, b_mod_fee, b_size;
279280

@@ -291,7 +292,8 @@ class CompareTxMemPoolEntryByAncestorFee
291292
}
292293

293294
// Return the fee/size we're using for sorting this entry.
294-
void GetModFeeAndSize(const CTxMemPoolEntry &a, double &mod_fee, double &size) const
295+
template <typename T>
296+
void GetModFeeAndSize(const T &a, double &mod_fee, double &size) const
295297
{
296298
// Compare feerate with ancestors to feerate of the transaction, and
297299
// return the fee/size for the min.

0 commit comments

Comments
 (0)