diff --git a/substrate/client/network/src/protocol.rs b/substrate/client/network/src/protocol.rs index 11e1197998..b86b1a9745 100644 --- a/substrate/client/network/src/protocol.rs +++ b/substrate/client/network/src/protocol.rs @@ -27,7 +27,7 @@ use crate::{ use bytes::Bytes; use codec::{Decode, DecodeAll, Encode}; use futures::{channel::oneshot, prelude::*}; -use generic_proto::{GenericProto, GenericProtoOut}; +use notifications::{Notifications, NotificationsOut}; use libp2p::core::{ConnectedPoint, connection::{ConnectionId, ListenerId}}; use libp2p::request_response::OutboundFailure; use libp2p::swarm::{NetworkBehaviour, NetworkBehaviourAction, PollParameters}; @@ -56,13 +56,13 @@ use std::collections::{HashMap, HashSet, VecDeque}; use std::sync::Arc; use std::{io, iter, num::NonZeroUsize, pin::Pin, task::Poll, time}; -mod generic_proto; +mod notifications; pub mod message; pub mod event; pub mod sync; -pub use generic_proto::{NotificationsSink, Ready, NotifsHandlerError}; +pub use notifications::{NotificationsSink, Ready, NotifsHandlerError}; /// Interval at which we perform time based maintenance const TICK_TIMEOUT: time::Duration = time::Duration::from_millis(1100); @@ -161,7 +161,7 @@ pub struct Protocol { /// Used to report reputation changes. peerset_handle: sc_peerset::PeersetHandle, /// Handles opening the unique substream and sending and receiving raw messages. - behaviour: GenericProto, + behaviour: Notifications, /// List of notifications protocols that have been registered. notification_protocols: Vec>, /// If we receive a new "substream open" event that contains an invalid handshake, we ask the @@ -362,7 +362,7 @@ impl Protocol { genesis_hash, ).encode(); - GenericProto::new( + Notifications::new( peerset, iter::once((block_announces_protocol, block_announces_handshake, MAX_BLOCK_ANNOUNCE_SIZE)) .chain(network_config.extra_sets.iter() @@ -1169,7 +1169,7 @@ pub enum CustomMessageOutcome { } impl NetworkBehaviour for Protocol { - type ProtocolsHandler = ::ProtocolsHandler; + type ProtocolsHandler = ::ProtocolsHandler; type OutEvent = CustomMessageOutcome; fn new_handler(&mut self) -> Self::ProtocolsHandler { @@ -1332,7 +1332,7 @@ impl NetworkBehaviour for Protocol { }; let outcome = match event { - GenericProtoOut::CustomProtocolOpen { peer_id, set_id, received_handshake, notifications_sink, .. } => { + NotificationsOut::CustomProtocolOpen { peer_id, set_id, received_handshake, notifications_sink, .. } => { // Set number 0 is hardcoded the default set of peers we sync from. if set_id == HARDCODED_PEERSETS_SYNC { // `received_handshake` can be either a `Status` message if received from the @@ -1419,7 +1419,7 @@ impl NetworkBehaviour for Protocol { } } } - GenericProtoOut::CustomProtocolReplaced { peer_id, notifications_sink, set_id } => { + NotificationsOut::CustomProtocolReplaced { peer_id, notifications_sink, set_id } => { if set_id == HARDCODED_PEERSETS_SYNC { CustomMessageOutcome::None } else if self.bad_handshake_substreams.contains(&(peer_id.clone(), set_id)) { @@ -1432,7 +1432,7 @@ impl NetworkBehaviour for Protocol { } } }, - GenericProtoOut::CustomProtocolClosed { peer_id, set_id } => { + NotificationsOut::CustomProtocolClosed { peer_id, set_id } => { // Set number 0 is hardcoded the default set of peers we sync from. if set_id == HARDCODED_PEERSETS_SYNC { if self.on_sync_peer_disconnected(peer_id.clone()).is_ok() { @@ -1457,7 +1457,7 @@ impl NetworkBehaviour for Protocol { } } }, - GenericProtoOut::Notification { peer_id, set_id, message } => + NotificationsOut::Notification { peer_id, set_id, message } => match set_id { HARDCODED_PEERSETS_SYNC if self.peers.contains_key(&peer_id) => { if let Ok(announce) = message::BlockAnnounce::decode(&mut message.as_ref()) { diff --git a/substrate/client/network/src/protocol/generic_proto.rs b/substrate/client/network/src/protocol/notifications.rs similarity index 71% rename from substrate/client/network/src/protocol/generic_proto.rs rename to substrate/client/network/src/protocol/notifications.rs index a305fc1f5e..ef25795758 100644 --- a/substrate/client/network/src/protocol/generic_proto.rs +++ b/substrate/client/network/src/protocol/notifications.rs @@ -16,13 +16,10 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -//! Implementation of libp2p's `NetworkBehaviour` trait that opens a single substream with the -//! remote and then allows any communication with them. -//! -//! The `Protocol` struct uses `GenericProto` in order to open substreams with the rest of the -//! network, then performs the Substrate protocol handling on top. +//! Implementation of libp2p's `NetworkBehaviour` trait that establishes communications and opens +//! notifications substreams. -pub use self::behaviour::{GenericProto, GenericProtoOut}; +pub use self::behaviour::{Notifications, NotificationsOut}; pub use self::handler::{NotifsHandlerError, NotificationsSink, Ready}; mod behaviour; diff --git a/substrate/client/network/src/protocol/generic_proto/behaviour.rs b/substrate/client/network/src/protocol/notifications/behaviour.rs similarity index 98% rename from substrate/client/network/src/protocol/generic_proto/behaviour.rs rename to substrate/client/network/src/protocol/notifications/behaviour.rs index 05247dc6f0..08c4ec5d4f 100644 --- a/substrate/client/network/src/protocol/generic_proto/behaviour.rs +++ b/substrate/client/network/src/protocol/notifications/behaviour.rs @@ -16,7 +16,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use crate::protocol::generic_proto::{ +use crate::protocol::notifications::{ handler::{NotificationsSink, NotifsHandlerProto, NotifsHandlerOut, NotifsHandlerIn} }; @@ -44,7 +44,7 @@ use wasm_timer::Instant; /// /// # How it works /// -/// The role of the `GenericProto` is to synchronize the following components: +/// The role of the `Notifications` is to synchronize the following components: /// /// - The libp2p swarm that opens new connections and reports disconnects. /// - The connection handler (see `group.rs`) that handles individual connections. @@ -83,9 +83,9 @@ use wasm_timer::Instant; /// different than a single connection failing and being re-established /// in terms of potential reordering and dropped messages. Messages can /// be received on any connection. -/// 3. The behaviour reports `GenericProtoOut::CustomProtocolOpen` when the +/// 3. The behaviour reports `NotificationsOut::CustomProtocolOpen` when the /// first connection reports `NotifsHandlerOut::OpenResultOk`. -/// 4. The behaviour reports `GenericProtoOut::CustomProtocolClosed` when the +/// 4. The behaviour reports `NotificationsOut::CustomProtocolClosed` when the /// last connection reports `NotifsHandlerOut::ClosedResult`. /// /// In this way, the number of actual established connections to the peer is @@ -94,7 +94,7 @@ use wasm_timer::Instant; /// and only as a result of simultaneous dialing. However, the implementation /// accommodates for any number of connections. /// -pub struct GenericProto { +pub struct Notifications { /// Notification protocols. Entries are only ever added and not removed. /// Contains, for each protocol, the protocol name and the message to send as part of the /// initial handshake. @@ -127,7 +127,7 @@ pub struct GenericProto { next_incoming_index: sc_peerset::IncomingIndex, /// Events to produce from `poll()`. - events: VecDeque>, + events: VecDeque>, } /// Identifier for a delay firing. @@ -302,9 +302,9 @@ struct IncomingPeer { incoming_id: sc_peerset::IncomingIndex, } -/// Event that can be emitted by the `GenericProto`. +/// Event that can be emitted by the `Notifications`. #[derive(Debug)] -pub enum GenericProtoOut { +pub enum NotificationsOut { /// Opened a custom protocol with the remote. CustomProtocolOpen { /// Id of the peer we are connected to. @@ -354,7 +354,7 @@ pub enum GenericProtoOut { }, } -impl GenericProto { +impl Notifications { /// Creates a `CustomProtos`. pub fn new( peerset: sc_peerset::Peerset, @@ -366,7 +366,7 @@ impl GenericProto { assert!(!notif_protocols.is_empty()); - GenericProto { + Notifications { notif_protocols, peerset, peers: FnvHashMap::default(), @@ -462,7 +462,7 @@ impl GenericProto { if connections.iter().any(|(_, s)| matches!(s, ConnectionState::Open(_))) { debug!(target: "sub-libp2p", "External API <= Closed({}, {:?})", peer_id, set_id); - let event = GenericProtoOut::CustomProtocolClosed { + let event = NotificationsOut::CustomProtocolClosed { peer_id: peer_id.clone(), set_id, }; @@ -828,7 +828,7 @@ impl GenericProto { if connections.iter().any(|(_, s)| matches!(s, ConnectionState::Open(_))) { debug!(target: "sub-libp2p", "External API <= Closed({}, {:?})", entry.key().0, set_id); - let event = GenericProtoOut::CustomProtocolClosed { + let event = NotificationsOut::CustomProtocolClosed { peer_id: entry.key().0.clone(), set_id, }; @@ -1013,9 +1013,9 @@ impl GenericProto { } } -impl NetworkBehaviour for GenericProto { +impl NetworkBehaviour for Notifications { type ProtocolsHandler = NotifsHandlerProto; - type OutEvent = GenericProtoOut; + type OutEvent = NotificationsOut; fn new_handler(&mut self) -> Self::ProtocolsHandler { NotifsHandlerProto::new(self.notif_protocols.clone()) @@ -1265,7 +1265,7 @@ impl NetworkBehaviour for GenericProto { "External API <= Sink replaced({}, {:?})", peer_id, set_id ); - let event = GenericProtoOut::CustomProtocolReplaced { + let event = NotificationsOut::CustomProtocolReplaced { peer_id: peer_id.clone(), set_id, notifications_sink: replacement_sink, @@ -1277,7 +1277,7 @@ impl NetworkBehaviour for GenericProto { target: "sub-libp2p", "External API <= Closed({}, {:?})", peer_id, set_id ); - let event = GenericProtoOut::CustomProtocolClosed { + let event = NotificationsOut::CustomProtocolClosed { peer_id: peer_id.clone(), set_id, }; @@ -1642,7 +1642,7 @@ impl NetworkBehaviour for GenericProto { { if pos <= replacement_pos { debug!(target: "sub-libp2p", "External API <= Sink replaced({:?})", source); - let event = GenericProtoOut::CustomProtocolReplaced { + let event = NotificationsOut::CustomProtocolReplaced { peer_id: source, set_id, notifications_sink: replacement_sink, @@ -1665,7 +1665,7 @@ impl NetworkBehaviour for GenericProto { } debug!(target: "sub-libp2p", "External API <= Closed({}, {:?})", source, set_id); - let event = GenericProtoOut::CustomProtocolClosed { + let event = NotificationsOut::CustomProtocolClosed { peer_id: source, set_id, }; @@ -1739,7 +1739,7 @@ impl NetworkBehaviour for GenericProto { { if !any_open { debug!(target: "sub-libp2p", "External API <= Open({:?})", source); - let event = GenericProtoOut::CustomProtocolOpen { + let event = NotificationsOut::CustomProtocolOpen { peer_id: source, set_id, received_handshake, @@ -1876,7 +1876,7 @@ impl NetworkBehaviour for GenericProto { ); trace!(target: "sub-libp2p", "External API <= Message({}, {:?})", source, set_id); - let event = GenericProtoOut::Notification { + let event = NotificationsOut::Notification { peer_id: source, set_id, message, diff --git a/substrate/client/network/src/protocol/generic_proto/handler.rs b/substrate/client/network/src/protocol/notifications/handler.rs similarity index 99% rename from substrate/client/network/src/protocol/generic_proto/handler.rs rename to substrate/client/network/src/protocol/notifications/handler.rs index 0db249f90a..ec3760d525 100644 --- a/substrate/client/network/src/protocol/generic_proto/handler.rs +++ b/substrate/client/network/src/protocol/notifications/handler.rs @@ -57,7 +57,7 @@ //! It is illegal to send a [`NotifsHandlerIn::Open`] before a previously-emitted //! [`NotifsHandlerIn::Open`] has gotten an answer. -use crate::protocol::generic_proto::{ +use crate::protocol::notifications::{ upgrade::{ NotificationsIn, NotificationsOut, NotificationsInSubstream, NotificationsOutSubstream, NotificationsHandshakeError, UpgradeCollec diff --git a/substrate/client/network/src/protocol/generic_proto/tests.rs b/substrate/client/network/src/protocol/notifications/tests.rs similarity index 92% rename from substrate/client/network/src/protocol/generic_proto/tests.rs rename to substrate/client/network/src/protocol/notifications/tests.rs index 2c80fe8523..f159a8e631 100644 --- a/substrate/client/network/src/protocol/generic_proto/tests.rs +++ b/substrate/client/network/src/protocol/notifications/tests.rs @@ -18,7 +18,7 @@ #![cfg(test)] -use crate::protocol::generic_proto::{GenericProto, GenericProtoOut}; +use crate::protocol::notifications::{Notifications, NotificationsOut}; use futures::prelude::*; use libp2p::{PeerId, Multiaddr, Transport}; @@ -80,7 +80,7 @@ fn build_nodes() -> (Swarm, Swarm) { }); let behaviour = CustomProtoWithAddr { - inner: GenericProto::new(peerset, iter::once(("/foo".into(), Vec::new(), 1024 * 1024))), + inner: Notifications::new(peerset, iter::once(("/foo".into(), Vec::new(), 1024 * 1024))), addrs: addrs .iter() .enumerate() @@ -110,12 +110,12 @@ fn build_nodes() -> (Swarm, Swarm) { /// Wraps around the `CustomBehaviour` network behaviour, and adds hardcoded node addresses to it. struct CustomProtoWithAddr { - inner: GenericProto, + inner: Notifications, addrs: Vec<(PeerId, Multiaddr)>, } impl std::ops::Deref for CustomProtoWithAddr { - type Target = GenericProto; + type Target = Notifications; fn deref(&self) -> &Self::Target { &self.inner @@ -129,8 +129,8 @@ impl std::ops::DerefMut for CustomProtoWithAddr { } impl NetworkBehaviour for CustomProtoWithAddr { - type ProtocolsHandler = ::ProtocolsHandler; - type OutEvent = ::OutEvent; + type ProtocolsHandler = ::ProtocolsHandler; + type OutEvent = ::OutEvent; fn new_handler(&mut self) -> Self::ProtocolsHandler { self.inner.new_handler() @@ -240,7 +240,7 @@ fn reconnect_after_disconnect() { }; match event { - future::Either::Left(GenericProtoOut::CustomProtocolOpen { .. }) => { + future::Either::Left(NotificationsOut::CustomProtocolOpen { .. }) => { match service1_state { ServiceState::NotConnected => { service1_state = ServiceState::FirstConnec; @@ -255,14 +255,14 @@ fn reconnect_after_disconnect() { ServiceState::FirstConnec | ServiceState::ConnectedAgain => panic!(), } }, - future::Either::Left(GenericProtoOut::CustomProtocolClosed { .. }) => { + future::Either::Left(NotificationsOut::CustomProtocolClosed { .. }) => { match service1_state { ServiceState::FirstConnec => service1_state = ServiceState::Disconnected, ServiceState::ConnectedAgain| ServiceState::NotConnected | ServiceState::Disconnected => panic!(), } }, - future::Either::Right(GenericProtoOut::CustomProtocolOpen { .. }) => { + future::Either::Right(NotificationsOut::CustomProtocolOpen { .. }) => { match service2_state { ServiceState::NotConnected => { service2_state = ServiceState::FirstConnec; @@ -277,7 +277,7 @@ fn reconnect_after_disconnect() { ServiceState::FirstConnec | ServiceState::ConnectedAgain => panic!(), } }, - future::Either::Right(GenericProtoOut::CustomProtocolClosed { .. }) => { + future::Either::Right(NotificationsOut::CustomProtocolClosed { .. }) => { match service2_state { ServiceState::FirstConnec => service2_state = ServiceState::Disconnected, ServiceState::ConnectedAgain| ServiceState::NotConnected | @@ -310,8 +310,8 @@ fn reconnect_after_disconnect() { }; match event { - GenericProtoOut::CustomProtocolOpen { .. } | - GenericProtoOut::CustomProtocolClosed { .. } => panic!(), + NotificationsOut::CustomProtocolOpen { .. } | + NotificationsOut::CustomProtocolClosed { .. } => panic!(), _ => {} } } diff --git a/substrate/client/network/src/protocol/generic_proto/upgrade.rs b/substrate/client/network/src/protocol/notifications/upgrade.rs similarity index 100% rename from substrate/client/network/src/protocol/generic_proto/upgrade.rs rename to substrate/client/network/src/protocol/notifications/upgrade.rs diff --git a/substrate/client/network/src/protocol/generic_proto/upgrade/collec.rs b/substrate/client/network/src/protocol/notifications/upgrade/collec.rs similarity index 100% rename from substrate/client/network/src/protocol/generic_proto/upgrade/collec.rs rename to substrate/client/network/src/protocol/notifications/upgrade/collec.rs diff --git a/substrate/client/network/src/protocol/generic_proto/upgrade/notifications.rs b/substrate/client/network/src/protocol/notifications/upgrade/notifications.rs similarity index 100% rename from substrate/client/network/src/protocol/generic_proto/upgrade/notifications.rs rename to substrate/client/network/src/protocol/notifications/upgrade/notifications.rs