Skip to content

Commit 15a9df0

Browse files
committed
Merge #20964: rpc: Add specific error code for "wallet already loaded"
a6739cc 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 a6739cc promag: Code review ACK a6739cc. Tree-SHA512: 9091872e6ea148aec733705d6af330f72a02f23b936b892ac28f9023da7430af6332418048adbee6014305b812316391812039e9180f7f3362d11f206c13b7d0
2 parents 11d3b58 + a6739cc commit 15a9df0

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

src/rpc/protocol.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ enum RPCErrorCode
7979
RPC_WALLET_ALREADY_UNLOCKED = -17, //!< Wallet is already unlocked
8080
RPC_WALLET_NOT_FOUND = -18, //!< Invalid wallet specified
8181
RPC_WALLET_NOT_SPECIFIED = -19, //!< No wallet specified (error when there are multiple wallets loaded)
82+
RPC_WALLET_ALREADY_LOADED = -35, //!< This same wallet is already loaded
8283

8384
//! Backwards compatible aliases
8485
RPC_WALLET_INVALID_ACCOUNT_NAME = RPC_WALLET_INVALID_LABEL_NAME,

src/wallet/rpcwallet.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2615,7 +2615,18 @@ static RPCHelpMan loadwallet()
26152615
if (!wallet) {
26162616
// Map bad format to not found, since bad format is returned when the
26172617
// wallet directory exists, but doesn't contain a data file.
2618-
RPCErrorCode code = status == DatabaseStatus::FAILED_NOT_FOUND || status == DatabaseStatus::FAILED_BAD_FORMAT ? RPC_WALLET_NOT_FOUND : RPC_WALLET_ERROR;
2618+
RPCErrorCode code = RPC_WALLET_ERROR;
2619+
switch (status) {
2620+
case DatabaseStatus::FAILED_NOT_FOUND:
2621+
case DatabaseStatus::FAILED_BAD_FORMAT:
2622+
code = RPC_WALLET_NOT_FOUND;
2623+
break;
2624+
case DatabaseStatus::FAILED_ALREADY_LOADED:
2625+
code = RPC_WALLET_ALREADY_LOADED;
2626+
break;
2627+
default: // RPC_WALLET_ERROR is returned for all other cases.
2628+
break;
2629+
}
26192630
throw JSONRPCError(code, error.original);
26202631
}
26212632

test/functional/wallet_multiwallet.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,12 @@ def wallet_file(name):
304304
if self.options.descriptors:
305305
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 bitcoind?", self.nodes[0].loadwallet, wallet_names[0])
306306
else:
307-
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])
307+
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])
308308

309309
# This tests the default wallet that BDB makes, so SQLite wallet doesn't need to test this
310310
# Fail to load duplicate wallets by different ways (directory and filepath)
311311
path = os.path.join(self.options.tmpdir, "node0", "regtest", "wallets", "wallet.dat")
312-
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.dat')
312+
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.dat')
313313

314314
# 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
315315
# Fail to load if one wallet is a copy of another

0 commit comments

Comments
 (0)