feat(sync): add unwinding support for chain reorganizations #323
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Implements unwinding support for the sync pipeline to handle chain reorganizations. When the pipeline detects that a new chain tip is smaller than the current tip, it needs to revert the blockchain state back to a specific block number. This is a critical feature for maintaining consensus in distributed systems where temporary forks can occur.
The implementation extends the Stage trait with an unwind method that each stage must implement according to its specific data storage patterns. The Blocks stage handles removal of block metadata, headers, and all associated transactions from the database. The Classes stage manages the cleanup of contract class declarations and their compiled artifacts. The StateTrie stage unwinding is intentionally left unimplemented with clear documentation noting that it requires complex merkle trie unwinding logic to maintain tree invariants, which is beyond the current scope.
Each unwinding operation is performed within a database transaction to ensure atomicity, and stage checkpoints are updated afterward to maintain consistency across the pipeline. This ensures that if the node crashes during unwinding, the database remains in a valid state.