This commit is contained in:
realbigsean
2024-05-07 16:02:14 -04:00
parent 75ab913a3a
commit 5728f78032
3 changed files with 22 additions and 48 deletions

View File

@@ -521,16 +521,8 @@ pub fn get_expected_withdrawals<E: EthSpec>(
break; break;
} }
let withdrawal_balance = state let withdrawal_balance = state.get_balance(withdrawal.index as usize)?;
.balances() let validator = state.get_validator(withdrawal.index as usize)?;
.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 has_sufficient_effective_balance = let has_sufficient_effective_balance =
validator.effective_balance >= spec.min_activation_balance; validator.effective_balance >= spec.min_activation_balance;
@@ -547,13 +539,9 @@ pub fn get_expected_withdrawals<E: EthSpec>(
withdrawals.push(Withdrawal { withdrawals.push(Withdrawal {
index: withdrawal_index, index: withdrawal_index,
validator_index: withdrawal.index, validator_index: withdrawal.index,
address: Address::from_slice( address: validator
validator .get_execution_withdrawal_address(spec)
.withdrawal_credentials .ok_or(BeaconStateError::NonExecutionAddresWithdrawalCredential)?,
.0
.get(12..)
.ok_or(BeaconStateError::IndexNotSupported(12))?,
),
amount: withdrawable_balance, amount: withdrawable_balance,
}); });
withdrawal_index.safe_add_assign(1)?; withdrawal_index.safe_add_assign(1)?;

View File

@@ -550,19 +550,15 @@ pub fn process_execution_layer_withdrawal_requests<E: EthSpec>(
continue; continue;
}; };
let validator = state let validator = state.get_validator(index)?;
.validators()
.get(index)
.ok_or(BeaconStateError::UnknownValidator(index))?;
// Verify withdrawal credentials // Verify withdrawal credentials
let has_correct_credential = validator.has_execution_withdrawal_credential(spec); let has_correct_credential = validator.has_execution_withdrawal_credential(spec);
let is_correct_source_address = validator let is_correct_source_address = validator
.withdrawal_credentials .get_execution_withdrawal_address(spec)
.as_bytes() .ok_or(BeaconStateError::NonExecutionAddresWithdrawalCredential)?
.get(12..) == request.source_address;
.ok_or(BeaconStateError::IndexNotSupported(12))?
== request.source_address.as_bytes();
if !(has_correct_credential && is_correct_source_address) { if !(has_correct_credential && is_correct_source_address) {
continue; continue;
} }
@@ -595,10 +591,7 @@ pub fn process_execution_layer_withdrawal_requests<E: EthSpec>(
continue; continue;
} }
let balance = *state let balance = state.get_balance(index)?;
.balances()
.get(index)
.ok_or(BeaconStateError::UnknownValidator(index))?;
let has_sufficient_effective_balance = let has_sufficient_effective_balance =
validator.effective_balance >= spec.min_activation_balance; validator.effective_balance >= spec.min_activation_balance;
let has_excess_balance = balance let has_excess_balance = balance
@@ -692,18 +685,8 @@ pub fn process_consolidations<E: EthSpec>(
} }
} }
let source_validator = state let source_validator = state.get_validator(consolidation.source_index as usize)?;
.validators() let target_validator = state.get_validator(consolidation.target_index as usize)?;
.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,
))?;
// Verify the source and the target are active // Verify the source and the target are active
let current_epoch = state.current_epoch(); let current_epoch = state.current_epoch();
@@ -790,12 +773,7 @@ pub fn process_consolidations<E: EthSpec>(
source_validator.effective_balance, source_validator.effective_balance,
spec, spec,
)?; )?;
let source_validator = state let source_validator = state.get_validator_mut(consolidation.source_index as usize)?;
.validators_mut()
.get_mut(consolidation.source_index as usize)
.ok_or(BeaconStateError::UnknownValidator(
consolidation.source_index as usize,
))?;
// Initiate source validator exit and append pending consolidation // Initiate source validator exit and append pending consolidation
source_validator.exit_epoch = exit_epoch; source_validator.exit_epoch = exit_epoch;
source_validator.withdrawable_epoch = source_validator source_validator.withdrawable_epoch = source_validator

View File

@@ -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. /// 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> { pub fn get_balance_mut(&mut self, validator_index: usize) -> Result<&mut u64, Error> {
self.balances_mut() self.balances_mut()