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

@@ -165,13 +165,16 @@ impl<C, M, S: ?Sized, F> Ethcore for EthcoreClient<C, M, S, F> where
fn net_peers(&self) -> Result<Peers, Error> {
try!(self.active());
let sync_status = take_weak!(self.sync).status();
let sync = take_weak!(self.sync);
let sync_status = sync.status();
let net_config = take_weak!(self.net).network_config();
let peers = sync.peers().into_iter().map(Into::into).collect();
Ok(Peers {
active: sync_status.num_active_peers,
connected: sync_status.num_peers,
max: sync_status.current_max_peers(net_config.min_peers, net_config.max_peers),
peers: peers
})
}

View File

@@ -17,7 +17,7 @@
//! Test implementation of SyncProvider.
use util::{RwLock, U256};
use ethsync::{SyncProvider, SyncStatus, SyncState};
use ethsync::{SyncProvider, SyncStatus, SyncState, PeerInfo};
/// TestSyncProvider config.
pub struct Config {
@@ -60,5 +60,30 @@ impl SyncProvider for TestSyncProvider {
fn status(&self) -> SyncStatus {
self.status.read().clone()
}
fn peers(&self) -> Vec<PeerInfo> {
vec![
PeerInfo {
id: Some("node1".to_owned()),
client_version: "Parity/1".to_owned(),
capabilities: vec!["eth/62".to_owned(), "eth/63".to_owned()],
remote_address: "127.0.0.1:7777".to_owned(),
local_address: "127.0.0.1:8888".to_owned(),
eth_version: 62,
eth_difficulty: Some(40.into()),
eth_head: 50.into()
},
PeerInfo {
id: None,
client_version: "Parity/2".to_owned(),
capabilities: vec!["eth/63".to_owned(), "eth/64".to_owned()],
remote_address: "Handshake".to_owned(),
local_address: "127.0.0.1:3333".to_owned(),
eth_version: 64,
eth_difficulty: None,
eth_head: 60.into()
}
]
}
}

View File

@@ -208,7 +208,12 @@ fn rpc_ethcore_net_peers() {
io.add_delegate(ethcore_client(&client, &miner, &sync, &net).to_delegate());
let request = r#"{"jsonrpc": "2.0", "method": "ethcore_netPeers", "params":[], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":{"active":0,"connected":120,"max":50},"id":1}"#;
let response = "{\"jsonrpc\":\"2.0\",\"result\":{\"active\":0,\"connected\":120,\"max\":50,\"peers\":[{\"caps\":[\"eth/62\",\"eth/63\"],\
\"id\":\"node1\",\"name\":\"Parity/1\",\"network\":{\"localAddress\":\"127.0.0.1:8888\",\"remoteAddress\":\"127.0.0.1:7777\"}\
,\"protocols\":{\"eth\":{\"difficulty\":\"0x28\",\"head\":\"0000000000000000000000000000000000000000000000000000000000000032\"\
,\"version\":62}}},{\"caps\":[\"eth/63\",\"eth/64\"],\"id\":null,\"name\":\"Parity/2\",\"network\":{\"localAddress\":\
\"127.0.0.1:3333\",\"remoteAddress\":\"Handshake\"},\"protocols\":{\"eth\":{\"difficulty\":null,\"head\":\
\"000000000000000000000000000000000000000000000000000000000000003c\",\"version\":64}}}]},\"id\":1}";
assert_eq!(io.handle_request_sync(request), Some(response.to_owned()));
}

View File

@@ -42,7 +42,7 @@ pub use self::filter::{Filter, FilterChanges};
pub use self::hash::{H64, H160, H256, H512, H520, H2048};
pub use self::index::Index;
pub use self::log::Log;
pub use self::sync::{SyncStatus, SyncInfo, Peers};
pub use self::sync::{SyncStatus, SyncInfo, Peers, PeerInfo, PeerNetworkInfo, PeerProtocolsInfo, PeerEthereumProtocolInfo};
pub use self::transaction::Transaction;
pub use self::transaction_request::TransactionRequest;
pub use self::receipt::Receipt;

View File

@@ -14,6 +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 ethsync::PeerInfo as SyncPeerInfo;
use serde::{Serialize, Serializer};
use v1::types::U256;
@@ -32,7 +33,7 @@ pub struct SyncInfo {
}
/// Peers info
#[derive(Default, Debug, Serialize, PartialEq)]
#[derive(Default, Debug, Serialize)]
pub struct Peers {
/// Number of active peers
pub active: usize,
@@ -40,6 +41,52 @@ pub struct Peers {
pub connected: usize,
/// Max number of peers
pub max: u32,
/// Detailed information on peers
pub peers: Vec<PeerInfo>,
}
/// Peer connection information
#[derive(Default, Debug, Serialize)]
pub struct PeerInfo {
/// Public node id
pub id: Option<String>,
/// Node client ID
pub name: String,
/// Capabilities
pub caps: Vec<String>,
/// Network information
pub network: PeerNetworkInfo,
/// Protocols information
pub protocols: PeerProtocolsInfo,
}
/// Peer network information
#[derive(Default, Debug, Serialize)]
pub struct PeerNetworkInfo {
/// Remote endpoint address
#[serde(rename="remoteAddress")]
pub remote_address: String,
/// Local endpoint address
#[serde(rename="localAddress")]
pub local_address: String,
}
/// Peer protocols information
#[derive(Default, Debug, Serialize)]
pub struct PeerProtocolsInfo {
/// Ethereum protocol information
pub eth: Option<PeerEthereumProtocolInfo>,
}
/// Peer Ethereum protocol information
#[derive(Default, Debug, Serialize)]
pub struct PeerEthereumProtocolInfo {
/// Negotiated ethereum protocol version
pub version: u32,
/// Peer total difficulty if known
pub difficulty: Option<U256>,
/// SHA3 of peer best block hash
pub head: String,
}
/// Sync status
@@ -61,6 +108,27 @@ impl Serialize for SyncStatus {
}
}
impl From<SyncPeerInfo> for PeerInfo {
fn from(p: SyncPeerInfo) -> PeerInfo {
PeerInfo {
id: p.id,
name: p.client_version,
caps: p.capabilities,
network: PeerNetworkInfo {
remote_address: p.remote_address,
local_address: p.local_address,
},
protocols: PeerProtocolsInfo {
eth: Some(PeerEthereumProtocolInfo {
version: p.eth_version,
difficulty: p.eth_difficulty.map(|d| d.into()),
head: p.eth_head.hex(),
})
},
}
}
}
#[cfg(test)]
mod tests {
use serde_json;
@@ -77,7 +145,7 @@ mod tests {
fn test_serialize_peers() {
let t = Peers::default();
let serialized = serde_json::to_string(&t).unwrap();
assert_eq!(serialized, r#"{"active":0,"connected":0,"max":0}"#);
assert_eq!(serialized, r#"{"active":0,"connected":0,"max":0,"peers":[]}"#);
}
#[test]