Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Extract protocol name generation into public module
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-markin committed Jul 29, 2022
1 parent 2b1381b commit 4c1be8c
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 73 deletions.
1 change: 1 addition & 0 deletions client/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ pub mod bitswap;
pub mod config;
pub mod error;
pub mod network_state;
pub mod protocol_name;
pub mod transactions;

#[doc(inline)]
Expand Down
97 changes: 97 additions & 0 deletions client/network/src/protocol_name.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// This file is part of Substrate.

// Copyright (C) 2017-2022 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! On-the-wire protocol name generation helpers.
use crate::config::ProtocolId;

/// Standard protocol name on the wire based on `genesis_hash`, `fork_id`, and protocol-specific
/// `short_name`.
///
/// `short_name` must include the leading slash, e.g.: "/transactions/1".
pub fn standard_protocol_name<Hash: AsRef<[u8]>>(
genesis_hash: Hash,
fork_id: &Option<String>,
short_name: &str,
) -> std::borrow::Cow<'static, str> {
let chain_prefix = match fork_id {
Some(fork_id) => format!("/{}/{}", hex::encode(genesis_hash), fork_id),
None => format!("/{}", hex::encode(genesis_hash)),
};
format!("{}{}", chain_prefix, short_name).into()
}

/// Legacy (fallback) protocol name on the wire based on [`ProtocolId`].
pub fn legacy_protocol_name(
protocol_id: &ProtocolId,
short_name: &str,
) -> std::borrow::Cow<'static, str> {
format!("/{}{}", protocol_id.as_ref(), short_name).into()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn standard_protocol_name_test() {
const SHORT_NAME: &str = "/protocol/1";

// Create protocol name using random genesis hash and no fork id.
let genesis_hash = sp_core::H256::random();
let fork_id = None;
let expected = format!("/{}/protocol/1", hex::encode(genesis_hash));
let protocol_name = standard_protocol_name(genesis_hash, &fork_id, SHORT_NAME);
assert_eq!(protocol_name.to_string(), expected);

// Create protocol name with fork id.
let fork_id = Some("fork".to_string());
let expected = format!("/{}/fork/protocol/1", hex::encode(genesis_hash));
let protocol_name = standard_protocol_name(genesis_hash, &fork_id, SHORT_NAME);
assert_eq!(protocol_name.to_string(), expected);

// Create protocol name using hardcoded genesis hash. Verify exact representation.
let genesis_hash = [
53, 79, 112, 97, 119, 217, 39, 202, 147, 138, 225, 38, 88, 182, 215, 185, 110, 88, 8,
53, 125, 210, 158, 151, 50, 113, 102, 59, 245, 199, 221, 240,
];
let fork_id = None;
let expected =
"/354f706177d927ca938ae12658b6d7b96e5808357dd29e973271663bf5c7ddf0/protocol/1";
let protocol_name = standard_protocol_name(genesis_hash, &fork_id, SHORT_NAME);
assert_eq!(protocol_name.to_string(), expected.to_string());

// Create protocol name using hardcoded genesis hash and fork_id.
// Verify exact representation.
let fork_id = Some("fork".to_string());
let expected =
"/354f706177d927ca938ae12658b6d7b96e5808357dd29e973271663bf5c7ddf0/fork/protocol/1";
let protocol_name = standard_protocol_name(genesis_hash, &fork_id, SHORT_NAME);
assert_eq!(protocol_name.to_string(), expected.to_string());
}

#[test]
fn legacy_protocol_name_test() {
const SHORT_NAME: &str = "/protocol/1";

let protocol_id = ProtocolId::from("dot");
let expected = "/dot/protocol/1";
let legacy_name = legacy_protocol_name(&protocol_id, SHORT_NAME);
assert_eq!(legacy_name.to_string(), expected.to_string());
}
}
3 changes: 2 additions & 1 deletion client/network/src/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ use crate::{
config::{self, TransactionImport, TransactionImportFuture, TransactionPool},
error,
protocol::message,
protocol_name::{legacy_protocol_name, standard_protocol_name},
service::NetworkService,
utils::{interval, legacy_protocol_name, standard_protocol_name, LruHashSet},
utils::{interval, LruHashSet},
Event, ExHashT, ObservedRole,
};

