Skip to content

Commit 00f0041

Browse files
committed
No need to check for duplicate fileids in all dbenvs
Since we have .walletlock in each directory, we don't need the duplicate fileid checks across all dbenvs as it shouldn't be possible anyways.
1 parent d86efab commit 00f0041

File tree

2 files changed

+9
-20
lines changed

2 files changed

+9
-20
lines changed

src/wallet/bdb.cpp

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ void CheckUniqueFileid(const BerkeleyEnvironment& env, const std::string& filena
3232

3333
int ret = db.get_mpf()->get_fileid(fileid.value);
3434
if (ret != 0) {
35-
throw std::runtime_error(strprintf("BerkeleyBatch: Can't open database %s (get_fileid failed with %d)", filename, ret));
35+
throw std::runtime_error(strprintf("BerkeleyDatabase: Can't open database %s (get_fileid failed with %d)", filename, ret));
3636
}
3737

3838
for (const auto& item : env.m_fileids) {
3939
if (fileid == item.second && &fileid != &item.second) {
40-
throw std::runtime_error(strprintf("BerkeleyBatch: Can't open database %s (duplicates fileid %s from %s)", filename,
40+
throw std::runtime_error(strprintf("BerkeleyDatabase: Can't open database %s (duplicates fileid %s from %s)", filename,
4141
HexStr(std::begin(item.second.value), std::end(item.second.value)), item.first));
4242
}
4343
}
@@ -309,6 +309,8 @@ BerkeleyDatabase::~BerkeleyDatabase()
309309
{
310310
if (env) {
311311
LOCK(cs_db);
312+
env->CloseDb(strFile);
313+
assert(!m_db);
312314
size_t erased = env->m_databases.erase(strFile);
313315
assert(erased == 1);
314316
env->m_fileids.erase(strFile);
@@ -373,25 +375,12 @@ void BerkeleyDatabase::Open(const char* pszMode)
373375
if (ret != 0) {
374376
throw std::runtime_error(strprintf("BerkeleyDatabase: Error %d, can't open database %s", ret, strFile));
375377
}
378+
m_file_path = (env->Directory() / strFile).string();
376379

377380
// Call CheckUniqueFileid on the containing BDB environment to
378381
// avoid BDB data consistency bugs that happen when different data
379382
// files in the same environment have the same fileid.
380-
//
381-
// Also call CheckUniqueFileid on all the other g_dbenvs to prevent
382-
// bitcoin from opening the same data file through another
383-
// environment when the file is referenced through equivalent but
384-
// not obviously identical symlinked or hard linked or bind mounted
385-
// paths. In the future a more relaxed check for equal inode and
386-
// device ids could be done instead, which would allow opening
387-
// different backup copies of a wallet at the same time. Maybe even
388-
// more ideally, an exclusive lock for accessing the database could
389-
// be implemented, so no equality checks are needed at all. (Newer
390-
// versions of BDB have an set_lk_exclusive method for this
391-
// purpose, but the older version we use does not.)
392-
for (const auto& env : g_dbenvs) {
393-
CheckUniqueFileid(*env.second.lock().get(), strFile, *pdb_temp, this->env->m_fileids[strFile]);
394-
}
383+
CheckUniqueFileid(*env, strFile, *pdb_temp, this->env->m_fileids[strFile]);
395384

396385
m_db.reset(pdb_temp.release());
397386

test/functional/wallet_multiwallet.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def wallet_file(name):
120120

121121
# should not initialize if one wallet is a copy of another
122122
shutil.copyfile(wallet_dir('w8'), wallet_dir('w8_copy'))
123-
exp_stderr = r"BerkeleyBatch: Can't open database w8_copy \(duplicates fileid \w+ from w8\)"
123+
exp_stderr = r"BerkeleyDatabase: Can't open database w8_copy \(duplicates fileid \w+ from w8\)"
124124
self.nodes[0].assert_start_raises_init_error(['-wallet=w8', '-wallet=w8_copy'], exp_stderr, match=ErrorMatch.PARTIAL_REGEX)
125125

126126
# should not initialize if wallet file is a symlink
@@ -258,10 +258,10 @@ def wallet_file(name):
258258
assert_raises_rpc_error(-4, "Wallet file verification failed. Error loading wallet wallet.dat. Duplicate -wallet filename specified.", self.nodes[0].loadwallet, 'wallet.dat')
259259

260260
# Fail to load if one wallet is a copy of another
261-
assert_raises_rpc_error(-4, "BerkeleyBatch: Can't open database w8_copy (duplicates fileid", self.nodes[0].loadwallet, 'w8_copy')
261+
assert_raises_rpc_error(-4, "BerkeleyDatabase: Can't open database w8_copy (duplicates fileid", self.nodes[0].loadwallet, 'w8_copy')
262262

263263
# Fail to load if one wallet is a copy of another, test this twice to make sure that we don't re-introduce #14304
264-
assert_raises_rpc_error(-4, "BerkeleyBatch: Can't open database w8_copy (duplicates fileid", self.nodes[0].loadwallet, 'w8_copy')
264+
assert_raises_rpc_error(-4, "BerkeleyDatabase: Can't open database w8_copy (duplicates fileid", self.nodes[0].loadwallet, 'w8_copy')
265265

266266

267267
# Fail to load if wallet file is a symlink

0 commit comments

Comments
 (0)