Skip to content

Commit 82f00de

Browse files
committed
mempool: Pass in -maxmempool instead of referencing gArgs
- Store the mempool size limit (-maxmempool) in CTxMemPool as a member. - Remove the requirement to explicitly specify a mempool size limit for CTxMemPool::GetMinFee(...) and LimitMempoolSize(...), just use the stored mempool size limit where possible. - Remove all now-unnecessary instances of: gArgs.GetIntArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE_MB) * 1000000 The code change in CChainState::GetCoinsCacheSizeState() is correct since the coinscache should not repurpose "extra" mempool memory headroom for itself if the mempool doesn't even exist.
1 parent f1941e8 commit 82f00de

File tree

10 files changed

+25
-16
lines changed

10 files changed

+25
-16
lines changed

src/kernel/mempool_options.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44
#ifndef BITCOIN_KERNEL_MEMPOOL_OPTIONS_H
55
#define BITCOIN_KERNEL_MEMPOOL_OPTIONS_H
66

7+
#include <cstdint>
8+
79
class CBlockPolicyEstimator;
810

11+
/** Default for -maxmempool, maximum megabytes of mempool memory usage */
12+
static constexpr unsigned int DEFAULT_MAX_MEMPOOL_SIZE_MB{300};
13+
914
namespace kernel {
1015
/**
1116
* Options struct containing options for constructing a CTxMemPool. Default
@@ -19,6 +24,7 @@ struct MemPoolOptions {
1924
CBlockPolicyEstimator* estimator{nullptr};
2025
/* The ratio used to determine how often sanity checks will run. */
2126
int check_ratio{0};
27+
int64_t max_size_bytes{DEFAULT_MAX_MEMPOOL_SIZE_MB * 1'000'000};
2228
};
2329
} // namespace kernel
2430

src/mempool_args.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ using kernel::MemPoolOptions;
1313
void ApplyArgsManOptions(const ArgsManager& argsman, MemPoolOptions& mempool_opts)
1414
{
1515
mempool_opts.check_ratio = argsman.GetIntArg("-checkmempool", mempool_opts.check_ratio);
16+
17+
if (auto mb = argsman.GetIntArg("-maxmempool")) mempool_opts.max_size_bytes = *mb * 1'000'000;
1618
}

src/net_processing.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4599,7 +4599,7 @@ void PeerManagerImpl::MaybeSendFeefilter(CNode& pto, Peer& peer, std::chrono::mi
45994599
// transactions to us, regardless of feefilter state.
46004600
if (pto.IsBlockOnlyConn()) return;
46014601

4602-
CAmount currentFilter = m_mempool.GetMinFee(gArgs.GetIntArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE_MB) * 1000000).GetFeePerK();
4602+
CAmount currentFilter = m_mempool.GetMinFee().GetFeePerK();
46034603
static FeeFilterRounder g_filter_rounder{CFeeRate{DEFAULT_MIN_RELAY_TX_FEE}};
46044604

46054605
if (m_chainman.ActiveChainstate().IsInitialBlockDownload()) {

src/node/interfaces.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ class ChainImpl : public Chain
685685
CFeeRate mempoolMinFee() override
686686
{
687687
if (!m_node.mempool) return {};
688-
return m_node.mempool->GetMinFee(gArgs.GetIntArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE_MB) * 1000000);
688+
return m_node.mempool->GetMinFee();
689689
}
690690
CFeeRate relayMinFee() override { return ::minRelayTxFee; }
691691
CFeeRate relayIncrementalFee() override { return ::incrementalRelayFee; }

src/policy/policy.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ static constexpr unsigned int MIN_STANDARD_TX_NONWITNESS_SIZE{82};
3131
static constexpr unsigned int MAX_P2SH_SIGOPS{15};
3232
/** The maximum number of sigops we're willing to relay/mine in a single tx */
3333
static constexpr unsigned int MAX_STANDARD_TX_SIGOPS_COST{MAX_BLOCK_SIGOPS_COST/5};
34-
/** Default for -maxmempool, maximum megabytes of mempool memory usage */
35-
static constexpr unsigned int DEFAULT_MAX_MEMPOOL_SIZE_MB{300};
3634
/** Default for -incrementalrelayfee, which sets the minimum feerate increase for mempool limiting or BIP 125 replacement **/
3735
static constexpr unsigned int DEFAULT_INCREMENTAL_RELAY_FEE{1000};
3836
/** Default for -bytespersigop */

