mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-11 20:01:08 +00:00
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:
@@ -53,7 +53,7 @@ mod transport;
|
||||
|
||||
pub use custom_proto::RegisteredProtocol;
|
||||
pub use error::{Error, ErrorKind, DisconnectReason};
|
||||
pub use libp2p::{Multiaddr, multiaddr::{Protocol}, multiaddr, PeerId};
|
||||
pub use libp2p::{Multiaddr, multiaddr::{Protocol}, multiaddr, PeerId, core::PublicKey};
|
||||
pub use secret::obtain_private_key;
|
||||
pub use service_task::{start_service, Service, ServiceEvent};
|
||||
pub use traits::{NetworkConfiguration, NodeIndex, NodeId, NonReservedPeerMode};
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -52,7 +52,7 @@ pub fn rpc_handler<Block: BlockT, ExHash, S, C, A, Y>(
|
||||
S: apis::state::StateApi<Block::Hash, Metadata=Metadata>,
|
||||
C: apis::chain::ChainApi<NumberFor<Block>, Block::Hash, Block::Header, SignedBlock<Block>, Metadata=Metadata>,
|
||||
A: apis::author::AuthorApi<ExHash, Block::Hash, Metadata=Metadata>,
|
||||
Y: apis::system::SystemApi,
|
||||
Y: apis::system::SystemApi<Block::Hash, NumberFor<Block>>,
|
||||
{
|
||||
let mut io = pubsub::PubSubHandler::default();
|
||||
io.extend_with(state.to_delegate());
|
||||
|
||||
@@ -45,6 +45,23 @@ pub struct Health {
|
||||
pub is_syncing: bool,
|
||||
}
|
||||
|
||||
/// Network Peer information
|
||||
#[derive(Debug, PartialEq, Serialize)]
|
||||
pub struct PeerInfo<Hash, Number> {
|
||||
/// Peer Node Index
|
||||
pub index: usize,
|
||||
/// Peer ID
|
||||
pub peer_id: String,
|
||||
/// Roles
|
||||
pub roles: String,
|
||||
/// Protocol version
|
||||
pub protocol_version: u32,
|
||||
/// Peer best block hash
|
||||
pub best_hash: Hash,
|
||||
/// Peer best block 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 {
|
||||
|
||||
@@ -24,14 +24,14 @@ mod tests;
|
||||
|
||||
use std::sync::Arc;
|
||||
use network;
|
||||
use runtime_primitives::traits;
|
||||
use runtime_primitives::traits::{self, Header as HeaderT};
|
||||
|
||||
use self::error::Result;
|
||||
pub use self::helpers::{Properties, SystemInfo, Health};
|
||||
pub use self::helpers::{Properties, SystemInfo, Health, PeerInfo};
|
||||
|
||||
build_rpc_trait! {
|
||||
/// Substrate system RPC API
|
||||
pub trait SystemApi {
|
||||
pub trait SystemApi<Hash, Number> {
|
||||
/// Get the node's implementation name. Plain old string.
|
||||
#[rpc(name = "system_name")]
|
||||
fn system_name(&self) -> Result<String>;
|
||||
@@ -55,6 +55,10 @@ build_rpc_trait! {
|
||||
/// - not performing a major sync
|
||||
#[rpc(name = "system_health")]
|
||||
fn system_health(&self) -> Result<Health>;
|
||||
|
||||
/// Returns currently connected peers
|
||||
#[rpc(name = "system_peers")]
|
||||
fn system_peers(&self) -> Result<Vec<PeerInfo<Hash, Number>>>;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,7 +84,7 @@ impl<B: traits::Block> System<B> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: traits::Block> SystemApi for System<B> {
|
||||
impl<B: traits::Block> SystemApi<B::Hash, <B::Header as HeaderT>::Number> for System<B> {
|
||||
fn system_name(&self) -> Result<String> {
|
||||
Ok(self.info.impl_name.clone())
|
||||
}
|
||||
@@ -115,4 +119,15 @@ impl<B: traits::Block> SystemApi for System<B> {
|
||||
Ok(health)
|
||||
}
|
||||
}
|
||||
|
||||
fn system_peers(&self) -> Result<Vec<PeerInfo<B::Hash, <B::Header as HeaderT>::Number>>> {
|
||||
Ok(self.sync.peers().into_iter().map(|(idx, peer_id, p)| PeerInfo {
|
||||
index: idx,
|
||||
peer_id: peer_id.map_or_else(Default::default, |p| p.to_base58()),
|
||||
roles: format!("{:?}", p.roles),
|
||||
protocol_version: p.protocol_version,
|
||||
best_hash: p.best_hash,
|
||||
best_number: p.best_number,
|
||||
}).collect())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,8 +16,10 @@
|
||||
|
||||
use super::*;
|
||||
|
||||
use network::{self, SyncState, SyncStatus, ProtocolStatus};
|
||||
use network::{self, SyncState, SyncStatus, ProtocolStatus, NodeIndex, PeerId, PeerInfo as NetworkPeerInfo, PublicKey};
|
||||
use network::config::Roles;
|
||||
use test_client::runtime::Block;
|
||||
use primitives::H256;
|
||||
|
||||
#[derive(Default)]
|
||||
struct Status {
|
||||
@@ -37,6 +39,15 @@ impl network::SyncProvider<Block> for Status {
|
||||
num_active_peers: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn peers(&self) -> Vec<(NodeIndex, Option<PeerId>, NetworkPeerInfo<Block>)> {
|
||||
vec![(1, Some(PublicKey::Ed25519((0 .. 32).collect::<Vec<u8>>()).into()), NetworkPeerInfo {
|
||||
roles: Roles::FULL,
|
||||
protocol_version: 1,
|
||||
best_hash: Default::default(),
|
||||
best_number: 1
|
||||
})]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -129,3 +140,18 @@ fn system_health() {
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn system_peers() {
|
||||
assert_eq!(
|
||||
api(None).system_peers().unwrap(),
|
||||
vec![PeerInfo {
|
||||
index: 1,
|
||||
peer_id: "QmS5oyTmdjwBowwAH1D9YQnoe2HyWpVemH8qHiU5RqWPh4".into(),
|
||||
roles: "FULL".into(),
|
||||
protocol_version: 1,
|
||||
best_hash: Default::default(),
|
||||
best_number: 1u64,
|
||||
}]
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user