mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 07:41:08 +00:00
Use custom type for ProtocolName (#5963)
* Use new ProtocolName in peer_set.rs
* Use new ProtocolName for request-response protocols
* Use new ProtocolName in polkadot-network-bridge
* Import and conversion fixes
* Use ProtocolName re-exported in sc_network
* update lockfile for {"substrate"}
Co-authored-by: parity-processbot <>
This commit is contained in:
Generated
+170
-170
File diff suppressed because it is too large
Load Diff
@@ -14,7 +14,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use std::{borrow::Cow, collections::HashSet, sync::Arc};
|
||||
use std::{collections::HashSet, sync::Arc};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use futures::{prelude::*, stream::BoxStream};
|
||||
@@ -27,6 +27,7 @@ use sc_network::{
|
||||
};
|
||||
use sc_network_common::{
|
||||
config::parse_addr,
|
||||
protocol::ProtocolName,
|
||||
service::{NetworkEventStream, NetworkNotification, NetworkPeers, NetworkRequest},
|
||||
};
|
||||
|
||||
@@ -92,12 +93,12 @@ pub trait Network: Clone + Send + 'static {
|
||||
/// Note that `out_peers` setting has no effect on this.
|
||||
async fn set_reserved_peers(
|
||||
&mut self,
|
||||
protocol: Cow<'static, str>,
|
||||
protocol: ProtocolName,
|
||||
multiaddresses: HashSet<Multiaddr>,
|
||||
) -> Result<(), String>;
|
||||
|
||||
/// Removes the peers for the protocol's peer set (both reserved and non-reserved).
|
||||
async fn remove_from_peers_set(&mut self, protocol: Cow<'static, str>, peers: Vec<PeerId>);
|
||||
async fn remove_from_peers_set(&mut self, protocol: ProtocolName, peers: Vec<PeerId>);
|
||||
|
||||
/// Send a request to a remote peer.
|
||||
async fn start_request<AD: AuthorityDiscovery>(
|
||||
@@ -112,10 +113,10 @@ pub trait Network: Clone + Send + 'static {
|
||||
fn report_peer(&self, who: PeerId, cost_benefit: Rep);
|
||||
|
||||
/// Disconnect a given peer from the protocol specified without harming reputation.
|
||||
fn disconnect_peer(&self, who: PeerId, protocol: Cow<'static, str>);
|
||||
fn disconnect_peer(&self, who: PeerId, protocol: ProtocolName);
|
||||
|
||||
/// Write a notification to a peer on the given protocol.
|
||||
fn write_notification(&self, who: PeerId, protocol: Cow<'static, str>, message: Vec<u8>);
|
||||
fn write_notification(&self, who: PeerId, protocol: ProtocolName, message: Vec<u8>);
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
@@ -126,13 +127,13 @@ impl Network for Arc<NetworkService<Block, Hash>> {
|
||||
|
||||
async fn set_reserved_peers(
|
||||
&mut self,
|
||||
protocol: Cow<'static, str>,
|
||||
protocol: ProtocolName,
|
||||
multiaddresses: HashSet<Multiaddr>,
|
||||
) -> Result<(), String> {
|
||||
NetworkService::set_reserved_peers(&**self, protocol, multiaddresses)
|
||||
}
|
||||
|
||||
async fn remove_from_peers_set(&mut self, protocol: Cow<'static, str>, peers: Vec<PeerId>) {
|
||||
async fn remove_from_peers_set(&mut self, protocol: ProtocolName, peers: Vec<PeerId>) {
|
||||
NetworkService::remove_peers_from_reserved_set(&**self, protocol, peers);
|
||||
}
|
||||
|
||||
@@ -140,11 +141,11 @@ impl Network for Arc<NetworkService<Block, Hash>> {
|
||||
NetworkService::report_peer(&**self, who, cost_benefit.into_base_rep());
|
||||
}
|
||||
|
||||
fn disconnect_peer(&self, who: PeerId, protocol: Cow<'static, str>) {
|
||||
fn disconnect_peer(&self, who: PeerId, protocol: ProtocolName) {
|
||||
NetworkService::disconnect_peer(&**self, who, protocol);
|
||||
}
|
||||
|
||||
fn write_notification(&self, who: PeerId, protocol: Cow<'static, str>, message: Vec<u8>) {
|
||||
fn write_notification(&self, who: PeerId, protocol: ProtocolName, message: Vec<u8>) {
|
||||
NetworkService::write_notification(&**self, who, protocol, message);
|
||||
}
|
||||
|
||||
|
||||
@@ -23,12 +23,11 @@ use assert_matches::assert_matches;
|
||||
use async_trait::async_trait;
|
||||
use parking_lot::Mutex;
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
collections::HashSet,
|
||||
sync::atomic::{AtomicBool, Ordering},
|
||||
};
|
||||
|
||||
use sc_network::{Event as NetworkEvent, IfDisconnected};
|
||||
use sc_network::{Event as NetworkEvent, IfDisconnected, ProtocolName};
|
||||
|
||||
use polkadot_node_network_protocol::{
|
||||
peer_set::PeerSetProtocolNames,
|
||||
@@ -112,13 +111,13 @@ impl Network for TestNetwork {
|
||||
|
||||
async fn set_reserved_peers(
|
||||
&mut self,
|
||||
_protocol: Cow<'static, str>,
|
||||
_protocol: ProtocolName,
|
||||
_: HashSet<Multiaddr>,
|
||||
) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn remove_from_peers_set(&mut self, _protocol: Cow<'static, str>, _: Vec<PeerId>) {}
|
||||
async fn remove_from_peers_set(&mut self, _protocol: ProtocolName, _: Vec<PeerId>) {}
|
||||
|
||||
async fn start_request<AD: AuthorityDiscovery>(
|
||||
&self,
|
||||
@@ -136,7 +135,7 @@ impl Network for TestNetwork {
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn disconnect_peer(&self, who: PeerId, protocol: Cow<'static, str>) {
|
||||
fn disconnect_peer(&self, who: PeerId, protocol: ProtocolName) {
|
||||
let (peer_set, version) = self.protocol_names.try_get_protocol(&protocol).unwrap();
|
||||
assert_eq!(version, peer_set.get_main_version());
|
||||
|
||||
@@ -146,7 +145,7 @@ impl Network for TestNetwork {
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn write_notification(&self, who: PeerId, protocol: Cow<'static, str>, message: Vec<u8>) {
|
||||
fn write_notification(&self, who: PeerId, protocol: ProtocolName, message: Vec<u8>) {
|
||||
let (peer_set, version) = self.protocol_names.try_get_protocol(&protocol).unwrap();
|
||||
assert_eq!(version, peer_set.get_main_version());
|
||||
|
||||
|
||||
@@ -20,9 +20,9 @@ use polkadot_node_subsystem_util::TimeoutExt;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use parking_lot::Mutex;
|
||||
use std::{borrow::Cow, collections::HashSet};
|
||||
use std::collections::HashSet;
|
||||
|
||||
use sc_network::{Event as NetworkEvent, IfDisconnected};
|
||||
use sc_network::{Event as NetworkEvent, IfDisconnected, ProtocolName};
|
||||
|
||||
use polkadot_node_network_protocol::{
|
||||
peer_set::PeerSetProtocolNames,
|
||||
@@ -99,13 +99,13 @@ impl Network for TestNetwork {
|
||||
|
||||
async fn set_reserved_peers(
|
||||
&mut self,
|
||||
_protocol: Cow<'static, str>,
|
||||
_protocol: ProtocolName,
|
||||
_: HashSet<Multiaddr>,
|
||||
) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn remove_from_peers_set(&mut self, _protocol: Cow<'static, str>, _: Vec<PeerId>) {}
|
||||
async fn remove_from_peers_set(&mut self, _protocol: ProtocolName, _: Vec<PeerId>) {}
|
||||
|
||||
async fn start_request<AD: AuthorityDiscovery>(
|
||||
&self,
|
||||
@@ -123,7 +123,7 @@ impl Network for TestNetwork {
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn disconnect_peer(&self, who: PeerId, protocol: Cow<'static, str>) {
|
||||
fn disconnect_peer(&self, who: PeerId, protocol: ProtocolName) {
|
||||
let (peer_set, version) = self.peerset_protocol_names.try_get_protocol(&protocol).unwrap();
|
||||
assert_eq!(version, peer_set.get_main_version());
|
||||
|
||||
@@ -133,7 +133,7 @@ impl Network for TestNetwork {
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn write_notification(&self, who: PeerId, protocol: Cow<'static, str>, message: Vec<u8>) {
|
||||
fn write_notification(&self, who: PeerId, protocol: ProtocolName, message: Vec<u8>) {
|
||||
let (peer_set, version) = self.peerset_protocol_names.try_get_protocol(&protocol).unwrap();
|
||||
assert_eq!(version, peer_set.get_main_version());
|
||||
|
||||
|
||||
@@ -174,12 +174,9 @@ mod tests {
|
||||
PeerId,
|
||||
};
|
||||
use polkadot_primitives::v2::Hash;
|
||||
use sc_network::{Event as NetworkEvent, IfDisconnected};
|
||||
use sc_network::{Event as NetworkEvent, IfDisconnected, ProtocolName};
|
||||
use sp_keyring::Sr25519Keyring;
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
collections::{HashMap, HashSet},
|
||||
};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
fn new_service() -> Service<TestNetwork, TestAuthorityDiscovery> {
|
||||
let genesis_hash = Hash::repeat_byte(0xff);
|
||||
@@ -232,18 +229,14 @@ mod tests {
|
||||
|
||||
async fn set_reserved_peers(
|
||||
&mut self,
|
||||
_protocol: Cow<'static, str>,
|
||||
_protocol: ProtocolName,
|
||||
multiaddresses: HashSet<Multiaddr>,
|
||||
) -> Result<(), String> {
|
||||
self.peers_set = extract_peer_ids(multiaddresses.into_iter());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn remove_from_peers_set(
|
||||
&mut self,
|
||||
_protocol: Cow<'static, str>,
|
||||
peers: Vec<PeerId>,
|
||||
) {
|
||||
async fn remove_from_peers_set(&mut self, _protocol: ProtocolName, peers: Vec<PeerId>) {
|
||||
self.peers_set.retain(|elem| !peers.contains(elem));
|
||||
}
|
||||
|
||||
@@ -260,11 +253,11 @@ mod tests {
|
||||
panic!()
|
||||
}
|
||||
|
||||
fn disconnect_peer(&self, _: PeerId, _: Cow<'static, str>) {
|
||||
fn disconnect_peer(&self, _: PeerId, _: ProtocolName) {
|
||||
panic!()
|
||||
}
|
||||
|
||||
fn write_notification(&self, _: PeerId, _: Cow<'static, str>, _: Vec<u8>) {
|
||||
fn write_notification(&self, _: PeerId, _: ProtocolName, _: Vec<u8>) {
|
||||
panic!()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,9 +18,11 @@
|
||||
|
||||
use derive_more::Display;
|
||||
use polkadot_primitives::v2::Hash;
|
||||
use sc_network::config::{NonDefaultSetConfig, SetConfig};
|
||||
use sc_network::{
|
||||
config::{NonDefaultSetConfig, SetConfig},
|
||||
ProtocolName,
|
||||
};
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
collections::{hash_map::Entry, HashMap},
|
||||
ops::{Index, IndexMut},
|
||||
};
|
||||
@@ -231,8 +233,8 @@ impl From<CollationVersion> for ProtocolVersion {
|
||||
/// On the wire protocol name to [`PeerSet`] mapping.
|
||||
#[derive(Clone)]
|
||||
pub struct PeerSetProtocolNames {
|
||||
protocols: HashMap<Cow<'static, str>, (PeerSet, ProtocolVersion)>,
|
||||
names: HashMap<(PeerSet, ProtocolVersion), Cow<'static, str>>,
|
||||
protocols: HashMap<ProtocolName, (PeerSet, ProtocolVersion)>,
|
||||
names: HashMap<(PeerSet, ProtocolVersion), ProtocolName>,
|
||||
}
|
||||
|
||||
impl PeerSetProtocolNames {
|
||||
@@ -272,8 +274,8 @@ impl PeerSetProtocolNames {
|
||||
|
||||
/// Helper function to register main protocol.
|
||||
fn register_main_protocol(
|
||||
protocols: &mut HashMap<Cow<'static, str>, (PeerSet, ProtocolVersion)>,
|
||||
names: &mut HashMap<(PeerSet, ProtocolVersion), Cow<'static, str>>,
|
||||
protocols: &mut HashMap<ProtocolName, (PeerSet, ProtocolVersion)>,
|
||||
names: &mut HashMap<(PeerSet, ProtocolVersion), ProtocolName>,
|
||||
protocol: PeerSet,
|
||||
version: ProtocolVersion,
|
||||
genesis_hash: &Hash,
|
||||
@@ -286,7 +288,7 @@ impl PeerSetProtocolNames {
|
||||
|
||||
/// Helper function to register legacy protocol.
|
||||
fn register_legacy_protocol(
|
||||
protocols: &mut HashMap<Cow<'static, str>, (PeerSet, ProtocolVersion)>,
|
||||
protocols: &mut HashMap<ProtocolName, (PeerSet, ProtocolVersion)>,
|
||||
protocol: PeerSet,
|
||||
) {
|
||||
Self::insert_protocol_or_panic(
|
||||
@@ -299,8 +301,8 @@ impl PeerSetProtocolNames {
|
||||
|
||||
/// Helper function to make sure no protocols have the same name.
|
||||
fn insert_protocol_or_panic(
|
||||
protocols: &mut HashMap<Cow<'static, str>, (PeerSet, ProtocolVersion)>,
|
||||
name: Cow<'static, str>,
|
||||
protocols: &mut HashMap<ProtocolName, (PeerSet, ProtocolVersion)>,
|
||||
name: ProtocolName,
|
||||
protocol: PeerSet,
|
||||
version: ProtocolVersion,
|
||||
) {
|
||||
@@ -322,18 +324,18 @@ impl PeerSetProtocolNames {
|
||||
}
|
||||
|
||||
/// Lookup the protocol using its on the wire name.
|
||||
pub fn try_get_protocol(&self, name: &Cow<'static, str>) -> Option<(PeerSet, ProtocolVersion)> {
|
||||
pub fn try_get_protocol(&self, name: &ProtocolName) -> Option<(PeerSet, ProtocolVersion)> {
|
||||
self.protocols.get(name).map(ToOwned::to_owned)
|
||||
}
|
||||
|
||||
/// Get the main protocol name. It's used by the networking for keeping track
|
||||
/// of peersets and connections.
|
||||
pub fn get_main_name(&self, protocol: PeerSet) -> Cow<'static, str> {
|
||||
pub fn get_main_name(&self, protocol: PeerSet) -> ProtocolName {
|
||||
self.get_name(protocol, protocol.get_main_version())
|
||||
}
|
||||
|
||||
/// Get the protocol name for specific version.
|
||||
pub fn get_name(&self, protocol: PeerSet, version: ProtocolVersion) -> Cow<'static, str> {
|
||||
pub fn get_name(&self, protocol: PeerSet, version: ProtocolVersion) -> ProtocolName {
|
||||
self.names
|
||||
.get(&(protocol, version))
|
||||
.expect("Protocols & versions are specified via enums defined above, and they are all registered in `new()`; qed")
|
||||
@@ -346,7 +348,7 @@ impl PeerSetProtocolNames {
|
||||
fork_id: Option<&str>,
|
||||
protocol: PeerSet,
|
||||
version: ProtocolVersion,
|
||||
) -> Cow<'static, str> {
|
||||
) -> ProtocolName {
|
||||
let prefix = if let Some(fork_id) = fork_id {
|
||||
format!("/{}/{}", hex::encode(genesis_hash), fork_id)
|
||||
} else {
|
||||
@@ -362,7 +364,7 @@ impl PeerSetProtocolNames {
|
||||
}
|
||||
|
||||
/// Get the legacy protocol name, only `LEGACY_PROTOCOL_VERSION` = 1 is supported.
|
||||
fn get_legacy_name(protocol: PeerSet) -> Cow<'static, str> {
|
||||
fn get_legacy_name(protocol: PeerSet) -> ProtocolName {
|
||||
match protocol {
|
||||
PeerSet::Validation => LEGACY_VALIDATION_PROTOCOL_V1,
|
||||
PeerSet::Collation => LEGACY_COLLATION_PROTOCOL_V1,
|
||||
@@ -372,7 +374,7 @@ impl PeerSetProtocolNames {
|
||||
|
||||
/// Get the protocol fallback names. Currently only holds the legacy name
|
||||
/// for `LEGACY_PROTOCOL_VERSION` = 1.
|
||||
fn get_fallback_names(protocol: PeerSet) -> Vec<Cow<'static, str>> {
|
||||
fn get_fallback_names(protocol: PeerSet) -> Vec<ProtocolName> {
|
||||
std::iter::once(Self::get_legacy_name(protocol)).collect()
|
||||
}
|
||||
}
|
||||
@@ -406,7 +408,7 @@ mod tests {
|
||||
);
|
||||
let expected =
|
||||
"/7ac8741de8b7146d8a5617fd462914557fe63c265a7f1c10e7dae32858eebb80/validation/3";
|
||||
assert_eq!(name, expected);
|
||||
assert_eq!(name, expected.into());
|
||||
|
||||
let name = PeerSetProtocolNames::generate_name(
|
||||
&genesis_hash,
|
||||
@@ -416,7 +418,7 @@ mod tests {
|
||||
);
|
||||
let expected =
|
||||
"/7ac8741de8b7146d8a5617fd462914557fe63c265a7f1c10e7dae32858eebb80/collation/5";
|
||||
assert_eq!(name, expected);
|
||||
assert_eq!(name, expected.into());
|
||||
|
||||
let fork_id = Some("test-fork");
|
||||
let name = PeerSetProtocolNames::generate_name(
|
||||
@@ -427,7 +429,7 @@ mod tests {
|
||||
);
|
||||
let expected =
|
||||
"/7ac8741de8b7146d8a5617fd462914557fe63c265a7f1c10e7dae32858eebb80/test-fork/validation/7";
|
||||
assert_eq!(name, expected);
|
||||
assert_eq!(name, expected.into());
|
||||
|
||||
let name = PeerSetProtocolNames::generate_name(
|
||||
&genesis_hash,
|
||||
@@ -437,7 +439,7 @@ mod tests {
|
||||
);
|
||||
let expected =
|
||||
"/7ac8741de8b7146d8a5617fd462914557fe63c265a7f1c10e7dae32858eebb80/test-fork/collation/11";
|
||||
assert_eq!(name, expected);
|
||||
assert_eq!(name, expected.into());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -32,13 +32,13 @@
|
||||
//!
|
||||
//! Versioned (v1 module): The actual requests and responses as sent over the network.
|
||||
|
||||
use std::{borrow::Cow, collections::HashMap, time::Duration, u64};
|
||||
use std::{collections::HashMap, time::Duration, u64};
|
||||
|
||||
use futures::channel::mpsc;
|
||||
use polkadot_primitives::v2::{MAX_CODE_SIZE, MAX_POV_SIZE};
|
||||
use strum::{EnumIter, IntoEnumIterator};
|
||||
|
||||
pub use sc_network::{config as network, config::RequestResponseConfig};
|
||||
pub use sc_network::{config as network, config::RequestResponseConfig, ProtocolName};
|
||||
|
||||
/// Everything related to handling of incoming requests.
|
||||
pub mod incoming;
|
||||
@@ -248,7 +248,7 @@ impl Protocol {
|
||||
}
|
||||
|
||||
/// Fallback protocol names of this protocol, as understood by substrate networking.
|
||||
fn get_fallback_names(self) -> Vec<Cow<'static, str>> {
|
||||
fn get_fallback_names(self) -> Vec<ProtocolName> {
|
||||
std::iter::once(self.get_legacy_name().into()).collect()
|
||||
}
|
||||
|
||||
@@ -276,7 +276,7 @@ pub trait IsRequest {
|
||||
|
||||
/// Type for getting on the wire [`Protocol`] names using genesis hash & fork id.
|
||||
pub struct ReqProtocolNames {
|
||||
names: HashMap<Protocol, Cow<'static, str>>,
|
||||
names: HashMap<Protocol, ProtocolName>,
|
||||
}
|
||||
|
||||
impl ReqProtocolNames {
|
||||
@@ -290,7 +290,7 @@ impl ReqProtocolNames {
|
||||
}
|
||||
|
||||
/// Get on the wire [`Protocol`] name.
|
||||
pub fn get_name(&self, protocol: Protocol) -> Cow<'static, str> {
|
||||
pub fn get_name(&self, protocol: Protocol) -> ProtocolName {
|
||||
self.names
|
||||
.get(&protocol)
|
||||
.expect("All `Protocol` enum variants are added above via `strum`; qed")
|
||||
@@ -302,7 +302,7 @@ impl ReqProtocolNames {
|
||||
protocol: Protocol,
|
||||
genesis_hash: &Hash,
|
||||
fork_id: Option<&str>,
|
||||
) -> Cow<'static, str> {
|
||||
) -> ProtocolName {
|
||||
let prefix = if let Some(fork_id) = fork_id {
|
||||
format!("/{}/{}", hex::encode(genesis_hash), fork_id)
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user