diff --git a/client/finality-grandpa/src/communication/mod.rs b/client/finality-grandpa/src/communication/mod.rs index a8bfb84416b8f..9509922cf2d3a 100644 --- a/client/finality-grandpa/src/communication/mod.rs +++ b/client/finality-grandpa/src/communication/mod.rs @@ -69,7 +69,7 @@ mod periodic; pub(crate) mod tests; pub use sp_finality_grandpa::GRANDPA_ENGINE_ID; -pub const GRANDPA_PROTOCOL_NAME: &[u8] = b"/paritytech/grandpa/1"; +pub const GRANDPA_PROTOCOL_NAME: &'static str = "/paritytech/grandpa/1"; // cost scalars for reporting peers. mod cost { diff --git a/client/finality-grandpa/src/communication/tests.rs b/client/finality-grandpa/src/communication/tests.rs index 273804f7a4508..6a1513769aa26 100644 --- a/client/finality-grandpa/src/communication/tests.rs +++ b/client/finality-grandpa/src/communication/tests.rs @@ -61,7 +61,7 @@ impl sc_network_gossip::Network for TestNetwork { let _ = self.sender.unbounded_send(Event::WriteNotification(who, message)); } - fn register_notifications_protocol(&self, _: ConsensusEngineId, _: Cow<'static, [u8]>) {} + fn register_notifications_protocol(&self, _: ConsensusEngineId, _: Cow<'static, str>) {} fn announce(&self, block: Hash, _associated_data: Vec) { let _ = self.sender.unbounded_send(Event::Announce(block)); diff --git a/client/network-gossip/src/bridge.rs b/client/network-gossip/src/bridge.rs index df2a5c8e7e94a..70c2942597aa5 100644 --- a/client/network-gossip/src/bridge.rs +++ b/client/network-gossip/src/bridge.rs @@ -69,7 +69,7 @@ impl GossipEngine { pub fn new + Send + Clone + 'static>( network: N, engine_id: ConsensusEngineId, - protocol_name: impl Into>, + protocol_name: impl Into>, validator: Arc>, ) -> Self where B: 'static { // We grab the event stream before registering the notifications protocol, otherwise we @@ -333,7 +333,7 @@ mod tests { unimplemented!(); } - fn register_notifications_protocol(&self, _: ConsensusEngineId, _: Cow<'static, [u8]>) {} + fn register_notifications_protocol(&self, _: ConsensusEngineId, _: Cow<'static, str>) {} fn announce(&self, _: B::Hash, _: Vec) { unimplemented!(); @@ -362,7 +362,7 @@ mod tests { let mut gossip_engine = GossipEngine::::new( network.clone(), [1, 2, 3, 4], - "my_protocol".as_bytes(), + "my_protocol", Arc::new(AllowAll{}), ); @@ -390,7 +390,7 @@ mod tests { let mut gossip_engine = GossipEngine::::new( network.clone(), engine_id.clone(), - "my_protocol".as_bytes(), + "my_protocol", Arc::new(AllowAll{}), ); @@ -525,7 +525,7 @@ mod tests { let mut gossip_engine = GossipEngine::::new( network.clone(), engine_id.clone(), - "my_protocol".as_bytes(), + "my_protocol", Arc::new(TestValidator{}), ); diff --git a/client/network-gossip/src/lib.rs b/client/network-gossip/src/lib.rs index 42aeca86cb275..1d566ed3cbba2 100644 --- a/client/network-gossip/src/lib.rs +++ b/client/network-gossip/src/lib.rs @@ -87,7 +87,7 @@ pub trait Network { fn register_notifications_protocol( &self, engine_id: ConsensusEngineId, - protocol_name: Cow<'static, [u8]>, + protocol_name: Cow<'static, str>, ); /// Notify everyone we're connected to that we have the given block. @@ -117,7 +117,7 @@ impl Network for Arc> { fn register_notifications_protocol( &self, engine_id: ConsensusEngineId, - protocol_name: Cow<'static, [u8]>, + protocol_name: Cow<'static, str>, ) { NetworkService::register_notifications_protocol(self, engine_id, protocol_name) } diff --git a/client/network-gossip/src/state_machine.rs b/client/network-gossip/src/state_machine.rs index 80a0f9e70bcbf..60c669ecb6680 100644 --- a/client/network-gossip/src/state_machine.rs +++ b/client/network-gossip/src/state_machine.rs @@ -489,7 +489,7 @@ mod tests { unimplemented!(); } - fn register_notifications_protocol(&self, _: ConsensusEngineId, _: Cow<'static, [u8]>) {} + fn register_notifications_protocol(&self, _: ConsensusEngineId, _: Cow<'static, str>) {} fn announce(&self, _: B::Hash, _: Vec) { unimplemented!(); diff --git a/client/network/src/behaviour.rs b/client/network/src/behaviour.rs index 4a47a26f55c24..2c399cfdf7707 100644 --- a/client/network/src/behaviour.rs +++ b/client/network/src/behaviour.rs @@ -255,7 +255,7 @@ impl Behaviour { pub fn register_notifications_protocol( &mut self, engine_id: ConsensusEngineId, - protocol_name: impl Into>, + protocol_name: impl Into>, ) { // This is the message that we will send to the remote as part of the initial handshake. // At the moment, we force this to be an encoded `Roles`. diff --git a/client/network/src/config.rs b/client/network/src/config.rs index 5185befacf5ae..cf1f8393f380d 100644 --- a/client/network/src/config.rs +++ b/client/network/src/config.rs @@ -415,7 +415,7 @@ pub struct NetworkConfiguration { pub node_key: NodeKeyConfig, /// List of notifications protocols that the node supports. Must also include a /// `ConsensusEngineId` for backwards-compatibility. - pub notifications_protocols: Vec<(ConsensusEngineId, Cow<'static, [u8]>)>, + pub notifications_protocols: Vec<(ConsensusEngineId, Cow<'static, str>)>, /// List of request-response protocols that the node supports. pub request_response_protocols: Vec, /// Maximum allowed number of incoming connections. diff --git a/client/network/src/gossip/tests.rs b/client/network/src/gossip/tests.rs index 6c3e26da13c64..9ba44f564e132 100644 --- a/client/network/src/gossip/tests.rs +++ b/client/network/src/gossip/tests.rs @@ -130,14 +130,14 @@ fn build_nodes_one_proto() let listen_addr = config::build_multiaddr![Memory(rand::random::())]; let (node1, events_stream1) = build_test_full_node(config::NetworkConfiguration { - notifications_protocols: vec![(ENGINE_ID, From::from(&b"/foo"[..]))], + notifications_protocols: vec![(ENGINE_ID, From::from("/foo"))], listen_addresses: vec![listen_addr.clone()], transport: config::TransportConfig::MemoryOnly, .. config::NetworkConfiguration::new_local() }); let (node2, events_stream2) = build_test_full_node(config::NetworkConfiguration { - notifications_protocols: vec![(ENGINE_ID, From::from(&b"/foo"[..]))], + notifications_protocols: vec![(ENGINE_ID, From::from("/foo"))], listen_addresses: vec![], reserved_nodes: vec![config::MultiaddrWithPeerId { multiaddr: listen_addr, diff --git a/client/network/src/protocol.rs b/client/network/src/protocol.rs index b1945b4dba47d..00cb845274c61 100644 --- a/client/network/src/protocol.rs +++ b/client/network/src/protocol.rs @@ -245,13 +245,13 @@ pub struct Protocol { /// Handles opening the unique substream and sending and receiving raw messages. behaviour: GenericProto, /// For each legacy gossiping engine ID, the corresponding new protocol name. - protocol_name_by_engine: HashMap>, + protocol_name_by_engine: HashMap>, /// For each protocol name, the legacy equivalent. - legacy_equiv_by_name: HashMap, Fallback>, + legacy_equiv_by_name: HashMap, Fallback>, /// Name of the protocol used for transactions. - transactions_protocol: Cow<'static, [u8]>, + transactions_protocol: Cow<'static, str>, /// Name of the protocol used for block announces. - block_announces_protocol: Cow<'static, [u8]>, + block_announces_protocol: Cow<'static, str>, /// Prometheus metrics. metrics: Option, /// The `PeerId`'s of all boot nodes. @@ -417,19 +417,21 @@ impl Protocol { let mut legacy_equiv_by_name = HashMap::new(); - let transactions_protocol: Cow<'static, [u8]> = Cow::from({ - let mut proto = b"/".to_vec(); - proto.extend(protocol_id.as_ref().as_bytes()); - proto.extend(b"/transactions/1"); + let transactions_protocol: Cow<'static, str> = Cow::from({ + let mut proto = String::new(); + proto.push_str("/"); + proto.push_str(protocol_id.as_ref()); + proto.push_str("/transactions/1"); proto }); behaviour.register_notif_protocol(transactions_protocol.clone(), Vec::new()); legacy_equiv_by_name.insert(transactions_protocol.clone(), Fallback::Transactions); - let block_announces_protocol: Cow<'static, [u8]> = Cow::from({ - let mut proto = b"/".to_vec(); - proto.extend(protocol_id.as_ref().as_bytes()); - proto.extend(b"/block-announces/1"); + let block_announces_protocol: Cow<'static, str> = Cow::from({ + let mut proto = String::new(); + proto.push_str("/"); + proto.push_str(protocol_id.as_ref()); + proto.push_str("/block-announces/1"); proto }); behaviour.register_notif_protocol( @@ -679,7 +681,7 @@ impl Protocol { fn send_message( &mut self, who: &PeerId, - message: Option<(Cow<'static, [u8]>, Vec)>, + message: Option<(Cow<'static, str>, Vec)>, legacy: Message, ) { send_message::( @@ -1076,7 +1078,7 @@ impl Protocol { pub fn register_notifications_protocol<'a>( &'a mut self, engine_id: ConsensusEngineId, - protocol_name: impl Into>, + protocol_name: impl Into>, handshake_message: Vec, ) -> impl Iterator + 'a { let protocol_name = protocol_name.into(); @@ -1607,7 +1609,7 @@ fn send_message( behaviour: &mut GenericProto, stats: &mut HashMap<&'static str, PacketStats>, who: &PeerId, - message: Option<(Cow<'static, [u8]>, Vec)>, + message: Option<(Cow<'static, str>, Vec)>, legacy_message: Message, ) { let encoded = legacy_message.encode(); diff --git a/client/network/src/protocol/generic_proto/behaviour.rs b/client/network/src/protocol/generic_proto/behaviour.rs index f965980640ad6..56a5b3fb0ab2d 100644 --- a/client/network/src/protocol/generic_proto/behaviour.rs +++ b/client/network/src/protocol/generic_proto/behaviour.rs @@ -120,7 +120,7 @@ pub struct GenericProto { /// 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. - notif_protocols: Vec<(Cow<'static, [u8]>, Arc>>)>, + notif_protocols: Vec<(Cow<'static, str>, Arc>>)>, /// Receiver for instructions about who to connect to or disconnect from. peerset: sc_peerset::Peerset, @@ -322,7 +322,7 @@ pub enum GenericProtoOut { /// Id of the peer the message came from. peer_id: PeerId, /// Engine corresponding to the message. - protocol_name: Cow<'static, [u8]>, + protocol_name: Cow<'static, str>, /// Message that has been received. message: BytesMut, }, @@ -360,7 +360,7 @@ impl GenericProto { /// will retain the protocols that were registered then, and not any new one. pub fn register_notif_protocol( &mut self, - protocol_name: impl Into>, + protocol_name: impl Into>, handshake_msg: impl Into> ) { self.notif_protocols.push((protocol_name.into(), Arc::new(RwLock::new(handshake_msg.into())))); @@ -371,10 +371,10 @@ impl GenericProto { /// Has no effect if the protocol is unknown. pub fn set_notif_protocol_handshake( &mut self, - protocol_name: &[u8], + protocol_name: &str, handshake_message: impl Into> ) { - if let Some(protocol) = self.notif_protocols.iter_mut().find(|(name, _)| name == &protocol_name) { + if let Some(protocol) = self.notif_protocols.iter_mut().find(|(name, _)| name == protocol_name) { *protocol.1.write() = handshake_message.into(); } } @@ -551,7 +551,7 @@ impl GenericProto { pub fn write_notification( &mut self, target: &PeerId, - protocol_name: Cow<'static, [u8]>, + protocol_name: Cow<'static, str>, message: impl Into>, encoded_fallback_message: Vec, ) { @@ -569,11 +569,11 @@ impl GenericProto { target: "sub-libp2p", "External API => Notification({:?}, {:?})", target, - str::from_utf8(&protocol_name) + protocol_name, ); trace!(target: "sub-libp2p", "Handler({:?}) <= Packet", target); notifs_sink.send_sync_notification( - &protocol_name, + protocol_name, encoded_fallback_message, message ); @@ -1374,7 +1374,7 @@ impl NetworkBehaviour for GenericProto { target: "sub-libp2p", "Handler({:?}) => Notification({:?})", source, - str::from_utf8(&protocol_name) + protocol_name, ); trace!(target: "sub-libp2p", "External API <= Message({:?}, {:?})", protocol_name, source); let event = GenericProtoOut::Notification { diff --git a/client/network/src/protocol/generic_proto/handler/group.rs b/client/network/src/protocol/generic_proto/handler/group.rs index bcdbaf848511f..43627f3d6041c 100644 --- a/client/network/src/protocol/generic_proto/handler/group.rs +++ b/client/network/src/protocol/generic_proto/handler/group.rs @@ -224,7 +224,7 @@ pub enum NotifsHandlerOut { /// Received a message on a custom protocol substream. Notification { /// Name of the protocol of the message. - protocol_name: Cow<'static, [u8]>, + protocol_name: Cow<'static, str>, /// Message that has been received. message: BytesMut, @@ -270,7 +270,7 @@ enum NotificationsSinkMessage { /// Message emitted by [`NotificationsSink::reserve_notification`] and /// [`NotificationsSink::write_notification_now`]. Notification { - protocol_name: Vec, + protocol_name: Cow<'static, str>, encoded_fallback_message: Vec, message: Vec, }, @@ -311,13 +311,13 @@ impl NotificationsSink { /// This method will be removed in a future version. pub fn send_sync_notification<'a>( &'a self, - protocol_name: &[u8], + protocol_name: Cow<'static, str>, encoded_fallback_message: impl Into>, message: impl Into> ) { let mut lock = self.inner.sync_channel.lock(); let result = lock.try_send(NotificationsSinkMessage::Notification { - protocol_name: protocol_name.to_owned(), + protocol_name: protocol_name, encoded_fallback_message: encoded_fallback_message.into(), message: message.into() }); @@ -336,12 +336,12 @@ impl NotificationsSink { /// /// The protocol name is expected to be checked ahead of calling this method. It is a logic /// error to send a notification using an unknown protocol. - pub async fn reserve_notification<'a>(&'a self, protocol_name: &[u8]) -> Result, ()> { + pub async fn reserve_notification<'a>(&'a self, protocol_name: Cow<'static, str>) -> Result, ()> { let mut lock = self.inner.async_channel.lock().await; let poll_ready = future::poll_fn(|cx| lock.poll_ready(cx)).await; if poll_ready.is_ok() { - Ok(Ready { protocol_name: protocol_name.to_owned(), lock }) + Ok(Ready { protocol_name: protocol_name, lock }) } else { Err(()) } @@ -355,7 +355,7 @@ pub struct Ready<'a> { /// Guarded channel. The channel inside is guaranteed to not be full. lock: FuturesMutexGuard<'a, mpsc::Sender>, /// Name of the protocol. Should match one of the protocols passed at initialization. - protocol_name: Vec, + protocol_name: Cow<'static, str>, } impl<'a> Ready<'a> { @@ -392,7 +392,7 @@ impl NotifsHandlerProto { /// ourselves or respond to handshake from the remote. pub fn new( legacy: RegisteredProtocol, - list: impl Into, Arc>>)>>, + list: impl Into, Arc>>)>>, ) -> Self { let list = list.into(); @@ -613,7 +613,7 @@ impl ProtocolsHandler for NotifsHandler { message } => { for (handler, _) in &mut self.out_handlers { - if handler.protocol_name() == &protocol_name[..] && handler.is_open() { + if *handler.protocol_name() == protocol_name && handler.is_open() { handler.send_or_discard(message); continue 'poll_notifs_sink; } @@ -698,7 +698,7 @@ impl ProtocolsHandler for NotifsHandler { if self.notifications_sink_rx.is_some() { let msg = NotifsHandlerOut::Notification { message, - protocol_name: handler.protocol_name().to_owned().into(), + protocol_name: handler.protocol_name().clone(), }; return Poll::Ready(ProtocolsHandlerEvent::Custom(msg)); } diff --git a/client/network/src/protocol/generic_proto/handler/notif_in.rs b/client/network/src/protocol/generic_proto/handler/notif_in.rs index ddd78566fcd2a..9eb8ec7471610 100644 --- a/client/network/src/protocol/generic_proto/handler/notif_in.rs +++ b/client/network/src/protocol/generic_proto/handler/notif_in.rs @@ -109,7 +109,7 @@ pub enum NotifsInHandlerOut { impl NotifsInHandlerProto { /// Builds a new `NotifsInHandlerProto`. pub fn new( - protocol_name: impl Into> + protocol_name: impl Into> ) -> Self { NotifsInHandlerProto { in_protocol: NotificationsIn::new(protocol_name), @@ -136,7 +136,7 @@ impl IntoProtocolsHandler for NotifsInHandlerProto { impl NotifsInHandler { /// Returns the name of the protocol that we accept. - pub fn protocol_name(&self) -> &[u8] { + pub fn protocol_name(&self) -> &Cow<'static, str> { self.in_protocol.protocol_name() } } diff --git a/client/network/src/protocol/generic_proto/handler/notif_out.rs b/client/network/src/protocol/generic_proto/handler/notif_out.rs index 4ba9d9a0b74aa..4079d2fa2a6b2 100644 --- a/client/network/src/protocol/generic_proto/handler/notif_out.rs +++ b/client/network/src/protocol/generic_proto/handler/notif_out.rs @@ -57,13 +57,13 @@ const INITIAL_KEEPALIVE_TIME: Duration = Duration::from_secs(5); /// See the documentation of [`NotifsOutHandler`] for more information. pub struct NotifsOutHandlerProto { /// Name of the protocol to negotiate. - protocol_name: Cow<'static, [u8]>, + protocol_name: Cow<'static, str>, } impl NotifsOutHandlerProto { /// Builds a new [`NotifsOutHandlerProto`]. Will use the given protocol name for the /// notifications substream. - pub fn new(protocol_name: impl Into>) -> Self { + pub fn new(protocol_name: impl Into>) -> Self { NotifsOutHandlerProto { protocol_name: protocol_name.into(), } @@ -97,7 +97,7 @@ impl IntoProtocolsHandler for NotifsOutHandlerProto { /// the remote for the purpose of sending notifications to it. pub struct NotifsOutHandler { /// Name of the protocol to negotiate. - protocol_name: Cow<'static, [u8]>, + protocol_name: Cow<'static, str>, /// Relationship with the node we're connected to. state: State, @@ -220,7 +220,7 @@ impl NotifsOutHandler { } /// Returns the name of the protocol that we negotiate. - pub fn protocol_name(&self) -> &[u8] { + pub fn protocol_name(&self) -> &Cow<'static, str> { &self.protocol_name } diff --git a/client/network/src/protocol/generic_proto/upgrade/notifications.rs b/client/network/src/protocol/generic_proto/upgrade/notifications.rs index 80fd7761f8088..6b636607d8031 100644 --- a/client/network/src/protocol/generic_proto/upgrade/notifications.rs +++ b/client/network/src/protocol/generic_proto/upgrade/notifications.rs @@ -50,7 +50,7 @@ const MAX_HANDSHAKE_SIZE: usize = 1024; #[derive(Debug, Clone)] pub struct NotificationsIn { /// Protocol name to use when negotiating the substream. - protocol_name: Cow<'static, [u8]>, + protocol_name: Cow<'static, str>, } /// Upgrade that opens a substream, waits for the remote to accept by sending back a status @@ -58,7 +58,7 @@ pub struct NotificationsIn { #[derive(Debug, Clone)] pub struct NotificationsOut { /// Protocol name to use when negotiating the substream. - protocol_name: Cow<'static, [u8]>, + protocol_name: Cow<'static, str>, /// Message to send when we start the handshake. initial_message: Vec, } @@ -100,14 +100,14 @@ pub struct NotificationsOutSubstream { impl NotificationsIn { /// Builds a new potential upgrade. - pub fn new(protocol_name: impl Into>) -> Self { + pub fn new(protocol_name: impl Into>) -> Self { NotificationsIn { protocol_name: protocol_name.into(), } } /// Returns the name of the protocol that we accept. - pub fn protocol_name(&self) -> &[u8] { + pub fn protocol_name(&self) -> &Cow<'static, str> { &self.protocol_name } } @@ -117,7 +117,11 @@ impl UpgradeInfo for NotificationsIn { type InfoIter = iter::Once; fn protocol_info(&self) -> Self::InfoIter { - iter::once(self.protocol_name.clone()) + let bytes: Cow<'static, [u8]> = match &self.protocol_name { + Cow::Borrowed(s) => Cow::Borrowed(s.as_bytes()), + Cow::Owned(s) => Cow::Owned(s.as_bytes().to_vec()) + }; + iter::once(bytes) } } @@ -244,7 +248,7 @@ where TSubstream: AsyncRead + AsyncWrite + Unpin, impl NotificationsOut { /// Builds a new potential upgrade. - pub fn new(protocol_name: impl Into>, initial_message: impl Into>) -> Self { + pub fn new(protocol_name: impl Into>, initial_message: impl Into>) -> Self { let initial_message = initial_message.into(); if initial_message.len() > MAX_HANDSHAKE_SIZE { error!(target: "sub-libp2p", "Outbound networking handshake is above allowed protocol limit"); @@ -262,7 +266,11 @@ impl UpgradeInfo for NotificationsOut { type InfoIter = iter::Once; fn protocol_info(&self) -> Self::InfoIter { - iter::once(self.protocol_name.clone()) + let bytes: Cow<'static, [u8]> = match &self.protocol_name { + Cow::Borrowed(s) => Cow::Borrowed(s.as_bytes()), + Cow::Owned(s) => Cow::Owned(s.as_bytes().to_vec()) + }; + iter::once(bytes) } } @@ -378,10 +386,11 @@ mod tests { use async_std::net::{TcpListener, TcpStream}; use futures::{prelude::*, channel::oneshot}; use libp2p::core::upgrade; + use std::borrow::Cow; #[test] fn basic_works() { - const PROTO_NAME: &'static [u8] = b"/test/proto/1"; + const PROTO_NAME: Cow<'static, str> = Cow::Borrowed("/test/proto/1"); let (listener_addr_tx, listener_addr_rx) = oneshot::channel(); let client = async_std::task::spawn(async move { @@ -420,7 +429,7 @@ mod tests { fn empty_handshake() { // Check that everything still works when the handshake messages are empty. - const PROTO_NAME: &'static [u8] = b"/test/proto/1"; + const PROTO_NAME: Cow<'static, str> = Cow::Borrowed("/test/proto/1"); let (listener_addr_tx, listener_addr_rx) = oneshot::channel(); let client = async_std::task::spawn(async move { @@ -457,7 +466,7 @@ mod tests { #[test] fn refused() { - const PROTO_NAME: &'static [u8] = b"/test/proto/1"; + const PROTO_NAME: Cow<'static, str> = Cow::Borrowed("/test/proto/1"); let (listener_addr_tx, listener_addr_rx) = oneshot::channel(); let client = async_std::task::spawn(async move { @@ -495,7 +504,7 @@ mod tests { #[test] fn large_initial_message_refused() { - const PROTO_NAME: &'static [u8] = b"/test/proto/1"; + const PROTO_NAME: Cow<'static, str> = Cow::Borrowed("/test/proto/1"); let (listener_addr_tx, listener_addr_rx) = oneshot::channel(); let client = async_std::task::spawn(async move { @@ -526,7 +535,7 @@ mod tests { #[test] fn large_handshake_refused() { - const PROTO_NAME: &'static [u8] = b"/test/proto/1"; + const PROTO_NAME: Cow<'static, str> = Cow::Borrowed("/test/proto/1"); let (listener_addr_tx, listener_addr_rx) = oneshot::channel(); let client = async_std::task::spawn(async move { diff --git a/client/network/src/service.rs b/client/network/src/service.rs index 754b5b184c096..a3ac8371dc739 100644 --- a/client/network/src/service.rs +++ b/client/network/src/service.rs @@ -105,7 +105,7 @@ pub struct NetworkService { /// that peer. Updated by the [`NetworkWorker`]. peers_notifications_sinks: Arc>>, /// For each legacy gossiping engine ID, the corresponding new protocol name. - protocol_name_by_engine: Mutex>>, + protocol_name_by_engine: Mutex>>, /// Field extracted from the [`Metrics`] struct and necessary to report the /// notifications-related metrics. notifications_sizes_metric: Option, @@ -646,7 +646,7 @@ impl NetworkService { }) }); - sink.send_sync_notification(&protocol_name, fallback, message); + sink.send_sync_notification(protocol_name, fallback, message); } else { return; } @@ -828,7 +828,7 @@ impl NetworkService { pub fn register_notifications_protocol( &self, engine_id: ConsensusEngineId, - protocol_name: impl Into>, + protocol_name: impl Into>, ) { let protocol_name = protocol_name.into(); self.protocol_name_by_engine.lock().insert(engine_id, protocol_name.clone()); @@ -1062,7 +1062,7 @@ pub struct NotificationSender { sink: NotificationsSink, /// Name of the protocol on the wire. - protocol_name: Cow<'static, [u8]>, + protocol_name: Cow<'static, str>, /// Engine ID used for the fallback message. engine_id: ConsensusEngineId, @@ -1076,7 +1076,7 @@ impl NotificationSender { /// Returns a future that resolves when the `NotificationSender` is ready to send a notification. pub async fn ready<'a>(&'a self) -> Result, NotificationSenderError> { Ok(NotificationSenderReady { - ready: match self.sink.reserve_notification(&self.protocol_name).await { + ready: match self.sink.reserve_notification(self.protocol_name.clone()).await { Ok(r) => r, Err(()) => return Err(NotificationSenderError::Closed), }, @@ -1158,7 +1158,7 @@ enum ServiceToWorkerMsg { }, RegisterNotifProtocol { engine_id: ConsensusEngineId, - protocol_name: Cow<'static, [u8]>, + protocol_name: Cow<'static, str>, }, DisconnectPeer(PeerId), UpdateChain, diff --git a/client/network/src/service/tests.rs b/client/network/src/service/tests.rs index 5090362e37606..4b6f9dd156482 100644 --- a/client/network/src/service/tests.rs +++ b/client/network/src/service/tests.rs @@ -131,14 +131,14 @@ fn build_nodes_one_proto() let listen_addr = config::build_multiaddr![Memory(rand::random::())]; let (node1, events_stream1) = build_test_full_node(config::NetworkConfiguration { - notifications_protocols: vec![(ENGINE_ID, From::from(&b"/foo"[..]))], + notifications_protocols: vec![(ENGINE_ID, From::from("/foo"))], listen_addresses: vec![listen_addr.clone()], transport: config::TransportConfig::MemoryOnly, .. config::NetworkConfiguration::new_local() }); let (node2, events_stream2) = build_test_full_node(config::NetworkConfiguration { - notifications_protocols: vec![(ENGINE_ID, From::from(&b"/foo"[..]))], + notifications_protocols: vec![(ENGINE_ID, From::from("/foo"))], listen_addresses: vec![], reserved_nodes: vec![config::MultiaddrWithPeerId { multiaddr: listen_addr, @@ -281,7 +281,7 @@ fn lots_of_incoming_peers_works() { let listen_addr = config::build_multiaddr![Memory(rand::random::())]; let (main_node, _) = build_test_full_node(config::NetworkConfiguration { - notifications_protocols: vec![(ENGINE_ID, From::from(&b"/foo"[..]))], + notifications_protocols: vec![(ENGINE_ID, From::from("/foo"))], listen_addresses: vec![listen_addr.clone()], in_peers: u32::max_value(), transport: config::TransportConfig::MemoryOnly, @@ -298,7 +298,7 @@ fn lots_of_incoming_peers_works() { let main_node_peer_id = main_node_peer_id.clone(); let (_dialing_node, event_stream) = build_test_full_node(config::NetworkConfiguration { - notifications_protocols: vec![(ENGINE_ID, From::from(&b"/foo"[..]))], + notifications_protocols: vec![(ENGINE_ID, From::from("/foo"))], listen_addresses: vec![], reserved_nodes: vec![config::MultiaddrWithPeerId { multiaddr: listen_addr.clone(),