Browse Source

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
0.15
Wladimir J. van der Laan 8 years ago
parent
commit
0b11a07848
No known key found for this signature in database
GPG Key ID: 1E4AED62986CD25D
  1. 4
      src/bitcoin-cli.cpp
  2. 2
      src/rpc/protocol.h
  3. 22
      src/wallet/rpcwallet.cpp
  4. 5
      test/functional/multiwallet.py

4
src/bitcoin-cli.cpp

@ -337,6 +337,10 @@ int CommandLineRPC(int argc, char *argv[])
if (errMsg.isStr()) if (errMsg.isStr())
strPrint += "error message:\n"+errMsg.get_str(); strPrint += "error message:\n"+errMsg.get_str();
if (errCode.isNum() && errCode.get_int() == RPC_WALLET_NOT_SPECIFIED) {
strPrint += "\nTry adding \"-rpcwallet=<filename>\" option to bitcoin-cli command line.";
}
} }
} else { } else {
// Result // Result

2
src/rpc/protocol.h

@ -82,6 +82,8 @@ enum RPCErrorCode
RPC_WALLET_WRONG_ENC_STATE = -15, //!< Command given in wrong wallet encryption state (encrypting an encrypted wallet etc.) RPC_WALLET_WRONG_ENC_STATE = -15, //!< Command given in wrong wallet encryption state (encrypting an encrypted wallet etc.)
RPC_WALLET_ENCRYPTION_FAILED = -16, //!< Failed to encrypt the wallet RPC_WALLET_ENCRYPTION_FAILED = -16, //!< Failed to encrypt the wallet
RPC_WALLET_ALREADY_UNLOCKED = -17, //!< Wallet is already unlocked RPC_WALLET_ALREADY_UNLOCKED = -17, //!< Wallet is already unlocked
RPC_WALLET_NOT_FOUND = -18, //!< Invalid wallet specified
RPC_WALLET_NOT_SPECIFIED = -19, //!< No wallet specified (error when there are multiple wallets loaded)
}; };
UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id); UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id);

22
src/wallet/rpcwallet.cpp

@ -43,7 +43,7 @@ CWallet *GetWalletForJSONRPCRequest(const JSONRPCRequest& request)
return pwallet; return pwallet;
} }
} }
throw JSONRPCError(RPC_INVALID_PARAMETER, "Requested wallet does not exist or is not loaded"); throw JSONRPCError(RPC_WALLET_NOT_FOUND, "Requested wallet does not exist or is not loaded");
} }
return ::vpwallets.size() == 1 || (request.fHelp && ::vpwallets.size() > 0) ? ::vpwallets[0] : nullptr; return ::vpwallets.size() == 1 || (request.fHelp && ::vpwallets.size() > 0) ? ::vpwallets[0] : nullptr;
} }
@ -57,13 +57,19 @@ std::string HelpRequiringPassphrase(CWallet * const pwallet)
bool EnsureWalletIsAvailable(CWallet * const pwallet, bool avoidException) bool EnsureWalletIsAvailable(CWallet * const pwallet, bool avoidException)
{ {
if (!pwallet) { if (pwallet) return true;
if (!avoidException) if (avoidException) return false;
throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Method not found (disabled)"); if (::vpwallets.empty()) {
else // Note: It isn't currently possible to trigger this error because
return false; // wallet RPC methods aren't registered unless a wallet is loaded. But
} // this error is being kept as a precaution, because it's possible in
return true; // the future that wallet RPC methods might get or remain registered
// when no wallets are loaded.
throw JSONRPCError(
RPC_METHOD_NOT_FOUND, "Method not found (wallet method is disabled because no wallet is loaded)");
}
throw JSONRPCError(RPC_WALLET_NOT_SPECIFIED,
"Wallet file not specified (must request wallet RPC through /wallet/<filename> uri-path).");
} }
void EnsureWalletIsUnlocked(CWallet * const pwallet) void EnsureWalletIsUnlocked(CWallet * const pwallet)

5
test/functional/multiwallet.py

@ -21,8 +21,11 @@ class MultiWalletTest(BitcoinTestFramework):
w1 = self.nodes[0] / "wallet/w1" w1 = self.nodes[0] / "wallet/w1"
w1.generate(1) w1.generate(1)
# accessing invalid wallet fails
assert_raises_jsonrpc(-18, "Requested wallet does not exist or is not loaded", (self.nodes[0] / "wallet/bad").getwalletinfo)
# accessing wallet RPC without using wallet endpoint fails # accessing wallet RPC without using wallet endpoint fails
assert_raises_jsonrpc(-32601, "Method not found", self.nodes[0].getwalletinfo) assert_raises_jsonrpc(-19, "Wallet file not specified", self.nodes[0].getwalletinfo)
# check w1 wallet balance # check w1 wallet balance
w1_info = w1.getwalletinfo() w1_info = w1.getwalletinfo()

Loading…
Cancel
Save