-
Notifications
You must be signed in to change notification settings - Fork 46
Adding queue builder helpers #1197
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
16 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 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
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
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
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
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
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
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
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
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
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
35 changes: 35 additions & 0 deletions
35
sidechain/consensus/common/src/is_descendent_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,35 @@ | ||
| #[cfg(test)] | ||
| use std::marker::PhantomData; | ||
|
|
||
| #[cfg(test)] | ||
| pub struct IsDescendentOfBuilder<Hash>(PhantomData<Hash>); | ||
| #[cfg(test)] | ||
| impl<'a, Hash: PartialEq> IsDescendentOfBuilder<Hash> { | ||
| #[cfg(test)] | ||
| /// Build the `is_descendent_of` closure for the fork-tree structure | ||
| /// to utilize when adding and removing nodes from the tree. | ||
| pub fn build_is_descendent_of( | ||
| _curr_block: Option<(&Hash, &Hash)>, | ||
| ) -> impl Fn(&Hash, &Hash) -> Result<bool, ()> + 'a { | ||
| move |_base, _head| Ok(true) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| pub struct LowestCommonAncestorFinder<Hash>(PhantomData<Hash>); | ||
| #[cfg(test)] | ||
| impl<Hash: PartialEq + Default> LowestCommonAncestorFinder<Hash> { | ||
| #[cfg(test)] | ||
| /// Used by the `build_is_descendent_of` to find the LCA of two nodes in the fork-tree. | ||
| pub fn find_lowest_common_ancestor(_a: Hash, _b: Hash) -> Hash { | ||
| Default::default() | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| #[test] | ||
| fn test_build_is_descendent_of_works() { | ||
| let is_descendent_of = <IsDescendentOfBuilder<u64>>::build_is_descendent_of(None); | ||
| let my_result = is_descendent_of(&42u64, &42u64); | ||
| assert_eq!(my_result, Ok(true)); | ||
| } | ||
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
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
130 changes: 130 additions & 0 deletions
130
sidechain/consensus/common/src/test/mocks/block_import_queue_worker_mock.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,130 @@ | ||
| use crate::{ | ||
| test::mocks::verifier_mock::VerifierMock, | ||
| BlockImport, | ||
| Error, | ||
| Result, | ||
| BlockImportQueueWorker, | ||
| SyncBlockFromPeer, | ||
| IsDescendentOfBuilder, | ||
| }; | ||
| use its_test::{ | ||
| sidechain_block_builder::SidechainBlockBuilderTrait, | ||
| sidechain_block_builder::SidechainBlockBuilder, | ||
| sidechain_block_data_builder::SidechainBlockDataBuilder as SidechainBlockData, | ||
| sidechain_header_builder::SidechainHeaderBuilder as SidechainHeader, | ||
| }; | ||
| use core::marker::PhantomData; | ||
| use itp_sgx_crypto::aes::Aes; | ||
| use itp_sgx_externalities::SgxExternalities; | ||
| use itp_test::mock::onchain_mock::OnchainMock; | ||
| use itp_types::{H256}; | ||
| use its_primitives::traits::{ShardIdentifierFor, SignedBlock as SignedSidechainBlockTrait}; | ||
| use sp_core::Pair; | ||
| use itp_block_import_queue::PopFromBlockQueue; | ||
| use its_primitives::{ | ||
| traits::{Block as BlockT, Header as HeaderT}, | ||
| types::{block_data::BlockData, header::SidechainHeader as Header, Block, SignedBlock} | ||
| }; | ||
| use sp_runtime::traits::Block as ParentchainBlockTrait; | ||
| use std::{collections::VecDeque, sync::RwLock}; | ||
|
|
||
| #[derive(Default)] | ||
| pub struct BlockQueueBuilder<B, Builder> { | ||
| queue: VecDeque<B>, | ||
| _phantom_data: PhantomData<Builder>, | ||
| } | ||
|
|
||
| impl<B, Builder> BlockQueueBuilder<B, Builder> | ||
| where | ||
| Builder: SidechainBlockBuilderTrait<Block = Block> + Default, | ||
| B: BlockT + From<Block> | ||
| { | ||
|
|
||
| fn new() -> Self { | ||
| Self { | ||
| queue: VecDeque::new(), | ||
| _phantom_data: PhantomData::default(), | ||
| } | ||
| } | ||
|
|
||
| /// Allows definining a mock queue based and assumes that a genesis block | ||
| /// will need to be appended to the queue as the first item. | ||
| /// Returns: BuiltQueue | ||
| fn build_queue(&mut self, f: impl Fn(VecDeque<B>) -> VecDeque<B>) -> VecDeque<B> { | ||
| self.add_genesis_block_to_queue(); | ||
| f(self.queue.clone()) | ||
| } | ||
|
|
||
| fn add_genesis_block_to_queue(&mut self) { | ||
| let genesis_header = Header { | ||
| block_number: 0, | ||
| parent_hash: H256::from_slice(&[0; 32]), | ||
| ..Default::default() | ||
| }; | ||
| let block: B = Builder::default().with_header(genesis_header).build().into(); | ||
| self.queue.push_back(block); | ||
| } | ||
| } | ||
|
|
||
| pub trait BlockQueueHeaderBuild<BlockNumber, Hash> { | ||
| type QueueHeader; | ||
| /// Helper trait to build a Header for a BlockQueue. | ||
| fn build_queue_header(block_number: BlockNumber, parent_hash: Hash) -> Self::QueueHeader; | ||
| } | ||
|
|
||
| pub struct BlockQueueHeaderBuilder<BlockNumber, Hash>(PhantomData<(BlockNumber, Hash)>); | ||
|
|
||
| impl<BlockNumber, Hash> BlockQueueHeaderBuild<BlockNumber, Hash> for BlockQueueHeaderBuilder<BlockNumber, Hash> | ||
| where | ||
| BlockNumber: Into<u64>, | ||
| Hash: Into<H256>, | ||
| { | ||
| type QueueHeader = Header; | ||
| fn build_queue_header(block_number: BlockNumber, parent_hash: Hash) -> Self::QueueHeader { | ||
| Header { | ||
| block_number: block_number.into(), | ||
| parent_hash: parent_hash.into(), | ||
| ..Default::default() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn process_sequential_queue_no_forks() { | ||
|
|
||
| let queue = <BlockQueueBuilder<Block, SidechainBlockBuilder>>::new().build_queue(|mut queue| { | ||
| for i in 1..5 { | ||
| let parent_header = queue.back().unwrap().header(); | ||
| let header = <BlockQueueHeaderBuilder<u64, H256>>::build_queue_header(i, parent_header.hash()); | ||
| queue.push_back(SidechainBlockBuilder::default().with_header(header).build()); | ||
| } | ||
| queue | ||
| }); | ||
|
|
||
| // TODO: Add blocks to the fork-tree and assert that everything is correct | ||
| // | ||
| // H1 - H2 - H3 - H4 - H5 | ||
| // | ||
| todo!(); | ||
| println!("Process Sequential Queue With No Forks"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn process_sequential_queue_with_forks() { | ||
| // TODO: Make sure this works correctly | ||
| // | ||
coax1d marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // - H2.. | ||
| // / | ||
| // H1.. - H4.. | ||
| // \ / | ||
| // - H3.. | ||
| // \ | ||
| // - H5.. | ||
| // | ||
| todo!(); | ||
| println!("Process Sequential Queue with Forks") | ||
| } | ||
| } | ||
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
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.
Minor thing I forgot to address: First comes the doc string, and afterwards any proc_macro attribute. I wonder why this is not a lint yet. :) However, I will be lenient, as you will start using the stuff afterwards anyhow, and therefore be removing those flags. :)