From 5e9c8db4c9930c516bcb9dbdf00bc5e03246c74e Mon Sep 17 00:00:00 2001 From: arkpar Date: Mon, 20 Jun 2016 11:07:22 +0200 Subject: [PATCH] Deactivate peers on invalid block bodies --- sync/src/blocks.rs | 21 ++++++++++++++++----- sync/src/chain.rs | 5 ++++- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/sync/src/blocks.rs b/sync/src/blocks.rs index c1c6becde..17a97fe70 100644 --- a/sync/src/blocks.rs +++ b/sync/src/blocks.rs @@ -95,12 +95,17 @@ impl BlockCollection { } /// Insert a collection of block bodies for previously downloaded headers. - pub fn insert_bodies(&mut self, bodies: Vec) { + pub fn insert_bodies(&mut self, bodies: Vec) -> 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 { diff --git a/sync/src/chain.rs b/sync/src/chain.rs index caed95e0c..5b46978a2 100644 --- a/sync/src/chain.rs +++ b/sync/src/chain.rs @@ -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);