Clean up sc-network (#9761)

* Clean up sc-network

- Avoid using clone() for the Copy type `PeerId`.
- Use `find_map` for `filter_map` and `next`.
- Use `Self`.

* More on Copy types

* Cargo +nightly fmt --all

* More ..

* fmt

* Revert vec![default_notif_handshake_message]
This commit is contained in:
Liu-Cheng Xu
2021-09-14 02:11:29 +08:00
committed by GitHub
parent 0472a43855
commit 2562f8c65e
37 changed files with 475 additions and 526 deletions
+5 -5
View File
@@ -426,7 +426,7 @@ impl Peerset {
// We want reputations to be up-to-date before adjusting them.
self.update_time();
let mut reputation = self.data.peer_reputation(peer_id.clone());
let mut reputation = self.data.peer_reputation(peer_id);
reputation.add_reputation(change.value);
if reputation.reputation() >= BANNED_THRESHOLD {
trace!(target: "peerset", "Report {}: {:+} to {}. Reason: {}",
@@ -486,7 +486,7 @@ impl Peerset {
reput.saturating_sub(diff)
}
let mut peer_reputation = self.data.peer_reputation(peer_id.clone());
let mut peer_reputation = self.data.peer_reputation(peer_id);
let before = peer_reputation.reputation();
let after = reput_tick(before);
@@ -920,7 +920,7 @@ mod tests {
assert_eq!(Stream::poll_next(Pin::new(&mut peerset), cx), Poll::Pending);
// Check that an incoming connection from that node gets refused.
peerset.incoming(SetId::from(0), peer_id.clone(), IncomingIndex(1));
peerset.incoming(SetId::from(0), peer_id, IncomingIndex(1));
if let Poll::Ready(msg) = Stream::poll_next(Pin::new(&mut peerset), cx) {
assert_eq!(msg.unwrap(), Message::Reject(IncomingIndex(1)));
} else {
@@ -931,7 +931,7 @@ mod tests {
thread::sleep(Duration::from_millis(1500));
// Try again. This time the node should be accepted.
peerset.incoming(SetId::from(0), peer_id.clone(), IncomingIndex(2));
peerset.incoming(SetId::from(0), peer_id, IncomingIndex(2));
while let Poll::Ready(msg) = Stream::poll_next(Pin::new(&mut peerset), cx) {
assert_eq!(msg.unwrap(), Message::Accept(IncomingIndex(2)));
}
@@ -965,7 +965,7 @@ mod tests {
// Check that an incoming connection from that node gets refused.
// This is already tested in other tests, but it is done again here because it doesn't
// hurt.
peerset.incoming(SetId::from(0), peer_id.clone(), IncomingIndex(1));
peerset.incoming(SetId::from(0), peer_id, IncomingIndex(1));
if let Poll::Ready(msg) = Stream::poll_next(Pin::new(&mut peerset), cx) {
assert_eq!(msg.unwrap(), Message::Reject(IncomingIndex(1)));
} else {
+3 -3
View File
@@ -167,7 +167,7 @@ impl PeersState {
/// Returns an object that grants access to the reputation value of a peer.
pub fn peer_reputation(&mut self, peer_id: PeerId) -> Reputation {
if !self.nodes.contains_key(&peer_id) {
self.nodes.insert(peer_id.clone(), Node::new(self.sets.len()));
self.nodes.insert(peer_id, Node::new(self.sets.len()));
}
let entry = match self.nodes.entry(peer_id) {
@@ -256,7 +256,7 @@ impl PeersState {
}
Some(to_try)
})
.map(|(peer_id, _)| peer_id.clone());
.map(|(peer_id, _)| *peer_id);
outcome.map(move |peer_id| NotConnectedPeer {
state: self,
@@ -275,7 +275,7 @@ impl PeersState {
/// Has no effect if the node was already in the group.
pub fn add_no_slot_node(&mut self, set: usize, peer_id: PeerId) {
// Reminder: `HashSet::insert` returns false if the node was already in the set
if !self.sets[set].no_slot_nodes.insert(peer_id.clone()) {
if !self.sets[set].no_slot_nodes.insert(peer_id) {
return
}