Skip to content

Commit c985a34

Browse files
committed
Merge bitcoin/bitcoin#26990: cli: Improve error message on multiwallet cli-side commands
54227e6 rpc, cli: improve error message on multiwallet mode (pablomartin4btc) Pull request description: Running a CLI command when multiple wallets are loaded and `-rpcwallet` is not specified, should return a clearer error. Currently in `master`: ``` $ bitcoin-cli -regtest -generate 1 error code: -19 error message: Wallet file not specified (must request wallet RPC through /wallet/<filename> uri-path). Try adding "-rpcwallet=<filename>" option to bitcoin-cli command line. ``` With this change: ``` $ bitcoin-cli -regtest -generate 1 error code: -19 error message: Multiple wallets are loaded. Please select which wallet to use by requesting the RPC through the /wallet/<walletname> URI path. Or for the CLI, specify the "-rpcwallet=<walletname>" option before the command (run "bitcoin-cli -h" for help or "bitcoin-cli listwallets" to see which wallets are currently loaded). ``` ACKs for top commit: maflcko: review ACK 54227e6 achow101: ACK 54227e6 furszy: utACK 54227e6 mzumsande: Code Review ACK 54227e6 jonatack: ACK 54227e6 Tree-SHA512: 51ff24f64858aa6be6adf6f20105c9f076ebea743780bf2a4399f7fe8b5239cbb1ea06d32b2ef5e850da2369abb0ef7a52c50c2b8f31f4ca90d3a486abc9b77e
2 parents 79f20fa + 54227e6 commit c985a34

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

src/bitcoin-cli.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,8 @@ static void ParseError(const UniValue& error, std::string& strPrint, int& nRet)
950950
strPrint += ("error message:\n" + err_msg.get_str());
951951
}
952952
if (err_code.isNum() && err_code.getInt<int>() == RPC_WALLET_NOT_SPECIFIED) {
953-
strPrint += "\nTry adding \"-rpcwallet=<filename>\" option to bitcoin-cli command line.";
953+
strPrint += " Or for the CLI, specify the \"-rpcwallet=<walletname>\" option before the command";
954+
strPrint += " (run \"bitcoin-cli -h\" for help or \"bitcoin-cli listwallets\" to see which wallets are currently loaded).";
954955
}
955956
} else {
956957
strPrint = "error: " + error.write();

src/wallet/rpc/util.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ std::shared_ptr<CWallet> GetWalletForJSONRPCRequest(const JSONRPCRequest& reques
9191
RPC_WALLET_NOT_FOUND, "No wallet is loaded. Load a wallet using loadwallet or create a new one with createwallet. (Note: A default wallet is no longer automatically created)");
9292
}
9393
throw JSONRPCError(RPC_WALLET_NOT_SPECIFIED,
94-
"Wallet file not specified (must request wallet RPC through /wallet/<filename> uri-path).");
94+
"Multiple wallets are loaded. Please select which wallet to use by requesting the RPC through the /wallet/<walletname> URI path.");
9595
}
9696

9797
void EnsureWalletIsUnlocked(const CWallet& wallet)

test/functional/interface_bitcoin_cli.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@
3030
BLOCKS_VALUE_OF_ZERO = 'error: the first argument (number of blocks to generate, default: 1) must be an integer value greater than zero'
3131
TOO_MANY_ARGS = 'error: too many arguments (maximum 2 for nblocks and maxtries)'
3232
WALLET_NOT_LOADED = 'Requested wallet does not exist or is not loaded'
33-
WALLET_NOT_SPECIFIED = 'Wallet file not specified'
33+
WALLET_NOT_SPECIFIED = (
34+
"Multiple wallets are loaded. Please select which wallet to use by requesting the RPC "
35+
"through the /wallet/<walletname> URI path. Or for the CLI, specify the \"-rpcwallet=<walletname>\" "
36+
"option before the command (run \"bitcoin-cli -h\" for help or \"bitcoin-cli listwallets\" to see "
37+
"which wallets are currently loaded)."
38+
)
3439

3540

3641
def cli_get_info_string_to_dict(cli_get_info_string):
@@ -331,6 +336,10 @@ def run_test(self):
331336
n4 = 10
332337
blocks = self.nodes[0].getblockcount()
333338

339+
self.log.info('Test -generate -rpcwallet=<filename> raise RPC error')
340+
wallet2_path = f'-rpcwallet={self.nodes[0].wallets_path / wallets[2] / self.wallet_data_filename}'
341+
assert_raises_rpc_error(-18, WALLET_NOT_LOADED, self.nodes[0].cli(wallet2_path, '-generate').echo)
342+
334343
self.log.info('Test -generate -rpcwallet with no args')
335344
generate = self.nodes[0].cli(rpcwallet2, '-generate').send_cli()
336345
assert_equal(set(generate.keys()), {'address', 'blocks'})

test/functional/wallet_multiwallet.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def wallet_file(name):
229229
assert_raises_rpc_error(-18, "Requested wallet does not exist or is not loaded", wallet_bad.getwalletinfo)
230230

231231
# accessing wallet RPC without using wallet endpoint fails
232-
assert_raises_rpc_error(-19, "Wallet file not specified", node.getwalletinfo)
232+
assert_raises_rpc_error(-19, "Multiple wallets are loaded. Please select which wallet", node.getwalletinfo)
233233

234234
w1, w2, w3, w4, *_ = wallets
235235
self.generatetoaddress(node, nblocks=COINBASE_MATURITY + 1, address=w1.getnewaddress(), sync_fun=self.no_op)
@@ -275,7 +275,7 @@ def wallet_file(name):
275275
loadwallet_name = node.loadwallet(wallet_names[1])
276276
assert_equal(loadwallet_name['name'], wallet_names[1])
277277
assert_equal(node.listwallets(), wallet_names[0:2])
278-
assert_raises_rpc_error(-19, "Wallet file not specified", node.getwalletinfo)
278+
assert_raises_rpc_error(-19, "Multiple wallets are loaded. Please select which wallet", node.getwalletinfo)
279279
w2 = node.get_wallet_rpc(wallet_names[1])
280280
w2.getwalletinfo()
281281

0 commit comments

Comments
 (0)