From 1c5e2132d82538b9c6eb93eb0050c2e9cf4efc89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Wed, 6 Nov 2019 09:00:29 +0100 Subject: [PATCH] Fixes tests --- cumulus/Cargo.lock | 4 ++ cumulus/collator/Cargo.toml | 4 ++ cumulus/collator/src/lib.rs | 75 +++++++++++++++++++-- cumulus/runtime/src/validate_block/tests.rs | 25 ++++--- 4 files changed, 94 insertions(+), 14 deletions(-) diff --git a/cumulus/Cargo.lock b/cumulus/Cargo.lock index a3289bfd24..e3c13cd1c1 100644 --- a/cumulus/Cargo.lock +++ b/cumulus/Cargo.lock @@ -553,13 +553,16 @@ version = "0.1.0" dependencies = [ "cumulus-consensus 0.1.0", "cumulus-runtime 0.1.0", + "cumulus-test-client 0.1.0", "cumulus-test-runtime 0.1.0", + "env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)", "futures-preview 0.3.0-alpha.19 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "parity-scale-codec 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "polkadot-collator 0.6.0 (git+https://github.com/paritytech/polkadot?branch=bkchr-cumulus-branch)", + "polkadot-executor 0.6.0 (git+https://github.com/paritytech/polkadot?branch=bkchr-cumulus-branch)", "polkadot-primitives 0.6.0 (git+https://github.com/paritytech/polkadot?branch=bkchr-cumulus-branch)", "sr-primitives 2.0.0 (git+https://github.com/paritytech/substrate?branch=bkchr-cumulus-branch)", "substrate-cli 2.0.0 (git+https://github.com/paritytech/substrate?branch=bkchr-cumulus-branch)", @@ -569,6 +572,7 @@ dependencies = [ "substrate-keyring 2.0.0 (git+https://github.com/paritytech/substrate?branch=bkchr-cumulus-branch)", "substrate-primitives 2.0.0 (git+https://github.com/paritytech/substrate?branch=bkchr-cumulus-branch)", "substrate-service 2.0.0 (git+https://github.com/paritytech/substrate?branch=bkchr-cumulus-branch)", + "substrate-test-client 2.0.0 (git+https://github.com/paritytech/substrate?branch=bkchr-cumulus-branch)", ] [[package]] diff --git a/cumulus/collator/Cargo.toml b/cumulus/collator/Cargo.toml index bfdae74437..7ec67147d1 100644 --- a/cumulus/collator/Cargo.toml +++ b/cumulus/collator/Cargo.toml @@ -31,4 +31,8 @@ parking_lot = "0.9" [dev-dependencies] test-runtime = { package = "cumulus-test-runtime", path = "../test/runtime" } +test-client = { package = "cumulus-test-client", path = "../test/client" } +substrate-test-client = { git = "https://github.com/paritytech/substrate", branch = "bkchr-cumulus-branch" } +polkadot-executor = { git = "https://github.com/paritytech/polkadot", branch = "bkchr-cumulus-branch" } keyring = { package = "substrate-keyring", git = "https://github.com/paritytech/substrate", branch = "bkchr-cumulus-branch" } +env_logger = "0.7.1" diff --git a/cumulus/collator/src/lib.rs b/cumulus/collator/src/lib.rs index 60a4f112a1..da27ac6a00 100644 --- a/cumulus/collator/src/lib.rs +++ b/cumulus/collator/src/lib.rs @@ -319,10 +319,13 @@ mod tests { use polkadot_primitives::parachain::{ConsolidatedIngress, HeadData, FeeSchedule}; use keyring::Sr25519Keyring; - use sr_primitives::traits::{DigestFor, Header as HeaderT}; + use sr_primitives::{generic::BlockId, traits::{DigestFor, Header as HeaderT}}; use inherents::InherentData; + use substrate_client::error::Result as ClientResult; + use substrate_test_client::{NativeExecutor, WasmExecutionMethod::Interpreted}; use test_runtime::{Block, Header}; + use test_client::{Client, DefaultTestClientBuilderExt, TestClientBuilder, TestClientBuilderExt}; use futures03::future; use futures::Stream; @@ -368,7 +371,7 @@ mod tests { digest, ); - future::ready(Ok((Block::new(header, Vec::new()), None))) + future::ready(Ok((Block::new(header, Vec::new()), Some(Default::default())))) } } @@ -398,11 +401,72 @@ mod tests { Ok(ConsolidatedIngress(Vec::new())) } } -/* + + #[derive(Clone)] + struct DummyPolkadotClient; + + impl cumulus_consensus::PolkadotClient for DummyPolkadotClient { + type Error = Error; + type Finalized = Box> + Send + Unpin>; + + fn finalized_heads(&self, _: ParaId) -> ClientResult { + unimplemented!("Not required in tests") + } + + fn parachain_head_at( + &self, + _: &BlockId, + _: ParaId, + ) -> ClientResult>>{ + unimplemented!("Not required in tests") + } + } + + struct DummySetup; + + impl SetupParachain for DummySetup { + type ProposerFactory = DummyFactory; + type BlockImport = Client; + + fn setup_parachain( + self, + _: P, + _: TaskExecutor, + ) -> Result<(Self::ProposerFactory, Self::BlockImport, InherentDataProviders), String> { + Ok((DummyFactory, TestClientBuilder::new().build(), InherentDataProviders::default())) + } + } + + type BoxFuture = Box + Send>; + + struct DummyFutureExecutor; + + impl futures::future::Executor + for DummyFutureExecutor + { + fn execute( + &self, + _: BoxFuture, + ) -> Result<(), futures::future::ExecuteError> { + unimplemented!("Not required in tests") + } + } + #[test] fn collates_produces_a_block() { - let builder = CollatorBuilder::new(DummyFactory); - let context = builder.build(Arc::new(DummyCollatorNetwork)).expect("Creates parachain context"); + let _ = env_logger::try_init(); + + let builder = CollatorBuilder::new(DummySetup); + let context = builder.build( + Arc::new( + substrate_test_client::TestClientBuilder::<_, _, ()>::default() + .build_with_native_executor( + Some(NativeExecutor::::new(Interpreted, None)), + ).0 + ), + Arc::new(DummyFutureExecutor), + Arc::new(DummyCollatorNetwork), + ).expect("Creates parachain context"); let id = ParaId::from(100); let header = Header::new( @@ -435,5 +499,4 @@ mod tests { assert_eq!(1337, *block.header().number()); } - */ } diff --git a/cumulus/runtime/src/validate_block/tests.rs b/cumulus/runtime/src/validate_block/tests.rs index 690c070948..d64eb32be0 100644 --- a/cumulus/runtime/src/validate_block/tests.rs +++ b/cumulus/runtime/src/validate_block/tests.rs @@ -25,11 +25,14 @@ use test_client::{ runtime::{Block, Transfer, Hash, WASM_BINARY, Header} }; use consensus_common::SelectChain; -use parachain::ValidationParams; +use parachain::{ValidationParams, ValidationResult}; -use codec::Encode; +use codec::{Encode, Decode}; -fn call_validate_block(parent_head: Header, block_data: ParachainBlockData) -> Result<()> { +fn call_validate_block( + parent_head: Header, + block_data: ParachainBlockData, +) -> Result
{ let mut ext = TestExternalities::default(); let mut ext_ext = ext.ext(); let params = ValidationParams { @@ -45,7 +48,9 @@ fn call_validate_block(parent_head: Header, block_data: ParachainBlockData Vec<::Extrinsic> { @@ -110,12 +115,14 @@ fn validate_block_with_no_extrinsics() { let (header, extrinsics) = block.deconstruct(); let block_data = ParachainBlockData::new( - header, + header.clone(), extrinsics, witness_data, witness_data_storage_root ); - call_validate_block(parent_head, block_data).expect("Calls `validate_block`"); + + let res_header = call_validate_block(parent_head, block_data).expect("Calls `validate_block`"); + assert_eq!(header, res_header); } #[test] @@ -127,12 +134,14 @@ fn validate_block_with_extrinsics() { let (header, extrinsics) = block.deconstruct(); let block_data = ParachainBlockData::new( - header, + header.clone(), extrinsics, witness_data, witness_data_storage_root ); - call_validate_block(parent_head, block_data).expect("Calls `validate_block`"); + + let res_header = call_validate_block(parent_head, block_data).expect("Calls `validate_block`"); + assert_eq!(header, res_header); } #[test]