Skip to content

Commit 5544a19

Browse files
committed
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 7430775 from bitcoin/bitcoin#18594, which inadvertently changed it.
1 parent 6e8e7f4 commit 5544a19

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/bitcoin-cli.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ static void SetupCliArgs(ArgsManager& argsman)
110110
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);
111111
}
112112

113+
std::optional<std::string> RpcWalletName(const ArgsManager& args)
114+
{
115+
// Check IsArgNegated to return nullopt instead of "0" if -norpcwallet is specified
116+
if (args.IsArgNegated("-rpcwallet")) return std::nullopt;
117+
return args.GetArg("-rpcwallet");
118+
}
119+
113120
/** libevent event log callback */
114121
static void libevent_log_cb(int severity, const char *msg)
115122
{
@@ -1183,10 +1190,8 @@ static void ParseGetInfoResult(UniValue& result)
11831190
*/
11841191
static UniValue GetNewAddress()
11851192
{
1186-
std::optional<std::string> wallet_name{};
1187-
if (gArgs.IsArgSet("-rpcwallet")) wallet_name = gArgs.GetArg("-rpcwallet", "");
11881193
DefaultRequestHandler rh;
1189-
return ConnectAndCallRPC(&rh, "getnewaddress", /* args=*/{}, wallet_name);
1194+
return ConnectAndCallRPC(&rh, "getnewaddress", /* args=*/{}, RpcWalletName(gArgs));
11901195
}
11911196

11921197
/**
@@ -1291,16 +1296,15 @@ static int CommandLineRPC(int argc, char *argv[])
12911296
}
12921297
if (nRet == 0) {
12931298
// Perform RPC call
1294-
std::optional<std::string> wallet_name{};
1295-
if (gArgs.IsArgSet("-rpcwallet")) wallet_name = gArgs.GetArg("-rpcwallet", "");
1299+
const std::optional<std::string> wallet_name{RpcWalletName(gArgs)};
12961300
const UniValue reply = ConnectAndCallRPC(rh.get(), method, args, wallet_name);
12971301

12981302
// Parse reply
12991303
UniValue result = reply.find_value("result");
13001304
const UniValue& error = reply.find_value("error");
13011305
if (error.isNull()) {
13021306
if (gArgs.GetBoolArg("-getinfo", false)) {
1303-
if (!gArgs.IsArgSet("-rpcwallet")) {
1307+
if (!wallet_name) {
13041308
GetWalletBalances(result); // fetch multiwallet balances and append to result
13051309
}
13061310
ParseGetInfoResult(result);

test/functional/interface_bitcoin_cli.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,10 @@ def run_test(self):
279279
assert_equal(cli_get_info['Wallet'], wallets[1])
280280
assert_equal(Decimal(cli_get_info['Balance']), amounts[1])
281281

282+
self.log.info("Test -getinfo -norpcwallet returns the same as -getinfo")
283+
# Previously there was a bug where -norpcwallet was treated like -rpcwallet=0
284+
assert_equal(self.nodes[0].cli('-getinfo', "-norpcwallet").send_cli(), cli_get_info_string)
285+
282286
self.log.info("Test -getinfo with -rpcwallet=remaining-non-default-wallet returns only its balance")
283287
cli_get_info_string = self.nodes[0].cli('-getinfo', rpcwallet2).send_cli()
284288
cli_get_info = cli_get_info_string_to_dict(cli_get_info_string)

0 commit comments

Comments
 (0)