Skip to content

Commit c7007ba

Browse files
author
MarcoFalke
committed
Merge #18907: walletdb: Don't remove database transaction logs and instead error
d0ea9ba walletdb: Don't remove database transaction logs and instead error (Andrew Chow) Pull request description: Instead of removing the database transaction logs and retrying the wallet loading, just return an error message to the user. Additionally, speciically for DB_RUNRECOVERY, notify the user that this could be due to different BDB versions. Kind of implements the suggestion from bitcoin/bitcoin#18870 (comment) ACKs for top commit: Sjors: re-utACK d0ea9ba ryanofsky: Code review ACK d0ea9ba. Only changes since last review are rebase and expanding error and commit messages. Tree-SHA512: f6e67dc70f58188742a5c8af7cdc63a2b58779aa0d26ae7f1e75805a239f1a342433860e5a238d6577fae5ab04b9d15e7f11c55b867065dfd13781a6a62e4958
2 parents 2c0c3f8 + d0ea9ba commit c7007ba

File tree

3 files changed

+16
-24
lines changed

3 files changed

+16
-24
lines changed

src/wallet/bdb.cpp

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ BerkeleyEnvironment::~BerkeleyEnvironment()
139139
Close();
140140
}
141141

142-
bool BerkeleyEnvironment::Open(bool retry)
142+
bool BerkeleyEnvironment::Open(bilingual_str& err)
143143
{
144144
if (fDbEnvInit) {
145145
return true;
@@ -149,6 +149,7 @@ bool BerkeleyEnvironment::Open(bool retry)
149149
TryCreateDirectories(pathIn);
150150
if (!LockDirectory(pathIn, ".walletlock")) {
151151
LogPrintf("Cannot obtain a lock on wallet directory %s. Another instance of bitcoin may be using it.\n", strPath);
152+
err = strprintf(_("Error initializing wallet database environment %s!"), Directory());
152153
return false;
153154
}
154155

@@ -188,23 +189,11 @@ bool BerkeleyEnvironment::Open(bool retry)
188189
LogPrintf("BerkeleyEnvironment::Open: Error %d closing failed database environment: %s\n", ret2, DbEnv::strerror(ret2));
189190
}
190191
Reset();
191-
if (retry) {
192-
// try moving the database env out of the way
193-
fs::path pathDatabaseBak = pathIn / strprintf("database.%d.bak", GetTime());
194-
try {
195-
fs::rename(pathLogDir, pathDatabaseBak);
196-
LogPrintf("Moved old %s to %s. Retrying.\n", pathLogDir.string(), pathDatabaseBak.string());
197-
} catch (const fs::filesystem_error&) {
198-
// failure is ok (well, not really, but it's not worse than what we started with)
199-
}
200-
// try opening it again one more time
201-
if (!Open(false /* retry */)) {
202-
// if it still fails, it probably means we can't even create the database env
203-
return false;
204-
}
205-
} else {
206-
return false;
192+
err = strprintf(_("Error initializing wallet database environment %s!"), Directory());
193+
if (ret == DB_RUNRECOVERY) {
194+
err += Untranslated(" ") + _("This error could occur if this wallet was not shutdown cleanly and was last loaded using a build with a newer version of Berkeley DB. If so, please use the software that last loaded this wallet");
207195
}
196+
return false;
208197
}
209198

210199
fDbEnvInit = true;
@@ -300,8 +289,7 @@ bool BerkeleyDatabase::Verify(bilingual_str& errorStr)
300289
LogPrintf("Using BerkeleyDB version %s\n", BerkeleyDatabaseVersion());
301290
LogPrintf("Using wallet %s\n", file_path.string());
302291

303-
if (!env->Open(true /* retry */)) {
304-
errorStr = strprintf(_("Error initializing wallet database environment %s!"), walletDir);
292+
if (!env->Open(errorStr)) {
305293
return false;
306294
}
307295

@@ -342,7 +330,8 @@ BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode, bo
342330

343331
{
344332
LOCK(cs_db);
345-
if (!env->Open(false /* retry */))
333+
bilingual_str open_err;
334+
if (!env->Open(open_err))
346335
throw std::runtime_error("BerkeleyBatch: Failed to open database environment.");
347336

348337
pdb = database.m_db.get();
@@ -482,7 +471,8 @@ void BerkeleyEnvironment::ReloadDbEnv()
482471
// Reset the environment
483472
Flush(true); // This will flush and close the environment
484473
Reset();
485-
Open(true);
474+
bilingual_str open_err;
475+
Open(open_err);
486476
}
487477

488478
bool BerkeleyDatabase::Rewrite(const char* pszSkip)

src/wallet/bdb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class BerkeleyEnvironment
6969

7070
bool Verify(const std::string& strFile);
7171

72-
bool Open(bool retry);
72+
bool Open(bilingual_str& error);
7373
void Close();
7474
void Flush(bool fShutdown);
7575
void CheckpointLSN(const std::string& strFile);

src/wallet/salvage.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <fs.h>
77
#include <streams.h>
8+
#include <util/translation.h>
89
#include <wallet/salvage.h>
910
#include <wallet/wallet.h>
1011
#include <wallet/walletdb.h>
@@ -20,8 +21,9 @@ bool RecoverDatabaseFile(const fs::path& file_path)
2021
std::string filename;
2122
std::shared_ptr<BerkeleyEnvironment> env = GetWalletEnv(file_path, filename);
2223

23-
if (!env->Open(true /* retry */)) {
24-
tfm::format(std::cerr, "Error initializing wallet database environment %s!", env->Directory());
24+
bilingual_str open_err;
25+
if (!env->Open(open_err)) {
26+
tfm::format(std::cerr, "%s\n", open_err.original);
2527
return false;
2628
}
2729

0 commit comments

Comments
 (0)