Deactivate peers on invalid block bodies

This commit is contained in:
arkpar 2016-06-20 11:07:22 +02:00
parent 3bf67486ae
commit 5e9c8db4c9
2 changed files with 20 additions and 6 deletions

View File

@ -95,13 +95,18 @@ impl BlockCollection {
} }
/// Insert a collection of block bodies for previously downloaded headers. /// Insert a collection of block bodies for previously downloaded headers.
pub fn insert_bodies(&mut self, bodies: Vec<Bytes>) { pub fn insert_bodies(&mut self, bodies: Vec<Bytes>) -> usize {
let mut inserted = 0;
for b in bodies.into_iter() { for b in bodies.into_iter() {
if let Err(e) = self.insert_body(b) { if let Err(e) = self.insert_body(b) {
trace!(target: "sync", "Ignored invalid body: {:?}", e); trace!(target: "sync", "Ignored invalid body: {:?}", e);
} }
else {
inserted += 1;
} }
} }
inserted
}
/// Returns a set of block hashes that require a body download. The returned set is marked as being downloaded. /// Returns a set of block hashes that require a body download. The returned set is marked as being downloaded.
pub fn needed_bodies(&mut self, count: usize, _ignore_downloading: bool) -> Vec<H256> { pub fn needed_bodies(&mut self, count: usize, _ignore_downloading: bool) -> Vec<H256> {
@ -231,13 +236,19 @@ impl BlockCollection {
Some(ref mut block) => { Some(ref mut block) => {
trace!(target: "sync", "Got body {}", h); trace!(target: "sync", "Got body {}", h);
block.body = Some(body.as_raw().to_vec()); block.body = Some(body.as_raw().to_vec());
},
None => warn!("Got body with no header {}", h)
}
}
None => trace!(target: "sync", "Ignored unknown/stale block body")
};
Ok(()) Ok(())
},
None => {
warn!("Got body with no header {}", h);
Err(UtilError::Network(NetworkError::BadProtocol))
}
}
}
None => {
trace!(target: "sync", "Ignored unknown/stale block body");
Err(UtilError::Network(NetworkError::BadProtocol))
}
}
} }
fn insert_header(&mut self, header: Bytes) -> Result<H256, UtilError> { fn insert_header(&mut self, header: Bytes) -> Result<H256, UtilError> {

View File

@ -517,7 +517,10 @@ impl ChainSync {
for i in 0..item_count { for i in 0..item_count {
bodies.push(try!(r.at(i)).as_raw().to_vec()); bodies.push(try!(r.at(i)).as_raw().to_vec());
} }
self.blocks.insert_bodies(bodies); if self.blocks.insert_bodies(bodies) != item_count {
trace!(target: "sync", "Deactivating peer for giving invalid block bodies");
self.deactivate_peer(io, peer_id);
}
self.collect_blocks(io); self.collect_blocks(io);
} }
self.continue_sync(io); self.continue_sync(io);