Merge bitcoin/bitcoin#32987: init: [gui] Avoid UB/crash in InitAndLoadChainstate

fac90e5261 test: Check that the GUI interactive reindex works (MarcoFalke)
faaaddaaf8 init: [gui] Avoid UB/crash in InitAndLoadChainstate (MarcoFalke)

Pull request description:

  `InitAndLoadChainstate` is problematic, when called twice in the GUI. This can happen when it returns a failure and the user selects an interactive reindex.

  There are several bugs that have been introduced since the last time this was working correctly:

  * The first one is a crash (assertion failure), which happens due to a cached tip block in the notifiications from the previous run. See https://github.com/bitcoin/bitcoin/pull/31346#discussion_r2207914726
  * The second one is UB (use-after-free), which happens because the block index db in the blockmanager is not reset. See https://github.com/bitcoin/bitcoin/pull/30965#discussion_r2207822121

  Fix both bugs by resetting any dirty state in `InitAndLoadChainstate`.

  Also, add a test, because I don't really want to keep testing this manually every time. (A failing test run can be seen in https://github.com/bitcoin/bitcoin/pull/32979/checks)

ACKs for top commit:
  achow101:
    ACK fac90e5261
  TheCharlatan:
    ACK fac90e5261
  mzumsande:
    Tested ACK fac90e5261

Tree-SHA512: 9f744d36e7cdd3f5871764386ec5a5cca1ae144f1bacc26c07e60313c2bdacdc5fca351aa185cb51359540eea4534dda17e4fb6073ad90f91ba0a6936faeead8
This commit is contained in:
Ava Chow
2025-07-30 13:55:01 -07:00
4 changed files with 52 additions and 6 deletions

View File

@@ -0,0 +1,34 @@
#!/usr/bin/env python3
# Copyright (c) The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test reindex works on init after a db load failure"""
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal
import os
import shutil
class ReindexInitTest(BitcoinTestFramework):
def set_test_params(self):
self.num_nodes = 1
def run_test(self):
node = self.nodes[0]
self.stop_nodes()
self.log.info("Removing the block index leads to init error")
shutil.rmtree(node.blocks_path / "index")
node.assert_start_raises_init_error(
expected_msg=f": Error initializing block database.{os.linesep}"
"Please restart with -reindex or -reindex-chainstate to recover.",
)
self.log.info("Allowing the reindex should work fine")
self.start_node(0, extra_args=["-test=reindex_after_failure_noninteractive_yes"])
assert_equal(node.getblockcount(), 200)
if __name__ == "__main__":
ReindexInitTest(__file__).main()

View File

@@ -289,6 +289,7 @@ BASE_SCRIPTS = [
'p2p_leak.py',
'wallet_encryption.py',
'feature_dersig.py',
'feature_reindex_init.py',
'feature_cltv.py',
'rpc_uptime.py',
'feature_discover.py',