aboutsummaryrefslogtreecommitdiff
path: root/src/wallet/wallet.cpp
diff options
context:
space:
mode:
authorJonas Schnelli <jonas.schnelli@include7.ch>2015-02-04 21:19:27 +0100
committerWladimir J. van der Laan <laanwj@gmail.com>2015-03-20 16:23:39 +0100
commit2bb1c87700a80f0b20b4b2cbd68a0d82a18dbcc0 (patch)
tree61bdf899e90c96abd096c3c0abd5fdbd54369eff /src/wallet/wallet.cpp
parent05f17d4eaa9e74836d5736d50e456420949f2732 (diff)
downloadbitcoin-2bb1c87700a80f0b20b4b2cbd68a0d82a18dbcc0.tar.xz
refactor: move bdb (bitdb) interaction from init.cpp to wallet.cpp
this will remove db.h from init.cpp
Diffstat (limited to 'src/wallet/wallet.cpp')
-rw-r--r--src/wallet/wallet.cpp53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp
index dd33109267..44b416d496 100644
--- a/src/wallet/wallet.cpp
+++ b/src/wallet/wallet.cpp
@@ -18,6 +18,7 @@
#include <assert.h>
#include <boost/algorithm/string/replace.hpp>
+#include <boost/filesystem.hpp>
#include <boost/thread.hpp>
using namespace std;
@@ -339,6 +340,58 @@ set<uint256> CWallet::GetConflicts(const uint256& txid) const
return result;
}
+void CWallet::Flush(bool shutdown)
+{
+ bitdb.Flush(shutdown);
+}
+
+bool CWallet::Verify(const string walletFile, string& warningString, string& errorString)
+{
+ if (!bitdb.Open(GetDataDir()))
+ {
+ // try moving the database env out of the way
+ boost::filesystem::path pathDatabase = GetDataDir() / "database";
+ boost::filesystem::path pathDatabaseBak = GetDataDir() / strprintf("database.%d.bak", GetTime());
+ try {
+ boost::filesystem::rename(pathDatabase, pathDatabaseBak);
+ LogPrintf("Moved old %s to %s. Retrying.\n", pathDatabase.string(), pathDatabaseBak.string());
+ } catch (const boost::filesystem::filesystem_error&) {
+ // failure is ok (well, not really, but it's not worse than what we started with)
+ }
+
+ // try again
+ if (!bitdb.Open(GetDataDir())) {
+ // if it still fails, it probably means we can't even create the database env
+ string msg = strprintf(_("Error initializing wallet database environment %s!"), GetDataDir());
+ errorString += msg;
+ return true;
+ }
+ }
+
+ if (GetBoolArg("-salvagewallet", false))
+ {
+ // Recover readable keypairs:
+ if (!CWalletDB::Recover(bitdb, walletFile, true))
+ return false;
+ }
+
+ if (boost::filesystem::exists(GetDataDir() / walletFile))
+ {
+ CDBEnv::VerifyResult r = bitdb.Verify(walletFile, CWalletDB::Recover);
+ if (r == CDBEnv::RECOVER_OK)
+ {
+ warningString += strprintf(_("Warning: wallet.dat corrupt, data salvaged!"
+ " Original wallet.dat saved as wallet.{timestamp}.bak in %s; if"
+ " your balance or transactions are incorrect you should"
+ " restore from a backup."), GetDataDir());
+ }
+ if (r == CDBEnv::RECOVER_FAIL)
+ errorString += _("wallet.dat corrupt, salvage failed");
+ }
+
+ return true;
+}
+
void CWallet::SyncMetaData(pair<TxSpends::iterator, TxSpends::iterator> range)
{
// We want all the wallet transactions in range to have the same metadata as