Skip to content

Commit d829ded

Browse files
committed
add -invertmempool option
1 parent 5f82566 commit d829ded

File tree

6 files changed

+42
-7
lines changed

6 files changed

+42
-7
lines changed

src/init.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ void SetupServerArgs(ArgsManager& argsman)
509509
argsman.AddArg("-loadblock=<file>", "Imports blocks from external file on startup", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
510510
argsman.AddArg("-lowmem=<n>", strprintf("If system available memory falls below <n> MiB, flush caches (0 to disable, default: %s)", g_low_memory_threshold / 1024 / 1024), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
511511
argsman.AddArg("-maxmempool=<n>", strprintf("Keep the transaction memory pool below <n> megabytes (default: %u)", DEFAULT_MAX_MEMPOOL_SIZE_MB), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
512+
argsman.AddArg("-invertmempool", strprintf("Start pruning high fees txs when mempool get full (default: %u)", DEFAULT_INVERT_MEMPOOL), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
512513
argsman.AddArg("-maxorphantx=<n>", strprintf("Keep at most <n> unconnectable transactions in memory (default: %u)", DEFAULT_MAX_ORPHAN_TRANSACTIONS), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
513514
argsman.AddArg("-mempoolexpiry=<n>", strprintf("Do not keep transactions in the mempool longer than <n> hours (default: %u)", DEFAULT_MEMPOOL_EXPIRY_HOURS), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
514515
argsman.AddArg("-minimumchainwork=<hex>", strprintf("Minimum work assumed to exist on a valid chain in hex (default: %s, testnet3: %s, testnet4: %s, signet: %s)", defaultChainParams->GetConsensus().nMinimumChainWork.GetHex(), testnetChainParams->GetConsensus().nMinimumChainWork.GetHex(), testnet4ChainParams->GetConsensus().nMinimumChainWork.GetHex(), signetChainParams->GetConsensus().nMinimumChainWork.GetHex()), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::OPTIONS);

src/kernel/mempool_options.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ enum class TRUCPolicy { Reject, Accept, Enforce };
2222

2323
/** Default for -maxmempool, maximum megabytes of mempool memory usage */
2424
static constexpr unsigned int DEFAULT_MAX_MEMPOOL_SIZE_MB{300};
25+
/** Default for -invertmempool */
26+
static constexpr unsigned int DEFAULT_INVERT_MEMPOOL{false};
2527
/** Default for -maxmempool when blocksonly is set */
2628
static constexpr unsigned int DEFAULT_BLOCKSONLY_MAX_MEMPOOL_SIZE_MB{5};
2729
/** Default for -mempoolexpiry, expiration time for mempool transactions in hours */
@@ -52,6 +54,7 @@ struct MemPoolOptions {
5254
/* The ratio used to determine how often sanity checks will run. */
5355
int check_ratio{0};
5456
int64_t max_size_bytes{DEFAULT_MAX_MEMPOOL_SIZE_MB * 1'000'000};
57+
bool invert_mempool{DEFAULT_INVERT_MEMPOOL};
5558
std::chrono::seconds expiry{std::chrono::hours{DEFAULT_MEMPOOL_EXPIRY_HOURS}};
5659
CFeeRate incremental_relay_feerate{DEFAULT_INCREMENTAL_RELAY_FEE};
5760
/** A fee rate smaller than this is considered zero fee (for relaying, mining and transaction creation) */

src/node/mempool_args.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ util::Result<void> ApplyArgsManOptions(const ArgsManager& argsman, const CChainP
9696
mempool_opts.check_ratio = argsman.GetIntArg("-checkmempool", mempool_opts.check_ratio);
9797

9898
if (auto mb = argsman.GetIntArg("-maxmempool")) mempool_opts.max_size_bytes = *mb * 1'000'000;
99+
100+
if (auto invert = argsman.GetBoolArg("-invertmempool")) mempool_opts.invert_mempool = *invert;
99101

100102
if (auto hours = argsman.GetIntArg("-mempoolexpiry")) mempool_opts.expiry = std::chrono::hours{*hours};
101103

src/rpc/mempool.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,11 @@ UniValue MempoolInfoToJSON(const CTxMemPool& pool, const std::optional<MempoolHi
888888
ret.pushKV("usage", (int64_t)pool.DynamicMemoryUsage());
889889
ret.pushKV("total_fee", ValueFromAmount(pool.GetTotalFee()));
890890
ret.pushKV("maxmempool", pool.m_opts.max_size_bytes);
891-
ret.pushKV("mempoolminfee", ValueFromAmount(std::max(pool.GetMinFee(), pool.m_opts.min_relay_feerate).GetFeePerK()));
891+
if (pool.m_opts.invert_mempool) {
892+
ret.pushKV("mempoolmaxfee", ValueFromAmount(pool.GetMaxFee().GetFeePerK()));
893+
} else {
894+
ret.pushKV("mempoolminfee", ValueFromAmount(std::max(pool.GetMinFee(), pool.m_opts.min_relay_feerate).GetFeePerK()));
895+
}
892896
ret.pushKV("minrelaytxfee", ValueFromAmount(pool.m_opts.min_relay_feerate.GetFeePerK()));
893897
ret.pushKV("incrementalrelayfee", ValueFromAmount(pool.m_opts.incremental_relay_feerate.GetFeePerK()));
894898
ret.pushKV("dustrelayfee", ValueFromAmount(pool.m_opts.dust_relay_feerate.GetFeePerK()));

src/txmempool.cpp

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,18 @@ CFeeRate CTxMemPool::GetMinFee(size_t sizelimit) const {
12221222
return std::max(CFeeRate(llround(rollingMinimumFeeRate)), m_opts.incremental_relay_feerate);
12231223
}
12241224

1225+
CFeeRate CTxMemPool::GetMaxFee(size_t sizelimit) const {
1226+
LOCK(cs);
1227+
CFeeRate maxFeeRate(0);
1228+
for (const auto& entry : mapTx) {
1229+
CFeeRate entryFeeRate(entry.GetModifiedFee(), entry.GetTxSize());
1230+
if (entryFeeRate > maxFeeRate) {
1231+
maxFeeRate = entryFeeRate;
1232+
}
1233+
}
1234+
return maxFeeRate;
1235+
}
1236+
12251237
void CTxMemPool::trackPackageRemoved(const CFeeRate& rate) {
12261238
AssertLockHeld(cs);
12271239
if (rate.GetFeePerK() > rollingMinimumFeeRate) {
@@ -1236,12 +1248,15 @@ void CTxMemPool::TrimToSize(size_t sizelimit, std::vector<COutPoint>* pvNoSpends
12361248
unsigned nTxnRemoved = 0;
12371249
CFeeRate maxFeeRateRemoved(0);
12381250
while (!mapTx.empty() && DynamicMemoryUsage() > sizelimit) {
1239-
indexed_transaction_set::index<descendant_score>::type::iterator it = mapTx.get<descendant_score>().begin();
1251+
indexed_transaction_set::index<descendant_score>::type& index = mapTx.get<descendant_score>();
1252+
indexed_transaction_set::index<descendant_score>::type::iterator it;
1253+
1254+
if (m_opts.invert_mempool) {
1255+
it = std::prev(index.end());
1256+
} else {
1257+
it = index.begin();
1258+
}
12401259

1241-
// We set the new mempool min fee to the feerate of the removed set, plus the
1242-
// "minimum reasonable fee rate" (ie some value under which we consider txn
1243-
// to have 0 fee). This way, we don't allow txn to enter mempool with feerate
1244-
// equal to txn which were removed with no block in between.
12451260
CFeeRate removed(it->GetModFeesWithDescendants(), it->GetSizeWithDescendants());
12461261
removed += m_opts.incremental_relay_feerate;
12471262
trackPackageRemoved(removed);
@@ -1268,9 +1283,13 @@ void CTxMemPool::TrimToSize(size_t sizelimit, std::vector<COutPoint>* pvNoSpends
12681283
}
12691284
}
12701285

1271-
if (maxFeeRateRemoved > CFeeRate(0)) {
1286+
if (maxFeeRateRemoved > CFeeRate(0) && !m_opts.invert_mempool) {
12721287
LogPrint(BCLog::MEMPOOL, "Removed %u txn, rolling minimum fee bumped to %s\n", nTxnRemoved, maxFeeRateRemoved.ToString());
12731288
}
1289+
1290+
if (m_opts.invert_mempool) {
1291+
LogPrint(BCLog::MEMPOOL, "Removed %u txn, rolling maxmimum fee bumped to %s\n", nTxnRemoved, GetMaxFee().ToString());
1292+
}
12741293
}
12751294

12761295
uint64_t CTxMemPool::CalculateDescendantMaximum(txiter entry) const {

src/txmempool.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,8 @@ class CTxMemPool
336336
bool m_load_tried GUARDED_BY(cs){false};
337337

338338
CFeeRate GetMinFee(size_t sizelimit) const;
339+
340+
CFeeRate GetMaxFee(size_t sizelimit) const;
339341

340342
public:
341343

@@ -639,6 +641,10 @@ class CTxMemPool
639641
CFeeRate GetMinFee() const {
640642
return GetMinFee(m_opts.max_size_bytes);
641643
}
644+
645+
CFeeRate GetMaxFee() const {
646+
return GetMaxFee(m_opts.max_size_bytes);
647+
}
642648

643649
/** Remove transactions from the mempool until its dynamic size is <= sizelimit.
644650
* pvNoSpendsRemaining, if set, will be populated with the list of outpoints

0 commit comments

Comments
 (0)