fix: wait_for_min_peers shouldn't wait forever when all peers are in same work (#2340)

This commit is contained in:
Gary Yu
2019-01-12 07:38:27 +08:00
committed by GitHub
parent 1a6b46b09d
commit 7698b28e16
2 changed files with 21 additions and 5 deletions
+16
View File
@@ -199,6 +199,22 @@ impl Peers {
max_peers
}
// Return number of connected peers that currently advertise more/same work
// (total_difficulty) than/as we do.
pub fn more_or_same_work_peers(&self) -> usize {
let peers = self.connected_peers();
if peers.len() == 0 {
return 0;
}
let total_difficulty = self.total_difficulty();
peers
.iter()
.filter(|x| x.info.total_difficulty() >= total_difficulty)
.count()
}
/// Returns single random peer with more work than us.
pub fn more_work_peer(&self) -> Option<Arc<Peer>> {
self.more_work_peers().pop()