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

@@ -42,25 +42,25 @@ use types::{
SignedVoluntaryExit, Slot, SyncAggregate, SyncCommitteeContribution, Validator,
};
type SyncContributions<T> = RwLock<HashMap<SyncAggregateId, Vec<SyncCommitteeContribution<T>>>>;
type SyncContributions<E> = RwLock<HashMap<SyncAggregateId, Vec<SyncCommitteeContribution<E>>>>;
#[derive(Default, Debug)]
pub struct OperationPool<T: EthSpec + Default> {
pub struct OperationPool<E: EthSpec + Default> {
/// Map from attestation ID (see below) to vectors of attestations.
attestations: RwLock<AttestationMap<T>>,
attestations: RwLock<AttestationMap<E>>,
/// Map from sync aggregate ID to the best `SyncCommitteeContribution`s seen for that ID.
sync_contributions: SyncContributions<T>,
sync_contributions: SyncContributions<E>,
/// Set of attester slashings, and the fork version they were verified against.
attester_slashings: RwLock<HashSet<SigVerifiedOp<AttesterSlashing<T>, T>>>,
attester_slashings: RwLock<HashSet<SigVerifiedOp<AttesterSlashing<E>, E>>>,
/// Map from proposer index to slashing.
proposer_slashings: RwLock<HashMap<u64, SigVerifiedOp<ProposerSlashing, T>>>,
proposer_slashings: RwLock<HashMap<u64, SigVerifiedOp<ProposerSlashing, E>>>,
/// Map from exiting validator to their exit data.
voluntary_exits: RwLock<HashMap<u64, SigVerifiedOp<SignedVoluntaryExit, T>>>,
voluntary_exits: RwLock<HashMap<u64, SigVerifiedOp<SignedVoluntaryExit, E>>>,
/// Map from credential changing validator to their position in the queue.
bls_to_execution_changes: RwLock<BlsToExecutionChanges<T>>,
bls_to_execution_changes: RwLock<BlsToExecutionChanges<E>>,
/// Reward cache for accelerating attestation packing.
reward_cache: RwLock<RewardCache>,
_phantom: PhantomData<T>,
_phantom: PhantomData<E>,
}
#[derive(Debug, PartialEq)]
@@ -93,7 +93,7 @@ impl From<SyncAggregateError> for OpPoolError {
}
}
impl<T: EthSpec> OperationPool<T> {
impl<E: EthSpec> OperationPool<E> {
/// Create a new operation pool.
pub fn new() -> Self {
Self::default()
@@ -107,7 +107,7 @@ impl<T: EthSpec> OperationPool<T> {
/// This function assumes the given `contribution` is valid.
pub fn insert_sync_contribution(
&self,
contribution: SyncCommitteeContribution<T>,
contribution: SyncCommitteeContribution<E>,
) -> Result<(), OpPoolError> {
let aggregate_id = SyncAggregateId::new(contribution.slot, contribution.beacon_block_root);
let mut contributions = self.sync_contributions.write();
@@ -153,8 +153,8 @@ impl<T: EthSpec> OperationPool<T> {
/// contributions exist at this slot, or else `None`.
pub fn get_sync_aggregate(
&self,
state: &BeaconState<T>,
) -> Result<Option<SyncAggregate<T>>, OpPoolError> {
state: &BeaconState<E>,
) -> Result<Option<SyncAggregate<E>>, OpPoolError> {
// Sync aggregates are formed from the contributions from the previous slot.
let slot = state.slot().saturating_sub(1u64);
let block_root = *state
@@ -197,7 +197,7 @@ impl<T: EthSpec> OperationPool<T> {
/// This function assumes the given `attestation` is valid.
pub fn insert_attestation(
&self,
attestation: Attestation<T>,
attestation: Attestation<E>,
attesting_indices: Vec<u64>,
) -> Result<(), AttestationValidationError> {
self.attestations
@@ -220,18 +220,18 @@ impl<T: EthSpec> OperationPool<T> {
fn get_valid_attestations_for_epoch<'a>(
&'a self,
checkpoint_key: &'a CheckpointKey,
all_attestations: &'a AttestationMap<T>,
state: &'a BeaconState<T>,
all_attestations: &'a AttestationMap<E>,
state: &'a BeaconState<E>,
reward_cache: &'a RewardCache,
total_active_balance: u64,
validity_filter: impl FnMut(&AttestationRef<'a, T>) -> bool + Send,
validity_filter: impl FnMut(&AttestationRef<'a, E>) -> bool + Send,
spec: &'a ChainSpec,
) -> impl Iterator<Item = AttMaxCover<'a, T>> + Send {
) -> impl Iterator<Item = AttMaxCover<'a, E>> + Send {
all_attestations
.get_attestations(checkpoint_key)
.filter(|att| {
att.data.slot + spec.min_attestation_inclusion_delay <= state.slot()
&& state.slot() <= att.data.slot + T::slots_per_epoch()
&& state.slot() <= att.data.slot + E::slots_per_epoch()
})
.filter(validity_filter)
.filter_map(move |att| {
@@ -247,11 +247,11 @@ impl<T: EthSpec> OperationPool<T> {
/// in the operation pool.
pub fn get_attestations(
&self,
state: &BeaconState<T>,
prev_epoch_validity_filter: impl for<'a> FnMut(&AttestationRef<'a, T>) -> bool + Send,
curr_epoch_validity_filter: impl for<'a> FnMut(&AttestationRef<'a, T>) -> bool + Send,
state: &BeaconState<E>,
prev_epoch_validity_filter: impl for<'a> FnMut(&AttestationRef<'a, E>) -> bool + Send,
curr_epoch_validity_filter: impl for<'a> FnMut(&AttestationRef<'a, E>) -> bool + Send,
spec: &ChainSpec,
) -> Result<Vec<Attestation<T>>, OpPoolError> {
) -> Result<Vec<Attestation<E>>, OpPoolError> {
// Attestations for the current fork, which may be from the current or previous epoch.
let (prev_epoch_key, curr_epoch_key) = CheckpointKey::keys_for_state(state);
let all_attestations = self.attestations.read();
@@ -296,12 +296,12 @@ impl<T: EthSpec> OperationPool<T> {
let prev_epoch_limit = if let BeaconState::Base(base_state) = state {
std::cmp::min(
T::MaxPendingAttestations::to_usize()
E::MaxPendingAttestations::to_usize()
.saturating_sub(base_state.previous_epoch_attestations.len()),
T::MaxAttestations::to_usize(),
E::MaxAttestations::to_usize(),
)
} else {
T::MaxAttestations::to_usize()
E::MaxAttestations::to_usize()
};
let (prev_cover, curr_cover) = rayon::join(
@@ -318,7 +318,7 @@ impl<T: EthSpec> OperationPool<T> {
let _timer = metrics::start_timer(&metrics::ATTESTATION_CURR_EPOCH_PACKING_TIME);
maximum_cover(
curr_epoch_att,
T::MaxAttestations::to_usize(),
E::MaxAttestations::to_usize(),
"curr_epoch_attestations",
)
},
@@ -330,7 +330,7 @@ impl<T: EthSpec> OperationPool<T> {
Ok(max_cover::merge_solutions(
curr_cover,
prev_cover,
T::MaxAttestations::to_usize(),
E::MaxAttestations::to_usize(),
))
}
@@ -342,7 +342,7 @@ impl<T: EthSpec> OperationPool<T> {
/// Insert a proposer slashing into the pool.
pub fn insert_proposer_slashing(
&self,
verified_proposer_slashing: SigVerifiedOp<ProposerSlashing, T>,
verified_proposer_slashing: SigVerifiedOp<ProposerSlashing, E>,
) {
self.proposer_slashings.write().insert(
verified_proposer_slashing.as_inner().proposer_index(),
@@ -353,7 +353,7 @@ impl<T: EthSpec> OperationPool<T> {
/// Insert an attester slashing into the pool.
pub fn insert_attester_slashing(
&self,
verified_slashing: SigVerifiedOp<AttesterSlashing<T>, T>,
verified_slashing: SigVerifiedOp<AttesterSlashing<E>, E>,
) {
self.attester_slashings.write().insert(verified_slashing);
}
@@ -365,11 +365,11 @@ impl<T: EthSpec> OperationPool<T> {
/// earlier in the block.
pub fn get_slashings_and_exits(
&self,
state: &BeaconState<T>,
state: &BeaconState<E>,
spec: &ChainSpec,
) -> (
Vec<ProposerSlashing>,
Vec<AttesterSlashing<T>>,
Vec<AttesterSlashing<E>>,
Vec<SignedVoluntaryExit>,
) {
let proposer_slashings = filter_limit_operations(
@@ -382,7 +382,7 @@ impl<T: EthSpec> OperationPool<T> {
.map_or(false, |validator| !validator.slashed)
},
|slashing| slashing.as_inner().clone(),
T::MaxProposerSlashings::to_usize(),
E::MaxProposerSlashings::to_usize(),
);
// Set of validators to be slashed, so we don't attempt to construct invalid attester
@@ -408,9 +408,9 @@ impl<T: EthSpec> OperationPool<T> {
/// This function *must* remain private.
fn get_attester_slashings(
&self,
state: &BeaconState<T>,
state: &BeaconState<E>,
to_be_slashed: &mut HashSet<u64>,
) -> Vec<AttesterSlashing<T>> {
) -> Vec<AttesterSlashing<E>> {
let reader = self.attester_slashings.read();
let relevant_attester_slashings = reader.iter().flat_map(|slashing| {
@@ -423,7 +423,7 @@ impl<T: EthSpec> OperationPool<T> {
maximum_cover(
relevant_attester_slashings,
T::MaxAttesterSlashings::to_usize(),
E::MaxAttesterSlashings::to_usize(),
"attester_slashings",
)
.into_iter()
@@ -435,7 +435,7 @@ impl<T: EthSpec> OperationPool<T> {
}
/// Prune proposer slashings for validators which are exited in the finalized epoch.
pub fn prune_proposer_slashings(&self, head_state: &BeaconState<T>) {
pub fn prune_proposer_slashings(&self, head_state: &BeaconState<E>) {
prune_validator_hash_map(
&mut self.proposer_slashings.write(),
|_, validator| validator.exit_epoch <= head_state.finalized_checkpoint().epoch,
@@ -445,7 +445,7 @@ impl<T: EthSpec> OperationPool<T> {
/// Prune attester slashings for all slashed or withdrawn validators, or attestations on another
/// fork.
pub fn prune_attester_slashings(&self, head_state: &BeaconState<T>) {
pub fn prune_attester_slashings(&self, head_state: &BeaconState<E>) {
self.attester_slashings.write().retain(|slashing| {
// Check that the attestation's signature is still valid wrt the fork version.
let signature_ok = slashing.signature_is_still_valid(&head_state.fork());
@@ -476,7 +476,7 @@ impl<T: EthSpec> OperationPool<T> {
}
/// Insert a voluntary exit that has previously been checked elsewhere.
pub fn insert_voluntary_exit(&self, exit: SigVerifiedOp<SignedVoluntaryExit, T>) {
pub fn insert_voluntary_exit(&self, exit: SigVerifiedOp<SignedVoluntaryExit, E>) {
self.voluntary_exits
.write()
.insert(exit.as_inner().message.validator_index, exit);
@@ -485,7 +485,7 @@ impl<T: EthSpec> OperationPool<T> {
/// Get a list of voluntary exits for inclusion in a block.
fn get_voluntary_exits<F>(
&self,
state: &BeaconState<T>,
state: &BeaconState<E>,
filter: F,
spec: &ChainSpec,
) -> Vec<SignedVoluntaryExit>
@@ -501,12 +501,12 @@ impl<T: EthSpec> OperationPool<T> {
.is_ok()
},
|exit| exit.as_inner().clone(),
T::MaxVoluntaryExits::to_usize(),
E::MaxVoluntaryExits::to_usize(),
)
}
/// Prune if validator has already exited at or before the finalized checkpoint of the head.
pub fn prune_voluntary_exits(&self, head_state: &BeaconState<T>) {
pub fn prune_voluntary_exits(&self, head_state: &BeaconState<E>) {
prune_validator_hash_map(
&mut self.voluntary_exits.write(),
// This condition is slightly too loose, since there will be some finalized exits that
@@ -536,7 +536,7 @@ impl<T: EthSpec> OperationPool<T> {
/// Return `true` if the change was inserted.
pub fn insert_bls_to_execution_change(
&self,
verified_change: SigVerifiedOp<SignedBlsToExecutionChange, T>,
verified_change: SigVerifiedOp<SignedBlsToExecutionChange, E>,
received_pre_capella: ReceivedPreCapella,
) -> bool {
self.bls_to_execution_changes
@@ -549,7 +549,7 @@ impl<T: EthSpec> OperationPool<T> {
/// They're in random `HashMap` order, which isn't exactly fair, but isn't unfair either.
pub fn get_bls_to_execution_changes(
&self,
state: &BeaconState<T>,
state: &BeaconState<E>,
spec: &ChainSpec,
) -> Vec<SignedBlsToExecutionChange> {
filter_limit_operations(
@@ -563,7 +563,7 @@ impl<T: EthSpec> OperationPool<T> {
})
},
|address_change| address_change.as_inner().clone(),
T::MaxBlsToExecutionChanges::to_usize(),
E::MaxBlsToExecutionChanges::to_usize(),
)
}
@@ -573,7 +573,7 @@ impl<T: EthSpec> OperationPool<T> {
/// broadcast of messages.
pub fn get_bls_to_execution_changes_received_pre_capella(
&self,
state: &BeaconState<T>,
state: &BeaconState<E>,
spec: &ChainSpec,
) -> Vec<SignedBlsToExecutionChange> {
let mut changes = filter_limit_operations(
@@ -604,10 +604,10 @@ impl<T: EthSpec> OperationPool<T> {
}
/// Prune BLS to execution changes that have been applied to the state more than 1 block ago.
pub fn prune_bls_to_execution_changes<Payload: AbstractExecPayload<T>>(
pub fn prune_bls_to_execution_changes<Payload: AbstractExecPayload<E>>(
&self,
head_block: &SignedBeaconBlock<T, Payload>,
head_state: &BeaconState<T>,
head_block: &SignedBeaconBlock<E, Payload>,
head_state: &BeaconState<E>,
spec: &ChainSpec,
) {
self.bls_to_execution_changes
@@ -616,10 +616,10 @@ impl<T: EthSpec> OperationPool<T> {
}
/// Prune all types of transactions given the latest head state and head fork.
pub fn prune_all<Payload: AbstractExecPayload<T>>(
pub fn prune_all<Payload: AbstractExecPayload<E>>(
&self,
head_block: &SignedBeaconBlock<T, Payload>,
head_state: &BeaconState<T>,
head_block: &SignedBeaconBlock<E, Payload>,
head_state: &BeaconState<E>,
current_epoch: Epoch,
spec: &ChainSpec,
) {
@@ -639,7 +639,7 @@ impl<T: EthSpec> OperationPool<T> {
/// Returns all known `Attestation` objects.
///
/// This method may return objects that are invalid for block inclusion.
pub fn get_all_attestations(&self) -> Vec<Attestation<T>> {
pub fn get_all_attestations(&self) -> Vec<Attestation<E>> {
self.attestations
.read()
.iter()
@@ -650,7 +650,7 @@ impl<T: EthSpec> OperationPool<T> {
/// Returns all known `Attestation` objects that pass the provided filter.
///
/// This method may return objects that are invalid for block inclusion.
pub fn get_filtered_attestations<F>(&self, filter: F) -> Vec<Attestation<T>>
pub fn get_filtered_attestations<F>(&self, filter: F) -> Vec<Attestation<E>>
where
F: Fn(&AttestationData) -> bool,
{
@@ -665,7 +665,7 @@ impl<T: EthSpec> OperationPool<T> {
/// Returns all known `AttesterSlashing` objects.
///
/// This method may return objects that are invalid for block inclusion.
pub fn get_all_attester_slashings(&self) -> Vec<AttesterSlashing<T>> {
pub fn get_all_attester_slashings(&self) -> Vec<AttesterSlashing<E>> {
self.attester_slashings
.read()
.iter()
@@ -751,7 +751,7 @@ fn prune_validator_hash_map<T, F, E: EthSpec>(
}
/// Compare two operation pools.
impl<T: EthSpec + Default> PartialEq for OperationPool<T> {
impl<E: EthSpec + Default> PartialEq for OperationPool<E> {
fn eq(&self, other: &Self) -> bool {
if ptr::eq(self, other) {
return true;