ProtocolId can now be more than 3 bytes (#2350)

This commit is contained in:
Pierre Krieger
2019-04-23 18:15:21 +02:00
committed by Gavin Wood
parent 517746bd62
commit b483c5608f
4 changed files with 26 additions and 18 deletions
@@ -44,10 +44,11 @@ pub struct RegisteredProtocol<TMessage> {
impl<TMessage> RegisteredProtocol<TMessage> {
/// Creates a new `RegisteredProtocol`. The `custom_data` parameter will be
/// passed inside the `RegisteredProtocolOutput`.
pub fn new(protocol: ProtocolId, versions: &[u8])
pub fn new(protocol: impl Into<ProtocolId>, versions: &[u8])
-> Self {
let protocol = protocol.into();
let mut base_name = Bytes::from_static(b"/substrate/");
base_name.extend_from_slice(&protocol);
base_name.extend_from_slice(protocol.as_bytes());
base_name.extend_from_slice(b"/");
RegisteredProtocol {
@@ -63,16 +64,15 @@ impl<TMessage> RegisteredProtocol<TMessage> {
}
/// Returns the ID of the protocol.
#[inline]
pub fn id(&self) -> ProtocolId {
self.id
pub fn id(&self) -> &ProtocolId {
&self.id
}
}
impl<TMessage> Clone for RegisteredProtocol<TMessage> {
fn clone(&self) -> Self {
RegisteredProtocol {
id: self.id,
id: self.id.clone(),
base_name: self.base_name.clone(),
supported_versions: self.supported_versions.clone(),
marker: PhantomData,
@@ -110,8 +110,8 @@ pub struct RegisteredProtocolSubstream<TMessage, TSubstream> {
impl<TMessage, TSubstream> RegisteredProtocolSubstream<TMessage, TSubstream> {
/// Returns the protocol id.
#[inline]
pub fn protocol_id(&self) -> ProtocolId {
self.protocol_id
pub fn protocol_id(&self) -> &ProtocolId {
&self.protocol_id
}
/// Returns the version of the protocol that was negotiated.
+16 -2
View File
@@ -38,8 +38,22 @@ use serde_derive::{Deserialize, Serialize};
use slog_derive::SerdeValue;
use std::{collections::{HashMap, HashSet}, error, fmt, time::Duration};
/// Protocol / handler id
pub type ProtocolId = [u8; 3];
/// Name of a protocol, transmitted on the wire. Should be unique for each chain.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ProtocolId(smallvec::SmallVec<[u8; 6]>);
impl<'a> From<&'a [u8]> for ProtocolId {
fn from(bytes: &'a [u8]) -> ProtocolId {
ProtocolId(bytes.into())
}
}
impl ProtocolId {
/// Exposes the `ProtocolId` as bytes.
pub fn as_bytes(&self) -> &[u8] {
self.0.as_ref()
}
}
/// Parses a string address and returns the component, if valid.
pub fn parse_str_addr(addr_str: &str) -> Result<(PeerId, Multiaddr), ParseErr> {