test: add feature_dbcrash_replay.py #36837

pull ViniciusCestarii wants to merge 1 commits into bitcoin:master from ViniciusCestarii:test-dbcrash-replay changing 2 files +70 −0
  1. ViniciusCestarii commented at 2:49 PM on November 11, 2026: contributor

    Add feature_dbcrash_replay.py, a deterministic functional test for Chainstate::ReplayBlocks.

    ReplayBlocks is currently only exercised by feature_dbcrash.py, which relies on random crash points and reorgs, takes several minutes and is in EXTENDED_SCRIPTS, so it never runs on PRs.

    feature_dbcrash_replay.py finish in ~1s and deterministically kills at least these 2 mutants:

    diff --git a/src/validation.cpp b/src/validation.cpp
    index 6bed39ba5f..f4a0094df9 100644
    --- a/src/validation.cpp
    +++ b/src/validation.cpp
    @@ -4903,7 +4903,7 @@ bool Chainstate::ReplayBlocks()
         const int nForkHeight{pindexFork ? pindexFork->nHeight : 0};
         if (pindexOld != pindexFork) {
             LogInfo("Rolling back from %s (%i to %i)", pindexOld->GetBlockHash().ToString(), pindexOld->nHeight, nForkHeight);
    -        while (pindexOld != pindexFork) {
    +        while (pindexOld->pprev != pindexFork) {
                 if (pindexOld->nHeight > 0) { // Never disconnect the genesis block.
                     CBlock block;
                     if (!m_blockman.ReadBlock(block, *pindexOld)) {
    
    diff --git a/src/validation.cpp b/src/validation.cpp
    index 6bed39b..e006c9a 100644
    --- a/src/validation.cpp
    +++ b/src/validation.cpp
    @@ -4931,7 +4931,7 @@ bool Chainstate::ReplayBlocks()
         // Roll forward from the forking point to the new tip.
         if (nForkHeight < pindexNew->nHeight) {
             LogInfo("Rolling forward to %s (%i to %i)", pindexNew->GetBlockHash().ToString(), nForkHeight, pindexNew->nHeight);
    -        for (int nHeight = nForkHeight + 1; nHeight <= pindexNew->nHeight; ++nHeight) {
    +        for (int nHeight = nForkHeight + 1; nHeight < pindexNew->nHeight; ++nHeight) {
                 const CBlockIndex& pindex{*Assert(pindexNew->GetAncestor(nHeight))};
     
                 if (nHeight % 10'000 == 0) {
    
  2. DrahtBot added the label Tests on Nov 11, 2026
  3. DrahtBot commented at 2:49 PM on November 11, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process. A summary of reviews will appear here.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  4. in test/functional/feature_dbcrash_replay.py:29 in f70a21b569
      24 | +        node, ref = self.nodes
      25 | +
      26 | +        self.log.info("Build and flush a short branch on node0 that will be reorged out")
      27 | +        self.disconnect_nodes(0, 1)
      28 | +        node_wallet = MiniWallet(node)
      29 | +        for _ in range(2):
    


    brunoerg commented at 1:14 PM on November 13, 2026:

    Since both MiniWallets start with the same UTXO set and pick the largest coin by default, what about spending the smallest coins which branch B never spends? So a rollback must restore them. Suggestion from my clanker:

    diff --git a/test/functional/feature_dbcrash_replay.py b/test/functional/feature_dbcrash_replay.py
    index dbabb325aa..63ad2bfa30 100755
    --- a/test/functional/feature_dbcrash_replay.py
    +++ b/test/functional/feature_dbcrash_replay.py
    @@ -26,8 +26,10 @@ class ChainstateReplayTest(BitcoinTestFramework):
             self.log.info("Build and flush a short branch on node0 that will be reorged out")
             self.disconnect_nodes(0, 1)
             node_wallet = MiniWallet(node)
    -        for _ in range(2):
    -            node_wallet.send_self_transfer_multi(from_node=node, num_outputs=10)
    +        smallest = sorted(node_wallet.get_utxos(mark_as_spent=False), key=lambda u: (u['value'], -u['height']))[:2]
    +        for u in smallest:
    +            utxo = node_wallet.get_utxo(txid=u['txid'], vout=u['vout'])
    +            node_wallet.send_self_transfer_multi(from_node=node, utxos_to_spend=[utxo], num_outputs=10)
                 self.generate(node, 1, sync_fun=self.no_op)
     
             self.log.info("Restart node0 so that every partial coins batch write crashes")
    

    ViniciusCestarii commented at 5:45 PM on November 13, 2026:

    Thanks! I agree that it is better to also exercise the coin rollback.

    I changed the diff because every coin here has the same 50 BTC value so instead of ordering by smallest value it now orders by newest height since send_self_transfer_multi picks the oldest coins if the values are equal.

  5. ViniciusCestarii force-pushed on Nov 13, 2026
  6. ViniciusCestarii commented at 5:45 PM on November 13, 2026: contributor

    Thanks for reviewing! Forced-push 8d2969caf3d6a63c8ce11cc21d18c35b1cd2ab49 addressing @brunoerg #36837 (review)

  7. KY-U commented at 10:28 PM on November 13, 2026: contributor

    Verified that the added functional test kills both mutants. While testing, I noticed that the existing feature_dbcrash.py also appears to kill the second mutant. Still, I do think it is valuable to cover this scenario deterministically besides relying on random reorgs.

  8. test: add feature_dbcrash_replay.py 35002076f8
  9. ViniciusCestarii force-pushed on Nov 14, 2026
  10. ViniciusCestarii commented at 4:30 PM on November 14, 2026: contributor

    Force-pushed 35002076f8cc7add82ac3ae356114701c0643d05 addressing ralph comments https://git.fish.foo/bitcoin/bitcoin/pulls/36837#issuecomment-621835


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: 2026-11-18 16:51 UTC

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