Rework the event system of sc-network (#1370)

This commit introduces a new concept called `NotificationService` which
allows Polkadot protocols to communicate with the underlying
notification protocol implementation directly, without routing events
through `NetworkWorker`. This implies that each protocol has its own
service which it uses to communicate with remote peers and that each
`NotificationService` is unique with respect to the underlying
notification protocol, meaning `NotificationService` for the transaction
protocol can only be used to send and receive transaction-related
notifications.

The `NotificationService` concept introduces two additional benefits:
  * allow protocols to start using custom handshakes
  * allow protocols to accept/reject inbound peers

Previously the validation of inbound connections was solely the
responsibility of `ProtocolController`. This caused issues with light
peers and `SyncingEngine` as `ProtocolController` would accept more
peers than `SyncingEngine` could accept which caused peers to have
differing views of their own states. `SyncingEngine` would reject excess
peers but these rejections were not properly communicated to those peers
causing them to assume that they were accepted.

With `NotificationService`, the local handshake is not sent to remote
peer if peer is rejected which allows it to detect that it was rejected.

This commit also deprecates the use of `NetworkEventStream` for all
notification-related events and going forward only DHT events are
provided through `NetworkEventStream`. If protocols wish to follow each
other's events, they must introduce additional abtractions, as is done
for GRANDPA and transactions protocols by following the syncing protocol
through `SyncEventStream`.

Fixes https://github.com/paritytech/polkadot-sdk/issues/512
Fixes https://github.com/paritytech/polkadot-sdk/issues/514
Fixes https://github.com/paritytech/polkadot-sdk/issues/515
Fixes https://github.com/paritytech/polkadot-sdk/issues/554
Fixes https://github.com/paritytech/polkadot-sdk/issues/556

---
These changes are transferred from
https://github.com/paritytech/substrate/pull/14197 but there are no
functional changes compared to that PR

---------

Co-authored-by: Dmitry Markin <dmitry@markin.tech>
Co-authored-by: Alexandru Vasile <60601340+lexnv@users.noreply.github.com>
This commit is contained in:
Aaro Altonen
2023-11-28 20:18:52 +02:00
committed by GitHub
parent ec3a61ed86
commit e71c484d5b
102 changed files with 5694 additions and 2603 deletions
+173 -93
View File
@@ -21,7 +21,11 @@ use crate::{
Network, Syncing, Validator,
};
use sc_network::{event::Event, types::ProtocolName, ReputationChange};
use sc_network::{
service::traits::{NotificationEvent, ValidationResult},
types::ProtocolName,
NotificationService, ReputationChange,
};
use sc_network_sync::SyncEvent;
use futures::{
@@ -48,10 +52,10 @@ pub struct GossipEngine<B: BlockT> {
periodic_maintenance_interval: futures_timer::Delay,
protocol: ProtocolName,
/// Incoming events from the network.
network_event_stream: Pin<Box<dyn Stream<Item = Event> + Send>>,
/// Incoming events from the syncing service.
sync_event_stream: Pin<Box<dyn Stream<Item = SyncEvent> + Send>>,
/// Handle for polling notification-related events.
notification_service: Box<dyn NotificationService>,
/// Outgoing events to the consumer.
message_sinks: HashMap<B::Hash, Vec<Sender<TopicNotification>>>,
/// Buffered messages (see [`ForwardingState`]).
@@ -81,6 +85,7 @@ impl<B: BlockT> GossipEngine<B> {
pub fn new<N, S>(
network: N,
sync: S,
notification_service: Box<dyn NotificationService>,
protocol: impl Into<ProtocolName>,
validator: Arc<dyn Validator<B>>,
metrics_registry: Option<&Registry>,
@@ -91,17 +96,16 @@ impl<B: BlockT> GossipEngine<B> {
S: Syncing<B> + Send + Clone + 'static,
{
let protocol = protocol.into();
let network_event_stream = network.event_stream("network-gossip");
let sync_event_stream = sync.event_stream("network-gossip");
GossipEngine {
state_machine: ConsensusGossip::new(validator, protocol.clone(), metrics_registry),
network: Box::new(network),
sync: Box::new(sync),
notification_service,
periodic_maintenance_interval: futures_timer::Delay::new(PERIODIC_MAINTENANCE_INTERVAL),
protocol,
network_event_stream,
sync_event_stream,
message_sinks: HashMap::new(),
forwarding_state: ForwardingState::Idle,
@@ -125,7 +129,7 @@ impl<B: BlockT> GossipEngine<B> {
/// Broadcast all messages with given topic.
pub fn broadcast_topic(&mut self, topic: B::Hash, force: bool) {
self.state_machine.broadcast_topic(&mut *self.network, topic, force);
self.state_machine.broadcast_topic(&mut self.notification_service, topic, force);
}
/// Get data of valid, incoming messages for a topic (but might have expired meanwhile).
@@ -150,19 +154,21 @@ impl<B: BlockT> GossipEngine<B> {
/// Send all messages with given topic to a peer.
pub fn send_topic(&mut self, who: &PeerId, topic: B::Hash, force: bool) {
self.state_machine.send_topic(&mut *self.network, who, topic, force)
self.state_machine.send_topic(&mut self.notification_service, who, topic, force)
}
/// Multicast a message to all peers.
pub fn gossip_message(&mut self, topic: B::Hash, message: Vec<u8>, force: bool) {
self.state_machine.multicast(&mut *self.network, topic, message, force)
self.state_machine
.multicast(&mut self.notification_service, topic, message, force)
}
/// Send addressed message to the given peers. The message is not kept or multicast
/// later on.
pub fn send_message(&mut self, who: Vec<PeerId>, data: Vec<u8>) {
for who in &who {
self.state_machine.send_message(&mut *self.network, who, data.clone());
self.state_machine
.send_message(&mut self.notification_service, who, data.clone());
}
}
@@ -173,6 +179,11 @@ impl<B: BlockT> GossipEngine<B> {
pub fn announce(&self, block: B::Hash, associated_data: Option<Vec<u8>>) {
self.sync.announce_block(block, associated_data);
}
/// Consume [`GossipEngine`] and return the notification service.
pub fn take_notification_service(self) -> Box<dyn NotificationService> {
self.notification_service
}
}
impl<B: BlockT> Future for GossipEngine<B> {
@@ -184,46 +195,56 @@ impl<B: BlockT> Future for GossipEngine<B> {
'outer: loop {
match &mut this.forwarding_state {
ForwardingState::Idle => {
let net_event_stream = this.network_event_stream.poll_next_unpin(cx);
let next_notification_event =
this.notification_service.next_event().poll_unpin(cx);
let sync_event_stream = this.sync_event_stream.poll_next_unpin(cx);
if net_event_stream.is_pending() && sync_event_stream.is_pending() {
if next_notification_event.is_pending() && sync_event_stream.is_pending() {
break
}
match net_event_stream {
match next_notification_event {
Poll::Ready(Some(event)) => match event {
Event::NotificationStreamOpened { remote, protocol, role, .. } =>
if protocol == this.protocol {
this.state_machine.new_peer(&mut *this.network, remote, role);
},
Event::NotificationStreamClosed { remote, protocol } => {
if protocol == this.protocol {
this.state_machine
.peer_disconnected(&mut *this.network, remote);
}
NotificationEvent::ValidateInboundSubstream {
peer,
handshake,
result_tx,
..
} => {
// only accept peers whose role can be determined
let result = this
.network
.peer_role(peer, handshake)
.map_or(ValidationResult::Reject, |_| ValidationResult::Accept);
let _ = result_tx.send(result);
},
Event::NotificationsReceived { remote, messages } => {
let messages = messages
.into_iter()
.filter_map(|(engine, data)| {
if engine == this.protocol {
Some(data.to_vec())
} else {
None
}
})
.collect();
NotificationEvent::NotificationStreamOpened {
peer, handshake, ..
} => {
let Some(role) = this.network.peer_role(peer, handshake) else {
log::debug!(target: "gossip", "role for {peer} couldn't be determined");
continue
};
this.state_machine.new_peer(
&mut this.notification_service,
peer,
role,
);
},
NotificationEvent::NotificationStreamClosed { peer } => {
this.state_machine
.peer_disconnected(&mut this.notification_service, peer);
},
NotificationEvent::NotificationReceived { peer, notification } => {
let to_forward = this.state_machine.on_incoming(
&mut *this.network,
remote,
messages,
&mut this.notification_service,
peer,
vec![notification],
);
this.forwarding_state = ForwardingState::Busy(to_forward.into());
},
Event::Dht(_) => {},
},
// The network event stream closed. Do the same for [`GossipValidator`].
Poll::Ready(None) => {
@@ -306,7 +327,7 @@ impl<B: BlockT> Future for GossipEngine<B> {
while let Poll::Ready(()) = this.periodic_maintenance_interval.poll_unpin(cx) {
this.periodic_maintenance_interval.reset(PERIODIC_MAINTENANCE_INTERVAL);
this.state_machine.tick(&mut *this.network);
this.state_machine.tick(&mut this.notification_service);
this.message_sinks.retain(|_, sinks| {
sinks.retain(|sink| !sink.is_closed());
@@ -328,15 +349,19 @@ impl<B: BlockT> futures::future::FusedFuture for GossipEngine<B> {
mod tests {
use super::*;
use crate::{multiaddr::Multiaddr, ValidationResult, ValidatorContext};
use codec::{DecodeAll, Encode};
use futures::{
channel::mpsc::{unbounded, UnboundedSender},
channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender},
executor::{block_on, block_on_stream},
future::poll_fn,
};
use quickcheck::{Arbitrary, Gen, QuickCheck};
use sc_network::{
config::MultiaddrWithPeerId, NetworkBlock, NetworkEventStream, NetworkNotification,
NetworkPeers, NotificationSenderError, NotificationSenderT as NotificationSender,
config::MultiaddrWithPeerId,
service::traits::{Direction, MessageSink, NotificationEvent},
Event, NetworkBlock, NetworkEventStream, NetworkNotification, NetworkPeers,
NotificationSenderError, NotificationSenderT as NotificationSender, NotificationService,
Roles,
};
use sc_network_common::role::ObservedRole;
use sc_network_sync::SyncEventStream;
@@ -351,14 +376,10 @@ mod tests {
use substrate_test_runtime_client::runtime::Block;
#[derive(Clone, Default)]
struct TestNetwork {
inner: Arc<Mutex<TestNetworkInner>>,
}
struct TestNetwork {}
#[derive(Clone, Default)]
struct TestNetworkInner {
event_senders: Vec<UnboundedSender<Event>>,
}
struct TestNetworkInner {}
impl NetworkPeers for TestNetwork {
fn set_authorized_peers(&self, _peers: HashSet<PeerId>) {
@@ -422,14 +443,17 @@ mod tests {
fn sync_num_connected(&self) -> usize {
unimplemented!();
}
fn peer_role(&self, _peer_id: PeerId, handshake: Vec<u8>) -> Option<ObservedRole> {
Roles::decode_all(&mut &handshake[..])
.ok()
.and_then(|role| Some(ObservedRole::from(role)))
}
}
impl NetworkEventStream for TestNetwork {
fn event_stream(&self, _name: &'static str) -> Pin<Box<dyn Stream<Item = Event> + Send>> {
let (tx, rx) = unbounded();
self.inner.lock().unwrap().event_senders.push(tx);
Box::pin(rx)
unimplemented!();
}
}
@@ -501,6 +525,58 @@ mod tests {
}
}
#[derive(Debug)]
pub(crate) struct TestNotificationService {
rx: UnboundedReceiver<NotificationEvent>,
}
#[async_trait::async_trait]
impl sc_network::service::traits::NotificationService for TestNotificationService {
async fn open_substream(&mut self, _peer: PeerId) -> Result<(), ()> {
unimplemented!();
}
async fn close_substream(&mut self, _peer: PeerId) -> Result<(), ()> {
unimplemented!();
}
fn send_sync_notification(&self, _peer: &PeerId, _notification: Vec<u8>) {
unimplemented!();
}
async fn send_async_notification(
&self,
_peer: &PeerId,
_notification: Vec<u8>,
) -> Result<(), sc_network::error::Error> {
unimplemented!();
}
async fn set_handshake(&mut self, _handshake: Vec<u8>) -> Result<(), ()> {
unimplemented!();
}
fn try_set_handshake(&mut self, _handshake: Vec<u8>) -> Result<(), ()> {
unimplemented!();
}
async fn next_event(&mut self) -> Option<NotificationEvent> {
self.rx.next().await
}
fn clone(&mut self) -> Result<Box<dyn NotificationService>, ()> {
unimplemented!();
}
fn protocol(&self) -> &ProtocolName {
unimplemented!();
}
fn message_sink(&self, _peer: &PeerId) -> Option<Box<dyn MessageSink>> {
unimplemented!();
}
}
struct AllowAll;
impl Validator<Block> for AllowAll {
fn validate(
@@ -521,16 +597,19 @@ mod tests {
fn returns_when_network_event_stream_closes() {
let network = TestNetwork::default();
let sync = Arc::new(TestSync::default());
let (tx, rx) = unbounded();
let notification_service = Box::new(TestNotificationService { rx });
let mut gossip_engine = GossipEngine::<Block>::new(
network.clone(),
sync,
notification_service,
"/my_protocol",
Arc::new(AllowAll {}),
None,
);
// Drop network event stream sender side.
drop(network.inner.lock().unwrap().event_senders.pop());
// drop notification service sender side.
drop(tx);
block_on(poll_fn(move |ctx| {
if let Poll::Pending = gossip_engine.poll_unpin(ctx) {
@@ -550,42 +629,37 @@ mod tests {
let remote_peer = PeerId::random();
let network = TestNetwork::default();
let sync = Arc::new(TestSync::default());
let (mut tx, rx) = unbounded();
let notification_service = Box::new(TestNotificationService { rx });
let mut gossip_engine = GossipEngine::<Block>::new(
network.clone(),
sync.clone(),
notification_service,
protocol.clone(),
Arc::new(AllowAll {}),
None,
);
let mut event_sender = network.inner.lock().unwrap().event_senders.pop().unwrap();
// Register the remote peer.
event_sender
.start_send(Event::NotificationStreamOpened {
remote: remote_peer,
protocol: protocol.clone(),
negotiated_fallback: None,
role: ObservedRole::Authority,
received_handshake: vec![],
})
.expect("Event stream is unbounded; qed.");
tx.send(NotificationEvent::NotificationStreamOpened {
peer: remote_peer,
direction: Direction::Inbound,
negotiated_fallback: None,
handshake: Roles::FULL.encode(),
})
.await
.unwrap();
let messages = vec![vec![1], vec![2]];
let events = messages
.iter()
.cloned()
.map(|m| Event::NotificationsReceived {
remote: remote_peer,
messages: vec![(protocol.clone(), m.into())],
})
.collect::<Vec<_>>();
// Send first event before subscribing.
event_sender
.start_send(events[0].clone())
.expect("Event stream is unbounded; qed.");
tx.send(NotificationEvent::NotificationReceived {
peer: remote_peer,
notification: messages[0].clone().into(),
})
.await
.unwrap();
let mut subscribers = vec![];
for _ in 0..2 {
@@ -593,9 +667,12 @@ mod tests {
}
// Send second event after subscribing.
event_sender
.start_send(events[1].clone())
.expect("Event stream is unbounded; qed.");
tx.send(NotificationEvent::NotificationReceived {
peer: remote_peer,
notification: messages[1].clone().into(),
})
.await
.unwrap();
tokio::spawn(gossip_engine);
@@ -672,6 +749,8 @@ mod tests {
let remote_peer = PeerId::random();
let network = TestNetwork::default();
let sync = Arc::new(TestSync::default());
let (mut tx, rx) = unbounded();
let notification_service = Box::new(TestNotificationService { rx });
let num_channels_per_topic = channels.iter().fold(
HashMap::new(),
@@ -699,6 +778,7 @@ mod tests {
let mut gossip_engine = GossipEngine::<Block>::new(
network.clone(),
sync.clone(),
notification_service,
protocol.clone(),
Arc::new(TestValidator {}),
None,
@@ -724,22 +804,18 @@ mod tests {
}
}
let mut event_sender = network.inner.lock().unwrap().event_senders.pop().unwrap();
// Register the remote peer.
event_sender
.start_send(Event::NotificationStreamOpened {
remote: remote_peer,
protocol: protocol.clone(),
negotiated_fallback: None,
role: ObservedRole::Authority,
received_handshake: vec![],
})
.expect("Event stream is unbounded; qed.");
tx.start_send(NotificationEvent::NotificationStreamOpened {
peer: remote_peer,
direction: Direction::Inbound,
negotiated_fallback: None,
handshake: Roles::FULL.encode(),
})
.unwrap();
// Send messages into the network event stream.
for (i_notification, messages) in notifications.iter().enumerate() {
let messages = messages
let messages: Vec<Vec<u8>> = messages
.into_iter()
.enumerate()
.map(|(i_message, Message { topic })| {
@@ -752,13 +828,17 @@ mod tests {
message.push(i_notification.try_into().unwrap());
message.push(i_message.try_into().unwrap());
(protocol.clone(), message.into())
message.into()
})
.collect();
event_sender
.start_send(Event::NotificationsReceived { remote: remote_peer, messages })
.expect("Event stream is unbounded; qed.");
for message in messages {
tx.start_send(NotificationEvent::NotificationReceived {
peer: remote_peer,
notification: message,
})
.unwrap();
}
}
let mut received_msgs_per_topic_all_chan = HashMap::<H256, _>::new();
@@ -23,7 +23,7 @@ use libp2p::PeerId;
use schnellru::{ByLength, LruMap};
use prometheus_endpoint::{register, Counter, PrometheusError, Registry, U64};
use sc_network::types::ProtocolName;
use sc_network::{types::ProtocolName, NotificationService};
use sc_network_common::role::ObservedRole;
use sp_runtime::traits::{Block as BlockT, Hash, HashingFor};
use std::{collections::HashMap, iter, sync::Arc, time, time::Instant};
@@ -74,33 +74,33 @@ struct MessageEntry<B: BlockT> {
/// Local implementation of `ValidatorContext`.
struct NetworkContext<'g, 'p, B: BlockT> {
gossip: &'g mut ConsensusGossip<B>,
network: &'p mut dyn Network<B>,
notification_service: &'p mut Box<dyn NotificationService>,
}
impl<'g, 'p, B: BlockT> ValidatorContext<B> for NetworkContext<'g, 'p, B> {
/// Broadcast all messages with given topic to peers that do not have it yet.
fn broadcast_topic(&mut self, topic: B::Hash, force: bool) {
self.gossip.broadcast_topic(self.network, topic, force);
self.gossip.broadcast_topic(self.notification_service, topic, force);
}
/// Broadcast a message to all peers that have not received it previously.
fn broadcast_message(&mut self, topic: B::Hash, message: Vec<u8>, force: bool) {
self.gossip.multicast(self.network, topic, message, force);
self.gossip.multicast(self.notification_service, topic, message, force);
}
/// Send addressed message to a peer.
fn send_message(&mut self, who: &PeerId, message: Vec<u8>) {
self.network.write_notification(*who, self.gossip.protocol.clone(), message);
self.notification_service.send_sync_notification(who, message);
}
/// Send all messages with given topic to a peer.
fn send_topic(&mut self, who: &PeerId, topic: B::Hash, force: bool) {
self.gossip.send_topic(self.network, who, topic, force);
self.gossip.send_topic(self.notification_service, who, topic, force);
}
}
fn propagate<'a, B: BlockT, I>(
network: &mut dyn Network<B>,
notification_service: &mut Box<dyn NotificationService>,
protocol: ProtocolName,
messages: I,
intent: MessageIntent,
@@ -147,7 +147,7 @@ where
?message,
"Propagating message",
);
network.write_notification(*id, protocol.clone(), message.clone());
notification_service.send_sync_notification(id, message.clone());
}
}
}
@@ -191,7 +191,12 @@ impl<B: BlockT> ConsensusGossip<B> {
}
/// Handle new connected peer.
pub fn new_peer(&mut self, network: &mut dyn Network<B>, who: PeerId, role: ObservedRole) {
pub fn new_peer(
&mut self,
notification_service: &mut Box<dyn NotificationService>,
who: PeerId,
role: ObservedRole,
) {
tracing::trace!(
target:"gossip",
%who,
@@ -202,7 +207,7 @@ impl<B: BlockT> ConsensusGossip<B> {
self.peers.insert(who, PeerConsensus { known_messages: Default::default() });
let validator = self.validator.clone();
let mut context = NetworkContext { gossip: self, network };
let mut context = NetworkContext { gossip: self, notification_service };
validator.new_peer(&mut context, &who, role);
}
@@ -233,30 +238,35 @@ impl<B: BlockT> ConsensusGossip<B> {
}
/// Call when a peer has been disconnected to stop tracking gossip status.
pub fn peer_disconnected(&mut self, network: &mut dyn Network<B>, who: PeerId) {
pub fn peer_disconnected(
&mut self,
notification_service: &mut Box<dyn NotificationService>,
who: PeerId,
) {
let validator = self.validator.clone();
let mut context = NetworkContext { gossip: self, network };
let mut context = NetworkContext { gossip: self, notification_service };
validator.peer_disconnected(&mut context, &who);
self.peers.remove(&who);
}
/// Perform periodic maintenance
pub fn tick(&mut self, network: &mut dyn Network<B>) {
pub fn tick(&mut self, notification_service: &mut Box<dyn NotificationService>) {
self.collect_garbage();
if Instant::now() >= self.next_broadcast {
self.rebroadcast(network);
self.rebroadcast(notification_service);
self.next_broadcast = Instant::now() + REBROADCAST_INTERVAL;
}
}
/// Rebroadcast all messages to all peers.
fn rebroadcast(&mut self, network: &mut dyn Network<B>) {
fn rebroadcast(&mut self, notification_service: &mut Box<dyn NotificationService>) {
let messages = self
.messages
.iter()
.map(|entry| (&entry.message_hash, &entry.topic, &entry.message));
propagate(
network,
notification_service,
self.protocol.clone(),
messages,
MessageIntent::PeriodicRebroadcast,
@@ -266,7 +276,12 @@ impl<B: BlockT> ConsensusGossip<B> {
}
/// Broadcast all messages with given topic.
pub fn broadcast_topic(&mut self, network: &mut dyn Network<B>, topic: B::Hash, force: bool) {
pub fn broadcast_topic(
&mut self,
notification_service: &mut Box<dyn NotificationService>,
topic: B::Hash,
force: bool,
) {
let messages = self.messages.iter().filter_map(|entry| {
if entry.topic == topic {
Some((&entry.message_hash, &entry.topic, &entry.message))
@@ -276,7 +291,7 @@ impl<B: BlockT> ConsensusGossip<B> {
});
let intent = if force { MessageIntent::ForcedBroadcast } else { MessageIntent::Broadcast };
propagate(
network,
notification_service,
self.protocol.clone(),
messages,
intent,
@@ -327,6 +342,7 @@ impl<B: BlockT> ConsensusGossip<B> {
pub fn on_incoming(
&mut self,
network: &mut dyn Network<B>,
notification_service: &mut Box<dyn NotificationService>,
who: PeerId,
messages: Vec<Vec<u8>>,
) -> Vec<(B::Hash, TopicNotification)> {
@@ -367,7 +383,7 @@ impl<B: BlockT> ConsensusGossip<B> {
// validate the message
let validation = {
let validator = self.validator.clone();
let mut context = NetworkContext { gossip: self, network };
let mut context = NetworkContext { gossip: self, notification_service };
validator.validate(&mut context, &who, &message)
};
@@ -414,7 +430,7 @@ impl<B: BlockT> ConsensusGossip<B> {
/// Send all messages with given topic to a peer.
pub fn send_topic(
&mut self,
network: &mut dyn Network<B>,
notification_service: &mut Box<dyn NotificationService>,
who: &PeerId,
topic: B::Hash,
force: bool,
@@ -443,7 +459,7 @@ impl<B: BlockT> ConsensusGossip<B> {
?entry.message,
"Sending topic message",
);
network.write_notification(*who, self.protocol.clone(), entry.message.clone());
notification_service.send_sync_notification(who, entry.message.clone());
}
}
}
@@ -451,7 +467,7 @@ impl<B: BlockT> ConsensusGossip<B> {
/// Multicast a message to all peers.
pub fn multicast(
&mut self,
network: &mut dyn Network<B>,
notification_service: &mut Box<dyn NotificationService>,
topic: B::Hash,
message: Vec<u8>,
force: bool,
@@ -460,7 +476,7 @@ impl<B: BlockT> ConsensusGossip<B> {
self.register_message_hashed(message_hash, topic, message.clone(), None);
let intent = if force { MessageIntent::ForcedBroadcast } else { MessageIntent::Broadcast };
propagate(
network,
notification_service,
self.protocol.clone(),
iter::once((&message_hash, &topic, &message)),
intent,
@@ -471,7 +487,12 @@ impl<B: BlockT> ConsensusGossip<B> {
/// Send addressed message to a peer. The message is not kept or multicast
/// later on.
pub fn send_message(&mut self, network: &mut dyn Network<B>, who: &PeerId, message: Vec<u8>) {
pub fn send_message(
&mut self,
notification_service: &mut Box<dyn NotificationService>,
who: &PeerId,
message: Vec<u8>,
) {
let peer = match self.peers.get_mut(who) {
None => return,
Some(peer) => peer,
@@ -488,7 +509,7 @@ impl<B: BlockT> ConsensusGossip<B> {
);
peer.known_messages.insert(message_hash);
network.write_notification(*who, self.protocol.clone(), message);
notification_service.send_sync_notification(who, message)
}
}
@@ -524,9 +545,9 @@ mod tests {
use crate::multiaddr::Multiaddr;
use futures::prelude::*;
use sc_network::{
config::MultiaddrWithPeerId, event::Event, NetworkBlock, NetworkEventStream,
NetworkNotification, NetworkPeers, NotificationSenderError,
NotificationSenderT as NotificationSender, ReputationChange,
config::MultiaddrWithPeerId, event::Event, service::traits::NotificationEvent, MessageSink,
NetworkBlock, NetworkEventStream, NetworkNotification, NetworkPeers,
NotificationSenderError, NotificationSenderT as NotificationSender, ReputationChange,
};
use sp_runtime::{
testing::{Block as RawBlock, ExtrinsicWrapper, H256},
@@ -651,6 +672,10 @@ mod tests {
fn sync_num_connected(&self) -> usize {
unimplemented!();
}
fn peer_role(&self, _peer_id: PeerId, _handshake: Vec<u8>) -> Option<ObservedRole> {
None
}
}
impl NetworkEventStream for NoOpNetwork {
@@ -691,6 +716,62 @@ mod tests {
}
}
#[derive(Debug, Default)]
struct NoOpNotificationService {}
#[async_trait::async_trait]
impl NotificationService for NoOpNotificationService {
/// 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(&self, _peer: &PeerId, _notification: Vec<u8>) {
unimplemented!();
}
/// Send asynchronous `notification` to `peer`, allowing sender to exercise backpressure.
async fn send_async_notification(
&self,
_peer: &PeerId,
_notification: Vec<u8>,
) -> Result<(), sc_network::error::Error> {
unimplemented!();
}
/// Set handshake for the notification protocol replacing the old handshake.
async fn set_handshake(&mut self, _handshake: Vec<u8>) -> Result<(), ()> {
unimplemented!();
}
fn try_set_handshake(&mut self, _handshake: Vec<u8>) -> Result<(), ()> {
unimplemented!();
}
/// Get next event from the `Notifications` event stream.
async fn next_event(&mut self) -> Option<NotificationEvent> {
None
}
fn clone(&mut self) -> Result<Box<dyn NotificationService>, ()> {
unimplemented!();
}
fn protocol(&self) -> &ProtocolName {
unimplemented!();
}
fn message_sink(&self, _peer: &PeerId) -> Option<Box<dyn MessageSink>> {
unimplemented!();
}
}
#[test]
fn collects_garbage() {
struct AllowOne;
@@ -773,20 +854,28 @@ mod tests {
fn peer_is_removed_on_disconnect() {
let mut consensus = ConsensusGossip::<Block>::new(Arc::new(AllowAll), "/foo".into(), None);
let mut network = NoOpNetwork::default();
let mut notification_service: Box<dyn NotificationService> =
Box::new(NoOpNotificationService::default());
let peer_id = PeerId::random();
consensus.new_peer(&mut network, peer_id, ObservedRole::Full);
consensus.new_peer(&mut notification_service, peer_id, ObservedRole::Full);
assert!(consensus.peers.contains_key(&peer_id));
consensus.peer_disconnected(&mut network, peer_id);
consensus.peer_disconnected(&mut notification_service, peer_id);
assert!(!consensus.peers.contains_key(&peer_id));
}
#[test]
fn on_incoming_ignores_discarded_messages() {
let mut notification_service: Box<dyn NotificationService> =
Box::new(NoOpNotificationService::default());
let to_forward = ConsensusGossip::<Block>::new(Arc::new(DiscardAll), "/foo".into(), None)
.on_incoming(&mut NoOpNetwork::default(), PeerId::random(), vec![vec![1, 2, 3]]);
.on_incoming(
&mut NoOpNetwork::default(),
&mut notification_service,
PeerId::random(),
vec![vec![1, 2, 3]],
);
assert!(
to_forward.is_empty(),
@@ -798,11 +887,14 @@ mod tests {
#[test]
fn on_incoming_ignores_unregistered_peer() {
let mut network = NoOpNetwork::default();
let mut notification_service: Box<dyn NotificationService> =
Box::new(NoOpNotificationService::default());
let remote = PeerId::random();
let to_forward = ConsensusGossip::<Block>::new(Arc::new(AllowAll), "/foo".into(), None)
.on_incoming(
&mut network,
&mut notification_service,
// Unregistered peer.
remote,
vec![vec![1, 2, 3]],
@@ -822,18 +914,20 @@ mod tests {
let mut consensus = ConsensusGossip::<Block>::new(Arc::new(AllowAll), "/foo".into(), None);
let mut network = NoOpNetwork::default();
let mut notification_service: Box<dyn NotificationService> =
Box::new(NoOpNotificationService::default());
let peer_id = PeerId::random();
consensus.new_peer(&mut network, peer_id, ObservedRole::Full);
consensus.new_peer(&mut notification_service, peer_id, ObservedRole::Full);
assert!(consensus.peers.contains_key(&peer_id));
let peer_id2 = PeerId::random();
consensus.new_peer(&mut network, peer_id2, ObservedRole::Full);
consensus.new_peer(&mut notification_service, peer_id2, ObservedRole::Full);
assert!(consensus.peers.contains_key(&peer_id2));
let message = vec![vec![1, 2, 3]];
consensus.on_incoming(&mut network, peer_id, message.clone());
consensus.on_incoming(&mut network, peer_id2, message.clone());
consensus.on_incoming(&mut network, &mut notification_service, peer_id, message.clone());
consensus.on_incoming(&mut network, &mut notification_service, peer_id2, message.clone());
assert_eq!(
vec![(peer_id, rep::GOSSIP_SUCCESS)],