Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
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
32 changes: 24 additions & 8 deletions core/network/src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,20 +117,23 @@ impl NetworkSpecialization<Block> for DummySpecialization {

pub type PeersClient = client::Client<test_client::Backend, test_client::Executor, Block, test_client::runtime::RuntimeApi>;

#[derive(Clone)]
/// A Link that can wait for a block to have been imported.
pub struct TestLink<S: NetworkSpecialization<Block> + Clone> {
pub struct TestLink<S: NetworkSpecialization<Block>> {
import_done: Arc<AtomicBool>,
hash: Arc<Mutex<Hash>>,
link: NetworkLink<Block, S>,
protocol_sender: Sender<ProtocolMsg<Block, S>>,
network_sender: NetworkChan<Block>,
}

impl<S: NetworkSpecialization<Block> + Clone> TestLink<S> {
impl<S: NetworkSpecialization<Block>> TestLink<S> {
fn new(
protocol_sender: Sender<ProtocolMsg<Block, S>>,
network_sender: NetworkChan<Block>
) -> TestLink<S> {
TestLink {
protocol_sender: protocol_sender.clone(),
network_sender: network_sender.clone(),
import_done: Arc::new(AtomicBool::new(false)),
hash: Arc::new(Mutex::new(Default::default())),
link: NetworkLink {
Expand All @@ -140,6 +143,19 @@ impl<S: NetworkSpecialization<Block> + Clone> TestLink<S> {
}
}

fn clone_link(&self) -> Self {
TestLink {
protocol_sender: self.protocol_sender.clone(),
network_sender: self.network_sender.clone(),
import_done: self.import_done.clone(),
hash: self.hash.clone(),
link: NetworkLink {
protocol_sender: self.protocol_sender.clone(),
network_sender: self.network_sender.clone(),
}
}
}

/// Set the hash which will be awaited for import.
fn with_hash(&self, hash: Hash) {
self.import_done.store(false, Ordering::SeqCst);
Expand All @@ -154,7 +170,7 @@ impl<S: NetworkSpecialization<Block> + Clone> TestLink<S> {
}
}

impl<S: NetworkSpecialization<Block> + Clone> Link<Block> for TestLink<S> {
impl<S: NetworkSpecialization<Block>> Link<Block> for TestLink<S> {
fn block_imported(&self, hash: &Hash, number: NumberFor<Block>) {
if hash == &*self.hash.lock() {
self.import_done.store(true, Ordering::SeqCst);
Expand Down Expand Up @@ -187,7 +203,7 @@ impl<S: NetworkSpecialization<Block> + Clone> Link<Block> for TestLink<S> {
}
}

pub struct Peer<D, S: NetworkSpecialization<Block> + Clone> {
pub struct Peer<D, S: NetworkSpecialization<Block>> {
pub is_offline: Arc<AtomicBool>,
pub is_major_syncing: Arc<AtomicBool>,
pub peers: Arc<RwLock<HashMap<PeerId, ConnectedPeer<Block>>>>,
Expand All @@ -203,7 +219,7 @@ pub struct Peer<D, S: NetworkSpecialization<Block> + Clone> {
finalized_hash: Mutex<Option<H256>>,
}

impl<D, S: NetworkSpecialization<Block> + Clone> Peer<D, S> {
impl<D, S: NetworkSpecialization<Block>> Peer<D, S> {
fn new(
is_offline: Arc<AtomicBool>,
is_major_syncing: Arc<AtomicBool>,
Expand All @@ -218,7 +234,7 @@ impl<D, S: NetworkSpecialization<Block> + Clone> Peer<D, S> {
) -> Self {
let network_port = Arc::new(Mutex::new(network_port));
let network_link = TestLink::new(protocol_sender.clone(), network_sender.clone());
import_queue.start(Box::new(network_link.clone())).expect("Test ImportQueue always starts");
import_queue.start(Box::new(network_link.clone_link())).expect("Test ImportQueue always starts");
Peer {
is_offline,
is_major_syncing,
Expand Down Expand Up @@ -518,7 +534,7 @@ impl SpecializationFactory for DummySpecialization {
}

pub trait TestNetFactory: Sized {
type Specialization: NetworkSpecialization<Block> + Clone + SpecializationFactory;
type Specialization: NetworkSpecialization<Block> + SpecializationFactory;
type Verifier: 'static + Verifier<Block>;
type PeerData: Default;

Expand Down