Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Expose set_reserved_peers (#9960)
Browse files Browse the repository at this point in the history
  • Loading branch information
eskimor authored Oct 7, 2021
1 parent 4b6635c commit 5e2b0f0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
20 changes: 17 additions & 3 deletions client/network/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,7 @@ impl<B: BlockT> Protocol<B> {

/// Removes a `PeerId` from the list of reserved peers for syncing purposes.
pub fn remove_reserved_peer(&self, peer: PeerId) {
self.peerset_handle.remove_reserved_peer(HARDCODED_PEERSETS_SYNC, peer.clone());
self.peerset_handle.remove_reserved_peer(HARDCODED_PEERSETS_SYNC, peer);
}

/// Returns the list of reserved peers.
Expand All @@ -1112,12 +1112,26 @@ impl<B: BlockT> Protocol<B> {

/// Adds a `PeerId` to the list of reserved peers for syncing purposes.
pub fn add_reserved_peer(&self, peer: PeerId) {
self.peerset_handle.add_reserved_peer(HARDCODED_PEERSETS_SYNC, peer.clone());
self.peerset_handle.add_reserved_peer(HARDCODED_PEERSETS_SYNC, peer);
}

/// Sets the list of reserved peers for syncing purposes.
pub fn set_reserved_peers(&self, peers: HashSet<PeerId>) {
self.peerset_handle.set_reserved_peers(HARDCODED_PEERSETS_SYNC, peers.clone());
self.peerset_handle.set_reserved_peers(HARDCODED_PEERSETS_SYNC, peers);
}

/// Sets the list of reserved peers for the given protocol/peerset.
pub fn set_reserved_peerset_peers(&self, protocol: Cow<'static, str>, peers: HashSet<PeerId>) {
if let Some(index) = self.notification_protocols.iter().position(|p| *p == protocol) {
self.peerset_handle
.set_reserved_peers(sc_peerset::SetId::from(index + NUM_HARDCODED_PEERSETS), peers);
} else {
error!(
target: "sub-libp2p",
"set_reserved_peerset_peers with unknown protocol: {}",
protocol
);
}
}

/// Removes a `PeerId` from the list of reserved peers.
Expand Down
44 changes: 44 additions & 0 deletions client/network/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,44 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkService<B, H> {
let _ = self.to_worker.unbounded_send(ServiceToWorkerMsg::RemoveReserved(peer_id));
}

/// Sets the reserved set of a protocol to the given set of peers.
///
/// Each `Multiaddr` must end with a `/p2p/` component containing the `PeerId`. It can also
/// consist of only `/p2p/<peerid>`.
///
/// Returns an `Err` if one of the given addresses is invalid or contains an
/// invalid peer ID (which includes the local peer ID).
pub fn set_reserved_peers(
&self,
protocol: Cow<'static, str>,
peers: HashSet<Multiaddr>,
) -> Result<(), String> {
let peers_addrs = self.split_multiaddr_and_peer_id(peers)?;

let mut peers: HashSet<PeerId> = HashSet::with_capacity(peers_addrs.len());

for (peer_id, addr) in peers_addrs.into_iter() {
// Make sure the local peer ID is never added to the PSM.
if peer_id == self.local_peer_id {
return Err("Local peer ID cannot be added as a reserved peer.".to_string())
}

peers.insert(peer_id);

if !addr.is_empty() {
let _ = self
.to_worker
.unbounded_send(ServiceToWorkerMsg::AddKnownAddress(peer_id, addr));
}
}

let _ = self
.to_worker
.unbounded_send(ServiceToWorkerMsg::SetPeersetReserved(protocol, peers));

Ok(())
}

/// Add peers to a peer set.
///
/// Each `Multiaddr` must end with a `/p2p/` component containing the `PeerId`. It can also
Expand Down Expand Up @@ -1400,6 +1438,7 @@ enum ServiceToWorkerMsg<B: BlockT, H: ExHashT> {
AddReserved(PeerId),
RemoveReserved(PeerId),
SetReserved(HashSet<PeerId>),
SetPeersetReserved(Cow<'static, str>, HashSet<PeerId>),
AddSetReserved(Cow<'static, str>, PeerId),
RemoveSetReserved(Cow<'static, str>, PeerId),
AddToPeersSet(Cow<'static, str>, PeerId),
Expand Down Expand Up @@ -1541,6 +1580,11 @@ impl<B: BlockT + 'static, H: ExHashT> Future for NetworkWorker<B, H> {
.behaviour_mut()
.user_protocol_mut()
.set_reserved_peers(peers),
ServiceToWorkerMsg::SetPeersetReserved(protocol, peers) => this
.network_service
.behaviour_mut()
.user_protocol_mut()
.set_reserved_peerset_peers(protocol, peers),
ServiceToWorkerMsg::AddReserved(peer_id) => this
.network_service
.behaviour_mut()
Expand Down

0 comments on commit 5e2b0f0

Please sign in to comment.