Skip to content

Commit

Permalink
*: Fix newly raised clippy warnings (#3106)
Browse files Browse the repository at this point in the history
Fixed minor issues raised by clippy to improve correctness and readablitity.

(cherry picked from commit c32f03c)

# Conflicts:
#	examples/gossipsub-chat.rs
#	protocols/kad/src/query/peers/closest/disjoint.rs
  • Loading branch information
umgefahren authored and mergify[bot] committed Feb 15, 2023
1 parent 2769ef5 commit d6915a3
Show file tree
Hide file tree
Showing 20 changed files with 78 additions and 47 deletions.
2 changes: 1 addition & 1 deletion core/src/identity/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl PublicKey {
}

let key_len = bitstr_head[1].checked_sub(1)? as usize;
let key_buf = asn1_buf.get(4 + oids_len + 3..4 + oids_len + 3 + key_len as usize)?;
let key_buf = asn1_buf.get(4 + oids_len + 3..4 + oids_len + 3 + key_len)?;
Some(key_buf)
}
}
Expand Down
6 changes: 3 additions & 3 deletions examples/chat-tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
// Create a random PeerId
let id_keys = identity::Keypair::generate_ed25519();
let peer_id = PeerId::from(id_keys.public());
println!("Local peer id: {:?}", peer_id);
println!("Local peer id: {peer_id:?}");

// Create a keypair for authenticated encryption of the transport.
let noise_keys = noise::Keypair::<noise::X25519Spec>::new()
Expand Down Expand Up @@ -136,7 +136,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
if let Some(to_dial) = std::env::args().nth(1) {
let addr: Multiaddr = to_dial.parse()?;
swarm.dial(addr)?;
println!("Dialed {:?}", to_dial);
println!("Dialed {to_dial:?}");
}

