Skip to content

Commit 2bb1c87

Browse files
jonasschnellilaanwj
authored andcommitted
refactor: move bdb (bitdb) interaction from init.cpp to wallet.cpp
this will remove db.h from init.cpp
1 parent 05f17d4 commit 2bb1c87

File tree

3 files changed

+72
-44
lines changed

3 files changed

+72
-44
lines changed

src/init.cpp

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include "util.h"
2525
#include "utilmoneystr.h"
2626
#ifdef ENABLE_WALLET
27-
#include "wallet/db.h"
2827
#include "wallet/wallet.h"
2928
#include "wallet/walletdb.h"
3029
#endif
@@ -151,7 +150,7 @@ void Shutdown()
151150
StopRPCThreads();
152151
#ifdef ENABLE_WALLET
153152
if (pwalletMain)
154-
bitdb.Flush(false);
153+
pwalletMain->Flush(false);
155154
GenerateBitcoins(false, NULL, 0);
156155
#endif
157156
StopNode();
@@ -184,7 +183,7 @@ void Shutdown()
184183
}
185184
#ifdef ENABLE_WALLET
186185
if (pwalletMain)
187-
bitdb.Flush(true);
186+
pwalletMain->Flush(true);
188187
#endif
189188
#ifndef WIN32
190189
boost::filesystem::remove(GetPidFile());
@@ -852,47 +851,17 @@ bool AppInit2(boost::thread_group& threadGroup)
852851
LogPrintf("Using wallet %s\n", strWalletFile);
853852
uiInterface.InitMessage(_("Verifying wallet..."));
854853

855-
if (!bitdb.Open(GetDataDir()))
856-
{
857-
// try moving the database env out of the way
858-
boost::filesystem::path pathDatabase = GetDataDir() / "database";
859-
boost::filesystem::path pathDatabaseBak = GetDataDir() / strprintf("database.%d.bak", GetTime());
860-
try {
861-
boost::filesystem::rename(pathDatabase, pathDatabaseBak);
862-
LogPrintf("Moved old %s to %s. Retrying.\n", pathDatabase.string(), pathDatabaseBak.string());
863-
} catch (const boost::filesystem::filesystem_error&) {
864-
// failure is ok (well, not really, but it's not worse than what we started with)
865-
}
866-
867-
// try again
868-
if (!bitdb.Open(GetDataDir())) {
869-
// if it still fails, it probably means we can't even create the database env
870-
string msg = strprintf(_("Error initializing wallet database environment %s!"), strDataDir);
871-
return InitError(msg);
872-
}
873-
}
874-
875-
if (GetBoolArg("-salvagewallet", false))
876-
{
877-
// Recover readable keypairs:
878-
if (!CWalletDB::Recover(bitdb, strWalletFile, true))
879-
return false;
880-
}
881-
882-
if (boost::filesystem::exists(GetDataDir() / strWalletFile))
883-
{
884-
CDBEnv::VerifyResult r = bitdb.Verify(strWalletFile, CWalletDB::Recover);
885-
if (r == CDBEnv::RECOVER_OK)
886-
{
887-
string msg = strprintf(_("Warning: wallet.dat corrupt, data salvaged!"
888-
" Original wallet.dat saved as wallet.{timestamp}.bak in %s; if"
889-
" your balance or transactions are incorrect you should"
890-
" restore from a backup."), strDataDir);
891-
InitWarning(msg);
892-
}
893-
if (r == CDBEnv::RECOVER_FAIL)
894-
return InitError(_("wallet.dat corrupt, salvage failed"));
895-
}
854+
std::string warningString;
855+
std::string errorString;
856+
857+
if (!CWallet::Verify(strWalletFile, warningString, errorString))
858+
return false;
859+
860+
if (!warningString.empty())
861+
InitWarning(warningString);
862+
if (!errorString.empty())
863+
return InitError(warningString);
864+
896865
} // (!fDisableWallet)
897866
#endif // ENABLE_WALLET
898867
// ********************************************************* Step 6: network initialization

src/wallet/wallet.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <assert.h>
1919

2020
#include <boost/algorithm/string/replace.hpp>
21+
#include <boost/filesystem.hpp>
2122
#include <boost/thread.hpp>
2223

2324
using namespace std;
@@ -339,6 +340,58 @@ set<uint256> CWallet::GetConflicts(const uint256& txid) const
339340
return result;
340341
}
341342

343+
void CWallet::Flush(bool shutdown)
344+
{
345+
bitdb.Flush(shutdown);
346+
}
347+
348+
bool CWallet::Verify(const string walletFile, string& warningString, string& errorString)
349+
{
350+
if (!bitdb.Open(GetDataDir()))
351+
{
352+
// try moving the database env out of the way
353+
boost::filesystem::path pathDatabase = GetDataDir() / "database";
354+
boost::filesystem::path pathDatabaseBak = GetDataDir() / strprintf("database.%d.bak", GetTime());
355+
try {
356+
boost::filesystem::rename(pathDatabase, pathDatabaseBak);
357+
LogPrintf("Moved old %s to %s. Retrying.\n", pathDatabase.string(), pathDatabaseBak.string());
358+
} catch (const boost::filesystem::filesystem_error&) {
359+
// failure is ok (well, not really, but it's not worse than what we started with)
360+
}
361+
362+
// try again
363+
if (!bitdb.Open(GetDataDir())) {
364+
// if it still fails, it probably means we can't even create the database env
365+
string msg = strprintf(_("Error initializing wallet database environment %s!"), GetDataDir());
366+
errorString += msg;
367+
return true;
368+
}
369+
}
370+
371+
if (GetBoolArg("-salvagewallet", false))
372+
{
373+
// Recover readable keypairs:
374+
if (!CWalletDB::Recover(bitdb, walletFile, true))
375+
return false;
376+
}
377+
378+
if (boost::filesystem::exists(GetDataDir() / walletFile))
379+
{
380+
CDBEnv::VerifyResult r = bitdb.Verify(walletFile, CWalletDB::Recover);
381+
if (r == CDBEnv::RECOVER_OK)
382+
{
383+
warningString += strprintf(_("Warning: wallet.dat corrupt, data salvaged!"
384+
" Original wallet.dat saved as wallet.{timestamp}.bak in %s; if"
385+
" your balance or transactions are incorrect you should"
386+
" restore from a backup."), GetDataDir());
387+
}
388+
if (r == CDBEnv::RECOVER_FAIL)
389+
errorString += _("wallet.dat corrupt, salvage failed");
390+
}
391+
392+
return true;
393+
}
394+
342395
void CWallet::SyncMetaData(pair<TxSpends::iterator, TxSpends::iterator> range)
343396
{
344397
// We want all the wallet transactions in range to have the same metadata as

src/wallet/wallet.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,12 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
743743
//! Get wallet transactions that conflict with given transaction (spend same outputs)
744744
std::set<uint256> GetConflicts(const uint256& txid) const;
745745

746+
//! Flush wallet (bitdb flush)
747+
void Flush(bool shutdown=false);
748+
749+
//! Verify the wallet database and perform salvage if required
750+
static bool Verify(const std::string walletFile, std::string& warningString, std::string& errorString);
751+
746752
/**
747753
* Address book entry changed.
748754
* @note called with lock cs_wallet held.

0 commit comments

Comments
 (0)