Skip to content

Commit

Permalink
Wallet.dat "unhooked" from the data directory
Browse files Browse the repository at this point in the history
wallet.dat no longer restricted in name or path. Can be anything,
anywhere. It is still "married" to the 1MB file
[datadir]/database/log.0000000001, what ever that is? I think it could
be moved along with [wallet.dat] too? Built and tested with bitcoind.exe
& bitcoin-qt.exe on windows. Tetsed with partial pathnames, full
pathnames, just filenames, just filenames with no extension.
I did not test with coins in the wallet but I see no difference. The
constant error messages need to say something other than wallet.dat, I
should think.

Ron
  • Loading branch information
older-c-coder committed Jul 3, 2013
1 parent 8ab14e6 commit 9290190
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 22 deletions.
54 changes: 39 additions & 15 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ using namespace std;
using namespace boost;

CWallet* pwalletMain;
char *walletPath; // useful for printf style messages, etc.
CClientUIInterface uiInterface;

// Used to pass flags to the Bind() function
Expand Down Expand Up @@ -247,6 +248,7 @@ std::string HelpMessage()
" -pid=<file> " + _("Specify pid file (default: bitcoind.pid)") + "\n" +
" -gen " + _("Generate coins") + "\n" +
" -gen=0 " + _("Don't generate coins") + "\n" +
" -walletpath=<file> " + _("Specify the wallet filename (default: wallet.dat)") + "\n" +
" -datadir=<dir> " + _("Specify data directory") + "\n" +
" -dbcache=<n> " + _("Set database cache size in megabytes (default: 25)") + "\n" +
" -timeout=<n> " + _("Specify connection timeout in milliseconds (default: 5000)") + "\n" +
Expand Down Expand Up @@ -610,6 +612,21 @@ bool AppInit2()

uiInterface.InitMessage(_("Verifying wallet..."));

boost::filesystem::path
pathWalletFile(GetArg("-walletpath", "wallet.dat"));

if (!pathWalletFile.is_complete())
pathWalletFile = GetDataDir(false) / pathWalletFile;

walletPath = new char[ pathWalletFile.string().size() + 1 ];
strcpy( walletPath, pathWalletFile.string().c_str() );

(void)printf(
"wallet filename is %s, path (derived) is:\n%s\n",
pathWalletFile.filename().string().c_str(),
walletPath
);

if (!bitdb.Open(GetDataDir()))
{
string msg = strprintf(_("Error initializing wallet database environment %s!"), strDataDir.c_str());
Expand All @@ -619,23 +636,27 @@ bool AppInit2()
if (GetBoolArg("-salvagewallet"))
{
// Recover readable keypairs:
if (!CWalletDB::Recover(bitdb, "wallet.dat", true))
//if (!CWalletDB::Recover(bitdb, "wallet.dat", true))
if (!CWalletDB::Recover(bitdb, walletPath, true))
return false;
}

if (filesystem::exists(GetDataDir() / "wallet.dat"))
//if (filesystem::exists(GetDataDir() / "wallet.dat"))
if (filesystem::exists(walletPath))
{
CDBEnv::VerifyResult r = bitdb.Verify("wallet.dat", CWalletDB::Recover);
//CDBEnv::VerifyResult r = bitdb.Verify("wallet.dat", CWalletDB::Recover);
CDBEnv::VerifyResult r = bitdb.Verify(walletPath, CWalletDB::Recover);
if (r == CDBEnv::RECOVER_OK)
{
string msg = 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."), strDataDir.c_str());
string msg = strprintf(_("Warning: %s corrupt, data salvaged!"
" Original %s saved as wallet.{timestamp}.bak in %s; if"
" your balance or transactions are incorrect, you should"
" restore from a backup."),
walletPath, walletPath, strDataDir.c_str());
InitWarning(msg);
}
if (r == CDBEnv::RECOVER_FAIL)
return InitError(_("wallet.dat corrupt, salvage failed"));
return InitError(_("wallet.dat corrupt, salvage failed")); // should say %s walletPath
}

// ********************************************************* Step 6: network initialization
Expand Down Expand Up @@ -835,7 +856,7 @@ bool AppInit2()
}

fLoaded = true;
} while(false);
} while(false); // this always fails, so isn't it redundant? Better just { } if at all?

if (!fLoaded) {
// first suggest a reindex
Expand Down Expand Up @@ -903,16 +924,17 @@ bool AppInit2()

nStart = GetTimeMillis();
bool fFirstRun = true;
pwalletMain = new CWallet("wallet.dat");
//pwalletMain = new CWallet("wallet.dat");
pwalletMain = new CWallet(walletPath);
DBErrors nLoadWalletRet = pwalletMain->LoadWallet(fFirstRun);
if (nLoadWalletRet != DB_LOAD_OK)
{
if (nLoadWalletRet == DB_CORRUPT)
strErrors << _("Error loading wallet.dat: Wallet corrupted") << "\n";
strErrors << _("Error loading wallet.dat: Wallet corrupted") << "\n"; // again walletPath
else if (nLoadWalletRet == DB_NONCRITICAL_ERROR)
{
string msg(_("Warning: error reading wallet.dat! All keys read correctly, but transaction data"
" or address book entries might be missing or incorrect."));
" or address book entries might be missing or incorrect.")); // ditto
InitWarning(msg);
}
else if (nLoadWalletRet == DB_TOO_NEW)
Expand All @@ -924,7 +946,7 @@ bool AppInit2()
return InitError(strErrors.str());
}
else
strErrors << _("Error loading wallet.dat") << "\n";
strErrors << _("Error loading wallet.dat") << "\n"; //ditto
}

