|
|
|
|
@@ -89,6 +89,8 @@
|
|
|
|
|
/// All other messages are ignored.
|
|
|
|
|
///
|
|
|
|
|
|
|
|
|
|
use std::collections::{HashSet, HashMap};
|
|
|
|
|
use std::cmp;
|
|
|
|
|
use util::*;
|
|
|
|
|
use rlp::*;
|
|
|
|
|
use network::*;
|
|
|
|
|
@@ -421,7 +423,7 @@ impl ChainSync {
|
|
|
|
|
start_block_number: self.starting_block,
|
|
|
|
|
last_imported_block_number: Some(last_imported_number),
|
|
|
|
|
last_imported_old_block_number: self.old_blocks.as_ref().map(|d| d.last_imported_block_number()),
|
|
|
|
|
highest_block_number: self.highest_block.map(|n| max(n, last_imported_number)),
|
|
|
|
|
highest_block_number: self.highest_block.map(|n| cmp::max(n, last_imported_number)),
|
|
|
|
|
blocks_received: if last_imported_number > self.starting_block { last_imported_number - self.starting_block } else { 0 },
|
|
|
|
|
blocks_total: match self.highest_block { Some(x) if x > self.starting_block => x - self.starting_block, _ => 0 },
|
|
|
|
|
num_peers: self.peers.values().filter(|p| p.is_allowed()).count(),
|
|
|
|
|
@@ -961,7 +963,7 @@ impl ChainSync {
|
|
|
|
|
}
|
|
|
|
|
if self.state != SyncState::Idle {
|
|
|
|
|
trace!(target: "sync", "Ignoring new hashes since we're already downloading.");
|
|
|
|
|
let max = r.iter().take(MAX_NEW_HASHES).map(|item| item.val_at::<BlockNumber>(1).unwrap_or(0)).fold(0u64, max);
|
|
|
|
|
let max = r.iter().take(MAX_NEW_HASHES).map(|item| item.val_at::<BlockNumber>(1).unwrap_or(0)).fold(0u64, cmp::max);
|
|
|
|
|
if max > self.highest_block.unwrap_or(0) {
|
|
|
|
|
self.highest_block = Some(max);
|
|
|
|
|
}
|
|
|
|
|
@@ -1473,7 +1475,7 @@ impl ChainSync {
|
|
|
|
|
|
|
|
|
|
let mut item_count = r.item_count()?;
|
|
|
|
|
trace!(target: "sync", "{:02} -> Transactions ({} entries)", peer_id, item_count);
|
|
|
|
|
item_count = min(item_count, MAX_TX_TO_IMPORT);
|
|
|
|
|
item_count = cmp::min(item_count, MAX_TX_TO_IMPORT);
|
|
|
|
|
let mut transactions = Vec::with_capacity(item_count);
|
|
|
|
|
for i in 0 .. item_count {
|
|
|
|
|
let rlp = r.at(i)?;
|
|
|
|
|
@@ -1549,11 +1551,11 @@ impl ChainSync {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let mut number = if reverse {
|
|
|
|
|
min(last, number)
|
|
|
|
|
cmp::min(last, number)
|
|
|
|
|
} else {
|
|
|
|
|
max(0, number)
|
|
|
|
|
cmp::max(0, number)
|
|
|
|
|
};
|
|
|
|
|
let max_count = min(MAX_HEADERS_TO_SEND, max_headers);
|
|
|
|
|
let max_count = cmp::min(MAX_HEADERS_TO_SEND, max_headers);
|
|
|
|
|
let mut count = 0;
|
|
|
|
|
let mut data = Bytes::new();
|
|
|
|
|
let inc = (skip + 1) as BlockNumber;
|
|
|
|
|
@@ -1594,7 +1596,7 @@ impl ChainSync {
|
|
|
|
|
debug!(target: "sync", "Empty GetBlockBodies request, ignoring.");
|
|
|
|
|
return Ok(None);
|
|
|
|
|
}
|
|
|
|
|
count = min(count, MAX_BODIES_TO_SEND);
|
|
|
|
|
count = cmp::min(count, MAX_BODIES_TO_SEND);
|
|
|
|
|
let mut added = 0usize;
|
|
|
|
|
let mut data = Bytes::new();
|
|
|
|
|
for i in 0..count {
|
|
|
|
|
@@ -1617,7 +1619,7 @@ impl ChainSync {
|
|
|
|
|
debug!(target: "sync", "Empty GetNodeData request, ignoring.");
|
|
|
|
|
return Ok(None);
|
|
|
|
|
}
|
|
|
|
|
count = min(count, MAX_NODE_DATA_TO_SEND);
|
|
|
|
|
count = cmp::min(count, MAX_NODE_DATA_TO_SEND);
|
|
|
|
|
let mut added = 0usize;
|
|
|
|
|
let mut data = Vec::new();
|
|
|
|
|
for i in 0..count {
|
|
|
|
|
@@ -1641,7 +1643,7 @@ impl ChainSync {
|
|
|
|
|
debug!(target: "sync", "Empty GetReceipts request, ignoring.");
|
|
|
|
|
return Ok(None);
|
|
|
|
|
}
|
|
|
|
|
count = min(count, MAX_RECEIPTS_HEADERS_TO_SEND);
|
|
|
|
|
count = cmp::min(count, MAX_RECEIPTS_HEADERS_TO_SEND);
|
|
|
|
|
let mut added_headers = 0usize;
|
|
|
|
|
let mut added_receipts = 0usize;
|
|
|
|
|
let mut data = Bytes::new();
|
|
|
|
|
@@ -1915,8 +1917,8 @@ impl ChainSync {
|
|
|
|
|
// take sqrt(x) peers
|
|
|
|
|
let mut peers = peers.to_vec();
|
|
|
|
|
let mut count = (peers.len() as f64).powf(0.5).round() as usize;
|
|
|
|
|
count = min(count, MAX_PEERS_PROPAGATION);
|
|
|
|
|
count = max(count, MIN_PEERS_PROPAGATION);
|
|
|
|
|
count = cmp::min(count, MAX_PEERS_PROPAGATION);
|
|
|
|
|
count = cmp::max(count, MIN_PEERS_PROPAGATION);
|
|
|
|
|
random::new().shuffle(&mut peers);
|
|
|
|
|
peers.truncate(count);
|
|
|
|
|
peers
|
|
|
|
|
@@ -2006,7 +2008,7 @@ impl ChainSync {
|
|
|
|
|
fn select_peers_for_transactions<F>(&self, filter: F) -> Vec<PeerId>
|
|
|
|
|
where F: Fn(&PeerId) -> bool {
|
|
|
|
|
// 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 fraction = ((self.peers.len() as f64).powf(-0.5) * (u32::max_value() as f64).round()) as u32;
|
|
|
|
|
let small = self.peers.len() < MIN_PEERS_PROPAGATION;
|
|
|
|
|
|
|
|
|
|
let mut random = random::new();
|
|
|
|
|
@@ -2112,7 +2114,7 @@ impl ChainSync {
|
|
|
|
|
peers.insert(peer_id);
|
|
|
|
|
self.send_packet(io, peer_id, TRANSACTIONS_PACKET, rlp);
|
|
|
|
|
trace!(target: "sync", "{:02} <- Transactions ({} entries)", peer_id, sent);
|
|
|
|
|
max_sent = max(max_sent, sent);
|
|
|
|
|
max_sent = cmp::max(max_sent, sent);
|
|
|
|
|
}
|
|
|
|
|
debug!(target: "sync", "Sent up to {} transactions to {} peers.", max_sent, lucky_peers_len);
|
|
|
|
|
}
|
|
|
|
|
|