@@ -523,6 +523,11 @@ bool CWallet::ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase,
523
523
524
524
void CWallet::chainStateFlushed (const CBlockLocator& loc)
525
525
{
526
+ // Don't update the best block until the chain is attached so that in case of a shutdown,
527
+ // the rescan will be restarted at next startup.
528
+ if (m_attaching_chain) {
529
+ return ;
530
+ }
526
531
WalletBatch batch (GetDatabase ());
527
532
batch.WriteBestBlock (loc);
528
533
}
@@ -2940,6 +2945,8 @@ bool CWallet::AttachChain(const std::shared_ptr<CWallet>& walletInstance, interf
2940
2945
// be pending on the validation-side until lock release. It's likely to have
2941
2946
// block processing duplicata (if rescan block range overlaps with notification one)
2942
2947
// but we guarantee at least than wallet state is correct after notifications delivery.
2948
+ // However, chainStateFlushed notifications are ignored until the rescan is finished
2949
+ // so that in case of a shutdown event, the rescan will be repeated at the next start.
2943
2950
// This is temporary until rescan and notifications delivery are unified under same
2944
2951
// interface.
2945
2952
walletInstance->m_chain_notifications_handler = walletInstance->chain ().handleNotifications (walletInstance);
@@ -2968,6 +2975,7 @@ bool CWallet::AttachChain(const std::shared_ptr<CWallet>& walletInstance, interf
2968
2975
2969
2976
if (tip_height && *tip_height != rescan_height)
2970
2977
{
2978
+ walletInstance->m_attaching_chain = true ; // ignores chainStateFlushed notifications
2971
2979
if (chain.havePruned ()) {
2972
2980
int block_height = *tip_height;
2973
2981
while (block_height > 0 && chain.haveBlockOnDisk (block_height - 1 ) && rescan_height != block_height) {
@@ -3007,6 +3015,7 @@ bool CWallet::AttachChain(const std::shared_ptr<CWallet>& walletInstance, interf
3007
3015
return false ;
3008
3016
}
3009
3017
}
3018
+ walletInstance->m_attaching_chain = false ;
3010
3019
walletInstance->chainStateFlushed (chain.getTipLocator ());
3011
3020
walletInstance->GetDatabase ().IncrementUpdateCounter ();
3012
3021
}
0 commit comments