Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
realbigsean committed May 7, 2024
1 parent 75ab913 commit 5728f78
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 48 deletions.
22 changes: 5 additions & 17 deletions consensus/state_processing/src/per_block_processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,16 +521,8 @@ pub fn get_expected_withdrawals<E: EthSpec>(
break;
}

let withdrawal_balance = state
.balances()
.get(withdrawal.index as usize)
.copied()
.ok_or(BeaconStateError::BalancesOutOfBounds(
withdrawal.index as usize,
))?;
let validator = state.validators().get(withdrawal.index as usize).ok_or(
BeaconStateError::UnknownValidator(withdrawal.index as usize),
)?;
let withdrawal_balance = state.get_balance(withdrawal.index as usize)?;
let validator = state.get_validator(withdrawal.index as usize)?;

let has_sufficient_effective_balance =
validator.effective_balance >= spec.min_activation_balance;
Expand All @@ -547,13 +539,9 @@ pub fn get_expected_withdrawals<E: EthSpec>(
withdrawals.push(Withdrawal {
index: withdrawal_index,
validator_index: withdrawal.index,
address: Address::from_slice(
validator
.withdrawal_credentials
.0
.get(12..)
.ok_or(BeaconStateError::IndexNotSupported(12))?,
),
address: validator
.get_execution_withdrawal_address(spec)
.ok_or(BeaconStateError::NonExecutionAddresWithdrawalCredential)?,
amount: withdrawable_balance,
});
withdrawal_index.safe_add_assign(1)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,19 +550,15 @@ pub fn process_execution_layer_withdrawal_requests<E: EthSpec>(
continue;
};

let validator = state
.validators()
.get(index)
.ok_or(BeaconStateError::UnknownValidator(index))?;
let validator = state.get_validator(index)?;

// Verify withdrawal credentials
let has_correct_credential = validator.has_execution_withdrawal_credential(spec);
let is_correct_source_address = validator
.withdrawal_credentials
.as_bytes()
.get(12..)
.ok_or(BeaconStateError::IndexNotSupported(12))?
== request.source_address.as_bytes();
.get_execution_withdrawal_address(spec)
.ok_or(BeaconStateError::NonExecutionAddresWithdrawalCredential)?
== request.source_address;

if !(has_correct_credential && is_correct_source_address) {
continue;
}
Expand Down Expand Up @@ -595,10 +591,7 @@ pub fn process_execution_layer_withdrawal_requests<E: EthSpec>(
continue;
}

let balance = *state
.balances()
.get(index)
.ok_or(BeaconStateError::UnknownValidator(index))?;
let balance = state.get_balance(index)?;
let has_sufficient_effective_balance =
validator.effective_balance >= spec.min_activation_balance;
let has_excess_balance = balance
Expand Down Expand Up @@ -692,18 +685,8 @@ pub fn process_consolidations<E: EthSpec>(
}
}

let source_validator = state
.validators()
.get(consolidation.source_index as usize)
.ok_or(BeaconStateError::UnknownValidator(
consolidation.source_index as usize,
))?;
let target_validator = state
.validators()
.get(consolidation.target_index as usize)
.ok_or(BeaconStateError::UnknownValidator(
consolidation.target_index as usize,
))?;
let source_validator = state.get_validator(consolidation.source_index as usize)?;
let target_validator = state.get_validator(consolidation.target_index as usize)?;

// Verify the source and the target are active
let current_epoch = state.current_epoch();
Expand Down Expand Up @@ -790,12 +773,7 @@ pub fn process_consolidations<E: EthSpec>(
source_validator.effective_balance,
spec,
)?;
let source_validator = state
.validators_mut()
.get_mut(consolidation.source_index as usize)
.ok_or(BeaconStateError::UnknownValidator(
consolidation.source_index as usize,
))?;
let source_validator = state.get_validator_mut(consolidation.source_index as usize)?;
// Initiate source validator exit and append pending consolidation
source_validator.exit_epoch = exit_epoch;
source_validator.withdrawable_epoch = source_validator
Expand Down
8 changes: 8 additions & 0 deletions consensus/types/src/beacon_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1469,6 +1469,14 @@ impl<E: EthSpec> BeaconState<E> {
}
}

/// Get the balance of a single validator.
pub fn get_balance(&self, validator_index: usize) -> Result<u64, Error> {
self.balances()
.get(validator_index)
.ok_or(Error::BalancesOutOfBounds(validator_index))
.copied()
}

/// Get a mutable reference to the balance of a single validator.
pub fn get_balance_mut(&mut self, validator_index: usize) -> Result<&mut u64, Error> {
self.balances_mut()
Expand Down

0 comments on commit 5728f78

Please sign in to comment.