Fix lots of small nits in sc-network (#6028)

* Fix lots of small nits in sc-network

* Update client/network/src/protocol/sync/blocks.rs

Co-authored-by: Arkadiy Paronyan <arkady.paronyan@gmail.com>

* Fix warning

* Yes. The line width.

Co-authored-by: Arkadiy Paronyan <arkady.paronyan@gmail.com>
This commit is contained in:
Pierre Krieger
2020-05-15 13:58:52 +02:00
committed by GitHub
parent 1dbd761192
commit 6ecdf20a1f
14 changed files with 111 additions and 112 deletions
+39 -35
View File
@@ -574,7 +574,7 @@ impl<B: BlockT> ChainSync<B> {
if number > peer.best_number {
peer.best_number = number;
peer.best_hash = hash.clone();
peer.best_hash = *hash;
}
self.pending_requests.add(peer_id);
}
@@ -639,7 +639,7 @@ impl<B: BlockT> ChainSync<B> {
}
/// Get an iterator over all block requests of all peers.
pub fn block_requests(&mut self) -> impl Iterator<Item = (PeerId, BlockRequest<B>)> + '_ {
pub fn block_requests(&mut self) -> impl Iterator<Item = (&PeerId, BlockRequest<B>)> + '_ {
if self.pending_requests.is_empty() {
return Either::Left(std::iter::empty())
}
@@ -682,7 +682,7 @@ impl<B: BlockT> ChainSync<B> {
req,
);
have_requests = true;
Some((id.clone(), req))
Some((id, req))
} else if let Some((hash, req)) = fork_sync_request(
id,
fork_targets,
@@ -698,7 +698,7 @@ impl<B: BlockT> ChainSync<B> {
trace!(target: "sync", "Downloading fork {:?} from {}", hash, id);
peer.state = PeerSyncState::DownloadingStale(hash);
have_requests = true;
Some((id.clone(), req))
Some((id, req))
} else {
None
}
@@ -713,24 +713,27 @@ impl<B: BlockT> ChainSync<B> {
///
/// If this corresponds to a valid block, this outputs the block that
/// must be imported in the import queue.
pub fn on_block_data
(&mut self, who: PeerId, request: Option<BlockRequest<B>>, response: BlockResponse<B>) -> Result<OnBlockData<B>, BadPeer>
{
pub fn on_block_data(
&mut self,
who: &PeerId,
request: Option<BlockRequest<B>>,
response: BlockResponse<B>
) -> Result<OnBlockData<B>, BadPeer> {
let mut new_blocks: Vec<IncomingBlock<B>> =
if let Some(peer) = self.peers.get_mut(&who) {
if let Some(peer) = self.peers.get_mut(who) {
let mut blocks = response.blocks;
if request.as_ref().map_or(false, |r| r.direction == message::Direction::Descending) {
trace!(target: "sync", "Reversing incoming block list");
blocks.reverse()
}
self.pending_requests.add(&who);
self.pending_requests.add(who);
if request.is_some() {
match &mut peer.state {
PeerSyncState::DownloadingNew(start_block) => {
self.blocks.clear_peer_download(&who);
self.blocks.clear_peer_download(who);
let start_block = *start_block;
peer.state = PeerSyncState::Available;
validate_blocks::<B>(&blocks, &who)?;
validate_blocks::<B>(&blocks, who)?;
self.blocks.insert(start_block, blocks, who.clone());
self.blocks
.drain(self.best_queued_number + One::one())
@@ -751,9 +754,9 @@ impl<B: BlockT> ChainSync<B> {
peer.state = PeerSyncState::Available;
if blocks.is_empty() {
debug!(target: "sync", "Empty block response from {}", who);
return Err(BadPeer(who, rep::NO_BLOCK));
return Err(BadPeer(who.clone(), rep::NO_BLOCK));
}
validate_blocks::<B>(&blocks, &who)?;
validate_blocks::<B>(&blocks, who)?;
blocks.into_iter().map(|b| {
IncomingBlock {
hash: b.hash,
@@ -774,11 +777,11 @@ impl<B: BlockT> ChainSync<B> {
},
(None, _) => {
debug!(target: "sync", "Invalid response when searching for ancestor from {}", who);
return Err(BadPeer(who, rep::UNKNOWN_ANCESTOR))
return Err(BadPeer(who.clone(), rep::UNKNOWN_ANCESTOR))
},
(_, Err(e)) => {
info!("❌ Error answering legitimate blockchain query: {:?}", e);
return Err(BadPeer(who, rep::BLOCKCHAIN_READ_ERROR))
return Err(BadPeer(who.clone(), rep::BLOCKCHAIN_READ_ERROR))
}
};
if matching_hash.is_some() {
@@ -794,7 +797,7 @@ impl<B: BlockT> ChainSync<B> {
}
if matching_hash.is_none() && current.is_zero() {
trace!(target:"sync", "Ancestry search: genesis mismatch for peer {}", who);
return Err(BadPeer(who, rep::GENESIS_MISMATCH))
return Err(BadPeer(who.clone(), rep::GENESIS_MISMATCH))
}
if let Some((next_state, next_num)) = handle_ancestor_search_state(state, *current, matching_hash.is_some()) {
peer.state = PeerSyncState::AncestorSearch {
@@ -802,7 +805,7 @@ impl<B: BlockT> ChainSync<B> {
start: *start,
state: next_state,
};
return Ok(OnBlockData::Request(who, ancestry_request::<B>(next_num)))
return Ok(OnBlockData::Request(who.clone(), ancestry_request::<B>(next_num)))
} else {
// Ancestry search is complete. Check if peer is on a stale fork unknown to us and
// add it to sync targets if necessary.
@@ -838,7 +841,7 @@ impl<B: BlockT> ChainSync<B> {
}
} else {
// When request.is_none() this is a block announcement. Just accept blocks.
validate_blocks::<B>(&blocks, &who)?;
validate_blocks::<B>(&blocks, who)?;
blocks.into_iter().map(|b| {
IncomingBlock {
hash: b.hash,
@@ -869,7 +872,7 @@ impl<B: BlockT> ChainSync<B> {
// So the only way this can happen is when peers lie about the
// common block.
debug!(target: "sync", "Ignoring known blocks from {}", who);
return Err(BadPeer(who, rep::KNOWN_BLOCK));
return Err(BadPeer(who.clone(), rep::KNOWN_BLOCK));
}
let orig_len = new_blocks.len();
new_blocks.retain(|b| !self.queue_blocks.contains(&b.hash));
@@ -1124,7 +1127,7 @@ impl<B: BlockT> ChainSync<B> {
/// Updates our internal state for best queued block and then goes
/// through all peers to update our view of their state as well.
fn on_block_queued(&mut self, hash: &B::Hash, number: NumberFor<B>) {
if let Some(_) = self.fork_targets.remove(&hash) {
if self.fork_targets.remove(&hash).is_some() {
trace!(target: "sync", "Completed fork sync {:?}", hash);
}
if number > self.best_queued_number {
@@ -1162,7 +1165,7 @@ impl<B: BlockT> ChainSync<B> {
/// header (call `on_block_data`). The network request isn't sent
/// in this case. Both hash and header is passed as an optimization
/// to avoid rehashing the header.
pub fn on_block_announce(&mut self, who: PeerId, hash: B::Hash, announce: &BlockAnnounce<B::Header>, is_best: bool)
pub fn on_block_announce(&mut self, who: &PeerId, hash: B::Hash, announce: &BlockAnnounce<B::Header>, is_best: bool)
-> OnBlockAnnounce
{
let header = &announce.header;
@@ -1177,7 +1180,7 @@ impl<B: BlockT> ChainSync<B> {
let ancient_parent = parent_status == BlockStatus::InChainPruned;
let known = self.is_known(&hash);
let peer = if let Some(peer) = self.peers.get_mut(&who) {
let peer = if let Some(peer) = self.peers.get_mut(who) {
peer
} else {
error!(target: "sync", "💔 Called on_block_announce with a bad peer ID");
@@ -1206,13 +1209,13 @@ impl<B: BlockT> ChainSync<B> {
peer.common_number = number - One::one();
}
}
self.pending_requests.add(&who);
self.pending_requests.add(who);
// known block case
if known || self.is_already_downloading(&hash) {
trace!(target: "sync", "Known block announce from {}: {}", who, hash);
if let Some(target) = self.fork_targets.get_mut(&hash) {
target.peers.insert(who);
target.peers.insert(who.clone());
}
return OnBlockAnnounce::Nothing
}
@@ -1251,21 +1254,21 @@ impl<B: BlockT> ChainSync<B> {
.entry(hash.clone())
.or_insert_with(|| ForkTarget {
number,
parent_hash: Some(header.parent_hash().clone()),
parent_hash: Some(*header.parent_hash()),
peers: Default::default(),
})
.peers.insert(who);
.peers.insert(who.clone());
}
OnBlockAnnounce::Nothing
}
/// Call when a peer has disconnected.
pub fn peer_disconnected(&mut self, who: PeerId) {
self.blocks.clear_peer_download(&who);
self.peers.remove(&who);
self.extra_justifications.peer_disconnected(&who);
self.extra_finality_proofs.peer_disconnected(&who);
pub fn peer_disconnected(&mut self, who: &PeerId) {
self.blocks.clear_peer_download(who);
self.peers.remove(who);
self.extra_justifications.peer_disconnected(who);
self.extra_finality_proofs.peer_disconnected(who);
self.pending_requests.set_all();
}
@@ -1471,11 +1474,12 @@ fn fork_sync_request<B: BlockT>(
}
if r.number <= best_num {
let parent_status = r.parent_hash.as_ref().map_or(BlockStatus::Unknown, check_block);
let mut count = (r.number - finalized).saturated_into::<u32>(); // up to the last finalized block
if parent_status != BlockStatus::Unknown {
let count = if parent_status == BlockStatus::Unknown {
(r.number - finalized).saturated_into::<u32>() // up to the last finalized block
} else {
// request only single block
count = 1;
}
1
};
trace!(target: "sync", "Downloading requested fork {:?} from {}, {} blocks", hash, id, count);
return Some((hash.clone(), message::generic::BlockRequest {
id: 0,