Skip to content

Commit 417d95f

Browse files
laanwjPastaPastaPasta
authored andcommitted
Merge bitcoin#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
1 parent 0a0ba28 commit 417d95f

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

src/dash-cli.cpp

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

351351
if (errMsg.isStr())
352352
strPrint += "error message:\n"+errMsg.get_str();
353+
354+
if (errCode.isNum() && errCode.get_int() == RPC_WALLET_NOT_SPECIFIED) {
355+
strPrint += "\nTry adding \"-rpcwallet=<filename>\" option to bitcoin-cli command line.";
356+
}
353357
}
354358
} else {
355359
// 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
@@ -46,7 +46,7 @@ CWallet *GetWalletForJSONRPCRequest(const JSONRPCRequest& request)
4646
return pwallet;
4747
}
4848
}
49-
throw JSONRPCError(RPC_INVALID_PARAMETER, "Requested wallet does not exist or is not loaded");
49+
throw JSONRPCError(RPC_WALLET_NOT_FOUND, "Requested wallet does not exist or is not loaded");
5050
}
5151
return ::vpwallets.size() == 1 || (request.fHelp && ::vpwallets.size() > 0) ? ::vpwallets[0] : nullptr;
5252
}
@@ -60,13 +60,19 @@ std::string HelpRequiringPassphrase(CWallet * const pwallet)
6060

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

7278
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)