Have you considered adding this logic to mempool_args.cpp::ApplyArgsManOptions() instead, so we don't set our mempool_opts.max_size_bytes in two different locations? I think this has the benefit of 1) being easier to understand why options are set to the values they are and 2) avoid unexpected issues/behaviour where mempool_opts.max_size_bytes is temporarily set to 300MB before being changed to 5MB.
The downside, as previously discussed, is including net.h, which probably needs fixing anyway (perhaps in separate PR?).
I think I'd much prefer this approach.
<details>
<summary>git diff on 931924f5cc1dd9c82306a0ce59b62cf5c9276565</summary>
diff --git a/src/init.cpp b/src/init.cpp
index c4b15392c..641fef81c 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -1467,12 +1467,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
if (mempool_opts.max_size_bytes < 0 || mempool_opts.max_size_bytes < descendant_limit_bytes) {
return InitError(strprintf(_("-maxmempool must be at least %d MB"), std::ceil(descendant_limit_bytes / 1'000'000.0)));
}
- // If we are running in blocksonly mode don't allocate the default 300MB of space for mempool
- // unless the user specifies -maxmempool, as otherwise it will leak into dbcache allowance.
- // Keeping the mempool enabled will still permit transaction relay for whitelisted peers
- if (args.GetBoolArg("-blocksonly", DEFAULT_BLOCKSONLY) && !args.IsArgSet("-maxmempool")) {
- mempool_opts.max_size_bytes = 5 * 1'000'000;
- }
LogPrintf("* Using %.1f MiB for in-memory UTXO set (plus up to %.1f MiB of unused mempool space)\n",
cache_sizes.coins * (1.0 / 1024 / 1024),
mempool_opts.max_size_bytes * (1.0 / 1024 / 1024));
diff --git a/src/node/mempool_args.cpp b/src/node/mempool_args.cpp
index 8c929e5e0..b530ac981 100644
--- a/src/node/mempool_args.cpp
+++ b/src/node/mempool_args.cpp
@@ -10,6 +10,7 @@
#include <chainparams.h>
#include <consensus/amount.h>
#include <logging.h>
+#include <net.h>
#include <policy/feerate.h>
#include <policy/policy.h>
#include <script/standard.h>
@@ -42,7 +43,14 @@ std::optional<bilingual_str> ApplyArgsManOptions(const ArgsManager& argsman, con
{
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 mb = argsman.GetIntArg("-maxmempool")) {
+ mempool_opts.max_size_bytes = *mb * 1'000'000;
+ } else if (argsman.GetBoolArg("-blocksonly", DEFAULT_BLOCKSONLY)) {
+ // If we are running in blocksonly mode don't allocate the default 300MB of space for mempool
+ // unless the user specifies -maxmempool, as otherwise it will leak into dbcache allowance.
+ // Keeping the mempool enabled will still permit transaction relay for whitelisted peers
+ mempool_opts.max_size_bytes = 5 * 1'000'000;
+ }
if (auto hours = argsman.GetIntArg("-mempoolexpiry")) mempool_opts.expiry = std::chrono::hours{*hours};
</details>