if (GetBoolArg("-upgradewallet", fFirstRun))
Expand Down Expand Up @@ -966,15 +988,17 @@ bool AppInit2()
pindexRescan = pindexGenesisBlock;
else
{
CWalletDB walletdb("wallet.dat");
//CWalletDB walletdb("wallet.dat");
CWalletDB walletdb(walletPath);
CBlockLocator locator;
if (walletdb.ReadBestBlock(locator))
pindexRescan = locator.GetBlockIndex();
}
if (pindexBest && pindexBest != pindexRescan)
{
uiInterface.InitMessage(_("Rescanning..."));
printf("Rescanning last %i blocks (from block %i)...\n", pindexBest->nHeight - pindexRescan->nHeight, pindexRescan->nHeight);
printf("Rescanning last %i blocks (from block %i)...\n",
pindexBest->nHeight - pindexRescan->nHeight, pindexRescan->nHeight);
nStart = GetTimeMillis();
pwalletMain->ScanForWalletTransactions(pindexRescan, true);
printf(" rescan %15"PRI64d"ms\n", GetTimeMillis() - nStart);
Expand Down
1 change: 1 addition & 0 deletions src/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "wallet.h"

extern CWallet* pwalletMain;
extern char *walletPath; // useful for printf style messages, etc.

void StartShutdown();
void Shutdown(void* parg);
Expand Down
6 changes: 4 additions & 2 deletions src/qt/optionsmodel.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "optionsmodel.h"

#include "bitcoinunits.h"
#include "init.h"
#include "init.h" // graciously gives walletPath
#include "walletdb.h"
#include "guiutil.h"

Expand Down Expand Up @@ -89,7 +89,9 @@ bool OptionsModel::Upgrade()
settings.setValue("bImportFinished", true);

// Move settings from old wallet.dat (if any):
CWalletDB walletdb("wallet.dat");
//CWalletDB walletdb("wallet.dat");
CWalletDB walletdb(walletPath);


QList<QString> intOptions;
intOptions << "nDisplayUnit" << "nTransactionFee";
Expand Down
15 changes: 10 additions & 5 deletions src/walletdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "walletdb.h"
#include "wallet.h"
#include "init.h" // in virtue of walletPath
#include <boost/filesystem.hpp>

using namespace std;
Expand Down Expand Up @@ -527,7 +528,8 @@ void ThreadFlushWalletDB(void* parg)
map<string, int>::iterator mi = bitdb.mapFileUseCount.find(strFile);
if (mi != bitdb.mapFileUseCount.end())
{
printf("Flushing wallet.dat\n");
//printf("Flushing wallet.dat\n");
printf("Flushing %s\n", walletPath);
nLastFlushed = nWalletDBUpdated;
int64 nStart = GetTimeMillis();

Expand All @@ -536,7 +538,8 @@ void ThreadFlushWalletDB(void* parg)
bitdb.CheckpointLSN(strFile);

bitdb.mapFileUseCount.erase(mi++);
printf("Flushed wallet.dat %"PRI64d"ms\n", GetTimeMillis() - nStart);
//printf("Flushed wallet.dat %"PRI64d"ms\n", GetTimeMillis() - nStart);
printf("Flushed %s %"PRI64d"ms\n", walletPath, GetTimeMillis() - nStart);
}
}
}
Expand All @@ -560,7 +563,8 @@ bool BackupWallet(const CWallet& wallet, const string& strDest)
bitdb.mapFileUseCount.erase(wallet.strWalletFile);

// Copy wallet.dat
filesystem::path pathSrc = GetDataDir() / wallet.strWalletFile;
//filesystem::path pathSrc = GetDataDir() / wallet.strWalletFile;
filesystem::path pathSrc = walletPath;
filesystem::path pathDest(strDest);
if (filesystem::is_directory(pathDest))
pathDest /= wallet.strWalletFile;
Expand All @@ -571,10 +575,11 @@ bool BackupWallet(const CWallet& wallet, const string& strDest)
#else
filesystem::copy_file(pathSrc, pathDest);
#endif
printf("copied wallet.dat to %s\n", pathDest.string().c_str());
//printf("copied wallet.dat to %s\n", pathDest.string().c_str());
printf("copied %s to %s\n", walletPath, pathDest.string().c_str());
return true;
} catch(const filesystem::filesystem_error &e) {
printf("error copying wallet.dat to %s - %s\n", pathDest.string().c_str(), e.what());
printf("error copying %s to %s - %s\n", walletPath, pathDest.string().c_str(), e.what());
return false;
}
}
Expand Down

0 comments on commit 9290190

Please sign in to comment.