This commit is contained in:
arkpar 2016-01-08 16:26:00 +01:00
parent 290d738e3f
commit 9ea7f14542
3 changed files with 27 additions and 23 deletions

View File

@ -81,15 +81,14 @@ pub struct SyncStatus {
pub state: SyncState,
pub protocol_version: u8,
pub start_block_number: BlockNumber,
pub current_block_number: BlockNumber,
pub last_imported_block_number: BlockNumber,
pub highest_block_number: BlockNumber,
pub blocks_total: usize,
pub blocks_received: usize
pub blocks_received: usize,
}
#[derive(PartialEq, Eq, Debug)]
enum PeerAsking
{
enum PeerAsking {
Nothing,
BlockHeaders,
BlockBodies,
@ -102,7 +101,7 @@ struct PeerInfo {
latest: H256,
difficulty: U256,
asking: PeerAsking,
asking_blocks: Vec<BlockNumber>
asking_blocks: Vec<BlockNumber>,
}
type Body = Bytes;
@ -119,9 +118,9 @@ pub struct ChainSync {
/// Set of block body numbers being downloaded
downloading_bodies: HashSet<BlockNumber>,
/// Downloaded headers.
headers: Vec<(BlockNumber, Vec<Header>)>, //TODO: use BTreeMap once range API is sable. For now it a vector sorted in descending order
headers: Vec<(BlockNumber, Vec<Header>)>, //TODO: use BTreeMap once range API is sable. For now it is a vector sorted in descending order
/// Downloaded bodies
bodies: Vec<(BlockNumber, Vec<Body>)>, //TODO: use BTreeMap once range API is sable. For now it a vector sorted in descending order
bodies: Vec<(BlockNumber, Vec<Body>)>, //TODO: use BTreeMap once range API is sable. For now it is a vector sorted in descending order
/// Peer info
peers: HashMap<PeerId, PeerInfo>,
/// Used to map body to header
@ -152,7 +151,7 @@ impl ChainSync {
last_imported_block: 0,
last_imported_hash: H256::new(),
syncing_difficulty: U256::from(0u64),
have_common_block: false
have_common_block: false,
}
}
@ -162,10 +161,10 @@ impl ChainSync {
state: self.state.clone(),
protocol_version: 63,
start_block_number: self.starting_block,
current_block_number: 0, //TODO:
last_imported_block_number: self.last_imported_block,
highest_block_number: self.highest_block,
blocks_total: (self.last_imported_block - self.starting_block) as usize,
blocks_received: (self.highest_block - self.starting_block) as usize
blocks_received: (self.highest_block - self.starting_block) as usize,
}
}
@ -732,7 +731,7 @@ impl ChainSync {
warn!(target:"sync", "Error sending status request: {:?}", e);
io.disable_peer(peer_id);
}
Ok(_) => { }
Ok(_) => ()
}
}
@ -765,6 +764,7 @@ impl ChainSync {
let max_count = min(MAX_HEADERS_TO_SEND, max_headers);
let mut count = 0;
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) => {
@ -774,10 +774,13 @@ impl ChainSync {
None => {}
}
if reverse {
number -= (skip + 1) as BlockNumber;
if number <= inc {
break;
}
number -= inc;
}
else {
number += (skip + 1) as BlockNumber;
number += inc;
}
}
let mut rlp = RlpStream::new_list(count as usize);

View File

@ -205,6 +205,7 @@ fn test_range() {
assert_eq!(ranges.find_item(&18), Some(&'r'));
assert!(ranges.have_item(&16));
assert_eq!(ranges.get_tail(&17), 17..19);
assert_eq!(ranges.get_tail(&16), 16..19);
ranges.insert_item(2, 'b');
assert_eq!(ranges.range_iter().cmp(vec![(2, &['b'][..]), (16, &['p', 'q', 'r'][..])]), Ordering::Equal);