src/rpc/fees.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ static RPCHelpMan estimatesmartfee()
8989
FeeCalculation feeCalc;
9090
CFeeRate feeRate{fee_estimator.estimateSmartFee(conf_target, &feeCalc, conservative)};
9191
if (feeRate != CFeeRate(0)) {
92-
CFeeRate min_mempool_feerate{mempool.GetMinFee(gArgs.GetIntArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE_MB) * 1000000)};
92+
CFeeRate min_mempool_feerate{mempool.GetMinFee()};
9393
CFeeRate min_relay_feerate{::minRelayTxFee};
9494
feeRate = std::max({feeRate, min_mempool_feerate, min_relay_feerate});
9595
result.pushKV("feerate", ValueFromAmount(feeRate.GetFeePerK()));

src/rpc/mempool.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -657,9 +657,8 @@ UniValue MempoolInfoToJSON(const CTxMemPool& pool)
657657
ret.pushKV("bytes", (int64_t)pool.GetTotalTxSize());
658658
ret.pushKV("usage", (int64_t)pool.DynamicMemoryUsage());
659659
ret.pushKV("total_fee", ValueFromAmount(pool.GetTotalFee()));
660-
int64_t maxmempool{gArgs.GetIntArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE_MB) * 1000000};
661-
ret.pushKV("maxmempool", maxmempool);
662-
ret.pushKV("mempoolminfee", ValueFromAmount(std::max(pool.GetMinFee(maxmempool), ::minRelayTxFee).GetFeePerK()));
660+
ret.pushKV("maxmempool", pool.m_max_size_bytes);
661+
ret.pushKV("mempoolminfee", ValueFromAmount(std::max(pool.GetMinFee(), ::minRelayTxFee).GetFeePerK()));
663662
ret.pushKV("minrelaytxfee", ValueFromAmount(::minRelayTxFee.GetFeePerK()));
664663
ret.pushKV("unbroadcastcount", uint64_t{pool.GetUnbroadcastTxs().size()});
665664
return ret;

