client/*: Treat protocol name as str and not [u8] (#6967)

* client/*: Treat protocol name as str and not [u8]

Notification protocol names are in practice always valid utf8 strings.
Instead of treating them as such in the type system, thus far they were
casted to a [u8] at creation time.

With this commit protocol names are instead treated as valid utf8
strings throughout the codebase and passed as `Cow<'static, str>`
instead of `Cow<'static, [u8]>`. Among other things this eliminates the
need for string casting when logging.

* client/network: Don't allocate when protocol name is borrowed
This commit is contained in:
Max Inden
2020-08-28 17:34:25 +02:00
committed by GitHub
parent ff1e8150e1
commit 8fd343e39d
16 changed files with 87 additions and 76 deletions
+6 -6
View File
@@ -105,7 +105,7 @@ pub struct NetworkService<B: BlockT + 'static, H: ExHashT> {
/// that peer. Updated by the [`NetworkWorker`].
peers_notifications_sinks: Arc<Mutex<HashMap<(PeerId, ConsensusEngineId), NotificationsSink>>>,
/// For each legacy gossiping engine ID, the corresponding new protocol name.
protocol_name_by_engine: Mutex<HashMap<ConsensusEngineId, Cow<'static, [u8]>>>,
protocol_name_by_engine: Mutex<HashMap<ConsensusEngineId, Cow<'static, str>>>,
/// Field extracted from the [`Metrics`] struct and necessary to report the
/// notifications-related metrics.
notifications_sizes_metric: Option<HistogramVec>,
@@ -646,7 +646,7 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkService<B, H> {
})
});
sink.send_sync_notification(&protocol_name, fallback, message);
sink.send_sync_notification(protocol_name, fallback, message);
} else {
return;
}
@@ -828,7 +828,7 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkService<B, H> {
pub fn register_notifications_protocol(
&self,
engine_id: ConsensusEngineId,
protocol_name: impl Into<Cow<'static, [u8]>>,
protocol_name: impl Into<Cow<'static, str>>,
) {
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<NotificationSenderReady<'a>, 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<B: BlockT, H: ExHashT> {
},
RegisterNotifProtocol {
engine_id: ConsensusEngineId,
protocol_name: Cow<'static, [u8]>,
protocol_name: Cow<'static, str>,
},
DisconnectPeer(PeerId),
UpdateChain,