Merge pull request #1345 from ethcore/sync-bodies
Sync attack defense: Deactivate peers on invalid block bodies
This commit is contained in:
		
						commit
						f5682737d3
					
				| @ -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> { | ||||||
|  | |||||||
| @ -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); | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user