Skip to content

Commit

Permalink
clean up withdrawals naming
Browse files Browse the repository at this point in the history
  • Loading branch information
djrtwo committed Jun 3, 2022
1 parent 5555944 commit d892e37
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
27 changes: 14 additions & 13 deletions specs/capella/beacon-chain.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
Capella is a consensus-layer upgrade containing a number of features related
to validator withdrawals. Including:
* Automatic withdrawals of `withdrawable` validators
* Partial withdrawals during block proposal
* Partial withdrawals sweep for validators with 0x01 withdrawal
credentials an balances in exceess of `MAX_EFFECTIVE_BALANCE`
* Operation to change from `BLS_WITHDRAWAL_PREFIX` to
`ETH1_ADDRESS_WITHDRAWAL_PREFIX` versioned withdrawal credentials to enable withdrawals for a validator

Expand Down Expand Up @@ -83,7 +84,7 @@ We define the following Python custom types for type hinting and readability:

| Name | Value | Unit | Duration |
| - | - | :-: | :-: |
| `WITHDRAWALS_QUEUE_LIMIT` | `uint64(2**40)` (= 1,099,511,627,776) | withdrawals enqueued in state|
| `WITHDRAWAL_QUEUE_LIMIT` | `uint64(2**40)` (= 1,099,511,627,776) | withdrawals enqueued in state|

### Max operations per block

Expand Down Expand Up @@ -254,9 +255,9 @@ class BeaconState(Container):
# Execution
latest_execution_payload_header: ExecutionPayloadHeader
# Withdrawals
withdrawal_index: WithdrawalIndex # [New in Capella]
next_partial_withdrawal_index: ValidatorIndex # [New in Capella]
withdrawals_queue: List[Withdrawal, WITHDRAWALS_QUEUE_LIMIT] # [New in Capella]
next_withdrawal_index: WithdrawalIndex # [New in Capella]
next_partial_withdrawal_validator_index: ValidatorIndex # [New in Capella]
withdrawal_queue: List[Withdrawal, WITHDRAWAL_QUEUE_LIMIT] # [New in Capella]
```

## Helpers
Expand All @@ -271,12 +272,12 @@ def withdraw_balance(state: BeaconState, index: ValidatorIndex, amount: Gwei) ->
decrease_balance(state, index, amount)
# Create a corresponding withdrawal receipt
withdrawal = Withdrawal(
index=state.withdrawal_index,
index=state.next_withdrawal_index,
address=ExecutionAddress(state.validators[index].withdrawal_credentials[12:]),
amount=amount,
)
state.withdrawal_index = WithdrawalIndex(state.withdrawal_index + 1)
state.withdrawals_queue.append(withdrawal)
state.next_withdrawal_index = WithdrawalIndex(state.next_withdrawal_index + 1)
state.withdrawal_queue.append(withdrawal)
```

### Predicates
Expand Down Expand Up @@ -350,7 +351,7 @@ def process_full_withdrawals(state: BeaconState) -> None:
def process_partial_withdrawals(state: BeaconState) -> None:
partial_withdrawals_count = 0
# Begin where we left off last time
validator_index = state.next_partial_withdrawal_index
validator_index = state.next_partial_withdrawal_validator_index
for _ in range(len(state.validators)):
balance = state.balances[validator_index]
validator = state.validators[validator_index]
Expand All @@ -364,7 +365,7 @@ def process_partial_withdrawals(state: BeaconState) -> None:
if partial_withdrawals_count == MAX_PARTIAL_WITHDRAWALS_PER_EPOCH:
break

state.next_partial_withdrawal_index = validator_index
state.next_partial_withdrawal_validator_index = validator_index
```

### Block processing
Expand All @@ -385,15 +386,15 @@ def process_block(state: BeaconState, block: BeaconBlock) -> None:

```python
def process_withdrawals(state: BeaconState, payload: ExecutionPayload) -> None:
num_withdrawals = min(MAX_WITHDRAWALS_PER_PAYLOAD, len(state.withdrawals_queue))
dequeued_withdrawals = state.withdrawals_queue[:num_withdrawals]
num_withdrawals = min(MAX_WITHDRAWALS_PER_PAYLOAD, len(state.withdrawal_queue))
dequeued_withdrawals = state.withdrawal_queue[:num_withdrawals]

assert len(dequeued_withdrawals) == len(payload.withdrawals)
for dequeued_withdrawal, withdrawal in zip(dequeued_withdrawals, payload.withdrawals):
assert dequeued_withdrawal == withdrawal

# Remove dequeued withdrawals from state
state.withdrawals_queue = state.withdrawals_queue[num_withdrawals:]
state.withdrawal_queue = state.withdrawal_queue[num_withdrawals:]
```

#### Modified `process_execution_payload`
Expand Down
6 changes: 3 additions & 3 deletions specs/capella/fork.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ def upgrade_to_capella(pre: bellatrix.BeaconState) -> BeaconState:
# Execution-layer
latest_execution_payload_header=pre.latest_execution_payload_header,
# Withdrawals
withdrawal_index=WithdrawalIndex(0),
next_partial_withdrawal_index=ValidatorIndex(0),
withdrawals_queue=[],
next_withdrawal_index=WithdrawalIndex(0),
next_partial_withdrawal_validator_index=ValidatorIndex(0),
withdrawal_queue=[],
)

for pre_validator in pre.validators:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def set_validator_partially_withdrawable(spec, state, index, rng=random.Random(6


def run_process_partial_withdrawals(spec, state, num_expected_withdrawals=None):
pre_withdrawal_index = state.withdrawal_index
pre_withdrawals_queue = state.withdrawals_queue
pre_next_withdrawal_index = state.next_withdrawal_index
pre_withdrawal_queue = state.withdrawal_queue
partially_withdrawable_indices = [
index for index, validator in enumerate(state.validators)
if spec.is_partially_withdrawable_validator(validator, state.balances[index])
Expand All @@ -38,8 +38,8 @@ def run_process_partial_withdrawals(spec, state, num_expected_withdrawals=None):

assert len(partially_withdrawable_indices) - num_partial_withdrawals == len(post_partially_withdrawable_indices)

assert len(state.withdrawals_queue) == len(pre_withdrawals_queue) + num_expected_withdrawals
assert state.withdrawal_index == pre_withdrawal_index + num_expected_withdrawals
assert len(state.withdrawal_queue) == len(pre_withdrawal_queue) + num_expected_withdrawals
assert state.next_withdrawal_index == pre_next_withdrawal_index + num_expected_withdrawals


@with_capella_and_later
Expand Down Expand Up @@ -137,7 +137,7 @@ def test_success_max_plus_one_withdrawable(spec, state):

def run_random_partial_withdrawals_test(spec, state, rng):
num_validators = len(state.validators)
state.next_partial_withdrawal_index = rng.randint(0, num_validators - 1)
state.next_partial_withdrawal_validator_index = rng.randint(0, num_validators - 1)

num_partially_withdrawable = rng.randint(0, num_validators - 1)
partially_withdrawable_indices = rng.sample(range(num_validators), num_partially_withdrawable)
Expand Down

0 comments on commit d892e37

Please sign in to comment.