expose peer information via rpc (#1362)

* expose peer information via rpc

* fixes tests

* cleanup

Co-Authored-By: xlc <xlchen1291@gmail.com>

* Update docs

Co-Authored-By: xlc <xlchen1291@gmail.com>

* Add missing docs

* keep original type for PeerInfo best_hash/best_number

* cleanup

* Update mod.rs
This commit is contained in:
Xiliang Chen
2019-01-10 00:18:24 +13:00
committed by Gav Wood
parent 43269f0dbc
commit eb3503b0c7
8 changed files with 91 additions and 9 deletions
+1 -1
View File
@@ -69,7 +69,7 @@ pub use protocol::{ProtocolStatus, PeerInfo, Context};
pub use sync::{Status as SyncStatus, SyncState};
pub use network_libp2p::{
NodeIndex, ProtocolId, Severity, Protocol, Multiaddr,
obtain_private_key, multiaddr,
obtain_private_key, multiaddr, PeerId, PublicKey
};
pub use message::{generic as generic_message, RequestId, Status as StatusMessage};
pub use error::Error;
+14
View File
@@ -240,6 +240,20 @@ impl<B: BlockT, S: NetworkSpecialization<B>, H: ExHashT> Protocol<B, S, H> {
}
}
pub fn peers(&self) -> Vec<(NodeIndex, PeerInfo<B>)> {
self.context_data.peers.read().iter().map(|(idx, p)| {
(
*idx,
PeerInfo {
roles: p.roles,
protocol_version: p.protocol_version,
best_hash: p.best_hash,
best_number: p.best_number,
}
)
}).collect()
}
pub fn handle_packet(&self, io: &mut SyncIo, who: NodeIndex, mut data: &[u8]) {
let message: Message<B> = match Decode::decode(&mut data) {
Some(m) => m,
+11 -1
View File
@@ -26,7 +26,7 @@ use network_libp2p::{RegisteredProtocol, parse_str_addr, Protocol as Libp2pProto
use io::NetSyncIo;
use consensus::import_queue::{ImportQueue, Link};
use consensus_gossip::ConsensusGossip;
use protocol::{self, Protocol, ProtocolContext, Context, ProtocolStatus};
use protocol::{self, Protocol, ProtocolContext, Context, ProtocolStatus, PeerInfo};
use config::Params;
use error::Error;
use specialization::NetworkSpecialization;
@@ -45,6 +45,8 @@ const PROPAGATE_TIMEOUT: Duration = Duration::from_millis(5000);
pub trait SyncProvider<B: BlockT>: Send + Sync {
/// Get sync status
fn status(&self) -> ProtocolStatus<B>;
/// Get currently connected peers
fn peers(&self) -> Vec<(NodeIndex, Option<PeerId>, PeerInfo<B>)>;
}
/// Minimum Requirements for a Hash within Networking
@@ -228,6 +230,14 @@ impl<B: BlockT + 'static, S: NetworkSpecialization<B>, H: ExHashT> SyncProvider<
fn status(&self) -> ProtocolStatus<B> {
self.handler.status()
}
fn peers(&self) -> Vec<(NodeIndex, Option<PeerId>, PeerInfo<B>)> {
let peers = self.handler.peers();
let network = self.network.lock();
peers.into_iter().map(|(idx, info)| {
(idx, network.peer_id_of_node(idx).map(|p| p.clone()), info)
}).collect::<Vec<_>>()
}
}
/// Trait for managing network