Skip to content

Commit 171f4a5

Browse files
author
MarcoFalke
committed
Merge #19324: wallet: Move BerkeleyBatch static functions to BerkeleyDatabase
d8e9ca6 walletdb: Move Rewrite into BerkeleyDatabase (Andrew Chow) 91d1091 walletdb: Move PeriodicFlush into WalletDatabase (Andrew Chow) 8f1bcf8 walletdb: Combine VerifyDatabaseFile and VerifyEnvironment (Andrew Chow) Pull request description: The `BerkeleyBatch` class has 4 static functions that operate on `BerkeleyDatabase` or `BerkeleyEnvironment`. It doesn't make sense for these to be standalone nor for them to be static functions. So instead, move them from `BerkeleyBatch` into `BerkeleyDatabase` and make them member functions instead of static. `BerkeleyBatch::VerifyEnvironment` and `BerkeleyBatch::VerifyDatabaseFile` are combined into a single `BerkeleyDatabase::Verify` function that operates on that `BerkeleyDatabase` object. `BerkeleyBatch::Rewrite` and `BerkeleyBatch::PeriodicFlush` both took a `BerkeleyDatabase` as an argument and did stuff on it. So we just make it a member function so it doesn't need to take a database as an argument. Part of #18971 ACKs for top commit: MarcoFalke: re-ACK d8e9ca6 only change is test fixup 🤞 promag: Code review ACK d8e9ca6, good stuff. Tree-SHA512: 9847e55b13d98bf4e5636cc14bc3f5351d56737f7e320fafffaed128606240765599e5400382c5aecac06690f7e36265ca3e1031f3f6d8a9688f6d5cb1bacd2a
2 parents 8783bcc + d8e9ca6 commit 171f4a5

File tree

7 files changed

+25
-63
lines changed

7 files changed

+25
-63
lines changed

src/wallet/bdb.cpp

Lines changed: 10 additions & 29 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
}
@@ -495,13 +485,11 @@ void BerkeleyEnvironment::ReloadDbEnv()
495485
Open(true);
496486
}
497487

