Skip to content

Commit 8bca30e

Browse files
committed
[rpc] expose ability to mock scheduler via the rpc
1 parent 7c8b6e5 commit 8bca30e

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/rpc/client.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class CRPCConvertParam
2727
static const CRPCConvertParam vRPCConvertParams[] =
2828
{
2929
{ "setmocktime", 0, "timestamp" },
30+
{ "mockscheduler", 0, "delta_time" },
3031
{ "utxoupdatepsbt", 1, "descriptors" },
3132
{ "generatetoaddress", 0, "nblocks" },
3233
{ "generatetoaddress", 2, "maxtries" },

src/rpc/misc.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55

66
#include <httpserver.h>
77
#include <key_io.h>
8+
#include <node/context.h>
89
#include <outputtype.h>
910
#include <rpc/blockchain.h>
1011
#include <rpc/server.h>
1112
#include <rpc/util.h>
13+
#include <scheduler.h>
1214
#include <script/descriptor.h>
1315
#include <util/check.h>
1416
#include <util/strencodings.h>
@@ -366,6 +368,36 @@ static UniValue setmocktime(const JSONRPCRequest& request)
366368
return NullUniValue;
367369
}
368370

371+
static UniValue mockscheduler(const JSONRPCRequest& request)
372+
{
373+
RPCHelpMan{"mockscheduler",
374+
"\nBump the scheduler into the future (-regtest only)\n",
375+
{
376+
{"delta_time", RPCArg::Type::NUM, RPCArg::Optional::NO, "Number of seconds to forward the scheduler into the future." },
377+
},
378+
RPCResults{},
379+
RPCExamples{""},
380+
}.Check(request);
381+
382+
if (!Params().IsMockableChain()) {
383+
throw std::runtime_error("mockscheduler is for regression testing (-regtest mode) only");
384+
}
385+
386+
// check params are valid values
387+
RPCTypeCheck(request.params, {UniValue::VNUM});
388+
int64_t delta_seconds = request.params[0].get_int64();
389+
if ((delta_seconds <= 0) || (delta_seconds > 3600)) {
390+
throw std::runtime_error("delta_time must be between 1 and 3600 seconds (1 hr)");
391+
}
392+
393+
// protect against null pointer dereference
394+
CHECK_NONFATAL(g_rpc_node);
395+
CHECK_NONFATAL(g_rpc_node->scheduler);
396+
g_rpc_node->scheduler->MockForward(boost::chrono::seconds(delta_seconds));
397+
398+
return NullUniValue;
399+
}
400+
369401
static UniValue RPCLockedMemoryInfo()
370402
{
371403
LockedPool::Stats stats = LockedPoolManager::Instance().stats();
@@ -570,6 +602,7 @@ static const CRPCCommand commands[] =
570602

571603
/* Not shown in help */
572604
{ "hidden", "setmocktime", &setmocktime, {"timestamp"}},
605+
{ "hidden", "mockscheduler", &mockscheduler, {"delta_time"}},
573606
{ "hidden", "echo", &echo, {"arg0","arg1","arg2","arg3","arg4","arg5","arg6","arg7","arg8","arg9"}},
574607
{ "hidden", "echojson", &echo, {"arg0","arg1","arg2","arg3","arg4","arg5","arg6","arg7","arg8","arg9"}},
575608
};

0 commit comments

Comments
 (0)