blockchain: Implement new chain view. #1337
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.
This PR is rebased on #1334.
This implements a new type in the blockchain package that takes advantage of the fact that all block nodes are now in memory to provide a flat view of a specific chain of blocks (a specific branch of the overall block tree) from a given tip all the way back to the genesis block along with several convenience functions such as efficiently comparing two views, quickly finding the fork point (if any) between two views, and O(1) lookup of the node at a specific height.
The view is not currently used, but the intent is that the code will be refactored to make use of these views to simplify and optimize several areas such as best chain selection and reorg logic and finding successor nodes. They will also greatly simplify the process of disconnecting the download logic from the connection logic.
Since the ultimate intent is for there to be a long-lived chain view instance for the current best chain, this also implements efficient handling of setting the tip to new values including domain-specific capacity increase handling which chooses the desired increase amount more intelligently than the default algorithm, which would way overshoot, as well as adds some additional space when the view is initialized.
A comprehensive suite of tests is provided to ensure the chain views behave correctly.
This is work towards #1145.