@@ -1127,7 +1127,7 @@ void CWallet::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const
11271127 TransactionRemovedFromMempool (pblock->vtx [i]);
11281128 }
11291129
1130- m_last_block_processed = pindex;
1130+ m_last_block_processed = pindex-> GetBlockHash () ;
11311131}
11321132
11331133void CWallet::BlockDisconnected (const std::shared_ptr<const CBlock>& pblock) {
@@ -1152,9 +1152,8 @@ void CWallet::BlockUntilSyncedToCurrentChain() {
11521152 // protected by cs_wallet instead of cs_main, but as long as we need
11531153 // cs_main here anyway, it's easier to just call it cs_main-protected.
11541154 auto locked_chain = chain ().lock ();
1155- const CBlockIndex* initialChainTip = chainActive.Tip ();
11561155
1157- if (m_last_block_processed && m_last_block_processed-> GetAncestor (initialChainTip-> nHeight ) == initialChainTip ) {
1156+ if (! m_last_block_processed. IsNull () && locked_chain-> isPotentialTip (m_last_block_processed) ) {
11581157 return ;
11591158 }
11601159 }
@@ -4041,7 +4040,7 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
40414040 }
40424041
40434042 auto locked_chain = chain.assumeLocked (); // Temporary. Removed in upcoming lock cleanup
4044- walletInstance->ChainStateFlushed (chainActive. GetLocator ());
4043+ walletInstance->ChainStateFlushed (locked_chain-> getLocator ());
40454044 } else if (wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS) {
40464045 // Make it impossible to disable private keys after creation
40474046 InitError (strprintf (_ (" Error loading %s: Private keys can only be disabled during creation" ), walletFile));
@@ -4128,58 +4127,68 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
41284127 // Try to top up keypool. No-op if the wallet is locked.
41294128 walletInstance->TopUpKeyPool ();
41304129
4131- LockAnnotation lock (::cs_main); // Temporary, for FindForkInGlobalIndex below. Removed in upcoming commit.
41324130 auto locked_chain = chain.lock ();
41334131 LOCK (walletInstance->cs_wallet );
41344132
4135- CBlockIndex *pindexRescan = chainActive. Genesis () ;
4133+ int rescan_height = 0 ;
41364134 if (!gArgs .GetBoolArg (" -rescan" , false ))
41374135 {
41384136 WalletBatch batch (*walletInstance->database );
41394137 CBlockLocator locator;
4140- if (batch.ReadBestBlock (locator))
4141- pindexRescan = FindForkInGlobalIndex (chainActive, locator);
4138+ if (batch.ReadBestBlock (locator)) {
4139+ if (const Optional<int > fork_height = locked_chain->findLocatorFork (locator)) {
4140+ rescan_height = *fork_height;
4141+ }
4142+ }
41424143 }
41434144
4144- walletInstance->m_last_block_processed = chainActive.Tip ();
4145+ const Optional<int > tip_height = locked_chain->getHeight ();
4146+ if (tip_height) {
4147+ walletInstance->m_last_block_processed = locked_chain->getBlockHash (*tip_height);
4148+ } else {
4149+ walletInstance->m_last_block_processed .SetNull ();
4150+ }
41454151
4146- if (chainActive. Tip () && chainActive. Tip () != pindexRescan )
4152+ if (tip_height && *tip_height != rescan_height )
41474153 {
41484154 // We can't rescan beyond non-pruned blocks, stop and throw an error
41494155 // this might happen if a user uses an old wallet within a pruned node
41504156 // or if he ran -disablewallet for a longer time, then decided to re-enable
41514157 if (fPruneMode )
41524158 {
4153- CBlockIndex *block = chainActive.Tip ();
4154- while (block && block->pprev && (block->pprev ->nStatus & BLOCK_HAVE_DATA) && block->pprev ->nTx > 0 && pindexRescan != block)
4155- block = block->pprev ;
4159+ int block_height = *tip_height;
4160+ while (block_height > 0 && locked_chain->haveBlockOnDisk (block_height - 1 ) && rescan_height != block_height) {
4161+ --block_height;
4162+ }
41564163
4157- if (pindexRescan != block ) {
4164+ if (rescan_height != block_height ) {
41584165 InitError (_ (" Prune: last wallet synchronisation goes beyond pruned data. You need to -reindex (download the whole blockchain again in case of pruned node)" ));
41594166 return nullptr ;
41604167 }
41614168 }
41624169
41634170 uiInterface.InitMessage (_ (" Rescanning..." ));
4164- walletInstance->WalletLogPrintf (" Rescanning last %i blocks (from block %i)...\n " , chainActive. Height () - pindexRescan-> nHeight , pindexRescan-> nHeight );
4171+ walletInstance->WalletLogPrintf (" Rescanning last %i blocks (from block %i)...\n " , *tip_height - rescan_height, rescan_height );
41654172
41664173 // No need to read and scan block if block was created before
41674174 // our wallet birthday (as adjusted for block time variability)
4168- while (pindexRescan && walletInstance->nTimeFirstKey && (pindexRescan->GetBlockTime () < (walletInstance->nTimeFirstKey - TIMESTAMP_WINDOW))) {
4169- pindexRescan = chainActive.Next (pindexRescan);
4175+ if (walletInstance->nTimeFirstKey ) {
4176+ if (Optional<int > first_block = locked_chain->findFirstBlockWithTimeAndHeight (walletInstance->nTimeFirstKey - TIMESTAMP_WINDOW, rescan_height)) {
4177+ rescan_height = *first_block;
4178+ }
41704179 }
41714180
41724181 nStart = GetTimeMillis ();
41734182 {
41744183 WalletRescanReserver reserver (walletInstance.get ());
41754184 uint256 stop_block, failed_block;
4176- if (!reserver.reserve () || (ScanResult::SUCCESS != walletInstance->ScanForWalletTransactions (pindexRescan-> GetBlockHash ( ), {} /* stop block */ , reserver, failed_block, stop_block, true /* update */ ))) {
4185+ if (!reserver.reserve () || (ScanResult::SUCCESS != walletInstance->ScanForWalletTransactions (locked_chain-> getBlockHash (rescan_height ), {} /* stop block */ , reserver, failed_block, stop_block, true /* update */ ))) {
41774186 InitError (_ (" Failed to rescan the wallet during initialization" ));
41784187 return nullptr ;
41794188 }
41804189 }
41814190 walletInstance->WalletLogPrintf (" Rescan completed in %15dms\n " , GetTimeMillis () - nStart);
4182- walletInstance->ChainStateFlushed (chainActive. GetLocator ());
4191+ walletInstance->ChainStateFlushed (locked_chain-> getLocator ());
41834192 walletInstance->database ->IncrementUpdateCounter ();
41844193
41854194 // Restore wallet transaction metadata after -zapwallettxes=1
0 commit comments