Changeset View
Changeset View
Standalone View
Standalone View
src/bitcoin-cli.cpp
Show First 20 Lines • Show All 866 Lines • ▼ Show 20 Lines | do { | ||||
} else { | } else { | ||||
throw; | throw; | ||||
} | } | ||||
} | } | ||||
} while (fWait); | } while (fWait); | ||||
return response; | return response; | ||||
} | } | ||||
/** Parse UniValue result to update the message to print to std::cout. */ | |||||
static void ParseResult(const UniValue &result, std::string &strPrint) { | |||||
if (result.isNull()) { | |||||
return; | |||||
} | |||||
strPrint = result.isStr() ? result.get_str() : result.write(2); | |||||
} | |||||
/** | |||||
* Parse UniValue error to update the message to print to std::cerr and the | |||||
* code to return. | |||||
*/ | |||||
static void ParseError(const UniValue &error, std::string &strPrint, | |||||
int &nRet) { | |||||
if (error.isObject()) { | |||||
const UniValue &err_code = find_value(error, "code"); | |||||
const UniValue &err_msg = find_value(error, "message"); | |||||
if (!err_code.isNull()) { | |||||
strPrint = "error code: " + err_code.getValStr() + "\n"; | |||||
} | |||||
if (err_msg.isStr()) { | |||||
strPrint += ("error message:\n" + err_msg.get_str()); | |||||
} | |||||
if (err_code.isNum() && | |||||
err_code.get_int() == RPC_WALLET_NOT_SPECIFIED) { | |||||
strPrint += "\nTry adding \"-rpcwallet=<filename>\" option to " | |||||
"bitcoin-cli command line."; | |||||
} | |||||
} else { | |||||
strPrint = "error: " + error.write(); | |||||
} | |||||
nRet = abs(error["code"].get_int()); | |||||
} | |||||
/** | /** | ||||
* GetWalletBalances calls listwallets; if more than one wallet is loaded, it | * GetWalletBalances calls listwallets; if more than one wallet is loaded, it | ||||
* then fetches mine.trusted balances for each loaded wallet and pushes them to | * then fetches mine.trusted balances for each loaded wallet and pushes them to | ||||
* `result`. | * `result`. | ||||
* | * | ||||
* @param result Reference to UniValue object the wallet names and balances are | * @param result Reference to UniValue object the wallet names and balances are | ||||
* pushed to. | * pushed to. | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 103 Lines • ▼ Show 20 Lines | try { | ||||
throw std::runtime_error( | throw std::runtime_error( | ||||
"too few parameters (need at least command)"); | "too few parameters (need at least command)"); | ||||
} | } | ||||
method = args[0]; | method = args[0]; | ||||
// Remove trailing method name from arguments vector | // Remove trailing method name from arguments vector | ||||
args.erase(args.begin()); | args.erase(args.begin()); | ||||
} | } | ||||
if (nRet == 0) { | |||||
// Perform RPC call | |||||
std::optional<std::string> wallet_name{}; | std::optional<std::string> wallet_name{}; | ||||
if (gArgs.IsArgSet("-rpcwallet")) { | if (gArgs.IsArgSet("-rpcwallet")) { | ||||
wallet_name = gArgs.GetArg("-rpcwallet", ""); | wallet_name = gArgs.GetArg("-rpcwallet", ""); | ||||
} | } | ||||
const UniValue reply = | const UniValue reply = | ||||
ConnectAndCallRPC(rh.get(), method, args, wallet_name); | ConnectAndCallRPC(rh.get(), method, args, wallet_name); | ||||
// Parse reply | // Parse reply | ||||
UniValue result = find_value(reply, "result"); | UniValue result = find_value(reply, "result"); | ||||
const UniValue &error = find_value(reply, "error"); | const UniValue &error = find_value(reply, "error"); | ||||
if (!error.isNull()) { | if (error.isNull()) { | ||||
// Error | if (gArgs.IsArgSet("-getinfo") && | ||||
strPrint = "error: " + error.write(); | !gArgs.IsArgSet("-rpcwallet")) { | ||||
nRet = abs(error["code"].get_int()); | |||||
if (error.isObject()) { | |||||
const UniValue &errCode = find_value(error, "code"); | |||||
const UniValue &errMsg = find_value(error, "message"); | |||||
strPrint = errCode.isNull() | |||||
? "" | |||||
: ("error code: " + errCode.getValStr() + "\n"); | |||||
if (errMsg.isStr()) { | |||||
strPrint += ("error message:\n" + errMsg.get_str()); | |||||
} | |||||
if (errCode.isNum() && | |||||
errCode.get_int() == RPC_WALLET_NOT_SPECIFIED) { | |||||
strPrint += "\nTry adding \"-rpcwallet=<filename>\" option " | |||||
"to bitcoin-cli command line."; | |||||
} | |||||
} | |||||
} else { | |||||
if (gArgs.IsArgSet("-getinfo") && !gArgs.IsArgSet("-rpcwallet")) { | |||||
// fetch multiwallet balances and append to result | // fetch multiwallet balances and append to result | ||||
GetWalletBalances(result); | GetWalletBalances(result); | ||||
} | } | ||||
// Result | ParseResult(result, strPrint); | ||||
if (result.isNull()) { | |||||
strPrint = ""; | |||||
} else if (result.isStr()) { | |||||
strPrint = result.get_str(); | |||||
} else { | } else { | ||||
strPrint = result.write(2); | ParseError(error, strPrint, nRet); | ||||
} | } | ||||
} | } | ||||
} catch (const std::exception &e) { | } catch (const std::exception &e) { | ||||
strPrint = std::string("error: ") + e.what(); | strPrint = std::string("error: ") + e.what(); | ||||
nRet = EXIT_FAILURE; | nRet = EXIT_FAILURE; | ||||
} catch (...) { | } catch (...) { | ||||
PrintExceptionContinue(nullptr, "CommandLineRPC()"); | PrintExceptionContinue(nullptr, "CommandLineRPC()"); | ||||
throw; | throw; | ||||
▲ Show 20 Lines • Show All 43 Lines • Show Last 20 Lines |