Skip to content

Commit 539dea4

Browse files
committed
[Wallet] move "load wallet phase" to CWallet
>> inspired by bitcoin/bitcoin@fc7c60d Include also zwallet initialization (to be removed after v5.0)
1 parent 7644318 commit 539dea4

File tree

3 files changed

+193
-174
lines changed

3 files changed

+193
-174
lines changed

src/init.cpp

Lines changed: 11 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,183 +1699,20 @@ bool AppInit2()
16991699

17001700
// ********************************************************* Step 8: load wallet
17011701
#ifdef ENABLE_WALLET
1702+
pwalletMain = nullptr;
1703+
zwalletMain = nullptr;
17021704
if (fDisableWallet) {
1703-
pwalletMain = NULL;
1704-
zwalletMain = NULL;
17051705
LogPrintf("Wallet disabled!\n");
17061706
} else {
1707-
// needed to restore wallet transaction meta data after -zapwallettxes
1708-
std::vector<CWalletTx> vWtx;
1709-
1710-
if (GetBoolArg("-zapwallettxes", false)) {
1711-
uiInterface.InitMessage(_("Zapping all transactions from wallet..."));
1712-
1713-
pwalletMain = new CWallet(strWalletFile);
1714-
DBErrors nZapWalletRet = pwalletMain->ZapWalletTx(vWtx);
1715-
if (nZapWalletRet != DB_LOAD_OK) {
1716-
uiInterface.InitMessage(_("Error loading wallet.dat: Wallet corrupted"));
1717-
return false;
1718-
}
1719-
1720-
delete pwalletMain;
1721-
pwalletMain = NULL;
1722-
}
1723-
1724-
uiInterface.InitMessage(_("Loading wallet..."));
1725-
fVerifyingBlocks = true;
1726-
1727-
const int64_t nWalletStartTime = GetTimeMillis();
1728-
bool fFirstRun = true;
1729-
pwalletMain = new CWallet(strWalletFile);
1730-
DBErrors nLoadWalletRet = pwalletMain->LoadWallet(fFirstRun);
1731-
if (nLoadWalletRet != DB_LOAD_OK) {
1732-
if (nLoadWalletRet == DB_CORRUPT)
1733-
strErrors << _("Error loading wallet.dat: Wallet corrupted") << "\n";
1734-
else if (nLoadWalletRet == DB_NONCRITICAL_ERROR) {
1735-
std::string msg(_("Warning: error reading wallet.dat! All keys read correctly, but transaction data"
1736-
" or address book entries might be missing or incorrect."));
1737-
UIWarning(msg);
1738-
} else if (nLoadWalletRet == DB_TOO_NEW)
1739-
strErrors << _("Error loading wallet.dat: Wallet requires newer version of PIVX Core") << "\n";
1740-
else if (nLoadWalletRet == DB_NEED_REWRITE) {
1741-
strErrors << _("Wallet needed to be rewritten: restart PIVX Core to complete") << "\n";
1742-
LogPrintf("%s", strErrors.str());
1743-
return UIError(strErrors.str());
1744-
} else
1745-
strErrors << _("Error loading wallet.dat") << "\n";
1746-
}
1747-
1748-
// check minimum stake split threshold
1749-
if (pwalletMain->nStakeSplitThreshold && pwalletMain->nStakeSplitThreshold < CWallet::minStakeSplitThreshold) {
1750-
LogPrintf("WARNING: stake split threshold value %s too low. Restoring to minimum value %s.\n",
1751-
FormatMoney(pwalletMain->nStakeSplitThreshold), FormatMoney(CWallet::minStakeSplitThreshold));
1752-
pwalletMain->nStakeSplitThreshold = CWallet::minStakeSplitThreshold;
1753-
}
1754-
1755-
int prev_version = pwalletMain->GetVersion();
1756-
1757-
// Forced upgrade
1758-
const bool fLegacyWallet = GetBoolArg("-legacywallet", false);
1759-
if (GetBoolArg("-upgradewallet", fFirstRun && !fLegacyWallet)) {
1760-
if (prev_version <= FEATURE_PRE_PIVX && pwalletMain->IsLocked()) {
1761-
// Cannot upgrade a locked wallet
1762-
std::string strProblem = "Cannot upgrade a locked wallet.\n";
1763-
strErrors << _("Error: ") << strProblem;
1764-
LogPrintf("%s", strErrors.str());
1765-
return UIError(strProblem);
1766-
}
1767-
1768-
int nMaxVersion = GetArg("-upgradewallet", 0);
1769-
if (nMaxVersion == 0) // the -upgradewallet without argument case
1770-
{
1771-
LogPrintf("Performing wallet upgrade to %i\n", FEATURE_LATEST);
1772-
nMaxVersion = FEATURE_LATEST;
1773-
pwalletMain->SetMinVersion(FEATURE_LATEST); // permanently upgrade the wallet immediately
1774-
} else
1775-
LogPrintf("Allowing wallet upgrade up to %i\n", nMaxVersion);
1776-
if (nMaxVersion < pwalletMain->GetVersion())
1777-
strErrors << _("Cannot downgrade wallet") << "\n";
1778-
pwalletMain->SetMaxVersion(nMaxVersion);
1779-
}
1780-
1781-
// Upgrade to HD only if explicit upgrade was requested
1782-
if (GetBoolArg("-upgradewallet", false)) {
1783-
std::string upgradeError;
1784-
if (!pwalletMain->Upgrade(upgradeError, prev_version)) {
1785-
strErrors << upgradeError << "\n";
1786-
}
1787-
}
1788-
1789-
if (fFirstRun) {
1790-
if (!fLegacyWallet) {
1791-
// Create new HD Wallet
1792-
LogPrintf("Creating HD Wallet\n");
1793-
// Ensure this wallet.dat can only be opened by clients supporting HD.
1794-
pwalletMain->SetMinVersion(FEATURE_LATEST);
1795-
pwalletMain->SetupSPKM();
1796-
} else {
1797-
if (!Params().IsRegTestNet()) {
1798-
std::string strProblem = "Legacy wallets can only be created on RegTest.\n";
1799-
strErrors << _("Error: ") << strProblem;
1800-
LogPrintf("%s", strErrors.str());
1801-
return UIError(strProblem);
1802-
}
1803-
// Create legacy wallet
1804-
LogPrintf("Creating Pre-HD Wallet\n");
1805-
pwalletMain->SetMaxVersion(FEATURE_PRE_PIVX);
1806-
}
1807-
1808-
// Top up the keypool
1809-
if (!pwalletMain->TopUpKeyPool()) {
1810-
// Error generating keys
1811-
UIError(_("Unable to generate initial key") += "\n");
1812-
return error("%s %s", __func__ , "Unable to generate initial key");
1813-
}
1814-
1815-
pwalletMain->SetBestChain(chainActive.GetLocator());
1816-
}
1817-
1818-
LogPrintf("Init errors: %s\n", strErrors.str());
1819-
LogPrintf("Wallet completed loading in %15dms\n", GetTimeMillis() - nWalletStartTime);
1820-
zwalletMain = new CzPIVWallet(pwalletMain);
1821-
pwalletMain->setZWallet(zwalletMain);
1822-
1823-
RegisterValidationInterface(pwalletMain);
1824-
1825-
CBlockIndex* pindexRescan = chainActive.Tip();
1826-
if (GetBoolArg("-rescan", false))
1827-
pindexRescan = chainActive.Genesis();
1828-
else {
1829-
CWalletDB walletdb(strWalletFile);
1830-
CBlockLocator locator;
1831-
if (walletdb.ReadBestBlock(locator))
1832-
pindexRescan = FindForkInGlobalIndex(chainActive, locator);
1833-
else
1834-
pindexRescan = chainActive.Genesis();
1835-
}
1836-
if (chainActive.Tip() && chainActive.Tip() != pindexRescan) {
1837-
uiInterface.InitMessage(_("Rescanning..."));
1838-
LogPrintf("Rescanning last %i blocks (from block %i)...\n", chainActive.Height() - pindexRescan->nHeight, pindexRescan->nHeight);
1839-
const int64_t nWalletRescanTime = GetTimeMillis();
1840-
if (pwalletMain->ScanForWalletTransactions(pindexRescan, true, true) == -1) {
1841-
return error("Shutdown requested over the txs scan. Exiting.");
1842-
}
1843-
LogPrintf("Rescan completed in %15dms\n", GetTimeMillis() - nWalletRescanTime);
1844-
pwalletMain->SetBestChain(chainActive.GetLocator());
1845-
CWalletDB::IncrementUpdateCounter();
1846-
1847-
// Restore wallet transaction metadata after -zapwallettxes=1
1848-
if (GetBoolArg("-zapwallettxes", false) && GetArg("-zapwallettxes", "1") != "2") {
1849-
CWalletDB walletdb(strWalletFile);
1850-
for (const CWalletTx& wtxOld : vWtx) {
1851-
uint256 hash = wtxOld.GetHash();
1852-
std::map<uint256, CWalletTx>::iterator mi = pwalletMain->mapWallet.find(hash);
1853-
if (mi != pwalletMain->mapWallet.end()) {
1854-
const CWalletTx* copyFrom = &wtxOld;
1855-
CWalletTx* copyTo = &mi->second;
1856-
copyTo->mapValue = copyFrom->mapValue;
1857-
copyTo->vOrderForm = copyFrom->vOrderForm;
1858-
copyTo->nTimeReceived = copyFrom->nTimeReceived;
1859-
copyTo->nTimeSmart = copyFrom->nTimeSmart;
1860-
copyTo->fFromMe = copyFrom->fFromMe;
1861-
copyTo->strFromAccount = copyFrom->strFromAccount;
1862-
copyTo->nOrderPos = copyFrom->nOrderPos;
1863-
walletdb.WriteTx(*copyTo);
1864-
}
1865-
}
1866-
}
1867-
}
1868-
fVerifyingBlocks = false;
1869-
1870-
if (!zwalletMain->GetMasterSeed().IsNull()) {
1871-
//Inititalize zPIVWallet
1872-
uiInterface.InitMessage(_("Syncing zPIV wallet..."));
1873-
1874-
//Load zerocoin mint hashes to memory
1875-
pwalletMain->zpivTracker->Init();
1876-
zwalletMain->LoadMintPoolFromDB();
1877-
zwalletMain->SyncWithChain();
1878-
}
1707+
std::string warningString;
1708+
std::string errorString;
1709+
pwalletMain = CWallet::InitLoadWallet(fDisableWallet, strWalletFile, warningString, errorString, zwalletMain);
1710+
if (!pwalletMain)
1711+
return false;
1712+
if (!warningString.empty())
1713+
UIWarning(warningString);
1714+
if (!errorString.empty())
1715+
return UIError(errorString);
18791716
} // (!fDisableWallet)
18801717
#else // ENABLE_WALLET
18811718
LogPrintf("No wallet compiled in!\n");

