diff --git a/substrate/core/network/src/custom_proto/behaviour.rs b/substrate/core/network/src/legacy_proto/behaviour.rs similarity index 97% rename from substrate/core/network/src/custom_proto/behaviour.rs rename to substrate/core/network/src/legacy_proto/behaviour.rs index 1cfa16ea6b35359b52cd34e70afa099e92fc5aac..790c1d158dd63fc3c3cd46d94119138abea98732 100644 --- a/substrate/core/network/src/custom_proto/behaviour.rs +++ b/substrate/core/network/src/legacy_proto/behaviour.rs @@ -15,8 +15,8 @@ // along with Substrate. If not, see <http://www.gnu.org/licenses/>. use crate::{DiscoveryNetBehaviour, config::ProtocolId}; -use crate::custom_proto::handler::{CustomProtoHandlerProto, CustomProtoHandlerOut, CustomProtoHandlerIn}; -use crate::custom_proto::upgrade::RegisteredProtocol; +use crate::legacy_proto::handler::{CustomProtoHandlerProto, CustomProtoHandlerOut, CustomProtoHandlerIn}; +use crate::legacy_proto::upgrade::RegisteredProtocol; use crate::protocol::message::Message; use fnv::FnvHashMap; use futures::prelude::*; @@ -34,7 +34,7 @@ use tokio_io::{AsyncRead, AsyncWrite}; /// /// ## How it works /// -/// The role of the `CustomProto` is to synchronize the following components: +/// The role of the `LegacyProto` is to synchronize the following components: /// /// - The libp2p swarm that opens new connections and reports disconnects. /// - The connection handler (see `handler.rs`) that handles individual connections. @@ -60,7 +60,7 @@ use tokio_io::{AsyncRead, AsyncWrite}; /// Note that this "banning" system is not an actual ban. If a "banned" node tries to connect to /// us, we accept the connection. The "banning" system is only about delaying dialing attempts. /// -pub struct CustomProto<B: BlockT, TSubstream> { +pub struct LegacyProto<B: BlockT, TSubstream> { /// List of protocols to open with peers. Never modified. protocol: RegisteredProtocol<B>, @@ -79,7 +79,7 @@ pub struct CustomProto<B: BlockT, TSubstream> { next_incoming_index: peerset::IncomingIndex, /// Events to produce from `poll()`. - events: SmallVec<[NetworkBehaviourAction<CustomProtoHandlerIn<B>, CustomProtoOut<B>>; 4]>, + events: SmallVec<[NetworkBehaviourAction<CustomProtoHandlerIn<B>, LegacyProtoOut<B>>; 4]>, /// Marker to pin the generics. marker: PhantomData<TSubstream>, @@ -186,9 +186,9 @@ struct IncomingPeer { incoming_id: peerset::IncomingIndex, } -/// Event that can be emitted by the `CustomProto`. +/// Event that can be emitted by the `LegacyProto`. #[derive(Debug)] -pub enum CustomProtoOut<B: BlockT> { +pub enum LegacyProtoOut<B: BlockT> { /// Opened a custom protocol with the remote. CustomProtocolOpen { /// Version of the protocol that has been opened. @@ -225,7 +225,7 @@ pub enum CustomProtoOut<B: BlockT> { }, } -impl<B: BlockT, TSubstream> CustomProto<B, TSubstream> { +impl<B: BlockT, TSubstream> LegacyProto<B, TSubstream> { /// Creates a `CustomProtos`. pub fn new( protocol: impl Into<ProtocolId>, @@ -234,7 +234,7 @@ impl<B: BlockT, TSubstream> CustomProto<B, TSubstream> { ) -> Self { let protocol = RegisteredProtocol::new(protocol, versions); - CustomProto { + LegacyProto { protocol, peerset, peers: FnvHashMap::default(), @@ -606,7 +606,7 @@ impl<B: BlockT, TSubstream> CustomProto<B, TSubstream> { } } -impl<B: BlockT, TSubstream> DiscoveryNetBehaviour for CustomProto<B, TSubstream> { +impl<B: BlockT, TSubstream> DiscoveryNetBehaviour for LegacyProto<B, TSubstream> { fn add_discovered_nodes(&mut self, peer_ids: impl Iterator<Item = PeerId>) { self.peerset.discovered(peer_ids.into_iter().map(|peer_id| { debug!(target: "sub-libp2p", "PSM <= Discovered({:?})", peer_id); @@ -615,13 +615,13 @@ impl<B: BlockT, TSubstream> DiscoveryNetBehaviour for CustomProto<B, TSubstream> } } -impl<B, TSubstream> NetworkBehaviour for CustomProto<B, TSubstream> +impl<B, TSubstream> NetworkBehaviour for LegacyProto<B, TSubstream> where TSubstream: AsyncRead + AsyncWrite, B: BlockT, { type ProtocolsHandler = CustomProtoHandlerProto<B, TSubstream>; - type OutEvent = CustomProtoOut<B>; + type OutEvent = LegacyProtoOut<B>; fn new_handler(&mut self) -> Self::ProtocolsHandler { CustomProtoHandlerProto::new(self.protocol.clone()) @@ -714,7 +714,7 @@ where } if open { debug!(target: "sub-libp2p", "External API <= Closed({:?})", peer_id); - let event = CustomProtoOut::CustomProtocolClosed { + let event = LegacyProtoOut::CustomProtocolClosed { peer_id: peer_id.clone(), reason: "Disconnected by libp2p".into(), }; @@ -731,7 +731,7 @@ where self.peers.insert(peer_id.clone(), PeerState::Banned { until: timer_deadline }); if open { debug!(target: "sub-libp2p", "External API <= Closed({:?})", peer_id); - let event = CustomProtoOut::CustomProtocolClosed { + let event = LegacyProtoOut::CustomProtocolClosed { peer_id: peer_id.clone(), reason: "Disconnected by libp2p".into(), }; @@ -748,7 +748,7 @@ where if open { debug!(target: "sub-libp2p", "External API <= Closed({:?})", peer_id); - let event = CustomProtoOut::CustomProtocolClosed { + let event = LegacyProtoOut::CustomProtocolClosed { peer_id: peer_id.clone(), reason: "Disconnected by libp2p".into(), }; @@ -833,7 +833,7 @@ where }; debug!(target: "sub-libp2p", "External API <= Closed({:?})", source); - let event = CustomProtoOut::CustomProtocolClosed { + let event = LegacyProtoOut::CustomProtocolClosed { reason, peer_id: source.clone(), }; @@ -891,7 +891,7 @@ where }; debug!(target: "sub-libp2p", "External API <= Open({:?})", source); - let event = CustomProtoOut::CustomProtocolOpen { + let event = LegacyProtoOut::CustomProtocolOpen { version, peer_id: source, endpoint, @@ -904,7 +904,7 @@ where debug_assert!(self.is_open(&source)); trace!(target: "sub-libp2p", "Handler({:?}) => Message", source); trace!(target: "sub-libp2p", "External API <= Message({:?})", source); - let event = CustomProtoOut::CustomMessage { + let event = LegacyProtoOut::CustomMessage { peer_id: source, message, }; @@ -918,7 +918,7 @@ where trace!(target: "sub-libp2p", "External API <= Clogged({:?})", source); warn!(target: "sub-libp2p", "Queue of packets to send to {:?} is \ pretty large", source); - self.events.push(NetworkBehaviourAction::GenerateEvent(CustomProtoOut::Clogged { + self.events.push(NetworkBehaviourAction::GenerateEvent(LegacyProtoOut::Clogged { peer_id: source, messages, })); diff --git a/substrate/core/network/src/custom_proto/handler.rs b/substrate/core/network/src/legacy_proto/handler.rs similarity index 99% rename from substrate/core/network/src/custom_proto/handler.rs rename to substrate/core/network/src/legacy_proto/handler.rs index 5cdedf49401fcb131856e965ba7f696752f61072..3fe88d3cfd410a83392cb875e8edb813854f70e3 100644 --- a/substrate/core/network/src/custom_proto/handler.rs +++ b/substrate/core/network/src/legacy_proto/handler.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Substrate. If not, see <http://www.gnu.org/licenses/>. -use crate::custom_proto::upgrade::{RegisteredProtocol, RegisteredProtocolEvent, RegisteredProtocolSubstream}; +use crate::legacy_proto::upgrade::{RegisteredProtocol, RegisteredProtocolEvent, RegisteredProtocolSubstream}; use crate::protocol::message::Message; use futures::prelude::*; use futures03::{compat::Compat, TryFutureExt as _}; diff --git a/substrate/core/network/src/custom_proto/mod.rs b/substrate/core/network/src/legacy_proto/mod.rs similarity index 93% rename from substrate/core/network/src/custom_proto/mod.rs rename to substrate/core/network/src/legacy_proto/mod.rs index 99f4f0183a1c9ecd594d331b112bb80dbdcf78b9..bbe795528be9d854217dc23a622b7bbe5fd8da36 100644 --- a/substrate/core/network/src/custom_proto/mod.rs +++ b/substrate/core/network/src/legacy_proto/mod.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Substrate. If not, see <http://www.gnu.org/licenses/>. -pub use self::behaviour::{CustomProto, CustomProtoOut}; +pub use self::behaviour::{LegacyProto, LegacyProtoOut}; mod behaviour; mod handler; diff --git a/substrate/core/network/src/custom_proto/tests.rs b/substrate/core/network/src/legacy_proto/tests.rs similarity index 92% rename from substrate/core/network/src/custom_proto/tests.rs rename to substrate/core/network/src/legacy_proto/tests.rs index 94456b1a0f2d95fd0e00097ced843749b82d6940..8fd47843df2e56f07d3a2a2c77a50159400b1bbd 100644 --- a/substrate/core/network/src/custom_proto/tests.rs +++ b/substrate/core/network/src/legacy_proto/tests.rs @@ -26,7 +26,7 @@ use rand::seq::SliceRandom; use std::{io, time::Duration, time::Instant}; use test_client::runtime::Block; use crate::message::generic::Message; -use crate::custom_proto::{CustomProto, CustomProtoOut}; +use crate::legacy_proto::{LegacyProto, LegacyProtoOut}; /// Builds two nodes that have each other as bootstrap nodes. /// This is to be used only for testing, and a panic will happen if something goes wrong. @@ -71,7 +71,7 @@ fn build_nodes() }); let behaviour = CustomProtoWithAddr { - inner: CustomProto::new(&b"test"[..], &[1], peerset), + inner: LegacyProto::new(&b"test"[..], &[1], peerset), addrs: addrs .iter() .enumerate() @@ -101,12 +101,12 @@ fn build_nodes() /// Wraps around the `CustomBehaviour` network behaviour, and adds hardcoded node addresses to it. struct CustomProtoWithAddr { - inner: CustomProto<Block, Substream<StreamMuxerBox>>, + inner: LegacyProto<Block, Substream<StreamMuxerBox>>, addrs: Vec<(PeerId, Multiaddr)>, } impl std::ops::Deref for CustomProtoWithAddr { - type Target = CustomProto<Block, Substream<StreamMuxerBox>>; + type Target = LegacyProto<Block, Substream<StreamMuxerBox>>; fn deref(&self) -> &Self::Target { &self.inner @@ -121,8 +121,8 @@ impl std::ops::DerefMut for CustomProtoWithAddr { impl NetworkBehaviour for CustomProtoWithAddr { type ProtocolsHandler = - <CustomProto<Block, Substream<StreamMuxerBox>> as NetworkBehaviour>::ProtocolsHandler; - type OutEvent = <CustomProto<Block, Substream<StreamMuxerBox>> as NetworkBehaviour>::OutEvent; + <LegacyProto<Block, Substream<StreamMuxerBox>> as NetworkBehaviour>::ProtocolsHandler; + type OutEvent = <LegacyProto<Block, Substream<StreamMuxerBox>> as NetworkBehaviour>::OutEvent; fn new_handler(&mut self) -> Self::ProtocolsHandler { self.inner.new_handler() @@ -205,7 +205,7 @@ fn two_nodes_transfer_lots_of_packets() { let fut1 = future::poll_fn(move || -> io::Result<_> { loop { match try_ready!(service1.poll()) { - Some(CustomProtoOut::CustomProtocolOpen { peer_id, .. }) => { + Some(LegacyProtoOut::CustomProtocolOpen { peer_id, .. }) => { for n in 0 .. NUM_PACKETS { service1.send_packet( &peer_id, @@ -222,8 +222,8 @@ fn two_nodes_transfer_lots_of_packets() { let fut2 = future::poll_fn(move || -> io::Result<_> { loop { match try_ready!(service2.poll()) { - Some(CustomProtoOut::CustomProtocolOpen { .. }) => {}, - Some(CustomProtoOut::CustomMessage { message: Message::ChainSpecific(message), .. }) => { + Some(LegacyProtoOut::CustomProtocolOpen { .. }) => {}, + Some(LegacyProtoOut::CustomMessage { message: Message::ChainSpecific(message), .. }) => { assert_eq!(message.len(), 1); packet_counter += 1; if packet_counter == NUM_PACKETS { @@ -261,7 +261,7 @@ fn basic_two_nodes_requests_in_parallel() { let fut1 = future::poll_fn(move || -> io::Result<_> { loop { match try_ready!(service1.poll()) { - Some(CustomProtoOut::CustomProtocolOpen { peer_id, .. }) => { + Some(LegacyProtoOut::CustomProtocolOpen { peer_id, .. }) => { for msg in to_send.drain(..) { service1.send_packet(&peer_id, msg); } @@ -274,8 +274,8 @@ fn basic_two_nodes_requests_in_parallel() { let fut2 = future::poll_fn(move || -> io::Result<_> { loop { match try_ready!(service2.poll()) { - Some(CustomProtoOut::CustomProtocolOpen { .. }) => {}, - Some(CustomProtoOut::CustomMessage { message, .. }) => { + Some(LegacyProtoOut::CustomProtocolOpen { .. }) => {}, + Some(LegacyProtoOut::CustomMessage { message, .. }) => { let pos = to_receive.iter().position(|m| *m == message).unwrap(); to_receive.remove(pos); if to_receive.is_empty() { @@ -313,7 +313,7 @@ fn reconnect_after_disconnect() { let mut service1_not_ready = false; match service1.poll().unwrap() { - Async::Ready(Some(CustomProtoOut::CustomProtocolOpen { .. })) => { + Async::Ready(Some(LegacyProtoOut::CustomProtocolOpen { .. })) => { match service1_state { ServiceState::NotConnected => { service1_state = ServiceState::FirstConnec; @@ -325,7 +325,7 @@ fn reconnect_after_disconnect() { ServiceState::FirstConnec | ServiceState::ConnectedAgain => panic!(), } }, - Async::Ready(Some(CustomProtoOut::CustomProtocolClosed { .. })) => { + Async::Ready(Some(LegacyProtoOut::CustomProtocolClosed { .. })) => { match service1_state { ServiceState::FirstConnec => service1_state = ServiceState::Disconnected, ServiceState::ConnectedAgain| ServiceState::NotConnected | @@ -337,7 +337,7 @@ fn reconnect_after_disconnect() { } match service2.poll().unwrap() { - Async::Ready(Some(CustomProtoOut::CustomProtocolOpen { .. })) => { + Async::Ready(Some(LegacyProtoOut::CustomProtocolOpen { .. })) => { match service2_state { ServiceState::NotConnected => { service2_state = ServiceState::FirstConnec; @@ -349,7 +349,7 @@ fn reconnect_after_disconnect() { ServiceState::FirstConnec | ServiceState::ConnectedAgain => panic!(), } }, - Async::Ready(Some(CustomProtoOut::CustomProtocolClosed { .. })) => { + Async::Ready(Some(LegacyProtoOut::CustomProtocolClosed { .. })) => { match service2_state { ServiceState::FirstConnec => service2_state = ServiceState::Disconnected, ServiceState::ConnectedAgain| ServiceState::NotConnected | diff --git a/substrate/core/network/src/custom_proto/upgrade.rs b/substrate/core/network/src/legacy_proto/upgrade.rs similarity index 100% rename from substrate/core/network/src/custom_proto/upgrade.rs rename to substrate/core/network/src/legacy_proto/upgrade.rs diff --git a/substrate/core/network/src/lib.rs b/substrate/core/network/src/lib.rs index bbb1f085566da49317c1819976472b345dd37f20..33f9e5d2eb8ddf83619254302eeaab8696d420b2 100644 --- a/substrate/core/network/src/lib.rs +++ b/substrate/core/network/src/lib.rs @@ -170,7 +170,7 @@ mod behaviour; mod chain; -mod custom_proto; +mod legacy_proto; mod debug_info; mod discovery; mod on_demand_layer; diff --git a/substrate/core/network/src/protocol.rs b/substrate/core/network/src/protocol.rs index 9d4a537ed208d32a8013da7a60cb7215a24adf53..88252d77866503747df84a8245a9058263ae348f 100644 --- a/substrate/core/network/src/protocol.rs +++ b/substrate/core/network/src/protocol.rs @@ -15,7 +15,7 @@ // along with Substrate. If not, see <http://www.gnu.org/licenses/>. use crate::{DiscoveryNetBehaviour, config::ProtocolId}; -use crate::custom_proto::{CustomProto, CustomProtoOut}; +use crate::legacy_proto::{LegacyProto, LegacyProtoOut}; use futures::prelude::*; use futures03::{StreamExt as _, TryStreamExt as _}; use libp2p::{Multiaddr, PeerId}; @@ -111,7 +111,7 @@ pub struct Protocol<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> { /// When asked for a proof of finality, we use this struct to build one. finality_proof_provider: Option<Arc<dyn FinalityProofProvider<B>>>, /// Handles opening the unique substream and sending and receiving raw messages. - behaviour: CustomProto<B, Substream<StreamMuxerBox>>, + behaviour: LegacyProto<B, Substream<StreamMuxerBox>>, } /// A peer that we are connected to @@ -150,7 +150,7 @@ pub struct PeerInfo<B: BlockT> { } struct LightDispatchIn<'a, B: BlockT> { - behaviour: &'a mut CustomProto<B, Substream<StreamMuxerBox>>, + behaviour: &'a mut LegacyProto<B, Substream<StreamMuxerBox>>, peerset: peerset::PeersetHandle, } @@ -281,7 +281,7 @@ pub trait Context<B: BlockT> { /// Protocol context. struct ProtocolContext<'a, B: 'a + BlockT, H: 'a + ExHashT> { - behaviour: &'a mut CustomProto<B, Substream<StreamMuxerBox>>, + behaviour: &'a mut LegacyProto<B, Substream<StreamMuxerBox>>, context_data: &'a mut ContextData<B, H>, peerset_handle: &'a peerset::PeersetHandle, } @@ -289,7 +289,7 @@ struct ProtocolContext<'a, B: 'a + BlockT, H: 'a + ExHashT> { impl<'a, B: BlockT + 'a, H: 'a + ExHashT> ProtocolContext<'a, B, H> { fn new( context_data: &'a mut ContextData<B, H>, - behaviour: &'a mut CustomProto<B, Substream<StreamMuxerBox>>, + behaviour: &'a mut LegacyProto<B, Substream<StreamMuxerBox>>, peerset_handle: &'a peerset::PeersetHandle, ) -> Self { ProtocolContext { context_data, peerset_handle, behaviour } @@ -363,7 +363,7 @@ impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> Protocol<B, S, H> { let sync = ChainSync::new(config.roles, chain.clone(), &info, finality_proof_request_builder); let (peerset, peerset_handle) = peerset::Peerset::from_config(peerset_config); let versions = &((MIN_VERSION as u8)..=(CURRENT_VERSION as u8)).collect::<Vec<u8>>(); - let behaviour = CustomProto::new(protocol_id, versions, peerset); + let behaviour = LegacyProto::new(protocol_id, versions, peerset); let protocol = Protocol { tick_timeout: Box::new(futures_timer::Interval::new(TICK_TIMEOUT).map(|v| Ok::<_, ()>(v)).compat()), @@ -1479,7 +1479,7 @@ pub enum CustomMessageOutcome<B: BlockT> { } fn send_message<B: BlockT, H: ExHashT>( - behaviour: &mut CustomProto<B, Substream<StreamMuxerBox>>, + behaviour: &mut LegacyProto<B, Substream<StreamMuxerBox>>, peers: &mut HashMap<PeerId, Peer<B, H>>, who: PeerId, mut message: Message<B>, @@ -1500,7 +1500,7 @@ fn send_message<B: BlockT, H: ExHashT>( impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> NetworkBehaviour for Protocol<B, S, H> { - type ProtocolsHandler = <CustomProto<B, Substream<StreamMuxerBox>> as NetworkBehaviour>::ProtocolsHandler; + type ProtocolsHandler = <LegacyProto<B, Substream<StreamMuxerBox>> as NetworkBehaviour>::ProtocolsHandler; type OutEvent = CustomMessageOutcome<B>; fn new_handler(&mut self) -> Self::ProtocolsHandler { @@ -1568,7 +1568,7 @@ Protocol<B, S, H> { }; let outcome = match event { - CustomProtoOut::CustomProtocolOpen { peer_id, version, .. } => { + LegacyProtoOut::CustomProtocolOpen { peer_id, version, .. } => { debug_assert!( version <= CURRENT_VERSION as u8 && version >= MIN_VERSION as u8 @@ -1576,13 +1576,13 @@ Protocol<B, S, H> { self.on_peer_connected(peer_id); CustomMessageOutcome::None } - CustomProtoOut::CustomProtocolClosed { peer_id, .. } => { + LegacyProtoOut::CustomProtocolClosed { peer_id, .. } => { self.on_peer_disconnected(peer_id); CustomMessageOutcome::None }, - CustomProtoOut::CustomMessage { peer_id, message } => + LegacyProtoOut::CustomMessage { peer_id, message } => self.on_custom_message(peer_id, message), - CustomProtoOut::Clogged { peer_id, messages } => { + LegacyProtoOut::Clogged { peer_id, messages } => { debug!(target: "sync", "{} clogging messages:", messages.len()); for msg in messages.into_iter().take(5) { debug!(target: "sync", "{:?}", msg);