Skip to content

Commit 53ac704

Browse files
rpc, test: Fix error message in unloadwallet
The unloadwallet RPC previously failed with a low-level JSON parsing error when called without any arguments (wallet_name). Although this issue was first identified during review of the original unloadwallet implementation in #13111, it was never addressed. Raise RPC_INVALID_PARAMETER instead describing that either the RPC endpoint or wallet name must be provided. Adding a new functional test for this use case. Refactor migratewallet to use the same logic as the wallet_name argument handling is identical. Co-authored-by: maflcko <[email protected]>
1 parent 1fc3a8e commit 53ac704

File tree

3 files changed

+8
-25
lines changed

3 files changed

+8
-25
lines changed

src/wallet/rpc/wallet.cpp

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,8 @@ static RPCHelpMan createwallet()
438438
static RPCHelpMan unloadwallet()
439439
{
440440
return RPCHelpMan{"unloadwallet",
441-
"Unloads the wallet referenced by the request endpoint, otherwise unloads the wallet specified in the argument.\n"
442-
"Specifying the wallet name on a wallet endpoint is invalid.",
441+
"Unloads the wallet referenced by the request endpoint or the wallet_name argument.\n"
442+
"If both are specified, they must be identical.",
443443
{
444444
{"wallet_name", RPCArg::Type::STR, RPCArg::DefaultHint{"the wallet name from the RPC endpoint"}, "The name of the wallet to unload. If provided both here and in the RPC endpoint, the two must be identical."},
445445
{"load_on_startup", RPCArg::Type::BOOL, RPCArg::Optional::OMITTED, "Save wallet name to persistent settings and load on startup. True to add wallet to startup list, false to remove, null to leave unchanged."},
@@ -456,14 +456,7 @@ static RPCHelpMan unloadwallet()
456456
},
457457
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
458458
{
459-
std::string wallet_name;
460-
if (GetWalletNameFromJSONRPCRequest(request, wallet_name)) {
461-
if (!(request.params[0].isNull() || request.params[0].get_str() == wallet_name)) {
462-
throw JSONRPCError(RPC_INVALID_PARAMETER, "RPC endpoint wallet and wallet_name parameter specify different wallets");
463-
}
464-
} else {
465-
wallet_name = request.params[0].get_str();
466-
}
459+
const std::string wallet_name{EnsureUniqueWalletName(request, self.MaybeArg<std::string>("wallet_name"))};
467460

468461
WalletContext& context = EnsureWalletContext(request.context);
469462
std::shared_ptr<CWallet> wallet = GetWallet(context, wallet_name);
@@ -685,17 +678,7 @@ static RPCHelpMan migratewallet()
685678
},
686679
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
687680
{
688-
std::string wallet_name;
689-
if (GetWalletNameFromJSONRPCRequest(request, wallet_name)) {
690-
if (!(request.params[0].isNull() || request.params[0].get_str() == wallet_name)) {
691-
throw JSONRPCError(RPC_INVALID_PARAMETER, "RPC endpoint wallet and wallet_name parameter specify different wallets");
692-
}
693-
} else {
694-
if (request.params[0].isNull()) {
695-
throw JSONRPCError(RPC_INVALID_PARAMETER, "Either RPC endpoint wallet or wallet_name parameter must be provided");
696-
}
697-
wallet_name = request.params[0].get_str();
698-
}
681+
const std::string wallet_name{EnsureUniqueWalletName(request, self.MaybeArg<std::string>("wallet_name"))};
699682

700683
SecureString wallet_pass;
701684
wallet_pass.reserve(100);

test/functional/wallet_migration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,8 +518,8 @@ def test_encrypted(self):
518518
def test_nonexistent(self):
519519
self.log.info("Check migratewallet errors for nonexistent wallets")
520520
default = self.master_node.get_wallet_rpc(self.default_wallet_name)
521-
assert_raises_rpc_error(-8, "RPC endpoint wallet and wallet_name parameter specify different wallets", default.migratewallet, "someotherwallet")
522-
assert_raises_rpc_error(-8, "Either RPC endpoint wallet or wallet_name parameter must be provided", self.master_node.migratewallet)
521+
assert_raises_rpc_error(-8, "The RPC endpoint wallet and the wallet name parameter specify different wallets", default.migratewallet, "someotherwallet")
522+
assert_raises_rpc_error(-8, "Either the RPC endpoint wallet or the wallet name parameter must be provided", self.master_node.migratewallet)
523523
assert_raises_rpc_error(-4, "Error: Wallet does not exist", self.master_node.migratewallet, "notawallet")
524524

525525
def test_unloaded_by_path(self):

test/functional/wallet_multiwallet.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,10 +343,10 @@ def wallet_file(name):
343343
self.log.info("Test dynamic wallet unloading")
344344

345345
# Test `unloadwallet` errors
346-
assert_raises_rpc_error(-3, "JSON value of type null is not of expected type string", self.nodes[0].unloadwallet)
346+
assert_raises_rpc_error(-8, "Either the RPC endpoint wallet or the wallet name parameter must be provided", self.nodes[0].unloadwallet)
347347
assert_raises_rpc_error(-18, "Requested wallet does not exist or is not loaded", self.nodes[0].unloadwallet, "dummy")
348348
assert_raises_rpc_error(-18, "Requested wallet does not exist or is not loaded", node.get_wallet_rpc("dummy").unloadwallet)
349-
assert_raises_rpc_error(-8, "RPC endpoint wallet and wallet_name parameter specify different wallets", w1.unloadwallet, "w2"),
349+
assert_raises_rpc_error(-8, "The RPC endpoint wallet and the wallet name parameter specify different wallets", w1.unloadwallet, "w2"),
350350

351351
# Successfully unload the specified wallet name
352352
self.nodes[0].unloadwallet("w1")

0 commit comments

Comments
 (0)