// Read full lines from stdin
Expand All @@ -155,7 +155,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
event = swarm.select_next_some() => {
match event {
SwarmEvent::NewListenAddr { address, .. } => {
println!("Listening on {:?}", address);
println!("Listening on {address:?}");
}
SwarmEvent::Behaviour(MyBehaviourEvent::Floodsub(FloodsubEvent::Message(message))) => {
println!(
Expand Down
6 changes: 3 additions & 3 deletions examples/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
// Create a random PeerId
let local_key = identity::Keypair::generate_ed25519();
let local_peer_id = PeerId::from(local_key.public());
println!("Local peer id: {:?}", local_peer_id);
println!("Local peer id: {local_peer_id:?}");

// Set up an encrypted DNS-enabled TCP Transport over the Mplex and Yamux protocols
let transport = libp2p::development_transport(local_key).await?;
Expand Down Expand Up @@ -122,7 +122,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
if let Some(to_dial) = std::env::args().nth(1) {
let addr: Multiaddr = to_dial.parse()?;
swarm.dial(addr)?;
println!("Dialed {:?}", to_dial)
println!("Dialed {to_dial:?}")
}

// Read full lines from stdin
Expand All @@ -140,7 +140,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
.publish(floodsub_topic.clone(), line.expect("Stdin not to close").as_bytes()),
event = swarm.select_next_some() => match event {
SwarmEvent::NewListenAddr { address, .. } => {
println!("Listening on {:?}", address);
println!("Listening on {address:?}");
}
SwarmEvent::Behaviour(OutEvent::Floodsub(
FloodsubEvent::Message(message)
Expand Down
10 changes: 5 additions & 5 deletions examples/distributed-key-value-store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
line = stdin.select_next_some() => handle_input_line(&mut swarm.behaviour_mut().kademlia, line.expect("Stdin not to close")),
event = swarm.select_next_some() => match event {
SwarmEvent::NewListenAddr { address, .. } => {
println!("Listening in {:?}", address);
println!("Listening in {address:?}");
},
SwarmEvent::Behaviour(MyBehaviourEvent::Mdns(MdnsEvent::Discovered(list))) => {
for (peer_id, multiaddr) in list {
Expand All @@ -132,7 +132,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
}
}
QueryResult::GetProviders(Err(err)) => {
eprintln!("Failed to get providers: {:?}", err);
eprintln!("Failed to get providers: {err:?}");
}
QueryResult::GetRecord(Ok(ok)) => {
for PeerRecord {
Expand All @@ -148,7 +148,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
}
}
QueryResult::GetRecord(Err(err)) => {
eprintln!("Failed to get record: {:?}", err);
eprintln!("Failed to get record: {err:?}");
}
QueryResult::PutRecord(Ok(PutRecordOk { key })) => {
println!(
Expand All @@ -157,7 +157,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
);
}
QueryResult::PutRecord(Err(err)) => {
eprintln!("Failed to put record: {:?}", err);
eprintln!("Failed to put record: {err:?}");
}
QueryResult::StartProviding(Ok(AddProviderOk { key })) => {
println!(
Expand All @@ -166,7 +166,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
);
}
QueryResult::StartProviding(Err(err)) => {
eprintln!("Failed to put provider record: {:?}", err);
eprintln!("Failed to put provider record: {err:?}");
}
_ => {}
}
Expand Down
6 changes: 3 additions & 3 deletions examples/file-sharing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
// Locate all nodes providing the file.
let providers = network_client.get_providers(name.clone()).await;
if providers.is_empty() {
return Err(format!("Could not find provider for file {}.", name).into());
return Err(format!("Could not find provider for file {name}.").into());
}

// Request the content of the file from each node.
Expand Down Expand Up @@ -506,8 +506,8 @@ mod network {
}
}
SwarmEvent::IncomingConnectionError { .. } => {}
SwarmEvent::Dialing(peer_id) => eprintln!("Dialing {}", peer_id),
e => panic!("{:?}", e),
SwarmEvent::Dialing(peer_id) => eprintln!("Dialing {peer_id}"),
e => panic!("{e:?}"),
}
}

Expand Down
26 changes: 26 additions & 0 deletions examples/gossipsub-chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ async fn main() -> Result<(), Box<dyn Error>> {
// Create a random PeerId
let local_key = identity::Keypair::generate_ed25519();
let local_peer_id = PeerId::from(local_key.public());
<<<<<<< HEAD
println!("Local peer id: {:?}", local_peer_id);
=======
println!("Local peer id: {local_peer_id}");
>>>>>>> c32f03c3 (*: Fix newly raised clippy warnings (#3106))

// Set up an encrypted TCP Transport over the Mplex and Yamux protocols
let transport = libp2p::development_transport(local_key.clone()).await?;
Expand Down Expand Up @@ -133,6 +137,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
select! {
line = stdin.select_next_some() => {
if let Err(e) = swarm
<<<<<<< HEAD
.behaviour_mut()
.publish(topic.clone(), line.expect("Stdin not to close").as_bytes())
{
Expand All @@ -141,6 +146,27 @@ async fn main() -> Result<(), Box<dyn Error>> {
},
event = swarm.select_next_some() => match event {
SwarmEvent::Behaviour(GossipsubEvent::Message {
=======
.behaviour_mut().gossipsub
.publish(topic.clone(), line.expect("Stdin not to close").as_bytes()) {
println!("Publish error: {e:?}");
}
},
event = swarm.select_next_some() => match event {
SwarmEvent::Behaviour(MyBehaviourEvent::Mdns(MdnsEvent::Discovered(list))) => {
for (peer_id, _multiaddr) in list {
println!("mDNS discovered a new peer: {peer_id}");
swarm.behaviour_mut().gossipsub.add_explicit_peer(&peer_id);
}
},
SwarmEvent::Behaviour(MyBehaviourEvent::Mdns(MdnsEvent::Expired(list))) => {
for (peer_id, _multiaddr) in list {
println!("mDNS discover peer has expired: {peer_id}");
swarm.behaviour_mut().gossipsub.remove_explicit_peer(&peer_id);
}
},
SwarmEvent::Behaviour(MyBehaviourEvent::Gossipsub(GossipsubEvent::Message {
>>>>>>> c32f03c3 (*: Fix newly raised clippy warnings (#3106))
propagation_source: peer_id,
message_id: id,
message,
Expand Down
4 changes: 2 additions & 2 deletions examples/ipfs-kad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
identity::Keypair::generate_ed25519().public().into()
};

println!("Searching for the closest peers to {:?}", to_search);
println!("Searching for the closest peers to {to_search:?}");
swarm.behaviour_mut().get_closest_peers(to_search);

// Kick it off!
Expand All @@ -102,7 +102,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
}
Err(GetClosestPeersError::Timeout { peers, .. }) => {
if !peers.is_empty() {
println!("Query timed out with closest peers: {:#?}", peers)
println!("Query timed out with closest peers: {peers:#?}")
} else {
// The example is considered failed as there
// should always be at least 1 reachable peer.
Expand Down
16 changes: 8 additions & 8 deletions examples/ipfs-private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,15 @@ async fn main() -> Result<(), Box<dyn Error>> {
env_logger::init();

let ipfs_path = get_ipfs_path();
println!("using IPFS_PATH {:?}", ipfs_path);
println!("using IPFS_PATH {ipfs_path:?}");
let psk: Option<PreSharedKey> = get_psk(&ipfs_path)?
.map(|text| PreSharedKey::from_str(&text))
.transpose()?;

// Create a random PeerId
let local_key = identity::Keypair::generate_ed25519();
let local_peer_id = PeerId::from(local_key.public());
println!("using random peer id: {:?}", local_peer_id);
println!("using random peer id: {local_peer_id:?}");
if let Some(psk) = psk {
println!("using swarm key with fingerprint: {}", psk.fingerprint());
}
Expand Down Expand Up @@ -208,7 +208,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
ping: ping::Behaviour::new(ping::Config::new()),
};

println!("Subscribing to {:?}", gossipsub_topic);
println!("Subscribing to {gossipsub_topic:?}");
behaviour.gossipsub.subscribe(&gossipsub_topic).unwrap();
Swarm::new(transport, behaviour, local_peer_id)
};
Expand All @@ -217,7 +217,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
for to_dial in std::env::args().skip(1) {
let addr: Multiaddr = parse_legacy_multiaddr(&to_dial)?;
swarm.dial(addr)?;
println!("Dialed {:?}", to_dial)
println!("Dialed {to_dial:?}")
}

// Read full lines from stdin
Expand All @@ -235,16 +235,16 @@ async fn main() -> Result<(), Box<dyn Error>> {
.gossipsub
.publish(gossipsub_topic.clone(), line.expect("Stdin not to close").as_bytes())
{
println!("Publish error: {:?}", e);
println!("Publish error: {e:?}");
}
},
event = swarm.select_next_some() => {
match event {
SwarmEvent::NewListenAddr { address, .. } => {
println!("Listening on {:?}", address);
println!("Listening on {address:?}");
}
SwarmEvent::Behaviour(MyBehaviourEvent::Identify(event)) => {
println!("identify: {:?}", event);
println!("identify: {event:?}");
}
SwarmEvent::Behaviour(MyBehaviourEvent::Gossipsub(GossipsubEvent::Message {
propagation_source: peer_id,
Expand Down Expand Up @@ -292,7 +292,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
peer,
result: Result::Err(ping::Failure::Other { error }),
} => {
println!("ping: ping::Failure with {}: {}", peer.to_base58(), error);
println!("ping: ping::Failure with {}: {error}", peer.to_base58());
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions examples/mdns-passive-discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
// Create a random PeerId.
let id_keys = identity::Keypair::generate_ed25519();
let peer_id = PeerId::from(id_keys.public());
println!("Local peer id: {:?}", peer_id);
println!("Local peer id: {peer_id:?}");

// Create a transport.
let transport = libp2p::development_transport(id_keys).await?;
Expand All @@ -52,12 +52,12 @@ async fn main() -> Result<(), Box<dyn Error>> {
match swarm.select_next_some().await {
SwarmEvent::Behaviour(MdnsEvent::Discovered(peers)) => {
for (peer, addr) in peers {
println!("discovered {} {}", peer, addr);
println!("discovered {peer} {addr}");
}
}
SwarmEvent::Behaviour(MdnsEvent::Expired(expired)) => {
for (peer, addr) in expired {
println!("expired {} {}", peer, addr);
println!("expired {peer} {addr}");
}
}
_ => {}
Expand Down
8 changes: 4 additions & 4 deletions examples/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use std::error::Error;
async fn main() -> Result<(), Box<dyn Error>> {
let local_key = identity::Keypair::generate_ed25519();
let local_peer_id = PeerId::from(local_key.public());
println!("Local peer id: {:?}", local_peer_id);
println!("Local peer id: {local_peer_id:?}");

let transport = libp2p::development_transport(local_key).await?;

Expand All @@ -71,13 +71,13 @@ async fn main() -> Result<(), Box<dyn Error>> {
if let Some(addr) = std::env::args().nth(1) {
let remote: Multiaddr = addr.parse()?;
swarm.dial(remote)?;
println!("Dialed {}", addr)
println!("Dialed {addr}")
}

loop {
match swarm.select_next_some().await {
SwarmEvent::NewListenAddr { address, .. } => println!("Listening on {:?}", address),
SwarmEvent::Behaviour(event) => println!("{:?}", event),
SwarmEvent::NewListenAddr { address, .. } => println!("Listening on {address:?}"),
SwarmEvent::Behaviour(event) => println!("{event:?}"),
_ => {}
}
}
Expand Down
2 changes: 1 addition & 1 deletion muxers/mplex/src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ impl Decoder for Codec {
}

let buf = src.split_to(len);
let num = (header >> 3) as u64;
let num = header >> 3;
let out = match header & 7 {
0 => Frame::Open {
stream_id: RemoteStreamId::dialer(num),
Expand Down
4 changes: 2 additions & 2 deletions protocols/gossipsub/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1268,9 +1268,9 @@ where
// Ask in random order
let mut iwant_ids_vec: Vec<_> = iwant_ids.into_iter().collect();
let mut rng = thread_rng();
iwant_ids_vec.partial_shuffle(&mut rng, iask as usize);
iwant_ids_vec.partial_shuffle(&mut rng, iask);

iwant_ids_vec.truncate(iask as usize);
iwant_ids_vec.truncate(iask);
*iasked += iask;

for message_id in &iwant_ids_vec {
Expand Down
2 changes: 1 addition & 1 deletion protocols/gossipsub/src/peer_score.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ impl PeerScore {

// P2: first message deliveries
let p2 = {
let v = topic_stats.first_message_deliveries as f64;
let v = topic_stats.first_message_deliveries;
if v < topic_params.first_message_deliveries_cap {
v
} else {
Expand Down
2 changes: 1 addition & 1 deletion protocols/gossipsub/src/peer_score/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ fn test_score_ip_colocation() {
let n_shared = 3.0;
let ip_surplus = n_shared - ip_colocation_factor_threshold;
let penalty = ip_surplus * ip_surplus;
let expected = ip_colocation_factor_weight * penalty as f64;
let expected = ip_colocation_factor_weight * penalty;

assert_eq!(score_b, expected, "Peer B should have expected score");
assert_eq!(score_c, expected, "Peer C should have expected score");
Expand Down
2 changes: 1 addition & 1 deletion protocols/gossipsub/src/subscription_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub trait TopicSubscriptionFilter {
}
}
self.filter_incoming_subscription_set(
filtered_subscriptions.into_iter().map(|(_, v)| v).collect(),
filtered_subscriptions.into_values().collect(),
currently_subscribed_topics,
)
}
Expand Down
4 changes: 2 additions & 2 deletions protocols/kad/src/kbucket/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ impl<T> From<Key<T>> for KeyBytes {

impl From<Multihash> for Key<Multihash> {
fn from(m: Multihash) -> Self {
let bytes = KeyBytes(Sha256::digest(&m.to_bytes()));
let bytes = KeyBytes(Sha256::digest(m.to_bytes()));
Key { preimage: m, bytes }
}
}

impl From<PeerId> for Key<PeerId> {
fn from(p: PeerId) -> Self {
let bytes = KeyBytes(Sha256::digest(&p.to_bytes()));
let bytes = KeyBytes(Sha256::digest(p.to_bytes()));
Key { preimage: p, bytes }
}
}
Expand Down
Loading

0 comments on commit d6915a3

Please sign in to comment.