2024-03-06 06:00:55 +00:00
# Electra -- The Beacon Chain
**Notice**: This document is a work-in-progress for researchers and implementers.
2022-11-24 11:29:09 +00:00
## 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 )
2023-02-22 12:33:05 +00:00
- [Constants ](#constants )
- [Misc ](#misc )
2024-04-09 18:49:58 +00:00
- [Withdrawal prefixes ](#withdrawal-prefixes )
- [Domains ](#domains )
2022-11-24 11:29:09 +00:00
- [Preset ](#preset )
2024-04-09 18:49:58 +00:00
- [Gwei values ](#gwei-values )
- [Rewards and penalties ](#rewards-and-penalties )
- [State list lengths ](#state-list-lengths )
- [Max operations per block ](#max-operations-per-block )
2022-11-24 11:29:09 +00:00
- [Execution ](#execution )
2024-04-16 21:29:55 +00:00
- [Withdrawals processing ](#withdrawals-processing )
2024-06-20 13:09:41 +00:00
- [Pending deposits processing ](#pending-deposits-processing )
2024-04-09 18:49:58 +00:00
- [Configuration ](#configuration )
- [Validator cycle ](#validator-cycle )
2022-11-24 11:29:09 +00:00
- [Containers ](#containers )
- [New containers ](#new-containers )
2024-05-07 16:44:37 +00:00
- [`DepositRequest` ](#depositrequest )
2024-06-20 09:48:46 +00:00
- [`PendingDeposit` ](#pendingdeposit )
2024-04-09 18:49:58 +00:00
- [`PendingPartialWithdrawal` ](#pendingpartialwithdrawal )
2024-06-05 20:12:08 +00:00
- [`WithdrawalRequest` ](#withdrawalrequest )
- [`ConsolidationRequest` ](#consolidationrequest )
2024-04-09 18:49:58 +00:00
- [`PendingConsolidation` ](#pendingconsolidation )
2024-04-16 19:07:52 +00:00
- [Modified Containers ](#modified-containers )
2024-04-16 18:20:52 +00:00
- [`AttesterSlashing` ](#attesterslashing )
2024-04-16 19:07:52 +00:00
- [Extended Containers ](#extended-containers )
2024-04-05 17:02:03 +00:00
- [`Attestation` ](#attestation )
- [`IndexedAttestation` ](#indexedattestation )
- [`BeaconBlockBody` ](#beaconblockbody )
2022-11-24 11:29:09 +00:00
- [`ExecutionPayload` ](#executionpayload )
- [`ExecutionPayloadHeader` ](#executionpayloadheader )
- [`BeaconState` ](#beaconstate )
2024-04-05 17:02:03 +00:00
- [Helper functions ](#helper-functions )
2024-04-09 18:49:58 +00:00
- [Predicates ](#predicates )
2024-07-01 20:36:17 +00:00
- [Modified `compute_proposer_index` ](#modified-compute_proposer_index )
- [Modified `is_eligible_for_activation_queue` ](#modified-is_eligible_for_activation_queue )
2024-04-09 18:49:58 +00:00
- [New `is_compounding_withdrawal_credential` ](#new-is_compounding_withdrawal_credential )
- [New `has_compounding_withdrawal_credential` ](#new-has_compounding_withdrawal_credential )
- [New `has_execution_withdrawal_credential` ](#new-has_execution_withdrawal_credential )
2024-07-01 20:36:17 +00:00
- [Modified `is_fully_withdrawable_validator` ](#modified-is_fully_withdrawable_validator )
- [Modified `is_partially_withdrawable_validator` ](#modified-is_partially_withdrawable_validator )
2024-04-05 17:02:03 +00:00
- [Misc ](#misc-1 )
2024-06-28 19:24:49 +00:00
- [New `get_committee_indices` ](#new-get_committee_indices )
- [New `get_validator_max_effective_balance` ](#new-get_validator_max_effective_balance )
2024-04-05 17:02:03 +00:00
- [Beacon state accessors ](#beacon-state-accessors )
2024-04-16 17:01:21 +00:00
- [New `get_balance_churn_limit` ](#new-get_balance_churn_limit )
2024-04-09 18:49:58 +00:00
- [New `get_activation_exit_churn_limit` ](#new-get_activation_exit_churn_limit )
- [New `get_consolidation_churn_limit` ](#new-get_consolidation_churn_limit )
- [New `get_active_balance` ](#new-get_active_balance )
- [New `get_pending_balance_to_withdraw` ](#new-get_pending_balance_to_withdraw )
2024-04-05 17:02:03 +00:00
- [Modified `get_attesting_indices` ](#modified-get_attesting_indices )
2024-05-31 09:57:51 +00:00
- [Modified `get_next_sync_committee_indices` ](#modified-get_next_sync_committee_indices )
2024-04-09 18:49:58 +00:00
- [Beacon state mutators ](#beacon-state-mutators )
2024-07-01 20:36:17 +00:00
- [Modified `initiate_validator_exit` ](#modified-initiate_validator_exit )
2024-04-09 18:49:58 +00:00
- [New `switch_to_compounding_validator` ](#new-switch_to_compounding_validator )
- [New `queue_excess_active_balance` ](#new-queue_excess_active_balance )
2024-04-08 21:58:36 +00:00
- [New `queue_entire_balance_and_reset_validator` ](#new-queue_entire_balance_and_reset_validator )
2024-04-09 18:49:58 +00:00
- [New `compute_exit_epoch_and_update_churn` ](#new-compute_exit_epoch_and_update_churn )
- [New `compute_consolidation_epoch_and_update_churn` ](#new-compute_consolidation_epoch_and_update_churn )
2024-07-01 20:36:17 +00:00
- [Modified `slash_validator` ](#modified-slash_validator )
2022-11-24 11:29:09 +00:00
- [Beacon chain state transition function ](#beacon-chain-state-transition-function )
2024-04-09 18:49:58 +00:00
- [Epoch processing ](#epoch-processing )
2024-07-01 20:36:17 +00:00
- [Modified `process_epoch` ](#modified-process_epoch )
- [Modified `process_registry_updates` ](#modified-process_registry_updates )
2024-06-26 07:42:48 +00:00
- [New `apply_pending_deposit` ](#new-apply_pending_deposit )
2024-06-20 09:48:46 +00:00
- [New `process_pending_deposits` ](#new-process_pending_deposits )
2024-04-09 18:49:58 +00:00
- [New `process_pending_consolidations` ](#new-process_pending_consolidations )
2024-07-01 20:36:17 +00:00
- [Modified `process_effective_balance_updates` ](#modified-process_effective_balance_updates )
2022-11-24 11:29:09 +00:00
- [Block processing ](#block-processing )
2024-04-16 11:54:05 +00:00
- [Withdrawals ](#withdrawals )
2024-07-01 20:36:17 +00:00
- [Modified `get_expected_withdrawals` ](#modified-get_expected_withdrawals )
- [Modified `process_withdrawals` ](#modified-process_withdrawals )
2024-04-16 11:54:05 +00:00
- [Execution payload ](#execution-payload )
- [Modified `process_execution_payload` ](#modified-process_execution_payload )
- [Operations ](#operations )
- [Modified `process_operations` ](#modified-process_operations )
- [Attestations ](#attestations )
- [Modified `process_attestation` ](#modified-process_attestation )
2024-04-09 18:49:58 +00:00
- [Deposits ](#deposits )
2024-08-23 08:32:50 +00:00
- [Modified `get_validator_from_deposit` ](#modified-get_validator_from_deposit )
2024-07-01 20:36:17 +00:00
- [Modified `apply_deposit` ](#modified-apply_deposit )
2024-04-16 11:54:05 +00:00
- [New `is_valid_deposit_signature` ](#new-is_valid_deposit_signature )
- [Voluntary exits ](#voluntary-exits )
2024-07-01 20:36:17 +00:00
- [Modified `process_voluntary_exit` ](#modified-process_voluntary_exit )
2024-04-16 11:54:05 +00:00
- [Execution layer withdrawal requests ](#execution-layer-withdrawal-requests )
2024-06-05 19:57:45 +00:00
- [New `process_withdrawal_request` ](#new-process_withdrawal_request )
2024-05-07 16:44:37 +00:00
- [Deposit requests ](#deposit-requests )
- [New `process_deposit_request` ](#new-process_deposit_request )
2024-05-22 08:11:39 +00:00
- [Execution layer consolidation requests ](#execution-layer-consolidation-requests )
2024-06-05 19:58:23 +00:00
- [New `process_consolidation_request` ](#new-process_consolidation_request )
2022-11-24 11:29:09 +00:00
- [Testing ](#testing )
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
<!-- /TOC -->
## Introduction
2024-03-06 06:00:55 +00:00
Electra is a consensus-layer upgrade containing a number of features. Including:
* [EIP-6110 ](https://eips.ethereum.org/EIPS/eip-6110 ): Supply validator deposits on chain
2024-03-06 06:23:23 +00:00
* [EIP-7002 ](https://eips.ethereum.org/EIPS/eip-7002 ): Execution layer triggerable exits
2024-04-09 18:49:58 +00:00
* [EIP-7251 ](https://eips.ethereum.org/EIPS/eip-7251 ): Increase the MAX_EFFECTIVE_BALANCE
2024-04-05 17:02:03 +00:00
* [EIP-7549 ](https://eips.ethereum.org/EIPS/eip-7549 ): Move committee index outside Attestation
2024-07-01 23:42:32 +00:00
*Note:* This specification is built upon [Deneb ](../deneb/beacon_chain.md ) and is under active development.
2022-11-24 11:29:09 +00:00
2023-02-22 12:33:05 +00:00
## Constants
The following values are (non-configurable) constants used throughout the specification.
### Misc
2024-03-06 06:00:55 +00:00
| Name | Value | Description |
| - | - | - |
2024-05-07 16:44:37 +00:00
| `UNSET_DEPOSIT_REQUESTS_START_INDEX` | `uint64(2**64 - 1)` | *[New in Electra:EIP6110]* |
2024-04-15 17:16:11 +00:00
| `FULL_EXIT_REQUEST_AMOUNT` | `uint64(0)` | *[New in Electra:EIP7002]* |
2024-04-09 18:49:58 +00:00
### Withdrawal prefixes
| Name | Value |
| - | - |
| `COMPOUNDING_WITHDRAWAL_PREFIX` | `Bytes1('0x02')` |
### Domains
| Name | Value |
| - | - |
| `DOMAIN_CONSOLIDATION` | `DomainType('0x0B000000')` |
2023-02-22 12:33:05 +00:00
2022-11-24 11:29:09 +00:00
## Preset
2024-04-09 18:49:58 +00:00
### Gwei values
| Name | Value |
| - | - |
| `MIN_ACTIVATION_BALANCE` | `Gwei(2**5 * 10**9)` (= 32,000,000,000) |
| `MAX_EFFECTIVE_BALANCE_ELECTRA` | `Gwei(2**11 * 10**9)` (= 2048,000,000,000) |
### Rewards and penalties
| Name | Value |
| - | - |
| `MIN_SLASHING_PENALTY_QUOTIENT_ELECTRA` | `uint64(2**12)` (= 4,096) |
| `WHISTLEBLOWER_REWARD_QUOTIENT_ELECTRA` | `uint64(2**12)` (= 4,096) |
### State list lengths
| Name | Value | Unit |
| - | - | :-: |
2024-06-20 09:48:46 +00:00
| `PENDING_DEPOSITS_LIMIT` | `uint64(2**27)` (= 134,217,728) | pending deposits |
2024-04-09 18:49:58 +00:00
| `PENDING_PARTIAL_WITHDRAWALS_LIMIT` | `uint64(2**27)` (= 134,217,728) | pending partial withdrawals |
| `PENDING_CONSOLIDATIONS_LIMIT` | `uint64(2**18)` (= 262,144) | pending consolidations |
### Max operations per block
| Name | Value |
| - | - |
2024-05-21 15:47:57 +00:00
| `MAX_ATTESTER_SLASHINGS_ELECTRA` | `2**0` (= 1) | *[New in Electra:EIP7549]* |
| `MAX_ATTESTATIONS_ELECTRA` | `2**3` (= 8) | *[New in Electra:EIP7549]* |
2024-04-09 18:49:58 +00:00
2022-11-24 11:29:09 +00:00
### Execution
| Name | Value | Description |
| - | - | - |
2024-06-04 16:42:37 +00:00
| `MAX_DEPOSIT_REQUESTS_PER_PAYLOAD` | `uint64(2**13)` (= 8,192) | *[New in Electra:EIP6110]* Maximum number of deposit receipts allowed in each payload |
2024-04-15 17:16:11 +00:00
| `MAX_WITHDRAWAL_REQUESTS_PER_PAYLOAD` | `uint64(2**4)` (= 16)| *[New in Electra:EIP7002]* Maximum number of execution layer withdrawal requests in each payload |
2024-05-21 15:47:57 +00:00
| `MAX_CONSOLIDATION_REQUESTS_PER_PAYLOAD` | `uint64(1)` (= 1) | *[New in Electra:EIP7002]* Maximum number of execution layer consolidation requests in each payload |
2024-04-09 18:49:58 +00:00
2024-04-16 21:29:55 +00:00
### Withdrawals processing
| Name | Value | Description |
| - | - | - |
| `MAX_PENDING_PARTIALS_PER_WITHDRAWALS_SWEEP` | `uint64(2**3)` (= 8)| *[New in Electra:EIP7002]* Maximum number of pending partial withdrawals to process per payload |
2024-06-20 13:09:41 +00:00
### Pending deposits processing
| Name | Value | Description |
| - | - | - |
| `MAX_PENDING_DEPOSITS_PER_EPOCH_PROCESSING` | `uint64(2**4)` (= 16)| *[New in Electra:EIP6110]* Maximum number of pending deposits to process per epoch |
2024-04-09 18:49:58 +00:00
## Configuration
### Validator cycle
| Name | Value |
| - | - |
| `MIN_PER_EPOCH_CHURN_LIMIT_ELECTRA` | `Gwei(2**7 * 10**9)` (= 128,000,000,000) | # Equivalent to 4 32 ETH validators
2024-04-15 16:33:29 +00:00
| `MAX_PER_EPOCH_ACTIVATION_EXIT_CHURN_LIMIT` | `Gwei(2**8 * 10**9)` (= 256,000,000,000) |
2022-11-24 11:29:09 +00:00
## Containers
### New containers
2024-05-07 16:44:37 +00:00
#### `DepositRequest`
2022-11-24 11:29:09 +00:00
2024-03-06 06:00:55 +00:00
*Note*: The container is new in EIP6110.
2022-11-24 11:29:09 +00:00
```python
2024-05-07 16:44:37 +00:00
class DepositRequest(Container):
2022-11-24 11:29:09 +00:00
pubkey: BLSPubkey
withdrawal_credentials: Bytes32
amount: Gwei
signature: BLSSignature
index: uint64
```
2024-06-20 09:48:46 +00:00
#### `PendingDeposit`
2024-03-06 06:23:23 +00:00
2024-04-16 11:57:23 +00:00
*Note*: The container is new in EIP7251.
2024-04-09 18:49:58 +00:00
```python
2024-06-20 09:48:46 +00:00
class PendingDeposit(Container):
pubkey: BLSPubkey
withdrawal_credentials: Bytes32
2024-04-09 18:49:58 +00:00
amount: Gwei
2024-06-20 09:48:46 +00:00
signature: BLSSignature
slot: Slot
2024-04-09 18:49:58 +00:00
```
#### `PendingPartialWithdrawal`
2024-04-16 11:57:23 +00:00
*Note*: The container is new in EIP7251.
2024-03-06 06:23:23 +00:00
2024-04-09 18:49:58 +00:00
```python
class PendingPartialWithdrawal(Container):
index: ValidatorIndex
amount: Gwei
withdrawable_epoch: Epoch
```
2024-06-05 19:57:45 +00:00
#### `WithdrawalRequest`
2024-03-06 06:23:23 +00:00
2024-04-19 23:30:04 +00:00
*Note*: The container is new in EIP7251:EIP7002.
2024-03-06 06:23:23 +00:00
```python
2024-06-05 19:57:45 +00:00
class WithdrawalRequest(Container):
2024-03-06 06:23:23 +00:00
source_address: ExecutionAddress
validator_pubkey: BLSPubkey
2024-04-09 18:49:58 +00:00
amount: Gwei
```
2024-06-05 19:58:23 +00:00
#### `ConsolidationRequest`
2024-04-16 11:57:23 +00:00
*Note*: The container is new in EIP7251.
2024-04-09 18:49:58 +00:00
```python
2024-06-05 19:58:23 +00:00
class ConsolidationRequest(Container):
2024-04-18 09:22:27 +00:00
source_address: ExecutionAddress
source_pubkey: BLSPubkey
target_pubkey: BLSPubkey
2024-04-09 18:49:58 +00:00
```
#### `PendingConsolidation`
2024-04-16 11:57:23 +00:00
*Note*: The container is new in EIP7251.
2024-04-09 18:49:58 +00:00
```python
class PendingConsolidation(Container):
source_index: ValidatorIndex
target_index: ValidatorIndex
2024-03-06 06:23:23 +00:00
```
2024-04-16 11:41:28 +00:00
### Modified Containers
#### `AttesterSlashing`
```python
class AttesterSlashing(Container):
attestation_1: IndexedAttestation # [Modified in Electra:EIP7549]
attestation_2: IndexedAttestation # [Modified in Electra:EIP7549]
```
2022-11-24 11:29:09 +00:00
### Extended Containers
2024-04-05 17:02:03 +00:00
#### `Attestation`
```python
class Attestation(Container):
aggregation_bits: Bitlist[MAX_VALIDATORS_PER_COMMITTEE * MAX_COMMITTEES_PER_SLOT] # [Modified in Electra:EIP7549]
data: AttestationData
signature: BLSSignature
2024-05-16 07:59:51 +00:00
committee_bits: Bitvector[MAX_COMMITTEES_PER_SLOT] # [New in Electra:EIP7549]
2024-04-05 17:02:03 +00:00
```
#### `IndexedAttestation`
```python
class IndexedAttestation(Container):
# [Modified in Electra:EIP7549]
attesting_indices: List[ValidatorIndex, MAX_VALIDATORS_PER_COMMITTEE * MAX_COMMITTEES_PER_SLOT]
data: AttestationData
signature: BLSSignature
```
#### `BeaconBlockBody`
```python
class BeaconBlockBody(Container):
randao_reveal: BLSSignature
eth1_data: Eth1Data # Eth1 data vote
graffiti: Bytes32 # Arbitrary data
# Operations
proposer_slashings: List[ProposerSlashing, MAX_PROPOSER_SLASHINGS]
attester_slashings: List[AttesterSlashing, MAX_ATTESTER_SLASHINGS_ELECTRA] # [Modified in Electra:EIP7549]
attestations: List[Attestation, MAX_ATTESTATIONS_ELECTRA] # [Modified in Electra:EIP7549]
deposits: List[Deposit, MAX_DEPOSITS]
voluntary_exits: List[SignedVoluntaryExit, MAX_VOLUNTARY_EXITS]
sync_aggregate: SyncAggregate
# Execution
execution_payload: ExecutionPayload # [Modified in Electra:EIP6110:EIP7002]
bls_to_execution_changes: List[SignedBLSToExecutionChange, MAX_BLS_TO_EXECUTION_CHANGES]
blob_kzg_commitments: List[KZGCommitment, MAX_BLOB_COMMITMENTS_PER_BLOCK]
```
2022-11-24 11:29:09 +00:00
#### `ExecutionPayload`
```python
class ExecutionPayload(Container):
# Execution block header fields
parent_hash: Hash32
fee_recipient: ExecutionAddress
state_root: Bytes32
receipts_root: Bytes32
logs_bloom: ByteVector[BYTES_PER_LOGS_BLOOM]
prev_randao: Bytes32
block_number: uint64
gas_limit: uint64
gas_used: uint64
timestamp: uint64
extra_data: ByteList[MAX_EXTRA_DATA_BYTES]
base_fee_per_gas: uint256
# Extra payload fields
block_hash: Hash32
transactions: List[Transaction, MAX_TRANSACTIONS_PER_PAYLOAD]
withdrawals: List[Withdrawal, MAX_WITHDRAWALS_PER_PAYLOAD]
2023-07-25 05:28:34 +00:00
blob_gas_used: uint64
excess_blob_gas: uint64
2024-05-07 16:44:37 +00:00
deposit_requests: List[DepositRequest, MAX_DEPOSIT_REQUESTS_PER_PAYLOAD] # [New in Electra:EIP6110]
2024-04-09 18:49:58 +00:00
# [New in Electra:EIP7002:EIP7251]
2024-06-05 19:57:45 +00:00
withdrawal_requests: List[WithdrawalRequest, MAX_WITHDRAWAL_REQUESTS_PER_PAYLOAD]
2024-05-22 08:03:03 +00:00
# [New in Electra:EIP7251]
2024-06-05 19:58:23 +00:00
consolidation_requests: List[ConsolidationRequest, MAX_CONSOLIDATION_REQUESTS_PER_PAYLOAD]
2022-11-24 11:29:09 +00:00
```
#### `ExecutionPayloadHeader`
```python
class ExecutionPayloadHeader(Container):
# Execution block header fields
parent_hash: Hash32
fee_recipient: ExecutionAddress
state_root: Bytes32
receipts_root: Bytes32
logs_bloom: ByteVector[BYTES_PER_LOGS_BLOOM]
prev_randao: Bytes32
block_number: uint64
gas_limit: uint64
gas_used: uint64
timestamp: uint64
extra_data: ByteList[MAX_EXTRA_DATA_BYTES]
base_fee_per_gas: uint256
# Extra payload fields
block_hash: Hash32
transactions_root: Root
withdrawals_root: Root
2023-07-25 05:28:34 +00:00
blob_gas_used: uint64
excess_blob_gas: uint64
2024-05-07 16:44:37 +00:00
deposit_requests_root: Root # [New in Electra:EIP6110]
2024-04-15 17:16:11 +00:00
withdrawal_requests_root: Root # [New in Electra:EIP7002:EIP7251]
2024-05-22 08:03:03 +00:00
consolidation_requests_root: Root # [New in Electra:EIP7251]
2022-11-24 11:29:09 +00:00
```
#### `BeaconState`
```python
class BeaconState(Container):
# Versioning
genesis_time: uint64
genesis_validators_root: Root
slot: Slot
fork: Fork
# History
latest_block_header: BeaconBlockHeader
block_roots: Vector[Root, SLOTS_PER_HISTORICAL_ROOT]
state_roots: Vector[Root, SLOTS_PER_HISTORICAL_ROOT]
historical_roots: List[Root, HISTORICAL_ROOTS_LIMIT]
# Eth1
eth1_data: Eth1Data
eth1_data_votes: List[Eth1Data, EPOCHS_PER_ETH1_VOTING_PERIOD * SLOTS_PER_EPOCH]
eth1_deposit_index: uint64
# Registry
validators: List[Validator, VALIDATOR_REGISTRY_LIMIT]
balances: List[Gwei, VALIDATOR_REGISTRY_LIMIT]
# Randomness
randao_mixes: Vector[Bytes32, EPOCHS_PER_HISTORICAL_VECTOR]
# Slashings
slashings: Vector[Gwei, EPOCHS_PER_SLASHINGS_VECTOR] # Per-epoch sums of slashed effective balances
# Participation
previous_epoch_participation: List[ParticipationFlags, VALIDATOR_REGISTRY_LIMIT]
current_epoch_participation: List[ParticipationFlags, VALIDATOR_REGISTRY_LIMIT]
# Finality
justification_bits: Bitvector[JUSTIFICATION_BITS_LENGTH] # Bit set for every recent justified epoch
previous_justified_checkpoint: Checkpoint
current_justified_checkpoint: Checkpoint
finalized_checkpoint: Checkpoint
# Inactivity
inactivity_scores: List[uint64, VALIDATOR_REGISTRY_LIMIT]
# Sync
current_sync_committee: SyncCommittee
next_sync_committee: SyncCommittee
# Execution
2024-03-06 06:23:23 +00:00
latest_execution_payload_header: ExecutionPayloadHeader # [Modified in Electra:EIP6110:EIP7002]
2022-11-24 11:29:09 +00:00
# Withdrawals
next_withdrawal_index: WithdrawalIndex
next_withdrawal_validator_index: ValidatorIndex
2023-02-23 14:34:32 +00:00
# Deep history valid from Capella onwards
historical_summaries: List[HistoricalSummary, HISTORICAL_ROOTS_LIMIT]
2024-05-07 16:44:37 +00:00
deposit_requests_start_index: uint64 # [New in Electra:EIP6110]
2024-04-09 18:49:58 +00:00
deposit_balance_to_consume: Gwei # [New in Electra:EIP7251]
exit_balance_to_consume: Gwei # [New in Electra:EIP7251]
earliest_exit_epoch: Epoch # [New in Electra:EIP7251]
consolidation_balance_to_consume: Gwei # [New in Electra:EIP7251]
earliest_consolidation_epoch: Epoch # [New in Electra:EIP7251]
2024-06-20 09:48:46 +00:00
pending_deposits: List[PendingDeposit, PENDING_DEPOSITS_LIMIT] # [New in Electra:EIP7251]
2024-04-09 18:49:58 +00:00
# [New in Electra:EIP7251]
pending_partial_withdrawals: List[PendingPartialWithdrawal, PENDING_PARTIAL_WITHDRAWALS_LIMIT]
pending_consolidations: List[PendingConsolidation, PENDING_CONSOLIDATIONS_LIMIT] # [New in Electra:EIP7251]
2022-11-24 11:29:09 +00:00
```
2024-04-05 17:02:03 +00:00
## Helper functions
2024-04-09 18:49:58 +00:00
### Predicates
2024-07-01 20:36:17 +00:00
#### Modified `compute_proposer_index`
2024-05-16 12:29:32 +00:00
2024-07-01 20:36:17 +00:00
*Note*: The function `compute_proposer_index` is modified to use `MAX_EFFECTIVE_BALANCE_ELECTRA` .
2024-05-31 09:57:51 +00:00
2024-05-16 12:29:32 +00:00
```python
def compute_proposer_index(state: BeaconState, indices: Sequence[ValidatorIndex], seed: Bytes32) -> ValidatorIndex:
"""
Return from ``indices`` a random index sampled by effective balance.
"""
assert len(indices) > 0
MAX_RANDOM_BYTE = 2**8 - 1
i = uint64(0)
total = uint64(len(indices))
while True:
candidate_index = indices[compute_shuffled_index(i % total, total, seed)]
random_byte = hash(seed + uint_to_bytes(uint64(i // 32)))[i % 32]
effective_balance = state.validators[candidate_index].effective_balance
2024-05-16 14:03:10 +00:00
# [Modified in Electra:EIP7251]
if effective_balance * MAX_RANDOM_BYTE >= MAX_EFFECTIVE_BALANCE_ELECTRA * random_byte:
2024-05-16 12:29:32 +00:00
return candidate_index
i += 1
```
2024-07-01 20:36:17 +00:00
#### Modified `is_eligible_for_activation_queue`
*Note*: The function `is_eligible_for_activation_queue` is modified to use `MIN_ACTIVATION_BALANCE` instead of `MAX_EFFECTIVE_BALANCE` .
2024-04-09 18:49:58 +00:00
```python
def is_eligible_for_activation_queue(validator: Validator) -> bool:
"""
Check if ``validator`` is eligible to be placed into the activation queue.
"""
return (
validator.activation_eligibility_epoch == FAR_FUTURE_EPOCH
and validator.effective_balance >= MIN_ACTIVATION_BALANCE # [Modified in Electra:EIP7251]
)
```
#### New `is_compounding_withdrawal_credential`
```python
def is_compounding_withdrawal_credential(withdrawal_credentials: Bytes32) -> bool:
return withdrawal_credentials[:1] == COMPOUNDING_WITHDRAWAL_PREFIX
```
#### New `has_compounding_withdrawal_credential`
```python
def has_compounding_withdrawal_credential(validator: Validator) -> bool:
"""
Check if ``validator`` has an 0x02 prefixed "compounding" withdrawal credential.
"""
return is_compounding_withdrawal_credential(validator.withdrawal_credentials)
```
#### New `has_execution_withdrawal_credential`
```python
def has_execution_withdrawal_credential(validator: Validator) -> bool:
"""
Check if ``validator`` has a 0x01 or 0x02 prefixed withdrawal credential.
"""
return has_compounding_withdrawal_credential(validator) or has_eth1_withdrawal_credential(validator)
```
2024-07-01 20:36:17 +00:00
#### Modified `is_fully_withdrawable_validator`
*Note*: The function `is_fully_withdrawable_validator` is modified to use `has_execution_withdrawal_credential` instead of `has_eth1_withdrawal_credential` .
2024-04-09 18:49:58 +00:00
```python
def is_fully_withdrawable_validator(validator: Validator, balance: Gwei, epoch: Epoch) -> bool:
"""
Check if ``validator`` is fully withdrawable.
"""
return (
has_execution_withdrawal_credential(validator) # [Modified in Electra:EIP7251]
and validator.withdrawable_epoch < = epoch
and balance > 0
)
```
2024-07-01 20:36:17 +00:00
#### Modified `is_partially_withdrawable_validator`
*Note*: The function `is_partially_withdrawable_validator` is modified to use `get_validator_max_effective_balance` instead of `MAX_EFFECTIVE_BALANCE` and `has_execution_withdrawal_credential` instead of `has_eth1_withdrawal_credential` .
2024-04-09 18:49:58 +00:00
```python
def is_partially_withdrawable_validator(validator: Validator, balance: Gwei) -> bool:
"""
Check if ``validator`` is partially withdrawable.
"""
max_effective_balance = get_validator_max_effective_balance(validator)
has_max_effective_balance = validator.effective_balance == max_effective_balance # [Modified in Electra:EIP7251]
has_excess_balance = balance > max_effective_balance # [Modified in Electra:EIP7251]
return (
has_execution_withdrawal_credential(validator) # [Modified in Electra:EIP7251]
and has_max_effective_balance
and has_excess_balance
)
```
2024-04-05 17:02:03 +00:00
### Misc
2024-06-28 19:24:49 +00:00
#### New `get_committee_indices`
2024-04-05 17:02:03 +00:00
```python
2024-05-02 07:54:17 +00:00
def get_committee_indices(committee_bits: Bitvector) -> Sequence[CommitteeIndex]:
return [CommitteeIndex(index) for index, bit in enumerate(committee_bits) if bit]
2024-04-05 17:02:03 +00:00
```
2024-06-28 19:24:49 +00:00
#### New `get_validator_max_effective_balance`
2024-04-09 18:49:58 +00:00
```python
def get_validator_max_effective_balance(validator: Validator) -> Gwei:
"""
Get max effective balance for ``validator``.
"""
if has_compounding_withdrawal_credential(validator):
return MAX_EFFECTIVE_BALANCE_ELECTRA
else:
return MIN_ACTIVATION_BALANCE
```
2024-04-05 17:02:03 +00:00
### Beacon state accessors
2024-04-16 17:01:21 +00:00
#### New `get_balance_churn_limit`
2024-04-09 18:49:58 +00:00
```python
2024-04-16 17:01:21 +00:00
def get_balance_churn_limit(state: BeaconState) -> Gwei:
2024-04-09 18:49:58 +00:00
"""
Return the churn limit for the current epoch.
"""
churn = max(
MIN_PER_EPOCH_CHURN_LIMIT_ELECTRA,
get_total_active_balance(state) // CHURN_LIMIT_QUOTIENT
)
return churn - churn % EFFECTIVE_BALANCE_INCREMENT
```
#### New `get_activation_exit_churn_limit`
```python
def get_activation_exit_churn_limit(state: BeaconState) -> Gwei:
"""
Return the churn limit for the current epoch dedicated to activations and exits.
"""
2024-04-16 17:01:21 +00:00
return min(MAX_PER_EPOCH_ACTIVATION_EXIT_CHURN_LIMIT, get_balance_churn_limit(state))
2024-04-09 18:49:58 +00:00
```
#### New `get_consolidation_churn_limit`
```python
def get_consolidation_churn_limit(state: BeaconState) -> Gwei:
2024-04-16 17:01:21 +00:00
return get_balance_churn_limit(state) - get_activation_exit_churn_limit(state)
2024-04-09 18:49:58 +00:00
```
#### New `get_active_balance`
```python
def get_active_balance(state: BeaconState, validator_index: ValidatorIndex) -> Gwei:
max_effective_balance = get_validator_max_effective_balance(state.validators[validator_index])
return min(state.balances[validator_index], max_effective_balance)
```
#### New `get_pending_balance_to_withdraw`
```python
def get_pending_balance_to_withdraw(state: BeaconState, validator_index: ValidatorIndex) -> Gwei:
return sum(
2024-04-15 17:16:11 +00:00
withdrawal.amount for withdrawal in state.pending_partial_withdrawals if withdrawal.index == validator_index
)
2024-04-09 18:49:58 +00:00
```
2024-04-05 17:02:03 +00:00
#### Modified `get_attesting_indices`
2024-07-01 20:36:17 +00:00
*Note*: The function `get_attesting_indices` is modified to support EIP7549.
2024-04-05 17:02:03 +00:00
```python
def get_attesting_indices(state: BeaconState, attestation: Attestation) -> Set[ValidatorIndex]:
"""
Return the set of attesting indices corresponding to ``aggregation_bits`` and ``committee_bits``.
"""
output: Set[ValidatorIndex] = set()
committee_indices = get_committee_indices(attestation.committee_bits)
committee_offset = 0
for index in committee_indices:
committee = get_beacon_committee(state, attestation.data.slot, index)
committee_attesters = set(
index for i, index in enumerate(committee) if attestation.aggregation_bits[committee_offset + i])
output = output.union(committee_attesters)
committee_offset += len(committee)
return output
```
2024-05-31 09:57:51 +00:00
#### Modified `get_next_sync_committee_indices`
2024-07-01 20:36:17 +00:00
*Note*: The function `get_next_sync_committee_indices` is modified to use `MAX_EFFECTIVE_BALANCE_ELECTRA` .
2024-05-31 09:57:51 +00:00
```python
def get_next_sync_committee_indices(state: BeaconState) -> Sequence[ValidatorIndex]:
"""
Return the sync committee indices, with possible duplicates, for the next sync committee.
"""
epoch = Epoch(get_current_epoch(state) + 1)
MAX_RANDOM_BYTE = 2**8 - 1
active_validator_indices = get_active_validator_indices(state, epoch)
active_validator_count = uint64(len(active_validator_indices))
seed = get_seed(state, epoch, DOMAIN_SYNC_COMMITTEE)
i = 0
sync_committee_indices: List[ValidatorIndex] = []
while len(sync_committee_indices) < SYNC_COMMITTEE_SIZE:
shuffled_index = compute_shuffled_index(uint64(i % active_validator_count), active_validator_count, seed)
candidate_index = active_validator_indices[shuffled_index]
random_byte = hash(seed + uint_to_bytes(uint64(i // 32)))[i % 32]
effective_balance = state.validators[candidate_index].effective_balance
# [Modified in Electra:EIP7251]
if effective_balance * MAX_RANDOM_BYTE >= MAX_EFFECTIVE_BALANCE_ELECTRA * random_byte:
sync_committee_indices.append(candidate_index)
i += 1
return sync_committee_indices
```
2024-04-09 18:49:58 +00:00
### Beacon state mutators
2024-07-01 20:36:17 +00:00
#### Modified `initiate_validator_exit`
*Note*: The function `initiate_validator_exit` is modified to use the new `compute_exit_epoch_and_update_churn` function.
2024-04-09 18:49:58 +00:00
```python
def initiate_validator_exit(state: BeaconState, index: ValidatorIndex) -> None:
"""
Initiate the exit of the validator with index ``index``.
"""
# Return if validator already initiated exit
validator = state.validators[index]
if validator.exit_epoch != FAR_FUTURE_EPOCH:
return
# Compute exit queue epoch [Modified in Electra:EIP7251]
exit_queue_epoch = compute_exit_epoch_and_update_churn(state, validator.effective_balance)
# Set validator exit epoch and withdrawable epoch
validator.exit_epoch = exit_queue_epoch
validator.withdrawable_epoch = Epoch(validator.exit_epoch + MIN_VALIDATOR_WITHDRAWABILITY_DELAY)
```
#### New `switch_to_compounding_validator`
```python
def switch_to_compounding_validator(state: BeaconState, index: ValidatorIndex) -> None:
validator = state.validators[index]
if has_eth1_withdrawal_credential(validator):
2024-04-16 02:26:03 +00:00
validator.withdrawal_credentials = COMPOUNDING_WITHDRAWAL_PREFIX + validator.withdrawal_credentials[1:]
2024-04-09 18:49:58 +00:00
queue_excess_active_balance(state, index)
```
#### New `queue_excess_active_balance`
```python
def queue_excess_active_balance(state: BeaconState, index: ValidatorIndex) -> None:
balance = state.balances[index]
if balance > MIN_ACTIVATION_BALANCE:
excess_balance = balance - MIN_ACTIVATION_BALANCE
state.balances[index] = MIN_ACTIVATION_BALANCE
2024-06-20 09:48:46 +00:00
validator = state.validators[index]
state.pending_deposits.append(PendingDeposit(
pubkey=validator.pubkey,
withdrawal_credentials=validator.withdrawal_credentials,
amount=excess_balance,
signature=bls.G2_POINT_AT_INFINITY,
slot=GENESIS_SLOT,
))
2024-04-09 18:49:58 +00:00
```
2024-04-08 21:58:36 +00:00
#### New `queue_entire_balance_and_reset_validator`
2024-07-01 20:36:17 +00:00
2024-04-08 21:58:36 +00:00
```python
def queue_entire_balance_and_reset_validator(state: BeaconState, index: ValidatorIndex) -> None:
balance = state.balances[index]
state.balances[index] = 0
validator = state.validators[index]
validator.effective_balance = 0
validator.activation_eligibility_epoch = FAR_FUTURE_EPOCH
2024-06-20 09:48:46 +00:00
state.pending_deposits.append(PendingDeposit(
pubkey=validator.pubkey,
withdrawal_credentials=validator.withdrawal_credentials,
amount=balance,
signature=bls.G2_POINT_AT_INFINITY,
slot=GENESIS_SLOT,
))
2024-04-08 21:58:36 +00:00
```
2024-04-09 18:49:58 +00:00
#### New `compute_exit_epoch_and_update_churn`
```python
def compute_exit_epoch_and_update_churn(state: BeaconState, exit_balance: Gwei) -> Epoch:
2024-04-17 12:18:59 +00:00
earliest_exit_epoch = max(state.earliest_exit_epoch, compute_activation_exit_epoch(get_current_epoch(state)))
2024-04-09 18:49:58 +00:00
per_epoch_churn = get_activation_exit_churn_limit(state)
# New epoch for exits.
if state.earliest_exit_epoch < earliest_exit_epoch:
2024-04-17 12:18:59 +00:00
exit_balance_to_consume = per_epoch_churn
2024-04-09 18:49:58 +00:00
else:
2024-04-17 12:18:59 +00:00
exit_balance_to_consume = state.exit_balance_to_consume
# Exit doesn't fit in the current earliest epoch.
if exit_balance > exit_balance_to_consume:
balance_to_process = exit_balance - exit_balance_to_consume
additional_epochs = (balance_to_process - 1) // per_epoch_churn + 1
earliest_exit_epoch += additional_epochs
exit_balance_to_consume += additional_epochs * per_epoch_churn
# Consume the balance and update state variables.
state.exit_balance_to_consume = exit_balance_to_consume - exit_balance
state.earliest_exit_epoch = earliest_exit_epoch
2024-04-09 18:49:58 +00:00
return state.earliest_exit_epoch
```
#### New `compute_consolidation_epoch_and_update_churn`
```python
def compute_consolidation_epoch_and_update_churn(state: BeaconState, consolidation_balance: Gwei) -> Epoch:
2024-04-17 12:18:59 +00:00
earliest_consolidation_epoch = max(
state.earliest_consolidation_epoch, compute_activation_exit_epoch(get_current_epoch(state)))
2024-04-09 18:49:58 +00:00
per_epoch_consolidation_churn = get_consolidation_churn_limit(state)
# New epoch for consolidations.
if state.earliest_consolidation_epoch < earliest_consolidation_epoch:
2024-04-17 12:18:59 +00:00
consolidation_balance_to_consume = per_epoch_consolidation_churn
2024-04-09 18:49:58 +00:00
else:
2024-04-17 12:18:59 +00:00
consolidation_balance_to_consume = state.consolidation_balance_to_consume
# Consolidation doesn't fit in the current earliest epoch.
if consolidation_balance > consolidation_balance_to_consume:
balance_to_process = consolidation_balance - consolidation_balance_to_consume
additional_epochs = (balance_to_process - 1) // per_epoch_consolidation_churn + 1
earliest_consolidation_epoch += additional_epochs
consolidation_balance_to_consume += additional_epochs * per_epoch_consolidation_churn
# Consume the balance and update state variables.
state.consolidation_balance_to_consume = consolidation_balance_to_consume - consolidation_balance
state.earliest_consolidation_epoch = earliest_consolidation_epoch
2024-04-09 18:49:58 +00:00
return state.earliest_consolidation_epoch
```
2024-07-01 20:36:17 +00:00
#### Modified `slash_validator`
*Note*: The function `slash_validator` is modified to change how the slashing penalty and proposer/whistleblower rewards are calculated in accordance with EIP7251.
2024-04-09 18:49:58 +00:00
```python
def slash_validator(state: BeaconState,
slashed_index: ValidatorIndex,
whistleblower_index: ValidatorIndex=None) -> None:
"""
Slash the validator with index ``slashed_index``.
"""
epoch = get_current_epoch(state)
initiate_validator_exit(state, slashed_index)
validator = state.validators[slashed_index]
validator.slashed = True
validator.withdrawable_epoch = max(validator.withdrawable_epoch, Epoch(epoch + EPOCHS_PER_SLASHINGS_VECTOR))
state.slashings[epoch % EPOCHS_PER_SLASHINGS_VECTOR] += validator.effective_balance
# [Modified in Electra:EIP7251]
slashing_penalty = validator.effective_balance // MIN_SLASHING_PENALTY_QUOTIENT_ELECTRA
decrease_balance(state, slashed_index, slashing_penalty)
# Apply proposer and whistleblower rewards
proposer_index = get_beacon_proposer_index(state)
if whistleblower_index is None:
whistleblower_index = proposer_index
whistleblower_reward = Gwei(
validator.effective_balance // WHISTLEBLOWER_REWARD_QUOTIENT_ELECTRA) # [Modified in Electra:EIP7251]
proposer_reward = Gwei(whistleblower_reward * PROPOSER_WEIGHT // WEIGHT_DENOMINATOR)
increase_balance(state, proposer_index, proposer_reward)
increase_balance(state, whistleblower_index, Gwei(whistleblower_reward - proposer_reward))
```
2022-11-24 11:29:09 +00:00
## Beacon chain state transition function
2024-04-09 18:49:58 +00:00
### Epoch processing
2024-07-01 20:36:17 +00:00
#### Modified `process_epoch`
*Note*: The function `process_epoch` is modified to call updated functions and to process pending balance deposits and pending consolidations which are new in Electra.
2024-04-09 18:49:58 +00:00
```python
def process_epoch(state: BeaconState) -> None:
process_justification_and_finalization(state)
process_inactivity_updates(state)
process_rewards_and_penalties(state)
process_registry_updates(state) # [Modified in Electra:EIP7251]
process_slashings(state)
process_eth1_data_reset(state)
2024-06-20 09:48:46 +00:00
process_pending_deposits(state) # [New in Electra:EIP7251]
2024-04-09 18:49:58 +00:00
process_pending_consolidations(state) # [New in Electra:EIP7251]
process_effective_balance_updates(state) # [Modified in Electra:EIP7251]
process_slashings_reset(state)
process_randao_mixes_reset(state)
process_historical_summaries_update(state)
process_participation_flag_updates(state)
process_sync_committee_updates(state)
```
2024-07-01 20:36:17 +00:00
#### Modified `process_registry_updates`
2024-04-09 18:49:58 +00:00
2024-07-01 20:36:17 +00:00
*Note*: The function `process_registry_updates` is modified to use the updated definition of `initiate_validator_exit`
2024-04-09 18:49:58 +00:00
and changes how the activation epochs are computed for eligible validators.
```python
def process_registry_updates(state: BeaconState) -> None:
# Process activation eligibility and ejections
for index, validator in enumerate(state.validators):
if is_eligible_for_activation_queue(validator):
validator.activation_eligibility_epoch = get_current_epoch(state) + 1
if (
is_active_validator(validator, get_current_epoch(state))
and validator.effective_balance < = EJECTION_BALANCE
):
initiate_validator_exit(state, ValidatorIndex(index))
# Activate all eligible validators
activation_epoch = compute_activation_exit_epoch(get_current_epoch(state))
for validator in state.validators:
if is_eligible_for_activation(state, validator):
validator.activation_epoch = activation_epoch
```
2024-06-26 07:42:48 +00:00
#### New `apply_pending_deposit`
```python
2024-07-05 12:04:38 +00:00
def apply_pending_deposit(state: BeaconState, deposit: PendingDeposit) -> None:
2024-06-26 07:42:48 +00:00
"""
Applies ``deposit`` to the ``state``.
"""
validator_pubkeys = [v.pubkey for v in state.validators]
if deposit.pubkey not in validator_pubkeys:
# Verify the deposit signature (proof of possession) which is not checked by the deposit contract
if is_valid_deposit_signature(
deposit.pubkey,
deposit.withdrawal_credentials,
deposit.amount,
deposit.signature
):
add_validator_to_registry(state, deposit.pubkey, deposit.withdrawal_credentials, deposit.amount)
else:
validator_index = ValidatorIndex(validator_pubkeys.index(deposit.pubkey))
2024-07-05 12:04:38 +00:00
# Increase balance
increase_balance(state, validator_index, deposit.amount)
2024-06-26 07:42:48 +00:00
```
2024-06-20 09:48:46 +00:00
#### New `process_pending_deposits`
2024-04-09 18:49:58 +00:00
2024-06-26 07:42:48 +00:00
Iterating over `pending_deposits` queue this function runs the following checks before applying pending deposit:
1. All Eth1 bridge deposits are processed before the first deposit request gets processed.
2. Deposit position in the queue is finalized.
3. Deposit does not exceed the `MAX_PENDING_DEPOSITS_PER_EPOCH_PROCESSING` limit.
4. Deposit does not exceed the activation churn limit.
2024-04-09 18:49:58 +00:00
```python
2024-06-20 09:48:46 +00:00
def process_pending_deposits(state: BeaconState) -> None:
2024-08-06 11:29:13 +00:00
next_epoch = Epoch(get_current_epoch(state) + 1)
2024-04-09 18:49:58 +00:00
available_for_processing = state.deposit_balance_to_consume + get_activation_exit_churn_limit(state)
processed_amount = 0
next_deposit_index = 0
2024-05-22 10:31:22 +00:00
deposits_to_postpone = []
2024-06-25 11:26:17 +00:00
is_churn_limit_reached = False
2024-06-25 11:47:18 +00:00
finalized_slot = compute_start_slot_at_epoch(state.finalized_checkpoint.epoch)
2024-04-09 18:49:58 +00:00
2024-06-20 09:48:46 +00:00
for deposit in state.pending_deposits:
2024-06-26 07:42:48 +00:00
# Do not process deposit requests if Eth1 bridge deposits are not yet applied.
if (
# Is deposit request
deposit.slot > GENESIS_SLOT and
# There are pending Eth1 bridge deposits
state.eth1_deposit_index < state.deposit_requests_start_index
):
2024-06-25 11:26:17 +00:00
break
2024-06-25 11:47:18 +00:00
# Check if deposit has been finalized, otherwise, stop processing.
if deposit.slot > finalized_slot:
break
2024-06-26 07:42:48 +00:00
# Check if number of processed deposits has not reached the limit, otherwise, stop processing.
2024-07-02 09:30:42 +00:00
if next_deposit_index >= MAX_PENDING_DEPOSITS_PER_EPOCH_PROCESSING:
2024-06-20 13:09:41 +00:00
break
2024-07-05 12:04:38 +00:00
# Read validator state
is_validator_exited = False
is_validator_withdrawn = False
validator_pubkeys = [v.pubkey for v in state.validators]
2024-07-09 12:52:23 +00:00
if deposit.pubkey in validator_pubkeys:
2024-07-05 12:04:38 +00:00
validator = state.validators[ValidatorIndex(validator_pubkeys.index(deposit.pubkey))]
is_validator_exited = validator.exit_epoch < FAR_FUTURE_EPOCH
2024-08-23 08:37:08 +00:00
is_validator_withdrawn = validator.withdrawable_epoch < next_epoch
2024-06-20 12:46:38 +00:00
2024-07-05 12:04:38 +00:00
if is_validator_withdrawn:
# Deposited balance will never become active. Increase balance but do not consume churn
apply_pending_deposit(state, deposit)
elif is_validator_exited:
# Validator is exiting, postpone the deposit until after withdrawable epoch
2024-06-26 07:42:48 +00:00
deposits_to_postpone.append(deposit)
2024-07-05 12:04:38 +00:00
else:
# Check if deposit fits in the churn, otherwise, do no more deposit processing in this epoch.
is_churn_limit_reached = processed_amount + deposit.amount > available_for_processing
if is_churn_limit_reached:
break
# Consume churn and apply deposit.
processed_amount += deposit.amount
apply_pending_deposit(state, deposit)
2024-06-20 09:48:46 +00:00
2024-05-22 10:31:22 +00:00
# Regardless of how the deposit was handled, we move on in the queue.
2024-04-09 18:49:58 +00:00
next_deposit_index += 1
2024-06-20 09:48:46 +00:00
state.pending_deposits = state.pending_deposits[next_deposit_index:]
2024-04-09 18:49:58 +00:00
2024-06-26 07:42:48 +00:00
# Accumulate churn only if the churn limit has been hit.
2024-06-25 11:26:17 +00:00
if is_churn_limit_reached:
2024-04-09 18:49:58 +00:00
state.deposit_balance_to_consume = available_for_processing - processed_amount
2024-06-25 11:26:17 +00:00
else:
state.deposit_balance_to_consume = Gwei(0)
2024-05-22 10:31:22 +00:00
2024-06-20 09:48:46 +00:00
state.pending_deposits += deposits_to_postpone
2024-04-09 18:49:58 +00:00
```
#### New `process_pending_consolidations`
```python
def process_pending_consolidations(state: BeaconState) -> None:
2024-08-06 11:17:26 +00:00
next_epoch = Epoch(get_current_epoch(state) + 1)
2024-04-09 18:49:58 +00:00
next_pending_consolidation = 0
for pending_consolidation in state.pending_consolidations:
source_validator = state.validators[pending_consolidation.source_index]
if source_validator.slashed:
next_pending_consolidation += 1
continue
2024-08-06 11:17:26 +00:00
if source_validator.withdrawable_epoch > next_epoch:
2024-04-09 18:49:58 +00:00
break
# Churn any target excess active balance of target and raise its max
switch_to_compounding_validator(state, pending_consolidation.target_index)
# Move active balance to target. Excess balance is withdrawable.
active_balance = get_active_balance(state, pending_consolidation.source_index)
decrease_balance(state, pending_consolidation.source_index, active_balance)
increase_balance(state, pending_consolidation.target_index, active_balance)
next_pending_consolidation += 1
state.pending_consolidations = state.pending_consolidations[next_pending_consolidation:]
```
2024-07-01 20:36:17 +00:00
#### Modified `process_effective_balance_updates`
2024-04-09 18:49:58 +00:00
2024-07-01 20:36:17 +00:00
*Note*: The function `process_effective_balance_updates` is modified to use the new limit for the maximum effective balance.
2024-04-09 18:49:58 +00:00
```python
def process_effective_balance_updates(state: BeaconState) -> None:
# Update effective balances with hysteresis
for index, validator in enumerate(state.validators):
balance = state.balances[index]
HYSTERESIS_INCREMENT = uint64(EFFECTIVE_BALANCE_INCREMENT // HYSTERESIS_QUOTIENT)
DOWNWARD_THRESHOLD = HYSTERESIS_INCREMENT * HYSTERESIS_DOWNWARD_MULTIPLIER
UPWARD_THRESHOLD = HYSTERESIS_INCREMENT * HYSTERESIS_UPWARD_MULTIPLIER
2024-07-01 20:36:17 +00:00
# [Modified in Electra:EIP7251]
2024-04-09 18:49:58 +00:00
EFFECTIVE_BALANCE_LIMIT = (
MAX_EFFECTIVE_BALANCE_ELECTRA if has_compounding_withdrawal_credential(validator)
else MIN_ACTIVATION_BALANCE
)
if (
balance + DOWNWARD_THRESHOLD < validator.effective_balance
or validator.effective_balance + UPWARD_THRESHOLD < balance
):
validator.effective_balance = min(balance - balance % EFFECTIVE_BALANCE_INCREMENT, EFFECTIVE_BALANCE_LIMIT)
```
2023-02-22 12:33:05 +00:00
### Block processing
2022-11-24 11:29:09 +00:00
```python
2023-02-22 12:33:05 +00:00
def process_block(state: BeaconState, block: BeaconBlock) -> None:
process_block_header(state, block)
2024-04-09 18:49:58 +00:00
process_withdrawals(state, block.body.execution_payload) # [Modified in Electra:EIP7251]
2024-03-06 06:00:55 +00:00
process_execution_payload(state, block.body, EXECUTION_ENGINE) # [Modified in Electra:EIP6110]
2023-02-22 12:33:05 +00:00
process_randao(state, block.body)
process_eth1_data(state, block.body)
2024-04-09 18:49:58 +00:00
process_operations(state, block.body) # [Modified in Electra:EIP6110:EIP7002:EIP7549:EIP7251]
2023-02-22 12:33:05 +00:00
process_sync_aggregate(state, block.body.sync_aggregate)
2022-11-24 11:29:09 +00:00
```
2024-04-16 11:54:05 +00:00
#### Withdrawals
2024-07-01 20:36:17 +00:00
##### Modified `get_expected_withdrawals`
*Note*: The function `get_expected_withdrawals` is modified to support EIP7251.
2024-04-09 18:49:58 +00:00
```python
def get_expected_withdrawals(state: BeaconState) -> Tuple[Sequence[Withdrawal], uint64]:
epoch = get_current_epoch(state)
withdrawal_index = state.next_withdrawal_index
validator_index = state.next_withdrawal_validator_index
withdrawals: List[Withdrawal] = []
# [New in Electra:EIP7251] Consume pending partial withdrawals
for withdrawal in state.pending_partial_withdrawals:
2024-04-16 21:29:55 +00:00
if withdrawal.withdrawable_epoch > epoch or len(withdrawals) == MAX_PENDING_PARTIALS_PER_WITHDRAWALS_SWEEP:
2024-04-09 18:49:58 +00:00
break
validator = state.validators[withdrawal.index]
has_sufficient_effective_balance = validator.effective_balance >= MIN_ACTIVATION_BALANCE
has_excess_balance = state.balances[withdrawal.index] > MIN_ACTIVATION_BALANCE
if validator.exit_epoch == FAR_FUTURE_EPOCH and has_sufficient_effective_balance and has_excess_balance:
withdrawable_balance = min(state.balances[withdrawal.index] - MIN_ACTIVATION_BALANCE, withdrawal.amount)
withdrawals.append(Withdrawal(
index=withdrawal_index,
validator_index=withdrawal.index,
address=ExecutionAddress(validator.withdrawal_credentials[12:]),
amount=withdrawable_balance,
))
withdrawal_index += WithdrawalIndex(1)
partial_withdrawals_count = len(withdrawals)
# Sweep for remaining.
bound = min(len(state.validators), MAX_VALIDATORS_PER_WITHDRAWALS_SWEEP)
for _ in range(bound):
validator = state.validators[validator_index]
balance = state.balances[validator_index]
if is_fully_withdrawable_validator(validator, balance, epoch):
withdrawals.append(Withdrawal(
index=withdrawal_index,
validator_index=validator_index,
address=ExecutionAddress(validator.withdrawal_credentials[12:]),
amount=balance,
))
withdrawal_index += WithdrawalIndex(1)
elif is_partially_withdrawable_validator(validator, balance):
withdrawals.append(Withdrawal(
index=withdrawal_index,
validator_index=validator_index,
address=ExecutionAddress(validator.withdrawal_credentials[12:]),
amount=balance - get_validator_max_effective_balance(validator), # [Modified in Electra:EIP7251]
))
withdrawal_index += WithdrawalIndex(1)
if len(withdrawals) == MAX_WITHDRAWALS_PER_PAYLOAD:
break
validator_index = ValidatorIndex((validator_index + 1) % len(state.validators))
return withdrawals, partial_withdrawals_count
```
2024-07-01 20:36:17 +00:00
##### Modified `process_withdrawals`
*Note*: The function `process_withdrawals` is modified to support EIP7251.
2024-04-09 18:49:58 +00:00
```python
def process_withdrawals(state: BeaconState, payload: ExecutionPayload) -> None:
expected_withdrawals, partial_withdrawals_count = get_expected_withdrawals(state) # [Modified in Electra:EIP7251]
assert len(payload.withdrawals) == len(expected_withdrawals)
for expected_withdrawal, withdrawal in zip(expected_withdrawals, payload.withdrawals):
assert withdrawal == expected_withdrawal
decrease_balance(state, withdrawal.validator_index, withdrawal.amount)
# Update pending partial withdrawals [New in Electra:EIP7251]
state.pending_partial_withdrawals = state.pending_partial_withdrawals[partial_withdrawals_count:]
# Update the next withdrawal index if this block contained withdrawals
if len(expected_withdrawals) != 0:
latest_withdrawal = expected_withdrawals[-1]
state.next_withdrawal_index = WithdrawalIndex(latest_withdrawal.index + 1)
# Update the next validator index to start the next withdrawal sweep
if len(expected_withdrawals) == MAX_WITHDRAWALS_PER_PAYLOAD:
# Next sweep starts after the latest withdrawal's validator index
next_validator_index = ValidatorIndex((expected_withdrawals[-1].validator_index + 1) % len(state.validators))
state.next_withdrawal_validator_index = next_validator_index
else:
# Advance sweep by the max length of the sweep if there was not a full set of withdrawals
next_index = state.next_withdrawal_validator_index + MAX_VALIDATORS_PER_WITHDRAWALS_SWEEP
next_validator_index = ValidatorIndex(next_index % len(state.validators))
state.next_withdrawal_validator_index = next_validator_index
```
2024-04-16 11:54:05 +00:00
#### Execution payload
##### Modified `process_execution_payload`
2024-04-09 18:49:58 +00:00
2024-04-16 11:54:05 +00:00
*Note*: The function `process_execution_payload` is modified to use the new `ExecutionPayloadHeader` type.
```python
def process_execution_payload(state: BeaconState, body: BeaconBlockBody, execution_engine: ExecutionEngine) -> None:
payload = body.execution_payload
# Verify consistency of the parent hash with respect to the previous execution payload header
assert payload.parent_hash == state.latest_execution_payload_header.block_hash
# Verify prev_randao
assert payload.prev_randao == get_randao_mix(state, get_current_epoch(state))
# Verify timestamp
assert payload.timestamp == compute_timestamp_at_slot(state, state.slot)
# Verify commitments are under limit
assert len(body.blob_kzg_commitments) < = MAX_BLOBS_PER_BLOCK
# Verify the execution payload is valid
versioned_hashes = [kzg_commitment_to_versioned_hash(commitment) for commitment in body.blob_kzg_commitments]
assert execution_engine.verify_and_notify_new_payload(
NewPayloadRequest(
execution_payload=payload,
versioned_hashes=versioned_hashes,
parent_beacon_block_root=state.latest_block_header.parent_root,
)
)
# Cache execution payload header
state.latest_execution_payload_header = ExecutionPayloadHeader(
parent_hash=payload.parent_hash,
fee_recipient=payload.fee_recipient,
state_root=payload.state_root,
receipts_root=payload.receipts_root,
logs_bloom=payload.logs_bloom,
prev_randao=payload.prev_randao,
block_number=payload.block_number,
gas_limit=payload.gas_limit,
gas_used=payload.gas_used,
timestamp=payload.timestamp,
extra_data=payload.extra_data,
base_fee_per_gas=payload.base_fee_per_gas,
block_hash=payload.block_hash,
transactions_root=hash_tree_root(payload.transactions),
withdrawals_root=hash_tree_root(payload.withdrawals),
blob_gas_used=payload.blob_gas_used,
excess_blob_gas=payload.excess_blob_gas,
2024-05-07 16:44:37 +00:00
deposit_requests_root=hash_tree_root(payload.deposit_requests), # [New in Electra:EIP6110]
2024-04-16 11:54:05 +00:00
withdrawal_requests_root=hash_tree_root(payload.withdrawal_requests), # [New in Electra:EIP7002:EIP7251]
2024-05-21 16:27:32 +00:00
consolidation_requests_root=hash_tree_root(payload.consolidation_requests), # [New in Electra:EIP7251]
2024-04-16 11:54:05 +00:00
)
```
#### Operations
##### Modified `process_operations`
2023-02-22 12:33:05 +00:00
2024-04-09 18:49:58 +00:00
*Note*: The function `process_operations` is modified to support all of the new functionality in Electra.
2023-02-22 12:33:05 +00:00
```python
def process_operations(state: BeaconState, body: BeaconBlockBody) -> None:
2024-03-06 06:00:55 +00:00
# [Modified in Electra:EIP6110]
2023-03-02 11:29:22 +00:00
# Disable former deposit mechanism once all prior deposits are processed
2024-05-07 16:44:37 +00:00
eth1_deposit_index_limit = min(state.eth1_data.deposit_count, state.deposit_requests_start_index)
2023-03-02 11:29:22 +00:00
if state.eth1_deposit_index < eth1_deposit_index_limit:
assert len(body.deposits) == min(MAX_DEPOSITS, eth1_deposit_index_limit - state.eth1_deposit_index)
2023-02-28 10:36:46 +00:00
else:
assert len(body.deposits) == 0
2023-02-22 12:33:05 +00:00
def for_ops(operations: Sequence[Any], fn: Callable[[BeaconState, Any], None]) -> None:
for operation in operations:
fn(state, operation)
for_ops(body.proposer_slashings, process_proposer_slashing)
for_ops(body.attester_slashings, process_attester_slashing)
2024-04-05 17:02:03 +00:00
for_ops(body.attestations, process_attestation) # [Modified in Electra:EIP7549]
2024-04-09 18:49:58 +00:00
for_ops(body.deposits, process_deposit) # [Modified in Electra:EIP7251]
for_ops(body.voluntary_exits, process_voluntary_exit) # [Modified in Electra:EIP7251]
2023-02-22 12:33:05 +00:00
for_ops(body.bls_to_execution_changes, process_bls_to_execution_change)
2024-06-04 16:42:37 +00:00
for_ops(body.execution_payload.deposit_requests, process_deposit_request) # [New in Electra:EIP6110]
2024-04-09 18:49:58 +00:00
# [New in Electra:EIP7002:EIP7251]
2024-06-05 19:57:45 +00:00
for_ops(body.execution_payload.withdrawal_requests, process_withdrawal_request)
2024-05-22 08:03:03 +00:00
# [New in Electra:EIP7251]
2024-06-05 19:58:23 +00:00
for_ops(body.execution_payload.consolidation_requests, process_consolidation_request)
2023-02-22 12:33:05 +00:00
```
2022-11-24 11:29:09 +00:00
2024-04-16 11:54:05 +00:00
##### Attestations
###### Modified `process_attestation`
2024-04-05 17:02:03 +00:00
```python
def process_attestation(state: BeaconState, attestation: Attestation) -> None:
data = attestation.data
assert data.target.epoch in (get_previous_epoch(state), get_current_epoch(state))
assert data.target.epoch == compute_epoch_at_slot(data.slot)
assert data.slot + MIN_ATTESTATION_INCLUSION_DELAY < = state.slot
# [Modified in Electra:EIP7549]
assert data.index == 0
committee_indices = get_committee_indices(attestation.committee_bits)
participants_count = 0
for index in committee_indices:
assert index < get_committee_count_per_slot ( state , data . target . epoch )
committee = get_beacon_committee(state, data.slot, index)
participants_count += len(committee)
assert len(attestation.aggregation_bits) == participants_count
# Participation flag indices
participation_flag_indices = get_attestation_participation_flag_indices(state, data, state.slot - data.slot)
# Verify signature
assert is_valid_indexed_attestation(state, get_indexed_attestation(state, attestation))
# Update epoch participation flags
if data.target.epoch == get_current_epoch(state):
epoch_participation = state.current_epoch_participation
else:
epoch_participation = state.previous_epoch_participation
proposer_reward_numerator = 0
for index in get_attesting_indices(state, attestation):
for flag_index, weight in enumerate(PARTICIPATION_FLAG_WEIGHTS):
if flag_index in participation_flag_indices and not has_flag(epoch_participation[index], flag_index):
epoch_participation[index] = add_flag(epoch_participation[index], flag_index)
proposer_reward_numerator += get_base_reward(state, index) * weight
# Reward proposer
proposer_reward_denominator = (WEIGHT_DENOMINATOR - PROPOSER_WEIGHT) * WEIGHT_DENOMINATOR // PROPOSER_WEIGHT
proposer_reward = Gwei(proposer_reward_numerator // proposer_reward_denominator)
increase_balance(state, get_beacon_proposer_index(state), proposer_reward)
```
2024-04-09 18:49:58 +00:00
##### Deposits
2023-02-28 10:36:46 +00:00
2024-08-23 08:32:50 +00:00
###### Modified `get_validator_from_deposit`
*Note*: The function is modified to use `MAX_EFFECTIVE_BALANCE_ELECTRA` for compounding withdrawal credential.
```python
def get_validator_from_deposit(pubkey: BLSPubkey, withdrawal_credentials: Bytes32, amount: uint64) -> Validator:
if is_compounding_withdrawal_credential(withdrawal_credentials):
max_effective_balance = MAX_EFFECTIVE_BALANCE_ELECTRA
else:
max_effective_balance = MIN_ACTIVATION_BALANCE
# [Modified in Electra:EIP7251]
effective_balance = min(amount - amount % EFFECTIVE_BALANCE_INCREMENT, max_effective_balance)
return Validator(
pubkey=pubkey,
withdrawal_credentials=withdrawal_credentials,
activation_eligibility_epoch=FAR_FUTURE_EPOCH,
activation_epoch=FAR_FUTURE_EPOCH,
exit_epoch=FAR_FUTURE_EPOCH,
withdrawable_epoch=FAR_FUTURE_EPOCH,
effective_balance=effective_balance,
)
```
2024-07-01 20:36:17 +00:00
###### Modified `apply_deposit`
2024-04-09 18:49:58 +00:00
2024-07-01 20:36:17 +00:00
*Note*: The function `process_deposit` is modified to support EIP7251.
2024-03-06 06:00:55 +00:00
2023-02-28 10:36:46 +00:00
```python
2024-04-09 18:49:58 +00:00
def apply_deposit(state: BeaconState,
pubkey: BLSPubkey,
withdrawal_credentials: Bytes32,
amount: uint64,
signature: BLSSignature) -> None:
validator_pubkeys = [v.pubkey for v in state.validators]
if pubkey not in validator_pubkeys:
# Verify the deposit signature (proof of possession) which is not checked by the deposit contract
if is_valid_deposit_signature(pubkey, withdrawal_credentials, amount, signature):
2024-06-20 09:48:46 +00:00
add_validator_to_registry(state, pubkey, withdrawal_credentials, Gwei(0)) # [Modified in Electra:EIP7251]
# [New in Electra:EIP7251]
state.pending_deposits.append(PendingDeposit(
pubkey=pubkey,
withdrawal_credentials=withdrawal_credentials,
amount=amount,
signature=signature,
slot=GENESIS_SLOT,
))
2024-04-09 18:49:58 +00:00
else:
# Increase balance by deposit amount
2024-07-03 11:30:16 +00:00
# [Modified in Electra:EIP7251]
2024-06-20 09:48:46 +00:00
state.pending_deposits.append(PendingDeposit(
pubkey=pubkey,
withdrawal_credentials=withdrawal_credentials,
amount=amount,
signature=signature,
slot=GENESIS_SLOT
))
2024-04-09 18:49:58 +00:00
```
2024-04-16 11:54:05 +00:00
###### New `is_valid_deposit_signature`
2024-04-09 18:49:58 +00:00
```python
def is_valid_deposit_signature(pubkey: BLSPubkey,
withdrawal_credentials: Bytes32,
amount: uint64,
signature: BLSSignature) -> bool:
deposit_message = DepositMessage(
pubkey=pubkey,
withdrawal_credentials=withdrawal_credentials,
amount=amount,
2023-02-28 10:36:46 +00:00
)
2024-04-09 18:49:58 +00:00
domain = compute_domain(DOMAIN_DEPOSIT) # Fork-agnostic domain since deposits are valid across forks
signing_root = compute_signing_root(deposit_message, domain)
return bls.Verify(pubkey, signing_root, signature)
```
2024-04-16 11:54:05 +00:00
##### Voluntary exits
2024-07-01 20:36:17 +00:00
###### Modified `process_voluntary_exit`
*Note*: The function `process_voluntary_exit` is modified to ensure the validator has no pending withdrawals in the queue.
2024-03-06 06:00:55 +00:00
2023-02-28 10:36:46 +00:00
```python
2024-04-16 11:54:05 +00:00
def process_voluntary_exit(state: BeaconState, signed_voluntary_exit: SignedVoluntaryExit) -> None:
voluntary_exit = signed_voluntary_exit.message
validator = state.validators[voluntary_exit.validator_index]
# Verify the validator is active
assert is_active_validator(validator, get_current_epoch(state))
# Verify exit has not been initiated
assert validator.exit_epoch == FAR_FUTURE_EPOCH
# Exits must specify an epoch when they become valid; they are not valid before then
assert get_current_epoch(state) >= voluntary_exit.epoch
# Verify the validator has been active long enough
assert get_current_epoch(state) >= validator.activation_epoch + SHARD_COMMITTEE_PERIOD
# Only exit validator if it has no pending withdrawals in the queue
assert get_pending_balance_to_withdraw(state, voluntary_exit.validator_index) == 0 # [New in Electra:EIP7251]
# Verify signature
domain = compute_domain(DOMAIN_VOLUNTARY_EXIT, CAPELLA_FORK_VERSION, state.genesis_validators_root)
signing_root = compute_signing_root(voluntary_exit, domain)
assert bls.Verify(validator.pubkey, signing_root, signed_voluntary_exit.signature)
# Initiate exit
initiate_validator_exit(state, voluntary_exit.validator_index)
2023-02-28 10:36:46 +00:00
```
2024-04-16 11:54:05 +00:00
##### Execution layer withdrawal requests
2024-06-05 19:57:45 +00:00
###### New `process_withdrawal_request`
2024-03-06 06:23:23 +00:00
```python
2024-06-05 19:57:45 +00:00
def process_withdrawal_request(
2024-04-09 18:49:58 +00:00
state: BeaconState,
2024-06-05 19:57:45 +00:00
withdrawal_request: WithdrawalRequest
2024-04-09 18:49:58 +00:00
) -> None:
2024-06-05 19:57:45 +00:00
amount = withdrawal_request.amount
2024-04-09 18:49:58 +00:00
is_full_exit_request = amount == FULL_EXIT_REQUEST_AMOUNT
# If partial withdrawal queue is full, only full exits are processed
2024-04-15 17:31:36 +00:00
if len(state.pending_partial_withdrawals) == PENDING_PARTIAL_WITHDRAWALS_LIMIT and not is_full_exit_request:
2024-04-09 18:49:58 +00:00
return
2024-03-06 06:23:23 +00:00
validator_pubkeys = [v.pubkey for v in state.validators]
2024-04-09 03:26:07 +00:00
# Verify pubkey exists
2024-06-05 19:57:45 +00:00
request_pubkey = withdrawal_request.validator_pubkey
2024-04-09 18:49:58 +00:00
if request_pubkey not in validator_pubkeys:
2024-04-09 03:26:07 +00:00
return
2024-04-09 18:49:58 +00:00
index = ValidatorIndex(validator_pubkeys.index(request_pubkey))
validator = state.validators[index]
2024-03-06 06:23:23 +00:00
# Verify withdrawal credentials
2024-04-16 01:30:24 +00:00
has_correct_credential = has_execution_withdrawal_credential(validator)
2024-04-15 17:35:24 +00:00
is_correct_source_address = (
2024-06-05 19:57:45 +00:00
validator.withdrawal_credentials[12:] == withdrawal_request.source_address
2024-04-15 17:35:24 +00:00
)
2024-04-16 01:30:24 +00:00
if not (has_correct_credential and is_correct_source_address):
2024-03-06 06:23:23 +00:00
return
# Verify the validator is active
if not is_active_validator(validator, get_current_epoch(state)):
return
# Verify exit has not been initiated
if validator.exit_epoch != FAR_FUTURE_EPOCH:
return
# Verify the validator has been active long enough
if get_current_epoch(state) < validator.activation_epoch + SHARD_COMMITTEE_PERIOD:
return
2024-04-09 18:49:58 +00:00
pending_balance_to_withdraw = get_pending_balance_to_withdraw(state, index)
if is_full_exit_request:
# Only exit validator if it has no pending withdrawals in the queue
if pending_balance_to_withdraw == 0:
initiate_validator_exit(state, index)
return
has_sufficient_effective_balance = validator.effective_balance >= MIN_ACTIVATION_BALANCE
has_excess_balance = state.balances[index] > MIN_ACTIVATION_BALANCE + pending_balance_to_withdraw
# Only allow partial withdrawals with compounding withdrawal credentials
if has_compounding_withdrawal_credential(validator) and has_sufficient_effective_balance and has_excess_balance:
to_withdraw = min(
state.balances[index] - MIN_ACTIVATION_BALANCE - pending_balance_to_withdraw,
amount
)
exit_queue_epoch = compute_exit_epoch_and_update_churn(state, to_withdraw)
withdrawable_epoch = Epoch(exit_queue_epoch + MIN_VALIDATOR_WITHDRAWABILITY_DELAY)
state.pending_partial_withdrawals.append(PendingPartialWithdrawal(
index=index,
amount=to_withdraw,
withdrawable_epoch=withdrawable_epoch,
))
2024-03-06 06:23:23 +00:00
```
2024-05-07 16:44:37 +00:00
##### Deposit requests
2022-11-24 11:29:09 +00:00
2024-05-07 16:44:37 +00:00
###### New `process_deposit_request`
2024-04-16 11:54:05 +00:00
2022-11-24 11:29:09 +00:00
```python
2024-05-07 16:44:37 +00:00
def process_deposit_request(state: BeaconState, deposit_request: DepositRequest) -> None:
# Set deposit request start index
if state.deposit_requests_start_index == UNSET_DEPOSIT_REQUESTS_START_INDEX:
state.deposit_requests_start_index = deposit_request.index
2023-05-15 16:27:00 +00:00
2024-08-26 10:17:53 +00:00
# Create pending deposit
2024-06-20 09:48:46 +00:00
state.pending_deposits.append(PendingDeposit(
2024-05-07 16:44:37 +00:00
pubkey=deposit_request.pubkey,
withdrawal_credentials=deposit_request.withdrawal_credentials,
amount=deposit_request.amount,
signature=deposit_request.signature,
2024-06-20 09:48:46 +00:00
slot=state.slot,
))
2024-08-26 10:17:53 +00:00
# If validator is active, check switch to compounding
validator_pubkeys = [v.pubkey for v in state.validators]
if deposit_request.pubkey in validator_pubkeys:
validator_index = ValidatorIndex(validator_pubkeys.index(deposit_request.pubkey))
validator = state.validators[validator_index]
if (
validator.exit_epoch > get_current_epoch(state)
and is_compounding_withdrawal_credential(deposit_request.withdrawal_credentials)
and has_eth1_withdrawal_credential(validator)
and is_valid_deposit_signature(
deposit_request.pubkey,
deposit_request.withdrawal_credentials,
deposit_request.amount,
deposit_request.signature
)
):
switch_to_compounding_validator(state, validator_index)
2022-11-24 11:29:09 +00:00
```
2024-05-21 15:47:57 +00:00
##### Execution layer consolidation requests
2024-04-16 11:54:05 +00:00
2024-06-05 19:58:23 +00:00
###### New `process_consolidation_request`
2024-04-09 18:49:58 +00:00
```python
2024-06-05 19:58:23 +00:00
def process_consolidation_request(
2024-05-21 15:47:57 +00:00
state: BeaconState,
2024-06-05 19:58:23 +00:00
consolidation_request: ConsolidationRequest
2024-05-22 08:03:03 +00:00
) -> None:
2024-04-18 09:22:27 +00:00
# If the pending consolidations queue is full, consolidation requests are ignored
if len(state.pending_consolidations) == PENDING_CONSOLIDATIONS_LIMIT:
return
# If there is too little available consolidation churn limit, consolidation requests are ignored
2024-04-18 11:11:36 +00:00
if get_consolidation_churn_limit(state) < = MIN_ACTIVATION_BALANCE:
2024-04-18 09:22:27 +00:00
return
validator_pubkeys = [v.pubkey for v in state.validators]
# Verify pubkeys exists
2024-06-05 19:58:23 +00:00
request_source_pubkey = consolidation_request.source_pubkey
request_target_pubkey = consolidation_request.target_pubkey
2024-05-21 15:47:57 +00:00
if request_source_pubkey not in validator_pubkeys:
2024-04-18 09:22:27 +00:00
return
2024-05-21 15:47:57 +00:00
if request_target_pubkey not in validator_pubkeys:
2024-04-18 09:22:27 +00:00
return
2024-05-21 15:47:57 +00:00
source_index = ValidatorIndex(validator_pubkeys.index(request_source_pubkey))
target_index = ValidatorIndex(validator_pubkeys.index(request_target_pubkey))
2024-04-18 09:22:27 +00:00
source_validator = state.validators[source_index]
2024-04-18 18:33:37 +00:00
target_validator = state.validators[target_index]
2024-04-18 09:22:27 +00:00
2024-04-09 18:49:58 +00:00
# Verify that source != target, so a consolidation cannot be used as an exit.
2024-04-18 09:22:27 +00:00
if source_index == target_index:
return
# Verify source withdrawal credentials
has_correct_credential = has_execution_withdrawal_credential(source_validator)
is_correct_source_address = (
2024-06-05 19:58:23 +00:00
source_validator.withdrawal_credentials[12:] == consolidation_request.source_address
2024-04-18 09:22:27 +00:00
)
if not (has_correct_credential and is_correct_source_address):
2024-04-18 09:30:18 +00:00
return
# Verify that target has execution withdrawal credentials
if not has_execution_withdrawal_credential(target_validator):
2024-04-18 09:22:27 +00:00
return
2024-04-09 18:49:58 +00:00
# Verify the source and the target are active
current_epoch = get_current_epoch(state)
2024-04-18 09:22:27 +00:00
if not is_active_validator(source_validator, current_epoch):
return
if not is_active_validator(target_validator, current_epoch):
return
2024-04-09 18:49:58 +00:00
# Verify exits for source and target have not been initiated
2024-04-18 09:22:27 +00:00
if source_validator.exit_epoch != FAR_FUTURE_EPOCH:
return
if target_validator.exit_epoch != FAR_FUTURE_EPOCH:
return
2024-04-09 18:49:58 +00:00
# Initiate source validator exit and append pending consolidation
source_validator.exit_epoch = compute_consolidation_epoch_and_update_churn(
2024-04-15 17:16:11 +00:00
state, source_validator.effective_balance
2022-11-24 11:29:09 +00:00
)
2024-04-09 18:49:58 +00:00
source_validator.withdrawable_epoch = Epoch(
source_validator.exit_epoch + MIN_VALIDATOR_WITHDRAWABILITY_DELAY
)
state.pending_consolidations.append(PendingConsolidation(
2024-04-18 11:45:17 +00:00
source_index=source_index,
target_index=target_index
2024-04-09 18:49:58 +00:00
))
2022-11-24 11:29:09 +00:00
```
## Testing
2024-03-06 06:00:55 +00:00
*Note*: The function `initialize_beacon_state_from_eth1` is modified for pure Electra testing only.
2022-11-24 11:29:09 +00:00
Modifications include:
2024-02-16 04:29:00 +00:00
1. Use `ELECTRA_FORK_VERSION` as the previous and current fork version.
2024-03-06 06:00:55 +00:00
2. Utilize the Electra `BeaconBlockBody` when constructing the initial `latest_block_header` .
2024-05-07 16:44:37 +00:00
3. *[New in Electra:EIP6110]* Add `deposit_requests_start_index` variable to the genesis state initialization.
2024-04-09 18:49:58 +00:00
4. *[New in Electra:EIP7251]* Initialize new fields to support increasing the maximum effective balance.
2022-11-24 11:29:09 +00:00
```python
def initialize_beacon_state_from_eth1(eth1_block_hash: Hash32,
eth1_timestamp: uint64,
deposits: Sequence[Deposit],
execution_payload_header: ExecutionPayloadHeader=ExecutionPayloadHeader()
) -> BeaconState:
fork = Fork(
2024-03-06 06:00:55 +00:00
previous_version=ELECTRA_FORK_VERSION, # [Modified in Electra:EIP6110] for testing only
current_version=ELECTRA_FORK_VERSION, # [Modified in Electra:EIP6110]
2022-11-24 11:29:09 +00:00
epoch=GENESIS_EPOCH,
)
state = BeaconState(
genesis_time=eth1_timestamp + GENESIS_DELAY,
fork=fork,
eth1_data=Eth1Data(block_hash=eth1_block_hash, deposit_count=uint64(len(deposits))),
latest_block_header=BeaconBlockHeader(body_root=hash_tree_root(BeaconBlockBody())),
randao_mixes=[eth1_block_hash] * EPOCHS_PER_HISTORICAL_VECTOR, # Seed RANDAO with Eth1 entropy
2024-05-07 16:44:37 +00:00
deposit_requests_start_index=UNSET_DEPOSIT_REQUESTS_START_INDEX, # [New in Electra:EIP6110]
2022-11-24 11:29:09 +00:00
)
# Process deposits
leaves = list(map(lambda deposit: deposit.data, deposits))
for index, deposit in enumerate(deposits):
deposit_data_list = List[DepositData, 2**DEPOSIT_CONTRACT_TREE_DEPTH](*leaves[:index + 1])
state.eth1_data.deposit_root = hash_tree_root(deposit_data_list)
process_deposit(state, deposit)
2024-04-09 18:49:58 +00:00
# Process deposit balance updates
2024-06-20 09:48:46 +00:00
validator_pubkeys = [v.pubkey for v in state.validators]
for deposit in state.pending_deposits:
validator_index = ValidatorIndex(validator_pubkeys.index(deposit.pubkey))
increase_balance(state, validator_index, deposit.amount)
state.pending_deposits = []
2024-04-09 18:49:58 +00:00
2022-11-24 11:29:09 +00:00
# Process activations
for index, validator in enumerate(state.validators):
balance = state.balances[index]
2024-06-03 08:56:39 +00:00
# [Modified in Electra:EIP7251]
2024-05-31 16:16:18 +00:00
validator.effective_balance = min(
2024-06-14 06:26:47 +00:00
balance - balance % EFFECTIVE_BALANCE_INCREMENT, get_validator_max_effective_balance(validator))
2024-05-31 09:57:51 +00:00
if validator.effective_balance >= MIN_ACTIVATION_BALANCE:
2022-11-24 11:29:09 +00:00
validator.activation_eligibility_epoch = GENESIS_EPOCH
validator.activation_epoch = GENESIS_EPOCH
# Set genesis validators root for domain separation and chain versioning
state.genesis_validators_root = hash_tree_root(state.validators)
# Fill in sync committees
# Note: A duplicate committee is assigned for the current and next committee at genesis
state.current_sync_committee = get_next_sync_committee(state)
state.next_sync_committee = get_next_sync_committee(state)
# Initialize the execution payload header
state.latest_execution_payload_header = execution_payload_header
return state
```