// This file is part of Bizinikiwi.
// Copyright (C) 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 codec::{DecodeAll, Encode};
use futures::prelude::*;
use pezsc_network::{
config::{MultiaddrWithPeerId, Role},
event::Event as NetworkEvent,
service::traits::{Direction, MessageSink, NotificationEvent, NotificationService},
types::ProtocolName,
Multiaddr, NetworkBlock, NetworkEventStream, NetworkPeers, NetworkSyncForkRequest,
ReputationChange,
};
use pezsc_network_common::role::{ObservedRole, Roles};
use pezsc_network_gossip::Validator;
use pezsc_network_sync::{SyncEvent as SyncStreamEvent, SyncEventStream};
use pezsc_network_test::{Block, Hash};
use pezsc_network_types::PeerId;
use pezsc_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender};
use pezsp_consensus_grandpa::AuthorityList;
use pezsp_keyring::Ed25519Keyring;
use pezsp_runtime::traits::NumberFor;
use std::{collections::HashSet, pin::Pin, sync::Arc, task::Poll};
#[derive(Debug)]
pub(crate) enum Event {
WriteNotification(PeerId, Vec),
Report(PeerId, ReputationChange),
}
#[derive(Clone)]
pub(crate) struct TestNetwork {
sender: TracingUnboundedSender,
}
#[async_trait::async_trait]
impl NetworkPeers for TestNetwork {
fn set_authorized_peers(&self, _peers: HashSet) {
unimplemented!();
}
fn set_authorized_only(&self, _reserved_only: bool) {
unimplemented!();
}
fn add_known_address(&self, _peer_id: PeerId, _addr: Multiaddr) {
unimplemented!();
}
fn report_peer(&self, peer_id: PeerId, cost_benefit: ReputationChange) {
let _ = self.sender.unbounded_send(Event::Report(peer_id, cost_benefit));
}
fn peer_reputation(&self, _peer_id: &PeerId) -> i32 {
unimplemented!()
}
fn disconnect_peer(&self, _peer_id: PeerId, _protocol: ProtocolName) {}
fn accept_unreserved_peers(&self) {
unimplemented!();
}
fn deny_unreserved_peers(&self) {
unimplemented!();
}
fn add_reserved_peer(&self, _peer: MultiaddrWithPeerId) -> Result<(), String> {
unimplemented!();
}
fn remove_reserved_peer(&self, _peer_id: PeerId) {
unimplemented!();
}
fn set_reserved_peers(
&self,
_protocol: ProtocolName,
_peers: HashSet,
) -> Result<(), String> {
unimplemented!();
}
fn add_peers_to_reserved_set(
&self,
_protocol: ProtocolName,
_peers: HashSet,
) -> Result<(), String> {
unimplemented!();
}
fn remove_peers_from_reserved_set(
&self,
_protocol: ProtocolName,
_peers: Vec,
) -> Result<(), String> {
unimplemented!();
}
fn sync_num_connected(&self) -> usize {
unimplemented!();
}
fn peer_role(&self, _peer_id: PeerId, handshake: Vec) -> Option {
Roles::decode_all(&mut &handshake[..])
.ok()
.and_then(|role| Some(ObservedRole::from(role)))
}
async fn reserved_peers(&self) -> Result, ()> {
unimplemented!();
}
}
impl NetworkEventStream for TestNetwork {
fn event_stream(
&self,
_name: &'static str,
) -> Pin + Send>> {
futures::stream::pending().boxed()
}
}
impl NetworkBlock> for TestNetwork {
fn announce_block(&self, _: Hash, _data: Option>) {
unimplemented!();
}
fn new_best_block_imported(&self, _hash: Hash, _number: NumberFor) {
unimplemented!();
}
}
impl NetworkSyncForkRequest> for TestNetwork {
fn set_sync_fork_request(&self, _peers: Vec, _hash: Hash, _number: NumberFor) {}
}
impl pezsc_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: &PeerId, data: Vec) {
let _ = self.sender.unbounded_send(Event::WriteNotification(*who, data));
}
fn send_topic(&mut self, _: &PeerId, _: Hash, _: bool) {}
}
#[derive(Clone)]
pub(crate) struct TestSync;
impl SyncEventStream for TestSync {
fn event_stream(
&self,
_name: &'static str,
) -> Pin + Send>> {
Box::pin(futures::stream::pending())
}
}
impl NetworkBlock> for TestSync {
fn announce_block(&self, _hash: Hash, _data: Option>) {
unimplemented!();
}
fn new_best_block_imported(&self, _hash: Hash, _number: NumberFor) {
unimplemented!();
}
}
impl NetworkSyncForkRequest> for TestSync {
fn set_sync_fork_request(&self, _peers: Vec, _hash: Hash, _number: NumberFor) {}
}
#[derive(Debug)]
pub(crate) struct TestNotificationService {
sender: TracingUnboundedSender,
rx: TracingUnboundedReceiver,
}
#[async_trait::async_trait]
impl NotificationService for TestNotificationService {
/// Instruct `Notifications` to open a new substream for `peer`.
async fn open_substream(&mut self, _peer: PeerId) -> Result<(), ()> {
unimplemented!();
}
/// Instruct `Notifications` to close substream for `peer`.
async fn close_substream(&mut self, _peer: PeerId) -> Result<(), ()> {
unimplemented!();
}
/// Send synchronous `notification` to `peer`.
fn send_sync_notification(&mut self, peer: &PeerId, notification: Vec) {
let _ = self.sender.unbounded_send(Event::WriteNotification(*peer, notification));
}
/// Send asynchronous `notification` to `peer`, allowing sender to exercise backpressure.
async fn send_async_notification(
&mut self,
_peer: &PeerId,
_notification: Vec,
) -> Result<(), pezsc_network::error::Error> {
unimplemented!();
}
/// Set handshake for the notification protocol replacing the old handshake.
async fn set_handshake(&mut self, _handshake: Vec) -> Result<(), ()> {
unimplemented!();
}
fn try_set_handshake(&mut self, _handshake: Vec) -> Result<(), ()> {
unimplemented!();
}
/// Get next event from the `Notifications` event stream.
async fn next_event(&mut self) -> Option {
self.rx.next().await
}
fn clone(&mut self) -> Result, ()> {
unimplemented!();
}
fn protocol(&self) -> &ProtocolName {
unimplemented!();
}
fn message_sink(&self, _peer: &PeerId) -> Option> {
unimplemented!();
}
}
pub(crate) struct Tester {
pub(crate) net_handle: super::NetworkBridge,
gossip_validator: Arc>,
pub(crate) events: TracingUnboundedReceiver,
pub(crate) notification_tx: TracingUnboundedSender,
}
impl Tester {
fn filter_network_events(self, mut pred: F) -> impl Future