From 331991b0d038b5611fbe6faca3aabf1633eee1a3 Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Wed, 27 Jan 2021 13:42:14 +0100 Subject: [PATCH] Merge #20964: rpc: Add specific error code for "wallet already loaded" a6739cc86827759c543bf81f5532ec46e40549c3 rpc: Add specific error code for "wallet already loaded" (Wladimir J. van der Laan) Pull request description: Add a separate RPC error code for "wallet already loaded" to avoid having to match on message to detect this. Requested by shesek for rust-bitcoinrpc. If concept ACKed needs: - [ ] Release note - [x] A functional test (updated the existing test to make it pass, I think this is enough) ACKs for top commit: jonasschnelli: Code Review ACK a6739cc86827759c543bf81f5532ec46e40549c3 promag: Code review ACK a6739cc86827759c543bf81f5532ec46e40549c3. Tree-SHA512: 9091872e6ea148aec733705d6af330f72a02f23b936b892ac28f9023da7430af6332418048adbee6014305b812316391812039e9180f7f3362d11f206c13b7d0 --- src/rpc/protocol.h | 1 + src/wallet/rpcwallet.cpp | 13 ++++++++++++- test/functional/wallet_multiwallet.py | 4 ++-- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/rpc/protocol.h b/src/rpc/protocol.h index 359c0d1c82..aa2a1c6af3 100644 --- a/src/rpc/protocol.h +++ b/src/rpc/protocol.h @@ -79,6 +79,7 @@ enum RPCErrorCode 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) + RPC_WALLET_ALREADY_LOADED = -35, //!< This same wallet is already loaded //! Backwards compatible aliases diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index a19490c298..c71009b4d9 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -2807,7 +2807,18 @@ static UniValue loadwallet(const JSONRPCRequest& request) if (!wallet) { // Map bad format to not found, since bad format is returned when the // wallet directory exists, but doesn't contain a data file. - RPCErrorCode code = status == DatabaseStatus::FAILED_NOT_FOUND || status == DatabaseStatus::FAILED_BAD_FORMAT ? RPC_WALLET_NOT_FOUND : RPC_WALLET_ERROR; + RPCErrorCode code = RPC_WALLET_ERROR; + switch (status) { + case DatabaseStatus::FAILED_NOT_FOUND: + case DatabaseStatus::FAILED_BAD_FORMAT: + code = RPC_WALLET_NOT_FOUND; + break; + case DatabaseStatus::FAILED_ALREADY_LOADED: + code = RPC_WALLET_ALREADY_LOADED; + break; + default: // RPC_WALLET_ERROR is returned for all other cases. + break; + } throw JSONRPCError(code, error.original); } diff --git a/test/functional/wallet_multiwallet.py b/test/functional/wallet_multiwallet.py index cd66169b91..75e471bc5f 100755 --- a/test/functional/wallet_multiwallet.py +++ b/test/functional/wallet_multiwallet.py @@ -284,12 +284,12 @@ class MultiWalletTest(BitcoinTestFramework): if self.options.is_sqlite_only: assert_raises_rpc_error(-4, "Wallet file verification failed. SQLiteDatabase: Unable to obtain an exclusive lock on the database, is it being used by another dashd?", self.nodes[0].loadwallet, wallet_names[0]) else: - assert_raises_rpc_error(-4, "Wallet file verification failed. Refusing to load database. Data file '{}' is already loaded.".format(path), self.nodes[0].loadwallet, wallet_names[0]) + assert_raises_rpc_error(-35, "Wallet file verification failed. Refusing to load database. Data file '{}' is already loaded.".format(path), self.nodes[0].loadwallet, wallet_names[0]) # This tests the default wallet that BDB makes, so SQLite wallet doesn't need to test this # Fail to load duplicate wallets by different ways (directory and filepath) path = os.path.join(self.options.tmpdir, "node0", self.chain, "wallets", self.wallet_data_filename) - assert_raises_rpc_error(-4, "Wallet file verification failed. Refusing to load database. Data file '{}' is already loaded.".format(path), self.nodes[0].loadwallet, self.wallet_data_filename) + assert_raises_rpc_error(-35, "Wallet file verification failed. Refusing to load database. Data file '{}' is already loaded.".format(path), self.nodes[0].loadwallet, self.wallet_data_filename) # Only BDB doesn't open duplicate wallet files. SQLite does not have this limitation. While this may be desired in the future, it is not necessary # Fail to load if one wallet is a copy of another