diff --git a/client/network/src/protocol.rs b/client/network/src/protocol.rs index ff64b9d599c04..e0fa7a1cb467c 100644 --- a/client/network/src/protocol.rs +++ b/client/network/src/protocol.rs @@ -1014,6 +1014,11 @@ impl Protocol { self.peerset_handle.remove_reserved_peer(HARDCODED_PEERSETS_SYNC, peer.clone()); } + /// Returns the list of reserved peers. + pub fn reserved_peers(&self) -> impl Iterator { + self.behaviour.reserved_peers(HARDCODED_PEERSETS_SYNC) + } + /// 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()); diff --git a/client/network/src/protocol/notifications/behaviour.rs b/client/network/src/protocol/notifications/behaviour.rs index 6b17c5253f364..e1ed61722c549 100644 --- a/client/network/src/protocol/notifications/behaviour.rs +++ b/client/network/src/protocol/notifications/behaviour.rs @@ -555,6 +555,11 @@ impl Notifications { .map(|((id, _), _)| id) } + /// Returns the list of reserved peers. + pub fn reserved_peers<'a>(&'a self, set_id: sc_peerset::SetId) -> impl Iterator + 'a { + self.peerset.reserved_peers(set_id) + } + /// Sends a notification to a peer. /// /// Has no effect if the custom protocol is not open with the given peer. diff --git a/client/network/src/service.rs b/client/network/src/service.rs index 4ad5053d9b287..99036c5effad8 100644 --- a/client/network/src/service.rs +++ b/client/network/src/service.rs @@ -619,6 +619,11 @@ impl NetworkWorker { pub fn add_reserved_peer(&self, peer: String) -> Result<(), String> { self.service.add_reserved_peer(peer) } + + /// Returns the list of reserved peers. + pub fn reserved_peers(&self) -> impl Iterator { + self.network_service.behaviour().user_protocol().reserved_peers() + } } impl NetworkService { diff --git a/client/peerset/src/lib.rs b/client/peerset/src/lib.rs index 153e097dc8b46..eefab81b851da 100644 --- a/client/peerset/src/lib.rs +++ b/client/peerset/src/lib.rs @@ -379,6 +379,11 @@ impl Peerset { } } + /// Returns the list of reserved peers. + pub fn reserved_peers(&self, set_id: SetId) -> impl Iterator { + self.reserved_nodes[set_id.0].0.iter() + } + /// Adds a node to the given set. The peerset will, if possible and not already the case, /// try to connect to it. /// diff --git a/client/rpc-api/src/system/mod.rs b/client/rpc-api/src/system/mod.rs index 2e8a7aa12633b..4252ef20ac22a 100644 --- a/client/rpc-api/src/system/mod.rs +++ b/client/rpc-api/src/system/mod.rs @@ -102,6 +102,10 @@ pub trait SystemApi { fn system_remove_reserved_peer(&self, peer_id: String) -> Compat>>; + /// Returns the list of reserved peers + #[rpc(name = "system_reservedPeers", returns = "Vec")] + fn system_reserved_peers(&self) -> Receiver>; + /// Returns the roles the node is running as. #[rpc(name = "system_nodeRoles", returns = "Vec")] fn system_node_roles(&self) -> Receiver>; diff --git a/client/rpc/src/system/mod.rs b/client/rpc/src/system/mod.rs index 240de6c62876e..b721dbf0c9360 100644 --- a/client/rpc/src/system/mod.rs +++ b/client/rpc/src/system/mod.rs @@ -66,6 +66,8 @@ pub enum Request { NetworkAddReservedPeer(String, oneshot::Sender>), /// Must return any potential parse error. NetworkRemoveReservedPeer(String, oneshot::Sender>), + /// Must return the list of reserved peers + NetworkReservedPeers(oneshot::Sender>), /// Must return the node role. NodeRoles(oneshot::Sender>), /// Must return the state of the node syncing. @@ -187,6 +189,12 @@ impl SystemApi::Number> for Sy }.boxed().compat() } + fn system_reserved_peers(&self) -> Receiver> { + let (tx, rx) = oneshot::channel(); + let _ = self.send_back.unbounded_send(Request::NetworkReservedPeers(tx)); + Receiver(Compat::new(rx)) + } + fn system_node_roles(&self) -> Receiver> { let (tx, rx) = oneshot::channel(); let _ = self.send_back.unbounded_send(Request::NodeRoles(tx)); diff --git a/client/rpc/src/system/tests.rs b/client/rpc/src/system/tests.rs index c196403501035..41d6029ddde2b 100644 --- a/client/rpc/src/system/tests.rs +++ b/client/rpc/src/system/tests.rs @@ -104,6 +104,9 @@ fn api>>(sync: T) -> System { Err(s) => sender.send(Err(error::Error::MalformattedPeerArg(s.to_string()))), }; } + Request::NetworkReservedPeers(sender) => { + let _ = sender.send(vec!["QmSk5HQbn6LhUwDiNMseVUjuRYhEtYj4aUZ6WfWoGURpdV".to_string()]); + } Request::NodeRoles(sender) => { let _ = sender.send(vec![NodeRole::Authority]); } @@ -337,6 +340,14 @@ fn system_network_remove_reserved() { assert!(runtime.block_on(bad_fut).is_err()); } +#[test] +fn system_network_reserved_peers() { + assert_eq!( + wait_receiver(api(None).system_reserved_peers()), + vec!["QmSk5HQbn6LhUwDiNMseVUjuRYhEtYj4aUZ6WfWoGURpdV".to_string()] + ); +} + #[test] fn test_add_reset_log_filter() { const EXPECTED_BEFORE_ADD: &'static str = "EXPECTED_BEFORE_ADD"; diff --git a/client/service/src/lib.rs b/client/service/src/lib.rs index db5f296953e3f..da05f99506a94 100644 --- a/client/service/src/lib.rs +++ b/client/service/src/lib.rs @@ -302,6 +302,14 @@ async fn build_network_future< ))), }; } + sc_rpc::system::Request::NetworkReservedPeers(sender) => { + let reserved_peers = network.reserved_peers(); + let reserved_peers = reserved_peers + .map(|peer_id| peer_id.to_base58()) + .collect(); + + let _ = sender.send(reserved_peers); + } sc_rpc::system::Request::NodeRoles(sender) => { use sc_rpc::system::NodeRole;