Changeset View
Changeset View
Standalone View
Standalone View
src/rpc/misc.cpp
Show First 20 Lines • Show All 439 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
static RPCHelpMan setmocktime() { | static RPCHelpMan setmocktime() { | ||||
return RPCHelpMan{ | return RPCHelpMan{ | ||||
"setmocktime", | "setmocktime", | ||||
"Set the local time to given timestamp (-regtest only)\n", | "Set the local time to given timestamp (-regtest only)\n", | ||||
{ | { | ||||
{"timestamp", RPCArg::Type::NUM, RPCArg::Optional::NO, | {"timestamp", RPCArg::Type::NUM, RPCArg::Optional::NO, | ||||
UNIX_EPOCH_TIME + | UNIX_EPOCH_TIME + "\n" | ||||
"\n" | |||||
" Pass 0 to go back to using the system time."}, | "Pass 0 to go back to using the system time."}, | ||||
}, | }, | ||||
RPCResult{RPCResult::Type::NONE, "", ""}, | RPCResult{RPCResult::Type::NONE, "", ""}, | ||||
RPCExamples{""}, | RPCExamples{""}, | ||||
[&](const RPCHelpMan &self, const Config &config, | [&](const RPCHelpMan &self, const Config &config, | ||||
const JSONRPCRequest &request) -> UniValue { | const JSONRPCRequest &request) -> UniValue { | ||||
if (!config.GetChainParams().IsMockableChain()) { | if (!config.GetChainParams().IsMockableChain()) { | ||||
throw std::runtime_error( | throw std::runtime_error( | ||||
"setmocktime for regression testing (-regtest mode) only"); | "setmocktime for regression testing (-regtest mode) only"); | ||||
} | } | ||||
// For now, don't change mocktime if we're in the middle of | // For now, don't change mocktime if we're in the middle of | ||||
// validation, as this could have an effect on mempool time-based | // validation, as this could have an effect on mempool time-based | ||||
// eviction, as well as IsInitialBlockDownload(). | // eviction, as well as IsInitialBlockDownload(). | ||||
// TODO: figure out the right way to synchronize around mocktime, | // TODO: figure out the right way to synchronize around mocktime, | ||||
// and ensure all call sites of GetTime() are accessing this safely. | // and ensure all call sites of GetTime() are accessing this safely. | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
RPCTypeCheck(request.params, {UniValue::VNUM}); | RPCTypeCheck(request.params, {UniValue::VNUM}); | ||||
int64_t time = request.params[0].get_int64(); | const int64_t time{request.params[0].get_int64()}; | ||||
if (time < 0) { | if (time < 0) { | ||||
throw JSONRPCError(RPC_INVALID_PARAMETER, | throw JSONRPCError( | ||||
"Timestamp must be 0 or greater"); | RPC_INVALID_PARAMETER, | ||||
strprintf("Mocktime can not be negative: %s.", time)); | |||||
} | } | ||||
SetMockTime(time); | SetMockTime(time); | ||||
auto node_context = util::AnyPtr<NodeContext>(request.context); | auto node_context = util::AnyPtr<NodeContext>(request.context); | ||||
if (node_context) { | if (node_context) { | ||||
for (const auto &chain_client : node_context->chain_clients) { | for (const auto &chain_client : node_context->chain_clients) { | ||||
chain_client->setMockTime(time); | chain_client->setMockTime(time); | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 440 Lines • Show Last 20 Lines |