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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 5 deletions

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()

View file

@ -77,18 +77,18 @@ impl SyncRunner {
let mut n = 0;
const MIN_PEERS: usize = 3;
loop {
let wp = self.peers.more_work_peers();
let wp = self.peers.more_or_same_work_peers();
// exit loop when:
// * we have more than MIN_PEERS more_work peers
// * we have more than MIN_PEERS more_or_same_work peers
// * we are synced already, e.g. grin was quickly restarted
// * timeout
if wp.len() > MIN_PEERS
|| (wp.len() == 0
if wp > MIN_PEERS
|| (wp == 0
&& self.peers.enough_peers()
&& head.total_difficulty > Difficulty::zero())
|| n > wait_secs
{
if wp.len() > 0 || !global::is_production_mode() {
if wp > 0 || !global::is_production_mode() {
break;
}
}