-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Refactor get_expected_withdrawals
#4766
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
jtraglia
merged 20 commits into
ethereum:master
from
jtraglia:refactor-get-expected-withdrawals
Dec 15, 2025
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
0cf01cd
Refactor get_expected_withdrawals
jtraglia 50d153f
Remove unnecessary list casting
jtraglia 2aa8633
Include prior withdrawals in bound calc for builder withdrawals
jtraglia eac87cd
Rename processed_count variable
jtraglia f5ca5a1
Add new get_balance_minus_withdrawals helper function
jtraglia 7eaec1d
Use get_builder_withdrawable_balance helper function
jtraglia ac6b3fc
Add new is_eligible_for_partial_withdrawals helper function
jtraglia 9348147
Add comments & minimize diff
jtraglia d4b0fe7
Not all sweep withdrawals are excess balance
jtraglia 0369003
Use empty withdrawals variable
jtraglia 0345d85
Avoid withdrawal abbrev & rename variable
jtraglia 0c67b5f
Make electra.get_sweep_withdrawals consistent with capella version
jtraglia f5aa8e6
Change is_not_withdrawable to is_withdrawable
jtraglia b3a8bd8
Rename bound variables
jtraglia b63a78f
Clean up limit code
jtraglia b49a262
Rename has_reached_bound to has_reached_limit
jtraglia cd00cf3
Rename get_balance_minus_withdrawals
jtraglia 678d224
Update get_expected_withdrawals to return sweep count
jtraglia 3623bf0
Group limits together & be more consistent with naming
jtraglia 92c34c7
Use more has_reached_limit
jtraglia 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
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 |
|---|---|---|
|
|
@@ -44,6 +44,7 @@ | |
| - [New `has_execution_withdrawal_credential`](#new-has_execution_withdrawal_credential) | ||
| - [Modified `is_fully_withdrawable_validator`](#modified-is_fully_withdrawable_validator) | ||
| - [Modified `is_partially_withdrawable_validator`](#modified-is_partially_withdrawable_validator) | ||
| - [New `is_eligible_for_partial_withdrawals`](#new-is_eligible_for_partial_withdrawals) | ||
| - [Misc](#misc-1) | ||
| - [New `get_committee_indices`](#new-get_committee_indices) | ||
| - [New `get_max_effective_balance`](#new-get_max_effective_balance) | ||
|
|
@@ -79,6 +80,8 @@ | |
| - [Modified `verify_and_notify_new_payload`](#modified-verify_and_notify_new_payload) | ||
| - [Block processing](#block-processing) | ||
| - [Withdrawals](#withdrawals) | ||
| - [New `get_pending_partial_withdrawals`](#new-get_pending_partial_withdrawals) | ||
| - [Modified `get_sweep_withdrawals`](#modified-get_sweep_withdrawals) | ||
| - [Modified `get_expected_withdrawals`](#modified-get_expected_withdrawals) | ||
| - [Modified `process_withdrawals`](#modified-process_withdrawals) | ||
| - [Execution payload](#execution-payload) | ||
|
|
@@ -545,6 +548,22 @@ def is_partially_withdrawable_validator(validator: Validator, balance: Gwei) -> | |
| ) | ||
| ``` | ||
|
|
||
| #### New `is_eligible_for_partial_withdrawals` | ||
|
|
||
| ```python | ||
| def is_eligible_for_partial_withdrawals(validator: Validator, balance: Gwei) -> bool: | ||
| """ | ||
| Check if ``validator`` can process a pending partial withdrawal. | ||
| """ | ||
| has_sufficient_effective_balance = validator.effective_balance >= MIN_ACTIVATION_BALANCE | ||
| has_excess_balance = balance > MIN_ACTIVATION_BALANCE | ||
| return ( | ||
| validator.exit_epoch == FAR_FUTURE_EPOCH | ||
| and has_sufficient_effective_balance | ||
| and has_excess_balance | ||
| ) | ||
|
Comment on lines
+554
to
+564
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In a follow up PR, I would like to rename:
The "partially withdrawable" function is very misleading now. |
||
| ``` | ||
|
|
||
| ### Misc | ||
|
|
||
| #### New `get_committee_indices` | ||
|
|
@@ -1183,59 +1202,75 @@ def process_block(state: BeaconState, block: BeaconBlock) -> None: | |
|
|
||
| #### Withdrawals | ||
|
|
||
| ##### Modified `get_expected_withdrawals` | ||
|
|
||
| *Note*: The function `get_expected_withdrawals` is modified to support EIP7251. | ||
| ##### New `get_pending_partial_withdrawals` | ||
|
|
||
| ```python | ||
| def get_expected_withdrawals(state: BeaconState) -> Tuple[Sequence[Withdrawal], uint64]: | ||
| epoch = get_current_epoch(state) | ||
| withdrawal_index = state.next_withdrawal_index | ||
| validator_index = state.next_withdrawal_validator_index | ||
| withdrawals: List[Withdrawal] = [] | ||
| processed_partial_withdrawals_count = 0 | ||
| def get_pending_partial_withdrawals( | ||
| state: BeaconState, | ||
| withdrawal_index: WithdrawalIndex, | ||
| epoch: Epoch, | ||
| prior_withdrawals: Sequence[Withdrawal], | ||
| ) -> Tuple[Sequence[Withdrawal], WithdrawalIndex, uint64]: | ||
| withdrawals_limit = min( | ||
| len(prior_withdrawals) + MAX_PENDING_PARTIALS_PER_WITHDRAWALS_SWEEP, | ||
| MAX_WITHDRAWALS_PER_PAYLOAD - 1, | ||
| ) | ||
|
|
||
| # [New in Electra:EIP7251] | ||
| # Consume pending partial withdrawals | ||
| processed_count = 0 | ||
| withdrawals: List[Withdrawal] = [] | ||
| for withdrawal in state.pending_partial_withdrawals: | ||
| if ( | ||
| withdrawal.withdrawable_epoch > epoch | ||
| or len(withdrawals) == MAX_PENDING_PARTIALS_PER_WITHDRAWALS_SWEEP | ||
| ): | ||
| all_withdrawals = prior_withdrawals + withdrawals | ||
| is_withdrawable = withdrawal.withdrawable_epoch <= epoch | ||
| has_reached_limit = len(all_withdrawals) == withdrawals_limit | ||
| if not is_withdrawable or has_reached_limit: | ||
| break | ||
|
|
||
| validator = state.validators[withdrawal.validator_index] | ||
| has_sufficient_effective_balance = validator.effective_balance >= MIN_ACTIVATION_BALANCE | ||
| total_withdrawn = sum( | ||
| w.amount for w in withdrawals if w.validator_index == withdrawal.validator_index | ||
| ) | ||
| balance = state.balances[withdrawal.validator_index] - total_withdrawn | ||
| has_excess_balance = balance > MIN_ACTIVATION_BALANCE | ||
| if ( | ||
| validator.exit_epoch == FAR_FUTURE_EPOCH | ||
| and has_sufficient_effective_balance | ||
| and has_excess_balance | ||
| ): | ||
| withdrawable_balance = min(balance - MIN_ACTIVATION_BALANCE, withdrawal.amount) | ||
| validator_index = withdrawal.validator_index | ||
| validator = state.validators[validator_index] | ||
| balance = get_balance_after_withdrawals(state, validator_index, all_withdrawals) | ||
| if is_eligible_for_partial_withdrawals(validator, balance): | ||
| withdrawal_amount = min(balance - MIN_ACTIVATION_BALANCE, withdrawal.amount) | ||
| withdrawals.append( | ||
| Withdrawal( | ||
| index=withdrawal_index, | ||
| validator_index=withdrawal.validator_index, | ||
| validator_index=validator_index, | ||
| address=ExecutionAddress(validator.withdrawal_credentials[12:]), | ||
| amount=withdrawable_balance, | ||
| amount=withdrawal_amount, | ||
| ) | ||
| ) | ||
| withdrawal_index += WithdrawalIndex(1) | ||
|
|
||
| processed_partial_withdrawals_count += 1 | ||
| processed_count += 1 | ||
|
|
||
| return withdrawals, withdrawal_index, processed_count | ||
| ``` | ||
|
|
||
| ##### Modified `get_sweep_withdrawals` | ||
|
|
||
| *Note*: The function `get_sweep_withdrawals` is modified to use | ||
| `get_max_effective_balance`. | ||
|
|
||
| ```python | ||
| def get_sweep_withdrawals( | ||
| state: BeaconState, | ||
| withdrawal_index: WithdrawalIndex, | ||
| validator_index: ValidatorIndex, | ||
| epoch: Epoch, | ||
| prior_withdrawals: Sequence[Withdrawal], | ||
| ) -> Tuple[Sequence[Withdrawal], WithdrawalIndex, uint64]: | ||
| validators_limit = min(len(state.validators), MAX_VALIDATORS_PER_WITHDRAWALS_SWEEP) | ||
| withdrawals_limit = MAX_WITHDRAWALS_PER_PAYLOAD | ||
|
|
||
| processed_count: uint64 = 0 | ||
| withdrawals: List[Withdrawal] = [] | ||
| for _ in range(validators_limit): | ||
| all_withdrawals = prior_withdrawals + withdrawals | ||
| has_reached_limit = len(all_withdrawals) == withdrawals_limit | ||
| if has_reached_limit: | ||
| break | ||
|
|
||
| # Sweep for remaining. | ||
| bound = min(len(state.validators), MAX_VALIDATORS_PER_WITHDRAWALS_SWEEP) | ||
| for _ in range(bound): | ||
| validator = state.validators[validator_index] | ||
| # [Modified in Electra:EIP7251] | ||
| total_withdrawn = sum(w.amount for w in withdrawals if w.validator_index == validator_index) | ||
| balance = state.balances[validator_index] - total_withdrawn | ||
| balance = get_balance_after_withdrawals(state, validator_index, all_withdrawals) | ||
| if is_fully_withdrawable_validator(validator, balance, epoch): | ||
| withdrawals.append( | ||
| Withdrawal( | ||
|
|
@@ -1257,10 +1292,39 @@ def get_expected_withdrawals(state: BeaconState) -> Tuple[Sequence[Withdrawal], | |
| ) | ||
| ) | ||
| withdrawal_index += WithdrawalIndex(1) | ||
| if len(withdrawals) == MAX_WITHDRAWALS_PER_PAYLOAD: | ||
| break | ||
|
|
||
| validator_index = ValidatorIndex((validator_index + 1) % len(state.validators)) | ||
| return withdrawals, processed_partial_withdrawals_count | ||
| processed_count += 1 | ||
|
|
||
| return withdrawals, withdrawal_index, processed_count | ||
| ``` | ||
|
|
||
| ##### Modified `get_expected_withdrawals` | ||
|
|
||
| *Note*: The function `get_expected_withdrawals` is modified to support EIP7251. | ||
|
|
||
| ```python | ||
| def get_expected_withdrawals(state: BeaconState) -> Tuple[Sequence[Withdrawal], uint64, uint64]: | ||
| epoch = get_current_epoch(state) | ||
| withdrawal_index = state.next_withdrawal_index | ||
| validator_index = state.next_withdrawal_validator_index | ||
| withdrawals: List[Withdrawal] = [] | ||
|
|
||
| # [New in Electra:EIP7251] | ||
| # Get partial withdrawals | ||
| partial_withdrawals, withdrawal_index, processed_partial_withdrawals_count = ( | ||
| get_pending_partial_withdrawals(state, withdrawal_index, epoch, withdrawals) | ||
| ) | ||
| withdrawals.extend(partial_withdrawals) | ||
|
|
||
| # Get sweep withdrawals | ||
| sweep_withdrawals, withdrawal_index, processed_validators_sweep_count = get_sweep_withdrawals( | ||
| state, withdrawal_index, validator_index, epoch, withdrawals | ||
| ) | ||
| withdrawals.extend(sweep_withdrawals) | ||
|
|
||
| # [Modified in Electra:EIP7251] | ||
| return withdrawals, processed_partial_withdrawals_count, processed_validators_sweep_count | ||
| ``` | ||
|
|
||
| ##### Modified `process_withdrawals` | ||
|
|
@@ -1270,7 +1334,9 @@ def get_expected_withdrawals(state: BeaconState) -> Tuple[Sequence[Withdrawal], | |
| ```python | ||
| def process_withdrawals(state: BeaconState, payload: ExecutionPayload) -> None: | ||
| # [Modified in Electra:EIP7251] | ||
| expected_withdrawals, processed_partial_withdrawals_count = get_expected_withdrawals(state) | ||
| expected_withdrawals, processed_partial_withdrawals_count, processed_validators_sweep_count = ( | ||
| get_expected_withdrawals(state) | ||
| ) | ||
|
|
||
| assert payload.withdrawals == expected_withdrawals | ||
|
|
||
|
|
@@ -1289,17 +1355,9 @@ def process_withdrawals(state: BeaconState, payload: ExecutionPayload) -> None: | |
| state.next_withdrawal_index = WithdrawalIndex(latest_withdrawal.index + 1) | ||
|
|
||
| # Update the next validator index to start the next withdrawal sweep | ||
| if len(expected_withdrawals) == MAX_WITHDRAWALS_PER_PAYLOAD: | ||
| # Next sweep starts after the latest withdrawal's validator index | ||
| next_validator_index = ValidatorIndex( | ||
| (expected_withdrawals[-1].validator_index + 1) % len(state.validators) | ||
| ) | ||
| state.next_withdrawal_validator_index = next_validator_index | ||
| else: | ||
| # Advance sweep by the max length of the sweep if there was not a full set of withdrawals | ||
| next_index = state.next_withdrawal_validator_index + MAX_VALIDATORS_PER_WITHDRAWALS_SWEEP | ||
| next_validator_index = ValidatorIndex(next_index % len(state.validators)) | ||
| state.next_withdrawal_validator_index = next_validator_index | ||
| next_index = state.next_withdrawal_validator_index + processed_validators_sweep_count | ||
| next_validator_index = ValidatorIndex(next_index % len(state.validators)) | ||
| state.next_withdrawal_validator_index = next_validator_index | ||
| ``` | ||
|
|
||
| #### Execution payload | ||
|
|
||
Oops, something went wrong.
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.