Rename GenericProto to Notifications (#8415)

* Rename GenericProto to Notifications

* Small comment fix
This commit is contained in:
Pierre Krieger
2021-03-22 11:32:47 +01:00
committed by GitHub
parent 998d190ea0
commit 5087634ccd
8 changed files with 46 additions and 49 deletions
@@ -16,13 +16,10 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! 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;
@@ -16,7 +16,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
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<NetworkBehaviourAction<NotifsHandlerIn, GenericProtoOut>>,
events: VecDeque<NetworkBehaviourAction<NotifsHandlerIn, NotificationsOut>>,
}
/// 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,
@@ -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
@@ -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<CustomProtoWithAddr>, Swarm<CustomProtoWithAddr>) {
});
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<CustomProtoWithAddr>, Swarm<CustomProtoWithAddr>) {
/// 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 = <GenericProto as NetworkBehaviour>::ProtocolsHandler;
type OutEvent = <GenericProto as NetworkBehaviour>::OutEvent;
type ProtocolsHandler = <Notifications as NetworkBehaviour>::ProtocolsHandler;
type OutEvent = <Notifications as NetworkBehaviour>::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!(),
_ => {}
}
}