mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-09 20:11:09 +00:00
Rename GenericProto to Notifications (#8415)
* Rename GenericProto to Notifications * Small comment fix
This commit is contained in:
@@ -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<B: BlockT> {
|
||||
/// 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<Cow<'static, str>>,
|
||||
/// If we receive a new "substream open" event that contains an invalid handshake, we ask the
|
||||
@@ -362,7 +362,7 @@ impl<B: BlockT> Protocol<B> {
|
||||
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<B: BlockT> {
|
||||
}
|
||||
|
||||
impl<B: BlockT> NetworkBehaviour for Protocol<B> {
|
||||
type ProtocolsHandler = <GenericProto as NetworkBehaviour>::ProtocolsHandler;
|
||||
type ProtocolsHandler = <Notifications as NetworkBehaviour>::ProtocolsHandler;
|
||||
type OutEvent = CustomMessageOutcome<B>;
|
||||
|
||||
fn new_handler(&mut self) -> Self::ProtocolsHandler {
|
||||
@@ -1332,7 +1332,7 @@ impl<B: BlockT> NetworkBehaviour for Protocol<B> {
|
||||
};
|
||||
|
||||
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<B: BlockT> NetworkBehaviour for Protocol<B> {
|
||||
}
|
||||
}
|
||||
}
|
||||
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<B: BlockT> NetworkBehaviour for Protocol<B> {
|
||||
}
|
||||
}
|
||||
},
|
||||
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<B: BlockT> NetworkBehaviour for Protocol<B> {
|
||||
}
|
||||
}
|
||||
},
|
||||
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()) {
|
||||
|
||||
+3
-6
@@ -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;
|
||||
+20
-20
@@ -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,
|
||||
+1
-1
@@ -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
|
||||
+12
-12
@@ -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!(),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user