@@ -2007,10 +2007,7 @@ DBErrors CWallet::LoadWallet()
20072007 assert (m_internal_spk_managers.empty ());
20082008 }
20092009
2010- if (nLoadWalletRet != DBErrors::LOAD_OK)
2011- return nLoadWalletRet;
2012-
2013- return DBErrors::LOAD_OK;
2010+ return nLoadWalletRet;
20142011}
20152012
20162013DBErrors CWallet::ZapSelectTx (std::vector<uint256>& vHashIn, std::vector<uint256>& vHashOut)
@@ -2542,6 +2539,7 @@ std::shared_ptr<CWallet> CWallet::Create(WalletContext& context, const std::stri
25422539 // TODO: Can't use std::make_shared because we need a custom deleter but
25432540 // should be possible to use std::allocate_shared.
25442541 std::shared_ptr<CWallet> walletInstance (new CWallet (chain, name, std::move (database)), ReleaseWallet);
2542+ bool rescan_required = false ;
25452543 DBErrors nLoadWalletRet = walletInstance->LoadWallet ();
25462544 if (nLoadWalletRet != DBErrors::LOAD_OK) {
25472545 if (nLoadWalletRet == DBErrors::CORRUPT) {
@@ -2562,6 +2560,10 @@ std::shared_ptr<CWallet> CWallet::Create(WalletContext& context, const std::stri
25622560 {
25632561 error = strprintf (_ (" Wallet needed to be rewritten: restart %s to complete" ), PACKAGE_NAME);
25642562 return nullptr ;
2563+ } else if (nLoadWalletRet == DBErrors::RESCAN_REQUIRED) {
2564+ warnings.push_back (strprintf (_ (" Error reading %s! Transaction data may be missing or incorrect."
2565+ " Rescanning wallet." ), walletFile));
2566+ rescan_required = true ;
25652567 }
25662568 else {
25672569 error = strprintf (_ (" Error loading %s" ), walletFile);
@@ -2753,7 +2755,7 @@ std::shared_ptr<CWallet> CWallet::Create(WalletContext& context, const std::stri
27532755
27542756 LOCK (walletInstance->cs_wallet );
27552757
2756- if (chain && !AttachChain (walletInstance, *chain, error, warnings)) {
2758+ if (chain && !AttachChain (walletInstance, *chain, rescan_required, error, warnings)) {
27572759 return nullptr ;
27582760 }
27592761
@@ -2775,7 +2777,7 @@ std::shared_ptr<CWallet> CWallet::Create(WalletContext& context, const std::stri
27752777 return walletInstance;
27762778}
27772779
2778- bool CWallet::AttachChain (const std::shared_ptr<CWallet>& walletInstance, interfaces::Chain& chain, bilingual_str& error, std::vector<bilingual_str>& warnings)
2780+ bool CWallet::AttachChain (const std::shared_ptr<CWallet>& walletInstance, interfaces::Chain& chain, const bool rescan_required, bilingual_str& error, std::vector<bilingual_str>& warnings)
27792781{
27802782 LOCK (walletInstance->cs_wallet );
27812783 // allow setting the chain if it hasn't been set already but prevent changing it
@@ -2792,8 +2794,9 @@ bool CWallet::AttachChain(const std::shared_ptr<CWallet>& walletInstance, interf
27922794 // interface.
27932795 walletInstance->m_chain_notifications_handler = walletInstance->chain ().handleNotifications (walletInstance);
27942796
2797+ // If either rescan_required = true or -rescan is set, rescan_height remains equal to 0
27952798 int rescan_height = 0 ;
2796- if (!gArgs .GetBoolArg (" -rescan" , false ))
2799+ if (!rescan_required && ! gArgs .GetBoolArg (" -rescan" , false ))
27972800 {
27982801 WalletBatch batch (walletInstance->GetDatabase ());
27992802 CBlockLocator locator;
0 commit comments