Skip to content

Commit 9044522

Browse files
committed
Drop JSONRPCRequest constructors after #21366
This just makes an additional simplification after #21366 replaced util::Ref with std::any. It was originally suggested bitcoin/bitcoin#21366 (comment) but delayed for a followup. It would have prevented usage bug bitcoin/bitcoin#21572.
1 parent aa69471 commit 9044522

File tree

10 files changed

+28
-41
lines changed

10 files changed

+28
-41
lines changed

src/bitcoind.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ static bool AppInit(int argc, char* argv[])
224224
// If locking the data directory failed, exit immediately
225225
return false;
226226
}
227-
fRet = AppInitInterfaces(node) && AppInitMain(context, node);
227+
fRet = AppInitInterfaces(node) && AppInitMain(node);
228228
}
229229
catch (const std::exception& e) {
230230
PrintExceptionContinue(&e, "AppInit()");

src/httprpc.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ static bool HTTPReq_JSONRPC(const std::any& context, HTTPRequest* req)
159159
return false;
160160
}
161161

162-
JSONRPCRequest jreq(context);
162+
JSONRPCRequest jreq;
163+
jreq.context = context;
163164
jreq.peerAddr = req->GetPeer().ToString();
164165
if (!RPCAuthorized(authHeader.second, jreq.authUser)) {
165166
LogPrintf("ThreadRPCServer incorrect password attempt from %s\n", jreq.peerAddr);
@@ -294,7 +295,7 @@ bool StartHTTPRPC(const std::any& context)
294295
if (!InitRPCAuthentication())
295296
return false;
296297

297-
auto handle_rpc = [&context](HTTPRequest* req, const std::string&) { return HTTPReq_JSONRPC(context, req); };
298+
auto handle_rpc = [context](HTTPRequest* req, const std::string&) { return HTTPReq_JSONRPC(context, req); };
298299
RegisterHTTPHandler("/", true, handle_rpc);
299300
if (g_wallet_init_interface.HasWalletSupport()) {
300301
RegisterHTTPHandler("/wallet/", false, handle_rpc);

src/init.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ static bool InitSanityCheck()
788788
return true;
789789
}
790790

791-
static bool AppInitServers(const std::any& context, NodeContext& node)
791+
static bool AppInitServers(NodeContext& node)
792792
{
793793
const ArgsManager& args = *Assert(node.args);
794794
RPCServer::OnStarted(&OnRPCStarted);
@@ -797,9 +797,9 @@ static bool AppInitServers(const std::any& context, NodeContext& node)
797797
return false;
798798
StartRPC();
799799
node.rpc_interruption_point = RpcInterruptionPoint;
800-
if (!StartHTTPRPC(context))
800+
if (!StartHTTPRPC(&node))
801801
return false;
802-
if (args.GetBoolArg("-rest", DEFAULT_REST_ENABLE)) StartREST(context);
802+
if (args.GetBoolArg("-rest", DEFAULT_REST_ENABLE)) StartREST(&node);
803803
StartHTTPServer();
804804
return true;
805805
}
@@ -1277,7 +1277,7 @@ bool AppInitInterfaces(NodeContext& node)
12771277
return true;
12781278
}
12791279

