order peers by difficulty

This commit is contained in:
arkpar 2015-12-27 02:27:15 +01:00
parent 1d67a7a373
commit 1a1c61179f
2 changed files with 11 additions and 5 deletions

View File

@ -261,7 +261,8 @@ impl ChainSync {
_ => { _ => {
if self.have_common_block { if self.have_common_block {
//validate chain //validate chain
if number == self.last_imported_block + 1 && info.parent_hash != self.last_imported_hash { if self.have_common_block && number == self.last_imported_block + 1 && info.parent_hash != self.last_imported_hash {
// TODO: lower peer rating
debug!(target: "sync", "Mismatched block header {} {}", number, info.hash()); debug!(target: "sync", "Mismatched block header {} {}", number, info.hash());
continue; continue;
} }
@ -423,10 +424,11 @@ impl ChainSync {
self.send_status(io, peer); self.send_status(io, peer);
} }
/// Resume downloading after witing state /// Resume downloading
fn continue_sync(&mut self, io: &mut SyncIo) { fn continue_sync(&mut self, io: &mut SyncIo) {
let peers: Vec<PeerId> = self.peers.keys().map(|k| *k).collect(); let mut peers: Vec<(PeerId, U256)> = self.peers.iter().map(|(k, p)| (*k, p.difficulty)).collect();
for p in peers { peers.sort_by(|&(_, d1), &(_, d2)| d1.cmp(&d2).reverse()); //TODO: sort by rating
for (p, _) in peers {
self.sync_peer(io, &p, false); self.sync_peer(io, &p, false);
} }
} }
@ -511,12 +513,14 @@ impl ChainSync {
let mut start = 0usize; let mut start = 0usize;
if !self.have_common_block { if !self.have_common_block {
// download backwards until common block is found 1 header at a time // download backwards until common block is found 1 header at a time
start = io.chain().info().last_block_number as usize; let chain_info = io.chain().info();
start = chain_info.last_block_number as usize;
if !self.headers.is_empty() { if !self.headers.is_empty() {
start = min(start, self.headers.range_iter().next().unwrap().0 as usize - 1); start = min(start, self.headers.range_iter().next().unwrap().0 as usize - 1);
} }
if start == 0 { if start == 0 {
self.have_common_block = true; //reached genesis self.have_common_block = true; //reached genesis
self.last_imported_hash = chain_info.genesis_hash;
} }
} }
if self.have_common_block { if self.have_common_block {

View File

@ -298,6 +298,7 @@ impl TestNet {
#[test] #[test]
fn full_sync_two_peers() { fn full_sync_two_peers() {
::env_logger::init().ok();
let mut net = TestNet::new(3); let mut net = TestNet::new(3);
net.peer_mut(1).chain.add_blocks(1000, false); net.peer_mut(1).chain.add_blocks(1000, false);
net.peer_mut(2).chain.add_blocks(1000, false); net.peer_mut(2).chain.add_blocks(1000, false);
@ -308,6 +309,7 @@ fn full_sync_two_peers() {
#[test] #[test]
fn full_sync_empty_blocks() { fn full_sync_empty_blocks() {
::env_logger::init().ok();
let mut net = TestNet::new(3); let mut net = TestNet::new(3);
for n in 0..200 { for n in 0..200 {
net.peer_mut(1).chain.add_blocks(5, n % 2 == 0); net.peer_mut(1).chain.add_blocks(5, n % 2 == 0);