Skip to content

Commit aa9141c

Browse files
committed
mempool: Pass in -mempoolexpiry instead of referencing gArgs
- Store the mempool expiry (-mempoolexpiry) in CTxMemPool as a std::chrono::seconds member. - Remove the requirement to explicitly specify a mempool expiry for LimitMempoolSize(...), just use the newly-introduced member. - Remove all now-unnecessary instances of: std::chrono::hours{gArgs.GetIntArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY)}
1 parent 51c7a41 commit aa9141c

File tree

6 files changed

+15
-13
lines changed

6 files changed

+15
-13
lines changed

src/kernel/mempool_options.h

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

7+
#include <chrono>
78
#include <cstdint>
89

910
class CBlockPolicyEstimator;
1011

1112
/** Default for -maxmempool, maximum megabytes of mempool memory usage */
1213
static constexpr unsigned int DEFAULT_MAX_MEMPOOL_SIZE_MB{300};
14+
/** Default for -mempoolexpiry, expiration time for mempool transactions in hours */
15+
static constexpr unsigned int DEFAULT_MEMPOOL_EXPIRY{336};
1316

1417
namespace kernel {
1518
/**
@@ -25,6 +28,7 @@ struct MemPoolOptions {
2528
/* The ratio used to determine how often sanity checks will run. */
2629
int check_ratio{0};
2730
int64_t max_size_bytes{DEFAULT_MAX_MEMPOOL_SIZE_MB * 1'000'000};
31+
std::chrono::seconds expiry{std::chrono::hours{DEFAULT_MEMPOOL_EXPIRY}};
2832
};
2933
} // namespace kernel
3034

src/mempool_args.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ void ApplyArgsManOptions(const ArgsManager& argsman, MemPoolOptions& mempool_opt
1515
mempool_opts.check_ratio = argsman.GetIntArg("-checkmempool", mempool_opts.check_ratio);
1616

1717
if (auto mb = argsman.GetIntArg("-maxmempool")) mempool_opts.max_size_bytes = *mb * 1'000'000;
18+
19+
if (auto hours = argsman.GetIntArg("-mempoolexpiry")) mempool_opts.expiry = std::chrono::hours{*hours};
1820
}

src/txmempool.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,8 @@ void CTxMemPoolEntry::UpdateAncestorState(int64_t modifySize, CAmount modifyFee,
454454
CTxMemPool::CTxMemPool(const Options& opts)
455455
: m_check_ratio{opts.check_ratio},
456456
minerPolicyEstimator{opts.estimator},
457-
m_max_size_bytes{opts.max_size_bytes}
457+
m_max_size_bytes{opts.max_size_bytes},
458+
m_expiry{opts.expiry}
458459
{
459460
_clear(); //lock free clear
460461
}

src/txmempool.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,7 @@ class CTxMemPool
567567
using Options = kernel::MemPoolOptions;
568568

569569
const int64_t m_max_size_bytes;
570+
const std::chrono::seconds m_expiry;
570571

571572
/** Create a new CTxMemPool.
572573
* Sanity checks will be off by default for performance, because otherwise

src/validation.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,12 @@ 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, std::chrono::seconds age)
258+
static void LimitMempoolSize(CTxMemPool& pool, CCoinsViewCache& coins_cache)
259259
EXCLUSIVE_LOCKS_REQUIRED(::cs_main, pool.cs)
260260
{
261261
AssertLockHeld(::cs_main);
262262
AssertLockHeld(pool.cs);
263-
int expired = pool.Expire(GetTime<std::chrono::seconds>() - age);
263+
int expired = pool.Expire(GetTime<std::chrono::seconds>() - pool.m_expiry);
264264
if (expired != 0) {
265265
LogPrint(BCLog::MEMPOOL, "Expired %i transactions from the memory pool\n", expired);
266266
}
@@ -374,10 +374,7 @@ void CChainState::MaybeUpdateMempoolForReorg(
374374
// We also need to remove any now-immature transactions
375375
m_mempool->removeForReorg(m_chain, filter_final_and_mature);
376376
// Re-limit mempool size, in case we added any transactions
377-
LimitMempoolSize(
378-
*m_mempool,
379-
this->CoinsTip(),
380-
std::chrono::hours{gArgs.GetIntArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY)});
377+
LimitMempoolSize(*m_mempool, this->CoinsTip());
381378
}
382379

383380
/**
@@ -1081,7 +1078,7 @@ bool MemPoolAccept::Finalize(const ATMPArgs& args, Workspace& ws)
10811078
// in the package. LimitMempoolSize() should be called at the very end to make sure the mempool
10821079
// is still within limits and package submission happens atomically.
10831080
if (!args.m_package_submission && !bypass_limits) {
1084-
LimitMempoolSize(m_pool, m_active_chainstate.CoinsTip(), std::chrono::hours{gArgs.GetIntArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY)});
1081+
LimitMempoolSize(m_pool, m_active_chainstate.CoinsTip());
10851082
if (!m_pool.exists(GenTxid::Txid(hash)))
10861083
return state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "mempool full");
10871084
}
@@ -1146,8 +1143,7 @@ bool MemPoolAccept::SubmitPackage(const ATMPArgs& args, std::vector<Workspace>&
11461143

11471144
// It may or may not be the case that all the transactions made it into the mempool. Regardless,
11481145
// make sure we haven't exceeded max mempool size.
1149-
LimitMempoolSize(m_pool, m_active_chainstate.CoinsTip(),
1150-
std::chrono::hours{gArgs.GetIntArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY)});
1146+
LimitMempoolSize(m_pool, m_active_chainstate.CoinsTip());
11511147

11521148
// Find the wtxids of the transactions that made it into the mempool. Allow partial submission,
11531149
// but don't report success unless they all made it into the mempool.
@@ -4645,7 +4641,7 @@ static const uint64_t MEMPOOL_DUMP_VERSION = 1;
46454641

46464642
bool LoadMempool(CTxMemPool& pool, CChainState& active_chainstate, FopenFn mockable_fopen_function)
46474643
{
4648-
int64_t nExpiryTimeout = gArgs.GetIntArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60;
4644+
int64_t nExpiryTimeout = std::chrono::seconds{pool.m_expiry}.count();
46494645
FILE* filestr{mockable_fopen_function(gArgs.GetDataDirNet() / "mempool.dat", "rb")};
46504646
CAutoFile file(filestr, SER_DISK, CLIENT_VERSION);
46514647
if (file.IsNull()) {

src/validation.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ namespace Consensus {
5959
struct Params;
6060
} // namespace Consensus
6161

62-
/** Default for -mempoolexpiry, expiration time for mempool transactions in hours */
63-
static const unsigned int DEFAULT_MEMPOOL_EXPIRY = 336;
6462
/** Maximum number of dedicated script-checking threads allowed */
6563
static const int MAX_SCRIPTCHECK_THREADS = 15;
6664
/** -par default (number of script-checking threads, 0 = auto) */

0 commit comments

Comments
 (0)