1280-
bool AppInitMain(const std::any& context, NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
1280+
bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
12811281
{
12821282
const ArgsManager& args = *Assert(node.args);
12831283
const CChainParams& chainparams = Params();
@@ -1382,7 +1382,7 @@ bool AppInitMain(const std::any& context, NodeContext& node, interfaces::BlockAn
13821382
*/
13831383
if (args.GetBoolArg("-server", false)) {
13841384
uiInterface.InitMessage_connect(SetRPCWarmupStatus);
1385-
if (!AppInitServers(context, node))
1385+
if (!AppInitServers(node))
13861386
return InitError(_("Unable to start HTTP server. See debug log for details."));
13871387
}
13881388

src/init.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ bool AppInitInterfaces(NodeContext& node);
6464
* @note This should only be done after daemonization. Call Shutdown() if this function fails.
6565
* @pre Parameters should be parsed and config file should be read, AppInitLockDataDirectory should have been called.
6666
*/
67-
bool AppInitMain(const std::any& context, NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info = nullptr);
67+
bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info = nullptr);
6868

6969
/**
7070
* Register all arguments with the ArgsManager

src/node/interfaces.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class NodeImpl : public Node
8080
}
8181
bool appInitMain(interfaces::BlockAndHeaderTipInfo* tip_info) override
8282
{
83-
return AppInitMain(m_context_ref, *m_context, tip_info);
83+
return AppInitMain(*m_context, tip_info);
8484
}
8585
void appShutdown() override
8686
{
@@ -244,7 +244,8 @@ class NodeImpl : public Node
244244
CFeeRate getDustRelayFee() override { return ::dustRelayFee; }
245245
UniValue executeRpc(const std::string& command, const UniValue& params, const std::string& uri) override
246246
{
247-
JSONRPCRequest req(m_context_ref);
247+
JSONRPCRequest req;
248+
req.context = m_context;
248249
req.params = params;
249250
req.strMethod = command;
250251
req.URI = uri;
@@ -314,14 +315,8 @@ class NodeImpl : public Node
314315
void setContext(NodeContext* context) override
315316
{
316317
m_context = context;
317-
if (context) {
318-
m_context_ref = context;
319-
} else {
320-
m_context_ref.reset();
321-
}
322318
}
323319
NodeContext* m_context{nullptr};
324-
std::any m_context_ref;
325320
};
326321

327322
bool FillBlock(const CBlockIndex* index, const FoundBlock& block, UniqueLock<RecursiveMutex>& lock, const CChain& active)

src/rest.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,8 @@ static bool rest_chaininfo(const std::any& context, HTTPRequest* req, const std:
317317

318318
switch (rf) {
319319
case RetFormat::JSON: {
320-
JSONRPCRequest jsonRequest(context);
320+
JSONRPCRequest jsonRequest;
321+
jsonRequest.context = context;
321322
jsonRequest.params = UniValue(UniValue::VARR);
322323
UniValue chainInfoObject = getblockchaininfo().HandleRequest(jsonRequest);
323324
std::string strJSON = chainInfoObject.write() + "\n";
@@ -687,7 +688,7 @@ static const struct {
687688
void StartREST(const std::any& context)
688689
{
689690
for (const auto& up : uri_prefixes) {
690-
auto handler = [&context, up](HTTPRequest* req, const std::string& prefix) { return up.handler(context, req, prefix); };
691+
auto handler = [context, up](HTTPRequest* req, const std::string& prefix) { return up.handler(context, req, prefix); };
691692
RegisterHTTPHandler(up.prefix, false, handler);
692693
}
693694
}

src/rpc/request.h

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,7 @@ class JSONRPCRequest
3535
std::string URI;
3636
std::string authUser;
3737
std::string peerAddr;
38-
const std::any& context;
39-
40-
explicit JSONRPCRequest(const std::any& context) : id(NullUniValue), params(NullUniValue), context(context) {}
41-
42-
//! Initializes request information from another request object and the
43-
//! given context. The implementation should be updated if any members are
44-
//! added or removed above.
45-
JSONRPCRequest(const JSONRPCRequest& other, const std::any& context)
46-
: id(other.id), strMethod(other.strMethod), params(other.params), mode(other.mode), URI(other.URI),
47-
authUser(other.authUser), peerAddr(other.peerAddr), context(context)
48-
{
49-
}
38+
std::any context;
5039

5140
void parse(const UniValue& valRequest);
5241
};

src/test/rpc_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ UniValue RPCTestingSetup::CallRPC(std::string args)
3333
boost::split(vArgs, args, boost::is_any_of(" \t"));
3434
std::string strMethod = vArgs[0];
3535
vArgs.erase(vArgs.begin());
36-
std::any context{&m_node};
37-
JSONRPCRequest request(context);
36+
JSONRPCRequest request;
37+
request.context = &m_node;
3838
request.strMethod = strMethod;
3939
request.params = RPCConvertValues(strMethod, vArgs);
4040
if (RPCIsInWarmup(nullptr)) SetRPCWarmupFinished();

src/wallet/interfaces.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,15 +514,19 @@ class WalletClientImpl : public WalletClient
514514
{
515515
for (const CRPCCommand& command : GetWalletRPCCommands()) {
516516
m_rpc_commands.emplace_back(command.category, command.name, [this, &command](const JSONRPCRequest& request, UniValue& result, bool last_handler) {
517-
return command.actor({request, &m_context}, result, last_handler);
517+
JSONRPCRequest wallet_request = request;
518+
wallet_request.context = &m_context;
519+
return command.actor(wallet_request, result, last_handler);
518520
}, command.argNames, command.unique_id);
519521
m_rpc_handlers.emplace_back(m_context.chain->handleRpc(m_rpc_commands.back()));
520522
}
521523

522524
#ifdef ENABLE_EXTERNAL_SIGNER
523525
for (const CRPCCommand& command : GetSignerRPCCommands()) {
524526
m_rpc_commands.emplace_back(command.category, command.name, [this, &command](const JSONRPCRequest& request, UniValue& result, bool last_handler) {
525-
return command.actor({request, &m_context}, result, last_handler);
527+
JSONRPCRequest wallet_request = request;
528+
wallet_request.context = &m_context;
529+
return command.actor(wallet_request, result, last_handler);
526530
}, command.argNames, command.unique_id);
527531
m_rpc_handlers.emplace_back(m_context.chain->handleRpc(m_rpc_commands.back()));
528532
}

src/wallet/test/wallet_tests.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,7 @@ BOOST_FIXTURE_TEST_CASE(importmulti_rescan, TestChain100Setup)
213213
key.pushKV("timestamp", newTip->GetBlockTimeMax() + TIMESTAMP_WINDOW + 1);
214214
key.pushKV("internal", UniValue(true));
215215
keys.push_back(key);
216-
std::any context;
217-
JSONRPCRequest request(context);
216+
JSONRPCRequest request;
218217
request.params.setArray();
219218
request.params.push_back(keys);
220219

@@ -265,8 +264,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
265264
AddWallet(wallet);
266265
wallet->SetLastBlockProcessed(::ChainActive().Height(), ::ChainActive().Tip()->GetBlockHash());
267266
}
268-
std::any context;
269-
JSONRPCRequest request(context);
267+
JSONRPCRequest request;
270268
request.params.setArray();
271269
request.params.push_back(backup_file);
272270

@@ -281,8 +279,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
281279
LOCK(wallet->cs_wallet);
282280
wallet->SetupLegacyScriptPubKeyMan();
283281

284-
std::any context;
285-
JSONRPCRequest request(context);
282+
JSONRPCRequest request;
286283
request.params.setArray();
287284
request.params.push_back(backup_file);
288285
AddWallet(wallet);

0 commit comments

Comments
 (0)