test: deduplicate reorg test code #33959

Open
yuvicc wants to merge 2 commits from 2025-11-followup_fix_reorgs_tests into master AGit
yuvicc commented 2025-11-27 05:49:29 +00:00 (Migrated from github.com)

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.

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.
DrahtBot commented 2025-11-27 05:49:37 +00:00 (Migrated from github.com)

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

External sites

Reviews

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36990 (test: replace hard-coded RPC error codes by l0rinc)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/33959 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/33959.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/33959#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. A summary of reviews will appear here. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#36990](https://github.com/bitcoin/bitcoin/pull/36990) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36990.svg"></sub> (test: replace hard-coded RPC error codes by l0rinc) If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. <!--5faf32d7da4f0f540f40219e4f7537a3-->
maflcko commented 2026-05-21 14:05:06 +00:00 (Migrated from github.com)

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

are you still working on this, or can it be closed?
yuvicc commented 2026-05-22 11:50:48 +00:00 (Migrated from github.com)

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

Yes resumed the work, will push the update soon!

> are you still working on this, or can it be closed? Yes resumed the work, will push the update soon!
yuvicc commented 2026-06-12 12:24:27 +00:00 (Migrated from github.com)

Updated the PR description and ready for review

Updated the PR description and ready for review
sedited commented 2026-07-24 09:19:32 +00:00 (Migrated from github.com)

@instagibbs do you want to take a look here?

@instagibbs do you want to take a look here?
instagibbs commented 2026-07-24 11:09:11 +00:00 (Migrated from github.com)

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?

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?
yuvicc commented 2026-07-27 04:59:16 +00:00 (Migrated from github.com)

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

Addressed @instagibbs suggestion to only keep `trigger_reorg` method in blocktools.py.
instagibbs (Migrated from github.com) reviewed 2026-07-27 13:54:52 +00:00
instagibbs (Migrated from github.com) commented 2026-07-27 13:52:50 +00:00

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

wondering if there's a good place to add this removed comment to have people not reintroduce this pattern
instagibbs (Migrated from github.com) commented 2026-07-27 13:39:25 +00:00

stale comment with "invalid"

stale comment with "invalid"
instagibbs (Migrated from github.com) commented 2026-07-27 13:43:39 +00:00

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.
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.
instagibbs (Migrated from github.com) commented 2026-07-27 13:41:52 +00:00

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

are these nodes disconnected? I think it can be prepped once and used to trigger reorg on both sides?
instagibbs (Migrated from github.com) commented 2026-07-27 13:51:45 +00:00

no need for [0] subscript anymore

no need for [0] subscript anymore
yuvicc (Migrated from github.com) reviewed 2026-07-28 04:20:17 +00:00
yuvicc (Migrated from github.com) commented 2026-07-28 04:20:17 +00:00

Maybe we can add in create_empty_fork docstring?

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.
    '''
Maybe we can add in `create_empty_fork` docstring? <details> ```python 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>
yuvicc (Migrated from github.com) reviewed 2026-07-28 04:30:52 +00:00
yuvicc (Migrated from github.com) commented 2026-07-28 04:30:52 +00:00

Correct, they are connected. Will do the changes.

Correct, they are connected. Will do the changes.
yuvicc commented 2026-07-30 04:16:23 +00:00 (Migrated from github.com)

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.

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.
First-time contributor

🔵 Suggestion · Use the shared helper for the remaining fork submission

mempool_updatefromblock.py:195-196 still submits fork_blocks in an inline loop and skips the fork-tip assertion, even though trigger_reorg is imported and used elsewhere in the file. The same loop exists in base, so this PR did not introduce it; it is an incomplete migration.

Location: test/functional/mempool_updatefromblock.py:193 (head).

Review coverage was incomplete.


Reviewed: d72c022fa262 · Full review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:4d44b6ffa09c54db24f8f0ab27ae388beae06a733efcae741ca64db76bb1c938:7efd3c3ba41667382004cfa7a7db56605da7d8f9b7ab69bc483f27117390ba48 --> <!-- ralph-base:94181c18e25436d5e4a2de2ff71714413b4552a2 --> <!-- ralph-head:d72c022fa26243222dba968775587d1dba8da2df --> <!-- ralph-case:83781080b504ae55e76264e859265aebe1be1a631b284da30e91a12203f1b3eb --> **🔵 Suggestion · Use the shared helper for the remaining fork submission** mempool_updatefromblock.py:195-196 still submits fork_blocks in an inline loop and skips the fork-tip assertion, even though trigger_reorg is imported and used elsewhere in the file. The same loop exists in base, so this PR did not introduce it; it is an incomplete migration. Location: [`test/functional/mempool_updatefromblock.py:193`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/d72c022fa26243222dba968775587d1dba8da2df/test/functional/mempool_updatefromblock.py#L193>) (head). Review coverage was incomplete. <!-- Review tier: routine --> --- Reviewed: [`d72c022fa262`](<https://git.fish.foo/bitcoin/bitcoin/commit/d72c022fa26243222dba968775587d1dba8da2df>) · [Full review report](<https://review.fish.foo/traces/33959-d72c022fa26243222dba968775587d1dba8da2df-2142-5.html>) · [Previous reports](<https://review.fish.foo/traces/33959/>) [permalink](<https://review.fish.foo/traces/33959-d72c022fa26243222dba968775587d1dba8da2df-2142-5.html>)
DrahtBot commented 2026-10-19 09:37:12 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
sedited commented 2026-10-26 12:22:48 +00:00 (Migrated from github.com)

Ping for rebase @yuvicc

Ping for rebase @yuvicc
yuvicc commented 2026-10-28 05:48:50 +00:00 (Migrated from github.com)

Rebased to master

*Rebased to master*
DrahtBot commented 2026-11-02 01:07:44 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
yuvicc commented 2026-11-03 10:35:03 +00:00 (Migrated from github.com)

Rebased

Rebased
maflcko (Migrated from github.com) reviewed 2026-11-03 12:41:47 +00:00
maflcko (Migrated from github.com) commented 2026-11-03 12:41:47 +00:00

ralph llm nit:

🔵 Suggestion · Remove the unused block-result indexing

wallet_rescan_unconfirmed.py:49 still evaluates self.generate(tester_wallet, 1)[0] and discards the result. That index only existed to feed invalidateblock, which this PR replaced with trigger_reorg, so the PR left behind dead indexing.

ralph llm nit: > 🔵 Suggestion · Remove the unused block-result indexing wallet_rescan_unconfirmed.py:49 still evaluates self.generate(tester_wallet, 1)[0] and discards the result. That index only existed to feed invalidateblock, which this PR replaced with trigger_reorg, so the PR left behind dead indexing.
yuvicc (Migrated from github.com) reviewed 2026-11-04 17:23:46 +00:00
yuvicc (Migrated from github.com) commented 2026-11-04 17:23:46 +00:00

Done, thanks.

Done, thanks.
yuvicc commented 2026-11-04 17:25:01 +00:00 (Migrated from github.com)

Addressed ralph llvm review nit: removed unused block-hash indexing in wallet_rescan_unconfirmed.py.

Addressed ralph llvm review nit: removed unused block-hash indexing in `wallet_rescan_unconfirmed.py`.
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!33959
No description provided.