From 5728f780326500da7100376eb45e74b1a291cce8 Mon Sep 17 00:00:00 2001 From: realbigsean Date: Tue, 7 May 2024 16:02:14 -0400 Subject: [PATCH] clean up --- .../src/per_block_processing.rs | 22 +++------- .../process_operations.rs | 40 +++++-------------- consensus/types/src/beacon_state.rs | 8 ++++ 3 files changed, 22 insertions(+), 48 deletions(-) diff --git a/consensus/state_processing/src/per_block_processing.rs b/consensus/state_processing/src/per_block_processing.rs index 43414f70859..0f67dd95f85 100644 --- a/consensus/state_processing/src/per_block_processing.rs +++ b/consensus/state_processing/src/per_block_processing.rs @@ -521,16 +521,8 @@ pub fn get_expected_withdrawals( 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; @@ -547,13 +539,9 @@ pub fn get_expected_withdrawals( 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)?; diff --git a/consensus/state_processing/src/per_block_processing/process_operations.rs b/consensus/state_processing/src/per_block_processing/process_operations.rs index 9341ca811bc..aba184b8043 100644 --- a/consensus/state_processing/src/per_block_processing/process_operations.rs +++ b/consensus/state_processing/src/per_block_processing/process_operations.rs @@ -550,19 +550,15 @@ pub fn process_execution_layer_withdrawal_requests( 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; } @@ -595,10 +591,7 @@ pub fn process_execution_layer_withdrawal_requests( 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 @@ -692,18 +685,8 @@ pub fn process_consolidations( } } - 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(); @@ -790,12 +773,7 @@ pub fn process_consolidations( 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 diff --git a/consensus/types/src/beacon_state.rs b/consensus/types/src/beacon_state.rs index a1601a5d1ff..0ff2a24ca01 100644 --- a/consensus/types/src/beacon_state.rs +++ b/consensus/types/src/beacon_state.rs @@ -1469,6 +1469,14 @@ impl BeaconState { } } + /// Get the balance of a single validator. + pub fn get_balance(&self, validator_index: usize) -> Result { + 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()