Skip to content

Commit

Permalink
minor withdrawals renamings
Browse files Browse the repository at this point in the history
  • Loading branch information
djrtwo committed Mar 10, 2022
1 parent 507f550 commit 1ab5206
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 30 deletions.
46 changes: 19 additions & 27 deletions specs/capella/beacon-chain.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,21 @@ We define the following Python custom types for type hinting and readability:

| Name | SSZ equivalent | Description |
| - | - | - |
| `TransactionType` | `Bytes1` | an EIP-2718 type |
| `WithdrawalIndex` | `uint64` | an index of a `WithdrawalTransaction`|
| `WithdrawalIndex` | `uint64` | an index of a `Withdrawal`|

## Preset

### State list lengths

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

### Execution

| Name | Value | Description |
| - | - | - |
| `TX_TYPE_WITHDRAWAL` | `TransactionType('0x03')` | EIP-2718 TX Type |
| `MAX_WITHDRAWAL_TRANSACTIONS_PER_PAYLOAD` | `uint64(2**4)` (= 16) | Maximum amount of withdrawal transactions allowed in each payload |
| `MAX_WITHDRAWALS_PER_PAYLOAD` | `uint64(2**4)` (= 16) | Maximum amount of withdrawals allowed in each payload |

## Configuration

Expand Down Expand Up @@ -108,7 +106,7 @@ class BeaconState(Container):
latest_execution_payload_header: ExecutionPayloadHeader
# Withdrawals
withdrawal_index: WithdrawalIndex
withdrawal_receipts: List[WithdrawalTransaction, WITHDRAWAL_TRANSACTION_LIMIT] # [New in Capella]
withdrawals_queue: List[Withdrawal, WITHDRAWALS_QUEUE_LIMIT] # [New in Capella]
```

#### `ExecutionPayload`
Expand All @@ -131,7 +129,7 @@ class ExecutionPayload(Container):
# Extra payload fields
block_hash: Hash32 # Hash of execution block
transactions: List[Transaction, MAX_TRANSACTIONS_PER_PAYLOAD]
withdrawal_transactions: List[WithdrawalTransaction, MAX_WITHDRAWAL_TRANSACTIONS_PER_PAYLOAD] # [New in Capella]
withdrawals: List[Withdrawal, MAX_WITHDRAWALS_PER_PAYLOAD] # [New in Capella]
```

