Style + comments

This commit is contained in:
arkpar 2016-06-14 12:18:20 +02:00
parent 5c019b74e4
commit 9ed2122ae2

View File

@ -414,6 +414,7 @@ impl ChainSync {
for i in 0..item_count { for i in 0..item_count {
let info: BlockHeader = try!(r.val_at(i)); let info: BlockHeader = try!(r.val_at(i));
let number = BlockNumber::from(info.number); let number = BlockNumber::from(info.number);
// Check if any of the headers matches the hash we requested
if !valid_response { if !valid_response {
if let Some(expected) = expected_hash { if let Some(expected) = expected_hash {
valid_response = expected == info.hash() valid_response = expected == info.hash()
@ -449,29 +450,32 @@ impl ChainSync {
} }
} }
// Disable the peer for this syncing round if it gives invalid chain
if !valid_response { if !valid_response {
trace!(target: "sync", "{} Disabled for invalid headers response", peer_id); trace!(target: "sync", "{} Disabled for invalid headers response", peer_id);
self.deactivate_peer(io, peer_id); // disable the peer for this syncing round if it gives invalid chain self.deactivate_peer(io, peer_id);
} }
match self.state { else {
SyncState::ChainHead => { match self.state {
if headers.is_empty() { SyncState::ChainHead => {
// peer is not on our chain if headers.is_empty() {
// track back and try again // peer is not on our chain
self.imported_this_round = Some(0); // track back and try again
self.start_sync_round(io); self.imported_this_round = Some(0);
} else { self.start_sync_round(io);
// TODO: validate heads better. E.g. check that there is enough distance between blocks. } else {
trace!(target: "sync", "Received {} subchain heads, proceeding to download", headers.len()); // TODO: validate heads better. E.g. check that there is enough distance between blocks.
self.blocks.reset_to(hashes); trace!(target: "sync", "Received {} subchain heads, proceeding to download", headers.len());
self.state = SyncState::Blocks; self.blocks.reset_to(hashes);
} self.state = SyncState::Blocks;
}, }
SyncState::Blocks | SyncState::NewBlocks | SyncState::Waiting => { },
trace!(target: "sync", "Inserted {} headers", headers.len()); SyncState::Blocks | SyncState::NewBlocks | SyncState::Waiting => {
self.blocks.insert_headers(headers); trace!(target: "sync", "Inserted {} headers", headers.len());
}, self.blocks.insert_headers(headers);
_ => trace!(target: "sync", "Unexpected headers({}) from {} ({}), state = {:?}", headers.len(), peer_id, io.peer_info(peer_id), self.state) },
_ => trace!(target: "sync", "Unexpected headers({}) from {} ({}), state = {:?}", headers.len(), peer_id, io.peer_info(peer_id), self.state)
}
} }
self.collect_blocks(io); self.collect_blocks(io);
@ -843,9 +847,9 @@ impl ChainSync {
rlp.append(&skip); rlp.append(&skip);
rlp.append(&if reverse {1u32} else {0u32}); rlp.append(&if reverse {1u32} else {0u32});
self.send_request(sync, peer_id, asking, GET_BLOCK_HEADERS_PACKET, rlp.out()); self.send_request(sync, peer_id, asking, GET_BLOCK_HEADERS_PACKET, rlp.out());
if let Some(ref mut peer) = self.peers.get_mut(&peer_id) { self.peers.get_mut(&peer_id)
peer.asking_hash = Some(h.clone()); .expect("peer_id may originate either from on_packet, where it is already validated or from enumerating self.peers. qed")
} .asking_hash = Some(h.clone());
} }
/// Request block bodies from a peer /// Request block bodies from a peer
@ -940,10 +944,11 @@ impl ChainSync {
match io.chain().block_header(BlockID::Hash(hash)) { match io.chain().block_header(BlockID::Hash(hash)) {
Some(hdr) => { Some(hdr) => {
let number = From::from(HeaderView::new(&hdr).number()); let number = From::from(HeaderView::new(&hdr).number());
assert_eq!(HeaderView::new(&hdr).sha3(), hash); debug_assert_eq!(HeaderView::new(&hdr).sha3(), hash);
if max_headers == 1 || io.chain().block_hash(BlockID::Number(number)) != Some(hash) { if max_headers == 1 || io.chain().block_hash(BlockID::Number(number)) != Some(hash) {
// Non canonical header or single header requested // Non canonical header or single header requested
trace!("Returning single header: {:?}", hash); // TODO: handle single-step reverse hashchains of non-canon hashes
trace!(target:"sync", "Returning single header: {:?}", hash);
let mut rlp = RlpStream::new_list(1); let mut rlp = RlpStream::new_list(1);
rlp.append_raw(&hdr, 1); rlp.append_raw(&hdr, 1);
return Ok(Some((BLOCK_HEADERS_PACKET, rlp))); return Ok(Some((BLOCK_HEADERS_PACKET, rlp)));
@ -1145,7 +1150,8 @@ impl ChainSync {
let mut rlp_stream = RlpStream::new_list(blocks.len()); let mut rlp_stream = RlpStream::new_list(blocks.len());
for block_hash in blocks { for block_hash in blocks {
let mut hash_rlp = RlpStream::new_list(2); let mut hash_rlp = RlpStream::new_list(2);
let number = HeaderView::new(&chain.block_header(BlockID::Hash(block_hash.clone())).expect("Malformed block without a header on the chain!")).number(); let number = HeaderView::new(&chain.block_header(BlockID::Hash(block_hash.clone()))
.expect("chain.tree_route and chain.find_uncles only return hahses of blocks that are in the blockchain. qed.")).number();
hash_rlp.append(&block_hash); hash_rlp.append(&block_hash);
hash_rlp.append(&number); hash_rlp.append(&number);
rlp_stream.append_raw(&hash_rlp.as_raw(), 1); rlp_stream.append_raw(&hash_rlp.as_raw(), 1);