add withdrawal index to wihdrawal transaction

This commit is contained in:
Danny Ryan 2022-03-03 14:02:52 -07:00
parent 0a55f062d7
commit e1b9cf97e2
No known key found for this signature in database
GPG Key ID: 2765A792E42CE07A
3 changed files with 12 additions and 1 deletions

View File

@ -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
@ -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
@ -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]
```
@ -167,6 +169,7 @@ as well as in the `BeaconState`'s `withdrawal_receipts` queue.
```python
class WithdrawalTransaction(Container):
index: WithdrawalIndex
address: ExecutionAddress
amount: Gwei
```
@ -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)
```

View File

@ -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=[],
)

View File

@ -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)
@ -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
@ -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