From 9333427014695ac235c96d48791098168dfdc9db Mon Sep 17 00:00:00 2001 From: Carl Dong Date: Thu, 17 Mar 2022 22:09:05 -0400 Subject: mempool: Introduce (still-unused) MemPoolLimits They live as a CTxMemPool member. [META] These limits will be used in subsequent commits to replace calls to gArgs. --- src/Makefile.am | 1 + src/kernel/mempool_limits.h | 26 ++++++++++++++++++++++++++ src/kernel/mempool_options.h | 3 +++ src/mempool_args.cpp | 17 +++++++++++++++++ src/mempool_args.h | 7 +++++++ src/txmempool.cpp | 3 ++- src/txmempool.h | 5 +++++ 7 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 src/kernel/mempool_limits.h (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index bdb279d176..734af78ae5 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -173,6 +173,7 @@ BITCOIN_CORE_H = \ kernel/checks.h \ kernel/coinstats.h \ kernel/context.h \ + kernel/mempool_limits.h \ kernel/mempool_options.h \ key.h \ key_io.h \ diff --git a/src/kernel/mempool_limits.h b/src/kernel/mempool_limits.h new file mode 100644 index 0000000000..083e9681e7 --- /dev/null +++ b/src/kernel/mempool_limits.h @@ -0,0 +1,26 @@ +// Copyright (c) 2022 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. +#ifndef BITCOIN_KERNEL_MEMPOOL_LIMITS_H +#define BITCOIN_KERNEL_MEMPOOL_LIMITS_H + +#include + +#include + +namespace kernel { +/** + * Options struct containing limit options for a CTxMemPool. Default constructor + * populates the struct with sane default values which can be modified. + * + * Most of the time, this struct should be referenced as CTxMemPool::Limits. + */ +struct MemPoolLimits { + int64_t ancestor_count{DEFAULT_ANCESTOR_LIMIT}; + int64_t ancestor_size_vbytes{DEFAULT_ANCESTOR_SIZE_LIMIT_KVB * 1'000}; + int64_t descendant_count{DEFAULT_DESCENDANT_LIMIT}; + int64_t descendant_size_vbytes{DEFAULT_DESCENDANT_SIZE_LIMIT_KVB * 1'000}; +}; +} // namespace kernel + +#endif // BITCOIN_KERNEL_MEMPOOL_LIMITS_H diff --git a/src/kernel/mempool_options.h b/src/kernel/mempool_options.h index c4f34ff9f9..a14abb6628 100644 --- a/src/kernel/mempool_options.h +++ b/src/kernel/mempool_options.h @@ -4,6 +4,8 @@ #ifndef BITCOIN_KERNEL_MEMPOOL_OPTIONS_H #define BITCOIN_KERNEL_MEMPOOL_OPTIONS_H +#include + #include #include @@ -29,6 +31,7 @@ struct MemPoolOptions { 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_HOURS}}; + MemPoolLimits limits{}; }; } // namespace kernel diff --git a/src/mempool_args.cpp b/src/mempool_args.cpp index 06d553cf44..e26cbe0275 100644 --- a/src/mempool_args.cpp +++ b/src/mempool_args.cpp @@ -4,12 +4,27 @@ #include +#include #include #include +using kernel::MemPoolLimits; using kernel::MemPoolOptions; +namespace { +void ApplyArgsManOptions(const ArgsManager& argsman, MemPoolLimits& mempool_limits) +{ + mempool_limits.ancestor_count = argsman.GetIntArg("-limitancestorcount", mempool_limits.ancestor_count); + + if (auto vkb = argsman.GetIntArg("-limitancestorsize")) mempool_limits.ancestor_size_vbytes = *vkb * 1'000; + + mempool_limits.descendant_count = argsman.GetIntArg("-limitdescendantcount", mempool_limits.descendant_count); + + if (auto vkb = argsman.GetIntArg("-limitdescendantsize")) mempool_limits.descendant_size_vbytes = *vkb * 1'000; +} +} + void ApplyArgsManOptions(const ArgsManager& argsman, MemPoolOptions& mempool_opts) { mempool_opts.check_ratio = argsman.GetIntArg("-checkmempool", mempool_opts.check_ratio); @@ -17,4 +32,6 @@ void ApplyArgsManOptions(const ArgsManager& argsman, MemPoolOptions& mempool_opt 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}; + + ApplyArgsManOptions(argsman, mempool_opts.limits); } diff --git a/src/mempool_args.h b/src/mempool_args.h index 1732bdaac9..9a4abe6618 100644 --- a/src/mempool_args.h +++ b/src/mempool_args.h @@ -10,6 +10,13 @@ namespace kernel { struct MemPoolOptions; }; +/** + * Overlay the options set in \p argsman on top of corresponding members in \p mempool_opts. + * + * @param[in] argsman The ArgsManager in which to check set options. + * @param[in,out] mempool_opts The MemPoolOptions to modify according to \p argsman. + */ void ApplyArgsManOptions(const ArgsManager& argsman, kernel::MemPoolOptions& mempool_opts); + #endif // BITCOIN_MEMPOOL_ARGS_H diff --git a/src/txmempool.cpp b/src/txmempool.cpp index a2220e6874..2c2b063133 100644 --- a/src/txmempool.cpp +++ b/src/txmempool.cpp @@ -455,7 +455,8 @@ CTxMemPool::CTxMemPool(const Options& opts) : m_check_ratio{opts.check_ratio}, minerPolicyEstimator{opts.estimator}, m_max_size_bytes{opts.max_size_bytes}, - m_expiry{opts.expiry} + m_expiry{opts.expiry}, + m_limits{opts.limits} { _clear(); //lock free clear } diff --git a/src/txmempool.h b/src/txmempool.h index 0a91cf31c7..4cc62448f9 100644 --- a/src/txmempool.h +++ b/src/txmempool.h @@ -14,6 +14,7 @@ #include #include +#include #include #include @@ -569,6 +570,10 @@ public: const int64_t m_max_size_bytes; const std::chrono::seconds m_expiry; + using Limits = kernel::MemPoolLimits; + + const Limits m_limits; + /** Create a new CTxMemPool. * Sanity checks will be off by default for performance, because otherwise * accepting transactions becomes O(N^2) where N is the number of transactions -- cgit v1.2.3