Update Rust Edition to 2024 (#7766)

* #7749

Thanks @dknopik and @michaelsproul for your help!
This commit is contained in:
chonghe
2025-08-13 11:04:31 +08:00
committed by GitHub
parent bd6b8b6a65
commit 522bd9e9c6
468 changed files with 3594 additions and 3396 deletions

View File

@@ -4,7 +4,7 @@ use crate::discovery::enr_ext::EnrExt;
use crate::discovery::peer_id_to_node_id;
use crate::rpc::{GoodbyeReason, MetaData, Protocol, RPCError, RpcErrorResponse};
use crate::service::TARGET_SUBNET_PEERS;
use crate::{metrics, Gossipsub, NetworkGlobals, PeerId, Subnet, SubnetDiscovery};
use crate::{Gossipsub, NetworkGlobals, PeerId, Subnet, SubnetDiscovery, metrics};
use delay_map::HashSetDelay;
use discv5::Enr;
use libp2p::identify::Info as IdentifyInfo;
@@ -31,11 +31,11 @@ pub use peerdb::peer_info::{
};
use peerdb::score::{PeerAction, ReportSource};
pub use peerdb::sync_status::{SyncInfo, SyncStatus};
use std::collections::{hash_map::Entry, HashMap, HashSet};
use std::collections::{HashMap, HashSet, hash_map::Entry};
use std::net::IpAddr;
use strum::IntoEnumIterator;
use types::data_column_custody_group::{
compute_subnets_from_custody_group, get_custody_groups, CustodyIndex,
CustodyIndex, compute_subnets_from_custody_group, get_custody_groups,
};
pub mod config;
@@ -1141,7 +1141,7 @@ impl<E: EthSpec> PeerManager<E> {
if !peers_on_subnet.is_empty() {
// Order the peers by the number of subnets they are long-lived
// subscribed too, shuffle equal peers.
peers_on_subnet.shuffle(&mut rand::thread_rng());
peers_on_subnet.shuffle(&mut rand::rng());
peers_on_subnet.sort_by_key(|(_, info)| info.long_lived_subnet_count());
// Try and find a candidate peer to remove from the subnet.
@@ -1525,8 +1525,8 @@ enum ConnectingType {
#[cfg(test)]
mod tests {
use super::*;
use crate::rpc::MetaDataV3;
use crate::NetworkConfig;
use crate::rpc::MetaDataV3;
use types::{ChainSpec, ForkName, MainnetEthSpec as E};
async fn build_peer_manager(target_peer_count: usize) -> PeerManager<E> {
@@ -1619,32 +1619,40 @@ mod tests {
// Check that one outbound-only peer was removed because it had the worst score
// and that we did not disconnect the other outbound peer due to the minimum outbound quota.
assert_eq!(peer_manager.network_globals.connected_or_dialing_peers(), 3);
assert!(peer_manager
.network_globals
.peers
.read()
.is_connected(&outbound_only_peer1));
assert!(!peer_manager
.network_globals
.peers
.read()
.is_connected(&outbound_only_peer2));
assert!(
peer_manager
.network_globals
.peers
.read()
.is_connected(&outbound_only_peer1)
);
assert!(
!peer_manager
.network_globals
.peers
.read()
.is_connected(&outbound_only_peer2)
);
// The trusted peer remains connected
assert!(peer_manager
.network_globals
.peers
.read()
.is_connected(&trusted_peer));
assert!(
peer_manager
.network_globals
.peers
.read()
.is_connected(&trusted_peer)
);
peer_manager.heartbeat();
// The trusted peer remains connected, even after subsequent heartbeats.
assert!(peer_manager
.network_globals
.peers
.read()
.is_connected(&trusted_peer));
assert!(
peer_manager
.network_globals
.peers
.read()
.is_connected(&trusted_peer)
);
// Check that if we are at target number of peers, we do not disconnect any.
assert_eq!(peer_manager.network_globals.connected_or_dialing_peers(), 3);
@@ -1956,13 +1964,7 @@ mod tests {
// id mod % 4
// except for the last 5 peers which all go on their own subnets
// So subnets 0-2 should have 4 peers subnet 3 should have 3 and 15-19 should have 1
let subnet: u64 = {
if x < 15 {
x % 4
} else {
x
}
};
let subnet: u64 = { if x < 15 { x % 4 } else { x } };
let peer = PeerId::random();
peer_manager.inject_connect_ingoing(&peer, "/ip4/0.0.0.0".parse().unwrap(), None);

View File

@@ -4,21 +4,21 @@ use std::net::IpAddr;
use std::task::{Context, Poll};
use futures::StreamExt;
use libp2p::core::transport::PortUse;
use libp2p::core::ConnectedPoint;
use libp2p::core::transport::PortUse;
use libp2p::identity::PeerId;
use libp2p::multiaddr::Protocol;
use libp2p::swarm::behaviour::{ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm};
use libp2p::swarm::dial_opts::{DialOpts, PeerCondition};
use libp2p::swarm::dummy::ConnectionHandler;
use libp2p::swarm::{ConnectionDenied, ConnectionId, NetworkBehaviour, ToSwarm};
pub use metrics::{set_gauge_vec, NAT_OPEN};
pub use metrics::{NAT_OPEN, set_gauge_vec};
use tracing::{debug, error, trace};
use types::EthSpec;
use crate::discovery::enr_ext::EnrExt;
use crate::types::SyncState;
use crate::{metrics, ClearDialError};
use crate::{ClearDialError, metrics};
use super::{ConnectingType, PeerManager, PeerManagerEvent};
@@ -172,7 +172,7 @@ impl<E: EthSpec> NetworkBehaviour for PeerManager<E> {
_ => {
return Err(ConnectionDenied::new(format!(
"Connection to peer rejected: invalid multiaddr: {remote_addr}"
)))
)));
}
};
@@ -340,10 +340,10 @@ impl<E: EthSpec> PeerManager<E> {
/// connects and the dial attempt later fails. To handle this, we only update the peer_db if
/// the peer is not already connected.
fn on_dial_failure(&mut self, peer_id: Option<PeerId>) {
if let Some(peer_id) = peer_id {
if !self.network_globals.peers.read().is_connected(&peer_id) {
self.inject_disconnect(&peer_id);
}
if let Some(peer_id) = peer_id
&& !self.network_globals.peers.read().is_connected(&peer_id)
{
self.inject_disconnect(&peer_id);
}
}
}

View File

@@ -1,7 +1,7 @@
use crate::discovery::enr::PEERDAS_CUSTODY_GROUP_COUNT_ENR_KEY;
use crate::discovery::{peer_id_to_node_id, CombinedKey};
use crate::discovery::{CombinedKey, peer_id_to_node_id};
use crate::{
metrics, multiaddr::Multiaddr, types::Subnet, Enr, EnrExt, Gossipsub, PeerId, SyncInfo,
Enr, EnrExt, Gossipsub, PeerId, SyncInfo, metrics, multiaddr::Multiaddr, types::Subnet,
};
use itertools::Itertools;
use logging::crit;
@@ -11,7 +11,7 @@ use std::net::IpAddr;
use std::time::Instant;
use std::{cmp::Ordering, fmt::Display};
use std::{
collections::{hash_map::Entry, HashMap, HashSet},
collections::{HashMap, HashSet, hash_map::Entry},
fmt::Formatter,
};
use sync_status::SyncStatus;
@@ -431,12 +431,11 @@ impl<E: EthSpec> PeerDB<E> {
.peers
.iter()
.filter_map(|(peer_id, info)| {
if let PeerConnectionStatus::Dialing { since } = info.connection_status() {
if (*since) + std::time::Duration::from_secs(DIAL_TIMEOUT)
if let PeerConnectionStatus::Dialing { since } = info.connection_status()
&& (*since) + std::time::Duration::from_secs(DIAL_TIMEOUT)
< std::time::Instant::now()
{
return Some(*peer_id);
}
{
return Some(*peer_id);
}
None
})

View File

@@ -127,12 +127,12 @@ fn client_from_agent_version(agent_version: &str) -> (ClientKind, String, String
}
Some("teku") => {
let kind = ClientKind::Teku;
if agent_split.next().is_some() {
if let Some(agent_version) = agent_split.next() {
version = agent_version.into();
if let Some(agent_os_version) = agent_split.next() {
os_version = agent_os_version.into();
}
if agent_split.next().is_some()
&& let Some(agent_version) = agent_split.next()
{
version = agent_version.into();
if let Some(agent_os_version) = agent_split.next() {
os_version = agent_os_version.into();
}
}
(kind, version, os_version)
@@ -143,24 +143,24 @@ fn client_from_agent_version(agent_version: &str) -> (ClientKind, String, String
}
Some("Prysm") => {
let kind = ClientKind::Prysm;
if agent_split.next().is_some() {
if let Some(agent_version) = agent_split.next() {
version = agent_version.into();
if let Some(agent_os_version) = agent_split.next() {
os_version = agent_os_version.into();
}
if agent_split.next().is_some()
&& let Some(agent_version) = agent_split.next()
{
version = agent_version.into();
if let Some(agent_os_version) = agent_split.next() {
os_version = agent_os_version.into();
}
}
(kind, version, os_version)
}
Some("nimbus") => {
let kind = ClientKind::Nimbus;
if agent_split.next().is_some() {
if let Some(agent_version) = agent_split.next() {
version = agent_version.into();
if let Some(agent_os_version) = agent_split.next() {
os_version = agent_os_version.into();
}
if agent_split.next().is_some()
&& let Some(agent_version) = agent_split.next()
{
version = agent_version.into();
if let Some(agent_os_version) = agent_split.next() {
os_version = agent_os_version.into();
}
}
(kind, version, os_version)

View File

@@ -3,19 +3,19 @@ use super::score::{PeerAction, Score, ScoreState};
use super::sync_status::SyncStatus;
use crate::discovery::Eth2Enr;
use crate::{rpc::MetaData, types::Subnet};
use PeerConnectionStatus::*;
use discv5::Enr;
use eth2::types::{PeerDirection, PeerState};
use libp2p::core::multiaddr::{Multiaddr, Protocol};
use serde::{
ser::{SerializeStruct, Serializer},
Serialize,
ser::{SerializeStruct, Serializer},
};
use std::collections::HashSet;
use std::net::IpAddr;
use std::time::Instant;
use strum::AsRefStr;
use types::{DataColumnSubnetId, EthSpec};
use PeerConnectionStatus::*;
/// Information about a given connected peer.
#[derive(Clone, Debug, Serialize)]
@@ -95,15 +95,15 @@ impl<E: EthSpec> PeerInfo<E> {
if let Some(meta_data) = &self.meta_data {
match subnet {
Subnet::Attestation(id) => {
return meta_data.attnets().get(**id as usize).unwrap_or(false)
return meta_data.attnets().get(**id as usize).unwrap_or(false);
}
Subnet::SyncCommittee(id) => {
return meta_data
.syncnets()
.is_ok_and(|s| s.get(**id as usize).unwrap_or(false))
.is_ok_and(|s| s.get(**id as usize).unwrap_or(false));
}
Subnet::DataColumn(subnet_id) => {
return self.is_assigned_to_custody_subnet(subnet_id)
return self.is_assigned_to_custody_subnet(subnet_id);
}
}
}
@@ -179,10 +179,10 @@ impl<E: EthSpec> PeerInfo<E> {
pub fn long_lived_subnet_count(&self) -> usize {
if let Some(meta_data) = self.meta_data.as_ref() {
return meta_data.attnets().num_set_bits();
} else if let Some(enr) = self.enr.as_ref() {
if let Ok(attnets) = enr.attestation_bitfield::<E>() {
return attnets.num_set_bits();
}
} else if let Some(enr) = self.enr.as_ref()
&& let Ok(attnets) = enr.attestation_bitfield::<E>()
{
return attnets.num_set_bits();
}
0
}
@@ -247,20 +247,20 @@ impl<E: EthSpec> PeerInfo<E> {
if !meta_data.attnets().is_zero() && !self.subnets.is_empty() {
return true;
}
if let Ok(sync) = meta_data.syncnets() {
if !sync.is_zero() {
return true;
}
if let Ok(sync) = meta_data.syncnets()
&& !sync.is_zero()
{
return true;
}
}
// We may not have the metadata but may have an ENR. Lets check that
if let Some(enr) = self.enr.as_ref() {
if let Ok(attnets) = enr.attestation_bitfield::<E>() {
if !attnets.is_zero() && !self.subnets.is_empty() {
return true;
}
}
if let Some(enr) = self.enr.as_ref()
&& let Ok(attnets) = enr.attestation_bitfield::<E>()
&& !attnets.is_zero()
&& !self.subnets.is_empty()
{
return true;
}
false
}

View File

@@ -332,11 +332,7 @@ impl Score {
Some(v) => {
// Only reverse when none of the items is NAN,
// so that NAN's are never considered.
if reverse {
v.reverse()
} else {
v
}
if reverse { v.reverse() } else { v }
}
None if self.score().is_nan() && !other.score().is_nan() => Ordering::Less,
None if !self.score().is_nan() && other.score().is_nan() => Ordering::Greater,