Skip to content

Commit 5394b39

Browse files
committed
Wallet: Split main logic from InitLoadWallet into CreateWalletFromFile
1 parent fb0c934 commit 5394b39

File tree

2 files changed

+54
-28
lines changed

2 files changed

+54
-28
lines changed

src/wallet/wallet.cpp

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3374,16 +3374,8 @@ std::string CWallet::GetWalletHelpString(bool showDebug)
33743374
return strUsage;
33753375
}
33763376

3377-
bool CWallet::InitLoadWallet()
3377+
CWallet* CWallet::CreateWalletFromFile(const std::string walletFile)
33783378
{
3379-
if (GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) {
3380-
pwalletMain = NULL;
3381-
LogPrintf("Wallet disabled!\n");
3382-
return true;
3383-
}
3384-
3385-
std::string walletFile = GetArg("-wallet", DEFAULT_WALLET_DAT);
3386-
33873379
// needed to restore wallet transaction meta data after -zapwallettxes
33883380
std::vector<CWalletTx> vWtx;
33893381

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

33993392
delete tempWallet;
@@ -3408,23 +3401,29 @@ bool CWallet::InitLoadWallet()
34083401
DBErrors nLoadWalletRet = walletInstance->LoadWallet(fFirstRun);
34093402
if (nLoadWalletRet != DB_LOAD_OK)
34103403
{
3411-
if (nLoadWalletRet == DB_CORRUPT)
3412-
return InitError(strprintf(_("Error loading %s: Wallet corrupted"), walletFile));
3404+
if (nLoadWalletRet == DB_CORRUPT) {
3405+
InitError(strprintf(_("Error loading %s: Wallet corrupted"), walletFile));
3406+
return NULL;
3407+
}
34133408
else if (nLoadWalletRet == DB_NONCRITICAL_ERROR)
34143409
{
34153410
InitWarning(strprintf(_("Error reading %s! All keys read correctly, but transaction data"
34163411
" or address book entries might be missing or incorrect."),
34173412
walletFile));
34183413
}
3419-
else if (nLoadWalletRet == DB_TOO_NEW)
3420-
return InitError(strprintf(_("Error loading %s: Wallet requires newer version of %s"),
3421-
walletFile, _(PACKAGE_NAME)));
3414+
else if (nLoadWalletRet == DB_TOO_NEW) {
3415+
InitError(strprintf(_("Error loading %s: Wallet requires newer version of %s"), walletFile, _(PACKAGE_NAME)));
3416+
return NULL;
3417+
}
34223418
else if (nLoadWalletRet == DB_NEED_REWRITE)
34233419
{
3424-
return InitError(strprintf(_("Wallet needed to be rewritten: restart %s to complete"), _(PACKAGE_NAME)));
3420+
InitError(strprintf(_("Wallet needed to be rewritten: restart %s to complete"), _(PACKAGE_NAME)));
3421+
return NULL;
3422+
}
3423+
else {
3424+
InitError(strprintf(_("Error loading %s"), walletFile));
3425+
return NULL;
34253426
}
3426-
else
3427-
return InitError(strprintf(_("Error loading %s"), walletFile));
34283427
}
34293428

34303429
if (GetBoolArg("-upgradewallet", fFirstRun))
@@ -3440,7 +3439,8 @@ bool CWallet::InitLoadWallet()
34403439
LogPrintf("Allowing wallet upgrade up to %i\n", nMaxVersion);
34413440
if (nMaxVersion < walletInstance->GetVersion())
34423441
{
3443-
return InitError(_("Cannot downgrade wallet"));
3442+
InitError(_("Cannot downgrade wallet"));
3443+
return NULL;
34443444
}
34453445
walletInstance->SetMaxVersion(nMaxVersion);
34463446
}
@@ -3457,18 +3457,24 @@ bool CWallet::InitLoadWallet()
34573457
CPubKey newDefaultKey;
34583458
if (walletInstance->GetKeyFromPool(newDefaultKey)) {
34593459
walletInstance->SetDefaultKey(newDefaultKey);
3460-
if (!walletInstance->SetAddressBook(walletInstance->vchDefaultKey.GetID(), "", "receive"))
3461-
return InitError(_("Cannot write default address") += "\n");
3460+
if (!walletInstance->SetAddressBook(walletInstance->vchDefaultKey.GetID(), "", "receive")) {
3461+
InitError(_("Cannot write default address") += "\n");
3462+
return NULL;
3463+
}
34623464
}
34633465

34643466
walletInstance->SetBestChain(chainActive.GetLocator());
34653467
}
34663468
else if (mapArgs.count("-usehd")) {
34673469
bool useHD = GetBoolArg("-usehd", DEFAULT_USE_HD_WALLET);
3468-
if (walletInstance->IsHDEnabled() && !useHD)
3469-
return InitError(strprintf(_("Error loading %s: You can't disable HD on a already existing HD wallet"), walletFile));
3470-
if (!walletInstance->IsHDEnabled() && useHD)
3471-
return InitError(strprintf(_("Error loading %s: You can't enable HD on a already existing non-HD wallet"), walletFile));
3470+
if (walletInstance->IsHDEnabled() && !useHD) {
3471+
InitError(strprintf(_("Error loading %s: You can't disable HD on a already existing HD wallet"), walletFile));
3472+
return NULL;
3473+
}
3474+
if (!walletInstance->IsHDEnabled() && useHD) {
3475+
InitError(strprintf(_("Error loading %s: You can't enable HD on a already existing non-HD wallet"), walletFile));
3476+
return NULL;
3477+
}
34723478
}
34733479

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

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

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

3547-
pwalletMain = walletInstance;
3555+
return walletInstance;
3556+
}
3557+
3558+
bool CWallet::InitLoadWallet()
3559+
{
3560+
if (GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) {
3561+
pwalletMain = NULL;
3562+
LogPrintf("Wallet disabled!\n");
3563+
return true;
3564+
}
3565+
3566+
std::string walletFile = GetArg("-wallet", DEFAULT_WALLET_DAT);
3567+
3568+
CWallet * const pwallet = CreateWalletFromFile(walletFile);
3569+
if (!pwallet) {
3570+
return false;
3571+
}
3572+
pwalletMain = pwallet;
35483573

35493574
return true;
35503575
}

src/wallet/wallet.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
917917
static std::string GetWalletHelpString(bool showDebug);
918918

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

922923
/**

0 commit comments

Comments
 (0)