WIP: Node role RPC call (#3719)

* Add a Node Role RPC call

* Formatting

* Fix tests

* Change tests to use NodeRole::Authority so I don't forget to update the test

* Improve role checking

* return a vec instead

* fix tests
This commit is contained in:
Ashley
2019-10-16 10:59:25 +01:00
committed by Bastian Köcher
parent 8ef20a5534
commit 642c8504c4
5 changed files with 62 additions and 9 deletions
+19 -6
View File
@@ -50,6 +50,14 @@ pub struct Health {
pub should_have_peers: bool,
}
impl fmt::Display for Health {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "{} peers ({})", self.peers, if self.is_syncing {
"syncing"
} else { "idle" })
}
}
/// Network Peer information
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
@@ -66,12 +74,17 @@ pub struct PeerInfo<Hash, Number> {
pub best_number: Number,
}
impl fmt::Display for Health {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "{} peers ({})", self.peers, if self.is_syncing {
"syncing"
} else { "idle" })
}
/// The role the node is running as
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub enum NodeRole {
/// The node is a full node
Full,
/// The node is a light client
LightClient,
/// The node is an authority
Authority,
/// An unknown role with a bit number
UnknownRole(u8)
}
#[cfg(test)]
+5 -1
View File
@@ -24,7 +24,7 @@ use jsonrpc_derive::rpc;
use self::error::Result;
pub use self::helpers::{Properties, SystemInfo, Health, PeerInfo};
pub use self::helpers::{Properties, SystemInfo, Health, PeerInfo, NodeRole};
pub use self::gen_client::Client as SystemClient;
/// Substrate system RPC API
@@ -64,4 +64,8 @@ pub trait SystemApi<Hash, Number> {
// TODO: make this stable and move structs https://github.com/paritytech/substrate/issues/1890
#[rpc(name = "system_networkState", returns = "jsonrpc_core::Value")]
fn system_network_state(&self) -> Receiver<jsonrpc_core::Value>;
/// Returns the roles the node is running as.
#[rpc(name = "system_nodeRoles", returns = "Vec<NodeRole>")]
fn system_node_roles(&self) -> Receiver<Vec<NodeRole>>;
}
+9 -1
View File
@@ -25,7 +25,7 @@ use sr_primitives::traits::{self, Header as HeaderT};
use self::error::Result;
pub use api::system::*;
pub use self::helpers::{Properties, SystemInfo, Health, PeerInfo};
pub use self::helpers::{Properties, SystemInfo, Health, PeerInfo, NodeRole};
pub use self::gen_client::Client as SystemClient;
/// System API implementation
@@ -42,6 +42,8 @@ pub enum Request<B: traits::Block> {
Peers(oneshot::Sender<Vec<PeerInfo<B::Hash, <B::Header as HeaderT>::Number>>>),
/// Must return the state of the network.
NetworkState(oneshot::Sender<rpc::Value>),
/// Must return the node role.
NodeRoles(oneshot::Sender<Vec<NodeRole>>)
}
impl<B: traits::Block> System<B> {
@@ -94,4 +96,10 @@ impl<B: traits::Block> SystemApi<B::Hash, <B::Header as HeaderT>::Number> for Sy
let _ = self.send_back.unbounded_send(Request::NetworkState(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));
Receiver(Compat::new(rx))
}
}
+11
View File
@@ -79,6 +79,9 @@ fn api<T: Into<Option<Status>>>(sync: T) -> System<Block> {
average_upload_per_sec: 0,
peerset: serde_json::Value::Null,
}).unwrap());
},
Request::NodeRoles(sender) => {
let _ = sender.send(vec![NodeRole::Authority]);
}
};
@@ -221,3 +224,11 @@ fn system_network_state() {
}
);
}
#[test]
fn system_node_roles() {
assert_eq!(
wait_receiver(api(None).system_node_roles()),
vec![NodeRole::Authority]
);
}
+18 -1
View File
@@ -368,6 +368,7 @@ macro_rules! new_impl {
let _ = to_spawn_tx.unbounded_send(Box::new(build_network_future(
$config.roles,
network_mut,
client.clone(),
network_status_sinks.clone(),
@@ -662,6 +663,7 @@ fn build_network_future<
S: network::specialization::NetworkSpecialization<B>,
H: network::ExHashT
> (
roles: Roles,
mut network: network::NetworkWorker<B, S, H>,
client: Arc<C>,
status_sinks: Arc<Mutex<Vec<mpsc::UnboundedSender<(NetworkStatus<B>, NetworkState)>>>>,
@@ -669,7 +671,7 @@ fn build_network_future<
should_have_peers: bool,
dht_event_tx: Option<mpsc::Sender<DhtEvent>>,
) -> impl Future<Item = (), Error = ()> {
// Compatibility shim while we're transitionning to stable Futures.
// Compatibility shim while we're transitioning to stable Futures.
// See https://github.com/paritytech/substrate/issues/3099
let mut rpc_rx = futures03::compat::Compat::new(rpc_rx.map(|v| Ok::<_, ()>(v)));
@@ -725,6 +727,21 @@ fn build_network_future<
let _ = sender.send(network_state);
}
}
rpc::system::Request::NodeRoles(sender) => {
use rpc::system::NodeRole;
let node_roles = (0 .. 8)
.filter(|&bit_number| (roles.bits() >> bit_number) & 1 == 1)
.map(|bit_number| match Roles::from_bits(1 << bit_number) {
Some(Roles::AUTHORITY) => NodeRole::Authority,
Some(Roles::LIGHT) => NodeRole::LightClient,
Some(Roles::FULL) => NodeRole::Full,
_ => NodeRole::UnknownRole(bit_number),
})
.collect();
let _ = sender.send(node_roles);
}
};
}