calculating peer highest number on fly
This commit is contained in:
parent
b606df451e
commit
49e61b87a0
@ -1084,13 +1084,16 @@ impl ChainSync {
|
||||
let chain_info = chain.chain_info();
|
||||
let latest_hash = chain_info.best_block_hash;
|
||||
let latest_number = chain_info.best_block_number;
|
||||
self.peers.iter().filter(|&(peer_id, peer_info)|
|
||||
self.peers.iter().filter(|&(_, peer_info)|
|
||||
match io.chain().block_status(&peer_info.latest)
|
||||
{
|
||||
BlockStatus::InChain => peer_info.latest != latest_hash && latest_number - peer_info.latest_number < MAX_PEER_LAG_PROPAGATION,
|
||||
BlockStatus::InChain => {
|
||||
let peer_number = HeaderView::new(&io.chain().block_header(&peer_info.latest).unwrap()).number();
|
||||
peer_info.latest != latest_hash && latest_number > peer_number && latest_number - peer_number < MAX_PEER_LAG_PROPAGATION
|
||||
},
|
||||
_ => false
|
||||
})
|
||||
.map(|(peer_id, peer_info)| peer_id)
|
||||
.map(|(peer_id, _)| peer_id)
|
||||
.cloned().collect::<Vec<usize>>()
|
||||
}
|
||||
|
||||
@ -1100,7 +1103,7 @@ impl ChainSync {
|
||||
|
||||
// sqrt(x)/x scaled to max u32
|
||||
let fraction = (self.peers.len() as f64).powf(-0.5).mul(u32::max_value() as f64).round() as u32;
|
||||
let mut lucky_peers = match lagging_peers.len() {
|
||||
let lucky_peers = match lagging_peers.len() {
|
||||
0 ... MIN_PEERS_PROPAGATION => lagging_peers,
|
||||
_ => lagging_peers.iter().filter(|_| ::rand::random::<u32>() < fraction).cloned().collect::<Vec<usize>>()
|
||||
};
|
||||
@ -1131,16 +1134,6 @@ impl ChainSync {
|
||||
trace!(target: "sync", "Sent new blocks to peers: {:?}", blocks_propagaded);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn get_peer_latet(&self, peer_id: usize) -> H256 {
|
||||
self.peers[&peer_id].latest.clone()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn get_peer_latest_number(&self, peer_id: usize) -> BlockNumber {
|
||||
self.peers[&peer_id].latest_number
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -136,8 +136,6 @@ fn propagade() {
|
||||
assert_eq!(1000, net.peer(1).chain.chain_info().best_block_number);
|
||||
assert_eq!(1000, net.peer(2).chain.chain_info().best_block_number);
|
||||
|
||||
assert_eq!(net.peer(0).sync.get_peer_latest_number(1), 1000);
|
||||
|
||||
net.sync_step_peer(0);
|
||||
|
||||
// 2 peers to sync
|
||||
|
@ -84,7 +84,6 @@ impl BlockChainClient for TestBlockChainClient {
|
||||
|
||||
fn block_header(&self, h: &H256) -> Option<Bytes> {
|
||||
self.blocks.read().unwrap().get(h).map(|r| Rlp::new(r).at(0).as_raw().to_vec())
|
||||
|
||||
}
|
||||
|
||||
fn block_body(&self, h: &H256) -> Option<Bytes> {
|
||||
|
Loading…
Reference in New Issue
Block a user