Skip to content

Commit

Permalink
Wallet: Split main logic from InitLoadWallet into CreateWalletFromFile
Browse files Browse the repository at this point in the history
  • Loading branch information
luke-jr committed Nov 11, 2016
1 parent fb0c934 commit 5394b39
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 28 deletions.
81 changes: 53 additions & 28 deletions src/wallet/wallet.cpp
Expand Up @@ -3374,16 +3374,8 @@ std::string CWallet::GetWalletHelpString(bool showDebug)
return strUsage;
}

bool CWallet::InitLoadWallet()
CWallet* CWallet::CreateWalletFromFile(const std::string walletFile)
{
if (GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) {
pwalletMain = NULL;
LogPrintf("Wallet disabled!\n");
return true;
}

std::string walletFile = GetArg("-wallet", DEFAULT_WALLET_DAT);

// needed to restore wallet transaction meta data after -zapwallettxes
std::vector<CWalletTx> vWtx;

Expand All @@ -3393,7 +3385,8 @@ bool CWallet::InitLoadWallet()
CWallet *tempWallet = new CWallet(walletFile);
DBErrors nZapWalletRet = tempWallet->ZapWalletTx(vWtx);
if (nZapWalletRet != DB_LOAD_OK) {
return InitError(strprintf(_("Error loading %s: Wallet corrupted"), walletFile));
InitError(strprintf(_("Error loading %s: Wallet corrupted"), walletFile));
return NULL;
}

delete tempWallet;
Expand All @@ -3408,23 +3401,29 @@ bool CWallet::InitLoadWallet()
DBErrors nLoadWalletRet = walletInstance->LoadWallet(fFirstRun);
if (nLoadWalletRet != DB_LOAD_OK)
{
if (nLoadWalletRet == DB_CORRUPT)
return InitError(strprintf(_("Error loading %s: Wallet corrupted"), walletFile));
if (nLoadWalletRet == DB_CORRUPT) {
InitError(strprintf(_("Error loading %s: Wallet corrupted"), walletFile));
return NULL;
}
else if (nLoadWalletRet == DB_NONCRITICAL_ERROR)
{
InitWarning(strprintf(_("Error reading %s! All keys read correctly, but transaction data"
" or address book entries might be missing or incorrect."),
walletFile));
}
else if (nLoadWalletRet == DB_TOO_NEW)
return InitError(strprintf(_("Error loading %s: Wallet requires newer version of %s"),
walletFile, _(PACKAGE_NAME)));
else if (nLoadWalletRet == DB_TOO_NEW) {
InitError(strprintf(_("Error loading %s: Wallet requires newer version of %s"), walletFile, _(PACKAGE_NAME)));
return NULL;
}
else if (nLoadWalletRet == DB_NEED_REWRITE)
{
return InitError(strprintf(_("Wallet needed to be rewritten: restart %s to complete"), _(PACKAGE_NAME)));
InitError(strprintf(_("Wallet needed to be rewritten: restart %s to complete"), _(PACKAGE_NAME)));
return NULL;
}
else {
InitError(strprintf(_("Error loading %s"), walletFile));
return NULL;
}
else
return InitError(strprintf(_("Error loading %s"), walletFile));
}

if (GetBoolArg("-upgradewallet", fFirstRun))
Expand All @@ -3440,7 +3439,8 @@ bool CWallet::InitLoadWallet()
LogPrintf("Allowing wallet upgrade up to %i\n", nMaxVersion);
if (nMaxVersion < walletInstance->GetVersion())
{
return InitError(_("Cannot downgrade wallet"));
InitError(_("Cannot downgrade wallet"));
return NULL;
}
walletInstance->SetMaxVersion(nMaxVersion);
}
Expand All @@ -3457,18 +3457,24 @@ bool CWallet::InitLoadWallet()
CPubKey newDefaultKey;
if (walletInstance->GetKeyFromPool(newDefaultKey)) {
walletInstance->SetDefaultKey(newDefaultKey);
if (!walletInstance->SetAddressBook(walletInstance->vchDefaultKey.GetID(), "", "receive"))
return InitError(_("Cannot write default address") += "\n");
if (!walletInstance->SetAddressBook(walletInstance->vchDefaultKey.GetID(), "", "receive")) {
InitError(_("Cannot write default address") += "\n");
return NULL;
}
}

walletInstance->SetBestChain(chainActive.GetLocator());
}
else if (mapArgs.count("-usehd")) {
bool useHD = GetBoolArg("-usehd", DEFAULT_USE_HD_WALLET);
if (walletInstance->IsHDEnabled() && !useHD)
return InitError(strprintf(_("Error loading %s: You can't disable HD on a already existing HD wallet"), walletFile));
if (!walletInstance->IsHDEnabled() && useHD)
return InitError(strprintf(_("Error loading %s: You can't enable HD on a already existing non-HD wallet"), walletFile));
if (walletInstance->IsHDEnabled() && !useHD) {
InitError(strprintf(_("Error loading %s: You can't disable HD on a already existing HD wallet"), walletFile));
return NULL;
}
if (!walletInstance->IsHDEnabled() && useHD) {
InitError(strprintf(_("Error loading %s: You can't enable HD on a already existing non-HD wallet"), walletFile));
return NULL;
}
}

LogPrintf(" wallet %15dms\n", GetTimeMillis() - nStart);
Expand Down Expand Up @@ -3498,8 +3504,10 @@ bool CWallet::InitLoadWallet()
while (block && block->pprev && (block->pprev->nStatus & BLOCK_HAVE_DATA) && block->pprev->nTx > 0 && pindexRescan != block)
block = block->pprev;

if (pindexRescan != block)
return InitError(_("Prune: last wallet synchronisation goes beyond pruned data. You need to -reindex (download the whole blockchain again in case of pruned node)"));
if (pindexRescan != block) {
InitError(_("Prune: last wallet synchronisation goes beyond pruned data. You need to -reindex (download the whole blockchain again in case of pruned node)"));
return NULL;
}
}

uiInterface.InitMessage(_("Rescanning..."));
Expand Down Expand Up @@ -3544,7 +3552,24 @@ bool CWallet::InitLoadWallet()
LogPrintf("mapAddressBook.size() = %u\n", walletInstance->mapAddressBook.size());
}

pwalletMain = walletInstance;
return walletInstance;
}

bool CWallet::InitLoadWallet()
{
if (GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) {
pwalletMain = NULL;
LogPrintf("Wallet disabled!\n");
return true;
}

std::string walletFile = GetArg("-wallet", DEFAULT_WALLET_DAT);

CWallet * const pwallet = CreateWalletFromFile(walletFile);
if (!pwallet) {
return false;
}
pwalletMain = pwallet;

return true;
}
Expand Down
1 change: 1 addition & 0 deletions src/wallet/wallet.h
Expand Up @@ -917,6 +917,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
static std::string GetWalletHelpString(bool showDebug);

/* Initializes the wallet, returns a new CWallet instance or a null pointer in case of an error */
static CWallet* CreateWalletFromFile(const std::string walletFile);
static bool InitLoadWallet();

/**
Expand Down

0 comments on commit 5394b39

Please sign in to comment.