diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp index 1b2d97faff5..75911d00874 100644 --- a/src/bitcoin-cli.cpp +++ b/src/bitcoin-cli.cpp @@ -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); } +std::optional 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 */ static void libevent_log_cb(int severity, const char *msg) { @@ -1183,10 +1190,8 @@ static void ParseGetInfoResult(UniValue& result) */ static UniValue GetNewAddress() { - std::optional wallet_name{}; - if (gArgs.IsArgSet("-rpcwallet")) wallet_name = gArgs.GetArg("-rpcwallet", ""); 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) { // Perform RPC call - std::optional wallet_name{}; - if (gArgs.IsArgSet("-rpcwallet")) wallet_name = gArgs.GetArg("-rpcwallet", ""); + const std::optional wallet_name{RpcWalletName(gArgs)}; const UniValue reply = ConnectAndCallRPC(rh.get(), method, args, wallet_name); // Parse reply @@ -1300,7 +1304,7 @@ static int CommandLineRPC(int argc, char *argv[]) const UniValue& error = reply.find_value("error"); if (error.isNull()) { if (gArgs.GetBoolArg("-getinfo", false)) { - if (!gArgs.IsArgSet("-rpcwallet")) { + if (!wallet_name) { GetWalletBalances(result); // fetch multiwallet balances and append to result } ParseGetInfoResult(result); diff --git a/test/functional/interface_bitcoin_cli.py b/test/functional/interface_bitcoin_cli.py index 134b4e566be..2618c12e9f5 100755 --- a/test/functional/interface_bitcoin_cli.py +++ b/test/functional/interface_bitcoin_cli.py @@ -279,6 +279,10 @@ class TestBitcoinCli(BitcoinTestFramework): assert_equal(cli_get_info['Wallet'], wallets[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") 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)