Added peers details to ethcore_netPeers RPC (#2580)

* added peers details to ethcore_netPeers RPC

* fixed build (traits autoimplemented)

* - documentation fixes
- spaces -> tabs
- Rust-way Option's handling

* prepare for new protocols in ethcore_netPeers

* commas & documentation
This commit is contained in:
Svyatoslav Nikolsky
2016-10-12 21:18:59 +03:00
committed by Arkadiy Paronyan
parent 693b0ec402
commit c9ce25c8f3
16 changed files with 224 additions and 24 deletions

View File

@@ -61,6 +61,30 @@ binary_fixed_size!(SyncStatus);
pub trait SyncProvider: Send + Sync {
/// Get sync status
fn status(&self) -> SyncStatus;
/// Get peers information
fn peers(&self) -> Vec<PeerInfo>;
}
/// Peer connection information
#[derive(Debug, Binary)]
pub struct PeerInfo {
/// Public node id
pub id: Option<String>,
/// Node client ID
pub client_version: String,
/// Capabilities
pub capabilities: Vec<String>,
/// Remote endpoint address
pub remote_address: String,
/// Local endpoint address
pub local_address: String,
/// Ethereum protocol version
pub eth_version: u32,
/// SHA3 of peer best block hash
pub eth_head: H256,
/// Peer total difficulty if known
pub eth_difficulty: Option<U256>,
}
/// Ethereum network protocol handler
@@ -94,6 +118,14 @@ impl SyncProvider for EthSync {
fn status(&self) -> SyncStatus {
self.handler.sync.write().status()
}
/// Get sync peers
fn peers(&self) -> Vec<PeerInfo> {
self.network.with_context_eval(self.subprotocol_name, |context| {
let sync_io = NetSyncIo::new(context, &*self.handler.chain, &*self.handler.snapshot_service);
self.handler.sync.write().peers(&sync_io)
}).unwrap_or(Vec::new())
}
}
struct SyncProtocolHandler {

View File

@@ -102,6 +102,7 @@ use super::SyncConfig;
use blocks::BlockCollection;
use snapshot::{Snapshot, ChunkType};
use rand::{thread_rng, Rng};
use api::PeerInfo as PeerInfoDigest;
known_heap_size!(0, PeerInfo);
@@ -346,7 +347,7 @@ impl ChainSync {
}
}
/// @returns Synchonization status
/// Returns synchonization status
pub fn status(&self) -> SyncStatus {
SyncStatus {
state: self.state.clone(),
@@ -368,6 +369,25 @@ impl ChainSync {
}
}
/// Returns information on peers connections
pub fn peers(&self, io: &SyncIo) -> Vec<PeerInfoDigest> {
self.peers.iter()
.filter_map(|(&peer_id, ref peer_data)|
io.peer_session_info(peer_id).map(|session_info|
PeerInfoDigest {
id: session_info.id.map(|id| id.hex()),
client_version: session_info.client_version,
capabilities: session_info.peer_capabilities.into_iter().map(|c| c.to_string()).collect(),
remote_address: session_info.remote_address,
local_address: session_info.local_address,
eth_version: peer_data.protocol_version,
eth_difficulty: peer_data.difficulty,
eth_head: peer_data.latest_hash,
})
)
.collect()
}
/// Abort all sync activity
pub fn abort(&mut self, io: &mut SyncIo) {
self.restart(io);

View File

@@ -60,7 +60,7 @@ mod api {
}
pub use api::{EthSync, SyncProvider, SyncClient, NetworkManagerClient, ManageNetwork, SyncConfig,
ServiceConfiguration, NetworkConfiguration};
ServiceConfiguration, NetworkConfiguration, PeerInfo};
pub use chain::{SyncStatus, SyncState};
pub use network::{is_valid_node_url, NonReservedPeerMode, NetworkError};

View File

@@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
use network::{NetworkContext, PeerId, PacketId, NetworkError};
use network::{NetworkContext, PeerId, PacketId, NetworkError, SessionInfo};
use ethcore::client::BlockChainClient;
use ethcore::snapshot::SnapshotService;
@@ -34,10 +34,12 @@ pub trait SyncIo {
fn chain(&self) -> &BlockChainClient;
/// Get the snapshot service.
fn snapshot_service(&self) -> &SnapshotService;
/// Returns peer client identifier string
/// Returns peer identifier string
fn peer_info(&self, peer_id: PeerId) -> String {
peer_id.to_string()
}
/// Returns information on p2p session
fn peer_session_info(&self, peer_id: PeerId) -> Option<SessionInfo>;
/// Maximum mutuallt supported ETH protocol version
fn eth_protocol_version(&self, peer_id: PeerId) -> u8;
/// Returns if the chain block queue empty
@@ -91,8 +93,8 @@ impl<'s, 'h> SyncIo for NetSyncIo<'s, 'h> {
self.snapshot_service
}
fn peer_info(&self, peer_id: PeerId) -> String {
self.network.peer_info(peer_id)
fn peer_session_info(&self, peer_id: PeerId) -> Option<SessionInfo> {
self.network.session_info(peer_id)
}
fn is_expired(&self) -> bool {

View File

@@ -83,6 +83,10 @@ impl<'p> SyncIo for TestIo<'p> {
self.snapshot_service
}
fn peer_session_info(&self, _peer_id: PeerId) -> Option<SessionInfo> {
None
}
fn eth_protocol_version(&self, _peer: PeerId) -> u8 {
64
}