diff --git a/substrate/core/network/src/config.rs b/substrate/core/network/src/config.rs index 6cabb1217b003aee54f35c04f314944664f93839..46bb8aeff4ddd69deb3de94cdfaa0b561b6a214a 100644 --- a/substrate/core/network/src/config.rs +++ b/substrate/core/network/src/config.rs @@ -163,7 +163,8 @@ impl ProtocolId { } } -/// Parses a string address and returns the component, if valid. +/// Parses a string address and splits it into Multiaddress and PeerId, if +/// valid. /// /// # Example /// @@ -177,8 +178,12 @@ impl ProtocolId { /// ``` /// pub fn parse_str_addr(addr_str: &str) -> Result<(PeerId, Multiaddr), ParseErr> { - let mut addr: Multiaddr = addr_str.parse()?; + let addr: Multiaddr = addr_str.parse()?; + parse_addr(addr) +} +/// Splits a Multiaddress into a Multiaddress and PeerId. +pub fn parse_addr(mut addr: Multiaddr)-> Result<(PeerId, Multiaddr), ParseErr> { let who = match addr.pop() { Some(multiaddr::Protocol::P2p(key)) => PeerId::from_multihash(key) .map_err(|_| ParseErr::InvalidPeerId)?, diff --git a/substrate/core/network/src/service.rs b/substrate/core/network/src/service.rs index eed5de85eda43f921924bdd35b855c6d43afbb56..d5ba8db7ae4885c0c7449043c1085339653c85ea 100644 --- a/substrate/core/network/src/service.rs +++ b/substrate/core/network/src/service.rs @@ -25,7 +25,7 @@ //! The methods of the [`NetworkService`] are implemented by sending a message over a channel, //! which is then processed by [`NetworkWorker::poll`]. -use std::{collections::HashMap, fs, marker::PhantomData, io, path::Path}; +use std::{collections::{HashMap, HashSet}, fs, marker::PhantomData, io, path::Path}; use std::sync::{Arc, atomic::{AtomicBool, AtomicUsize, Ordering}}; use consensus::import_queue::{ImportQueue, Link}; @@ -40,7 +40,7 @@ use parking_lot::Mutex; use peerset::PeersetHandle; use sr_primitives::{traits::{Block as BlockT, NumberFor}, ConsensusEngineId}; -use crate::{behaviour::{Behaviour, BehaviourOut}, config::parse_str_addr}; +use crate::{behaviour::{Behaviour, BehaviourOut}, config::{parse_str_addr, parse_addr}}; use crate::{NetworkState, NetworkStateNotConnectedPeer, NetworkStatePeer}; use crate::{transport, config::NodeKeyConfig, config::NonReservedPeerMode}; use crate::config::{Params, TransportConfig}; @@ -497,6 +497,24 @@ impl<B: BlockT + 'static, S: NetworkSpecialization<B>, H: ExHashT> NetworkServic Ok(()) } + /// Modify a peerset priority group. + pub fn set_priority_group(&self, group_id: String, peers: HashSet<Multiaddr>) -> Result<(), String> { + let peers = peers.into_iter().map(|p| { + parse_addr(p).map_err(|e| format!("{:?}", e)) + }).collect::<Result<Vec<(PeerId, Multiaddr)>, String>>()?; + + let peer_ids = peers.iter().map(|(peer_id, _addr)| peer_id.clone()).collect(); + self.peerset.set_priority_group(group_id, peer_ids); + + for (peer_id, addr) in peers.into_iter() { + let _ = self + .to_worker + .unbounded_send(ServerToWorkerMsg::AddKnownAddress(peer_id, addr)); + } + + Ok(()) + } + /// Returns the number of peers we're connected to. pub fn num_connected(&self) -> usize { self.num_connected.load(Ordering::Relaxed)