Add an RPC request for the state of the network (#1884)

* Add an RPC request for the state of the network

* Fix concerns

* Fix tests

* Replace comment with TODO

* Rename the RPC
This commit is contained in:
Pierre Krieger
2019-02-28 19:28:38 +01:00
committed by Gav Wood
parent 72b9df8237
commit c21d7436cc
9 changed files with 300 additions and 21 deletions
+7 -1
View File
@@ -23,7 +23,7 @@ use futures::{Async, Future, Stream, stream, sync::oneshot};
use parking_lot::{Mutex, RwLock};
use network_libp2p::{ProtocolId, NetworkConfiguration, NodeIndex, ErrorKind, Severity};
use network_libp2p::{start_service, parse_str_addr, Service as NetworkService, ServiceEvent as NetworkServiceEvent};
use network_libp2p::{Protocol as Libp2pProtocol, RegisteredProtocol};
use network_libp2p::{Protocol as Libp2pProtocol, RegisteredProtocol, NetworkState};
use consensus::import_queue::{ImportQueue, Link};
use crate::consensus_gossip::ConsensusGossip;
use crate::message::{Message, ConsensusEngineId};
@@ -46,6 +46,8 @@ pub type FetchFuture = oneshot::Receiver<Vec<u8>>;
pub trait SyncProvider<B: BlockT>: Send + Sync {
/// Get sync status
fn status(&self) -> ProtocolStatus<B>;
/// Get network state.
fn network_state(&self) -> NetworkState;
/// Get currently connected peers
fn peers(&self) -> Vec<(NodeIndex, PeerInfo<B>)>;
}
@@ -290,6 +292,10 @@ impl<B: BlockT + 'static, S: NetworkSpecialization<B>> SyncProvider<B> for Servi
2 Service keeps a sender to protocol, and the ProtocolMsg::Stop is never sent.")
}
fn network_state(&self) -> NetworkState {
self.network.lock().state()
}
fn peers(&self) -> Vec<(NodeIndex, PeerInfo<B>)> {
let peers = (*self.peers.read()).clone();
peers.into_iter().map(|(idx, connected)| (idx, connected.peer_info)).collect()