Skip to content

Commit 67447ba

Browse files
committed
Merge #12225: Mempool cleanups
669c943 Avoid leaking prioritization information when relaying transactions (Suhas Daftuar) e868b22 fee estimator: avoid sorting mempool on shutdown (Suhas Daftuar) 0975406 Correct mempool mapTx comment (Suhas Daftuar) Pull request description: Following up on #12127 and #12118, this cleans up a comment that was left incorrect in txmempool.h, and addresses a couple of the observations @TheBlueMatt made about an unnecessary use of `queryHashes()` and a small information leak when prioritizing transactions. Left undone is nuking queryHashes altogether; that would require changing the behavior of the `getrawmempool` rpc call, which I think I might be in favor of doing, but wanted to save for its own PR. Tree-SHA512: c97d10b96dcd6520459287a4a2eda92774173757695100fcfe61e526aef86f394507c331d17f9e0c14b496c33ec46198a0f165a847762ca50f7c6780b993f162
2 parents d405bee + 669c943 commit 67447ba

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ void Shutdown()
213213

214214
if (fFeeEstimatesInitialized)
215215
{
216-
::feeEstimator.FlushUnconfirmed(::mempool);
216+
::feeEstimator.FlushUnconfirmed();
217217
fs::path est_path = GetDataDir() / FEE_ESTIMATES_FILENAME;
218218
CAutoFile est_fileout(fsbridge::fopen(est_path, "wb"), SER_DISK, CLIENT_VERSION);
219219
if (!est_fileout.IsNull())

src/policy/fees.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -981,16 +981,17 @@ bool CBlockPolicyEstimator::Read(CAutoFile& filein)
981981
return true;
982982
}
983983

984-
void CBlockPolicyEstimator::FlushUnconfirmed(CTxMemPool& pool) {
984+
void CBlockPolicyEstimator::FlushUnconfirmed() {
985985
int64_t startclear = GetTimeMicros();
986-
std::vector<uint256> txids;
987-
pool.queryHashes(txids);
988986
LOCK(cs_feeEstimator);
989-
for (auto& txid : txids) {
990-
removeTx(txid, false);
987+
size_t num_entries = mapMemPoolTxs.size();
988+
// Remove every entry in mapMemPoolTxs
989+
while (!mapMemPoolTxs.empty()) {
990+
auto mi = mapMemPoolTxs.begin();
991+
removeTx(mi->first, false); // this calls erase() on mapMemPoolTxs
991992
}
992993
int64_t endclear = GetTimeMicros();
993-
LogPrint(BCLog::ESTIMATEFEE, "Recorded %u unconfirmed txs from mempool in %gs\n",txids.size(), (endclear - startclear)*0.000001);
994+
LogPrint(BCLog::ESTIMATEFEE, "Recorded %u unconfirmed txs from mempool in %gs\n", num_entries, (endclear - startclear)*0.000001);
994995
}
995996

996997
FeeFilterRounder::FeeFilterRounder(const CFeeRate& minIncrementalFee)

src/policy/fees.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ class CBlockPolicyEstimator
223223
bool Read(CAutoFile& filein);
224224

225225
/** Empty mempool transactions on shutdown to record failure to confirm for txs still in mempool */
226-
void FlushUnconfirmed(CTxMemPool& pool);
226+
void FlushUnconfirmed();
227227

228228
/** Calculation of highest target that estimates are tracked for */
229229
unsigned int HighestTargetTracked(FeeEstimateHorizon horizon) const;

src/txmempool.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,15 +241,18 @@ class CompareTxMemPoolEntryByDescendantScore
241241

242242
/** \class CompareTxMemPoolEntryByScore
243243
*
244-
* Sort by score of entry ((fee+delta)/size) in descending order
244+
* Sort by feerate of entry (fee/size) in descending order
245+
* This is only used for transaction relay, so we use GetFee()
246+
* instead of GetModifiedFee() to avoid leaking prioritization
247+
* information via the sort order.
245248
*/
246249
class CompareTxMemPoolEntryByScore
247250
{
248251
public:
249252
bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
250253
{
251-
double f1 = (double)a.GetModifiedFee() * b.GetTxSize();
252-
double f2 = (double)b.GetModifiedFee() * a.GetTxSize();
254+
double f1 = (double)a.GetFee() * b.GetTxSize();
255+
double f2 = (double)b.GetFee() * a.GetTxSize();
253256
if (f1 == f2) {
254257
return b.GetTx().GetHash() < a.GetTx().GetHash();
255258
}
@@ -379,8 +382,9 @@ class SaltedTxidHasher
379382
*
380383
* mapTx is a boost::multi_index that sorts the mempool on 4 criteria:
381384
* - transaction hash
382-
* - feerate [we use max(feerate of tx, feerate of tx with all descendants)]
385+
* - descendant feerate [we use max(feerate of tx, feerate of tx with all descendants)]
383386
* - time in mempool
387+
* - ancestor feerate [we use min(feerate of tx, feerate of tx with all unconfirmed ancestors)]
384388
*
385389
* Note: the term "descendant" refers to in-mempool transactions that depend on
386390
* this one, while "ancestor" refers to in-mempool transactions that a given

0 commit comments

Comments
 (0)