Skip to content
Snippets Groups Projects
Commit ef07c3be authored by Roman Proskuryakov's avatar Roman Proskuryakov Committed by GitHub
Browse files

Add a JSON-RPC layer for reserved nodes (#8704)


* Add boilerplate for JSON-RPC layer for reserved nodes

* Add more boilerplate for JSON-RPC layer for reserved nodes

* Make JSON-RPC layer for reserved nodes async

* Use more realistic data in reserver_peers tests

* Make JSON-RPC layer for reserved nodes blocking

* Apply tomaka's suggestion to reduce .into_iter() for an iter

Co-authored-by: default avatarPierre Krieger <pierre.krieger1708@gmail.com>

Co-authored-by: default avatarPierre Krieger <pierre.krieger1708@gmail.com>
parent de5d0b23
Branches
No related merge requests found
......@@ -1014,6 +1014,11 @@ impl<B: BlockT> Protocol<B> {
self.peerset_handle.remove_reserved_peer(HARDCODED_PEERSETS_SYNC, peer.clone());
}
/// Returns the list of reserved peers.
pub fn reserved_peers(&self) -> impl Iterator<Item = &PeerId> {
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());
......
......@@ -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<Item = &'a PeerId> + '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.
......
......@@ -619,6 +619,11 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkWorker<B, H> {
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<Item = &PeerId> {
self.network_service.behaviour().user_protocol().reserved_peers()
}
}
impl<B: BlockT + 'static, H: ExHashT> NetworkService<B, H> {
......
......@@ -379,6 +379,11 @@ impl Peerset {
}
}
/// Returns the list of reserved peers.
pub fn reserved_peers(&self, set_id: SetId) -> impl Iterator<Item = &PeerId> {
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.
///
......
......@@ -102,6 +102,10 @@ pub trait SystemApi<Hash, Number> {
fn system_remove_reserved_peer(&self, peer_id: String)
-> Compat<BoxFuture<'static, Result<(), jsonrpc_core::Error>>>;
/// Returns the list of reserved peers
#[rpc(name = "system_reservedPeers", returns = "Vec<String>")]
fn system_reserved_peers(&self) -> Receiver<Vec<String>>;
/// Returns the roles the node is running as.
#[rpc(name = "system_nodeRoles", returns = "Vec<NodeRole>")]
fn system_node_roles(&self) -> Receiver<Vec<NodeRole>>;
......
......@@ -66,6 +66,8 @@ pub enum Request<B: traits::Block> {
NetworkAddReservedPeer(String, oneshot::Sender<Result<()>>),
/// Must return any potential parse error.
NetworkRemoveReservedPeer(String, oneshot::Sender<Result<()>>),
/// Must return the list of reserved peers
NetworkReservedPeers(oneshot::Sender<Vec<String>>),
/// Must return the node role.
NodeRoles(oneshot::Sender<Vec<NodeRole>>),
/// Must return the state of the node syncing.
......@@ -187,6 +189,12 @@ impl<B: traits::Block> SystemApi<B::Hash, <B::Header as HeaderT>::Number> for Sy
}.boxed().compat()
}
fn system_reserved_peers(&self) -> Receiver<Vec<String>> {
let (tx, rx) = oneshot::channel();
let _ = self.send_back.unbounded_send(Request::NetworkReservedPeers(tx));
Receiver(Compat::new(rx))
}
fn system_node_roles(&self) -> Receiver<Vec<NodeRole>> {
let (tx, rx) = oneshot::channel();
let _ = self.send_back.unbounded_send(Request::NodeRoles(tx));
......
......@@ -104,6 +104,9 @@ fn api<T: Into<Option<Status>>>(sync: T) -> System<Block> {
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";
......
......@@ -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;
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment