Merge pull request #1345 from ethcore/sync-bodies

Sync attack defense: Deactivate peers on invalid block bodies
This commit is contained in:
Arkadiy Paronyan 2016-06-20 14:03:53 +02:00 committed by GitHub
commit f5682737d3
2 changed files with 20 additions and 6 deletions

View File

@ -95,12 +95,17 @@ impl BlockCollection {
}
/// 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() {
if let Err(e) = self.insert_body(b) {
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.
@ -231,13 +236,19 @@ impl BlockCollection {
Some(ref mut block) => {
trace!(target: "sync", "Got body {}", h);
block.body = Some(body.as_raw().to_vec());
Ok(())
},
None => warn!("Got body with no header {}", h)
None => {
warn!("Got body with no header {}", h);
Err(UtilError::Network(NetworkError::BadProtocol))
}
}
}
None => trace!(target: "sync", "Ignored unknown/stale block body")
};
Ok(())
None => {
trace!(target: "sync", "Ignored unknown/stale block body");
Err(UtilError::Network(NetworkError::BadProtocol))
}
}
}
fn insert_header(&mut self, header: Bytes) -> Result<H256, UtilError> {

View File

@ -517,7 +517,10 @@ impl ChainSync {
for i in 0..item_count {
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.continue_sync(io);