-
Notifications
You must be signed in to change notification settings - Fork 987
Added Capella Epoch Processing Logic #3666
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| use crate::common::decrease_balance; | ||
| use safe_arith::SafeArith; | ||
| use types::{BeaconStateError as Error, *}; | ||
|
|
||
| pub fn withdraw_balance<T: EthSpec>( | ||
| state: &mut BeaconState<T>, | ||
| validator_index: usize, | ||
| amount: u64, | ||
| ) -> Result<(), Error> { | ||
| decrease_balance(state, validator_index as usize, amount)?; | ||
|
|
||
| let withdrawal_address = Address::from_slice( | ||
| &state | ||
| .get_validator(validator_index)? | ||
| .withdrawal_credentials | ||
| .as_bytes()[12..], | ||
| ); | ||
| let withdrawal = Withdrawal { | ||
| index: *state.next_withdrawal_index()?, | ||
| validator_index: validator_index as u64, | ||
| address: withdrawal_address, | ||
| amount, | ||
| }; | ||
| state.next_withdrawal_index_mut()?.safe_add_assign(1)?; | ||
| state.withdrawal_queue_mut()?.push(withdrawal)?; | ||
|
|
||
| Ok(()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 17 additions & 4 deletions
21
consensus/state_processing/src/per_epoch_processing/capella/full_withdrawals.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,23 @@ | ||
| use crate::common::withdraw_balance; | ||
| use crate::EpochProcessingError; | ||
| use types::beacon_state::BeaconState; | ||
| use types::eth_spec::EthSpec; | ||
| use types::{beacon_state::BeaconState, eth_spec::EthSpec, ChainSpec}; | ||
|
|
||
| pub fn process_full_withdrawals<T: EthSpec>( | ||
| _state: &mut BeaconState<T>, | ||
| state: &mut BeaconState<T>, | ||
| spec: &ChainSpec, | ||
| ) -> Result<(), EpochProcessingError> { | ||
| todo!("implement this"); | ||
| let current_epoch = state.current_epoch(); | ||
| // FIXME: is this the most efficient way to do this? | ||
| for validator_index in 0..state.validators().len() { | ||
| // TODO: is this the correct way to handle validators not existing? | ||
| if let (Some(validator), Some(balance)) = ( | ||
| state.validators().get(validator_index), | ||
| state.balances().get(validator_index), | ||
| ) { | ||
| if validator.is_fully_withdrawable_at(*balance, current_epoch, spec) { | ||
| withdraw_balance(state, validator_index, *balance)?; | ||
| } | ||
| } | ||
| } | ||
| Ok(()) | ||
| } | ||
37 changes: 33 additions & 4 deletions
37
consensus/state_processing/src/per_epoch_processing/capella/partial_withdrawals.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,39 @@ | ||
| use crate::common::withdraw_balance; | ||
| use crate::EpochProcessingError; | ||
| use types::beacon_state::BeaconState; | ||
| use types::eth_spec::EthSpec; | ||
| use safe_arith::SafeArith; | ||
| use types::{beacon_state::BeaconState, eth_spec::EthSpec, ChainSpec}; | ||
|
|
||
| pub fn process_partial_withdrawals<T: EthSpec>( | ||
| _state: &mut BeaconState<T>, | ||
| state: &mut BeaconState<T>, | ||
| spec: &ChainSpec, | ||
| ) -> Result<(), EpochProcessingError> { | ||
| todo!("implement this"); | ||
| let mut partial_withdrawals_count = 0; | ||
| let mut validator_index = *state.next_partial_withdrawal_validator_index()? as usize; | ||
|
|
||
| let n_validators = state.validators().len(); | ||
| // FIXME: is this the most efficient way to do this? | ||
| for _ in 0..n_validators { | ||
| // TODO: is this the correct way to handle validators not existing? | ||
| if let (Some(validator), Some(balance)) = ( | ||
| state.validators().get(validator_index), | ||
| state.balances().get(validator_index), | ||
| ) { | ||
| if validator.is_partially_withdrawable_validator(*balance, spec) { | ||
| withdraw_balance( | ||
| state, | ||
| validator_index, | ||
| *balance - spec.max_effective_balance, | ||
| )?; | ||
| partial_withdrawals_count.safe_add_assign(1)?; | ||
|
|
||
| validator_index = validator_index.safe_add(1)? % n_validators; | ||
| if partial_withdrawals_count == T::max_partial_withdrawals_per_epoch() { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| *state.next_partial_withdrawal_validator_index_mut()? = validator_index as u64; | ||
|
|
||
| Ok(()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.