mirror of
https://github.com/mimblewimble/grin.git
synced 2025-01-21 03:21:08 +03:00
fix: wait_for_min_peers shouldn't wait forever when all peers are in same work (#2340)
This commit is contained in:
parent
1a6b46b09d
commit
7698b28e16
2 changed files with 21 additions and 5 deletions
|
@ -199,6 +199,22 @@ impl Peers {
|
||||||
max_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.
|
/// Returns single random peer with more work than us.
|
||||||
pub fn more_work_peer(&self) -> Option<Arc<Peer>> {
|
pub fn more_work_peer(&self) -> Option<Arc<Peer>> {
|
||||||
self.more_work_peers().pop()
|
self.more_work_peers().pop()
|
||||||
|
|
|
@ -77,18 +77,18 @@ impl SyncRunner {
|
||||||
let mut n = 0;
|
let mut n = 0;
|
||||||
const MIN_PEERS: usize = 3;
|
const MIN_PEERS: usize = 3;
|
||||||
loop {
|
loop {
|
||||||
let wp = self.peers.more_work_peers();
|
let wp = self.peers.more_or_same_work_peers();
|
||||||
// exit loop when:
|
// 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
|
// * we are synced already, e.g. grin was quickly restarted
|
||||||
// * timeout
|
// * timeout
|
||||||
if wp.len() > MIN_PEERS
|
if wp > MIN_PEERS
|
||||||
|| (wp.len() == 0
|
|| (wp == 0
|
||||||
&& self.peers.enough_peers()
|
&& self.peers.enough_peers()
|
||||||
&& head.total_difficulty > Difficulty::zero())
|
&& head.total_difficulty > Difficulty::zero())
|
||||||
|| n > wait_secs
|
|| n > wait_secs
|
||||||
{
|
{
|
||||||
if wp.len() > 0 || !global::is_production_mode() {
|
if wp > 0 || !global::is_production_mode() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue