Use custom type for ProtocolName (#12172)

* Add ProtocolName custom type

* Use new ProtocolName in sc_network_common

* Use new ProtocolName in sc_network

* Use new ProtocolName for BEEFY and GRANDPA

* Use new ProtocolName for notifications

* Use new ProtocolName in sc_network (part 2)

* Use new ProtocolName in sc_network_gossip

* Use new ProtocolName in sc_offchain

* Remove unused imports

* Some more fixes

* Add tests

* Fix minor import issues

* Re-export ProtocolName in sc_network

* Revert "Re-export ProtocolName in sc_network"

This reverts commit 8d8ff71927e7750757f29c9bbd88dc0ba181d214.

* Re-export ProtocolName in sc_network

* Remove dependency on sc-network-common from beefy-gadget
This commit is contained in:
Dmitry Markin
2022-09-03 23:34:47 +03:00
committed by GitHub
parent 09a52ef882
commit 24d09fe8c7
27 changed files with 381 additions and 280 deletions
@@ -70,6 +70,7 @@ pub(crate) mod tests;
pub mod grandpa_protocol_name {
use sc_chain_spec::ChainSpec;
use sc_network_common::protocol::ProtocolName;
pub(crate) const NAME: &str = "/grandpa/1";
/// Old names for the notifications protocol, used for backward compatibility.
@@ -81,7 +82,7 @@ pub mod grandpa_protocol_name {
pub fn standard_name<Hash: AsRef<[u8]>>(
genesis_hash: &Hash,
chain_spec: &Box<dyn ChainSpec>,
) -> std::borrow::Cow<'static, str> {
) -> ProtocolName {
let chain_prefix = match chain_spec.fork_id() {
Some(fork_id) => format!("/{}/{}", hex::encode(genesis_hash), fork_id),
None => format!("/{}", hex::encode(genesis_hash)),
@@ -28,7 +28,10 @@ use parity_scale_codec::Encode;
use sc_network::{config::Role, Multiaddr, PeerId, ReputationChange};
use sc_network_common::{
config::MultiaddrWithPeerId,
protocol::event::{Event as NetworkEvent, ObservedRole},
protocol::{
event::{Event as NetworkEvent, ObservedRole},
ProtocolName,
},
service::{
NetworkBlock, NetworkEventStream, NetworkNotification, NetworkPeers,
NetworkSyncForkRequest, NotificationSender, NotificationSenderError,
@@ -41,7 +44,6 @@ use sp_finality_grandpa::AuthorityList;
use sp_keyring::Ed25519Keyring;
use sp_runtime::traits::NumberFor;
use std::{
borrow::Cow,
collections::HashSet,
pin::Pin,
sync::Arc,
@@ -78,7 +80,7 @@ impl NetworkPeers for TestNetwork {
let _ = self.sender.unbounded_send(Event::Report(who, cost_benefit));
}
fn disconnect_peer(&self, _who: PeerId, _protocol: Cow<'static, str>) {}
fn disconnect_peer(&self, _who: PeerId, _protocol: ProtocolName) {}
fn accept_unreserved_peers(&self) {
unimplemented!();
@@ -98,7 +100,7 @@ impl NetworkPeers for TestNetwork {
fn set_reserved_peers(
&self,
_protocol: Cow<'static, str>,
_protocol: ProtocolName,
_peers: HashSet<Multiaddr>,
) -> Result<(), String> {
unimplemented!();
@@ -106,23 +108,23 @@ impl NetworkPeers for TestNetwork {
fn add_peers_to_reserved_set(
&self,
_protocol: Cow<'static, str>,
_protocol: ProtocolName,
_peers: HashSet<Multiaddr>,
) -> Result<(), String> {
unimplemented!();
}
fn remove_peers_from_reserved_set(&self, _protocol: Cow<'static, str>, _peers: Vec<PeerId>) {}
fn remove_peers_from_reserved_set(&self, _protocol: ProtocolName, _peers: Vec<PeerId>) {}
fn add_to_peers_set(
&self,
_protocol: Cow<'static, str>,
_protocol: ProtocolName,
_peers: HashSet<Multiaddr>,
) -> Result<(), String> {
unimplemented!();
}
fn remove_from_peers_set(&self, _protocol: Cow<'static, str>, _peers: Vec<PeerId>) {
fn remove_from_peers_set(&self, _protocol: ProtocolName, _peers: Vec<PeerId>) {
unimplemented!();
}
@@ -143,14 +145,14 @@ impl NetworkEventStream for TestNetwork {
}
impl NetworkNotification for TestNetwork {
fn write_notification(&self, target: PeerId, _protocol: Cow<'static, str>, message: Vec<u8>) {
fn write_notification(&self, target: PeerId, _protocol: ProtocolName, message: Vec<u8>) {
let _ = self.sender.unbounded_send(Event::WriteNotification(target, message));
}
fn notification_sender(
&self,
_target: PeerId,
_protocol: Cow<'static, str>,
_protocol: ProtocolName,
) -> Result<Box<dyn NotificationSender>, NotificationSenderError> {
unimplemented!();
}
+3 -2
View File
@@ -68,6 +68,7 @@ use sc_client_api::{
StorageProvider, TransactionFor,
};
use sc_consensus::BlockImport;
use sc_network_common::protocol::ProtocolName;
use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_DEBUG, CONSENSUS_INFO};
use sc_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver};
use sp_api::ProvideRuntimeApi;
@@ -266,7 +267,7 @@ pub struct Config {
/// TelemetryHandle instance.
pub telemetry: Option<TelemetryHandle>,
/// Chain specific GRANDPA protocol name. See [`crate::protocol_standard_name`].
pub protocol_name: std::borrow::Cow<'static, str>,
pub protocol_name: ProtocolName,
}
impl Config {
@@ -723,7 +724,7 @@ pub struct GrandpaParams<Block: BlockT, C, N, SC, VR> {
/// [`sc_network::config::NetworkConfiguration::extra_sets`].
/// For standard protocol name see [`crate::protocol_standard_name`].
pub fn grandpa_peers_set_config(
protocol_name: std::borrow::Cow<'static, str>,
protocol_name: ProtocolName,
) -> sc_network::config::NonDefaultSetConfig {
use communication::grandpa_protocol_name;
sc_network::config::NonDefaultSetConfig {