0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-08 10:31:50 -05:00

Drop JSONRPCRequest constructors after #21366

This just makes an additional simplification after #21366 replaced
util::Ref with std::any. It was originally suggested
https://github.com/bitcoin/bitcoin/pull/21366#issuecomment-792044351 but
delayed for a followup. It would have prevented usage bug
https://github.com/bitcoin/bitcoin/pull/21572.
This commit is contained in:
Russell Yanofsky 2021-04-02 13:35:01 -04:00
parent aa69471ecd
commit 9044522ef7
10 changed files with 28 additions and 41 deletions

View file

@ -224,7 +224,7 @@ static bool AppInit(int argc, char* argv[])
// If locking the data directory failed, exit immediately // If locking the data directory failed, exit immediately
return false; return false;
} }
fRet = AppInitInterfaces(node) && AppInitMain(context, node); fRet = AppInitInterfaces(node) && AppInitMain(node);
} }
catch (const std::exception& e) { catch (const std::exception& e) {
PrintExceptionContinue(&e, "AppInit()"); PrintExceptionContinue(&e, "AppInit()");

View file

@ -159,7 +159,8 @@ static bool HTTPReq_JSONRPC(const std::any& context, HTTPRequest* req)
return false; return false;
} }
JSONRPCRequest jreq(context); JSONRPCRequest jreq;
jreq.context = context;
jreq.peerAddr = req->GetPeer().ToString(); jreq.peerAddr = req->GetPeer().ToString();
if (!RPCAuthorized(authHeader.second, jreq.authUser)) { if (!RPCAuthorized(authHeader.second, jreq.authUser)) {
LogPrintf("ThreadRPCServer incorrect password attempt from %s\n", jreq.peerAddr); LogPrintf("ThreadRPCServer incorrect password attempt from %s\n", jreq.peerAddr);
@ -294,7 +295,7 @@ bool StartHTTPRPC(const std::any& context)
if (!InitRPCAuthentication()) if (!InitRPCAuthentication())
return false; return false;
auto handle_rpc = [&context](HTTPRequest* req, const std::string&) { return HTTPReq_JSONRPC(context, req); }; auto handle_rpc = [context](HTTPRequest* req, const std::string&) { return HTTPReq_JSONRPC(context, req); };
RegisterHTTPHandler("/", true, handle_rpc); RegisterHTTPHandler("/", true, handle_rpc);
if (g_wallet_init_interface.HasWalletSupport()) { if (g_wallet_init_interface.HasWalletSupport()) {
RegisterHTTPHandler("/wallet/", false, handle_rpc); RegisterHTTPHandler("/wallet/", false, handle_rpc);

View file

@ -788,7 +788,7 @@ static bool InitSanityCheck()
return true; return true;
} }
static bool AppInitServers(const std::any& context, NodeContext& node) static bool AppInitServers(NodeContext& node)
{ {
const ArgsManager& args = *Assert(node.args); const ArgsManager& args = *Assert(node.args);
RPCServer::OnStarted(&OnRPCStarted); RPCServer::OnStarted(&OnRPCStarted);
@ -797,9 +797,9 @@ static bool AppInitServers(const std::any& context, NodeContext& node)
return false; return false;
StartRPC(); StartRPC();
node.rpc_interruption_point = RpcInterruptionPoint; node.rpc_interruption_point = RpcInterruptionPoint;
if (!StartHTTPRPC(context)) if (!StartHTTPRPC(&node))
return false; return false;
if (args.GetBoolArg("-rest", DEFAULT_REST_ENABLE)) StartREST(context); if (args.GetBoolArg("-rest", DEFAULT_REST_ENABLE)) StartREST(&node);
StartHTTPServer(); StartHTTPServer();
return true; return true;
} }
@ -1277,7 +1277,7 @@ bool AppInitInterfaces(NodeContext& node)
return true; return true;
} }
bool AppInitMain(const std::any& context, NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
{ {
const ArgsManager& args = *Assert(node.args); const ArgsManager& args = *Assert(node.args);
const CChainParams& chainparams = Params(); const CChainParams& chainparams = Params();
@ -1382,7 +1382,7 @@ bool AppInitMain(const std::any& context, NodeContext& node, interfaces::BlockAn
*/ */
if (args.GetBoolArg("-server", false)) { if (args.GetBoolArg("-server", false)) {
uiInterface.InitMessage_connect(SetRPCWarmupStatus); uiInterface.InitMessage_connect(SetRPCWarmupStatus);
if (!AppInitServers(context, node)) if (!AppInitServers(node))
return InitError(_("Unable to start HTTP server. See debug log for details.")); return InitError(_("Unable to start HTTP server. See debug log for details."));
} }

View file

@ -64,7 +64,7 @@ bool AppInitInterfaces(NodeContext& node);
* @note This should only be done after daemonization. Call Shutdown() if this function fails. * @note This should only be done after daemonization. Call Shutdown() if this function fails.
* @pre Parameters should be parsed and config file should be read, AppInitLockDataDirectory should have been called. * @pre Parameters should be parsed and config file should be read, AppInitLockDataDirectory should have been called.
*/ */
bool AppInitMain(const std::any& context, NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info = nullptr); bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info = nullptr);
/** /**
* Register all arguments with the ArgsManager * Register all arguments with the ArgsManager

View file

@ -80,7 +80,7 @@ public:
} }
bool appInitMain(interfaces::BlockAndHeaderTipInfo* tip_info) override bool appInitMain(interfaces::BlockAndHeaderTipInfo* tip_info) override
{ {
return AppInitMain(m_context_ref, *m_context, tip_info); return AppInitMain(*m_context, tip_info);
} }
void appShutdown() override void appShutdown() override
{ {
@ -244,7 +244,8 @@ public:
CFeeRate getDustRelayFee() override { return ::dustRelayFee; } CFeeRate getDustRelayFee() override { return ::dustRelayFee; }
UniValue executeRpc(const std::string& command, const UniValue& params, const std::string& uri) override UniValue executeRpc(const std::string& command, const UniValue& params, const std::string& uri) override
{ {
JSONRPCRequest req(m_context_ref); JSONRPCRequest req;
req.context = m_context;
req.params = params; req.params = params;
req.strMethod = command; req.strMethod = command;
req.URI = uri; req.URI = uri;
@ -314,14 +315,8 @@ public:
void setContext(NodeContext* context) override void setContext(NodeContext* context) override
{ {
m_context = context; m_context = context;
if (context) {
m_context_ref = context;
} else {
m_context_ref.reset();
}
} }
NodeContext* m_context{nullptr}; NodeContext* m_context{nullptr};
std::any m_context_ref;
}; };
bool FillBlock(const CBlockIndex* index, const FoundBlock& block, UniqueLock<RecursiveMutex>& lock, const CChain& active) bool FillBlock(const CBlockIndex* index, const FoundBlock& block, UniqueLock<RecursiveMutex>& lock, const CChain& active)

View file

@ -317,7 +317,8 @@ static bool rest_chaininfo(const std::any& context, HTTPRequest* req, const std:
switch (rf) { switch (rf) {
case RetFormat::JSON: { case RetFormat::JSON: {
JSONRPCRequest jsonRequest(context); JSONRPCRequest jsonRequest;
jsonRequest.context = context;
jsonRequest.params = UniValue(UniValue::VARR); jsonRequest.params = UniValue(UniValue::VARR);
UniValue chainInfoObject = getblockchaininfo().HandleRequest(jsonRequest); UniValue chainInfoObject = getblockchaininfo().HandleRequest(jsonRequest);
std::string strJSON = chainInfoObject.write() + "\n"; std::string strJSON = chainInfoObject.write() + "\n";
@ -687,7 +688,7 @@ static const struct {
void StartREST(const std::any& context) void StartREST(const std::any& context)
{ {
for (const auto& up : uri_prefixes) { for (const auto& up : uri_prefixes) {
auto handler = [&context, up](HTTPRequest* req, const std::string& prefix) { return up.handler(context, req, prefix); }; auto handler = [context, up](HTTPRequest* req, const std::string& prefix) { return up.handler(context, req, prefix); };
RegisterHTTPHandler(up.prefix, false, handler); RegisterHTTPHandler(up.prefix, false, handler);
} }
} }

View file

@ -35,18 +35,7 @@ public:
std::string URI; std::string URI;
std::string authUser; std::string authUser;
std::string peerAddr; std::string peerAddr;
const std::any& context; std::any context;
explicit JSONRPCRequest(const std::any& context) : id(NullUniValue), params(NullUniValue), context(context) {}
//! Initializes request information from another request object and the
//! given context. The implementation should be updated if any members are
//! added or removed above.
JSONRPCRequest(const JSONRPCRequest& other, const std::any& context)
: id(other.id), strMethod(other.strMethod), params(other.params), mode(other.mode), URI(other.URI),
authUser(other.authUser), peerAddr(other.peerAddr), context(context)
{
}
void parse(const UniValue& valRequest); void parse(const UniValue& valRequest);
}; };

View file

@ -33,8 +33,8 @@ UniValue RPCTestingSetup::CallRPC(std::string args)
boost::split(vArgs, args, boost::is_any_of(" \t")); boost::split(vArgs, args, boost::is_any_of(" \t"));
std::string strMethod = vArgs[0]; std::string strMethod = vArgs[0];
vArgs.erase(vArgs.begin()); vArgs.erase(vArgs.begin());
std::any context{&m_node}; JSONRPCRequest request;
JSONRPCRequest request(context); request.context = &m_node;
request.strMethod = strMethod; request.strMethod = strMethod;
request.params = RPCConvertValues(strMethod, vArgs); request.params = RPCConvertValues(strMethod, vArgs);
if (RPCIsInWarmup(nullptr)) SetRPCWarmupFinished(); if (RPCIsInWarmup(nullptr)) SetRPCWarmupFinished();

View file

@ -514,7 +514,9 @@ public:
{ {
for (const CRPCCommand& command : GetWalletRPCCommands()) { for (const CRPCCommand& command : GetWalletRPCCommands()) {
m_rpc_commands.emplace_back(command.category, command.name, [this, &command](const JSONRPCRequest& request, UniValue& result, bool last_handler) { m_rpc_commands.emplace_back(command.category, command.name, [this, &command](const JSONRPCRequest& request, UniValue& result, bool last_handler) {
return command.actor({request, &m_context}, result, last_handler); JSONRPCRequest wallet_request = request;
wallet_request.context = &m_context;
return command.actor(wallet_request, result, last_handler);
}, command.argNames, command.unique_id); }, command.argNames, command.unique_id);
m_rpc_handlers.emplace_back(m_context.chain->handleRpc(m_rpc_commands.back())); m_rpc_handlers.emplace_back(m_context.chain->handleRpc(m_rpc_commands.back()));
} }
@ -522,7 +524,9 @@ public:
#ifdef ENABLE_EXTERNAL_SIGNER #ifdef ENABLE_EXTERNAL_SIGNER
for (const CRPCCommand& command : GetSignerRPCCommands()) { for (const CRPCCommand& command : GetSignerRPCCommands()) {
m_rpc_commands.emplace_back(command.category, command.name, [this, &command](const JSONRPCRequest& request, UniValue& result, bool last_handler) { m_rpc_commands.emplace_back(command.category, command.name, [this, &command](const JSONRPCRequest& request, UniValue& result, bool last_handler) {
return command.actor({request, &m_context}, result, last_handler); JSONRPCRequest wallet_request = request;
wallet_request.context = &m_context;
return command.actor(wallet_request, result, last_handler);
}, command.argNames, command.unique_id); }, command.argNames, command.unique_id);
m_rpc_handlers.emplace_back(m_context.chain->handleRpc(m_rpc_commands.back())); m_rpc_handlers.emplace_back(m_context.chain->handleRpc(m_rpc_commands.back()));
} }

View file

@ -213,8 +213,7 @@ BOOST_FIXTURE_TEST_CASE(importmulti_rescan, TestChain100Setup)
key.pushKV("timestamp", newTip->GetBlockTimeMax() + TIMESTAMP_WINDOW + 1); key.pushKV("timestamp", newTip->GetBlockTimeMax() + TIMESTAMP_WINDOW + 1);
key.pushKV("internal", UniValue(true)); key.pushKV("internal", UniValue(true));
keys.push_back(key); keys.push_back(key);
std::any context; JSONRPCRequest request;
JSONRPCRequest request(context);
request.params.setArray(); request.params.setArray();
request.params.push_back(keys); request.params.push_back(keys);
@ -265,8 +264,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
AddWallet(wallet); AddWallet(wallet);
wallet->SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash()); wallet->SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash());
} }
std::any context; JSONRPCRequest request;
JSONRPCRequest request(context);
request.params.setArray(); request.params.setArray();
request.params.push_back(backup_file); request.params.push_back(backup_file);
@ -281,8 +279,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
LOCK(wallet->cs_wallet); LOCK(wallet->cs_wallet);
wallet->SetupLegacyScriptPubKeyMan(); wallet->SetupLegacyScriptPubKeyMan();
std::any context; JSONRPCRequest request;
JSONRPCRequest request(context);
request.params.setArray(); request.params.setArray();
request.params.push_back(backup_file); request.params.push_back(backup_file);
AddWallet(wallet); AddWallet(wallet);