Skip to content

Conversation

@AlexandruCihodaru
Copy link
Contributor

Add support for handling blockchain revert. This is useful in testing.

Changes:

  • Add ChainEvent::Reverted variant to represent backward blockchain progression
  • Implement handle_reverted() method that:
    • Collects transactions from retracted blocks via included_transactions cache or by fetching block bodies from the API
    • Removes all views beyond the revert point to prevent zombie views
    • Removes included transactions from mempool (they can be resubmitted later)
    • Updates enactment state (recent_finalized_block and recent_best_block)
    • Ensures a valid view exists at the revert target block
  • Add early return in maintain() for Reverted events to prevent normal forward-progression logic from running

These changes fix issues where reverting would leave zombie views in the view store, causing issues at subsequent operations.

Note: Transactions that were pending may not be visible after revert if they fail the revalidation

Add support for handling blockchain revert. This is useful in testing.

Changes:
- Add ChainEvent::Reverted variant to represent backward blockchain progression
- Implement handle_reverted() method that:
  * Collects transactions from retracted blocks via included_transactions cache
    or by fetching block bodies from the API
  * Removes all views beyond the revert point to prevent zombie views
  * Removes included transactions from mempool (they can be resubmitted later)
  * Updates enactment state (recent_finalized_block and recent_best_block)
  * Ensures a valid view exists at the revert target block
- Add early return in maintain() for Reverted events to prevent normal
  forward-progression logic from running

These changes fix issues where reverting would leave zombie views in the view store,
causing issues at subsequent operations.

Note: Transactions that were pending may not be visible after revert if they fail the
revalidation

Signed-off-by: Alexandru Cihodaru <[email protected]>
@AlexandruCihodaru
Copy link
Contributor Author

/cmd fmt

@AlexandruCihodaru
Copy link
Contributor Author

/cmd prdoc --audience runtime_dev --bump patch

@michalkucharczyk
Copy link
Contributor

michalkucharczyk commented Nov 28, 2025

DQ:

          D1-E1-F1-G1-..-X1
         /
A - B - C - D2-E2-F2-G2-..-X2
         \
          D3-E3-F3-G3-..-X3

Is it realistic scenario? Should we handle this properly? If we revert from X1 to B, shold we also remove all transactions included on D2-...-X2 and D3-...-X3 forks (as we do for D1-...-X1)?

If we assume that revert can only be called if there is single fork - should we somehow check this in handle_revert function? (or at least document it somehow).

@AlexandruCihodaru
Copy link
Contributor Author

DQ:

          D1-E1-F1-G1-..-X1
         /
A - B - C - D2-E2-F2-G2-..-X2
         \
          D3-E3-F3-G3-..-X3

Is it realistic scenario? Should we handle this properly? If we revert from X1 to B, shold we also remove all transactions included on D2-...-X2 and D3-...-X3 forks (as we do for D1-...-X1)?

If we assume that revert can only be called if there is single fork - should we somehow check this in handle_revert function? (or at least document it somehow).

Excellent question. I think that in anvil it is not possible to have such a scenario but I believe that we should delete the transactions on all possible paths.

/// Path from old finalized to new finalized parent.
tree_route: Arc<[B::Hash]>,
},
/// The chain has been reverted to a new head.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to have some more information on the impact of this event to the internal state of the pool, e.g. all included transaction will be removed from the pool, what forks are removed (all or only one defined by tree_route param).

Copy link
Contributor

@iulianbarbu iulianbarbu Dec 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all included transaction will be removed from the pool, what forks are removed

Do you think about more than logging information about how many txs and forks are removed, as in a metric that can be queried later? Given that revert is mostly useful currently in anvil context, I would just add some more logging about the # of reverted forks and the reverted txs on all forks.

Copy link
Contributor

@iulianbarbu iulianbarbu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good in general. It would be great to capture in the event the idea of reverting all existing forks, to have this logic appliable not just to single chain nodes like anvil-polkadot - but tbh not super sure how difficult it is.

/// Path from old finalized to new finalized parent.
tree_route: Arc<[B::Hash]>,
},
/// The chain has been reverted to a new head.
Copy link
Contributor

@iulianbarbu iulianbarbu Dec 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all included transaction will be removed from the pool, what forks are removed

Do you think about more than logging information about how many txs and forks are removed, as in a metric that can be queried later? Given that revert is mostly useful currently in anvil context, I would just add some more logging about the # of reverted forks and the reverted txs on all forks.

@AlexandruCihodaru
Copy link
Contributor Author

/cmd fmt

@AlexandruCihodaru AlexandruCihodaru added the T0-node This PR/Issue is related to the topic “node”. label Dec 2, 2025
Co-authored-by: Michal Kucharczyk <[email protected]>
Signed-off-by: Alexandru Cihodaru <[email protected]>
@AlexandruCihodaru
Copy link
Contributor Author

/cmd fmt

@paritytech-workflow-stopper
Copy link

All GitHub workflows were cancelled due to failure one of the required jobs.
Failed workflow url: https://github.com/paritytech/polkadot-sdk/actions/runs/19868343219
Failed job name: fmt

@AlexandruCihodaru
Copy link
Contributor Author

/cmd fmt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

T0-node This PR/Issue is related to the topic “node”.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants