// This file is part of Substrate.
// Copyright (C) 2019-2022 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
//! Tests for the communication portion of the GRANDPA crate.
use super::{
gossip::{self, GossipValidator},
Round, SetId, VoterSet,
};
use crate::{communication::GRANDPA_PROTOCOL_NAME, environment::SharedVoterSetState};
use futures::prelude::*;
use parity_scale_codec::Encode;
use sc_network::{config::Role, Event as NetworkEvent, ObservedRole, PeerId};
use sc_network_gossip::Validator;
use sc_network_test::{Block, Hash};
use sc_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender};
use sp_finality_grandpa::AuthorityList;
use sp_keyring::Ed25519Keyring;
use sp_runtime::traits::NumberFor;
use std::{
borrow::Cow,
pin::Pin,
sync::Arc,
task::{Context, Poll},
};
#[derive(Debug)]
pub(crate) enum Event {
EventStream(TracingUnboundedSender),
WriteNotification(sc_network::PeerId, Vec),
Report(sc_network::PeerId, sc_network::ReputationChange),
Announce(Hash),
}
#[derive(Clone)]
pub(crate) struct TestNetwork {
sender: TracingUnboundedSender,
}
impl sc_network_gossip::Network for TestNetwork {
fn event_stream(&self) -> Pin + Send>> {
let (tx, rx) = tracing_unbounded("test");
let _ = self.sender.unbounded_send(Event::EventStream(tx));
Box::pin(rx)
}
fn report_peer(&self, who: sc_network::PeerId, cost_benefit: sc_network::ReputationChange) {
let _ = self.sender.unbounded_send(Event::Report(who, cost_benefit));
}
fn add_set_reserved(&self, _: PeerId, _: Cow<'static, str>) {}
fn remove_set_reserved(&self, _: PeerId, _: Cow<'static, str>) {}
fn disconnect_peer(&self, _: PeerId, _: Cow<'static, str>) {}
fn write_notification(&self, who: PeerId, _: Cow<'static, str>, message: Vec) {
let _ = self.sender.unbounded_send(Event::WriteNotification(who, message));
}
fn announce(&self, block: Hash, _associated_data: Option>) {
let _ = self.sender.unbounded_send(Event::Announce(block));
}
}
impl super::Network for TestNetwork {
fn set_sync_fork_request(
&self,
_peers: Vec,
_hash: Hash,
_number: NumberFor,
) {
}
}
impl sc_network_gossip::ValidatorContext for TestNetwork {
fn broadcast_topic(&mut self, _: Hash, _: bool) {}
fn broadcast_message(&mut self, _: Hash, _: Vec, _: bool) {}
fn send_message(&mut self, who: &sc_network::PeerId, data: Vec) {
>::write_notification(
self,
who.clone(),
GRANDPA_PROTOCOL_NAME.into(),
data,
);
}
fn send_topic(&mut self, _: &sc_network::PeerId, _: Hash, _: bool) {}
}
pub(crate) struct Tester {
pub(crate) net_handle: super::NetworkBridge,
gossip_validator: Arc>,
pub(crate) events: TracingUnboundedReceiver,
}
impl Tester {
fn filter_network_events(self, mut pred: F) -> impl Future