mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-03-05 14:06:27 -05:00
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.
This commit is contained in:
parent
29f2cbdeb7
commit
743077544b
1 changed files with 12 additions and 10 deletions
|
@ -9,6 +9,7 @@
|
||||||
|
|
||||||
#include <chainparamsbase.h>
|
#include <chainparamsbase.h>
|
||||||
#include <clientversion.h>
|
#include <clientversion.h>
|
||||||
|
#include <optional.h>
|
||||||
#include <rpc/client.h>
|
#include <rpc/client.h>
|
||||||
#include <rpc/protocol.h>
|
#include <rpc/protocol.h>
|
||||||
#include <rpc/request.h>
|
#include <rpc/request.h>
|
||||||
|
@ -304,7 +305,7 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
static UniValue CallRPC(BaseRequestHandler *rh, const std::string& strMethod, const std::vector<std::string>& args)
|
static UniValue CallRPC(BaseRequestHandler* rh, const std::string& strMethod, const std::vector<std::string>& args, const Optional<std::string>& rpcwallet = {})
|
||||||
{
|
{
|
||||||
std::string host;
|
std::string host;
|
||||||
// In preference order, we choose the following for the port:
|
// In preference order, we choose the following for the port:
|
||||||
|
@ -369,14 +370,12 @@ static UniValue CallRPC(BaseRequestHandler *rh, const std::string& strMethod, co
|
||||||
|
|
||||||
// check if we should use a special wallet endpoint
|
// check if we should use a special wallet endpoint
|
||||||
std::string endpoint = "/";
|
std::string endpoint = "/";
|
||||||
if (!gArgs.GetArgs("-rpcwallet").empty()) {
|
if (rpcwallet) {
|
||||||
std::string walletName = gArgs.GetArg("-rpcwallet", "");
|
char* encodedURI = evhttp_uriencode(rpcwallet->data(), rpcwallet->size(), false);
|
||||||
char *encodedURI = evhttp_uriencode(walletName.data(), walletName.size(), false);
|
|
||||||
if (encodedURI) {
|
if (encodedURI) {
|
||||||
endpoint = "/wallet/"+ std::string(encodedURI);
|
endpoint = "/wallet/" + std::string(encodedURI);
|
||||||
free(encodedURI);
|
free(encodedURI);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
throw CConnectionFailed("uri-encode failed");
|
throw CConnectionFailed("uri-encode failed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -423,17 +422,18 @@ static UniValue CallRPC(BaseRequestHandler *rh, const std::string& strMethod, co
|
||||||
*
|
*
|
||||||
* @param[in] rh Pointer to RequestHandler.
|
* @param[in] rh Pointer to RequestHandler.
|
||||||
* @param[in] strMethod Reference to const string method to forward to CallRPC.
|
* @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.
|
* @returns the RPC response as a UniValue object.
|
||||||
* @throws a CConnectionFailed std::runtime_error if connection failed or RPC server still in warmup.
|
* @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 Optional<std::string>& rpcwallet = {})
|
||||||
{
|
{
|
||||||
UniValue response(UniValue::VOBJ);
|
UniValue response(UniValue::VOBJ);
|
||||||
// Execute and handle connection failures with -rpcwait.
|
// Execute and handle connection failures with -rpcwait.
|
||||||
const bool fWait = gArgs.GetBoolArg("-rpcwait", false);
|
const bool fWait = gArgs.GetBoolArg("-rpcwait", false);
|
||||||
do {
|
do {
|
||||||
try {
|
try {
|
||||||
response = CallRPC(rh, strMethod, args);
|
response = CallRPC(rh, strMethod, args, rpcwallet);
|
||||||
if (fWait) {
|
if (fWait) {
|
||||||
const UniValue& error = find_value(response, "error");
|
const UniValue& error = find_value(response, "error");
|
||||||
if (!error.isNull() && error["code"].get_int() == RPC_IN_WARMUP) {
|
if (!error.isNull() && error["code"].get_int() == RPC_IN_WARMUP) {
|
||||||
|
@ -519,7 +519,9 @@ static int CommandLineRPC(int argc, char *argv[])
|
||||||
method = args[0];
|
method = args[0];
|
||||||
args.erase(args.begin()); // Remove trailing method name from arguments vector
|
args.erase(args.begin()); // Remove trailing method name from arguments vector
|
||||||
}
|
}
|
||||||
const UniValue reply = ConnectAndCallRPC(rh.get(), method, args);
|
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
|
// Parse reply
|
||||||
UniValue result = find_value(reply, "result");
|
UniValue result = find_value(reply, "result");
|
||||||
|
|
Loading…
Add table
Reference in a new issue