@@ -1166,7 +1166,7 @@ void CWallet::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const
1166
1166
TransactionRemovedFromMempool (pblock->vtx [i]);
1167
1167
}
1168
1168
1169
- m_last_block_processed = pindex;
1169
+ m_last_block_processed = pindex-> GetBlockHash () ;
1170
1170
}
1171
1171
1172
1172
void CWallet::BlockDisconnected (const std::shared_ptr<const CBlock>& pblock) {
@@ -1191,9 +1191,8 @@ void CWallet::BlockUntilSyncedToCurrentChain() {
1191
1191
// protected by cs_wallet instead of cs_main, but as long as we need
1192
1192
// cs_main here anyway, it's easier to just call it cs_main-protected.
1193
1193
auto locked_chain = chain ().lock ();
1194
- const CBlockIndex* initialChainTip = chainActive.Tip ();
1195
1194
1196
- if (m_last_block_processed && m_last_block_processed-> GetAncestor (initialChainTip-> nHeight ) == initialChainTip ) {
1195
+ if (! m_last_block_processed. IsNull () && locked_chain-> isPotentialTip (m_last_block_processed) ) {
1197
1196
return ;
1198
1197
}
1199
1198
}
@@ -4074,7 +4073,7 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
4074
4073
}
4075
4074
4076
4075
auto locked_chain = chain.assumeLocked (); // Temporary. Removed in upcoming lock cleanup
4077
- walletInstance->ChainStateFlushed (chainActive. GetLocator ());
4076
+ walletInstance->ChainStateFlushed (locked_chain-> getLocator ());
4078
4077
} else if (wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS) {
4079
4078
// Make it impossible to disable private keys after creation
4080
4079
InitError (strprintf (_ (" Error loading %s: Private keys can only be disabled during creation" ), walletFile));
@@ -4161,57 +4160,67 @@ std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain,
4161
4160
// Try to top up keypool. No-op if the wallet is locked.
4162
4161
walletInstance->TopUpKeyPool ();
4163
4162
4164
- LockAnnotation lock (::cs_main); // Temporary, for FindForkInGlobalIndex below. Removed in upcoming commit.
4165
4163
auto locked_chain = chain.lock ();
4166
4164
LOCK (walletInstance->cs_wallet );
4167
4165
4168
- CBlockIndex *pindexRescan = chainActive. Genesis () ;
4166
+ int rescan_height = 0 ;
4169
4167
if (!gArgs .GetBoolArg (" -rescan" , false ))
4170
4168
{
4171
4169
WalletBatch batch (*walletInstance->database );
4172
4170
CBlockLocator locator;
4173
- if (batch.ReadBestBlock (locator))
4174
- pindexRescan = FindForkInGlobalIndex (chainActive, locator);
4171
+ if (batch.ReadBestBlock (locator)) {
4172
+ if (const Optional<int > fork_height = locked_chain->findLocatorFork (locator)) {
4173
+ rescan_height = *fork_height;
4174
+ }
4175
+ }
4175
4176
}
4176
4177
4177
- walletInstance->m_last_block_processed = chainActive.Tip ();
4178
+ const Optional<int > tip_height = locked_chain->getHeight ();
4179
+ if (tip_height) {
4180
+ walletInstance->m_last_block_processed = locked_chain->getBlockHash (*tip_height);
4181
+ } else {
4182
+ walletInstance->m_last_block_processed .SetNull ();
4183
+ }
4178
4184
4179
- if (chainActive. Tip () && chainActive. Tip () != pindexRescan )
4185
+ if (tip_height && *tip_height != rescan_height )
4180
4186
{
4181
4187
// We can't rescan beyond non-pruned blocks, stop and throw an error
4182
4188
// this might happen if a user uses an old wallet within a pruned node
4183
4189
// or if he ran -disablewallet for a longer time, then decided to re-enable
4184
4190
if (fPruneMode )
4185
4191
{
4186
- CBlockIndex *block = chainActive.Tip ();
4187
- while (block && block->pprev && (block->pprev ->nStatus & BLOCK_HAVE_DATA) && block->pprev ->nTx > 0 && pindexRescan != block)
4188
- block = block->pprev ;
4192
+ int block_height = *tip_height;
4193
+ while (block_height > 0 && locked_chain->haveBlockOnDisk (block_height - 1 ) && rescan_height != block_height) {
4194
+ --block_height;
4195
+ }
4189
4196
4190
- if (pindexRescan != block ) {
4197
+ if (rescan_height != block_height ) {
4191
4198
InitError (_ (" Prune: last wallet synchronisation goes beyond pruned data. You need to -reindex (download the whole blockchain again in case of pruned node)" ));
4192
4199
return nullptr ;
4193
4200
}
4194
4201
}
4195
4202
4196
4203
uiInterface.InitMessage (_ (" Rescanning..." ));
4197
- walletInstance->WalletLogPrintf (" Rescanning last %i blocks (from block %i)...\n " , chainActive. Height () - pindexRescan-> nHeight , pindexRescan-> nHeight );
4204
+ walletInstance->WalletLogPrintf (" Rescanning last %i blocks (from block %i)...\n " , *tip_height - rescan_height, rescan_height );
4198
4205
4199
4206
// No need to read and scan block if block was created before
4200
4207
// our wallet birthday (as adjusted for block time variability)
4201
- while (pindexRescan && walletInstance->nTimeFirstKey && (pindexRescan->GetBlockTime () < (walletInstance->nTimeFirstKey - TIMESTAMP_WINDOW))) {
4202
- pindexRescan = chainActive.Next (pindexRescan);
4208
+ if (walletInstance->nTimeFirstKey ) {
4209
+ if (Optional<int > first_block = locked_chain->findFirstBlockWithTimeAndHeight (walletInstance->nTimeFirstKey - TIMESTAMP_WINDOW, rescan_height)) {
4210
+ rescan_height = *first_block;
4211
+ }
4203
4212
}
4204
4213
4205
4214
nStart = GetTimeMillis ();
4206
4215
{
4207
4216
WalletRescanReserver reserver (walletInstance.get ());
4208
- if (!reserver.reserve () || (ScanResult::SUCCESS != walletInstance->ScanForWalletTransactions (pindexRescan-> GetBlockHash ( ), {} /* stop block */ , reserver, true /* update */ ).status )) {
4217
+ if (!reserver.reserve () || (ScanResult::SUCCESS != walletInstance->ScanForWalletTransactions (locked_chain-> getBlockHash (rescan_height ), {} /* stop block */ , reserver, true /* update */ ).status )) {
4209
4218
InitError (_ (" Failed to rescan the wallet during initialization" ));
4210
4219
return nullptr ;
4211
4220
}
4212
4221
}
4213
4222
walletInstance->WalletLogPrintf (" Rescan completed in %15dms\n " , GetTimeMillis () - nStart);
4214
- walletInstance->ChainStateFlushed (chainActive. GetLocator ());
4223
+ walletInstance->ChainStateFlushed (locked_chain-> getLocator ());
4215
4224
walletInstance->database ->IncrementUpdateCounter ();
4216
4225
4217
4226
// Restore wallet transaction metadata after -zapwallettxes=1
0 commit comments