-
Notifications
You must be signed in to change notification settings - Fork 46
Adding is_descendent_of defenitions and fork tree testing with blockqueues #1198
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
Merged
Merged
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
baed83b
Adding fork-tree to worker
coax1d b7a6a37
Rebasing
coax1d 8686ccc
Cargo fmt and clippy
coax1d 4533b6b
taplo fmt
coax1d 111b7c8
adding in default impl for clippy and removing senseless comments
coax1d 07dcd74
minor fmt
coax1d fdbe2c6
Cherry picking and merging
coax1d de3d31a
Cherry picking test of is_descendent_builder
coax1d 26daddc
Removing comments and cleanup
coax1d cfe8a6a
Adding in build_queue_header helper
coax1d 56d63cb
Adding in imports for SidechainBlockBuilderTrait
coax1d 9882418
Cargo fmt taplo fmt and clippy
coax1d 80a1c1e
Adding some documentation to new structures
coax1d 35f055e
cargo fmt
coax1d e88a6f0
Rebasing
coax1d 473ce64
Addressing comments
coax1d 97128ef
Refactoring for pr
coax1d 01a574c
Fix incorrect comment
coax1d 5d666cd
fixing docstring
coax1d 2e6ab83
rebasing
coax1d c452dc2
cargo fmt
coax1d ae8143a
Moving errors to correct file
coax1d d88d4ea
refactor from comments half
coax1d 8dc3830
Refactoring for Chris comments
coax1d 8620d4e
Merge branch 'master' into adding_is_descendent_of_fork_tree_testing
coax1d 3fddc34
Missing import
coax1d 511b8cf
cargo fmt
coax1d 45afc72
Merge branch 'master' into adding_is_descendent_of_fork_tree_testing
coax1d 3eccee1
Minor fixes for `is_descendant_builder` (#1206)
clangenb d35d025
Update sidechain/consensus/common/src/is_descendant_of_builder.rs
coax1d 47b57fd
Update sidechain/consensus/common/src/is_descendant_of_builder.rs
coax1d 6202662
Update sidechain/consensus/common/src/is_descendant_of_builder.rs
coax1d 8b6669a
Update sidechain/consensus/common/src/is_descendant_of_builder.rs
coax1d 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
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 |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| Copyright 2021 Integritee AG and Supercomputing Systems AG | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
|
|
||
| */ | ||
| use itp_types::H256; | ||
| use its_primitives::traits::Header as HeaderT; | ||
| use std::{collections::HashMap, convert::From, hash::Hash as HashT}; | ||
|
|
||
| /// Normally implemented on the `client` in substrate. | ||
| /// Is a trait which can offer methods for interfacing with a block Database. | ||
| pub trait HeaderDbTrait { | ||
| type Header: HeaderT; | ||
| /// Retrieves Header for the corresponding block hash. | ||
| fn header(&self, hash: &H256) -> Option<Self::Header>; | ||
| } | ||
|
|
||
| /// A mocked Header Database which allows you to take a Block Hash and Query a Block Header. | ||
| pub struct HeaderDb<Hash, Header>(pub HashMap<Hash, Header>); | ||
|
|
||
| impl<Hash, Header> HeaderDbTrait for HeaderDb<Hash, Header> | ||
| where | ||
| // TODO: the H256 trait bounds are needed because: #1203 | ||
| Hash: PartialEq + HashT + Into<H256> + From<H256> + core::cmp::Eq + Clone, | ||
| Header: HeaderT + Clone, | ||
| { | ||
| type Header = Header; | ||
|
|
||
| fn header(&self, hash: &H256) -> Option<Self::Header> { | ||
| let header = self.0.get(&Hash::from(*hash))?; | ||
| Some(header.clone()) | ||
| } | ||
| } |
133 changes: 133 additions & 0 deletions
133
sidechain/consensus/common/src/is_descendant_of_builder.rs
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 |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| /* | ||
| Copyright 2021 Integritee AG and Supercomputing Systems AG | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
|
|
||
| */ | ||
| use crate::header_db::HeaderDbTrait; | ||
| use core::{hash::Hash as HashT, marker::PhantomData}; | ||
| use itp_types::H256; | ||
| use its_primitives::traits::Header as HeaderT; | ||
|
|
||
| pub struct IsDescendantOfBuilder<Hash, HeaderDb, Error>(PhantomData<(Hash, HeaderDb, Error)>); | ||
|
|
||
| impl<'a, Hash, HeaderDb, Error> IsDescendantOfBuilder<Hash, HeaderDb, Error> | ||
| where | ||
| Error: From<()>, | ||
| Hash: PartialEq + HashT + Default + Into<H256> + From<H256> + Clone, | ||
| HeaderDb: HeaderDbTrait, | ||
| { | ||
| /// Builds the `is_descendant_of` closure for the fork-tree | ||
| /// used when adding and removing nodes from the tree. | ||
| pub fn build_is_descendant_of( | ||
| current: Option<(&'a Hash, &'a Hash)>, | ||
| header_db: &'a HeaderDb, | ||
| ) -> impl Fn(&Hash, &Hash) -> Result<bool, Error> + 'a { | ||
| move |base, head| { | ||
| // If the base is equal to the proposed head, then the head is for sure not a descendant of the base. | ||
| if base == head { | ||
| return Ok(false) | ||
| } | ||
|
|
||
| let mut head = head; | ||
| if let Some((current_hash, current_parent_hash)) = current { | ||
| // If the current hash is equal to the base, then it will not be a descendant of base. | ||
| if current_hash == base { | ||
| return Ok(false) | ||
| } | ||
|
|
||
| // If the current hash is the head and the parent is the base, then we know that | ||
| // this current hash is the descendant of the parent. Otherwise we can set the | ||
| // head to the parent and find the lowest common ancestor between `head` | ||
| /// and `base` in the tree. | ||
| if current_hash == head { | ||
| if current_parent_hash == base { | ||
| return Ok(true) | ||
| } else { | ||
| head = current_parent_hash; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let ancestor = | ||
| <LowestCommonAncestorFinder<Hash, HeaderDb>>::find_lowest_common_ancestor( | ||
| head, base, header_db, | ||
| )?; | ||
| Ok(ancestor == *base) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub struct LowestCommonAncestorFinder<Hash, HeaderDb>(PhantomData<(Hash, HeaderDb)>); | ||
|
|
||
| impl<Hash, HeaderDb> LowestCommonAncestorFinder<Hash, HeaderDb> | ||
| where | ||
| Hash: PartialEq + Default + Into<H256> + From<H256> + Clone, | ||
| HeaderDb: HeaderDbTrait, | ||
| { | ||
| /// Used by the `build_is_descendant_of` to find the LCA of two nodes in the fork-tree. | ||
| fn find_lowest_common_ancestor(a: &Hash, b: &Hash, header_db: &HeaderDb) -> Result<Hash, ()> { | ||
| let header_1 = header_db.header(&a.clone().into()).ok_or(())?; | ||
| let header_2 = header_db.header(&b.clone().into()).ok_or(())?; | ||
| let mut blocknum_1 = header_1.block_number(); | ||
| let mut blocknum_2 = header_2.block_number(); | ||
| let mut parent_1 = Hash::from(header_1.parent_hash()); | ||
| let mut parent_2 = Hash::from(header_2.parent_hash()); | ||
|
|
||
| if *a == parent_2 { | ||
| // Then a is the common ancestor of b and it means it is itself the ancestor | ||
| return Ok(parent_2) | ||
| } | ||
|
|
||
| if *b == parent_1 { | ||
| // Then b is the common ancestor of a and it means it is itself the ancestor | ||
| return Ok(parent_1) | ||
| } | ||
|
|
||
| while blocknum_1 > blocknum_2 { | ||
| // This means block 1 is further down in the tree than block 2 | ||
| let new_parent = header_db.header(&parent_1.clone().into()).ok_or(())?; | ||
|
|
||
| if new_parent.block_number() >= blocknum_2 { | ||
| blocknum_1 = new_parent.block_number(); | ||
| parent_1 = Hash::from(new_parent.parent_hash()); | ||
| } else { | ||
| break | ||
| } | ||
| } | ||
|
|
||
| while blocknum_2 > blocknum_1 { | ||
| // This means block 2 is further down in the tree than block 1 | ||
| let new_parent = header_db.header(&parent_2.clone().into()).ok_or(())?; | ||
|
|
||
| if new_parent.block_number() >= blocknum_1 { | ||
| blocknum_2 = new_parent.block_number(); | ||
| parent_2 = Hash::from(new_parent.parent_hash()); | ||
| } else { | ||
| break | ||
| } | ||
| } | ||
|
|
||
| // At this point will be at equal height | ||
| while parent_1 != parent_2 { | ||
| // go up on both nodes | ||
| let new_header_1 = header_db.header(&parent_1.into()).ok_or(())?; | ||
| let new_header_2 = header_db.header(&parent_2.into()).ok_or(())?; | ||
| parent_1 = Hash::from(new_header_1.parent_hash()); | ||
| parent_2 = Hash::from(new_header_2.parent_hash()); | ||
| } | ||
|
|
||
| // Return any Parent node Hash as in worst case scenario it is the root which is shared amongst all | ||
| Ok(parent_1) | ||
| } | ||
| } | ||
35 changes: 0 additions & 35 deletions
35
sidechain/consensus/common/src/is_descendent_of_builder.rs
This file was deleted.
Oops, something went wrong.
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.
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.
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.
Thanks a lot for all these comments here, they help a lot. Regardless, I have to tell you that we are very strict with documentation/comments. They should always be:
Hence, I will add some comments to this file. I hope you can cope with my pickiness. :)
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.
I dont mind the pickiness might just take me a second to get it exactly correct though.
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.
Hahah, thanks for your understanding! :D