Skip to content

Commit 15c93f0

Browse files
committed
wallet: Add trailing wallet.dat when detecting duplicate wallet if it's a directory.
1 parent c456fbd commit 15c93f0

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

src/wallet/db.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,8 @@ bool WalletDatabaseFileId::operator==(const WalletDatabaseFileId& rhs) const
5656
return memcmp(value, &rhs.value, sizeof(value)) == 0;
5757
}
5858

59-
BerkeleyEnvironment* GetWalletEnv(const fs::path& wallet_path, std::string& database_filename)
59+
static void SplitWalletPath(const fs::path& wallet_path, fs::path& env_directory, std::string& database_filename)
6060
{
61-
fs::path env_directory;
6261
if (fs::is_regular_file(wallet_path)) {
6362
// Special case for backwards compatibility: if wallet path points to an
6463
// existing file, treat it as the path to a BDB data file in a parent
@@ -71,6 +70,24 @@ BerkeleyEnvironment* GetWalletEnv(const fs::path& wallet_path, std::string& data
7170
env_directory = wallet_path;
7271
database_filename = "wallet.dat";
7372
}
73+
}
74+
75+
bool IsWalletLoaded(const fs::path& wallet_path)
76+
{
77+
fs::path env_directory;
78+
std::string database_filename;
79+
SplitWalletPath(wallet_path, env_directory, database_filename);
80+
LOCK(cs_db);
81+
auto env = g_dbenvs.find(env_directory.string());
82+
if (env == g_dbenvs.end()) return false;
83+
auto db = env->second.m_databases.find(database_filename);
84+
return db != env->second.m_databases.end();
85+
}
86+
87+
BerkeleyEnvironment* GetWalletEnv(const fs::path& wallet_path, std::string& database_filename)
88+
{
89+
fs::path env_directory;
90+
SplitWalletPath(wallet_path, env_directory, database_filename);
7491
LOCK(cs_db);
7592
// Note: An unused temporary BerkeleyEnvironment object may be created inside the
7693
// emplace function if the key already exists. This is a little inefficient,

src/wallet/db.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ class BerkeleyEnvironment
9797
}
9898
};
9999

100+
/** Return whether a wallet database is currently loaded. */
101+
bool IsWalletLoaded(const fs::path& wallet_path);
102+
100103
/** Get BerkeleyEnvironment and database filename given a wallet path. */
101104
BerkeleyEnvironment* GetWalletEnv(const fs::path& wallet_path, std::string& database_filename);
102105

src/wallet/wallet.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3848,11 +3848,9 @@ bool CWallet::Verify(const WalletLocation& location, bool salvage_wallet, std::s
38483848
}
38493849

38503850
// Make sure that the wallet path doesn't clash with an existing wallet path
3851-
for (auto wallet : GetWallets()) {
3852-
if (wallet->GetLocation().GetPath() == wallet_path) {
3853-
error_string = strprintf("Error loading wallet %s. Duplicate -wallet filename specified.", location.GetName());
3854-
return false;
3855-
}
3851+
if (IsWalletLoaded(wallet_path)) {
3852+
error_string = strprintf("Error loading wallet %s. Duplicate -wallet filename specified.", location.GetName());
3853+
return false;
38563854
}
38573855

38583856
try {

test/functional/wallet_multiwallet.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ def wallet_file(name):
223223
# Fail to load duplicate wallets
224224
assert_raises_rpc_error(-4, 'Wallet file verification failed: Error loading wallet w1. Duplicate -wallet filename specified.', self.nodes[0].loadwallet, wallet_names[0])
225225

226+
# Fail to load duplicate wallets by different ways (directory and filepath)
227+
assert_raises_rpc_error(-4, "Wallet file verification failed: Error loading wallet wallet.dat. Duplicate -wallet filename specified.", self.nodes[0].loadwallet, 'wallet.dat')
228+
226229
# Fail to load if one wallet is a copy of another
227230
assert_raises_rpc_error(-1, "BerkeleyBatch: Can't open database w8_copy (duplicates fileid", self.nodes[0].loadwallet, 'w8_copy')
228231

0 commit comments

Comments
 (0)