From 9044522ef76f880760165d98fab024802ccfc062 Mon Sep 17 00:00:00 2001 From: Russell Yanofsky Date: Fri, 2 Apr 2021 13:35:01 -0400 Subject: 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. --- src/bitcoind.cpp | 2 +- src/httprpc.cpp | 5 +++-- src/init.cpp | 10 +++++----- src/init.h | 2 +- src/node/interfaces.cpp | 11 +++-------- src/rest.cpp | 5 +++-- src/rpc/request.h | 13 +------------ src/test/rpc_tests.cpp | 4 ++-- src/wallet/interfaces.cpp | 8 ++++++-- src/wallet/test/wallet_tests.cpp | 9 +++------ 10 files changed, 28 insertions(+), 41 deletions(-) (limited to 'src') diff --git a/src/bitcoind.cpp b/src/bitcoind.cpp index 80ab69c131..225b8b1ec4 100644 --- a/src/bitcoind.cpp +++ b/src/bitcoind.cpp @@ -224,7 +224,7 @@ static bool AppInit(int argc, char* argv[]) // If locking the data directory failed, exit immediately return false; } - fRet = AppInitInterfaces(node) && AppInitMain(context, node); + fRet = AppInitInterfaces(node) && AppInitMain(node); } catch (const std::exception& e) { PrintExceptionContinue(&e, "AppInit()"); diff --git a/src/httprpc.cpp b/src/httprpc.cpp index 16ab38e0b2..e11e4acb5c 100644 --- a/src/httprpc.cpp +++ b/src/httprpc.cpp @@ -159,7 +159,8 @@ static bool HTTPReq_JSONRPC(const std::any& context, HTTPRequest* req) return false; } - JSONRPCRequest jreq(context); + JSONRPCRequest jreq; + jreq.context = context; jreq.peerAddr = req->GetPeer().ToString(); if (!RPCAuthorized(authHeader.second, jreq.authUser)) { LogPrintf("ThreadRPCServer incorrect password attempt from %s\n", jreq.peerAddr); @@ -294,7 +295,7 @@ bool StartHTTPRPC(const std::any& context) if (!InitRPCAuthentication()) 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); if (g_wallet_init_interface.HasWalletSupport()) { RegisterHTTPHandler("/wallet/", false, handle_rpc); diff --git a/src/init.cpp b/src/init.cpp index 17b216573f..701a7529af 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -788,7 +788,7 @@ static bool InitSanityCheck() return true; } -static bool AppInitServers(const std::any& context, NodeContext& node) +static bool AppInitServers(NodeContext& node) { const ArgsManager& args = *Assert(node.args); RPCServer::OnStarted(&OnRPCStarted); @@ -797,9 +797,9 @@ static bool AppInitServers(const std::any& context, NodeContext& node) return false; StartRPC(); node.rpc_interruption_point = RpcInterruptionPoint; - if (!StartHTTPRPC(context)) + if (!StartHTTPRPC(&node)) return false; - if (args.GetBoolArg("-rest", DEFAULT_REST_ENABLE)) StartREST(context); + if (args.GetBoolArg("-rest", DEFAULT_REST_ENABLE)) StartREST(&node); StartHTTPServer(); return true; } @@ -1277,7 +1277,7 @@ bool AppInitInterfaces(NodeContext& node) 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 CChainParams& chainparams = Params(); @@ -1382,7 +1382,7 @@ bool AppInitMain(const std::any& context, NodeContext& node, interfaces::BlockAn */ if (args.GetBoolArg("-server", false)) { uiInterface.InitMessage_connect(SetRPCWarmupStatus); - if (!AppInitServers(context, node)) + if (!AppInitServers(node)) return InitError(_("Unable to start HTTP server. See debug log for details.")); } diff --git a/src/init.h b/src/init.h index 5d01d4c1ac..328eda9c7e 100644 --- a/src/init.h +++ b/src/init.h @@ -64,7 +64,7 @@ bool AppInitInterfaces(NodeContext& node); * @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. */ -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 diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp index 7ad02f81dc..e10e89b8cf 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -80,7 +80,7 @@ public: } 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 { @@ -244,7 +244,8 @@ public: CFeeRate getDustRelayFee() override { return ::dustRelayFee; } 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.strMethod = command; req.URI = uri; @@ -314,14 +315,8 @@ public: void setContext(NodeContext* context) override { m_context = context; - if (context) { - m_context_ref = context; - } else { - m_context_ref.reset(); - } } NodeContext* m_context{nullptr}; - std::any m_context_ref; }; bool FillBlock(const CBlockIndex* index, const FoundBlock& block, UniqueLock& lock, const CChain& active) diff --git a/src/rest.cpp b/src/rest.cpp index aa97470ca7..71d258b077 100644 --- a/src/rest.cpp +++ b/src/rest.cpp @@ -317,7 +317,8 @@ static bool rest_chaininfo(const std::any& context, HTTPRequest* req, const std: switch (rf) { case RetFormat::JSON: { - JSONRPCRequest jsonRequest(context); + JSONRPCRequest jsonRequest; + jsonRequest.context = context; jsonRequest.params = UniValue(UniValue::VARR); UniValue chainInfoObject = getblockchaininfo().HandleRequest(jsonRequest); std::string strJSON = chainInfoObject.write() + "\n"; @@ -687,7 +688,7 @@ static const struct { void StartREST(const std::any& context) { 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); } } diff --git a/src/rpc/request.h b/src/rpc/request.h index e1569673f6..bb091efea8 100644 --- a/src/rpc/request.h +++ b/src/rpc/request.h @@ -35,18 +35,7 @@ public: std::string URI; std::string authUser; std::string peerAddr; - const 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) - { - } + std::any context; void parse(const UniValue& valRequest); }; diff --git a/src/test/rpc_tests.cpp b/src/test/rpc_tests.cpp index 7c50c72041..e2f8725fcb 100644 --- a/src/test/rpc_tests.cpp +++ b/src/test/rpc_tests.cpp @@ -33,8 +33,8 @@ UniValue RPCTestingSetup::CallRPC(std::string args) boost::split(vArgs, args, boost::is_any_of(" \t")); std::string strMethod = vArgs[0]; vArgs.erase(vArgs.begin()); - std::any context{&m_node}; - JSONRPCRequest request(context); + JSONRPCRequest request; + request.context = &m_node; request.strMethod = strMethod; request.params = RPCConvertValues(strMethod, vArgs); if (RPCIsInWarmup(nullptr)) SetRPCWarmupFinished(); diff --git a/src/wallet/interfaces.cpp b/src/wallet/interfaces.cpp index 0a284dd43e..23bdaa671a 100644 --- a/src/wallet/interfaces.cpp +++ b/src/wallet/interfaces.cpp @@ -514,7 +514,9 @@ public: { for (const CRPCCommand& command : GetWalletRPCCommands()) { 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); m_rpc_handlers.emplace_back(m_context.chain->handleRpc(m_rpc_commands.back())); } @@ -522,7 +524,9 @@ public: #ifdef ENABLE_EXTERNAL_SIGNER for (const CRPCCommand& command : GetSignerRPCCommands()) { 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); m_rpc_handlers.emplace_back(m_context.chain->handleRpc(m_rpc_commands.back())); } diff --git a/src/wallet/test/wallet_tests.cpp b/src/wallet/test/wallet_tests.cpp index ba2e17d62a..01f2dfe06b 100644 --- a/src/wallet/test/wallet_tests.cpp +++ b/src/wallet/test/wallet_tests.cpp @@ -213,8 +213,7 @@ BOOST_FIXTURE_TEST_CASE(importmulti_rescan, TestChain100Setup) key.pushKV("timestamp", newTip->GetBlockTimeMax() + TIMESTAMP_WINDOW + 1); key.pushKV("internal", UniValue(true)); keys.push_back(key); - std::any context; - JSONRPCRequest request(context); + JSONRPCRequest request; request.params.setArray(); request.params.push_back(keys); @@ -265,8 +264,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup) AddWallet(wallet); wallet->SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash()); } - std::any context; - JSONRPCRequest request(context); + JSONRPCRequest request; request.params.setArray(); request.params.push_back(backup_file); @@ -281,8 +279,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup) LOCK(wallet->cs_wallet); wallet->SetupLegacyScriptPubKeyMan(); - std::any context; - JSONRPCRequest request(context); + JSONRPCRequest request; request.params.setArray(); request.params.push_back(backup_file); AddWallet(wallet); -- cgit v1.2.3