Some refactoring in network-bridge in the course of dealing with #2177 (#2263)

* More doc fixes.

* Minor refactorings in the process of #2177

By having everything peer set related depend directly on the enum the
code becomes more clear and it is also straight forward to add more
peersets/protocols as the compiler will complain if you forget to
implement parts of it.

* Add peer set infos on startup properly

For feature real_overseer.

+ Fixes from review. Thanks @coriolinus and @ordian!

* More structure in network-bridge

Some changes, which would have helped me in groking the code faster.

Entry points/public types more to the top. Factored out implementation
in their own files, to clear up the top-level view.

* Get rid of local ProtocolName type definition.

Does not add much at this level.

* Fix tests + import cleanup.

* Make spaces tabs.

* Clarify what correct parameters to send_message are

* Be more less vague in docs of send_message.

* Apply suggestions from code review

Extend copyright on new files to 2021 as well.

Co-authored-by: Andronik Ordian <write@reusable.software>

Co-authored-by: Andronik Ordian <write@reusable.software>
This commit is contained in:
Robert Klotzner
2021-01-14 11:29:02 +01:00
committed by GitHub
parent 96f80ac3e3
commit 32d4670b0d
10 changed files with 733 additions and 514 deletions
+4 -9
View File
@@ -29,6 +29,10 @@ pub use polkadot_node_jaeger::JaegerSpan;
#[doc(hidden)]
pub use std::sync::Arc;
/// Peer-sets and protocols used for parachains.
pub mod peer_set;
/// A unique identifier of a request.
pub type RequestId = u64;
@@ -47,15 +51,6 @@ impl fmt::Display for WrongVariant {
impl std::error::Error for WrongVariant {}
/// The peer-sets that the network manages. Different subsystems will use different peer-sets.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PeerSet {
/// The validation peer-set is responsible for all messages related to candidate validation and communication among validators.
Validation,
/// The collation peer-set is used for validator<>collator communication.
Collation,
}
/// The advertised role of a node.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ObservedRole {
@@ -0,0 +1,90 @@
// Copyright 2021 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! All peersets and protocols used for parachains.
use sc_network::config::{NonDefaultSetConfig, SetConfig};
use std::borrow::Cow;
use strum::{EnumIter, IntoEnumIterator};
/// The peer-sets and thus the protocols which are used for the network.
#[derive(Debug, Clone, Copy, PartialEq, EnumIter)]
pub enum PeerSet {
/// The validation peer-set is responsible for all messages related to candidate validation and communication among validators.
Validation,
/// The collation peer-set is used for validator<>collator communication.
Collation,
}
impl PeerSet {
/// Get `sc_network` peer set configurations for each peerset.
///
/// Those should be used in the network configuration to register the protocols with the
/// network service.
pub fn get_info(self) -> NonDefaultSetConfig {
let protocol = self.into_protocol_name();
match self {
PeerSet::Validation => NonDefaultSetConfig {
notifications_protocol: protocol,
set_config: sc_network::config::SetConfig {
in_peers: 25,
out_peers: 0,
reserved_nodes: Vec::new(),
non_reserved_mode: sc_network::config::NonReservedPeerMode::Accept,
},
},
PeerSet::Collation => NonDefaultSetConfig {
notifications_protocol: protocol,
set_config: SetConfig {
in_peers: 25,
out_peers: 0,
reserved_nodes: Vec::new(),
non_reserved_mode: sc_network::config::NonReservedPeerMode::Accept,
},
},
}
}
/// Get the protocol name associated with each peer set as static str.
pub const fn get_protocol_name_static(self) -> &'static str {
match self {
PeerSet::Validation => "/polkadot/validation/1",
PeerSet::Collation => "/polkadot/collation/1",
}
}
/// Convert a peer set into a protocol name as understood by Substrate.
pub fn into_protocol_name(self) -> Cow<'static, str> {
self.get_protocol_name_static().into()
}
/// Try parsing a protocol name into a peer set.
pub fn try_from_protocol_name(name: &Cow<'static, str>) -> Option<PeerSet> {
match name {
n if n == &PeerSet::Validation.into_protocol_name() => Some(PeerSet::Validation),
n if n == &PeerSet::Collation.into_protocol_name() => Some(PeerSet::Collation),
_ => None,
}
}
}
/// Get `NonDefaultSetConfig`s for all available peer sets.
///
/// Should be used during network configuration (added to [`NetworkConfiguration::extra_sets`])
/// or shortly after startup to register the protocols with the network service.
pub fn peer_sets_info() -> Vec<sc_network::config::NonDefaultSetConfig> {
PeerSet::iter().map(PeerSet::get_info).collect()
}