@@ -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 " );
0 commit comments