Skip to content

Commit 266b241

Browse files
authored
Electra minor refactorings (#6839)
N/A Fix some typos and other minor refactorings in the electra code. Thanks @jtraglia for bringing them up. Note to reviewiers: 4780349 is the commit that needs looking into in detail. The rest are very minor refactorings
1 parent 54e3709 commit 266b241

File tree

6 files changed

+33
-62
lines changed

6 files changed

+33
-62
lines changed

beacon_node/operation_pool/src/attestation_storage.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ impl<E: EthSpec> CompactIndexedAttestationElectra<E> {
214214
.is_zero()
215215
}
216216

217-
/// Returns `true` if aggregated, otherwise `false`.
217+
/// Returns `true` if aggregated, otherwise `false`.
218218
pub fn aggregate_same_committee(&mut self, other: &Self) -> bool {
219219
if self.committee_bits != other.committee_bits {
220220
return false;

consensus/state_processing/src/per_block_processing.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -523,9 +523,9 @@ pub fn get_expected_withdrawals<E: EthSpec>(
523523
// [New in Electra:EIP7251]
524524
// Consume pending partial withdrawals
525525
let processed_partial_withdrawals_count =
526-
if let Ok(partial_withdrawals) = state.pending_partial_withdrawals() {
526+
if let Ok(pending_partial_withdrawals) = state.pending_partial_withdrawals() {
527527
let mut processed_partial_withdrawals_count = 0;
528-
for withdrawal in partial_withdrawals {
528+
for withdrawal in pending_partial_withdrawals {
529529
if withdrawal.withdrawable_epoch > epoch
530530
|| withdrawals.len() == spec.max_pending_partials_per_withdrawals_sweep as usize
531531
{
@@ -552,7 +552,7 @@ pub fn get_expected_withdrawals<E: EthSpec>(
552552
validator_index: withdrawal.validator_index,
553553
address: validator
554554
.get_execution_withdrawal_address(spec)
555-
.ok_or(BeaconStateError::NonExecutionAddresWithdrawalCredential)?,
555+
.ok_or(BeaconStateError::NonExecutionAddressWithdrawalCredential)?,
556556
amount: withdrawable_balance,
557557
});
558558
withdrawal_index.safe_add_assign(1)?;
@@ -583,7 +583,7 @@ pub fn get_expected_withdrawals<E: EthSpec>(
583583
validator_index as usize,
584584
))?
585585
.safe_sub(partially_withdrawn_balance)?;
586-
if validator.is_fully_withdrawable_at(balance, epoch, spec, fork_name) {
586+
if validator.is_fully_withdrawable_validator(balance, epoch, spec, fork_name) {
587587
withdrawals.push(Withdrawal {
588588
index: withdrawal_index,
589589
validator_index,
@@ -600,9 +600,7 @@ pub fn get_expected_withdrawals<E: EthSpec>(
600600
address: validator
601601
.get_execution_withdrawal_address(spec)
602602
.ok_or(BlockProcessingError::WithdrawalCredentialsInvalid)?,
603-
amount: balance.safe_sub(
604-
validator.get_max_effective_balance(spec, state.fork_name_unchecked()),
605-
)?,
603+
amount: balance.safe_sub(validator.get_max_effective_balance(spec, fork_name))?,
606604
});
607605
withdrawal_index.safe_add_assign(1)?;
608606
}
@@ -624,7 +622,7 @@ pub fn process_withdrawals<E: EthSpec, Payload: AbstractExecPayload<E>>(
624622
spec: &ChainSpec,
625623
) -> Result<(), BlockProcessingError> {
626624
if state.fork_name_unchecked().capella_enabled() {
627-
let (expected_withdrawals, partial_withdrawals_count) =
625+
let (expected_withdrawals, processed_partial_withdrawals_count) =
628626
get_expected_withdrawals(state, spec)?;
629627
let expected_root = expected_withdrawals.tree_hash_root();
630628
let withdrawals_root = payload.withdrawals_root()?;
@@ -645,14 +643,10 @@ pub fn process_withdrawals<E: EthSpec, Payload: AbstractExecPayload<E>>(
645643
}
646644

647645
// Update pending partial withdrawals [New in Electra:EIP7251]
648-
if let Some(partial_withdrawals_count) = partial_withdrawals_count {
649-
// TODO(electra): Use efficient pop_front after milhouse release https://github.com/sigp/milhouse/pull/38
650-
let new_partial_withdrawals = state
651-
.pending_partial_withdrawals()?
652-
.iter_from(partial_withdrawals_count)?
653-
.cloned()
654-
.collect::<Vec<_>>();
655-
*state.pending_partial_withdrawals_mut()? = List::new(new_partial_withdrawals)?;
646+
if let Some(processed_partial_withdrawals_count) = processed_partial_withdrawals_count {
647+
state
648+
.pending_partial_withdrawals_mut()?
649+
.pop_front(processed_partial_withdrawals_count)?;
656650
}
657651

658652
// Update the next withdrawal index if this block contained withdrawals

consensus/state_processing/src/per_epoch_processing/single_pass.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,13 +1075,9 @@ fn process_pending_consolidations<E: EthSpec>(
10751075
next_pending_consolidation.safe_add_assign(1)?;
10761076
}
10771077

1078-
let new_pending_consolidations = List::try_from_iter(
1079-
state
1080-
.pending_consolidations()?
1081-
.iter_from(next_pending_consolidation)?
1082-
.cloned(),
1083-
)?;
1084-
*state.pending_consolidations_mut()? = new_pending_consolidations;
1078+
state
1079+
.pending_consolidations_mut()?
1080+
.pop_front(next_pending_consolidation)?;
10851081

10861082
// the spec tests require we don't perform effective balance updates when testing pending_consolidations
10871083
if !perform_effective_balance_updates {

consensus/state_processing/src/upgrade/electra.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ pub fn upgrade_to_electra<E: EthSpec>(
4747
.enumerate()
4848
.filter(|(_, validator)| validator.activation_epoch == spec.far_future_epoch)
4949
.sorted_by_key(|(index, validator)| (validator.activation_eligibility_epoch, *index))
50+
.map(|(index, _)| index)
5051
.collect::<Vec<_>>();
5152

5253
// Process validators to queue entire balance and reset them
53-
for (index, _) in pre_activation {
54+
for index in pre_activation {
5455
let balance = post
5556
.balances_mut()
5657
.get_mut(index)

consensus/types/src/beacon_state.rs

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ pub enum Error {
161161
InvalidFlagIndex(usize),
162162
MerkleTreeError(merkle_proof::MerkleTreeError),
163163
PartialWithdrawalCountInvalid(usize),
164-
NonExecutionAddresWithdrawalCredential,
164+
NonExecutionAddressWithdrawalCredential,
165165
NoCommitteeFound(CommitteeIndex),
166166
InvalidCommitteeIndex(CommitteeIndex),
167167
InvalidSelectionProof {
@@ -2214,7 +2214,7 @@ impl<E: EthSpec> BeaconState<E> {
22142214

22152215
// ******* Electra accessors *******
22162216

2217-
/// Return the churn limit for the current epoch.
2217+
/// Return the churn limit for the current epoch.
22182218
pub fn get_balance_churn_limit(&self, spec: &ChainSpec) -> Result<u64, Error> {
22192219
let total_active_balance = self.get_total_active_balance()?;
22202220
let churn = std::cmp::max(
@@ -2329,21 +2329,12 @@ impl<E: EthSpec> BeaconState<E> {
23292329
| BeaconState::Bellatrix(_)
23302330
| BeaconState::Capella(_)
23312331
| BeaconState::Deneb(_) => Err(Error::IncorrectStateVariant),
2332-
BeaconState::Electra(_) => {
2333-
let state = self.as_electra_mut()?;
2334-
2332+
BeaconState::Electra(_) | BeaconState::Fulu(_) => {
23352333
// Consume the balance and update state variables
2336-
state.exit_balance_to_consume = exit_balance_to_consume.safe_sub(exit_balance)?;
2337-
state.earliest_exit_epoch = earliest_exit_epoch;
2338-
Ok(state.earliest_exit_epoch)
2339-
}
2340-
BeaconState::Fulu(_) => {
2341-
let state = self.as_fulu_mut()?;
2342-
2343-
// Consume the balance and update state variables
2344-
state.exit_balance_to_consume = exit_balance_to_consume.safe_sub(exit_balance)?;
2345-
state.earliest_exit_epoch = earliest_exit_epoch;
2346-
Ok(state.earliest_exit_epoch)
2334+
*self.exit_balance_to_consume_mut()? =
2335+
exit_balance_to_consume.safe_sub(exit_balance)?;
2336+
*self.earliest_exit_epoch_mut()? = earliest_exit_epoch;
2337+
self.earliest_exit_epoch()
23472338
}
23482339
}
23492340
}
@@ -2385,23 +2376,12 @@ impl<E: EthSpec> BeaconState<E> {
23852376
| BeaconState::Bellatrix(_)
23862377
| BeaconState::Capella(_)
23872378
| BeaconState::Deneb(_) => Err(Error::IncorrectStateVariant),
2388-
BeaconState::Electra(_) => {
2389-
let state = self.as_electra_mut()?;
2390-
2391-
// Consume the balance and update state variables.
2392-
state.consolidation_balance_to_consume =
2393-
consolidation_balance_to_consume.safe_sub(consolidation_balance)?;
2394-
state.earliest_consolidation_epoch = earliest_consolidation_epoch;
2395-
Ok(state.earliest_consolidation_epoch)
2396-
}
2397-
BeaconState::Fulu(_) => {
2398-
let state = self.as_fulu_mut()?;
2399-
2379+
BeaconState::Electra(_) | BeaconState::Fulu(_) => {
24002380
// Consume the balance and update state variables.
2401-
state.consolidation_balance_to_consume =
2381+
*self.consolidation_balance_to_consume_mut()? =
24022382
consolidation_balance_to_consume.safe_sub(consolidation_balance)?;
2403-
state.earliest_consolidation_epoch = earliest_consolidation_epoch;
2404-
Ok(state.earliest_consolidation_epoch)
2383+
*self.earliest_consolidation_epoch_mut()? = earliest_consolidation_epoch;
2384+
self.earliest_consolidation_epoch()
24052385
}
24062386
}
24072387
}

consensus/types/src/validator.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl Validator {
5656
};
5757

5858
let max_effective_balance = validator.get_max_effective_balance(spec, fork_name);
59-
// safe math is unnecessary here since the spec.effecive_balance_increment is never <= 0
59+
// safe math is unnecessary here since the spec.effective_balance_increment is never <= 0
6060
validator.effective_balance = std::cmp::min(
6161
amount - (amount % spec.effective_balance_increment),
6262
max_effective_balance,
@@ -195,22 +195,22 @@ impl Validator {
195195
/// Returns `true` if the validator is fully withdrawable at some epoch.
196196
///
197197
/// Calls the correct function depending on the provided `fork_name`.
198-
pub fn is_fully_withdrawable_at(
198+
pub fn is_fully_withdrawable_validator(
199199
&self,
200200
balance: u64,
201201
epoch: Epoch,
202202
spec: &ChainSpec,
203203
current_fork: ForkName,
204204
) -> bool {
205205
if current_fork.electra_enabled() {
206-
self.is_fully_withdrawable_at_electra(balance, epoch, spec)
206+
self.is_fully_withdrawable_validator_electra(balance, epoch, spec)
207207
} else {
208-
self.is_fully_withdrawable_at_capella(balance, epoch, spec)
208+
self.is_fully_withdrawable_validator_capella(balance, epoch, spec)
209209
}
210210
}
211211

212212
/// Returns `true` if the validator is fully withdrawable at some epoch.
213-
fn is_fully_withdrawable_at_capella(
213+
fn is_fully_withdrawable_validator_capella(
214214
&self,
215215
balance: u64,
216216
epoch: Epoch,
@@ -222,7 +222,7 @@ impl Validator {
222222
/// Returns `true` if the validator is fully withdrawable at some epoch.
223223
///
224224
/// Modified in electra as part of EIP 7251.
225-
fn is_fully_withdrawable_at_electra(
225+
fn is_fully_withdrawable_validator_electra(
226226
&self,
227227
balance: u64,
228228
epoch: Epoch,

0 commit comments

Comments
 (0)