diff --git a/src/bitcoind.cpp b/src/bitcoind.cpp index 1ec5867f36e356..ce8384026ee06d 100644 --- a/src/bitcoind.cpp +++ b/src/bitcoind.cpp @@ -234,7 +234,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 (...) { PrintExceptionContinue(std::current_exception(), "AppInit()"); } diff --git a/src/httprpc.cpp b/src/httprpc.cpp index af02b53c55892c..59256fe7b77192 100644 --- a/src/httprpc.cpp +++ b/src/httprpc.cpp @@ -190,8 +190,8 @@ static bool HTTPReq_JSONRPC(const CoreContext& context, HTTPRequest* req) return rpcRequest.send_reply(HTTP_UNAUTHORIZED); } - JSONRPCRequest jreq(context); - + JSONRPCRequest jreq; + jreq.context = context; jreq.peerAddr = req->GetPeer().ToString(); if (!RPCAuthorized(authHeader.second, rpcRequest.user)) { LogPrintf("ThreadRPCServer incorrect password attempt from %s\n", jreq.peerAddr); @@ -324,7 +324,7 @@ bool StartHTTPRPC(const CoreContext& 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 005cca71c5fa84..27b9b10dc64be3 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -960,7 +960,7 @@ static bool InitSanityCheck() return true; } -static bool AppInitServers(const CoreContext& context, NodeContext& node) +static bool AppInitServers(NodeContext& node) { const ArgsManager& args = *Assert(node.args); RPCServer::OnStarted(&OnRPCStarted); @@ -969,9 +969,9 @@ static bool AppInitServers(const CoreContext& 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; } @@ -1536,7 +1536,7 @@ bool AppInitInterfaces(NodeContext& node) return true; } -bool AppInitMain(const CoreContext& 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(); @@ -1643,7 +1643,7 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc */ 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 d4316f9c875ae2..a529770cb02efa 100644 --- a/src/init.h +++ b/src/init.h @@ -62,7 +62,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 CoreContext& context, NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info = nullptr); +bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info = nullptr); void PrepareShutdown(NodeContext& node); /** diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp index d750d50a8e6adc..7c51e7255ac232 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -318,7 +318,7 @@ class NodeImpl : public Node } 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 { @@ -480,7 +480,8 @@ class NodeImpl : public Node 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; @@ -581,15 +582,8 @@ class NodeImpl : public Node m_gov.setContext(context); m_llmq.setContext(context); m_masternodeSync.setContext(context); - - if (context) { - m_context_ref = *context; - } else { - m_context_ref = std::monostate{}; - } } NodeContext* m_context{nullptr}; - CoreContext m_context_ref{std::monostate{}}; }; bool FillBlock(const CBlockIndex* index, const FoundBlock& block, UniqueLock& lock, const CChain& active) diff --git a/src/rest.cpp b/src/rest.cpp index 3ba29564fec869..ad2f6a104912d4 100644 --- a/src/rest.cpp +++ b/src/rest.cpp @@ -346,7 +346,8 @@ static bool rest_chaininfo(const CoreContext& context, HTTPRequest* req, const s 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"; @@ -724,7 +725,7 @@ static const struct { void StartREST(const CoreContext& 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.cpp b/src/rpc/request.cpp index 992b76e766ed69..4c45c33c43a9fe 100644 --- a/src/rpc/request.cpp +++ b/src/rpc/request.cpp @@ -185,7 +185,7 @@ void JSONRPCRequest::parse(const UniValue& valRequest) throw JSONRPCError(RPC_INVALID_REQUEST, "Params must be an array or object"); } -const JSONRPCRequest JSONRPCRequest::squashed() const +JSONRPCRequest JSONRPCRequest::squashed() const { if (params.empty()) { return *this; diff --git a/src/rpc/request.h b/src/rpc/request.h index d849f6ee21c90c..41e9a72d684754 100644 --- a/src/rpc/request.h +++ b/src/rpc/request.h @@ -36,22 +36,11 @@ class JSONRPCRequest std::string URI; std::string authUser; std::string peerAddr; - const CoreContext& context; - - explicit JSONRPCRequest(const CoreContext& 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 CoreContext& context) - : id(other.id), strMethod(other.strMethod), params(other.params), mode(other.mode), URI(other.URI), - authUser(other.authUser), peerAddr(other.peerAddr), context(context) - { - } + CoreContext context{std::monostate()}; void parse(const UniValue& valRequest); // Returns new JSONRPCRequest with the first param "squashed' into strMethod - const JSONRPCRequest squashed() const; + JSONRPCRequest squashed() const; }; #endif // BITCOIN_RPC_REQUEST_H diff --git a/src/test/rpc_tests.cpp b/src/test/rpc_tests.cpp index 75f9b96bd243e6..687530b49fb72b 100644 --- a/src/test/rpc_tests.cpp +++ b/src/test/rpc_tests.cpp @@ -52,7 +52,8 @@ UniValue RPCTestingSetup::TransformParams(const UniValue& params, std::vector bool { transformed_params = request.params; return true; }, arg_names, /*unique_id=*/0}; table.appendCommand("method", &command); CoreContext context{m_node}; - JSONRPCRequest request(context); + JSONRPCRequest request; + request.context = context; request.strMethod = "method"; request.params = params; if (RPCIsInWarmup(nullptr)) SetRPCWarmupFinished(); @@ -66,7 +67,8 @@ UniValue RPCTestingSetup::CallRPC(std::string args) std::string strMethod = vArgs[0]; vArgs.erase(vArgs.begin()); CoreContext context{m_node}; - JSONRPCRequest request(context); + JSONRPCRequest request; + request.context = context; 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 9f18bf7e353365..ac422a870de8fa 100644 --- a/src/wallet/interfaces.cpp +++ b/src/wallet/interfaces.cpp @@ -573,7 +573,9 @@ class WalletLoaderImpl : public WalletLoader { 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())); } diff --git a/src/wallet/test/wallet_tests.cpp b/src/wallet/test/wallet_tests.cpp index c6fc807db18fb5..6ec9cfbfd15110 100644 --- a/src/wallet/test/wallet_tests.cpp +++ b/src/wallet/test/wallet_tests.cpp @@ -229,8 +229,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); - CoreContext context{m_node}; - JSONRPCRequest request(context); + JSONRPCRequest request; request.params.setArray(); request.params.push_back(keys); @@ -281,9 +280,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup) AddWallet(wallet); wallet->SetLastBlockProcessed(m_node.chainman->ActiveChain().Height(), m_node.chainman->ActiveChain().Tip()->GetBlockHash()); } - CoreContext context{m_node}; - JSONRPCRequest request(context); - + JSONRPCRequest request; request.params.setArray(); request.params.push_back(backup_file); @@ -298,8 +295,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup) LOCK(wallet->cs_wallet); wallet->SetupLegacyScriptPubKeyMan(); - CoreContext context{m_node}; - JSONRPCRequest request(context); + JSONRPCRequest request; request.params.setArray(); request.params.push_back(backup_file); AddWallet(wallet); @@ -324,7 +320,8 @@ BOOST_FIXTURE_TEST_CASE(rpc_getaddressinfo, TestChain100Setup) wallet->SetupLegacyScriptPubKeyMan(); AddWallet(wallet); CoreContext context{m_node}; - JSONRPCRequest request(context); + JSONRPCRequest request; + request.context = context; UniValue response; // test p2pkh @@ -798,7 +795,9 @@ class CreateTransactionTestSetup : public TestChain100Setup CoreContext context{m_node}; std::vector vecRecipients; for (auto entry : vecEntries) { - vecRecipients.push_back({GetScriptForDestination(DecodeDestination(getnewaddress().HandleRequest(JSONRPCRequest(context)).get_str())), entry.first, entry.second}); + JSONRPCRequest request; + request.context = context; + vecRecipients.push_back({GetScriptForDestination(DecodeDestination(getnewaddress().HandleRequest(request).get_str())), entry.first, entry.second}); } return vecRecipients; }