Skip to content

Commit

Permalink
cli: lift -rpcwallet logic up to CommandLineRPC()
Browse files Browse the repository at this point in the history
Summary:
> 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.

This is a backport of Core [[bitcoin/bitcoin#18594 | PR18594]] [2/6]
bitcoin/bitcoin@7430775

Depends on D9237

Test Plan: `ninja all check-all`

Reviewers: #bitcoin_abc, majcosta

Reviewed By: #bitcoin_abc, majcosta

Differential Revision: https://reviews.bitcoinabc.org/D9238
  • Loading branch information
jonatack authored and PiRK committed Feb 18, 2021
1 parent 61bb2ab commit 9aa48e1
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/bitcoin-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,8 @@ class DefaultRequestHandler : public BaseRequestHandler {
};

static UniValue CallRPC(BaseRequestHandler *rh, const std::string &strMethod,
const std::vector<std::string> &args) {
const std::vector<std::string> &args,
const std::optional<std::string> &rpcwallet = {}) {
std::string host;
// In preference order, we choose the following for the port:
// 1. -rpcport
Expand Down Expand Up @@ -462,10 +463,9 @@ static UniValue CallRPC(BaseRequestHandler *rh, const std::string &strMethod,

// check if we should use a special wallet endpoint
std::string endpoint = "/";
if (!gArgs.GetArgs("-rpcwallet").empty()) {
std::string walletName = gArgs.GetArg("-rpcwallet", "");
if (rpcwallet) {
char *encodedURI =
evhttp_uriencode(walletName.data(), walletName.size(), false);
evhttp_uriencode(rpcwallet->data(), rpcwallet->size(), false);
if (encodedURI) {
endpoint = "/wallet/" + std::string(encodedURI);
free(encodedURI);
Expand Down Expand Up @@ -535,19 +535,22 @@ static UniValue CallRPC(BaseRequestHandler *rh, const std::string &strMethod,
*
* @param[in] rh Pointer to RequestHandler.
* @param[in] strMethod Reference to const string method to forward to CallRPC.
* @param[in] rpcwallet Reference to const optional string wallet name to
* forward to CallRPC.
* @returns the RPC response as a UniValue object.
* @throws a CConnectionFailed std::runtime_error if connection failed or RPC
* server still in warmup.
*/
static UniValue ConnectAndCallRPC(BaseRequestHandler *rh,
const std::string &strMethod,
const std::vector<std::string> &args) {
static UniValue
ConnectAndCallRPC(BaseRequestHandler *rh, const std::string &strMethod,
const std::vector<std::string> &args,
const std::optional<std::string> &rpcwallet = {}) {
UniValue response(UniValue::VOBJ);
// Execute and handle connection failures with -rpcwait.
const bool fWait = gArgs.GetBoolArg("-rpcwait", false);
do {
try {
response = CallRPC(rh, strMethod, args);
response = CallRPC(rh, strMethod, args, rpcwallet);
if (fWait) {
const UniValue &error = find_value(response, "error");
if (!error.isNull() &&
Expand Down Expand Up @@ -642,7 +645,12 @@ static int CommandLineRPC(int argc, char *argv[]) {
args.erase(args.begin());
}

const UniValue reply = ConnectAndCallRPC(rh.get(), method, args);
std::optional<std::string> wallet_name{};
if (gArgs.IsArgSet("-rpcwallet")) {
wallet_name = gArgs.GetArg("-rpcwallet", "");
}
const UniValue reply =
ConnectAndCallRPC(rh.get(), method, args, wallet_name);

// Parse reply
UniValue result = find_value(reply, "result");
Expand Down

0 comments on commit 9aa48e1

Please sign in to comment.