-
Notifications
You must be signed in to change notification settings - Fork 417
feat(chain)!: make CheckPoint data field optional
#2024
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LagginTimes
wants to merge
7
commits into
bitcoindevkit:master
Choose a base branch
from
LagginTimes:optional_data
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,008
−64
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6dabf92
feat(chain)!: make `CheckPoint` `data` field optional
LagginTimes 214365f
fix(chain): Fix `merge_chains` logic
evanlinjin 1f1304c
test(chain): add test for `merge_chains`
LagginTimes ad322d2
docs(chain): update `CheckPoint` and `LocalChain` docs
LagginTimes 00060c1
test(core): add tests for CheckPoint::push and insert methods
evanlinjin 7feb47c
fix(core): fix `CheckPoint::insert` and `CheckPoint::push` logic
LagginTimes bd7a0db
fix(core): rework CheckPoint::insert and improve push logic
evanlinjin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,11 +26,14 @@ where | |
| let mut base: Option<CheckPoint<D>> = None; | ||
|
|
||
| for cp in init_cp.iter() { | ||
| if cp.height() >= start_height { | ||
| extension.insert(cp.height(), cp.data()); | ||
| } else { | ||
| base = Some(cp); | ||
| break; | ||
| // Base tip should always have data. | ||
| if let Some(data) = cp.data() { | ||
| if cp.height() >= start_height { | ||
| extension.insert(cp.height(), data); | ||
| } else { | ||
| base = Some(cp); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -51,6 +54,7 @@ where | |
| .expect("extension is strictly greater than base"), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
| None => LocalChain::from_blocks(extension)?.tip(), | ||
| }; | ||
|
|
||
| init_cp = new_tip; | ||
| } | ||
|
|
||
|
|
@@ -322,11 +326,7 @@ where | |
| /// recover the current chain. | ||
| pub fn initial_changeset(&self) -> ChangeSet<D> { | ||
| ChangeSet { | ||
| blocks: self | ||
| .tip | ||
| .iter() | ||
| .map(|cp| (cp.height(), Some(cp.data()))) | ||
| .collect(), | ||
| blocks: self.tip.iter().map(|cp| (cp.height(), cp.data())).collect(), | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -349,6 +349,20 @@ where | |
| update_hash: Some(data.to_blockhash()), | ||
| }); | ||
| } | ||
|
|
||
| // If this `CheckPoint` is an empty placeholder, append the `data` to it. | ||
| if original_cp.data_ref().is_none() { | ||
| let mut changeset = ChangeSet::<D>::default(); | ||
| changeset.blocks.insert(height, Some(data)); | ||
| self.apply_changeset(&changeset) | ||
| .map_err(|_| AlterCheckPointError { | ||
| height: 0, | ||
| original_hash: self.genesis_hash(), | ||
| update_hash: None, | ||
| })?; | ||
| return Ok(changeset); | ||
| } | ||
|
|
||
| return Ok(ChangeSet::default()); | ||
| } | ||
|
|
||
|
|
@@ -634,7 +648,12 @@ where | |
| match (curr_orig.as_ref(), curr_update.as_ref()) { | ||
| // Update block that doesn't exist in the original chain | ||
| (o, Some(u)) if Some(u.height()) > o.map(|o| o.height()) => { | ||
| changeset.blocks.insert(u.height(), Some(u.data())); | ||
| // Only append to `ChangeSet` when the update has complete data. Entries where | ||
| // `data` does not exist that are created via `prev_blockhash` should not alter the | ||
| // `ChangeSet`. | ||
| if let Some(data) = u.data() { | ||
| changeset.blocks.insert(u.height(), Some(data)); | ||
| } | ||
| prev_update = curr_update.take(); | ||
| } | ||
| // Original block that isn't in the update | ||
|
|
@@ -682,10 +701,15 @@ where | |
| return Ok((new_tip, changeset)); | ||
| } | ||
| } | ||
| // Even if the hashes are the same, the update may contain data which the | ||
| // original does not have. | ||
| if let (None, Some(u_data)) = (o.data_ref(), u.data()) { | ||
| changeset.blocks.insert(u.height(), Some(u_data)); | ||
| } | ||
| } else { | ||
| // We have an invalidation height so we set the height to the updated hash and | ||
| // also purge all the original chain block hashes above this block. | ||
| changeset.blocks.insert(u.height(), Some(u.data())); | ||
| changeset.blocks.insert(u.height(), u.data()); | ||
| for invalidated_height in potentially_invalidated_heights.drain(..) { | ||
| changeset.blocks.insert(invalidated_height, None); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There seems to be a lot of additional logic in
LocalChainto manageChangeSets. This is non-ideal as the mutating logic ofCheckPointis now more complex - so we really want to keep the complexity inCheckPointonly.Instead of having
apply_changeset_to_checkpoint, I'm thinking of using methods ofCheckPointdirectly and comparing the original and updated checkpoint chains to derive the checkpoint.