Skip to content

Commit

Permalink
add electra fork enabled fn to ForkName impl (#36)
Browse files Browse the repository at this point in the history
* add electra fork enabled fn to ForkName impl

* remove inadvertent file
  • Loading branch information
eserilev authored and dapplion committed Jun 19, 2024
1 parent 9e84779 commit 7af3f2e
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 21 deletions.
5 changes: 4 additions & 1 deletion beacon_node/beacon_chain/src/attestation_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1341,7 +1341,10 @@ pub fn verify_committee_index<E: EthSpec>(
attestation: AttestationRef<E>,
spec: &ChainSpec,
) -> Result<(), Error> {
if spec.fork_name_at_slot::<E>(attestation.data().slot) >= ForkName::Electra {
if spec
.fork_name_at_slot::<E>(attestation.data().slot)
.electra_enabled()
{
// Check to ensure that the attestation is for a single committee.
let num_committee_bits = get_committee_indices::<E>(
attestation
Expand Down
6 changes: 5 additions & 1 deletion beacon_node/beacon_chain/src/beacon_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1994,7 +1994,11 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
};
drop(cache_timer);

if self.spec.fork_name_at_slot::<T::EthSpec>(request_slot) >= ForkName::Electra {
if self
.spec
.fork_name_at_slot::<T::EthSpec>(request_slot)
.electra_enabled()
{
let mut committee_bits = BitVector::default();
if committee_len > 0 {
committee_bits.set(request_index as usize, true)?;
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/beacon_chain/src/early_attester_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl<E: EthSpec> EarlyAttesterCache<E> {
item.committee_lengths
.get_committee_length::<E>(request_slot, request_index, spec)?;

let attestation = if spec.fork_name_at_slot::<E>(request_slot) >= ForkName::Electra {
let attestation = if spec.fork_name_at_slot::<E>(request_slot).electra_enabled() {
let mut committee_bits = BitVector::default();
if committee_len > 0 {
committee_bits
Expand Down
12 changes: 6 additions & 6 deletions beacon_node/beacon_chain/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,7 @@ where
*state.get_block_root(target_slot)?
};

if self.spec.fork_name_at_slot::<E>(slot) >= ForkName::Electra {
if self.spec.fork_name_at_slot::<E>(slot).electra_enabled() {
let mut committee_bits = BitVector::default();
committee_bits.set(index as usize, true)?;
Ok(Attestation::Electra(AttestationElectra {
Expand Down Expand Up @@ -1376,7 +1376,7 @@ where

let fork_name = self.spec.fork_name_at_slot::<E>(slot);

let aggregate = if fork_name >= ForkName::Electra {
let aggregate = if fork_name.electra_enabled() {
self.chain
.get_aggregated_attestation_electra(
slot,
Expand Down Expand Up @@ -1541,7 +1541,7 @@ where

let fork_name = self.spec.fork_name_at_slot::<E>(Slot::new(0));

let mut attestation_1 = if fork_name >= ForkName::Electra {
let mut attestation_1 = if fork_name.electra_enabled() {
IndexedAttestation::Electra(IndexedAttestationElectra {
attesting_indices: VariableList::new(validator_indices).unwrap(),
data: AttestationData {
Expand Down Expand Up @@ -1623,7 +1623,7 @@ where
}
}

if fork_name >= ForkName::Electra {
if fork_name.electra_enabled() {
AttesterSlashing::Electra(AttesterSlashingElectra {
attestation_1: attestation_1.as_electra().unwrap().clone(),
attestation_2: attestation_2.as_electra().unwrap().clone(),
Expand Down Expand Up @@ -1657,7 +1657,7 @@ where
},
};

let (mut attestation_1, mut attestation_2) = if fork_name >= ForkName::Electra {
let (mut attestation_1, mut attestation_2) = if fork_name.electra_enabled() {
let attestation_1 = IndexedAttestationElectra {
attesting_indices: VariableList::new(validator_indices_1).unwrap(),
data: data.clone(),
Expand Down Expand Up @@ -1735,7 +1735,7 @@ where
}
}

if fork_name >= ForkName::Electra {
if fork_name.electra_enabled() {
AttesterSlashing::Electra(AttesterSlashingElectra {
attestation_1: attestation_1.as_electra().unwrap().clone(),
attestation_2: attestation_2.as_electra().unwrap().clone(),
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/beacon_chain/tests/block_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ async fn invalid_signature_attester_slashing() {
let mut snapshots = chain_segment.clone();
let fork_name = harness.chain.spec.fork_name_at_slot::<E>(Slot::new(0));

let attester_slashing = if fork_name >= ForkName::Electra {
let attester_slashing = if fork_name.electra_enabled() {
let indexed_attestation = IndexedAttestationElectra {
attesting_indices: vec![0].into(),
data: AttestationData {
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/beacon_chain/tests/store_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ async fn multiple_attestations_per_block() {
let slot = snapshot.beacon_block.slot();
let fork_name = harness.chain.spec.fork_name_at_slot::<E>(slot);

if fork_name >= ForkName::Electra {
if fork_name.electra_enabled() {
assert_eq!(
snapshot
.beacon_block
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/client/src/notifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ async fn electra_readiness_logging<T: BeaconChainTypes>(
.snapshot
.beacon_state
.fork_name_unchecked()
>= ForkName::Electra;
.electra_enabled();

let has_execution_layer = beacon_chain.execution_layer.is_some();

Expand Down
2 changes: 1 addition & 1 deletion beacon_node/operation_pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ impl<E: EthSpec> OperationPool<E> {
// TODO(electra): Work out how to do this more elegantly. This is a bit of a hack.
let mut all_attestations = self.attestations.write();

if fork_name >= ForkName::Electra {
if fork_name.electra_enabled() {
all_attestations.aggregate_across_committees(prev_epoch_key);
all_attestations.aggregate_across_committees(curr_epoch_key);
}
Expand Down
3 changes: 1 addition & 2 deletions consensus/types/src/attestation.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::slot_data::SlotData;
use crate::ForkName;
use crate::{test_utils::TestRandom, Hash256, Slot};
use derivative::Derivative;
use safe_arith::ArithError;
Expand Down Expand Up @@ -95,7 +94,7 @@ impl<E: EthSpec> Attestation<E> {
data: AttestationData,
spec: &ChainSpec,
) -> Result<Self, Error> {
if spec.fork_name_at_slot::<E>(data.slot) >= ForkName::Electra {
if spec.fork_name_at_slot::<E>(data.slot).electra_enabled() {
let mut committee_bits: BitVector<E::MaxCommitteesPerSlot> = BitVector::default();
committee_bits
.set(committee_index, true)
Expand Down
2 changes: 1 addition & 1 deletion consensus/types/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ impl ChainSpec {
state: &BeaconState<E>,
) -> u64 {
let fork_name = state.fork_name_unchecked();
if fork_name >= ForkName::Electra {
if fork_name.electra_enabled() {
self.min_slashing_penalty_quotient_electra
} else if fork_name >= ForkName::Bellatrix {
self.min_slashing_penalty_quotient_bellatrix
Expand Down
4 changes: 4 additions & 0 deletions consensus/types/src/fork_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ impl ForkName {
ForkName::Electra => None,
}
}

pub fn electra_enabled(self) -> bool {
self >= ForkName::Electra
}
}

/// Map a fork name into a fork-versioned superstruct type like `BeaconBlock`.
Expand Down
6 changes: 3 additions & 3 deletions consensus/types/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Validator {
spec: &ChainSpec,
current_fork: ForkName,
) -> bool {
if current_fork >= ForkName::Electra {
if current_fork.electra_enabled() {
self.is_eligible_for_activation_queue_electra(spec)
} else {
self.is_eligible_for_activation_queue_base(spec)
Expand Down Expand Up @@ -162,7 +162,7 @@ impl Validator {
spec: &ChainSpec,
current_fork: ForkName,
) -> bool {
if current_fork >= ForkName::Electra {
if current_fork.electra_enabled() {
self.is_fully_withdrawable_at_electra(balance, epoch, spec)
} else {
self.is_fully_withdrawable_at_capella(balance, epoch, spec)
Expand Down Expand Up @@ -202,7 +202,7 @@ impl Validator {
spec: &ChainSpec,
current_fork: ForkName,
) -> bool {
if current_fork >= ForkName::Electra {
if current_fork.electra_enabled() {
self.is_partially_withdrawable_validator_electra(balance, spec)
} else {
self.is_partially_withdrawable_validator_capella(balance, spec)
Expand Down
4 changes: 2 additions & 2 deletions testing/ef_tests/src/cases/fork_choice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl<E: EthSpec> LoadCase for ForkChoiceTest<E> {
})
}
Step::Attestation { attestation } => {
if fork_name >= ForkName::Electra {
if fork_name.electra_enabled() {
ssz_decode_file(&path.join(format!("{}.ssz_snappy", attestation))).map(
|attestation| Step::Attestation {
attestation: Attestation::Electra(attestation),
Expand All @@ -195,7 +195,7 @@ impl<E: EthSpec> LoadCase for ForkChoiceTest<E> {
}
}
Step::AttesterSlashing { attester_slashing } => {
if fork_name >= ForkName::Electra {
if fork_name.electra_enabled() {
ssz_decode_file(&path.join(format!("{}.ssz_snappy", attester_slashing)))
.map(|attester_slashing| Step::AttesterSlashing {
attester_slashing: AttesterSlashing::Electra(attester_slashing),
Expand Down

0 comments on commit 7af3f2e

Please sign in to comment.