Merge 21780 via rpc_maxmempool

This commit is contained in:
Luke Dashjr 2023-11-15 23:49:11 +00:00
commit c433f09bab
7 changed files with 50 additions and 3 deletions

View File

@ -1494,7 +1494,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
}
mempool_opts.check_ratio = std::clamp<int>(mempool_opts.check_ratio, 0, 1'000'000);
int64_t descendant_limit_bytes = mempool_opts.limits.descendant_size_vbytes * 40;
int64_t descendant_limit_bytes = maxmempoolMinimumBytes(mempool_opts.limits.descendant_size_vbytes);
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)));
}

View File

@ -67,6 +67,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "getbalance", 3, "avoid_reuse" },
{ "getblockfrompeer", 1, "peer_id" },
{ "getblockhash", 0, "height" },
{ "maxmempool", 0, "megabytes" },
{ "waitforblockheight", 0, "height" },
{ "waitforblockheight", 1, "timeout" },
{ "waitforblock", 1, "timeout" },

View File

@ -10,6 +10,7 @@
#include <chainparams.h>
#include <core_io.h>
#include <kernel/mempool_entry.h>
#include <node/context.h>
#include <node/mempool_persist_args.h>
#include <policy/rbf.h>
#include <policy/settings.h>
@ -23,6 +24,7 @@
#include <univalue.h>
#include <util/fs.h>
#include <util/moneystr.h>
#include <util/system.h>
#include <util/time.h>
#include <optional>
@ -378,6 +380,42 @@ UniValue MempoolToJSON(const CTxMemPool& pool, bool verbose, bool include_mempoo
}
}
static RPCHelpMan maxmempool()
{
return RPCHelpMan{"maxmempool",
"\nSets the allocated memory for the memory pool.\n",
{
{"megabytes", RPCArg::Type::NUM, RPCArg::Optional::NO, "The memory allocated in MB"},
},
RPCResult{
RPCResult::Type::NONE, "", ""},
RPCExamples{
HelpExampleCli("maxmempool", "150") + HelpExampleRpc("maxmempool", "150")
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
int64_t nSize = request.params[0].getInt<int32_t>();
int64_t nMempoolSizeMax = nSize * 1000000;
CTxMemPool& mempool = EnsureAnyMemPool(request.context);
LOCK2(cs_main, mempool.cs);
int64_t nMempoolSizeMin = maxmempoolMinimumBytes(mempool.m_limits.descendant_size_vbytes);
if (nMempoolSizeMax < 0 || nMempoolSizeMax < nMempoolSizeMin)
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("MaxMempool size %d is too small", nSize));
mempool.m_max_size_bytes = nSize;
auto node_context = util::AnyPtr<NodeContext>(request.context);
if (node_context && node_context->chainman) {
Chainstate& active_chainstate = node_context->chainman->ActiveChainstate();
LimitMempoolSize(mempool, active_chainstate.CoinsTip());
}
return NullUniValue;
}
};
}
static RPCHelpMan getrawmempool()
{
return RPCHelpMan{"getrawmempool",
@ -1025,6 +1063,7 @@ void RegisterMempoolRPCCommands(CRPCTable& t)
{"blockchain", &getmempoolinfo},
{"blockchain", &getrawmempool},
{"blockchain", &savemempool},
{"blockchain", &maxmempool},
{"hidden", &submitpackage},
};
for (const auto& c : commands) {

View File

@ -151,6 +151,7 @@ const std::vector<std::string> RPC_COMMANDS_SAFE_FOR_FUZZING{
"listbanned",
"listprunelocks",
"logging",
"maxmempool",
"mockscheduler",
"ping",
"preciousblock",

View File

@ -44,6 +44,10 @@ class Chainstate;
/** Fake height value used in Coin to signify they are only in the memory pool (since 0.8) */
static const uint32_t MEMPOOL_HEIGHT = 0x7FFFFFFF;
inline int64_t maxmempoolMinimumBytes(const int64_t descendant_size_vbytes) {
return descendant_size_vbytes * 40;
}
/**
* Test whether the LockPoints height and time are still valid on the current chain
*/
@ -451,7 +455,7 @@ public:
using Options = kernel::MemPoolOptions;
const int64_t m_max_size_bytes;
int64_t m_max_size_bytes;
const std::chrono::seconds m_expiry;
const CFeeRate m_incremental_relay_feerate;
const CFeeRate m_min_relay_feerate;

View File

@ -254,7 +254,7 @@ 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)
void LimitMempoolSize(CTxMemPool& pool, CCoinsViewCache& coins_cache)
EXCLUSIVE_LOCKS_REQUIRED(::cs_main, pool.cs)
{
AssertLockHeld(::cs_main);

View File

@ -303,6 +303,8 @@ std::optional<LockPoints> CalculateLockPointsAtTip(
bool CheckSequenceLocksAtTip(CBlockIndex* tip,
const LockPoints& lock_points);
void LimitMempoolSize(CTxMemPool&, CCoinsViewCache&);
/**
* Closure representing one script verification
* Note that this stores references to the spending transaction