Use E for EthSpec globally (#5264)

* Use `E` for `EthSpec` globally

* Fix tests

* Merge branch 'unstable' into e-ethspec

* Merge branch 'unstable' into e-ethspec

# Conflicts:
#	beacon_node/execution_layer/src/engine_api.rs
#	beacon_node/execution_layer/src/engine_api/http.rs
#	beacon_node/execution_layer/src/engine_api/json_structures.rs
#	beacon_node/execution_layer/src/test_utils/handle_rpc.rs
#	beacon_node/store/src/partial_beacon_state.rs
#	consensus/types/src/beacon_block.rs
#	consensus/types/src/beacon_block_body.rs
#	consensus/types/src/beacon_state.rs
#	consensus/types/src/config_and_preset.rs
#	consensus/types/src/execution_payload.rs
#	consensus/types/src/execution_payload_header.rs
#	consensus/types/src/light_client_optimistic_update.rs
#	consensus/types/src/payload.rs
#	lcli/src/parse_ssz.rs
This commit is contained in:
Mac L
2024-04-03 02:12:25 +11:00
committed by GitHub
parent f8fdb71f50
commit 969d12dc6f
230 changed files with 2743 additions and 2792 deletions

View File

@@ -67,9 +67,9 @@ pub const MIN_OUTBOUND_ONLY_FACTOR: f32 = 0.2;
pub const PRIORITY_PEER_EXCESS: f32 = 0.2;
/// The main struct that handles peer's reputation and connection status.
pub struct PeerManager<TSpec: EthSpec> {
pub struct PeerManager<E: EthSpec> {
/// Storage of network globals to access the `PeerDB`.
network_globals: Arc<NetworkGlobals<TSpec>>,
network_globals: Arc<NetworkGlobals<E>>,
/// A queue of events that the `PeerManager` is waiting to produce.
events: SmallVec<[PeerManagerEvent; 16]>,
/// A collection of inbound-connected peers awaiting to be Ping'd.
@@ -140,11 +140,11 @@ pub enum PeerManagerEvent {
DiscoverSubnetPeers(Vec<SubnetDiscovery>),
}
impl<TSpec: EthSpec> PeerManager<TSpec> {
impl<E: EthSpec> PeerManager<E> {
// NOTE: Must be run inside a tokio executor.
pub fn new(
cfg: config::Config,
network_globals: Arc<NetworkGlobals<TSpec>>,
network_globals: Arc<NetworkGlobals<E>>,
log: &slog::Logger,
) -> error::Result<Self> {
let config::Config {
@@ -672,7 +672,7 @@ impl<TSpec: EthSpec> PeerManager<TSpec> {
}
/// Received a metadata response from a peer.
pub fn meta_data_response(&mut self, peer_id: &PeerId, meta_data: MetaData<TSpec>) {
pub fn meta_data_response(&mut self, peer_id: &PeerId, meta_data: MetaData<E>) {
if let Some(peer_info) = self.network_globals.peers.write().peer_info_mut(peer_id) {
if let Some(known_meta_data) = &peer_info.meta_data() {
if *known_meta_data.seq_number() < *meta_data.seq_number() {
@@ -973,20 +973,19 @@ impl<TSpec: EthSpec> PeerManager<TSpec> {
}
// 1. Look through peers that have the worst score (ignoring non-penalized scored peers).
prune_peers!(|info: &PeerInfo<TSpec>| { info.score().score() < 0.0 });
prune_peers!(|info: &PeerInfo<E>| { info.score().score() < 0.0 });
// 2. Attempt to remove peers that are not subscribed to a subnet, if we still need to
// prune more.
if peers_to_prune.len() < connected_peer_count.saturating_sub(self.target_peers) {
prune_peers!(|info: &PeerInfo<TSpec>| { !info.has_long_lived_subnet() });
prune_peers!(|info: &PeerInfo<E>| { !info.has_long_lived_subnet() });
}
// 3. and 4. Remove peers that are too grouped on any given subnet. If all subnets are
// uniformly distributed, remove random peers.
if peers_to_prune.len() < connected_peer_count.saturating_sub(self.target_peers) {
// Of our connected peers, build a map from subnet_id -> Vec<(PeerId, PeerInfo)>
let mut subnet_to_peer: HashMap<Subnet, Vec<(PeerId, PeerInfo<TSpec>)>> =
HashMap::new();
let mut subnet_to_peer: HashMap<Subnet, Vec<(PeerId, PeerInfo<E>)>> = HashMap::new();
// These variables are used to track if a peer is in a long-lived sync-committee as we
// may wish to retain this peer over others when pruning.
let mut sync_committee_peer_count: HashMap<SyncSubnetId, u64> = HashMap::new();

View File

@@ -21,7 +21,7 @@ use crate::{metrics, ClearDialError};
use super::{ConnectingType, PeerManager, PeerManagerEvent};
impl<TSpec: EthSpec> NetworkBehaviour for PeerManager<TSpec> {
impl<E: EthSpec> NetworkBehaviour for PeerManager<E> {
type ConnectionHandler = ConnectionHandler;
type ToSwarm = PeerManagerEvent;
@@ -227,7 +227,7 @@ impl<TSpec: EthSpec> NetworkBehaviour for PeerManager<TSpec> {
}
}
impl<TSpec: EthSpec> PeerManager<TSpec> {
impl<E: EthSpec> PeerManager<E> {
fn on_connection_established(
&mut self,
peer_id: PeerId,

View File

@@ -32,9 +32,9 @@ const ALLOWED_NEGATIVE_GOSSIPSUB_FACTOR: f32 = 0.1;
const DIAL_TIMEOUT: u64 = 15;
/// Storage of known peers, their reputation and information
pub struct PeerDB<TSpec: EthSpec> {
pub struct PeerDB<E: EthSpec> {
/// The collection of known connected peers, their status and reputation
peers: HashMap<PeerId, PeerInfo<TSpec>>,
peers: HashMap<PeerId, PeerInfo<E>>,
/// The number of disconnected nodes in the database.
disconnected_peers: usize,
/// Counts banned peers in total and per ip
@@ -45,7 +45,7 @@ pub struct PeerDB<TSpec: EthSpec> {
log: slog::Logger,
}
impl<TSpec: EthSpec> PeerDB<TSpec> {
impl<E: EthSpec> PeerDB<E> {
pub fn new(trusted_peers: Vec<PeerId>, disable_peer_scoring: bool, log: &slog::Logger) -> Self {
// Initialize the peers hashmap with trusted peers
let peers = trusted_peers
@@ -72,7 +72,7 @@ impl<TSpec: EthSpec> PeerDB<TSpec> {
}
/// Returns an iterator over all peers in the db.
pub fn peers(&self) -> impl Iterator<Item = (&PeerId, &PeerInfo<TSpec>)> {
pub fn peers(&self) -> impl Iterator<Item = (&PeerId, &PeerInfo<E>)> {
self.peers.iter()
}
@@ -82,14 +82,14 @@ impl<TSpec: EthSpec> PeerDB<TSpec> {
}
/// Returns a peer's info, if known.
pub fn peer_info(&self, peer_id: &PeerId) -> Option<&PeerInfo<TSpec>> {
pub fn peer_info(&self, peer_id: &PeerId) -> Option<&PeerInfo<E>> {
self.peers.get(peer_id)
}
/// Returns a mutable reference to a peer's info if known.
// VISIBILITY: The peer manager is able to modify some elements of the peer info, such as sync
// status.
pub(super) fn peer_info_mut(&mut self, peer_id: &PeerId) -> Option<&mut PeerInfo<TSpec>> {
pub(super) fn peer_info_mut(&mut self, peer_id: &PeerId) -> Option<&mut PeerInfo<E>> {
self.peers.get_mut(peer_id)
}
@@ -154,7 +154,7 @@ impl<TSpec: EthSpec> PeerDB<TSpec> {
}
/// Checks if the peer's known addresses are currently banned.
fn ip_is_banned(&self, peer: &PeerInfo<TSpec>) -> Option<IpAddr> {
fn ip_is_banned(&self, peer: &PeerInfo<E>) -> Option<IpAddr> {
peer.seen_ip_addresses()
.find(|ip| self.banned_peers_count.ip_is_banned(ip))
}
@@ -177,7 +177,7 @@ impl<TSpec: EthSpec> PeerDB<TSpec> {
}
/// Gives the ids and info of all known connected peers.
pub fn connected_peers(&self) -> impl Iterator<Item = (&PeerId, &PeerInfo<TSpec>)> {
pub fn connected_peers(&self) -> impl Iterator<Item = (&PeerId, &PeerInfo<E>)> {
self.peers.iter().filter(|(_, info)| info.is_connected())
}
@@ -271,7 +271,7 @@ impl<TSpec: EthSpec> PeerDB<TSpec> {
/// Returns a vector of all connected peers sorted by score beginning with the worst scores.
/// Ties get broken randomly.
pub fn worst_connected_peers(&self) -> Vec<(&PeerId, &PeerInfo<TSpec>)> {
pub fn worst_connected_peers(&self) -> Vec<(&PeerId, &PeerInfo<E>)> {
let mut connected = self
.peers
.iter()
@@ -285,9 +285,9 @@ impl<TSpec: EthSpec> PeerDB<TSpec> {
/// Returns a vector containing peers (their ids and info), sorted by
/// score from highest to lowest, and filtered using `is_status`
pub fn best_peers_by_status<F>(&self, is_status: F) -> Vec<(&PeerId, &PeerInfo<TSpec>)>
pub fn best_peers_by_status<F>(&self, is_status: F) -> Vec<(&PeerId, &PeerInfo<E>)>
where
F: Fn(&PeerInfo<TSpec>) -> bool,
F: Fn(&PeerInfo<E>) -> bool,
{
let mut by_status = self
.peers
@@ -301,7 +301,7 @@ impl<TSpec: EthSpec> PeerDB<TSpec> {
/// Returns the peer with highest reputation that satisfies `is_status`
pub fn best_by_status<F>(&self, is_status: F) -> Option<&PeerId>
where
F: Fn(&PeerInfo<TSpec>) -> bool,
F: Fn(&PeerInfo<E>) -> bool,
{
self.peers
.iter()
@@ -1058,7 +1058,7 @@ impl<TSpec: EthSpec> PeerDB<TSpec> {
fn handle_score_transition(
previous_state: ScoreState,
peer_id: &PeerId,
info: &PeerInfo<TSpec>,
info: &PeerInfo<E>,
log: &slog::Logger,
) -> ScoreTransitionResult {
match (info.score_state(), previous_state) {
@@ -1269,13 +1269,13 @@ mod tests {
}
}
fn add_score<TSpec: EthSpec>(db: &mut PeerDB<TSpec>, peer_id: &PeerId, score: f64) {
fn add_score<E: EthSpec>(db: &mut PeerDB<E>, peer_id: &PeerId, score: f64) {
if let Some(info) = db.peer_info_mut(peer_id) {
info.add_to_score(score);
}
}
fn reset_score<TSpec: EthSpec>(db: &mut PeerDB<TSpec>, peer_id: &PeerId) {
fn reset_score<E: EthSpec>(db: &mut PeerDB<E>, peer_id: &PeerId) {
if let Some(info) = db.peer_info_mut(peer_id) {
info.reset_score();
}

View File

@@ -18,8 +18,8 @@ use PeerConnectionStatus::*;
/// Information about a given connected peer.
#[derive(Clone, Debug, Serialize)]
#[serde(bound = "T: EthSpec")]
pub struct PeerInfo<T: EthSpec> {
#[serde(bound = "E: EthSpec")]
pub struct PeerInfo<E: EthSpec> {
/// The peers reputation
score: Score,
/// Client managing this peer
@@ -37,7 +37,7 @@ pub struct PeerInfo<T: EthSpec> {
sync_status: SyncStatus,
/// The ENR subnet bitfield of the peer. This may be determined after it's initial
/// connection.
meta_data: Option<MetaData<T>>,
meta_data: Option<MetaData<E>>,
/// Subnets the peer is connected to.
subnets: HashSet<Subnet>,
/// The time we would like to retain this peer. After this time, the peer is no longer
@@ -53,8 +53,8 @@ pub struct PeerInfo<T: EthSpec> {
enr: Option<Enr>,
}
impl<TSpec: EthSpec> Default for PeerInfo<TSpec> {
fn default() -> PeerInfo<TSpec> {
impl<E: EthSpec> Default for PeerInfo<E> {
fn default() -> PeerInfo<E> {
PeerInfo {
score: Score::default(),
client: Client::default(),
@@ -72,7 +72,7 @@ impl<TSpec: EthSpec> Default for PeerInfo<TSpec> {
}
}
impl<T: EthSpec> PeerInfo<T> {
impl<E: EthSpec> PeerInfo<E> {
/// Return a PeerInfo struct for a trusted peer.
pub fn trusted_peer_info() -> Self {
PeerInfo {
@@ -120,7 +120,7 @@ impl<T: EthSpec> PeerInfo<T> {
}
/// Returns the metadata for the peer if currently known.
pub fn meta_data(&self) -> Option<&MetaData<T>> {
pub fn meta_data(&self) -> Option<&MetaData<E>> {
self.meta_data.as_ref()
}
@@ -151,7 +151,7 @@ impl<T: EthSpec> PeerInfo<T> {
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::<T>() {
if let Ok(attnets) = enr.attestation_bitfield::<E>() {
return attnets.num_set_bits();
}
}
@@ -177,7 +177,7 @@ impl<T: EthSpec> PeerInfo<T> {
}
}
} else if let Some(enr) = self.enr.as_ref() {
if let Ok(attnets) = enr.attestation_bitfield::<T>() {
if let Ok(attnets) = enr.attestation_bitfield::<E>() {
for subnet in 0..=attnets.highest_set_bit().unwrap_or(0) {
if attnets.get(subnet).unwrap_or(false) {
long_lived_subnets.push(Subnet::Attestation((subnet as u64).into()));
@@ -185,7 +185,7 @@ impl<T: EthSpec> PeerInfo<T> {
}
}
if let Ok(syncnets) = enr.sync_committee_bitfield::<T>() {
if let Ok(syncnets) = enr.sync_committee_bitfield::<E>() {
for subnet in 0..=syncnets.highest_set_bit().unwrap_or(0) {
if syncnets.get(subnet).unwrap_or(false) {
long_lived_subnets.push(Subnet::SyncCommittee((subnet as u64).into()));
@@ -217,7 +217,7 @@ impl<T: EthSpec> PeerInfo<T> {
// 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::<T>() {
if let Ok(attnets) = enr.attestation_bitfield::<E>() {
if !attnets.is_zero() && !self.subnets.is_empty() {
return true;
}
@@ -344,7 +344,7 @@ impl<T: EthSpec> PeerInfo<T> {
/// Sets an explicit value for the meta data.
// VISIBILITY: The peer manager is able to adjust the meta_data
pub(in crate::peer_manager) fn set_meta_data(&mut self, meta_data: MetaData<T>) {
pub(in crate::peer_manager) fn set_meta_data(&mut self, meta_data: MetaData<E>) {
self.meta_data = Some(meta_data)
}