src/wallet/wallet.cpp

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3816,6 +3816,184 @@ std::string CWallet::GetWalletHelpString(bool showDebug)
38163816
return strUsage;
38173817
}
38183818

3819+
CWallet* CWallet::InitLoadWallet(bool fDisableWallet, const std::string& strWalletFile, std::string& warningString, std::string& errorString, CzPIVWallet* zwallet)
3820+
{
3821+
// needed to restore wallet transaction meta data after -zapwallettxes
3822+
std::vector<CWalletTx> vWtx;
3823+
3824+
if (GetBoolArg("-zapwallettxes", false)) {
3825+
uiInterface.InitMessage(_("Zapping all transactions from wallet..."));
3826+
3827+
CWallet *tempWallet = new CWallet(strWalletFile);
3828+
DBErrors nZapWalletRet = pwalletMain->ZapWalletTx(vWtx);
3829+
if (nZapWalletRet != DB_LOAD_OK) {
3830+
uiInterface.InitMessage(_("Error loading wallet.dat: Wallet corrupted"));
3831+
return nullptr;
3832+
}
3833+
3834+
delete tempWallet;
3835+
tempWallet = nullptr;
3836+
}
3837+
3838+
uiInterface.InitMessage(_("Loading wallet..."));
3839+
fVerifyingBlocks = true;
3840+
3841+
int64_t nStart = GetTimeMillis();
3842+
bool fFirstRun = true;
3843+
CWallet *walletInstance = new CWallet(strWalletFile);
3844+
DBErrors nLoadWalletRet = walletInstance->LoadWallet(fFirstRun);
3845+
if (nLoadWalletRet != DB_LOAD_OK) {
3846+
if (nLoadWalletRet == DB_CORRUPT)
3847+
errorString += _("Error loading wallet.dat: Wallet corrupted\n");
3848+
else if (nLoadWalletRet == DB_NONCRITICAL_ERROR) {
3849+
warningString += _("Warning: error reading wallet.dat! All keys read correctly, but transaction data"
3850+
" or address book entries might be missing or incorrect.\n");
3851+
} else if (nLoadWalletRet == DB_TOO_NEW)
3852+
errorString += _("Error loading wallet.dat: Wallet requires newer version of PIVX Core\n");
3853+
else if (nLoadWalletRet == DB_NEED_REWRITE) {
3854+
errorString += _("Wallet needed to be rewritten: restart PIVX Core to complete\n");
3855+
LogPrintf("%s", errorString);
3856+
return walletInstance;
3857+
} else
3858+
errorString += _("Error loading wallet.dat\n");
3859+
}
3860+
3861+
// check minimum stake split threshold
3862+
if (walletInstance->nStakeSplitThreshold && walletInstance->nStakeSplitThreshold < CWallet::minStakeSplitThreshold) {
3863+
LogPrintf("WARNING: stake split threshold value %s too low. Restoring to minimum value %s.\n",
3864+
FormatMoney(pwalletMain->nStakeSplitThreshold), FormatMoney(CWallet::minStakeSplitThreshold));
3865+
walletInstance->nStakeSplitThreshold = CWallet::minStakeSplitThreshold;
3866+
}
3867+
3868+
int prev_version = walletInstance->GetVersion();
3869+
3870+
// Forced upgrade
3871+
const bool fLegacyWallet = GetBoolArg("-legacywallet", false);
3872+
if (GetBoolArg("-upgradewallet", fFirstRun && !fLegacyWallet)) {
3873+
if (prev_version <= FEATURE_PRE_PIVX && walletInstance->IsLocked()) {
3874+
// Cannot upgrade a locked wallet
3875+
errorString += "Cannot upgrade a locked wallet.\n";
3876+
LogPrintf("%s", errorString);
3877+
return walletInstance;
3878+
}
3879+
3880+
int nMaxVersion = GetArg("-upgradewallet", 0);
3881+
if (nMaxVersion == 0) // the -upgradewallet without argument case
3882+
{
3883+
LogPrintf("Performing wallet upgrade to %i\n", FEATURE_LATEST);
3884+
nMaxVersion = FEATURE_LATEST;
3885+
walletInstance->SetMinVersion(FEATURE_LATEST); // permanently upgrade the wallet immediately
3886+
} else
3887+
LogPrintf("Allowing wallet upgrade up to %i\n", nMaxVersion);
3888+
if (nMaxVersion < walletInstance->GetVersion())
3889+
errorString += _("Cannot downgrade wallet\n");
3890+
walletInstance->SetMaxVersion(nMaxVersion);
3891+
}
3892+
3893+
// Upgrade to HD only if explicit upgrade was requested
3894+
if (GetBoolArg("-upgradewallet", false)) {
3895+
std::string upgradeError;
3896+
if (!walletInstance->Upgrade(upgradeError, prev_version)) {
3897+
errorString += (upgradeError + "\n");
3898+
}
3899+
}
3900+
3901+
if (fFirstRun) {
3902+
if (!fLegacyWallet) {
3903+
// Create new HD Wallet
3904+
LogPrintf("Creating HD Wallet\n");
3905+
// Ensure this wallet.dat can only be opened by clients supporting HD.
3906+
walletInstance->SetMinVersion(FEATURE_LATEST);
3907+
walletInstance->SetupSPKM();
3908+
} else {
3909+
if (!Params().IsRegTestNet()) {
3910+
errorString += "Legacy wallets can only be created on RegTest.\n";
3911+
LogPrintf("%s", errorString);
3912+
return walletInstance;
3913+
}
3914+
// Create legacy wallet
3915+
LogPrintf("Creating Pre-HD Wallet\n");
3916+
walletInstance->SetMaxVersion(FEATURE_PRE_PIVX);
3917+
}
3918+
3919+
// Top up the keypool
3920+
if (!walletInstance->TopUpKeyPool()) {
3921+
// Error generating keys
3922+
errorString += _("Unable to generate initial key\n");
3923+
return walletInstance;
3924+
}
3925+
3926+
walletInstance->SetBestChain(chainActive.GetLocator());
3927+
}
3928+
3929+
LogPrintf("Init errors: %s\n", errorString);
3930+
LogPrintf("Wallet completed loading in %15dms\n", GetTimeMillis() - nStart);
3931+
3932+
zwallet = new CzPIVWallet(walletInstance);
3933+
walletInstance->setZWallet(zwallet);
3934+
3935+
RegisterValidationInterface(walletInstance);
3936+
3937+
CBlockIndex* pindexRescan = chainActive.Tip();
3938+
if (GetBoolArg("-rescan", false))
3939+
pindexRescan = chainActive.Genesis();
3940+
else {
3941+
CWalletDB walletdb(strWalletFile);
3942+
CBlockLocator locator;
3943+
if (walletdb.ReadBestBlock(locator))
3944+
pindexRescan = FindForkInGlobalIndex(chainActive, locator);
3945+
else
3946+
pindexRescan = chainActive.Genesis();
3947+
}
3948+
if (chainActive.Tip() && chainActive.Tip() != pindexRescan) {
3949+
uiInterface.InitMessage(_("Rescanning..."));
3950+
LogPrintf("Rescanning last %i blocks (from block %i)...\n", chainActive.Height() - pindexRescan->nHeight, pindexRescan->nHeight);
3951+
const int64_t nWalletRescanTime = GetTimeMillis();
3952+
if (walletInstance->ScanForWalletTransactions(pindexRescan, true, true) == -1) {
3953+
errorString += _("Shutdown requested over the txs scan. Exiting.\n");
3954+
return walletInstance;
3955+
}
3956+
LogPrintf("Rescan completed in %15dms\n", GetTimeMillis() - nWalletRescanTime);
3957+
walletInstance->SetBestChain(chainActive.GetLocator());
3958+
CWalletDB::IncrementUpdateCounter();
3959+
3960+
// Restore wallet transaction metadata after -zapwallettxes=1
3961+
if (GetBoolArg("-zapwallettxes", false) && GetArg("-zapwallettxes", "1") != "2") {
3962+
CWalletDB walletdb(strWalletFile);
3963+
for (const CWalletTx& wtxOld : vWtx) {
3964+
uint256 hash = wtxOld.GetHash();
3965+
std::map<uint256, CWalletTx>::iterator mi = walletInstance->mapWallet.find(hash);
3966+
if (mi != walletInstance->mapWallet.end()) {
3967+
const CWalletTx* copyFrom = &wtxOld;
3968+
CWalletTx* copyTo = &mi->second;
3969+
copyTo->mapValue = copyFrom->mapValue;
3970+
copyTo->vOrderForm = copyFrom->vOrderForm;
3971+
copyTo->nTimeReceived = copyFrom->nTimeReceived;
3972+
copyTo->nTimeSmart = copyFrom->nTimeSmart;
3973+
copyTo->fFromMe = copyFrom->fFromMe;
3974+
copyTo->strFromAccount = copyFrom->strFromAccount;
3975+
copyTo->nOrderPos = copyFrom->nOrderPos;
3976+
walletdb.WriteTx(*copyTo);
3977+
}
3978+
}
3979+
}
3980+
}
3981+
fVerifyingBlocks = false;
3982+
3983+
if (!zwallet->GetMasterSeed().IsNull()) {
3984+
//Inititalize zPIVWallet
3985+
uiInterface.InitMessage(_("Syncing zPIV wallet..."));
3986+
3987+
//Load zerocoin mint hashes to memory
3988+
walletInstance->zpivTracker->Init();
3989+
zwallet->LoadMintPoolFromDB();
3990+
zwallet->SyncWithChain();
3991+
}
3992+
3993+
return walletInstance;
3994+
}
3995+
3996+
38193997
CKeyPool::CKeyPool()
38203998
{
38213999
nTime = GetTime();

src/wallet/wallet.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,10 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
647647
/* Returns the wallets help message */
648648
static std::string GetWalletHelpString(bool showDebug);
649649

650+
/* initializes the wallet, returns a new CWallet instance or a null pointer in case of an error */
651+
static CWallet* InitLoadWallet(bool fDisableWallet, const std::string& strWalletFile, std::string& warningString, std::string& errorString, CzPIVWallet* zwallet);
652+
653+
650654
/**
651655
* Address book entry changed.
652656
* @note called with lock cs_wallet held.

0 commit comments

Comments
 (0)