Skip to content

Commit

Permalink
Merge pull request #4043 from lucassaldanha/rename-pending-withdrawal…
Browse files Browse the repository at this point in the history
…-field

Rename PartialPendingWithdrawal field `index` to `validator_index`
  • Loading branch information
jtraglia authored Dec 9, 2024
2 parents 31cd9cb + ca0801d commit 83a8042
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 26 deletions.
17 changes: 10 additions & 7 deletions specs/electra/beacon-chain.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ class PendingDeposit(Container):

```python
class PendingPartialWithdrawal(Container):
index: ValidatorIndex
validator_index: ValidatorIndex
amount: Gwei
withdrawable_epoch: Epoch
```
Expand Down Expand Up @@ -586,7 +586,8 @@ def get_consolidation_churn_limit(state: BeaconState) -> Gwei:
```python
def get_pending_balance_to_withdraw(state: BeaconState, validator_index: ValidatorIndex) -> Gwei:
return sum(
withdrawal.amount for withdrawal in state.pending_partial_withdrawals if withdrawal.index == validator_index
withdrawal.amount for withdrawal in state.pending_partial_withdrawals
if withdrawal.validator_index == validator_index
)
```

Expand Down Expand Up @@ -1127,14 +1128,16 @@ def get_expected_withdrawals(state: BeaconState) -> Tuple[Sequence[Withdrawal],
if withdrawal.withdrawable_epoch > epoch or len(withdrawals) == MAX_PENDING_PARTIALS_PER_WITHDRAWALS_SWEEP:
break

validator = state.validators[withdrawal.index]
validator = state.validators[withdrawal.validator_index]
has_sufficient_effective_balance = validator.effective_balance >= MIN_ACTIVATION_BALANCE
has_excess_balance = state.balances[withdrawal.index] > MIN_ACTIVATION_BALANCE
has_excess_balance = state.balances[withdrawal.validator_index] > MIN_ACTIVATION_BALANCE
if validator.exit_epoch == FAR_FUTURE_EPOCH and has_sufficient_effective_balance and has_excess_balance:
withdrawable_balance = min(state.balances[withdrawal.index] - MIN_ACTIVATION_BALANCE, withdrawal.amount)
withdrawable_balance = min(
state.balances[withdrawal.validator_index] - MIN_ACTIVATION_BALANCE,
withdrawal.amount)
withdrawals.append(Withdrawal(
index=withdrawal_index,
validator_index=withdrawal.index,
validator_index=withdrawal.validator_index,
address=ExecutionAddress(validator.withdrawal_credentials[12:]),
amount=withdrawable_balance,
))
Expand Down Expand Up @@ -1569,7 +1572,7 @@ def process_withdrawal_request(
exit_queue_epoch = compute_exit_epoch_and_update_churn(state, to_withdraw)
withdrawable_epoch = Epoch(exit_queue_epoch + MIN_VALIDATOR_WITHDRAWABILITY_DELAY)
state.pending_partial_withdrawals.append(PendingPartialWithdrawal(
index=index,
validator_index=index,
amount=to_withdraw,
withdrawable_epoch=withdrawable_epoch,
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,7 @@ def test_incorrect_source_has_pending_withdrawal(spec, state):

# Create pending withdrawal
pending_withdrawal = spec.PendingPartialWithdrawal(
index=0, amount=excess_balance, withdrawable_epoch=current_epoch
validator_index=0, amount=excess_balance, withdrawable_epoch=current_epoch
)
state.pending_partial_withdrawals.append(pending_withdrawal)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ def test_invalid_validator_has_pending_withdrawal(spec, state):

state.pending_partial_withdrawals.append(
spec.PendingPartialWithdrawal(
index=validator_index,
validator_index=validator_index,
amount=1,
withdrawable_epoch=spec.compute_activation_exit_epoch(current_epoch),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def run_withdrawal_request_processing(
+ spec.config.MIN_VALIDATOR_WITHDRAWABILITY_DELAY
)
expected_partial_withdrawal = spec.PendingPartialWithdrawal(
index=validator_index,
validator_index=validator_index,
amount=expected_amount_to_withdraw,
withdrawable_epoch=expected_withdrawable_epoch,
)
Expand Down Expand Up @@ -196,7 +196,7 @@ def test_basic_withdrawal_request_with_full_partial_withdrawal_queue(spec, state

# Fill the partial withdrawal queue to the max (with a different validator index)
partial_withdrawal = spec.PendingPartialWithdrawal(
index=1, amount=1, withdrawable_epoch=current_epoch
validator_index=1, amount=1, withdrawable_epoch=current_epoch
)
state.pending_partial_withdrawals = [
partial_withdrawal
Expand Down Expand Up @@ -471,7 +471,7 @@ def test_partial_withdrawal_request_with_pending_withdrawals(spec, state):

# Add pending withdrawals
partial_withdrawal = spec.PendingPartialWithdrawal(
index=validator_index, amount=amount, withdrawable_epoch=current_epoch
validator_index=validator_index, amount=amount, withdrawable_epoch=current_epoch
)
state.pending_partial_withdrawals = [partial_withdrawal] * 2

Expand Down Expand Up @@ -513,7 +513,7 @@ def test_partial_withdrawal_request_with_pending_withdrawals_and_high_amount(

# Add many pending withdrawals
partial_withdrawal = spec.PendingPartialWithdrawal(
index=validator_index,
validator_index=validator_index,
amount=spec.EFFECTIVE_BALANCE_INCREMENT,
withdrawable_epoch=current_epoch,
)
Expand Down Expand Up @@ -661,7 +661,7 @@ def test_partial_withdrawal_queue_full(spec, state):

# Fill the partial withdrawal queue to the max
partial_withdrawal = spec.PendingPartialWithdrawal(
index=1, amount=1, withdrawable_epoch=current_epoch
validator_index=1, amount=1, withdrawable_epoch=current_epoch
)
state.pending_partial_withdrawals = [
partial_withdrawal
Expand Down Expand Up @@ -746,7 +746,7 @@ def test_pending_withdrawals_consume_all_excess_balance(spec, state):

# Add pending withdrawals totalling an amount equal to the excess balance
partial_withdrawal = spec.PendingPartialWithdrawal(
index=validator_index, amount=amount, withdrawable_epoch=current_epoch
validator_index=validator_index, amount=amount, withdrawable_epoch=current_epoch
)
state.pending_partial_withdrawals = [partial_withdrawal] * 10

Expand Down Expand Up @@ -951,7 +951,7 @@ def test_full_exit_request_has_partial_withdrawal(spec, state):
state.balances[validator_index] = spec.MAX_EFFECTIVE_BALANCE_ELECTRA
state.pending_partial_withdrawals.append(
spec.PendingPartialWithdrawal(
index=validator_index,
validator_index=validator_index,
amount=1,
withdrawable_epoch=spec.compute_activation_exit_epoch(current_epoch),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ def test_success_excess_balance_but_no_max_effective_balance_compounding(spec, s
@with_electra_and_later
@spec_state_test
def test_pending_withdrawals_one_skipped_one_effective(spec, state):
index_0 = 3
index_1 = 5
validator_index_0 = 3
validator_index_1 = 5

pending_withdrawal_0 = prepare_pending_withdrawal(spec, state, index_0)
pending_withdrawal_1 = prepare_pending_withdrawal(spec, state, index_1)
pending_withdrawal_0 = prepare_pending_withdrawal(spec, state, validator_index_0)
pending_withdrawal_1 = prepare_pending_withdrawal(spec, state, validator_index_1)

# If validator doesn't have an excess balance pending withdrawal is skipped
state.balances[index_0] = spec.MIN_ACTIVATION_BALANCE
state.balances[validator_index_0] = spec.MIN_ACTIVATION_BALANCE

execution_payload = build_empty_execution_payload(spec, state)
assert state.pending_partial_withdrawals == [pending_withdrawal_0, pending_withdrawal_1]
Expand Down Expand Up @@ -155,7 +155,7 @@ def test_pending_withdrawals_exiting_validator(spec, state):
validator_index = len(state.validators) // 2

pending_withdrawal = prepare_pending_withdrawal(spec, state, validator_index)
spec.initiate_validator_exit(state, pending_withdrawal.index)
spec.initiate_validator_exit(state, pending_withdrawal.validator_index)

execution_payload = build_empty_execution_payload(spec, state)
yield from run_withdrawals_processing(spec, state, execution_payload, num_expected_withdrawals=0)
Expand All @@ -169,7 +169,7 @@ def test_pending_withdrawals_low_effective_balance(spec, state):
validator_index = len(state.validators) // 2

pending_withdrawal = prepare_pending_withdrawal(spec, state, validator_index)
state.validators[pending_withdrawal.index].effective_balance = (
state.validators[pending_withdrawal.validator_index].effective_balance = (
spec.MIN_ACTIVATION_BALANCE - spec.EFFECTIVE_BALANCE_INCREMENT
)

Expand All @@ -185,7 +185,7 @@ def test_pending_withdrawals_no_excess_balance(spec, state):
validator_index = len(state.validators) // 2

pending_withdrawal = prepare_pending_withdrawal(spec, state, validator_index)
state.balances[pending_withdrawal.index] = spec.MIN_ACTIVATION_BALANCE
state.balances[pending_withdrawal.validator_index] = spec.MIN_ACTIVATION_BALANCE

execution_payload = build_empty_execution_payload(spec, state)
yield from run_withdrawals_processing(spec, state, execution_payload, num_expected_withdrawals=0)
Expand Down
4 changes: 2 additions & 2 deletions tests/core/pyspec/eth2spec/test/helpers/withdrawals.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def prepare_pending_withdrawal(spec, state, validator_index,
)

withdrawal = spec.PendingPartialWithdrawal(
index=validator_index,
validator_index=validator_index,
amount=amount,
withdrawable_epoch=withdrawable_epoch,
)
Expand Down Expand Up @@ -238,7 +238,7 @@ def run_withdrawals_processing(spec, state, execution_payload, num_expected_with
assert len(pending_withdrawal_requests) <= len(execution_payload.withdrawals)
for index, request in enumerate(pending_withdrawal_requests):
withdrawal = execution_payload.withdrawals[index]
assert withdrawal.validator_index == request.index
assert withdrawal.validator_index == request.validator_index
assert withdrawal.amount == request.amount

return expected_withdrawals

0 comments on commit 83a8042

Please sign in to comment.