Expand Down
72 changes: 0 additions & 72 deletions client/network/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
// 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::config::ProtocolId;
use futures::{stream::unfold, FutureExt, Stream, StreamExt};
use futures_timer::Delay;
use linked_hash_set::LinkedHashSet;
Expand Down Expand Up @@ -58,30 +57,6 @@ impl<T: Hash + Eq> LruHashSet<T> {
}
}

/// Standard protocol name on the wire based on `genesis_hash`, `fork_id`, and protocol-specific
/// `short_name`.
///
/// `short_name` must include the leading slash, e.g.: "/transactions/1".
pub fn standard_protocol_name<Hash: AsRef<[u8]>>(
genesis_hash: Hash,
fork_id: &Option<String>,
short_name: &str,
) -> std::borrow::Cow<'static, str> {
let chain_prefix = match fork_id {
Some(fork_id) => format!("/{}/{}", hex::encode(genesis_hash), fork_id),
None => format!("/{}", hex::encode(genesis_hash)),
};
format!("{}{}", chain_prefix, short_name).into()
}

/// Legacy (fallback) protocol name on the wire based on [`ProtocolId`].
pub fn legacy_protocol_name(
protocol_id: &ProtocolId,
short_name: &str,
) -> std::borrow::Cow<'static, str> {
format!("/{}{}", protocol_id.as_ref(), short_name).into()
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -107,51 +82,4 @@ mod tests {
assert!(set.insert(3));
assert_eq!(vec![&1, &3], set.set.iter().collect::<Vec<_>>());
}

#[test]
fn standard_protocol_name_test() {
const SHORT_NAME: &str = "/protocol/1";

// Create protocol name using random genesis hash and no fork id.
let genesis_hash = sp_core::H256::random();
let fork_id = None;
let expected = format!("/{}/protocol/1", hex::encode(genesis_hash));
let protocol_name = standard_protocol_name(genesis_hash, &fork_id, SHORT_NAME);
assert_eq!(protocol_name.to_string(), expected);

// Create protocol name with fork id.
let fork_id = Some("fork".to_string());
let expected = format!("/{}/fork/protocol/1", hex::encode(genesis_hash));
let protocol_name = standard_protocol_name(genesis_hash, &fork_id, SHORT_NAME);
assert_eq!(protocol_name.to_string(), expected);

// Create protocol name using hardcoded genesis hash. Verify exact representation.
let genesis_hash = [
53, 79, 112, 97, 119, 217, 39, 202, 147, 138, 225, 38, 88, 182, 215, 185, 110, 88, 8,
53, 125, 210, 158, 151, 50, 113, 102, 59, 245, 199, 221, 240,
];
let fork_id = None;
let expected =
"/354f706177d927ca938ae12658b6d7b96e5808357dd29e973271663bf5c7ddf0/protocol/1";
let protocol_name = standard_protocol_name(genesis_hash, &fork_id, SHORT_NAME);
assert_eq!(protocol_name.to_string(), expected.to_string());

// Create protocol name using hardcoded genesis hash and fork_id.
// Verify exact representation.
let fork_id = Some("fork".to_string());
let expected =
"/354f706177d927ca938ae12658b6d7b96e5808357dd29e973271663bf5c7ddf0/fork/protocol/1";
let protocol_name = standard_protocol_name(genesis_hash, &fork_id, SHORT_NAME);
assert_eq!(protocol_name.to_string(), expected.to_string());
}

#[test]
fn legacy_protocol_name_test() {
const SHORT_NAME: &str = "/protocol/1";

let protocol_id = ProtocolId::from("dot");
let expected = "/dot/protocol/1";
let legacy_name = legacy_protocol_name(&protocol_id, SHORT_NAME);
assert_eq!(legacy_name.to_string(), expected.to_string());
}
}

0 comments on commit 4c1be8c

Please sign in to comment.