Apply review comments

This commit is contained in:
dapplion 2023-03-28 15:34:07 +09:00
parent f9b359be09
commit ee3e1ac63e
2 changed files with 32 additions and 12 deletions

View File

@ -9,9 +9,12 @@
- [Introduction](#introduction)
- [Preset](#preset)
- [Time parameters](#time-parameters)
- [Helpers](#helpers)
- [Predicates](#predicates)
- [`is_reusable_validator`](#is_reusable_validator)
- [Beacon chain state transition function](#beacon-chain-state-transition-function)
- [Block processing](#block-processing)
- [Modified `assign_index_to_deposit`](#modified-assign_index_to_deposit)
- [Modified `get_index_for_new_validator`](#modified-get_index_for_new_validator)
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
<!-- /TOC -->
@ -30,16 +33,33 @@ This is the beacon chain specification to assign new deposits to existing valida
| - | - | - |
| `REUSE_VALIDATOR_INDEX_DELAY` | `uint64(2**16)` (= 65,536) | epochs | ~1 year |
## Helper functions
### Predicates
#### `is_reusable_validator`
```python
def is_reusable_validator(validator: Validator, balance: Gwei, epoch: Epoch) -> bool:
"""
Check if ``validator`` index can be re-assigned to a new deposit.
"""
return (
validator.withdrawable_epoch < epoch - REUSE_VALIDATOR_INDEX_DELAY
and balance == 0
)
```
## Beacon chain state transition function
### Block processing
#### Modified `assign_index_to_deposit`
#### Modified `get_index_for_new_validator`
```python
def assign_index_to_deposit(state: BeaconState) -> int:
def get_index_for_new_validator(state: BeaconState) -> int:
for index, validator in enumerate(state.validators):
if validator.withdrawable_epoch < get_current_epoch(state) - REUSE_VALIDATOR_INDEX_DELAY:
if is_reusable_validator(validator, state.balances[index], get_current_epoch(state)):
return index
return len(state.validators)
```

View File

@ -511,24 +511,24 @@ def apply_deposit(state: BeaconState,
signing_root = compute_signing_root(deposit_message, domain)
# Initialize validator if the deposit signature is valid
if bls.Verify(pubkey, signing_root, signature):
index = assign_index_to_deposit(state)
update_list(state.validators, index, get_validator_from_deposit(pubkey, withdrawal_credentials, amount))
update_list(state.balances, index, amount)
index = get_index_for_new_validator(state)
update_or_append_to_list(state.validators, index, get_validator_from_deposit(pubkey, withdrawal_credentials, amount))
update_or_append_to_list(state.balances, index, amount)
# [New in Altair]
update_list(state.previous_epoch_participation, index, ParticipationFlags(0b0000_0000))
update_list(state.current_epoch_participation, index, ParticipationFlags(0b0000_0000))
update_list(state.inactivity_scores, index, uint64(0))
update_or_append_to_list(state.previous_epoch_participation, index, ParticipationFlags(0b0000_0000))
update_or_append_to_list(state.current_epoch_participation, index, ParticipationFlags(0b0000_0000))
update_or_append_to_list(state.inactivity_scores, index, uint64(0))
else:
# Increase balance by deposit amount
index = ValidatorIndex(validator_pubkeys.index(pubkey))
increase_balance(state, index, amount)
def assign_index_to_deposit(state: BeaconState) -> int:
def get_index_for_new_validator(state: BeaconState) -> int:
return len(state.validators)
def update_list(list: List, index: int, value: Any) -> None:
def update_or_append_to_list(list: List, index: int, value: Any) -> None:
if index == len(list):
list.append(value)
else: