Fix nonsensical bitcoin-cli -norpcwallet behavior

Treat specifying -norpcwallet the same as not specifying any -rpcwallet option,
instead of treating it like -rpcwallet=0 with 0 as the wallet name.

This restores previous behavior before 743077544b
from https://github.com/bitcoin/bitcoin/pull/18594, which inadvertently changed
it.
This commit is contained in:
Ryan Ofsky 2019-12-19 18:00:04 -05:00
parent 6e8e7f433f
commit 5544a19f86
2 changed files with 14 additions and 6 deletions

View file

@ -110,6 +110,13 @@ static void SetupCliArgs(ArgsManager& argsman)
argsman.AddArg("-stdinwalletpassphrase", "Read wallet passphrase from standard input as a single line. When combined with -stdin, the first line from standard input is used for the wallet passphrase.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS); argsman.AddArg("-stdinwalletpassphrase", "Read wallet passphrase from standard input as a single line. When combined with -stdin, the first line from standard input is used for the wallet passphrase.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
} }
std::optional<std::string> RpcWalletName(const ArgsManager& args)
{
// Check IsArgNegated to return nullopt instead of "0" if -norpcwallet is specified
if (args.IsArgNegated("-rpcwallet")) return std::nullopt;
return args.GetArg("-rpcwallet");
}
/** libevent event log callback */ /** libevent event log callback */
static void libevent_log_cb(int severity, const char *msg) static void libevent_log_cb(int severity, const char *msg)
{ {
@ -1183,10 +1190,8 @@ static void ParseGetInfoResult(UniValue& result)
*/ */
static UniValue GetNewAddress() static UniValue GetNewAddress()
{ {
std::optional<std::string> wallet_name{};
if (gArgs.IsArgSet("-rpcwallet")) wallet_name = gArgs.GetArg("-rpcwallet", "");
DefaultRequestHandler rh; DefaultRequestHandler rh;
return ConnectAndCallRPC(&rh, "getnewaddress", /* args=*/{}, wallet_name); return ConnectAndCallRPC(&rh, "getnewaddress", /* args=*/{}, RpcWalletName(gArgs));
} }
/** /**
@ -1291,8 +1296,7 @@ static int CommandLineRPC(int argc, char *argv[])
} }
if (nRet == 0) { if (nRet == 0) {
// Perform RPC call // Perform RPC call
std::optional<std::string> wallet_name{}; const std::optional<std::string> wallet_name{RpcWalletName(gArgs)};
if (gArgs.IsArgSet("-rpcwallet")) wallet_name = gArgs.GetArg("-rpcwallet", "");
const UniValue reply = ConnectAndCallRPC(rh.get(), method, args, wallet_name); const UniValue reply = ConnectAndCallRPC(rh.get(), method, args, wallet_name);
// Parse reply // Parse reply
@ -1300,7 +1304,7 @@ static int CommandLineRPC(int argc, char *argv[])
const UniValue& error = reply.find_value("error"); const UniValue& error = reply.find_value("error");
if (error.isNull()) { if (error.isNull()) {
if (gArgs.GetBoolArg("-getinfo", false)) { if (gArgs.GetBoolArg("-getinfo", false)) {
if (!gArgs.IsArgSet("-rpcwallet")) { if (!wallet_name) {
GetWalletBalances(result); // fetch multiwallet balances and append to result GetWalletBalances(result); // fetch multiwallet balances and append to result
} }
ParseGetInfoResult(result); ParseGetInfoResult(result);

View file

@ -279,6 +279,10 @@ class TestBitcoinCli(BitcoinTestFramework):
assert_equal(cli_get_info['Wallet'], wallets[1]) assert_equal(cli_get_info['Wallet'], wallets[1])
assert_equal(Decimal(cli_get_info['Balance']), amounts[1]) assert_equal(Decimal(cli_get_info['Balance']), amounts[1])
self.log.info("Test -getinfo -norpcwallet returns the same as -getinfo")
# Previously there was a bug where -norpcwallet was treated like -rpcwallet=0
assert_equal(self.nodes[0].cli('-getinfo', "-norpcwallet").send_cli(), cli_get_info_string)
self.log.info("Test -getinfo with -rpcwallet=remaining-non-default-wallet returns only its balance") self.log.info("Test -getinfo with -rpcwallet=remaining-non-default-wallet returns only its balance")
cli_get_info_string = self.nodes[0].cli('-getinfo', rpcwallet2).send_cli() cli_get_info_string = self.nodes[0].cli('-getinfo', rpcwallet2).send_cli()
cli_get_info = cli_get_info_string_to_dict(cli_get_info_string) cli_get_info = cli_get_info_string_to_dict(cli_get_info_string)