diff --git a/specs/_features/reuse_indexes/beacon-chain.md b/specs/_features/reuse_indexes/beacon-chain.md index fcf1de896..8677631e5 100644 --- a/specs/_features/reuse_indexes/beacon-chain.md +++ b/specs/_features/reuse_indexes/beacon-chain.md @@ -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) @@ -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) ``` diff --git a/specs/altair/beacon-chain.md b/specs/altair/beacon-chain.md index d5b89a387..5e24df83e 100644 --- a/specs/altair/beacon-chain.md +++ b/specs/altair/beacon-chain.md @@ -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: