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

@@ -28,38 +28,34 @@ pub const SYNC_COMMITTEE_BITFIELD_ENR_KEY: &str = "syncnets";
/// Extension trait for ENR's within Eth2.
pub trait Eth2Enr {
/// The attestation subnet bitfield associated with the ENR.
fn attestation_bitfield<TSpec: EthSpec>(
&self,
) -> Result<EnrAttestationBitfield<TSpec>, &'static str>;
fn attestation_bitfield<E: EthSpec>(&self) -> Result<EnrAttestationBitfield<E>, &'static str>;
/// The sync committee subnet bitfield associated with the ENR.
fn sync_committee_bitfield<TSpec: EthSpec>(
fn sync_committee_bitfield<E: EthSpec>(
&self,
) -> Result<EnrSyncCommitteeBitfield<TSpec>, &'static str>;
) -> Result<EnrSyncCommitteeBitfield<E>, &'static str>;
fn eth2(&self) -> Result<EnrForkId, &'static str>;
}
impl Eth2Enr for Enr {
fn attestation_bitfield<TSpec: EthSpec>(
&self,
) -> Result<EnrAttestationBitfield<TSpec>, &'static str> {
fn attestation_bitfield<E: EthSpec>(&self) -> Result<EnrAttestationBitfield<E>, &'static str> {
let bitfield_bytes = self
.get(ATTESTATION_BITFIELD_ENR_KEY)
.ok_or("ENR attestation bitfield non-existent")?;
BitVector::<TSpec::SubnetBitfieldLength>::from_ssz_bytes(bitfield_bytes)
BitVector::<E::SubnetBitfieldLength>::from_ssz_bytes(bitfield_bytes)
.map_err(|_| "Could not decode the ENR attnets bitfield")
}
fn sync_committee_bitfield<TSpec: EthSpec>(
fn sync_committee_bitfield<E: EthSpec>(
&self,
) -> Result<EnrSyncCommitteeBitfield<TSpec>, &'static str> {
) -> Result<EnrSyncCommitteeBitfield<E>, &'static str> {
let bitfield_bytes = self
.get(SYNC_COMMITTEE_BITFIELD_ENR_KEY)
.ok_or("ENR sync committee bitfield non-existent")?;
BitVector::<TSpec::SyncCommitteeSubnetCount>::from_ssz_bytes(bitfield_bytes)
BitVector::<E::SyncCommitteeSubnetCount>::from_ssz_bytes(bitfield_bytes)
.map_err(|_| "Could not decode the ENR syncnets bitfield")
}
@@ -125,7 +121,7 @@ pub fn use_or_load_enr(
///
/// If an ENR exists, with the same NodeId, this function checks to see if the loaded ENR from
/// disk is suitable to use, otherwise we increment our newly generated ENR's sequence number.
pub fn build_or_load_enr<T: EthSpec>(
pub fn build_or_load_enr<E: EthSpec>(
local_key: Keypair,
config: &NetworkConfig,
enr_fork_id: &EnrForkId,
@@ -135,14 +131,14 @@ pub fn build_or_load_enr<T: EthSpec>(
// Note: Discovery should update the ENR record's IP to the external IP as seen by the
// majority of our peers, if the CLI doesn't expressly forbid it.
let enr_key = CombinedKey::from_libp2p(local_key)?;
let mut local_enr = build_enr::<T>(&enr_key, config, enr_fork_id)?;
let mut local_enr = build_enr::<E>(&enr_key, config, enr_fork_id)?;
use_or_load_enr(&enr_key, &mut local_enr, config, log)?;
Ok(local_enr)
}
/// Builds a lighthouse ENR given a `NetworkConfig`.
pub fn build_enr<T: EthSpec>(
pub fn build_enr<E: EthSpec>(
enr_key: &CombinedKey,
config: &NetworkConfig,
enr_fork_id: &EnrForkId,
@@ -216,12 +212,12 @@ pub fn build_enr<T: EthSpec>(
builder.add_value(ETH2_ENR_KEY, &enr_fork_id.as_ssz_bytes());
// set the "attnets" field on our ENR
let bitfield = BitVector::<T::SubnetBitfieldLength>::new();
let bitfield = BitVector::<E::SubnetBitfieldLength>::new();
builder.add_value(ATTESTATION_BITFIELD_ENR_KEY, &bitfield.as_ssz_bytes());
// set the "syncnets" field on our ENR
let bitfield = BitVector::<T::SyncCommitteeSubnetCount>::new();
let bitfield = BitVector::<E::SyncCommitteeSubnetCount>::new();
builder.add_value(SYNC_COMMITTEE_BITFIELD_ENR_KEY, &bitfield.as_ssz_bytes());

View File

@@ -154,7 +154,7 @@ enum EventStream {
/// The main discovery service. This can be disabled via CLI arguements. When disabled the
/// underlying processes are not started, but this struct still maintains our current ENR.
pub struct Discovery<TSpec: EthSpec> {
pub struct Discovery<E: EthSpec> {
/// A collection of seen live ENRs for quick lookup and to map peer-id's to ENRs.
cached_enrs: LruCache<PeerId, Enr>,
@@ -168,7 +168,7 @@ pub struct Discovery<TSpec: EthSpec> {
discv5: Discv5,
/// A collection of network constants that can be read from other threads.
network_globals: Arc<NetworkGlobals<TSpec>>,
network_globals: Arc<NetworkGlobals<E>>,
/// Indicates if we are actively searching for peers. We only allow a single FindPeers query at
/// a time, regardless of the query concurrency.
@@ -194,12 +194,12 @@ pub struct Discovery<TSpec: EthSpec> {
log: slog::Logger,
}
impl<TSpec: EthSpec> Discovery<TSpec> {
impl<E: EthSpec> Discovery<E> {
/// NOTE: Creating discovery requires running within a tokio execution environment.
pub async fn new(
local_key: Keypair,
config: &NetworkConfig,
network_globals: Arc<NetworkGlobals<TSpec>>,
network_globals: Arc<NetworkGlobals<E>>,
log: &slog::Logger,
) -> error::Result<Self> {
let log = log.clone();
@@ -448,7 +448,7 @@ impl<TSpec: EthSpec> Discovery<TSpec> {
match subnet {
Subnet::Attestation(id) => {
let id = *id as usize;
let mut current_bitfield = local_enr.attestation_bitfield::<TSpec>()?;
let mut current_bitfield = local_enr.attestation_bitfield::<E>()?;
if id >= current_bitfield.len() {
return Err(format!(
"Subnet id: {} is outside the ENR bitfield length: {}",
@@ -481,7 +481,7 @@ impl<TSpec: EthSpec> Discovery<TSpec> {
}
Subnet::SyncCommittee(id) => {
let id = *id as usize;
let mut current_bitfield = local_enr.sync_committee_bitfield::<TSpec>()?;
let mut current_bitfield = local_enr.sync_committee_bitfield::<E>()?;
if id >= current_bitfield.len() {
return Err(format!(
@@ -718,7 +718,7 @@ impl<TSpec: EthSpec> Discovery<TSpec> {
// Only start a discovery query if we have a subnet to look for.
if !filtered_subnet_queries.is_empty() {
// build the subnet predicate as a combination of the eth2_fork_predicate and the subnet predicate
let subnet_predicate = subnet_predicate::<TSpec>(filtered_subnets, &self.log);
let subnet_predicate = subnet_predicate::<E>(filtered_subnets, &self.log);
debug!(
self.log,
@@ -845,7 +845,7 @@ impl<TSpec: EthSpec> Discovery<TSpec> {
// Check the specific subnet against the enr
let subnet_predicate =
subnet_predicate::<TSpec>(vec![query.subnet], &self.log);
subnet_predicate::<E>(vec![query.subnet], &self.log);
r.clone()
.into_iter()
@@ -916,7 +916,7 @@ impl<TSpec: EthSpec> Discovery<TSpec> {
/* NetworkBehaviour Implementation */
impl<TSpec: EthSpec> NetworkBehaviour for Discovery<TSpec> {
impl<E: EthSpec> NetworkBehaviour for Discovery<E> {
// Discovery is not a real NetworkBehaviour...
type ConnectionHandler = ConnectionHandler;
type ToSwarm = DiscoveredPeers;
@@ -1116,7 +1116,7 @@ impl<TSpec: EthSpec> NetworkBehaviour for Discovery<TSpec> {
}
}
impl<TSpec: EthSpec> Discovery<TSpec> {
impl<E: EthSpec> Discovery<E> {
fn on_dial_failure(&mut self, peer_id: Option<PeerId>, error: &DialError) {
if let Some(peer_id) = peer_id {
match error {

View File

@@ -5,26 +5,23 @@ use slog::trace;
use std::ops::Deref;
/// Returns the predicate for a given subnet.
pub fn subnet_predicate<TSpec>(
subnets: Vec<Subnet>,
log: &slog::Logger,
) -> impl Fn(&Enr) -> bool + Send
pub fn subnet_predicate<E>(subnets: Vec<Subnet>, log: &slog::Logger) -> impl Fn(&Enr) -> bool + Send
where
TSpec: EthSpec,
E: EthSpec,
{
let log_clone = log.clone();
move |enr: &Enr| {
let attestation_bitfield: EnrAttestationBitfield<TSpec> =
match enr.attestation_bitfield::<TSpec>() {
Ok(b) => b,
Err(_e) => return false,
};
let attestation_bitfield: EnrAttestationBitfield<E> = match enr.attestation_bitfield::<E>()
{
Ok(b) => b,
Err(_e) => return false,
};
// Pre-fork/fork-boundary enrs may not contain a syncnets field.
// Don't return early here
let sync_committee_bitfield: Result<EnrSyncCommitteeBitfield<TSpec>, _> =
enr.sync_committee_bitfield::<TSpec>();
let sync_committee_bitfield: Result<EnrSyncCommitteeBitfield<E>, _> =
enr.sync_committee_bitfield::<E>();
let predicate = subnets.iter().any(|subnet| match subnet {
Subnet::Attestation(s) => attestation_bitfield