Skip to content

Commit 99f9f3a

Browse files
jolestarbkchrtomaka
authored andcommitted
Fix cargo clippy warning in peerset. (paritytech#7641)
* Fix cargo clippy warning in peerset. * Update client/peerset/src/lib.rs Co-authored-by: Bastian Köcher <[email protected]> * Apply suggestions from code review Co-authored-by: Pierre Krieger <[email protected]> Co-authored-by: Bastian Köcher <[email protected]> Co-authored-by: Pierre Krieger <[email protected]>
1 parent f9d06a9 commit 99f9f3a

File tree

5 files changed

+33
-35
lines changed

5 files changed

+33
-35
lines changed

client/peerset/src/lib.rs

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const BANNED_THRESHOLD: i32 = 82 * (i32::min_value() / 100);
3636
/// Reputation change for a node when we get disconnected from it.
3737
const DISCONNECT_REPUTATION_CHANGE: i32 = -256;
3838
/// Reserved peers group ID
39-
const RESERVED_NODES: &'static str = "reserved";
39+
const RESERVED_NODES: &str = "reserved";
4040
/// Amount of time between the moment we disconnect from a node and the moment we remove it from
4141
/// the list.
4242
const FORGET_AFTER: Duration = Duration::from_secs(3600);
@@ -87,7 +87,7 @@ impl PeersetHandle {
8787
/// Has no effect if the node was already a reserved peer.
8888
///
8989
/// > **Note**: Keep in mind that the networking has to know an address for this node,
90-
/// > otherwise it will not be able to connect to it.
90+
/// > otherwise it will not be able to connect to it.
9191
pub fn add_reserved_peer(&self, peer_id: PeerId) {
9292
let _ = self.tx.unbounded_send(Action::AddReservedPeer(peer_id));
9393
}
@@ -169,7 +169,7 @@ pub struct PeersetConfig {
169169
/// List of bootstrap nodes to initialize the peer with.
170170
///
171171
/// > **Note**: Keep in mind that the networking has to know an address for these nodes,
172-
/// > otherwise it will not be able to connect to them.
172+
/// > otherwise it will not be able to connect to them.
173173
pub bootnodes: Vec<PeerId>,
174174

175175
/// If true, we only accept nodes in [`PeersetConfig::priority_groups`].
@@ -178,7 +178,7 @@ pub struct PeersetConfig {
178178
/// Lists of nodes we should always be connected to.
179179
///
180180
/// > **Note**: Keep in mind that the networking has to know an address for these nodes,
181-
/// > otherwise it will not be able to connect to them.
181+
/// > otherwise it will not be able to connect to them.
182182
pub priority_groups: Vec<(String, HashSet<PeerId>)>,
183183
}
184184

@@ -430,10 +430,9 @@ impl Peerset {
430430
.get(RESERVED_NODES)
431431
.into_iter()
432432
.flatten()
433-
.filter(move |n| {
433+
.find(move |n| {
434434
data.peer(n).into_connected().is_none()
435435
})
436-
.next()
437436
.cloned()
438437
};
439438

@@ -469,10 +468,9 @@ impl Peerset {
469468
self.priority_groups
470469
.values()
471470
.flatten()
472-
.filter(move |n| {
471+
.find(move |n| {
473472
data.peer(n).into_connected().is_none()
474473
})
475-
.next()
476474
.cloned()
477475
};
478476

@@ -497,21 +495,17 @@ impl Peerset {
497495
}
498496

499497
// Now, we try to connect to non-priority nodes.
500-
loop {
501-
// Try to grab the next node to attempt to connect to.
502-
let next = match self.data.highest_not_connected_peer() {
503-
Some(p) => p,
504-
None => break, // No known node to add.
505-
};
506-
498+
while let Some(next) = self.data.highest_not_connected_peer() {
507499
// Don't connect to nodes with an abysmal reputation.
508500
if next.reputation() < BANNED_THRESHOLD {
509501
break;
510502
}
511503

512504
match next.try_outgoing() {
513-
Ok(conn) => self.message_queue.push_back(Message::Connect(conn.into_peer_id())),
514-
Err(_) => break, // No more slots available.
505+
Ok(conn) => self
506+
.message_queue
507+
.push_back(Message::Connect(conn.into_peer_id())),
508+
Err(_) => break, // No more slots available.
515509
}
516510
}
517511
}
@@ -530,11 +524,9 @@ impl Peerset {
530524
trace!(target: "peerset", "Incoming {:?}", peer_id);
531525
self.update_time();
532526

533-
if self.reserved_only {
534-
if !self.priority_groups.get(RESERVED_NODES).map_or(false, |n| n.contains(&peer_id)) {
535-
self.message_queue.push_back(Message::Reject(index));
536-
return;
537-
}
527+
if self.reserved_only && !self.priority_groups.get(RESERVED_NODES).map_or(false, |n| n.contains(&peer_id)) {
528+
self.message_queue.push_back(Message::Reject(index));
529+
return;
538530
}
539531

540532
let not_connected = match self.data.peer(&peer_id) {
@@ -584,7 +576,7 @@ impl Peerset {
584576
/// Adds discovered peer ids to the PSM.
585577
///
586578
/// > **Note**: There is no equivalent "expired" message, meaning that it is the responsibility
587-
/// > of the PSM to remove `PeerId`s that fail to dial too often.
579+
/// > of the PSM to remove `PeerId`s that fail to dial too often.
588580
pub fn discovered<I: IntoIterator<Item = PeerId>>(&mut self, peer_ids: I) {
589581
let mut discovered_any = false;
590582

@@ -747,12 +739,12 @@ mod tests {
747739

748740
let (mut peerset, _handle) = Peerset::from_config(config);
749741
peerset.incoming(incoming.clone(), ii);
750-
peerset.incoming(incoming.clone(), ii4);
751-
peerset.incoming(incoming2.clone(), ii2);
752-
peerset.incoming(incoming3.clone(), ii3);
742+
peerset.incoming(incoming, ii4);
743+
peerset.incoming(incoming2, ii2);
744+
peerset.incoming(incoming3, ii3);
753745

754746
assert_messages(peerset, vec![
755-
Message::Connect(bootnode.clone()),
747+
Message::Connect(bootnode),
756748
Message::Accept(ii),
757749
Message::Accept(ii2),
758750
Message::Reject(ii3),
@@ -772,7 +764,7 @@ mod tests {
772764
};
773765

774766
let (mut peerset, _) = Peerset::from_config(config);
775-
peerset.incoming(incoming.clone(), ii);
767+
peerset.incoming(incoming, ii);
776768

777769
assert_messages(peerset, vec![
778770
Message::Reject(ii),

client/peerset/src/peersstate.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ pub struct PeersState {
4242
/// List of nodes that we know about.
4343
///
4444
/// > **Note**: This list should really be ordered by decreasing reputation, so that we can
45-
/// easily select the best node to connect to. As a first draft, however, we don't
46-
/// sort, to make the logic easier.
45+
/// easily select the best node to connect to. As a first draft, however, we don't
46+
/// sort, to make the logic easier.
4747
nodes: HashMap<PeerId, Node>,
4848

4949
/// Number of slot-occupying nodes for which the `ConnectionState` is `In`.
@@ -130,7 +130,7 @@ impl PeersState {
130130
/// Returns an object that grants access to the state of a peer.
131131
pub fn peer<'a>(&'a mut self, peer_id: &'a PeerId) -> Peer<'a> {
132132
match self.nodes.get_mut(peer_id) {
133-
None => return Peer::Unknown(UnknownPeer {
133+
None => Peer::Unknown(UnknownPeer {
134134
parent: self,
135135
peer_id: Cow::Borrowed(peer_id),
136136
}),
@@ -585,7 +585,7 @@ mod tests {
585585
peers_state.peer(&id2).into_connected().unwrap().disconnect();
586586
assert_eq!(peers_state.highest_not_connected_peer().map(|p| p.into_peer_id()), Some(id1.clone()));
587587
peers_state.peer(&id1).into_not_connected().unwrap().set_reputation(-100);
588-
assert_eq!(peers_state.highest_not_connected_peer().map(|p| p.into_peer_id()), Some(id2.clone()));
588+
assert_eq!(peers_state.highest_not_connected_peer().map(|p| p.into_peer_id()), Some(id2));
589589
}
590590

591591
#[test]

client/peerset/tests/fuzz.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ fn test_once() {
115115
4 => if let Some(id) = known_nodes.iter()
116116
.filter(|n| incoming_nodes.values().all(|m| m != *n) && !connected_nodes.contains(*n))
117117
.choose(&mut rng) {
118-
peerset.incoming(id.clone(), next_incoming_id.clone());
119-
incoming_nodes.insert(next_incoming_id.clone(), id.clone());
118+
peerset.incoming(id.clone(), next_incoming_id);
119+
incoming_nodes.insert(next_incoming_id, id.clone());
120120
next_incoming_id.0 += 1;
121121
}
122122

primitives/utils/src/mpsc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ mod inner {
6363
/// `UNBOUNDED_CHANNELS_COUNTER`
6464
pub fn tracing_unbounded<T>(key: &'static str) ->(TracingUnboundedSender<T>, TracingUnboundedReceiver<T>) {
6565
let (s, r) = mpsc::unbounded();
66-
(TracingUnboundedSender(key.clone(), s), TracingUnboundedReceiver(key,r))
66+
(TracingUnboundedSender(key, s), TracingUnboundedReceiver(key,r))
6767
}
6868

6969
impl<T> TracingUnboundedSender<T> {

primitives/utils/src/status_sinks.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ struct YieldAfter<T> {
4343
sender: Option<TracingUnboundedSender<T>>,
4444
}
4545

46+
impl <T> Default for StatusSinks<T> {
47+
fn default() -> Self {
48+
Self::new()
49+
}
50+
}
51+
4652
impl<T> StatusSinks<T> {
4753
/// Builds a new empty collection.
4854
pub fn new() -> StatusSinks<T> {

0 commit comments

Comments
 (0)