test: deduplicate reorg test code #33959

pull yuvicc wants to merge 2 commits into bitcoin:master from yuvicc:2025-11-followup_fix_reorgs_tests changing 13 files +104 −90
  1. yuvicc commented at 5:49 AM on November 27, 2025: contributor

    Follow-up to #32587.

    trigger_reorg() was copy-pasted across several functional tests. This moves it into blocktools.py, together with the fork construction, as two helpers: create_empty_fork() and trigger_reorg().

    The remaining tests that still simulated reorgs with the invalidateblock RPC are converted to use them, since invalidateblock takes a different codepath than a reorg caused by a competing chain arriving.

  2. DrahtBot added the label Tests on Nov 27, 2025
  3. DrahtBot commented at 5:49 AM on November 27, 2025: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/33959.

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

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

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    No conflicts as of last run.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  4. fanquake requested review from instagibbs on Nov 27, 2025
  5. fanquake renamed this:
    test: use ForkGenerator to deduplicate reorg test code (#32587 follow-up)
    test: use ForkGenerator to deduplicate reorg test code
    on Nov 27, 2025
  6. yuvicc force-pushed on Dec 13, 2025
  7. DrahtBot added the label Needs rebase on Feb 25, 2026
  8. maflcko commented at 2:05 PM on May 21, 2026: member

    are you still working on this, or can it be closed?

  9. yuvicc commented at 11:50 AM on May 22, 2026: contributor

    are you still working on this, or can it be closed?

    Yes resumed the work, will push the update soon!

  10. yuvicc force-pushed on Jun 12, 2026
  11. DrahtBot removed the label Needs rebase on Jun 12, 2026
  12. DrahtBot added the label CI failed on Jun 12, 2026
  13. DrahtBot removed the label CI failed on Jun 12, 2026
  14. yuvicc force-pushed on Jun 12, 2026
  15. yuvicc marked this as ready for review on Jun 12, 2026
  16. yuvicc commented at 12:24 PM on June 12, 2026: contributor

    Updated the PR description and ready for review

  17. sedited commented at 9:19 AM on July 24, 2026: contributor

    @instagibbs do you want to take a look here?

  18. instagibbs commented at 11:09 AM on July 24, 2026: member

    I think this might be a bit of overengineering. Centralizing code is nice, but really I don't see the value of the added abstraction aside from centralizing trigger_reorg?

  19. yuvicc force-pushed on Jul 27, 2026
  20. yuvicc commented at 4:59 AM on July 27, 2026: contributor

    Addressed @instagibbs suggestion to only keep trigger_reorg method in blocktools.py.

  21. in test/functional/p2p_sendheaders.py:214 in 2edf797078
     207 | @@ -208,17 +208,17 @@ def mine_reorg(self, length):
     208 |          to-be-reorged-out blocks are mined, so that we don't break later tests.
     209 |          return the list of block hashes newly mined."""
     210 |  
     211 | +        # Prep for fork with empty blocks
     212 | +        fork_blocks = create_empty_fork(self.nodes[1], fork_length=length + 1)
     213 | +
     214 |          # make sure all invalidated blocks are node0's
    


    instagibbs commented at 1:39 PM on July 27, 2026:

    stale comment with "invalid"

  22. in test/functional/wallet_balance.py:245 in 2edf797078
     240 | @@ -237,13 +241,17 @@ def test_balances(*, fee_node_1=0):
     241 |          self.sync_all()
     242 |          self.nodes[1].sendrawtransaction(hexstring=tx_replace, maxfeerate=0)
     243 |  
     244 | +        # Prep for fork
     245 | +        fork_blocks0 = create_empty_fork(self.nodes[0])
    


    instagibbs commented at 1:41 PM on July 27, 2026:

    are these nodes disconnected? I think it can be prepped once and used to trigger reorg on both sides?


    yuvicc commented at 4:30 AM on July 28, 2026:

    Correct, they are connected. Will do the changes.

  23. in test/functional/rpc_gettxspendingprevout.py:192 in 2edf797078
     188 | @@ -184,7 +189,10 @@ def create_tx(**kwargs):
     189 |          result = node0.gettxspendingprevout([prevout(tx1['txid'], vout=0)], return_spending_tx=True)
     190 |          assert_equal(result, [spent_out_in_block(tx1['txid'], vout=0, spending_tx_id=tx2["txid"], blockhash=blockhash, spending_tx=tx2['hex'])])
     191 |  
     192 | -        # replace tx1 with tx3
     193 | +        # replace tx1 with tx3. We use invalidateblock (rather than a fork-based
    


    instagibbs commented at 1:43 PM on July 27, 2026:

    suggestion for wording:

          # Disconnect tx1's block so tx1 can be replaced by tx3 below, which also
          # evicts tx1's child tx2. Unlike the case above, use invalidateblock rather
          # than a fork-based reorg: the index is only rewound when a block connects
          # so disconnecting without connecting anything lets us check below that tx2 is still indexed until the next block.
  24. in test/functional/wallet_balance.py:249 in 2edf797078
     245 | +        fork_blocks0 = create_empty_fork(self.nodes[0])
     246 | +        fork_blocks1 = create_empty_fork(self.nodes[1])
     247 | +
     248 |          # Now confirm tx_replace
     249 | -        block_reorg = self.generatetoaddress(self.nodes[1], 1, ADDRESS_WATCHONLY)[0]
     250 | +        self.generatetoaddress(self.nodes[1], 1, ADDRESS_WATCHONLY)[0]
    


    instagibbs commented at 1:51 PM on July 27, 2026:

    no need for [0] subscript anymore

  25. in test/functional/mempool_updatefromblock.py:51 in d3079bfb85 outdated
      43 | @@ -47,8 +44,7 @@ def transaction_graph_test(self, size, *, n_tx_to_mine, fee=100_000):
      44 |          """
      45 |          wallet = MiniWallet(self.nodes[0])
      46 |  
      47 | -        # Prep for fork with empty blocks to not use invalidateblock directly
      48 | -        # for reorg case. The rpc has different codepath
    


    instagibbs commented at 1:52 PM on July 27, 2026:

    wondering if there's a good place to add this removed comment to have people not reintroduce this pattern


    yuvicc commented at 4:20 AM on July 28, 2026:

    Maybe we can add in create_empty_fork docstring?

    <details>

    def create_empty_fork(node, fork_length=FORK_LENGTH):
        '''
            Creates a fork using node's chaintip as the starting point.
            Returns a list of blocks to submit in order.
    
            Prefer this over calling the invalidateblock RPC to simulate a reorg:
            invalidateblock takes a different codepath than a reorg caused by a
            competing chain arriving, so tests using it are not exercising the
            behaviour they mean to.
        '''
    

    </details>

  26. test: move duplicated `trigger_reorg` method to blocktools.py. 3948943974
  27. test: fix reorg behaviour in wallet, p2p and rpc functional tests to use proper fork-based approach 56e96c0a5d
  28. yuvicc force-pushed on Jul 30, 2026
  29. yuvicc renamed this:
    test: use ForkGenerator to deduplicate reorg test code
    test: deduplicate reorg test code and update remaining tests to fork-based reorgs
    on Jul 30, 2026
  30. yuvicc renamed this:
    test: deduplicate reorg test code and update remaining tests to fork-based reorgs
    test: deduplicate reorg test code
    on Jul 30, 2026
  31. yuvicc commented at 4:16 AM on July 30, 2026: contributor

    Thanks for the review @instagibbs I've removed Forkgenerator class and only kept trigger_reorg in blocktools and addressed some comments.

    I have also updated PR title and the description.


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-08-11 10:51 UTC

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