Skip to content

Commit 0b11a07

Browse files
committed
Merge #10931: Fix misleading "Method not found" multiwallet errors
df389bc Change wallet method disabled error text (Russell Yanofsky) e526b3d Fix misleading "Method not found" multiwallet errors (Russell Yanofsky) Pull request description: Raise RPC_WALLET_NOT_SPECIFIED instead of RPC_METHOD_NOT_FOUND when a required wallet filename was not specified in an RPC call. Also raise more specific RPC_WALLET_NOT_FOUND error instead of RPC_INVALID_PARAMETER in case an invalid wallet was specified, for consistency. Tree-SHA512: 6a8d885283f69bcfc28f2e08ac03eff02f9f8160a312ce2a90d868aa52533434fc0b4c4ab86547c2f09392338956df915637eaf7136a4fc105e6c8179f2d0ac8
2 parents ba1bbb0 + df389bc commit 0b11a07

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

src/bitcoin-cli.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,10 @@ int CommandLineRPC(int argc, char *argv[])
337337

338338
if (errMsg.isStr())
339339
strPrint += "error message:\n"+errMsg.get_str();
340+
341+
if (errCode.isNum() && errCode.get_int() == RPC_WALLET_NOT_SPECIFIED) {
342+
strPrint += "\nTry adding \"-rpcwallet=<filename>\" option to bitcoin-cli command line.";
343+
}
340344
}
341345
} else {
342346
// Result

src/rpc/protocol.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ enum RPCErrorCode
8282
RPC_WALLET_WRONG_ENC_STATE = -15, //!< Command given in wrong wallet encryption state (encrypting an encrypted wallet etc.)
8383
RPC_WALLET_ENCRYPTION_FAILED = -16, //!< Failed to encrypt the wallet
8484
RPC_WALLET_ALREADY_UNLOCKED = -17, //!< Wallet is already unlocked
85+
RPC_WALLET_NOT_FOUND = -18, //!< Invalid wallet specified
86+
RPC_WALLET_NOT_SPECIFIED = -19, //!< No wallet specified (error when there are multiple wallets loaded)
8587
};
8688

8789
UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id);

src/wallet/rpcwallet.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ CWallet *GetWalletForJSONRPCRequest(const JSONRPCRequest& request)
4343
return pwallet;
4444
}
4545
}
46-
throw JSONRPCError(RPC_INVALID_PARAMETER, "Requested wallet does not exist or is not loaded");
46+
throw JSONRPCError(RPC_WALLET_NOT_FOUND, "Requested wallet does not exist or is not loaded");
4747
}
4848
return ::vpwallets.size() == 1 || (request.fHelp && ::vpwallets.size() > 0) ? ::vpwallets[0] : nullptr;
4949
}
@@ -57,13 +57,19 @@ std::string HelpRequiringPassphrase(CWallet * const pwallet)
5757

5858
bool EnsureWalletIsAvailable(CWallet * const pwallet, bool avoidException)
5959
{
60-
if (!pwallet) {
61-
if (!avoidException)
62-
throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Method not found (disabled)");
63-
else
64-
return false;
65-
}
66-
return true;
60+
if (pwallet) return true;
61+
if (avoidException) return false;
62+
if (::vpwallets.empty()) {
63+
// Note: It isn't currently possible to trigger this error because
64+
// wallet RPC methods aren't registered unless a wallet is loaded. But
65+
// this error is being kept as a precaution, because it's possible in
66+
// the future that wallet RPC methods might get or remain registered
67+
// when no wallets are loaded.
68+
throw JSONRPCError(
69+
RPC_METHOD_NOT_FOUND, "Method not found (wallet method is disabled because no wallet is loaded)");
70+
}
71+
throw JSONRPCError(RPC_WALLET_NOT_SPECIFIED,
72+
"Wallet file not specified (must request wallet RPC through /wallet/<filename> uri-path).");
6773
}
6874

6975
void EnsureWalletIsUnlocked(CWallet * const pwallet)

test/functional/multiwallet.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ def run_test(self):
2121
w1 = self.nodes[0] / "wallet/w1"
2222
w1.generate(1)
2323

24+
# accessing invalid wallet fails
25+
assert_raises_jsonrpc(-18, "Requested wallet does not exist or is not loaded", (self.nodes[0] / "wallet/bad").getwalletinfo)
26+
2427
# accessing wallet RPC without using wallet endpoint fails
25-
assert_raises_jsonrpc(-32601, "Method not found", self.nodes[0].getwalletinfo)
28+
assert_raises_jsonrpc(-19, "Wallet file not specified", self.nodes[0].getwalletinfo)
2629

2730
# check w1 wallet balance
2831
w1_info = w1.getwalletinfo()

0 commit comments

Comments
 (0)