Merge pull request #3307 from dapplion/reuse-indexes

EIP6914 - Reuse indexes with full sweep
This commit is contained in:
Danny Ryan 2023-04-19 17:57:12 -06:00 committed by GitHub
commit 5e1153311b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 91 additions and 5 deletions

View File

@ -0,0 +1,65 @@
EIP-6914 -- The Beacon Chain
## Table of contents
<!-- TOC -->
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
- [Introduction](#introduction)
- [Preset](#preset)
- [Time parameters](#time-parameters)
- [Helper functions](#helper-functions)
- [Predicates](#predicates)
- [`is_reusable_validator`](#is_reusable_validator)
- [Beacon chain state transition function](#beacon-chain-state-transition-function)
- [Block processing](#block-processing)
- [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 -->
## Introduction
This is the beacon chain specification to assign new deposits to existing validator records. Refers to [EIP-6914](https://github.com/ethereum/EIPs/pull/6914).
*Note:* This specification is built upon [Capella](../../capella/beacon_chain.md) and is under active development.
## Preset
### Time parameters
| Name | Value | Unit | Duration |
| - | - | - | - |
| `SAFE_EPOCHS_TO_REUSE_INDEX` | `uint64(2**16)` (= 65,536) | epochs | ~0.8 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 (
epoch > validator.withdrawable_epoch + SAFE_EPOCHS_TO_REUSE_INDEX
and balance == 0
)
```
## Beacon chain state transition function
### Block processing
#### Modified `get_index_for_new_validator`
```python
def get_index_for_new_validator(state: BeaconState) -> ValidatorIndex:
for index, validator in enumerate(state.validators):
if is_reusable_validator(validator, state.balances[index], get_current_epoch(state)):
return ValidatorIndex(index)
return ValidatorIndex(len(state.validators))
```

View File

@ -30,6 +30,8 @@
- [Misc](#misc-1)
- [`add_flag`](#add_flag)
- [`has_flag`](#has_flag)
- [`get_index_for_new_validator`](#get_index_for_new_validator)
- [`set_or_append_list`](#set_or_append_list)
- [Beacon state accessors](#beacon-state-accessors)
- [`get_next_sync_committee_indices`](#get_next_sync_committee_indices)
- [`get_next_sync_committee`](#get_next_sync_committee)
@ -248,6 +250,23 @@ def has_flag(flags: ParticipationFlags, flag_index: int) -> bool:
return flags & flag == flag
```
#### `get_index_for_new_validator`
```python
def get_index_for_new_validator(state: BeaconState) -> ValidatorIndex:
return ValidatorIndex(len(state.validators))
```
#### `set_or_append_list`
```python
def set_or_append_list(list: List, index: ValidatorIndex, value: Any) -> None:
if index == len(list):
list.append(value)
else:
list[index] = value
```
### Beacon state accessors
#### `get_next_sync_committee_indices`
@ -511,12 +530,14 @@ 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):
state.validators.append(get_validator_from_deposit(pubkey, withdrawal_credentials, amount))
state.balances.append(amount)
index = get_index_for_new_validator(state)
validator = get_validator_from_deposit(pubkey, withdrawal_credentials, amount)
set_or_append_list(state.validators, index, validator)
set_or_append_list(state.balances, index, amount)
# [New in Altair]
state.previous_epoch_participation.append(ParticipationFlags(0b0000_0000))
state.current_epoch_participation.append(ParticipationFlags(0b0000_0000))
state.inactivity_scores.append(uint64(0))
set_or_append_list(state.previous_epoch_participation, index, ParticipationFlags(0b0000_0000))
set_or_append_list(state.current_epoch_participation, index, ParticipationFlags(0b0000_0000))
set_or_append_list(state.inactivity_scores, index, uint64(0))
else:
# Increase balance by deposit amount
index = ValidatorIndex(validator_pubkeys.index(pubkey))