Skip to content

Commit

Permalink
Merge #21574: Drop JSONRPCRequest constructors after #21366
Browse files Browse the repository at this point in the history
9044522 Drop JSONRPCRequest constructors after #21366 (Russell Yanofsky)

Pull request description:

  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.

ACKs for top commit:
  promag:
    ACK 9044522, fixed conflict in src/wallet/interfaces.cpp.

Tree-SHA512: e909411b8f75013620b94e1a609296befb832fdcb574cd2e6689bfe3c636b03cd4ac1ccb2b32b532daf0f2131bb043464024966310fffc7e3cad77713d4bd0ef
  • Loading branch information
MarcoFalke committed Apr 8, 2021
2 parents 2e9031f + 9044522 commit 6664211
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/bitcoind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()");
Expand Down
5 changes: 3 additions & 2 deletions src/httprpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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."));
}

Expand Down
2 changes: 1 addition & 1 deletion src/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 3 additions & 8 deletions src/node/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,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
{
Expand Down Expand Up @@ -244,7 +244,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;
Expand Down Expand Up @@ -314,14 +315,8 @@ class NodeImpl : public Node
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<RecursiveMutex>& lock, const CChain& active)
Expand Down
5 changes: 3 additions & 2 deletions src/rest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
}
}
Expand Down
13 changes: 1 addition & 12 deletions src/rpc/request.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,7 @@ class JSONRPCRequest
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);
};
Expand Down
4 changes: 2 additions & 2 deletions src/test/rpc_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 6 additions & 2 deletions src/wallet/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,15 +514,19 @@ class WalletClientImpl : public WalletClient
{
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()));
}

#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()));
}
Expand Down
9 changes: 3 additions & 6 deletions src/wallet/test/wallet_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand All @@ -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);
Expand Down

0 comments on commit 6664211

Please sign in to comment.