#### `ExecutionPayloadHeader`
Expand All @@ -154,21 +152,15 @@ class ExecutionPayloadHeader(Container):
# Extra payload fields
block_hash: Hash32 # Hash of execution block
transactions_root: Root
withdrawal_transactions_root: Root # [New in Capella]
withdrawals_root: Root # [New in Capella]
```

### New containers

#### `WithdrawalTransaction`

New EIP-2718 transaction type, with the format being the single byte `TX_TYPE_WITHDRAWAL`
followed by an SSZ encoding of the `WithdrawalTransaction` container comprising the transaction contents.

*Note*: This container is used for both a special TX that goes into an `ExecutionPayload`
as well as in the `BeaconState`'s `withdrawal_receipts` queue.
#### `Withdrawal`

```python
class WithdrawalTransaction(Container):
class Withdrawal(Container):
index: WithdrawalIndex
address: ExecutionAddress
amount: Gwei
Expand All @@ -185,13 +177,13 @@ def withdraw(state: BeaconState, index: ValidatorIndex, amount: Gwei) -> None:
# Decrease the validator's balance
decrease_balance(state, index, amount)
# Create a corresponding withdrawal receipt
receipt = WithdrawalTransaction(
withdrawal = Withdrawal(
index=state.withdrawal_index,
address=state.validators[index].withdrawal_credentials[12:],
amount=amount,
)
state.withdrawal_index = WithdrawalIndex(state.withdrawal_index + 1)
state.withdrawal_receipts.append(receipt)
state.withdrawals_queue.append(withdrawal)
```

### Predicates
Expand Down Expand Up @@ -248,30 +240,30 @@ def process_full_withdrawals(state: BeaconState) -> None:
def process_block(state: BeaconState, block: BeaconBlock) -> None:
process_block_header(state, block)
if is_execution_enabled(state, block.body):
process_withdrawal_transactions(state, block.body.execution_payload) # [New in Capella]
process_withdrawals(state, block.body.execution_payload) # [New in Capella]
process_execution_payload(state, block.body.execution_payload, EXECUTION_ENGINE) # [Modified in Capella]
process_randao(state, block.body)
process_eth1_data(state, block.body)
process_operations(state, block.body)
process_sync_aggregate(state, block.body.sync_aggregate)
```

#### New `process_withdrawal_transactions`
#### New `process_withdrawals`

```python
def process_withdrawal_transactions(state: BeaconState, payload: ExecutionPayload) -> None:
num_withdrawal_transactions = min(MAX_WITHDRAWAL_TRANSACTIONS_PER_PAYLOAD, len(state.withdrawal_receipts))
dequeued_withdrawal_receipts = state.withdrawal_receipts[:num_withdrawal_transactions]
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]

assert len(dequeued_withdrawal_receipts) == len(payload.withdrawal_transactions)
for dequeued_receipt, withdrawal_transaction in zip(dequeued_withdrawal_receipts, payload.withdrawal_transactions):
assert len(dequeued_withdrawals) == len(payload.withdrawals)
for dequeued_receipt, withdrawal_transaction in zip(dequeued_withdrawals, payload.withdrawals):
assert dequeued_receipt == withdrawal_transaction

# Ensure no withdrawal type transactions in the normal payload transactions
# assert no_withdrawal_type_transactions_in(payload.transactions)

# Remove dequeued receipts from state
state.withdrawal_receipts = state.withdrawal_receipts[num_withdrawal_transactions:]
state.withdrawals_queue = state.withdrawals_queue[num_withdrawals:]
```

#### Modified `process_execution_payload`
Expand Down Expand Up @@ -305,6 +297,6 @@ def process_execution_payload(state: BeaconState, payload: ExecutionPayload, exe
base_fee_per_gas=payload.base_fee_per_gas,
block_hash=payload.block_hash,
transactions_root=hash_tree_root(payload.transactions),
withdrawal_transactions_root=hash_tree_root(payload.withdrawal_transactions),
withdrawals_root=hash_tree_root(payload.withdrawals),
)
```
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def set_validator_withdrawable(spec, state, index, withdrawable_epoch=None):

def run_process_full_withdrawals(spec, state, num_expected_withdrawals=None):
pre_withdrawal_index = state.withdrawal_index
pre_withdrawal_receipts = state.withdrawal_receipts
pre_withdrawals_queue = state.withdrawals_queue
to_be_withdrawn_indices = [
index for index, validator in enumerate(state.validators)
if spec.is_fully_withdrawable_validator(validator, spec.get_current_epoch(state))
Expand All @@ -34,7 +34,7 @@ def run_process_full_withdrawals(spec, state, num_expected_withdrawals=None):
assert validator.withdrawn_epoch == spec.get_current_epoch(state)
assert state.balances[index] == 0

assert len(state.withdrawal_receipts) == len(pre_withdrawal_receipts) + num_expected_withdrawals
assert len(state.withdrawals_queue) == len(pre_withdrawals_queue) + num_expected_withdrawals
assert state.withdrawal_index == pre_withdrawal_index + num_expected_withdrawals


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def get_process_calls(spec):
'process_participation_record_updates'
),
'process_sync_committee_updates', # altair
'process_withdrawals', # capella
'process_full_withdrawals', # capella
# TODO: add sharding processing functions when spec stabilizes.
]

Expand Down

0 comments on commit 1ab5206

Please sign in to comment.