Skip to content

Commit

Permalink
Flexible peer filtering (#3458)
Browse files Browse the repository at this point in the history
* first pass at peers iter cleanup

* more flexible peer with diff lookups

* PeersIter with impl Iterator

* sync against outbound peers
reorder peers filters so expensive filters come later

* filter peers by capabilities during sync

* prefer outbound peers with high total difficulty

* with_difficulty now takes a fn to allow more flexible comparisons based on difficulty

* rename peers_iter() -> iter()
  • Loading branch information
antiochp committed Oct 27, 2020
1 parent cea546c commit 25fcefa
Show file tree
Hide file tree
Showing 13 changed files with 235 additions and 183 deletions.
10 changes: 6 additions & 4 deletions api/src/handlers/peers_api.rs
Expand Up @@ -28,7 +28,7 @@ pub struct PeersAllHandler {

impl Handler for PeersAllHandler {
fn get(&self, _req: Request<Body>) -> ResponseFuture {
let peers = &w_fut!(&self.peers).all_peers();
let peers = &w_fut!(&self.peers).all_peer_data();
json_response_pretty(&peers)
}
}
Expand All @@ -40,8 +40,9 @@ pub struct PeersConnectedHandler {
impl PeersConnectedHandler {
pub fn get_connected_peers(&self) -> Result<Vec<PeerInfoDisplay>, Error> {
let peers = w(&self.peers)?
.connected_peers()
.iter()
.connected()
.into_iter()
.map(|p| p.info.clone().into())
.collect::<Vec<PeerInfoDisplay>>();
Ok(peers)
Expand All @@ -51,8 +52,9 @@ impl PeersConnectedHandler {
impl Handler for PeersConnectedHandler {
fn get(&self, _req: Request<Body>) -> ResponseFuture {
let peers: Vec<PeerInfoDisplay> = w_fut!(&self.peers)
.connected_peers()
.iter()
.connected()
.into_iter()
.map(|p| p.info.clone().into())
.collect();
json_response(&peers)
Expand All @@ -77,7 +79,7 @@ impl PeerHandler {
})?;
return Ok(vec![peer_data]);
}
let peers = w(&self.peers)?.all_peers();
let peers = w(&self.peers)?.all_peer_data();
Ok(peers)
}

Expand Down
8 changes: 7 additions & 1 deletion api/src/handlers/server_api.rs
Expand Up @@ -21,6 +21,7 @@ use crate::types::*;
use crate::web::*;
use hyper::{Body, Request};
use serde_json::json;
use std::convert::TryInto;
use std::sync::Weak;

// RESTful index of available api endpoints
Expand Down Expand Up @@ -54,7 +55,12 @@ impl StatusHandler {
let (api_sync_status, api_sync_info) = sync_status_to_api(sync_status);
Ok(Status::from_tip_and_peers(
head,
w(&self.peers)?.peer_count(),
w(&self.peers)?
.iter()
.connected()
.count()
.try_into()
.unwrap(),
api_sync_status,
api_sync_info,
))
Expand Down

0 comments on commit 25fcefa

Please sign in to comment.