Skip to content

Commit 7430775

Browse files
committed
cli: lift -rpcwallet logic up to CommandLineRPC()
to allow passing rpcwallet independently from the -rpcwallet user option, and to move the logic to the top-level layer where most of the other option args are handled.
1 parent 29f2cbd commit 7430775

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/bitcoin-cli.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <chainparamsbase.h>
1111
#include <clientversion.h>
12+
#include <optional.h>
1213
#include <rpc/client.h>
1314
#include <rpc/protocol.h>
1415
#include <rpc/request.h>
@@ -304,7 +305,7 @@ class DefaultRequestHandler: public BaseRequestHandler {
304305
}
305306
};
306307

307-
static UniValue CallRPC(BaseRequestHandler *rh, const std::string& strMethod, const std::vector<std::string>& args)
308+
static UniValue CallRPC(BaseRequestHandler* rh, const std::string& strMethod, const std::vector<std::string>& args, const Optional<std::string>& rpcwallet = {})
308309
{
309310
std::string host;
310311
// In preference order, we choose the following for the port:
@@ -369,14 +370,12 @@ static UniValue CallRPC(BaseRequestHandler *rh, const std::string& strMethod, co
369370

370371
// check if we should use a special wallet endpoint
371372
std::string endpoint = "/";
372-
if (!gArgs.GetArgs("-rpcwallet").empty()) {
373-
std::string walletName = gArgs.GetArg("-rpcwallet", "");
374-
char *encodedURI = evhttp_uriencode(walletName.data(), walletName.size(), false);
373+
if (rpcwallet) {
374+
char* encodedURI = evhttp_uriencode(rpcwallet->data(), rpcwallet->size(), false);
375375
if (encodedURI) {
376-
endpoint = "/wallet/"+ std::string(encodedURI);
376+
endpoint = "/wallet/" + std::string(encodedURI);
377377
free(encodedURI);
378-
}
379-
else {
378+
} else {
380379
throw CConnectionFailed("uri-encode failed");
381380
}
382381
}
@@ -423,17 +422,18 @@ static UniValue CallRPC(BaseRequestHandler *rh, const std::string& strMethod, co
423422
*
424423
* @param[in] rh Pointer to RequestHandler.
425424
* @param[in] strMethod Reference to const string method to forward to CallRPC.
425+
* @param[in] rpcwallet Reference to const optional string wallet name to forward to CallRPC.
426426
* @returns the RPC response as a UniValue object.
427427
* @throws a CConnectionFailed std::runtime_error if connection failed or RPC server still in warmup.
428428
*/
429-
static UniValue ConnectAndCallRPC(BaseRequestHandler* rh, const std::string& strMethod, const std::vector<std::string>& args)
429+
static UniValue ConnectAndCallRPC(BaseRequestHandler* rh, const std::string& strMethod, const std::vector<std::string>& args, const Optional<std::string>& rpcwallet = {})
430430
{
431431
UniValue response(UniValue::VOBJ);
432432
// Execute and handle connection failures with -rpcwait.
433433
const bool fWait = gArgs.GetBoolArg("-rpcwait", false);
434434
do {
435435
try {
436-
response = CallRPC(rh, strMethod, args);
436+
response = CallRPC(rh, strMethod, args, rpcwallet);
437437
if (fWait) {
438438
const UniValue& error = find_value(response, "error");
439439
if (!error.isNull() && error["code"].get_int() == RPC_IN_WARMUP) {
@@ -519,7 +519,9 @@ static int CommandLineRPC(int argc, char *argv[])
519519
method = args[0];
520520
args.erase(args.begin()); // Remove trailing method name from arguments vector
521521
}
522-
const UniValue reply = ConnectAndCallRPC(rh.get(), method, args);
522+
Optional<std::string> wallet_name{};
523+
if (gArgs.IsArgSet("-rpcwallet")) wallet_name = gArgs.GetArg("-rpcwallet", "");
524+
const UniValue reply = ConnectAndCallRPC(rh.get(), method, args, wallet_name);
523525

524526
// Parse reply
525527
UniValue result = find_value(reply, "result");

0 commit comments

Comments
 (0)