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
@@ -22,9 +22,9 @@ use ahash::AHashSet;
use libp2p::PeerId;
use lru::LruCache;
use prometheus_endpoint::{register, Counter, PrometheusError, Registry, U64};
use sc_network_common::protocol::event::ObservedRole;
use sc_network_common::protocol::{event::ObservedRole, ProtocolName};
use sp_runtime::traits::{Block as BlockT, Hash, HashFor};
use std::{borrow::Cow, collections::HashMap, iter, sync::Arc, time, time::Instant};
use std::{collections::HashMap, iter, sync::Arc, time, time::Instant};
// FIXME: Add additional spam/DoS attack protection: https://github.com/paritytech/substrate/issues/1115
// NOTE: The current value is adjusted based on largest production network deployment (Kusama) and
@@ -99,7 +99,7 @@ impl<'g, 'p, B: BlockT> ValidatorContext<B> for NetworkContext<'g, 'p, B> {
fn propagate<'a, B: BlockT, I>(
network: &mut dyn Network<B>,
protocol: Cow<'static, str>,
protocol: ProtocolName,
messages: I,
intent: MessageIntent,
peers: &mut HashMap<PeerId, PeerConsensus<B::Hash>>,
@@ -155,7 +155,7 @@ pub struct ConsensusGossip<B: BlockT> {
peers: HashMap<PeerId, PeerConsensus<B::Hash>>,
messages: Vec<MessageEntry<B>>,
known_messages: LruCache<B::Hash, ()>,
protocol: Cow<'static, str>,
protocol: ProtocolName,
validator: Arc<dyn Validator<B>>,
next_broadcast: Instant,
metrics: Option<Metrics>,
@@ -165,7 +165,7 @@ impl<B: BlockT> ConsensusGossip<B> {
/// Create a new instance using the given validator.
pub fn new(
validator: Arc<dyn Validator<B>>,
protocol: Cow<'static, str>,
protocol: ProtocolName,
metrics_registry: Option<&Registry>,
) -> Self {
let metrics = match metrics_registry.map(Metrics::register) {
@@ -527,7 +527,6 @@ mod tests {
traits::NumberFor,
};
use std::{
borrow::Cow,
collections::HashSet,
pin::Pin,
sync::{Arc, Mutex},
@@ -599,7 +598,7 @@ mod tests {
self.inner.lock().unwrap().peer_reports.push((who, cost_benefit));
}
fn disconnect_peer(&self, _who: PeerId, _protocol: Cow<'static, str>) {
fn disconnect_peer(&self, _who: PeerId, _protocol: ProtocolName) {
unimplemented!();
}
@@ -621,7 +620,7 @@ mod tests {
fn set_reserved_peers(
&self,
_protocol: Cow<'static, str>,
_protocol: ProtocolName,
_peers: HashSet<Multiaddr>,
) -> Result<(), String> {
unimplemented!();
@@ -629,28 +628,23 @@ mod tests {
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!();
}
@@ -666,19 +660,14 @@ mod tests {
}
impl NetworkNotification for NoOpNetwork {
fn write_notification(
&self,
_target: PeerId,
_protocol: Cow<'static, str>,
_message: Vec<u8>,
) {
fn write_notification(&self, _target: PeerId, _protocol: ProtocolName, _message: Vec<u8>) {
unimplemented!();
}
fn notification_sender(
&self,
_target: PeerId,
_protocol: Cow<'static, str>,
_protocol: ProtocolName,
) -> Result<Box<dyn NotificationSender>, NotificationSenderError> {
unimplemented!();
}