src/txmempool.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,8 @@ void CTxMemPoolEntry::UpdateAncestorState(int64_t modifySize, CAmount modifyFee,
453453

454454
CTxMemPool::CTxMemPool(const Options& opts)
455455
: m_check_ratio{opts.check_ratio},
456-
minerPolicyEstimator{opts.estimator}
456+
minerPolicyEstimator{opts.estimator},
457+
m_max_size_bytes{opts.max_size_bytes}
457458
{
458459
_clear(); //lock free clear
459460
}

src/txmempool.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,8 @@ class CTxMemPool
564564

565565
using Options = kernel::MemPoolOptions;
566566

567+
const int64_t m_max_size_bytes;
568+
567569
/** Create a new CTxMemPool.
568570
* Sanity checks will be off by default for performance, because otherwise
569571
* accepting transactions becomes O(N^2) where N is the number of transactions
@@ -702,6 +704,9 @@ class CTxMemPool
702704
* takes the fee rate to go back down all the way to 0. When the feerate
703705
* would otherwise be half of this, it is set to 0 instead.
704706
*/
707+
CFeeRate GetMinFee() const {
708+
return GetMinFee(m_max_size_bytes);
709+
}
705710
CFeeRate GetMinFee(size_t sizelimit) const;
706711

707712
/** Remove transactions from the mempool until its dynamic size is <= sizelimit.

src/validation.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ bool CheckSequenceLocksAtTip(CBlockIndex* tip,
255255
// Returns the script flags which should be checked for a given block
256256
static unsigned int GetBlockScriptFlags(const CBlockIndex& block_index, const ChainstateManager& chainman);
257257

258-
static void LimitMempoolSize(CTxMemPool& pool, CCoinsViewCache& coins_cache, size_t limit, std::chrono::seconds age)
258+
static void LimitMempoolSize(CTxMemPool& pool, CCoinsViewCache& coins_cache, std::chrono::seconds age)
259259
EXCLUSIVE_LOCKS_REQUIRED(::cs_main, pool.cs)
260260
{
261261
AssertLockHeld(::cs_main);
@@ -266,7 +266,7 @@ static void LimitMempoolSize(CTxMemPool& pool, CCoinsViewCache& coins_cache, siz
266266
}
267267

268268
std::vector<COutPoint> vNoSpendsRemaining;
269-
pool.TrimToSize(limit, &vNoSpendsRemaining);
269+
pool.TrimToSize(pool.m_max_size_bytes, &vNoSpendsRemaining);
270270
for (const COutPoint& removed : vNoSpendsRemaining)
271271
coins_cache.Uncache(removed);
272272
}
@@ -377,7 +377,6 @@ void CChainState::MaybeUpdateMempoolForReorg(
377377
LimitMempoolSize(
378378
*m_mempool,
379379
this->CoinsTip(),
380-
gArgs.GetIntArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE_MB) * 1000000,
381380
std::chrono::hours{gArgs.GetIntArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY)});
382381
}
383382

@@ -644,7 +643,7 @@ class MemPoolAccept
644643
{
645644
AssertLockHeld(::cs_main);
646645
AssertLockHeld(m_pool.cs);
647-
CAmount mempoolRejectFee = m_pool.GetMinFee(gArgs.GetIntArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE_MB) * 1000000).GetFee(package_size);
646+
CAmount mempoolRejectFee = m_pool.GetMinFee().GetFee(package_size);
648647
if (mempoolRejectFee > 0 && package_fee < mempoolRejectFee) {
649648
return state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "mempool min fee not met", strprintf("%d < %d", package_fee, mempoolRejectFee));
650649
}
@@ -1082,7 +1081,7 @@ bool MemPoolAccept::Finalize(const ATMPArgs& args, Workspace& ws)
10821081
// in the package. LimitMempoolSize() should be called at the very end to make sure the mempool
10831082
// is still within limits and package submission happens atomically.
10841083
if (!args.m_package_submission && !bypass_limits) {
1085-
LimitMempoolSize(m_pool, m_active_chainstate.CoinsTip(), gArgs.GetIntArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE_MB) * 1000000, std::chrono::hours{gArgs.GetIntArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY)});
1084+
LimitMempoolSize(m_pool, m_active_chainstate.CoinsTip(), std::chrono::hours{gArgs.GetIntArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY)});
10861085
if (!m_pool.exists(GenTxid::Txid(hash)))
10871086
return state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "mempool full");
10881087
}
@@ -1148,7 +1147,6 @@ bool MemPoolAccept::SubmitPackage(const ATMPArgs& args, std::vector<Workspace>&
11481147
// It may or may not be the case that all the transactions made it into the mempool. Regardless,
11491148
// make sure we haven't exceeded max mempool size.
11501149
LimitMempoolSize(m_pool, m_active_chainstate.CoinsTip(),
1151-
gArgs.GetIntArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE_MB) * 1000000,
11521150
std::chrono::hours{gArgs.GetIntArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY)});
11531151

11541152
// Find the wtxids of the transactions that made it into the mempool. Allow partial submission,
@@ -2292,7 +2290,7 @@ CoinsCacheSizeState CChainState::GetCoinsCacheSizeState()
22922290
AssertLockHeld(::cs_main);
22932291
return this->GetCoinsCacheSizeState(
22942292
m_coinstip_cache_size_bytes,
2295-
gArgs.GetIntArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE_MB) * 1000000);
2293+
m_mempool ? m_mempool->m_max_size_bytes : 0);
22962294
}
22972295

22982296
CoinsCacheSizeState CChainState::GetCoinsCacheSizeState(

0 commit comments

Comments
 (0)