Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Closed
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
60 changes: 60 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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" }
11 changes: 10 additions & 1 deletion network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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};
Expand All @@ -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";

Expand Down
82 changes: 80 additions & 2 deletions network/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PeersClient>);

impl JustificationImport<Block> for ForceFinalized {
type Error = ConsensusError;

fn import_justification(
Copy link
Copy Markdown
Contributor

@ltfschoen ltfschoen Jul 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

&self,
hash: H256,
_number: NumberFor<Block>,
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<PeersClient>, config: &ProtocolConfig)
-> Arc<Self::Verifier>
{
self.0.make_verifier(client, config)
}

fn peer(&self, i: usize) -> &Peer<Self::PeerData, Self::Specialization> {
self.0.peer(i)
}

fn peers(&self) -> &Vec<Arc<Peer<Self::PeerData, Self::Specialization>>> {
self.0.peers()
}

fn mut_peers<F: FnOnce(&mut Vec<Arc<Peer<Self::PeerData, Self::Specialization>>>)>(&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<PeersClient>)
-> (SharedBlockImport<Block>, Option<SharedJustificationImport<Block>>, Self::PeerData)
{
(client.clone(), Some(Arc::new(ForceFinalized(client))), Default::default())
}
}


#[derive(Default)]
struct TestContext {
disabled: Vec<PeerId>,
Expand Down