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 cleans up (and optimizes) how we rebuild the sync MMR during node startup.
We do not need to truncate the sync MMR and rebuild it from the genesis header, we can simply "rewind and apply header fork" based on current sync MMR state and the current head of the header chain. In the common case we will not need to rewind any headers and we can simply "fast forward" the recent headers to allow the sync MMR to catch up to the header MMR.
This saves a significant amount of time when starting a node up if shutdown overnight for example.
Rebuilding a sync MMR from scratch was taking 5s or so on a reasonably fast laptop.
As part of this we have now made the "reset head" logic more explicit, passing in the new head updating as necessary.
This PR also cleans up and simplifies how we apply headers to the MMR when processing, either as part of sync or when processing "header first". This makes processing new headers and processing headers from the local db as part of a fork more consistent.
Previous behavior -
Simplified behavior -