Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prefer outbound peers when syncing #3521

Merged
merged 1 commit into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions p2p/src/peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,6 @@ impl Peers {
self.iter().connected().by_addr(addr)
}

pub fn max_peer_difficulty(&self) -> Difficulty {
self.iter()
.connected()
.max_difficulty()
.unwrap_or(Difficulty::zero())
}

pub fn is_banned(&self, peer_addr: PeerAddr) -> bool {
if let Ok(peer) = self.store.get_peer(peer_addr) {
return peer.flags == State::Banned;
Expand Down
14 changes: 5 additions & 9 deletions servers/src/grin/seed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use std::sync::{mpsc, Arc};
use std::{cmp, str, thread, time};

use crate::core::global;
use crate::core::pow::Difficulty;
use crate::p2p;
use crate::p2p::types::PeerAddr;
use crate::p2p::ChainAdapter;
Expand Down Expand Up @@ -172,15 +173,10 @@ fn monitor_peers(
total_count += 1;
}

let peers_count = peers.iter().connected().count();

let max_diff = peers.max_peer_difficulty();
let most_work_count = peers
.iter()
.outbound()
.with_difficulty(|x| x >= max_diff)
.connected()
.count();
let peers_iter = || peers.iter().connected();
let peers_count = peers_iter().count();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change to using empty closures here and in other files when iterating peers is just a rust convenience thing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to build a couple of instances of the same (or similar) iterator.
And the closure just makes this a lot easier to keep them consistent.

let max_diff = peers_iter().max_difficulty().unwrap_or(Difficulty::zero());
let most_work_count = peers_iter().with_difficulty(|x| x >= max_diff).count();

debug!(
"monitor_peers: on {}:{}, {} connected ({} most_work). \
Expand Down
27 changes: 19 additions & 8 deletions servers/src/grin/sync/body_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,25 @@ impl BodySync {
let head = self.chain.head()?;

// Find connected peers with strictly greater difficulty than us.
let peers: Vec<_> = self
.peers
.iter()
.outbound()
.with_difficulty(|x| x > head.total_difficulty)
.connected()
.into_iter()
.collect();
let peers_iter = || {
self.peers
.iter()
.with_difficulty(|x| x > head.total_difficulty)
.connected()
};

// We prefer outbound peers with greater difficulty.
let mut peers: Vec<_> = peers_iter().outbound().into_iter().collect();
if peers.is_empty() {
debug!("no outbound peers with more work, considering inbound");
peers = peers_iter().inbound().into_iter().collect();
}

// If we have no peers (outbound or inbound) then we are done for now.
if peers.is_empty() {
debug!("no peers (inbound or outbound) with more work");
return Ok(false);
}

// if we have 5 peers to sync from then ask for 50 blocks total (peer_count *
// 10) max will be 80 if all 8 peers are advertising more work
Expand Down
27 changes: 18 additions & 9 deletions servers/src/grin/sync/header_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::sync::Arc;
use crate::chain::{self, SyncState, SyncStatus};
use crate::common::types::Error;
use crate::core::core::hash::{Hash, Hashed};
use crate::core::pow::Difficulty;
use crate::p2p::{self, types::ReasonForBan, Capabilities, Peer};

pub struct HeaderSync {
Expand Down Expand Up @@ -170,15 +171,23 @@ impl HeaderSync {

fn header_sync(&mut self) -> Option<Arc<Peer>> {
if let Ok(header_head) = self.chain.header_head() {
let max_diff = self.peers.max_peer_difficulty();
let peer = self
.peers
.iter()
.outbound()
.with_capabilities(Capabilities::HEADER_HIST)
.with_difficulty(|x| x >= max_diff)
.connected()
.choose_random();
let peers_iter = || {
self.peers
.iter()
.with_capabilities(Capabilities::HEADER_HIST)
.connected()
};

// Filter peers further based on max difficulty.
let max_diff = peers_iter().max_difficulty().unwrap_or(Difficulty::zero());
let peers_iter = || peers_iter().with_difficulty(|x| x >= max_diff);

// Choose a random "most work" peer, preferring outbound if at all possible.
let peer = peers_iter().outbound().choose_random().or_else(|| {
warn!("no suitable outbound peer for header sync, considering inbound");
peers_iter().inbound().choose_random()
});

if let Some(peer) = peer {
if peer.info.total_difficulty() > header_head.total_difficulty {
return self.request_headers(peer);
Expand Down
27 changes: 18 additions & 9 deletions servers/src/grin/sync/state_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::sync::Arc;
use crate::chain::{self, SyncState, SyncStatus};
use crate::core::core::hash::Hashed;
use crate::core::global;
use crate::core::pow::Difficulty;
use crate::p2p::{self, Capabilities, Peer};

/// Fast sync has 3 "states":
Expand Down Expand Up @@ -158,15 +159,23 @@ impl StateSync {
let mut txhashset_height = header_head.height.saturating_sub(threshold);
txhashset_height = txhashset_height.saturating_sub(txhashset_height % archive_interval);

let max_diff = self.peers.max_peer_difficulty();
let peer = self
.peers
.iter()
.outbound()
.with_capabilities(Capabilities::TXHASHSET_HIST)
.with_difficulty(|x| x >= max_diff)
.connected()
.choose_random();
let peers_iter = || {
self.peers
.iter()
.with_capabilities(Capabilities::TXHASHSET_HIST)
.connected()
};

// Filter peers further based on max difficulty.
let max_diff = peers_iter().max_difficulty().unwrap_or(Difficulty::zero());
let peers_iter = || peers_iter().with_difficulty(|x| x >= max_diff);

// Choose a random "most work" peer, preferring outbound if at all possible.
let peer = peers_iter().outbound().choose_random().or_else(|| {
warn!("no suitable outbound peer for state sync, considering inbound");
peers_iter().inbound().choose_random()
});

if let Some(peer) = peer {
// ask for txhashset at state_sync_threshold
let mut txhashset_head = self
Expand Down
11 changes: 9 additions & 2 deletions servers/src/grin/sync/syncer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,18 @@ impl SyncRunner {
let mut is_syncing = self.sync_state.is_syncing();

// Find a peer with greatest known difficulty.
let max_diff = self.peers.max_peer_difficulty();
// Consider all peers, both inbound and outbound.
// We will prioritize syncing against outbound peers exclusively when possible.
// But we do support syncing against an inbound peer if greater work than any outbound peers.
let max_diff = self
.peers
.iter()
.connected()
.max_difficulty()
.unwrap_or(Difficulty::zero());
let peer = self
.peers
.iter()
.outbound()
.with_difficulty(|x| x >= max_diff)
.connected()
.choose_random();
Expand Down