Add connected peers to protocol, use in sync provider (#1857)

* add connected peers to protocol, use in sync provider

* use PeerId::random

* address comments`

* docs

* fix import of PeerId

* rewrite rpc tests using PeerId::random

* whitespace

* nits

* remove option around peer id and remove field

* further removal of the option around peer id

* fix rpc tests
This commit is contained in:
Gregory Terzian
2019-02-28 01:50:47 +08:00
committed by Gav Wood
parent ea7da0d4a4
commit bc15fa31ff
7 changed files with 140 additions and 74 deletions
+3 -3
View File
@@ -111,9 +111,9 @@ impl<B: traits::Block> SystemApi<B::Hash, <B::Header as HeaderT>::Number> for Sy
}
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()),
Ok(self.sync.peers().into_iter().map(|(index, p)| PeerInfo {
index,
peer_id: p.peer_id.to_base58(),
roles: format!("{:?}", p.roles),
protocol_version: p.protocol_version,
best_hash: p.best_hash,
+27 -6
View File
@@ -16,16 +16,27 @@
use super::*;
use network::{self, SyncState, SyncStatus, ProtocolStatus, NodeIndex, PeerId, PeerInfo as NetworkPeerInfo, PublicKey};
use network::{self, SyncState, SyncStatus, ProtocolStatus, NodeIndex, PeerId, PeerInfo as NetworkPeerInfo};
use network::config::Roles;
use test_client::runtime::Block;
use assert_matches::assert_matches;
#[derive(Default)]
struct Status {
pub peers: usize,
pub is_syncing: bool,
pub is_dev: bool,
pub peer_id: PeerId,
}
impl Default for Status {
fn default() -> Status {
Status {
peer_id: PeerId::random(),
peers: 0,
is_syncing: false,
is_dev: false,
}
}
}
impl network::SyncProvider<Block> for Status {
@@ -41,8 +52,9 @@ impl network::SyncProvider<Block> for Status {
}
}
fn peers(&self) -> Vec<(NodeIndex, Option<PeerId>, NetworkPeerInfo<Block>)> {
vec![(1, Some(PublicKey::Ed25519((0 .. 32).collect::<Vec<u8>>()).into()), NetworkPeerInfo {
fn peers(&self) -> Vec<(NodeIndex, NetworkPeerInfo<Block>)> {
vec![(1, NetworkPeerInfo {
peer_id: self.peer_id.clone(),
roles: Roles::FULL,
protocol_version: 1,
best_hash: Default::default(),
@@ -108,6 +120,7 @@ fn system_health() {
assert_matches!(
api(Status {
peer_id: PeerId::random(),
peers: 5,
is_syncing: true,
is_dev: true,
@@ -121,6 +134,7 @@ fn system_health() {
assert_eq!(
api(Status {
peer_id: PeerId::random(),
peers: 5,
is_syncing: false,
is_dev: false,
@@ -134,6 +148,7 @@ fn system_health() {
assert_eq!(
api(Status {
peer_id: PeerId::random(),
peers: 0,
is_syncing: false,
is_dev: true,
@@ -148,11 +163,17 @@ fn system_health() {
#[test]
fn system_peers() {
let peer_id = PeerId::random();
assert_eq!(
api(None).system_peers().unwrap(),
api(Status {
peer_id: peer_id.clone(),
peers: 1,
is_syncing: false,
is_dev: true,
}).system_peers().unwrap(),
vec![PeerInfo {
index: 1,
peer_id: "QmS5oyTmdjwBowwAH1D9YQnoe2HyWpVemH8qHiU5RqWPh4".into(),
peer_id: peer_id.to_base58(),
roles: "FULL".into(),
protocol_version: 1,
best_hash: Default::default(),