From 2052e3aa9aa666bdc86dac370f1dd8fb978d3497 Mon Sep 17 00:00:00 2001 From: Martin Zumsande Date: Mon, 25 Apr 2022 22:38:59 +0200 Subject: wallet: ignore chainStateFlushed notifications while attaching chain --- src/wallet/wallet.cpp | 9 +++++++++ src/wallet/wallet.h | 1 + 2 files changed, 10 insertions(+) (limited to 'src/wallet') diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 0d7075810d..34247d14e3 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -523,6 +523,11 @@ bool CWallet::ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, void CWallet::chainStateFlushed(const CBlockLocator& loc) { + // Don't update the best block until the chain is attached so that in case of a shutdown, + // the rescan will be restarted at next startup. + if (m_attaching_chain) { + return; + } WalletBatch batch(GetDatabase()); batch.WriteBestBlock(loc); } @@ -2940,6 +2945,8 @@ bool CWallet::AttachChain(const std::shared_ptr& walletInstance, interf // be pending on the validation-side until lock release. It's likely to have // block processing duplicata (if rescan block range overlaps with notification one) // but we guarantee at least than wallet state is correct after notifications delivery. + // However, chainStateFlushed notifications are ignored until the rescan is finished + // so that in case of a shutdown event, the rescan will be repeated at the next start. // This is temporary until rescan and notifications delivery are unified under same // interface. walletInstance->m_chain_notifications_handler = walletInstance->chain().handleNotifications(walletInstance); @@ -2968,6 +2975,7 @@ bool CWallet::AttachChain(const std::shared_ptr& walletInstance, interf if (tip_height && *tip_height != rescan_height) { + walletInstance->m_attaching_chain = true; //ignores chainStateFlushed notifications if (chain.havePruned()) { int block_height = *tip_height; while (block_height > 0 && chain.haveBlockOnDisk(block_height - 1) && rescan_height != block_height) { @@ -3007,6 +3015,7 @@ bool CWallet::AttachChain(const std::shared_ptr& walletInstance, interf return false; } } + walletInstance->m_attaching_chain = false; walletInstance->chainStateFlushed(chain.getTipLocator()); walletInstance->GetDatabase().IncrementUpdateCounter(); } diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 26b7f97b5f..80b6845ae6 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -237,6 +237,7 @@ private: std::atomic fAbortRescan{false}; std::atomic fScanningWallet{false}; // controlled by WalletRescanReserver + std::atomic m_attaching_chain{false}; std::atomic m_scanning_start{0}; std::atomic m_scanning_progress{0}; friend class WalletRescanReserver; -- cgit v1.2.3