498-
bool BerkeleyBatch::Rewrite(BerkeleyDatabase& database, const char* pszSkip)
488+
bool BerkeleyDatabase::Rewrite(const char* pszSkip)
499489
{
500-
if (database.IsDummy()) {
490+
if (IsDummy()) {
501491
return true;
502492
}
503-
BerkeleyEnvironment *env = database.env.get();
504-
const std::string& strFile = database.strFile;
505493
while (true) {
506494
{
507495
LOCK(cs_db);
@@ -515,7 +503,7 @@ bool BerkeleyBatch::Rewrite(BerkeleyDatabase& database, const char* pszSkip)
515503
LogPrintf("BerkeleyBatch::Rewrite: Rewriting %s...\n", strFile);
516504
std::string strFileRes = strFile + ".rewrite";
517505
{ // surround usage of db with extra {}
518-
BerkeleyBatch db(database, "r");
506+
BerkeleyBatch db(*this, "r");
519507
std::unique_ptr<Db> pdbCopy = MakeUnique<Db>(env->dbenv.get(), 0);
520508

521509
int ret = pdbCopy->open(nullptr, // Txn pointer
@@ -625,14 +613,12 @@ void BerkeleyEnvironment::Flush(bool fShutdown)
625613
}
626614
}
627615

628-
bool BerkeleyBatch::PeriodicFlush(BerkeleyDatabase& database)
616+
bool BerkeleyDatabase::PeriodicFlush()
629617
{
630-
if (database.IsDummy()) {
618+
if (IsDummy()) {
631619
return true;
632620
}
633621
bool ret = false;
634-
BerkeleyEnvironment *env = database.env.get();
635-
const std::string& strFile = database.strFile;
636622
TRY_LOCK(cs_db, lockDb);
637623
if (lockDb)
638624
{
@@ -667,11 +653,6 @@ bool BerkeleyBatch::PeriodicFlush(BerkeleyDatabase& database)
667653
return ret;
668654
}
669655

670-
bool BerkeleyDatabase::Rewrite(const char* pszSkip)
671-
{
672-
return BerkeleyBatch::Rewrite(*this, pszSkip);
673-
}
674-
675656
bool BerkeleyDatabase::Backup(const std::string& strDest) const
676657
{
677658
if (IsDummy()) {

src/wallet/bdb.h

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ class BerkeleyDatabase
131131
/** Make sure all changes are flushed to disk.
132132
*/
133133
void Flush(bool shutdown);
134+
/* flush the wallet passively (TRY_LOCK)
135+
ideal to be called periodically */
136+
bool PeriodicFlush();
134137

135138
void IncrementUpdateCounter();
136139

@@ -141,6 +144,9 @@ class BerkeleyDatabase
141144
unsigned int nLastFlushed;
142145
int64_t nLastWalletUpdate;
143146

147+
/** Verifies the environment and database file */
148+
bool Verify(bilingual_str& error);
149+
144150
/**
145151
* Pointer to shared database environment.
146152
*
@@ -213,14 +219,6 @@ class BerkeleyBatch
213219
void Flush();
214220
void Close();
215221

216-
/* flush the wallet passively (TRY_LOCK)
217-
ideal to be called periodically */
218-
static bool PeriodicFlush(BerkeleyDatabase& database);
219-
/* verifies the database environment */
220-
static bool VerifyEnvironment(const fs::path& file_path, bilingual_str& errorStr);
221-
/* verifies the database file */
222-
static bool VerifyDatabaseFile(const fs::path& file_path, bilingual_str& errorStr);
223-
224222
template <typename K, typename T>
225223
bool Read(const K& key, T& value)
226224
{
@@ -291,8 +289,6 @@ class BerkeleyBatch
291289
bool TxnBegin();
292290
bool TxnCommit();
293291
bool TxnAbort();
294-
295-
bool static Rewrite(BerkeleyDatabase& database, const char* pszSkip = nullptr);
296292
};
297293

298294
std::string BerkeleyDatabaseVersion();

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
@@ -3732,15 +3732,11 @@ bool CWallet::Verify(interfaces::Chain& chain, const WalletLocation& location, b
37323732
std::unique_ptr<WalletDatabase> database = CreateWalletDatabase(wallet_path);
37333733

37343734
try {
3735-
if (!WalletBatch::VerifyEnvironment(wallet_path, error_string)) {
3736-
return false;
3737-
}
3735+
return database->Verify(error_string);
37383736
} catch (const fs::filesystem_error& e) {
37393737
error_string = Untranslated(strprintf("Error loading wallet %s. %s", location.GetName(), fsbridge::get_filesystem_error_message(e)));
37403738
return false;
37413739
}
3742-
3743-
return WalletBatch::VerifyDatabaseFile(wallet_path, error_string);
37443740
}
37453741

37463742
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: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,7 @@ void MaybeCompactWalletDB()
967967
}
968968

969969
if (dbh.nLastFlushed != nUpdateCounter && GetTime() - dbh.nLastWalletUpdate >= 2) {
970-
if (BerkeleyBatch::PeriodicFlush(dbh)) {
970+
if (dbh.PeriodicFlush()) {
971971
dbh.nLastFlushed = nUpdateCounter;
972972
}
973973
}
@@ -976,16 +976,6 @@ void MaybeCompactWalletDB()
976976
fOneThread = false;
977977
}
978978

979-
bool WalletBatch::VerifyEnvironment(const fs::path& wallet_path, bilingual_str& errorStr)
980-
{
981-
return BerkeleyBatch::VerifyEnvironment(wallet_path, errorStr);
982-
}
983-
984-
bool WalletBatch::VerifyDatabaseFile(const fs::path& wallet_path, bilingual_str& errorStr)
985-
{
986-
return BerkeleyBatch::VerifyDatabaseFile(wallet_path, errorStr);
987-
}
988-
989979
bool WalletBatch::WriteDestData(const std::string &address, const std::string &key, const std::string &value)
990980
{
991981
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)