diff --git a/Cargo.lock b/Cargo.lock index 1a6fa2e38f7f..bb3b2fb76fa5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1210,6 +1210,15 @@ dependencies = [ "parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "kvdb-memorydb" +version = "0.1.0" +source = "git+https://github.com/paritytech/parity-common?rev=b0317f649ab2c665b7987b8475878fc4d2e1f81d#b0317f649ab2c665b7987b8475878fc4d2e1f81d" +dependencies = [ + "kvdb 0.1.0 (git+https://github.com/paritytech/parity-common?rev=b0317f649ab2c665b7987b8475878fc4d2e1f81d)", + "parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "kvdb-rocksdb" version = "0.1.4" @@ -2237,9 +2246,11 @@ dependencies = [ "slice-group-by 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", "sr-primitives 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", "substrate-client 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", + "substrate-consensus-common 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", "substrate-keyring 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", "substrate-network 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", "substrate-primitives 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", + "substrate-test-client 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", "tokio 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -3576,6 +3587,7 @@ source = "git+https://github.com/paritytech/substrate?branch=polkadot-master#724 dependencies = [ "hash-db 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "kvdb 0.1.0 (git+https://github.com/paritytech/parity-common?rev=b0317f649ab2c665b7987b8475878fc4d2e1f81d)", + "kvdb-memorydb 0.1.0 (git+https://github.com/paritytech/parity-common?rev=b0317f649ab2c665b7987b8475878fc4d2e1f81d)", "kvdb-rocksdb 0.1.4 (git+https://github.com/paritytech/parity-common?rev=b0317f649ab2c665b7987b8475878fc4d2e1f81d)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "lru-cache 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3784,9 +3796,11 @@ dependencies = [ "sr-primitives 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", "substrate-client 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", "substrate-consensus-common 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", + "substrate-keyring 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", "substrate-network-libp2p 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", "substrate-peerset 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", "substrate-primitives 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", + "substrate-test-client 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", "tokio 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -4031,6 +4045,49 @@ dependencies = [ "ws 0.7.9 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "substrate-test-client" +version = "0.1.0" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-master#72409f9fd1dcf25b094dc09a31a9f831dd56411a" +dependencies = [ + "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", + "parity-codec 3.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "sr-primitives 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", + "substrate-client 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", + "substrate-client-db 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", + "substrate-consensus-common 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", + "substrate-executor 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", + "substrate-keyring 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", + "substrate-primitives 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", + "substrate-state-machine 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", + "substrate-test-runtime 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", +] + +[[package]] +name = "substrate-test-runtime" +version = "0.1.0" +source = "git+https://github.com/paritytech/substrate?branch=polkadot-master#72409f9fd1dcf25b094dc09a31a9f831dd56411a" +dependencies = [ + "cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", + "hex-literal 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "parity-codec 3.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", + "sr-io 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", + "sr-primitives 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", + "sr-std 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", + "sr-version 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", + "srml-executive 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", + "srml-support 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", + "substrate-client 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", + "substrate-consensus-aura-primitives 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", + "substrate-inherents 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", + "substrate-keyring 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", + "substrate-offchain-primitives 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", + "substrate-primitives 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)", +] + [[package]] name = "substrate-transaction-graph" version = "0.1.0" @@ -4895,6 +4952,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum kvdb 0.1.0 (git+https://github.com/paritytech/parity-common?rev=616b40150ded71f57f650067fcbc5c99d7c343e6)" = "" "checksum kvdb 0.1.0 (git+https://github.com/paritytech/parity-common?rev=b0317f649ab2c665b7987b8475878fc4d2e1f81d)" = "" "checksum kvdb-memorydb 0.1.0 (git+https://github.com/paritytech/parity-common?rev=616b40150ded71f57f650067fcbc5c99d7c343e6)" = "" +"checksum kvdb-memorydb 0.1.0 (git+https://github.com/paritytech/parity-common?rev=b0317f649ab2c665b7987b8475878fc4d2e1f81d)" = "" "checksum kvdb-rocksdb 0.1.4 (git+https://github.com/paritytech/parity-common?rev=616b40150ded71f57f650067fcbc5c99d7c343e6)" = "" "checksum kvdb-rocksdb 0.1.4 (git+https://github.com/paritytech/parity-common?rev=b0317f649ab2c665b7987b8475878fc4d2e1f81d)" = "" "checksum language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a91d884b6667cd606bb5a69aa0c99ba811a115fc68915e7056ec08a46e93199a" @@ -5116,6 +5174,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum substrate-state-db 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)" = "" "checksum substrate-state-machine 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)" = "" "checksum substrate-telemetry 0.3.1 (git+https://github.com/paritytech/substrate?branch=polkadot-master)" = "" +"checksum substrate-test-client 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)" = "" +"checksum substrate-test-runtime 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)" = "" "checksum substrate-transaction-graph 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)" = "" "checksum substrate-transaction-pool 0.1.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)" = "" "checksum substrate-trie 0.4.0 (git+https://github.com/paritytech/substrate?branch=polkadot-master)" = "" diff --git a/network/Cargo.toml b/network/Cargo.toml index 2a8b58154ef7..0b9ace123df9 100644 --- a/network/Cargo.toml +++ b/network/Cargo.toml @@ -12,8 +12,10 @@ polkadot-validation = { path = "../validation" } polkadot-primitives = { path = "../primitives" } parity-codec = "3.0" parity-codec-derive = "3.0" -substrate-network = { git = "https://github.com/paritytech/substrate", branch = "polkadot-master" } +substrate-consensus-common = { git = "https://github.com/paritytech/substrate", branch = "polkadot-master" } +substrate-network = { git = "https://github.com/paritytech/substrate", branch = "polkadot-master", features = ["test-helpers"] } substrate-primitives = { git = "https://github.com/paritytech/substrate", branch = "polkadot-master" } +substrate-test-client = { git = "https://github.com/paritytech/substrate", branch = "polkadot-master" } sr-primitives = { git = "https://github.com/paritytech/substrate", branch = "polkadot-master" } futures = "0.1" tokio = "0.1.7" @@ -23,4 +25,5 @@ exit-future = "0.1.4" [dev-dependencies] substrate-client = { git = "https://github.com/paritytech/substrate", branch = "polkadot-master" } + substrate-keyring = { git = "https://github.com/paritytech/substrate", branch = "polkadot-master" } diff --git a/network/src/lib.rs b/network/src/lib.rs index 8c80114bca8f..32610efaca3c 100644 --- a/network/src/lib.rs +++ b/network/src/lib.rs @@ -22,6 +22,7 @@ extern crate parity_codec as codec; extern crate substrate_network; extern crate substrate_primitives; +extern crate substrate_consensus_common as substrate_consensus; extern crate sr_primitives; extern crate polkadot_validation; @@ -44,6 +45,9 @@ extern crate parity_codec_derive; #[cfg(test)] extern crate substrate_client; +#[cfg(test)] +extern crate substrate_test_client; + #[cfg(test)] extern crate substrate_keyring; @@ -55,7 +59,9 @@ pub mod gossip; use codec::{Decode, Encode}; use futures::sync::oneshot; -use polkadot_primitives::{Block, SessionKey, Hash, Header}; +#[cfg(not(test))] +use polkadot_primitives::{Block, Hash, Header}; +use polkadot_primitives::SessionKey; use polkadot_primitives::parachain::{Id as ParaId, CollatorId, BlockData, CandidateReceipt, Collation}; use substrate_network::{PeerId, RequestId, Context, Severity}; use substrate_network::{message, generic_message}; @@ -70,6 +76,9 @@ use std::collections::{HashMap, HashSet}; #[cfg(test)] mod tests; +#[cfg(test)] +use substrate_test_client::runtime::{Block, Hash, Header}; + /// Polkadot protocol id. pub const DOT_PROTOCOL_ID: ::substrate_network::ProtocolId = *b"dot"; diff --git a/network/src/tests/mod.rs b/network/src/tests/mod.rs index 5e6f0ebea147..8ed50d180560 100644 --- a/network/src/tests/mod.rs +++ b/network/src/tests/mod.rs @@ -20,20 +20,98 @@ use super::{PolkadotProtocol, Status, Message, FullStatus}; use validation::SessionParams; use polkadot_validation::GenericStatement; -use polkadot_primitives::{Block, Hash, SessionKey}; +use polkadot_primitives::SessionKey; use polkadot_primitives::parachain::{CandidateReceipt, HeadData, BlockData, CollatorId, ValidatorId}; +use sr_primitives::Justification; +use sr_primitives::generic::BlockId; +use sr_primitives::traits::NumberFor; +use substrate_primitives::H256; use substrate_primitives::crypto::UncheckedInto; use codec::Encode; +use substrate_consensus::import_queue::{SharedBlockImport, SharedJustificationImport, Verifier}; +use substrate_consensus::{Error as ConsensusError, ErrorKind as ConsensusErrorKind, JustificationImport}; use substrate_network::{ - Severity, PeerId, PeerInfo, ClientHandle, Context, config::Roles, + Severity, PeerId, PeerInfo, ClientHandle, Context, config::ProtocolConfig, config::Roles, message::Message as SubstrateMessage, specialization::NetworkSpecialization, generic_message::Message as GenericMessage }; +use substrate_network::test::{ + Block, DummySpecialization, Hash, TestNet, TestNetFactory, + PassThroughVerifier, Peer, PeersClient, SpecializationFactory +}; use futures::Future; +use std::sync::Arc; mod validation; +impl SpecializationFactory for PolkadotProtocol { + fn create() -> PolkadotProtocol { + PolkadotProtocol::new(None) + } +} + +pub struct ForceFinalized(Arc); + +impl JustificationImport for ForceFinalized { + type Error = ConsensusError; + + fn import_justification( + &self, + hash: H256, + _number: NumberFor, + justification: Justification, + ) -> Result<(), Self::Error> { + self.0.finalize_block(BlockId::Hash(hash), Some(justification), true) + .map_err(|_| ConsensusErrorKind::InvalidJustification.into()) + } +} + +struct PolkadotTestNet(TestNet); + +impl TestNetFactory for PolkadotTestNet { + type Specialization = PolkadotProtocol; + type Verifier = PassThroughVerifier; + type PeerData = (); + + fn from_config(config: &ProtocolConfig) -> Self { + PolkadotTestNet(TestNet::from_config(config)) + } + + fn make_verifier(&self, client: Arc, config: &ProtocolConfig) + -> Arc + { + self.0.make_verifier(client, config) + } + + fn peer(&self, i: usize) -> &Peer { + self.0.peer(i) + } + + fn peers(&self) -> &Vec>> { + self.0.peers() + } + + fn mut_peers>>)>(&mut self, closure: F ) { + self.0.mut_peers(closure) + } + + fn started(&self) -> bool { + self.0.started() + } + + fn set_started(&mut self, new: bool) { + self.0.set_started(new) + } + + fn make_block_import(&self, client: Arc) + -> (SharedBlockImport, Option>, Self::PeerData) + { + (client.clone(), Some(Arc::new(ForceFinalized(client))), Default::default()) + } +} + + #[derive(Default)] struct TestContext { disabled: Vec,