Skip to content

Commit

Permalink
add withdrawal index to wihdrawal transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
djrtwo committed Mar 3, 2022
1 parent 0a55f06 commit e1b9cf9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
7 changes: 6 additions & 1 deletion specs/capella/beacon-chain.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ 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`|

## Preset

Expand All @@ -40,7 +41,7 @@ We define the following Python custom types for type hinting and readability:

| Name | Value | Description |
| - | - | - |
| `TX_TYPE_WITHDRAWAL` | `TransactionType('0x05')` | EIP-2718 TX Type |
| `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 |

## Configuration
Expand Down Expand Up @@ -106,6 +107,7 @@ class BeaconState(Container):
# Execution
latest_execution_payload_header: ExecutionPayloadHeader
# Withdrawals
withdrawal_index: WithdrawalIndex
withdrawal_receipts: List[WithdrawalTransaction, WITHDRAWAL_TRANSACTION_LIMIT] # [New in Capella]
```

Expand Down Expand Up @@ -167,6 +169,7 @@ as well as in the `BeaconState`'s `withdrawal_receipts` queue.

```python
class WithdrawalTransaction(Container):
index: WithdrawalIndex
address: ExecutionAddress
amount: Gwei
```
Expand All @@ -183,9 +186,11 @@ def withdraw(state: BeaconState, index: ValidatorIndex, amount: Gwei) -> None:
decrease_balance(state, index, amount)
# Create a corresponding withdrawal receipt
receipt = WithdrawalTransaction(
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)
```

Expand Down
1 change: 1 addition & 0 deletions specs/capella/fork.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def upgrade_to_capella(pre: bellatrix.BeaconState) -> BeaconState:
# Execution-layer
latest_execution_payload_header=pre.latest_execution_payload_header,
# Withdrawals
withdrawal_index=WithdrawalIndex(0),
withdrawal_receipts=[],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,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
to_be_withdrawn_indices = [
index for index, validator in enumerate(state.validators)
Expand All @@ -34,6 +35,7 @@ def run_process_full_withdrawals(spec, state, num_expected_withdrawals=None):
assert state.balances[index] == 0

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


@with_capella_and_later
Expand Down Expand Up @@ -63,8 +65,11 @@ def test_single_withdrawal(spec, state):
# Make one validator withdrawable
set_validator_withdrawable(spec, state, 0)

assert state.withdrawal_index == 0
yield from run_process_full_withdrawals(spec, state, 1)

assert state.withdrawal_index == 1


@with_capella_and_later
@spec_state_test
Expand Down

0 comments on commit e1b9cf9

Please sign in to comment.