Skip to content

Commit 8f1bcf8

Browse files
committed
walletdb: Combine VerifyDatabaseFile and VerifyEnvironment
Combine these two functions into a single Verify function that is a member of WalletDatabase. Additionally, these are no longer static.
1 parent dbd7a91 commit 8f1bcf8

File tree

7 files changed

+16
-42
lines changed

7 files changed

+16
-42
lines changed

src/wallet/bdb.cpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -292,11 +292,10 @@ BerkeleyBatch::SafeDbt::operator Dbt*()
292292
return &m_dbt;
293293
}
294294

295-
bool BerkeleyBatch::VerifyEnvironment(const fs::path& file_path, bilingual_str& errorStr)
295+
bool BerkeleyDatabase::Verify(bilingual_str& errorStr)
296296
{
297-
std::string walletFile;
298-
std::shared_ptr<BerkeleyEnvironment> env = GetWalletEnv(file_path, walletFile);
299297
fs::path walletDir = env->Directory();
298+
fs::path file_path = walletDir / strFile;
300299

301300
LogPrintf("Using BerkeleyDB version %s\n", BerkeleyDatabaseVersion());
302301
LogPrintf("Using wallet %s\n", file_path.string());
@@ -306,19 +305,10 @@ bool BerkeleyBatch::VerifyEnvironment(const fs::path& file_path, bilingual_str&
306305
return false;
307306
}
308307

309-
return true;
310-
}
311-
312-
bool BerkeleyBatch::VerifyDatabaseFile(const fs::path& file_path, bilingual_str& errorStr)
313-
{
314-
std::string walletFile;
315-
std::shared_ptr<BerkeleyEnvironment> env = GetWalletEnv(file_path, walletFile);
316-
fs::path walletDir = env->Directory();
317-
318-
if (fs::exists(walletDir / walletFile))
308+
if (fs::exists(file_path))
319309
{
320-
if (!env->Verify(walletFile)) {
321-
errorStr = strprintf(_("%s corrupt. Try using the wallet tool bitcoin-wallet to salvage or restoring a backup."), walletFile);
310+
if (!env->Verify(strFile)) {
311+
errorStr = strprintf(_("%s corrupt. Try using the wallet tool bitcoin-wallet to salvage or restoring a backup."), file_path);
322312
return false;
323313
}
324314
}

src/wallet/bdb.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ class BerkeleyDatabase
141141
unsigned int nLastFlushed;
142142
int64_t nLastWalletUpdate;
143143

144+
/** Verifies the environment and database file */
145+
bool Verify(bilingual_str& error);
146+
144147
/**
145148
* Pointer to shared database environment.
146149
*
@@ -215,10 +218,6 @@ class BerkeleyBatch
215218
/* flush the wallet passively (TRY_LOCK)
216219
ideal to be called periodically */
217220
static bool PeriodicFlush(BerkeleyDatabase& database);
218-
/* verifies the database environment */
219-
static bool VerifyEnvironment(const fs::path& file_path, bilingual_str& errorStr);
220-
/* verifies the database file */
221-
static bool VerifyDatabaseFile(const fs::path& file_path, bilingual_str& errorStr);
222221

223222
template <typename K, typename T>
224223
bool Read(const K& key, T& value)

src/wallet/salvage.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ bool RecoverDatabaseFile(const fs::path& file_path)
2020
std::string filename;
2121
std::shared_ptr<BerkeleyEnvironment> env = GetWalletEnv(file_path, filename);
2222

23+
if (!env->Open(true /* retry */)) {
24+
tfm::format(std::cerr, "Error initializing wallet database environment %s!", env->Directory());
25+
return false;
26+
}
27+
2328
// Recovery procedure:
2429
// move wallet file to walletfilename.timestamp.bak
2530
// Call Salvage with fAggressive=true to

src/wallet/wallet.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3708,15 +3708,11 @@ bool CWallet::Verify(interfaces::Chain& chain, const WalletLocation& location, b
37083708
std::unique_ptr<WalletDatabase> database = CreateWalletDatabase(wallet_path);
37093709

37103710
try {
3711-
if (!WalletBatch::VerifyEnvironment(wallet_path, error_string)) {
3712-
return false;
3713-
}
3711+
return database->Verify(error_string);
37143712
} catch (const fs::filesystem_error& e) {
37153713
error_string = Untranslated(strprintf("Error loading wallet %s. %s", location.GetName(), fsbridge::get_filesystem_error_message(e)));
37163714
return false;
37173715
}
3718-
3719-
return WalletBatch::VerifyDatabaseFile(wallet_path, error_string);
37203716
}
37213717

37223718
std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain, const WalletLocation& location, bilingual_str& error, std::vector<bilingual_str>& warnings, uint64_t wallet_creation_flags)

src/wallet/walletdb.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -973,16 +973,6 @@ void MaybeCompactWalletDB()
973973
fOneThread = false;
974974
}
975975

976-
bool WalletBatch::VerifyEnvironment(const fs::path& wallet_path, bilingual_str& errorStr)
977-
{
978-
return BerkeleyBatch::VerifyEnvironment(wallet_path, errorStr);
979-
}
980-
981-
bool WalletBatch::VerifyDatabaseFile(const fs::path& wallet_path, bilingual_str& errorStr)
982-
{
983-
return BerkeleyBatch::VerifyDatabaseFile(wallet_path, errorStr);
984-
}
985-
986976
bool WalletBatch::WriteDestData(const std::string &address, const std::string &key, const std::string &value)
987977
{
988978
return WriteIC(std::make_pair(DBKeys::DESTDATA, std::make_pair(address, key)), value);

src/wallet/wallettool.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ static bool SalvageWallet(const fs::path& path)
112112
// Initialize the environment before recovery
113113
bilingual_str error_string;
114114
try {
115-
WalletBatch::VerifyEnvironment(path, error_string);
115+
database->Verify(error_string);
116116
} catch (const fs::filesystem_error& e) {
117117
error_string = Untranslated(strprintf("Error loading wallet. %s", fsbridge::get_filesystem_error_message(e)));
118118
}
@@ -140,11 +140,6 @@ bool ExecuteWalletToolFunc(const std::string& command, const std::string& name)
140140
tfm::format(std::cerr, "Error: no wallet file at %s\n", name);
141141
return false;
142142
}
143-
bilingual_str error;
144-
if (!WalletBatch::VerifyEnvironment(path, error)) {
145-
tfm::format(std::cerr, "%s\nError loading %s. Is wallet being used by other process?\n", error.original, name);
146-
return false;
147-
}
148143

149144
if (command == "info") {
150145
std::shared_ptr<CWallet> wallet_instance = LoadWallet(name, path);

test/functional/tool_wallet.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ def test_invalid_tool_commands_and_args(self):
7171
self.assert_raises_tool_error('Error: two methods provided (info and create). Only one method should be provided.', 'info', 'create')
7272
self.assert_raises_tool_error('Error parsing command line arguments: Invalid parameter -foo', '-foo')
7373
self.assert_raises_tool_error(
74-
'Error initializing wallet database environment "{}"!\nError loading wallet.dat. Is wallet being used by other process?'
75-
.format(os.path.join(self.nodes[0].datadir, self.chain, 'wallets')),
74+
'Error loading wallet.dat. Is wallet being used by another process?',
7675
'-wallet=wallet.dat',
7776
'info',
7877
)

0 commit comments

Comments
 (0)