From aa9141cd8185cb7ad532bc16feb9d302b05d9697 Mon Sep 17 00:00:00 2001 From: Carl Dong Date: Thu, 28 Oct 2021 14:13:04 -0400 Subject: [PATCH] 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)} --- src/kernel/mempool_options.h | 4 ++++ src/mempool_args.cpp | 2 ++ src/txmempool.cpp | 3 ++- src/txmempool.h | 1 + src/validation.cpp | 16 ++++++---------- src/validation.h | 2 -- 6 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/kernel/mempool_options.h b/src/kernel/mempool_options.h index e8c129050e0..35621e1a28e 100644 --- a/src/kernel/mempool_options.h +++ b/src/kernel/mempool_options.h @@ -4,12 +4,15 @@ #ifndef BITCOIN_KERNEL_MEMPOOL_OPTIONS_H #define BITCOIN_KERNEL_MEMPOOL_OPTIONS_H +#include #include class CBlockPolicyEstimator; /** Default for -maxmempool, maximum megabytes of mempool memory usage */ static constexpr unsigned int DEFAULT_MAX_MEMPOOL_SIZE_MB{300}; +/** Default for -mempoolexpiry, expiration time for mempool transactions in hours */ +static constexpr unsigned int DEFAULT_MEMPOOL_EXPIRY{336}; namespace kernel { /** @@ -25,6 +28,7 @@ struct MemPoolOptions { /* The ratio used to determine how often sanity checks will run. */ int check_ratio{0}; int64_t max_size_bytes{DEFAULT_MAX_MEMPOOL_SIZE_MB * 1'000'000}; + std::chrono::seconds expiry{std::chrono::hours{DEFAULT_MEMPOOL_EXPIRY}}; }; } // namespace kernel diff --git a/src/mempool_args.cpp b/src/mempool_args.cpp index aef4e478f3c..06d553cf444 100644 --- a/src/mempool_args.cpp +++ b/src/mempool_args.cpp @@ -15,4 +15,6 @@ void ApplyArgsManOptions(const ArgsManager& argsman, MemPoolOptions& mempool_opt mempool_opts.check_ratio = argsman.GetIntArg("-checkmempool", mempool_opts.check_ratio); if (auto mb = argsman.GetIntArg("-maxmempool")) mempool_opts.max_size_bytes = *mb * 1'000'000; + + if (auto hours = argsman.GetIntArg("-mempoolexpiry")) mempool_opts.expiry = std::chrono::hours{*hours}; } diff --git a/src/txmempool.cpp b/src/txmempool.cpp index 3e75f2db8d7..a2220e68746 100644 --- a/src/txmempool.cpp +++ b/src/txmempool.cpp @@ -454,7 +454,8 @@ void CTxMemPoolEntry::UpdateAncestorState(int64_t modifySize, CAmount modifyFee, CTxMemPool::CTxMemPool(const Options& opts) : m_check_ratio{opts.check_ratio}, minerPolicyEstimator{opts.estimator}, - m_max_size_bytes{opts.max_size_bytes} + m_max_size_bytes{opts.max_size_bytes}, + m_expiry{opts.expiry} { _clear(); //lock free clear } diff --git a/src/txmempool.h b/src/txmempool.h index 95fd56879fa..0a91cf31c76 100644 --- a/src/txmempool.h +++ b/src/txmempool.h @@ -567,6 +567,7 @@ public: using Options = kernel::MemPoolOptions; const int64_t m_max_size_bytes; + const std::chrono::seconds m_expiry; /** Create a new CTxMemPool. * Sanity checks will be off by default for performance, because otherwise diff --git a/src/validation.cpp b/src/validation.cpp index a14f82106f4..c1ff603faab 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -255,12 +255,12 @@ bool CheckSequenceLocksAtTip(CBlockIndex* tip, // Returns the script flags which should be checked for a given block static unsigned int GetBlockScriptFlags(const CBlockIndex& block_index, const ChainstateManager& chainman); -static void LimitMempoolSize(CTxMemPool& pool, CCoinsViewCache& coins_cache, std::chrono::seconds age) +static void LimitMempoolSize(CTxMemPool& pool, CCoinsViewCache& coins_cache) EXCLUSIVE_LOCKS_REQUIRED(::cs_main, pool.cs) { AssertLockHeld(::cs_main); AssertLockHeld(pool.cs); - int expired = pool.Expire(GetTime() - age); + int expired = pool.Expire(GetTime() - pool.m_expiry); if (expired != 0) { LogPrint(BCLog::MEMPOOL, "Expired %i transactions from the memory pool\n", expired); } @@ -374,10 +374,7 @@ void CChainState::MaybeUpdateMempoolForReorg( // We also need to remove any now-immature transactions m_mempool->removeForReorg(m_chain, filter_final_and_mature); // Re-limit mempool size, in case we added any transactions - LimitMempoolSize( - *m_mempool, - this->CoinsTip(), - std::chrono::hours{gArgs.GetIntArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY)}); + LimitMempoolSize(*m_mempool, this->CoinsTip()); } /** @@ -1081,7 +1078,7 @@ bool MemPoolAccept::Finalize(const ATMPArgs& args, Workspace& ws) // in the package. LimitMempoolSize() should be called at the very end to make sure the mempool // is still within limits and package submission happens atomically. if (!args.m_package_submission && !bypass_limits) { - LimitMempoolSize(m_pool, m_active_chainstate.CoinsTip(), std::chrono::hours{gArgs.GetIntArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY)}); + LimitMempoolSize(m_pool, m_active_chainstate.CoinsTip()); if (!m_pool.exists(GenTxid::Txid(hash))) return state.Invalid(TxValidationResult::TX_MEMPOOL_POLICY, "mempool full"); } @@ -1146,8 +1143,7 @@ bool MemPoolAccept::SubmitPackage(const ATMPArgs& args, std::vector& // It may or may not be the case that all the transactions made it into the mempool. Regardless, // make sure we haven't exceeded max mempool size. - LimitMempoolSize(m_pool, m_active_chainstate.CoinsTip(), - std::chrono::hours{gArgs.GetIntArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY)}); + LimitMempoolSize(m_pool, m_active_chainstate.CoinsTip()); // Find the wtxids of the transactions that made it into the mempool. Allow partial submission, // 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; bool LoadMempool(CTxMemPool& pool, CChainState& active_chainstate, FopenFn mockable_fopen_function) { - int64_t nExpiryTimeout = gArgs.GetIntArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60; + int64_t nExpiryTimeout = std::chrono::seconds{pool.m_expiry}.count(); FILE* filestr{mockable_fopen_function(gArgs.GetDataDirNet() / "mempool.dat", "rb")}; CAutoFile file(filestr, SER_DISK, CLIENT_VERSION); if (file.IsNull()) { diff --git a/src/validation.h b/src/validation.h index 3b6cd509c68..0e27e117fa5 100644 --- a/src/validation.h +++ b/src/validation.h @@ -59,8 +59,6 @@ namespace Consensus { struct Params; } // namespace Consensus -/** Default for -mempoolexpiry, expiration time for mempool transactions in hours */ -static const unsigned int DEFAULT_MEMPOOL_EXPIRY = 336; /** Maximum number of dedicated script-checking threads allowed */ static const int MAX_SCRIPTCHECK_THREADS = 15; /** -par default (number of script-checking threads, 0 = auto) */