Skip to content

Commit ac77659

Browse files
committed
Merge branch 'rpc_maxmempool' into bytespersigopstrict-28+knots
2 parents 7754dc8 + 475d37d commit ac77659

File tree

7 files changed

+51
-3
lines changed

7 files changed

+51
-3
lines changed

src/rpc/client.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
6666
{ "getbalance", 3, "avoid_reuse" },
6767
{ "getblockfrompeer", 1, "peer_id" },
6868
{ "getblockhash", 0, "height" },
69+
{ "maxmempool", 0, "megabytes" },
6970
{ "waitforblockheight", 0, "height" },
7071
{ "waitforblockheight", 1, "timeout" },
7172
{ "waitforblock", 1, "timeout" },

src/rpc/mempool.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <chainparams.h>
1111
#include <core_io.h>
1212
#include <kernel/mempool_entry.h>
13+
#include <node/context.h>
1314
#include <node/mempool_persist_args.h>
1415
#include <node/types.h>
1516
#include <policy/rbf.h>
@@ -20,10 +21,12 @@
2021
#include <rpc/util.h>
2122
#include <txmempool.h>
2223
#include <univalue.h>
24+
#include <util/any.h>
2325
#include <util/fs.h>
2426
#include <util/moneystr.h>
2527
#include <util/strencodings.h>
2628
#include <util/time.h>
29+
#include <validation.h>
2730

2831
#include <utility>
2932

@@ -419,6 +422,42 @@ UniValue MempoolToJSON(const CTxMemPool& pool, bool verbose, bool include_mempoo
419422
}
420423
}
421424

425+
static RPCHelpMan maxmempool()
426+
{
427+
return RPCHelpMan{"maxmempool",
428+
"\nSets the allocated memory for the memory pool.\n",
429+
{
430+
{"megabytes", RPCArg::Type::NUM, RPCArg::Optional::NO, "The memory allocated in MB"},
431+
},
432+
RPCResult{
433+
RPCResult::Type::NONE, "", ""},
434+
RPCExamples{
435+
HelpExampleCli("maxmempool", "150") + HelpExampleRpc("maxmempool", "150")
436+
},
437+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
438+
{
439+
int64_t nSize = request.params[0].getInt<int32_t>();
440+
int64_t nMempoolSizeMax = nSize * 1000000;
441+
442+
CTxMemPool& mempool = EnsureAnyMemPool(request.context);
443+
LOCK2(cs_main, mempool.cs);
444+
445+
int64_t nMempoolSizeMin = maxmempoolMinimumBytes(mempool.m_opts.limits.descendant_size_vbytes);
446+
if (nMempoolSizeMax < 0 || nMempoolSizeMax < nMempoolSizeMin)
447+
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("MaxMempool size %d is too small", nSize));
448+
mempool.m_opts.max_size_bytes = nMempoolSizeMax;
449+
450+
auto node_context = util::AnyPtr<NodeContext>(request.context);
451+
if (node_context && node_context->chainman) {
452+
Chainstate& active_chainstate = node_context->chainman->ActiveChainstate();
453+
LimitMempoolSize(mempool, active_chainstate.CoinsTip());
454+
}
455+
456+
return NullUniValue;
457+
}
458+
};
459+
}
460+
422461
static RPCHelpMan getrawmempool()
423462
{
424463
return RPCHelpMan{"getrawmempool",
@@ -1072,6 +1111,7 @@ void RegisterMempoolRPCCommands(CRPCTable& t)
10721111
{"blockchain", &getrawmempool},
10731112
{"blockchain", &importmempool},
10741113
{"blockchain", &savemempool},
1114+
{"blockchain", &maxmempool},
10751115
{"rawtransactions", &submitpackage},
10761116
};
10771117
for (const auto& c : commands) {

src/test/fuzz/rpc.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ const std::vector<std::string> RPC_COMMANDS_SAFE_FOR_FUZZING{
157157
"joinpsbts",
158158
"listbanned",
159159
"logging",
160+
"maxmempool",
160161
"mockscheduler",
161162
"ping",
162163
"preciousblock",

src/txmempool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ void CTxMemPoolEntry::UpdateAncestorState(int32_t modifySize, CAmount modifyFee,
401401
static CTxMemPool::Options&& Flatten(CTxMemPool::Options&& opts, bilingual_str& error)
402402
{
403403
opts.check_ratio = std::clamp<int>(opts.check_ratio, 0, 1'000'000);
404-
int64_t descendant_limit_bytes = opts.limits.descendant_size_vbytes * 40;
404+
int64_t descendant_limit_bytes = maxmempoolMinimumBytes(opts.limits.descendant_size_vbytes);
405405
if (opts.max_size_bytes < 0 || opts.max_size_bytes < descendant_limit_bytes) {
406406
error = strprintf(_("-maxmempool must be at least %d MB"), std::ceil(descendant_limit_bytes / 1'000'000.0));
407407
}

src/txmempool.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ struct bilingual_str;
4848
/** Fake height value used in Coin to signify they are only in the memory pool (since 0.8) */
4949
static const uint32_t MEMPOOL_HEIGHT = 0x7FFFFFFF;
5050

51+
inline int64_t maxmempoolMinimumBytes(const int64_t descendant_size_vbytes) {
52+
return descendant_size_vbytes * 40;
53+
}
54+
5155
/**
5256
* Test whether the LockPoints height and time are still valid on the current chain
5357
*/
@@ -436,7 +440,7 @@ class CTxMemPool
436440

437441
using Options = kernel::MemPoolOptions;
438442

439-
const Options m_opts;
443+
Options m_opts;
440444

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

src/validation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ bool CheckSequenceLocksAtTip(CBlockIndex* tip,
262262
// Returns the script flags which should be checked for a given block
263263
static unsigned int GetBlockScriptFlags(const CBlockIndex& block_index, const ChainstateManager& chainman);
264264

265-
static void LimitMempoolSize(CTxMemPool& pool, CCoinsViewCache& coins_cache)
265+
void LimitMempoolSize(CTxMemPool& pool, CCoinsViewCache& coins_cache)
266266
EXCLUSIVE_LOCKS_REQUIRED(::cs_main, pool.cs)
267267
{
268268
AssertLockHeld(::cs_main);

src/validation.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,8 @@ std::optional<LockPoints> CalculateLockPointsAtTip(
336336
bool CheckSequenceLocksAtTip(CBlockIndex* tip,
337337
const LockPoints& lock_points);
338338

339+
void LimitMempoolSize(CTxMemPool&, CCoinsViewCache&);
340+
339341
/**
340342
* Closure representing one script verification
341343
* Note that this stores references to the spending transaction

0 commit comments

Comments
 (0)