This repository was archived by the owner on Nov 15, 2023. It is now read-only.
fork tree: rebalance fork tree by max fork depth #4605
Merged
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.
BABE needs to keep track of the set of all epoch changes across all forks, only being able to prune those epoch changes as they are finalized, otherwise since it'd still be possible to re-org to the other forks we need to keep those nodes in the tree in order to be able to validate any blocks from those unfinalized forks. This tree can get potentially very large if finality is stalled (or lagging) and there is a high number of forks.
Most operations in the tree are performed with depth-first search starting from the leftmost node at every level. For our use-case a good heuristic is that the node we'll be looking for at any point will likely be in one of the deepest chains (i.e. the longest ones), therefore this PR introduces an optimization to rebalance the tree according to fork depth.
This implementation is very naive and simple, but given that searching for nodes in the tree incurs an expensive IO operation (to check block ancestry), the extra cost of traversing the tree multiple times to rebalance it is negligible compared to the gains.
In Kusama we experienced a high number of forks due to nodes going out of sync (caused by temporary 1s block times). Below are visualizations of the epoch changes tree before and after rebalancing. Rebalancing the tree made block import time go back to normal.
fork
fork-balance