Disaable peer if no common block found

This commit is contained in:
arkpar 2016-11-28 16:30:36 +01:00
parent a7037f8e5b
commit cf5b409fed
3 changed files with 20 additions and 0 deletions

View File

@ -258,6 +258,12 @@ impl BlockDownloader {
self.blocks.reset_to(hashes);
self.state = State::Blocks;
return Ok(DownloadAction::Reset);
} else {
let best = io.chain().chain_info().best_block_number;
if best > self.last_imported_block && best - self.last_imported_block > MAX_REORG_BLOCKS {
trace!(target: "sync", "No common block, disabling peer");
return Err(BlockDownloaderImportError::Invalid);
}
}
},
State::Blocks => {

View File

@ -250,3 +250,14 @@ fn high_td_attach() {
assert_eq!(net.peer(0).chain.chain_info().best_block_number, 5);
}
#[test]
fn disconnect_on_unrelated_chain() {
::env_logger::init().ok();
let mut net = TestNet::new(2);
net.peer_mut(0).chain.add_blocks(200, EachBlockWith::Uncle);
net.peer_mut(1).chain.add_blocks(100, EachBlockWith::Nothing);
net.sync();
assert_eq!(net.disconnect_events, vec![(0, 0)]);
}

View File

@ -123,6 +123,7 @@ pub struct TestPeer {
pub struct TestNet {
pub peers: Vec<TestPeer>,
pub started: bool,
pub disconnect_events: Vec<(PeerId, PeerId)>, //disconnected (initiated by, to)
}
impl TestNet {
@ -140,6 +141,7 @@ impl TestNet {
let mut net = TestNet {
peers: Vec::new(),
started: false,
disconnect_events: Vec::new(),
};
for _ in 0..n {
let chain = TestBlockChainClient::new();
@ -190,6 +192,7 @@ impl TestNet {
// notify this that disconnecting peers are disconnecting
let mut io = TestIo::new(&mut p.chain, &p.snapshot_service, &mut p.queue, Some(*d));
p.sync.write().on_peer_aborting(&mut io, *d);
self.disconnect_events.push((peer, *d));
}
to_disconnect
};