Potential crash (assert) rescanning wallet #31474

issue psgreco openend this issue on December 12, 2024
  1. psgreco commented at 0:17 am on December 12, 2024: contributor

    Is there an existing issue for this?

    • I have searched the existing issues

    Current behaviour

    Preface: This whole analysis was made using Elements Core, not Bitcoin Core, but the code and the conditions should line up in a way that makes this likely in Bitcoin too.

    There’s an assert crash in assert(chain_depth >= 0); // coinbase tx should not be conflicted calling CWallet::GetTxBlocksToMaturity because CWallet::GetTxDepthInMainChain returns a negative value for TxStateConfirmed txs. The reason for this is that m_last_block_processed is set at the beginning of AttachChain, but the rescan loop keeps going beyond that point, so when GetTxDepthInMainChain is called right after the scan is done, m_last_block_processed is lower than the actual block in the latest txs, hence returning a negative value and crashing.

    In discussions with @achow101 , it’s possible that #30221 fixes this issue by calling SetBestBlock after the rescan is done. Another approach could be to stop the scan when m_last_block_processed is reached with something like this

     0@@ -1689,6 +1693,11 @@ CWallet::ScanResult CWallet::ScanForWalletTransactions(const uint256& start_bloc
     1         if (max_height && block_height >= *max_height) {
     2             break;
     3         }
     4+        if (block_height >= GetLastBlockHeight()) {
     5+            WalletLogPrintf("Stopping scan. At block %d. Progress=%f\n", block_height, progress_current);
     6+            break;
     7+        }
     8+
     9         {
    10             if (!next_block) {
    11                 // break successfully when rescan has reached the tip, or
    

    Expected behaviour

    The wallet data should be consistent to avoid the crash

    Steps to reproduce

    This is reproduced using QT

    1. Create a chain with many coinbase txs.
    2. Attach a wallet (could be read only) with the address of the coinbase txs
    3. While the wallet is still being loaded, create many more blocks with coinbase txs to that address
    4. Assert happens when the wallet is done loading

    Relevant log output

    No response

    How did you obtain Bitcoin Core

    Compiled from source

    What version of Bitcoin Core are you using?

    master@62bd61de110b057cbfd6e31e4d0b727d93119c72

    Operating system and version

    Fedora 41

    Machine specifications

    No response


psgreco


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-12-21 12:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me