Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3754,6 +3754,7 @@ dependencies = [
name = "its-consensus-common"
version = "0.9.0"
dependencies = [
"fork-tree",
"itc-parentchain-light-client",
"itc-parentchain-test",
"itp-block-import-queue",
Expand Down
2 changes: 1 addition & 1 deletion core/rpc-server/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use super::*;
use crate::mock::MockSidechainBlockFetcher;
use itp_rpc::RpcResponse;
use its_rpc_handler::constants::RPC_METHOD_NAME_IMPORT_BLOCKS;
use its_test::sidechain_block_builder::SidechainBlockBuilder;
use its_test::sidechain_block_builder::{SidechainBlockBuilder, SidechainBlockBuilderTrait};
use jsonrpsee::{
types::{to_json_value, traits::Client},
ws_client::WsClientBuilder,
Expand Down
9 changes: 9 additions & 0 deletions enclave-runtime/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,14 @@ dependencies = [
"hashbrown 0.3.1",
]

[[package]]
name = "fork-tree"
version = "3.0.0"
dependencies = [
"parity-scale-codec",
"sgx_tstd",
]

[[package]]
name = "fp-evm"
version = "3.0.0-dev"
Expand Down Expand Up @@ -2252,6 +2260,7 @@ dependencies = [
name = "its-consensus-common"
version = "0.9.0"
dependencies = [
"fork-tree",
"itc-parentchain-light-client",
"itp-block-import-queue",
"itp-extrinsics-factory",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ mod tests {
use crate::ocall_bridge::test::mocks::sidechain_bridge_mock::SidechainBridgeMock;
use codec::{Decode, Encode};
use its_primitives::types::block::SignedBlock;
use its_test::sidechain_block_builder::SidechainBlockBuilder;
use its_test::sidechain_block_builder::{SidechainBlockBuilder, SidechainBlockBuilderTrait};
use primitive_types::H256;

#[test]
Expand Down
2 changes: 1 addition & 1 deletion service/src/ocall_bridge/sidechain_ocall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ mod tests {
use its_peer_fetch::mocks::fetch_blocks_from_peer_mock::FetchBlocksFromPeerMock;
use its_primitives::types::block::SignedBlock as SignedSidechainBlock;
use its_storage::{interface::BlockStorage, Result as StorageResult};
use its_test::sidechain_block_builder::SidechainBlockBuilder;
use its_test::sidechain_block_builder::{SidechainBlockBuilder, SidechainBlockBuilderTrait};
use primitive_types::H256;
use std::{collections::HashMap, vec::Vec};

Expand Down
2 changes: 1 addition & 1 deletion service/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ mod tests {
use frame_support::assert_ok;
use itp_node_api::node_api_factory::NodeApiFactory;
use its_primitives::types::block::SignedBlock as SignedSidechainBlock;
use its_test::sidechain_block_builder::SidechainBlockBuilder;
use its_test::sidechain_block_builder::{SidechainBlockBuilder, SidechainBlockBuilderTrait};
use jsonrpsee::{ws_server::WsServerBuilder, RpcModule};
use log::debug;
use sp_keyring::AccountKeyring;
Expand Down
2 changes: 1 addition & 1 deletion sidechain/block-verification/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ mod tests {
use itp_types::{AccountId, Block as ParentchainBlock};
use its_primitives::types::{block::SignedBlock, header::SidechainHeader as Header};
use its_test::{
sidechain_block_builder::SidechainBlockBuilder,
sidechain_block_builder::{SidechainBlockBuilder, SidechainBlockBuilderTrait},
sidechain_block_data_builder::SidechainBlockDataBuilder,
sidechain_header_builder::SidechainHeaderBuilder,
};
Expand Down
2 changes: 1 addition & 1 deletion sidechain/consensus/aura/src/test/block_importer_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use its_primitives::{
};
use its_state::StateUpdate;
use its_test::{
sidechain_block_builder::SidechainBlockBuilder,
sidechain_block_builder::{SidechainBlockBuilder, SidechainBlockBuilderTrait},
sidechain_block_data_builder::SidechainBlockDataBuilder,
sidechain_header_builder::SidechainHeaderBuilder,
};
Expand Down
2 changes: 1 addition & 1 deletion sidechain/consensus/aura/src/test/mocks/proposer_mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use itp_types::{Block as ParentchainBlock, Header};
use its_consensus_common::{Proposal, Proposer};
use its_primitives::types::block::SignedBlock as SignedSidechainBlock;
use its_test::{
sidechain_block_builder::SidechainBlockBuilder,
sidechain_block_builder::{SidechainBlockBuilder, SidechainBlockBuilderTrait},
sidechain_block_data_builder::SidechainBlockDataBuilder,
};
use std::time::Duration;
Expand Down
3 changes: 3 additions & 0 deletions sidechain/consensus/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ log = { version = "0.4", default-features = false }
thiserror = { version = "1.0.26", optional = true }

# local deps
fork-tree = { path = "../../fork-tree", default-features = false }
itc-parentchain-light-client = { path = "../../../core/parentchain/light-client", default-features = false }
itp-block-import-queue = { path = "../../../core-primitives/block-import-queue", default-features = false }
itp-extrinsics-factory = { path = "../../../core-primitives/extrinsics-factory", default-features = false }
Expand Down Expand Up @@ -60,6 +61,7 @@ std = [
"its-primitives/std",
"its-block-verification/std",
"its-state/std",
"fork-tree/std",
# substrate
"sp-runtime/std",
# scs
Expand All @@ -76,6 +78,7 @@ sgx = [
"itp-sgx-crypto/sgx",
"itp-sgx-externalities/sgx",
"its-state/sgx",
"fork-tree/sgx",
# scs
"its-block-verification/sgx",
]
35 changes: 35 additions & 0 deletions sidechain/consensus/common/src/is_descendent_of_builder.rs
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 {
Comment on lines +22 to +24
Copy link
Contributor

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. :)

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));
}
1 change: 1 addition & 0 deletions sidechain/consensus/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ mod block_import;
mod block_import_confirmation_handler;
mod block_import_queue_worker;
mod error;
mod is_descendent_of_builder;
mod peer_block_sync;

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion sidechain/consensus/common/src/peer_block_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ mod tests {
use itp_test::mock::sidechain_ocall_api_mock::SidechainOCallApiMock;
use itp_types::Block as ParentchainBlock;
use its_primitives::types::block::SignedBlock as SignedSidechainBlock;
use its_test::sidechain_block_builder::SidechainBlockBuilder;
use its_test::sidechain_block_builder::{SidechainBlockBuilder, SidechainBlockBuilderTrait};

type TestBlockImport = BlockImportMock<ParentchainBlock, SignedSidechainBlock>;
type TestOCallApi = SidechainOCallApiMock<SignedSidechainBlock>;
Expand Down
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
//
// - H2..
// /
// H1.. - H4..
// \ /
// - H3..
// \
// - H5..
//
todo!();
println!("Process Sequential Queue with Forks")
}
}
2 changes: 1 addition & 1 deletion sidechain/peer-fetch/src/block_fetch_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ mod tests {
};
use its_primitives::types::block::SignedBlock;
use its_storage::fetch_blocks_mock::FetchBlocksMock;
use its_test::sidechain_block_builder::SidechainBlockBuilder;
use its_test::sidechain_block_builder::{SidechainBlockBuilder, SidechainBlockBuilderTrait};
use jsonrpsee::ws_server::WsServerBuilder;
use std::{net::SocketAddr, sync::Arc};

Expand Down
2 changes: 1 addition & 1 deletion sidechain/storage/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use itp_time_utils::now_as_millis;
use itp_types::ShardIdentifier;
use its_primitives::types::{BlockHash, SignedBlock as SignedSidechainBlock};
use its_test::{
sidechain_block_builder::SidechainBlockBuilder,
sidechain_block_builder::{SidechainBlockBuilder, SidechainBlockBuilderTrait},
sidechain_block_data_builder::SidechainBlockDataBuilder,
sidechain_header_builder::SidechainHeaderBuilder,
};
Expand Down
Loading