Merge branch 'master' of github.com:ethcore/parity into io

This commit is contained in:
arkpar
2016-01-22 14:44:17 +01:00
58 changed files with 395 additions and 434 deletions

View File

@@ -218,7 +218,7 @@ impl ChainSync {
self.downloading_bodies.clear();
self.headers.clear();
self.bodies.clear();
for (_, ref mut p) in self.peers.iter_mut() {
for (_, ref mut p) in &mut self.peers {
p.asking_blocks.clear();
}
self.header_ids.clear();
@@ -274,6 +274,7 @@ impl ChainSync {
Ok(())
}
#[allow(cyclomatic_complexity)]
/// Called by peer once it has new block headers during sync
fn on_peer_block_headers(&mut self, io: &mut SyncIo, peer_id: PeerId, r: &UntrustedRlp) -> Result<(), PacketDecodeError> {
self.reset_peer_asking(peer_id, PeerAsking::BlockHeaders);
@@ -381,7 +382,7 @@ impl ChainSync {
transactions_root: tx_root,
uncles: uncles
};
match self.header_ids.get(&header_id).map(|n| *n) {
match self.header_ids.get(&header_id).cloned() {
Some(n) => {
self.header_ids.remove(&header_id);
self.bodies.insert_item(n, body.as_raw().to_vec());
@@ -709,16 +710,13 @@ impl ChainSync {
/// Used to recover from an error and re-download parts of the chain detected as bad.
fn remove_downloaded_blocks(&mut self, start: BlockNumber) {
for n in self.headers.get_tail(&start) {
match self.headers.find_item(&n) {
Some(ref header_data) => {
let header_to_delete = HeaderView::new(&header_data.data);
let header_id = HeaderId {
transactions_root: header_to_delete.transactions_root(),
uncles: header_to_delete.uncles_hash()
};
self.header_ids.remove(&header_id);
},
None => {}
if let Some(ref header_data) = self.headers.find_item(&n) {
let header_to_delete = HeaderView::new(&header_data.data);
let header_id = HeaderId {
transactions_root: header_to_delete.transactions_root(),
uncles: header_to_delete.uncles_hash()
};
self.header_ids.remove(&header_id);
}
self.downloading_bodies.remove(&n);
self.downloading_headers.remove(&n);
@@ -806,12 +804,9 @@ impl ChainSync {
packet.append(&chain.best_block_hash);
packet.append(&chain.genesis_hash);
//TODO: handle timeout for status request
match io.send(peer_id, STATUS_PACKET, packet.out()) {
Err(e) => {
warn!(target:"sync", "Error sending status request: {:?}", e);
io.disable_peer(peer_id);
}
Ok(_) => ()
if let Err(e) = io.send(peer_id, STATUS_PACKET, packet.out()) {
warn!(target:"sync", "Error sending status request: {:?}", e);
io.disable_peer(peer_id);
}
}
@@ -847,12 +842,9 @@ impl ChainSync {
let mut data = Bytes::new();
let inc = (skip + 1) as BlockNumber;
while number <= last && number > 0 && count < max_count {
match io.chain().block_header_at(number) {
Some(mut hdr) => {
data.append(&mut hdr);
count += 1;
}
None => {}
if let Some(mut hdr) = io.chain().block_header_at(number) {
data.append(&mut hdr);
count += 1;
}
if reverse {
if number <= inc {
@@ -884,12 +876,9 @@ impl ChainSync {
let mut added = 0usize;
let mut data = Bytes::new();
for i in 0..count {
match io.chain().block_body(&try!(r.val_at::<H256>(i))) {
Some(mut hdr) => {
data.append(&mut hdr);
added += 1;
}
None => {}
if let Some(mut hdr) = io.chain().block_body(&try!(r.val_at::<H256>(i))) {
data.append(&mut hdr);
added += 1;
}
}
let mut rlp = RlpStream::new_list(added);
@@ -911,12 +900,9 @@ impl ChainSync {
let mut added = 0usize;
let mut data = Bytes::new();
for i in 0..count {
match io.chain().state_data(&try!(r.val_at::<H256>(i))) {
Some(mut hdr) => {
data.append(&mut hdr);
added += 1;
}
None => {}
if let Some(mut hdr) = io.chain().state_data(&try!(r.val_at::<H256>(i))) {
data.append(&mut hdr);
added += 1;
}
}
let mut rlp = RlpStream::new_list(added);
@@ -937,12 +923,9 @@ impl ChainSync {
let mut added = 0usize;
let mut data = Bytes::new();
for i in 0..count {
match io.chain().block_receipts(&try!(r.val_at::<H256>(i))) {
Some(mut hdr) => {
data.append(&mut hdr);
added += 1;
}
None => {}
if let Some(mut hdr) = io.chain().block_receipts(&try!(r.val_at::<H256>(i))) {
data.append(&mut hdr);
added += 1;
}
}
let mut rlp = RlpStream::new_list(added);

View File

@@ -14,7 +14,7 @@ pub trait SyncIo {
/// Send a packet to a peer.
fn send(&mut self, peer_id: PeerId, packet_id: PacketId, data: Vec<u8>) -> Result<(), UtilError>;
/// Get the blockchain
fn chain<'s>(&'s self) -> &'s BlockChainClient;
fn chain(&self) -> &BlockChainClient;
/// Returns peer client identifier string
fn peer_info(&self, peer_id: PeerId) -> String {
peer_id.to_string()
@@ -50,7 +50,7 @@ impl<'s, 'h> SyncIo for NetSyncIo<'s, 'h> {
self.network.send(peer_id, packet_id, data)
}
fn chain<'a>(&'a self) -> &'a BlockChainClient {
fn chain(&self) -> &BlockChainClient {
self.chain
}

View File

@@ -29,7 +29,7 @@ pub trait RangeCollection<K, V> {
/// Remove all elements >= `tail`
fn insert_item(&mut self, key: K, value: V);
/// Get an iterator over ranges
fn range_iter<'c>(&'c self) -> RangeIterator<'c, K, V>;
fn range_iter(& self) -> RangeIterator<K, V>;
}
/// Range iterator. For each range yelds a key for the first element of the range and a vector of values.
@@ -60,7 +60,7 @@ impl<'c, K:'c, V:'c> Iterator for RangeIterator<'c, K, V> where K: Add<Output =
}
impl<K, V> RangeCollection<K, V> for Vec<(K, Vec<V>)> where K: Ord + PartialEq + Add<Output = K> + Sub<Output = K> + Copy + FromUsize + ToUsize {
fn range_iter<'c>(&'c self) -> RangeIterator<'c, K, V> {
fn range_iter(&self) -> RangeIterator<K, V> {
RangeIterator {
range: self.len(),
collection: self
@@ -191,6 +191,7 @@ impl<K, V> RangeCollection<K, V> for Vec<(K, Vec<V>)> where K: Ord + PartialEq +
}
#[test]
#[allow(cyclomatic_complexity)]
fn test_range() {
use std::cmp::{Ordering};

View File

@@ -66,7 +66,7 @@ impl BlockChainClient for TestBlockChainClient {
}
fn block(&self, h: &H256) -> Option<Bytes> {
self.blocks.read().unwrap().get(h).map(|b| b.clone())
self.blocks.read().unwrap().get(h).cloned()
}
fn block_status(&self, h: &H256) -> BlockStatus {
@@ -211,7 +211,7 @@ impl<'p> SyncIo for TestIo<'p> {
Ok(())
}
fn chain<'a>(&'a self) -> &'a BlockChainClient {
fn chain(&self) -> &BlockChainClient {
self.chain
}
}
@@ -268,14 +268,11 @@ impl TestNet {
pub fn sync_step(&mut self) {
for peer in 0..self.peers.len() {
match self.peers[peer].queue.pop_front() {
Some(packet) => {
let mut p = self.peers.get_mut(packet.recipient).unwrap();
trace!("--- {} -> {} ---", peer, packet.recipient);
p.sync.on_packet(&mut TestIo::new(&mut p.chain, &mut p.queue, Some(peer as PeerId)), peer as PeerId, packet.packet_id, &packet.data);
trace!("----------------");
},
None => {}
if let Some(packet) = self.peers[peer].queue.pop_front() {
let mut p = self.peers.get_mut(packet.recipient).unwrap();
trace!("--- {} -> {} ---", peer, packet.recipient);
p.sync.on_packet(&mut TestIo::new(&mut p.chain, &mut p.queue, Some(peer as PeerId)), peer as PeerId, packet.packet_id, &packet.data);
trace!("----------------");
}
let mut p = self.peers.get_mut(peer).unwrap();
p.sync._maintain_sync(&mut TestIo::new(&mut p.chain, &mut p.queue, None));