diff --git a/.gitignore b/.gitignore index 205bebcd9..b5f4bddba 100644 --- a/.gitignore +++ b/.gitignore @@ -25,6 +25,7 @@ tests/core/pyspec/eth2spec/eip6110/ tests/core/pyspec/eth2spec/eip7002/ tests/core/pyspec/eth2spec/eip7549/ tests/core/pyspec/eth2spec/whisk/ +tests/core/pyspec/eth2spec/eip7251/ tests/core/pyspec/eth2spec/eip7594/ # coverage reports diff --git a/configs/mainnet.yaml b/configs/mainnet.yaml index a4d157d80..1092ec507 100644 --- a/configs/mainnet.yaml +++ b/configs/mainnet.yaml @@ -50,17 +50,23 @@ CAPELLA_FORK_EPOCH: 194048 # April 12, 2023, 10:27:35pm UTC # Deneb DENEB_FORK_VERSION: 0x04000000 DENEB_FORK_EPOCH: 269568 # March 13, 2024, 01:55:35pm UTC +# Electra +ELECTRA_FORK_VERSION: 0x05000000 +ELECTRA_FORK_EPOCH: 18446744073709551615 # EIP6110 -EIP6110_FORK_VERSION: 0x05000000 # temporary stub +EIP6110_FORK_VERSION: 0x06000000 # temporary stub EIP6110_FORK_EPOCH: 18446744073709551615 # EIP7002 -EIP7002_FORK_VERSION: 0x05000000 # temporary stub +EIP7002_FORK_VERSION: 0x07000000 # temporary stub EIP7002_FORK_EPOCH: 18446744073709551615 +# EIP7251 +EIP7251_FORK_VERSION: 0x06000000 # temporary stub +EIP7251_FORK_EPOCH: 18446744073709551615 # EIP7549 -EIP7549_FORK_VERSION: 0x05000000 # temporary stub +EIP7549_FORK_VERSION: 0x06000000 # temporary stub EIP7549_FORK_EPOCH: 18446744073709551615 # WHISK -WHISK_FORK_VERSION: 0x06000000 # temporary stub +WHISK_FORK_VERSION: 0x08000000 # temporary stub WHISK_FORK_EPOCH: 18446744073709551615 @@ -161,3 +167,8 @@ WHISK_PROPOSER_SELECTION_GAP: 2 # EIP7594 EIP7594_FORK_VERSION: 0x06000001 EIP7594_FORK_EPOCH: 18446744073709551615 + +# [New in EIP7251] +MIN_PER_EPOCH_CHURN_LIMIT_EIP7251: 128000000000 # 2**7 * 10**9 (= 128,000,000,000) +MAX_PER_EPOCH_ACTIVATION_EXIT_CHURN_LIMIT: 256000000000 # 2**8 * 10**9 (= 256,000,000,000) + diff --git a/configs/minimal.yaml b/configs/minimal.yaml index 052576282..07f0da4e2 100644 --- a/configs/minimal.yaml +++ b/configs/minimal.yaml @@ -46,20 +46,26 @@ BELLATRIX_FORK_EPOCH: 18446744073709551615 # Capella CAPELLA_FORK_VERSION: 0x03000001 CAPELLA_FORK_EPOCH: 18446744073709551615 -# DENEB +# Deneb DENEB_FORK_VERSION: 0x04000001 DENEB_FORK_EPOCH: 18446744073709551615 +# Electra +ELECTRA_FORK_VERSION: 0x05000001 +ELECTRA_FORK_EPOCH: 18446744073709551615 # EIP6110 -EIP6110_FORK_VERSION: 0x05000001 +EIP6110_FORK_VERSION: 0x06000001 EIP6110_FORK_EPOCH: 18446744073709551615 # EIP7002 -EIP7002_FORK_VERSION: 0x05000001 +EIP7002_FORK_VERSION: 0x07000001 EIP7002_FORK_EPOCH: 18446744073709551615 +# EIP7251 +EIP7251_FORK_VERSION: 0x06000001 # temporary stub +EIP7251_FORK_EPOCH: 18446744073709551615 # EIP7549 -EIP7549_FORK_VERSION: 0x05000000 # temporary stub +EIP7549_FORK_VERSION: 0x06000001 # temporary stub EIP7549_FORK_EPOCH: 18446744073709551615 # WHISK -WHISK_FORK_VERSION: 0x06000001 +WHISK_FORK_VERSION: 0x08000001 WHISK_FORK_EPOCH: 18446744073709551615 @@ -160,3 +166,7 @@ WHISK_PROPOSER_SELECTION_GAP: 1 # EIP7594 EIP7594_FORK_VERSION: 0x06000001 EIP7594_FORK_EPOCH: 18446744073709551615 + +# [New in EIP7251] +MIN_PER_EPOCH_CHURN_LIMIT_EIP7251: 64000000000 # 2**6 * 10**9 (= 64,000,000,000) +MAX_PER_EPOCH_ACTIVATION_EXIT_CHURN_LIMIT: 128000000000 # 2**7 * 10**9 (= 128,000,000,000) diff --git a/presets/mainnet/eip7251.yaml b/presets/mainnet/eip7251.yaml new file mode 100644 index 000000000..ee543018e --- /dev/null +++ b/presets/mainnet/eip7251.yaml @@ -0,0 +1,28 @@ +# Mainnet preset - EIP7251 + +# Gwei values +# --------------------------------------------------------------- +# 2**5 * 10**9 (= 32,000,000,000) Gwei +MIN_ACTIVATION_BALANCE: 32000000000 +# 2**11 * 10**9 (= 2,048,000,000,000) Gwei +MAX_EFFECTIVE_BALANCE_EIP7251: 2048000000000 + +# State list lengths +# --------------------------------------------------------------- +PENDING_BALANCE_DEPOSITS_LIMIT: 134217728 +PENDING_PARTIAL_WITHDRAWALS_LIMIT: 134217728 +PENDING_CONSOLIDATIONS_LIMIT: 262144 + +# Reward and penalty quotients +# --------------------------------------------------------------- +MIN_SLASHING_PENALTY_QUOTIENT_EIP7251: 4096 +WHISTLEBLOWER_REWARD_QUOTIENT_EIP7251: 4096 + +# Max operations per block +# --------------------------------------------------------------- +MAX_CONSOLIDATIONS: 1 + +# Execution +# --------------------------------------------------------------- +# 2**3 (= 8) partial withdrawals +MAX_PARTIAL_WITHDRAWALS_PER_PAYLOAD: 8 diff --git a/presets/minimal/eip7251.yaml b/presets/minimal/eip7251.yaml new file mode 100644 index 000000000..27ea22138 --- /dev/null +++ b/presets/minimal/eip7251.yaml @@ -0,0 +1,30 @@ +# Minimal preset - EIP7251 + +# Gwei values +# --------------------------------------------------------------- +# 2**5 * 10**9 (= 32,000,000,000) Gwei +MIN_ACTIVATION_BALANCE: 32000000000 +# 2**11 * 10**9 (= 2,048,000,000,000) Gwei +MAX_EFFECTIVE_BALANCE_EIP7251: 2048000000000 + +# State list lengths +# --------------------------------------------------------------- +PENDING_BALANCE_DEPOSITS_LIMIT: 134217728 +# [customized] smaller queue +PENDING_PARTIAL_WITHDRAWALS_LIMIT: 64 +# [customized] smaller queue +PENDING_CONSOLIDATIONS_LIMIT: 64 + +# Reward and penalty quotients +# --------------------------------------------------------------- +MIN_SLASHING_PENALTY_QUOTIENT_EIP7251: 4096 +WHISTLEBLOWER_REWARD_QUOTIENT_EIP7251: 4096 + +# Max operations per block +# --------------------------------------------------------------- +MAX_CONSOLIDATIONS: 1 + +# Execution +# --------------------------------------------------------------- +# [customized] 2**1 (= 2) +MAX_PARTIAL_WITHDRAWALS_PER_PAYLOAD: 2 diff --git a/pysetup/constants.py b/pysetup/constants.py index 85d347a54..422bd1a43 100644 --- a/pysetup/constants.py +++ b/pysetup/constants.py @@ -6,6 +6,7 @@ CAPELLA = 'capella' DENEB = 'deneb' EIP6110 = 'eip6110' EIP7002 = 'eip7002' +EIP7251 = 'eip7251' EIP7549 = 'eip7549' WHISK = 'whisk' EIP7594 = 'eip7594' diff --git a/pysetup/md_doc_paths.py b/pysetup/md_doc_paths.py index 675f700d5..f79b5f642 100644 --- a/pysetup/md_doc_paths.py +++ b/pysetup/md_doc_paths.py @@ -8,6 +8,7 @@ from .constants import ( DENEB, EIP6110, EIP7002, + EIP7251, EIP7549, WHISK, EIP7594, @@ -24,6 +25,7 @@ PREVIOUS_FORK_OF = { EIP7549: DENEB, WHISK: CAPELLA, EIP7002: CAPELLA, + EIP7251: DENEB, EIP7594: DENEB, } diff --git a/pysetup/spec_builders/__init__.py b/pysetup/spec_builders/__init__.py index 54040bffb..61c1c807e 100644 --- a/pysetup/spec_builders/__init__.py +++ b/pysetup/spec_builders/__init__.py @@ -7,6 +7,7 @@ from .eip6110 import EIP6110SpecBuilder from .eip7002 import EIP7002SpecBuilder from .eip7549 import EIP7549SpecBuilder from .whisk import WhiskSpecBuilder +from .eip7251 import EIP7251SpecBuilder from .eip7594 import EIP7594SpecBuilder @@ -14,6 +15,7 @@ spec_builders = { builder.fork: builder for builder in ( Phase0SpecBuilder, AltairSpecBuilder, BellatrixSpecBuilder, CapellaSpecBuilder, DenebSpecBuilder, - EIP6110SpecBuilder, EIP7002SpecBuilder, EIP7549SpecBuilder, WhiskSpecBuilder, EIP7594SpecBuilder, + EIP6110SpecBuilder, EIP7002SpecBuilder, EIP7549SpecBuilder, WhiskSpecBuilder, EIP7251SpecBuilder, + EIP7594SpecBuilder, ) } diff --git a/pysetup/spec_builders/eip7251.py b/pysetup/spec_builders/eip7251.py new file mode 100644 index 000000000..6d9056e5f --- /dev/null +++ b/pysetup/spec_builders/eip7251.py @@ -0,0 +1,24 @@ +from typing import Dict + +from .base import BaseSpecBuilder +from ..constants import EIP7251 + + +class EIP7251SpecBuilder(BaseSpecBuilder): + fork: str = EIP7251 + + @classmethod + def imports(cls, preset_name: str): + return super().imports(preset_name) + f''' +from eth2spec.deneb import {preset_name} as deneb +''' + +## TODO: deal with changed gindices + + @classmethod + def hardcoded_ssz_dep_constants(cls) -> Dict[str, str]: + return { + 'FINALIZED_ROOT_GINDEX': 'GeneralizedIndex(169)', + 'CURRENT_SYNC_COMMITTEE_GINDEX': 'GeneralizedIndex(86)', + 'NEXT_SYNC_COMMITTEE_GINDEX': 'GeneralizedIndex(87)', + } diff --git a/specs/_features/eip6110/fork.md b/specs/_features/eip6110/fork.md index 6b5ab0431..8bb8e2a67 100644 --- a/specs/_features/eip6110/fork.md +++ b/specs/_features/eip6110/fork.md @@ -28,7 +28,7 @@ Warning: this configuration is not definitive. | Name | Value | | - | - | -| `EIP6110_FORK_VERSION` | `Version('0x05000000')` | +| `EIP6110_FORK_VERSION` | `Version('0x06000000')` | | `EIP6110_FORK_EPOCH` | `Epoch(18446744073709551615)` **TBD** | ## Helper functions diff --git a/specs/_features/eip7002/fork.md b/specs/_features/eip7002/fork.md index 87008de77..8f095389d 100644 --- a/specs/_features/eip7002/fork.md +++ b/specs/_features/eip7002/fork.md @@ -28,7 +28,7 @@ Warning: this configuration is not definitive. | Name | Value | | - | - | -| `EIP7002_FORK_VERSION` | `Version('0x05000000')` | +| `EIP7002_FORK_VERSION` | `Version('0x07000000')` | | `EIP7002_FORK_EPOCH` | `Epoch(18446744073709551615)` **TBD** | ## Helper functions diff --git a/specs/_features/eip7251/beacon-chain.md b/specs/_features/eip7251/beacon-chain.md new file mode 100644 index 000000000..e0ae0c803 --- /dev/null +++ b/specs/_features/eip7251/beacon-chain.md @@ -0,0 +1,963 @@ +# EIP7251 - Spec + +## Table of contents + + + + + +- [Introduction](#introduction) +- [Constants](#constants) + - [Withdrawal prefixes](#withdrawal-prefixes) + - [Domains](#domains) +- [Presets](#presets) + - [Gwei values](#gwei-values) + - [Rewards and penalties](#rewards-and-penalties) + - [Max operations per block](#max-operations-per-block) + - [Execution](#execution) + - [State list lengths](#state-list-lengths) +- [Configuration](#configuration) + - [Validator cycle](#validator-cycle) +- [Containers](#containers) + - [New containers](#new-containers) + - [New `PendingBalanceDeposit`](#new-pendingbalancedeposit) + - [New `PartialWithdrawal`](#new-partialwithdrawal) + - [New `ExecutionLayerWithdrawRequest`](#new-executionlayerwithdrawrequest) + - [New `Consolidation`](#new-consolidation) + - [New `SignedConsolidation`](#new-signedconsolidation) + - [New `PendingConsolidation`](#new-pendingconsolidation) + - [Extended Containers](#extended-containers) + - [`BeaconState`](#beaconstate) + - [`BeaconBlockBody`](#beaconblockbody) +- [Helpers](#helpers) + - [Predicates](#predicates) + - [Updated `is_eligible_for_activation_queue`](#updated-is_eligible_for_activation_queue) + - [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) + - [Updated `is_fully_withdrawable_validator`](#updated-is_fully_withdrawable_validator) + - [Updated `is_partially_withdrawable_validator`](#updated-is_partially_withdrawable_validator) + - [Beacon state accessors](#beacon-state-accessors) + - [New `get_validator_max_effective_balance`](#new-get_validator_max_effective_balance) + - [New `get_churn_limit`](#new-get_churn_limit) + - [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) + - [Beacon state mutators](#beacon-state-mutators) + - [Updated `initiate_validator_exit`](#updated--initiate_validator_exit) + - [New `set_compounding_withdrawal_credentials`](#new-set_compounding_withdrawal_credentials) + - [New `switch_to_compounding_validator`](#new-switch_to_compounding_validator) + - [New `queue_excess_active_balance`](#new-queue_excess_active_balance) + - [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) + - [Updated `slash_validator`](#updated-slash_validator) +- [Beacon chain state transition function](#beacon-chain-state-transition-function) + - [Epoch processing](#epoch-processing) + - [Updated `process_epoch`](#updated-process_epoch) + - [Updated `process_registry_updates`](#updated--process_registry_updates) + - [New `process_pending_balance_deposits`](#new-process_pending_balance_deposits) + - [New `process_pending_consolidations`](#new-process_pending_consolidations) + - [Updated `process_effective_balance_updates`](#updated-process_effective_balance_updates) + - [Block processing](#block-processing) + - [Updated `get_expected_withdrawals`](#updated-get_expected_withdrawals) + - [Updated `process_withdrawals`](#updated-process_withdrawals) + - [Operations](#operations) + - [Updated `process_operations`](#updated-process_operations) + - [Deposits](#deposits) + - [Updated `apply_deposit`](#updated--apply_deposit) + - [New `is_valid_deposit_signature`](#new-is_valid_deposit_signature) + - [Modified `add_validator_to_registry`](#modified-add_validator_to_registry) + - [Updated `get_validator_from_deposit`](#updated-get_validator_from_deposit) + - [Withdrawals](#withdrawals) + - [New `process_execution_layer_withdraw_request`](#new-process_execution_layer_withdraw_request) + - [Consolidations](#consolidations) + - [New `process_consolidation`](#new-process_consolidation) + + + + +## Introduction + +See [a modest proposal](https://notes.ethereum.org/@mikeneuder/increase-maxeb), the [diff view](https://github.com/michaelneuder/consensus-specs/pull/3/files) and +[security considerations](https://notes.ethereum.org/@fradamt/meb-increase-security). + +*Note:* This specification is built upon [Deneb](../../deneb/beacon-chain.md). + +## Constants + +The following values are (non-configurable) constants used throughout the specification. + +### Withdrawal prefixes + +| Name | Value | +| - | - | +| `BLS_WITHDRAWAL_PREFIX` | `Bytes1('0x00')` | +| `ETH1_ADDRESS_WITHDRAWAL_PREFIX` | `Bytes1('0x01')` | +| `COMPOUNDING_WITHDRAWAL_PREFIX` | `Bytes1('0x02')` | + +### Domains + +| Name | Value | +| - | - | +| `DOMAIN_CONSOLIDATION` | `DomainType('0x0B000000')` | + +## Presets + +### Gwei values + +| Name | Value | +| - | - | +| `MIN_ACTIVATION_BALANCE` | `Gwei(2**5 * 10**9)` (= 32,000,000,000) | +| `MAX_EFFECTIVE_BALANCE_EIP7251` | `Gwei(2**11 * 10**9)` (= 2048,000,000,000) | + +### Rewards and penalties + +| Name | Value | +| - | - | +| `MIN_SLASHING_PENALTY_QUOTIENT_EIP7251` | `uint64(2**12)` (= 4,096) | +| `WHISTLEBLOWER_REWARD_QUOTIENT_EIP7251` | `uint64(2**12)` (= 4,096) | + +### Max operations per block + +| Name | Value | +| - | - | +| `MAX_CONSOLIDATIONS` | `uint64(1)` | + +### Execution + +| Name | Value | Description | +| - | - | - | +| `MAX_PARTIAL_WITHDRAWALS_PER_PAYLOAD` | `uint64(2**3)` (= 8) | Maximum amount of partial withdrawals allowed in each payload | + +### State list lengths + +| Name | Value | Unit | +| - | - | :-: | +| `PENDING_BALANCE_DEPOSITS_LIMIT` | `uint64(2**27)` (= 134,217,728) | pending balance deposits | +| `PENDING_PARTIAL_WITHDRAWALS_LIMIT` | `uint64(2**27)` (= 134,217,728) | pending partial withdrawals | +| `PENDING_CONSOLIDATIONS_LIMIT` | `uint64(2**18)` (= 262,144) | pending consolidations | + + +## Configuration + +### Validator cycle + +| Name | Value | +| - | - | +| `MIN_PER_EPOCH_CHURN_LIMIT_EIP7251` | `Gwei(2**7 * 10**9)` (= 128,000,000,000) | # Equivalent to 4 32 ETH validators +| `MAX_PER_EPOCH_ACTIVATION_EXIT_CHURN_LIMIT` | `Gwei(2**8 * 10**9)` (256,000,000,000) | + + +## Containers + +### New containers + +#### New `PendingBalanceDeposit` + +```python +class PendingBalanceDeposit(Container): + index: ValidatorIndex + amount: Gwei +``` + +#### New `PartialWithdrawal` + +```python +class PartialWithdrawal(Container): + index: ValidatorIndex + amount: Gwei + withdrawable_epoch: Epoch +``` +#### New `ExecutionLayerWithdrawRequest` + +```python +class ExecutionLayerWithdrawRequest(Container): + source_address: ExecutionAddress + validator_pubkey: BLSPubkey + amount: Gwei +``` + +#### New `Consolidation` + +```python +class Consolidation(Container): + source_index: ValidatorIndex + target_index: ValidatorIndex + epoch: Epoch +``` + +#### New `SignedConsolidation` +```python +class SignedConsolidation(Container): + message: Consolidation + signature: BLSSignature +``` + +#### New `PendingConsolidation` +```python +class PendingConsolidation(Container): + source_index: ValidatorIndex + target_index: ValidatorIndex +``` + +### Extended Containers + +#### `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 + latest_execution_payload_header: ExecutionPayloadHeader + # Withdrawals + next_withdrawal_index: WithdrawalIndex + next_withdrawal_validator_index: ValidatorIndex + # Deep history valid from Capella onwards + historical_summaries: List[HistoricalSummary, HISTORICAL_ROOTS_LIMIT] + # EIP-7251 + deposit_balance_to_consume: Gwei # [New in EIP-7251] + exit_balance_to_consume: Gwei # [New in EIP-7251] + earliest_exit_epoch: Epoch # [New in EIP-7251] + consolidation_balance_to_consume: Gwei # [New in EIP-7251] + earliest_consolidation_epoch: Epoch # [New in EIP-7251] + pending_balance_deposits: List[PendingBalanceDeposit, PENDING_BALANCE_DEPOSITS_LIMIT] # [New in EIP-7251] + pending_partial_withdrawals: List[PartialWithdrawal, PENDING_PARTIAL_WITHDRAWALS_LIMIT] # [New in EIP-7251] + pending_consolidations: List[PendingConsolidation, PENDING_CONSOLIDATIONS_LIMIT] # [New in EIP-7251] +``` + +#### `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] + attestations: List[Attestation, MAX_ATTESTATIONS] + deposits: List[Deposit, MAX_DEPOSITS] + voluntary_exits: List[SignedVoluntaryExit, MAX_VOLUNTARY_EXITS] + sync_aggregate: SyncAggregate + # Execution + execution_payload: ExecutionPayload + bls_to_execution_changes: List[SignedBLSToExecutionChange, MAX_BLS_TO_EXECUTION_CHANGES] + blob_kzg_commitments: List[KZGCommitment, MAX_BLOB_COMMITMENTS_PER_BLOCK] + consolidations: List[SignedConsolidation, MAX_CONSOLIDATIONS] # [New in EIP-7251] +``` + +## Helpers + +### Predicates + +#### Updated `is_eligible_for_activation_queue` + +```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 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) +``` + +#### Updated `is_fully_withdrawable_validator` + +```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 EIP7251] + and validator.withdrawable_epoch <= epoch + and balance > 0 + ) +``` + +#### Updated `is_partially_withdrawable_validator` + +```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 EIP7251] + has_excess_balance = balance > max_effective_balance # [Modified in EIP7251] + return ( + has_execution_withdrawal_credential(validator) # [Modified in EIP7251] + and has_max_effective_balance + and has_excess_balance + ) +``` + +### Beacon state accessors + +#### New `get_validator_max_effective_balance` + +```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_EIP7251 + else: + return MIN_ACTIVATION_BALANCE +``` + +#### New `get_churn_limit` + +```python +def get_churn_limit(state: BeaconState) -> Gwei: + """ + Return the churn limit for the current epoch. + """ + churn = max( + MIN_PER_EPOCH_CHURN_LIMIT_EIP7251, + 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. + """ + return min(MAX_PER_EPOCH_ACTIVATION_EXIT_CHURN_LIMIT, get_churn_limit(state)) +``` + +#### New `get_consolidation_churn_limit` + +```python +def get_consolidation_churn_limit(state: BeaconState) -> Gwei: + return get_churn_limit(state) - get_activation_exit_churn_limit(state) +``` + +#### 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) +``` + +### Beacon state mutators + +#### Updated `initiate_validator_exit` + +```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 EIP 7251] + 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 `set_compounding_withdrawal_credentials` + +```python +def set_compounding_withdrawal_credentials(state: BeaconState, index: ValidatorIndex) -> None: + validator = state.validators[index] + if has_eth1_withdrawal_credential(validator): + validator.withdrawal_credentials[:1] = COMPOUNDING_WITHDRAWAL_PREFIX +``` + +#### 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): + validator.withdrawal_credentials[:1] = COMPOUNDING_WITHDRAWAL_PREFIX + 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 > MAX_EFFECTIVE_BALANCE: + excess_balance = balance - MAX_EFFECTIVE_BALANCE + state.balances[index] = balance - excess_balance + state.pending_balance_deposits.append( + PendingBalanceDeposit(index=index, amount=excess_balance) + ) +``` + +#### New `compute_exit_epoch_and_update_churn` + + +```python +def compute_exit_epoch_and_update_churn(state: BeaconState, exit_balance: Gwei) -> Epoch: + earliest_exit_epoch = compute_activation_exit_epoch(get_current_epoch(state)) + per_epoch_churn = get_activation_exit_churn_limit(state) + # New epoch for exits. + if state.earliest_exit_epoch < earliest_exit_epoch: + state.earliest_exit_epoch = earliest_exit_epoch + state.exit_balance_to_consume = per_epoch_churn + + if exit_balance <= state.exit_balance_to_consume: + # Exit fits in the current earliest epoch. + state.exit_balance_to_consume -= exit_balance + else: + # Exit doesn't fit in the current earliest epoch. + balance_to_process = exit_balance - state.exit_balance_to_consume + additional_epochs, remainder = divmod(balance_to_process, per_epoch_churn) + state.earliest_exit_epoch += additional_epochs + 1 + state.exit_balance_to_consume = per_epoch_churn - remainder + + 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: + earliest_consolidation_epoch = compute_activation_exit_epoch(get_current_epoch(state)) + per_epoch_consolidation_churn = get_consolidation_churn_limit(state) + # New epoch for consolidations. + if state.earliest_consolidation_epoch < earliest_consolidation_epoch: + state.earliest_consolidation_epoch = earliest_consolidation_epoch + state.consolidation_balance_to_consume = per_epoch_consolidation_churn + + if consolidation_balance <= state.consolidation_balance_to_consume: + # Consolidation fits in the current earliest consolidation epoch. + state.consolidation_balance_to_consume -= consolidation_balance + else: + # Consolidation doesn't fit in the current earliest epoch. + balance_to_process = consolidation_balance - state.consolidation_balance_to_consume + additional_epochs, remainder = divmod(balance_to_process, per_epoch_consolidation_churn) + state.earliest_consolidation_epoch += additional_epochs + 1 + state.consolidation_balance_to_consume = per_epoch_consolidation_churn - remainder + + return state.earliest_consolidation_epoch +``` + +#### Updated `slash_validator` + +```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 + slashing_penalty = validator.effective_balance // MIN_SLASHING_PENALTY_QUOTIENT_EIP7251 # [Modified in EIP7251] + 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_EIP7251) # [Modified in 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)) +``` + +## Beacon chain state transition function + +### Epoch processing + +#### Updated `process_epoch` +```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 EIP7251] + process_slashings(state) + process_eth1_data_reset(state) + process_pending_balance_deposits(state) # New in EIP7251 + process_pending_consolidations(state) # New in EIP7251 + process_effective_balance_updates(state) # [Modified in EIP7251] + process_slashings_reset(state) + process_randao_mixes_reset(state) +``` + +#### Updated `process_registry_updates` + +```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 +``` + +#### New `process_pending_balance_deposits` + +```python +def process_pending_balance_deposits(state: BeaconState) -> None: + available_for_processing = state.deposit_balance_to_consume + get_activation_exit_churn_limit(state) + processed_amount = 0 + next_deposit_index = 0 + + for deposit in state.pending_balance_deposits: + if processed_amount + deposit.amount > available_for_processing: + break + increase_balance(state, deposit.index, deposit.amount) + processed_amount += deposit.amount + next_deposit_index += 1 + + state.pending_balance_deposits = state.pending_balance_deposits[next_deposit_index:] + + if len(state.pending_balance_deposits) == 0: + state.deposit_balance_to_consume = 0 + else: + state.deposit_balance_to_consume = available_for_processing - processed_amount +``` + +#### New `process_pending_consolidations` + +```python +def process_pending_consolidations(state: BeaconState) -> None: + 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 + if source_validator.withdrawable_epoch > get_current_epoch(state): + 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:] +``` + +#### Updated `process_effective_balance_updates` + +```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 + EFFECTIVE_BALANCE_LIMIT = ( + MAX_EFFECTIVE_BALANCE_EIP7251 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) +``` + +### Block processing + +```python +def process_block(state: BeaconState, block: BeaconBlock) -> None: + process_block_header(state, block) + process_withdrawals(state, block.body.execution_payload) # [Modified in EIP7251] + process_execution_payload(state, block.body, EXECUTION_ENGINE) + process_randao(state, block.body) + process_eth1_data(state, block.body) + process_operations(state, block.body) # [Modified in EIP7251] + process_sync_aggregate(state, block.body.sync_aggregate) +``` + +##### Updated `get_expected_withdrawals` + +```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 EIP7251] Consume pending partial withdrawals + for withdrawal in state.pending_partial_withdrawals: + if withdrawal.withdrawable_epoch > epoch or len(withdrawals) == MAX_PARTIAL_WITHDRAWALS_PER_PAYLOAD: + break + + validator = state.validators[withdrawal.index] + if validator.exit_epoch == FAR_FUTURE_EPOCH and state.balances[withdrawal.index] > MIN_ACTIVATION_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) + # END: Consume pending partial 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 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 +``` + +##### Updated `process_withdrawals` + +```python +def process_withdrawals(state: BeaconState, payload: ExecutionPayload) -> None: + expected_withdrawals, partial_withdrawals_count = get_expected_withdrawals(state) # [Modified in 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) + + # [New in EIP7251] update pending partial withdrawals + 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 +``` + +#### Operations + +##### Updated `process_operations` + +```python +def process_operations(state: BeaconState, body: BeaconBlockBody) -> None: + # Verify that outstanding deposits are processed up to the maximum number of deposits + assert len(body.deposits) == min(MAX_DEPOSITS, state.eth1_data.deposit_count - state.eth1_deposit_index) + + 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) + for_ops(body.attestations, process_attestation) + for_ops(body.deposits, process_deposit) # [Modified in EIP7251] + for_ops(body.voluntary_exits, process_voluntary_exit) + for_ops(body.bls_to_execution_changes, process_bls_to_execution_change) + for_ops(body.execution_payload.withdraw_requests, process_execution_layer_withdraw_request) # New in EIP7251 + for_ops(body.consolidations, process_consolidation) # New in EIP7251 +``` + +##### Deposits + +###### Updated `apply_deposit` + +```python +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): + add_validator_to_registry(state, pubkey, withdrawal_credentials, amount) + else: + # Increase balance by deposit amount + index = ValidatorIndex(validator_pubkeys.index(pubkey)) + state.pending_balance_deposits.append( + PendingBalanceDeposit(index=index, amount=amount)) # [Modified in EIP-7251] + # Check if valid deposit switch to compounding credentials + if ( + is_compounding_withdrawal_credential(withdrawal_credentials) + and has_eth1_withdrawal_credential(state.validators[index]) + and is_valid_deposit_signature(pubkey, withdrawal_credentials, amount, signature) + ): + switch_to_compounding_validator(state, index) + +``` + +###### New `is_valid_deposit_signature` + +```python +def is_valid_deposit_signature(pubkey: BLSPubkey, + withdrawal_credentials: Bytes32, + amount: uint64, + signature: BLSSignature) -> None: + deposit_message = DepositMessage( + pubkey=pubkey, + withdrawal_credentials=withdrawal_credentials, + amount=amount, + ) + 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) +``` + +###### Modified `add_validator_to_registry` + +```python +def add_validator_to_registry(state: BeaconState, + pubkey: BLSPubkey, + withdrawal_credentials: Bytes32, + amount: uint64) -> None: + index = get_index_for_new_validator(state) + validator = get_validator_from_deposit(pubkey, withdrawal_credentials) + set_or_append_list(state.validators, index, validator) + set_or_append_list(state.balances, index, 0) # [Modified in EIP7251] + 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)) + state.pending_balance_deposits.append(PendingBalanceDeposit(index=index, amount=amount)) # [New in EIP7251] +``` + +###### Updated `get_validator_from_deposit` + +```python +def get_validator_from_deposit(pubkey: BLSPubkey, withdrawal_credentials: Bytes32) -> Validator: + 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=0, # [Modified in EIP7251] + ) +``` + +##### Withdrawals + +###### New `process_execution_layer_withdraw_request` + +```python +def process_execution_layer_withdraw_request( + state: BeaconState, + execution_layer_withdraw_request: ExecutionLayerWithdrawRequest +) -> None: + amount = execution_layer_withdraw_request.amount + is_full_exit_request = amount == 0 + # If partial withdrawal queue is full, only full exits are processed + if len(state.pending_partial_withdrawals) >= PENDING_PARTIAL_WITHDRAWALS_LIMIT and not is_full_exit_request: + return + + validator_pubkeys = [v.pubkey for v in state.validators] + index = ValidatorIndex(validator_pubkeys.index(execution_layer_withdraw_request.validator_pubkey)) + validator = state.validators[index] + + # Same conditions as in EIP-7002 + if not ( + has_execution_withdrawal_credential(validator) + # Verify withdrawal credentials + and validator.withdrawal_credentials[12:] == execution_layer_withdraw_request.source_address + # Verify the validator is active + and is_active_validator(validator, get_current_epoch(state)) + # Verify exit has not been initiated, and slashed + and validator.exit_epoch == FAR_FUTURE_EPOCH + # Verify the validator has been active long enough + and get_current_epoch(state) >= validator.activation_epoch + SHARD_COMMITTEE_PERIOD + ): + return + # New condition: only allow partial withdrawals with compounding withdrawal credentials + if not (is_full_exit_request or has_compounding_withdrawal_credential(validator)): + return + + pending_balance_to_withdraw = sum( + item.amount for item in state.pending_partial_withdrawals if item.index == index + ) + # only exit validator if it has no pending withdrawals in the queue + if is_full_exit_request and pending_balance_to_withdraw > 0: + return + + if is_full_exit_request: + initiate_validator_exit(state, index) + elif state.balances[index] > MIN_ACTIVATION_BALANCE + pending_balance_to_withdraw: + 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(PartialWithdrawal( + index=index, + amount=to_withdraw, + withdrawable_epoch=withdrawable_epoch, + )) +``` + +##### Consolidations + +###### New `process_consolidation` + +```python +def process_consolidation(state: BeaconState, signed_consolidation: SignedConsolidation) -> None: + # If the pending consolidations queue is full, no consolidations are allowed in the block + assert len(state.pending_consolidations) < PENDING_CONSOLIDATIONS_LIMIT + # If there is too little available consolidation churn limit, no consolidations are allowed in the block + assert get_consolidation_churn_limit(state) > MIN_ACTIVATION_BALANCE + consolidation = signed_consolidation.message + # Verify that source != target, so a consolidation cannot be used as an exit. + assert consolidation.source_index != consolidation.target_index + + source_validator = state.validators[consolidation.source_index] + target_validator = state.validators[consolidation.target_index] + # Verify the source and the target are active + current_epoch = get_current_epoch(state) + assert is_active_validator(source_validator, current_epoch) + assert is_active_validator(target_validator, current_epoch) + # Verify exits for source and target have not been initiated + assert source_validator.exit_epoch == FAR_FUTURE_EPOCH + assert target_validator.exit_epoch == FAR_FUTURE_EPOCH + # Consolidations must specify an epoch when they become valid; they are not valid before then + assert current_epoch >= consolidation.epoch + + # Verify the source and the target have Execution layer withdrawal credentials + assert has_execution_withdrawal_credential(source_validator) + assert has_execution_withdrawal_credential(target_validator) + # Verify the same withdrawal address + assert source_validator.withdrawal_credentials[1:] == target_validator.withdrawal_credentials[1:] + + # Verify consolidation is signed by the source and the target + domain = compute_domain(DOMAIN_CONSOLIDATION, genesis_validators_root=state.genesis_validators_root) + signing_root = compute_signing_root(consolidation, domain) + pubkeys = [source_validator.pubkey, target_validator.pubkey] + assert bls.FastAggregateVerify(pubkeys, signing_root, signed_consolidation.signature) + + # Initiate source validator exit and append pending consolidation + active_balance = get_active_balance(state, consolidation.source_index) + source_validator.exit_epoch = compute_consolidation_epoch_and_update_churn(state, active_balance) + source_validator.withdrawable_epoch = Epoch( + source_validator.exit_epoch + MIN_VALIDATOR_WITHDRAWABILITY_DELAY + ) + state.pending_consolidations.append(PendingConsolidation( + source_index=consolidation.source_index, + target_index=consolidation.target_index + )) +``` + diff --git a/specs/_features/eip7251/fork.md b/specs/_features/eip7251/fork.md new file mode 100644 index 000000000..609b4f3f8 --- /dev/null +++ b/specs/_features/eip7251/fork.md @@ -0,0 +1,139 @@ +# EIP7251 -- Fork Logic + +**Notice**: This document is a work-in-progress for researchers and implementers. + +## Table of contents + + + + +- [Introduction](#introduction) +- [Configuration](#configuration) +- [Helper functions](#helper-functions) + - [Misc](#misc) + - [Modified `compute_fork_version`](#modified-compute_fork_version) +- [Fork to EIP7251](#fork-to-eip7251) + - [Fork trigger](#fork-trigger) + - [Upgrading the state](#upgrading-the-state) + + + +## Introduction + +This document describes the process of the EIP7251 upgrade. + +## Configuration + +Warning: this configuration is not definitive. + +| Name | Value | +| - | - | +| `EIP7251_FORK_VERSION` | `Version('0x06000000')` | +| `EIP7251_FORK_EPOCH` | `Epoch(18446744073709551615)` **TBD** | + +## Helper functions + +### Misc + +#### Modified `compute_fork_version` + +```python +def compute_fork_version(epoch: Epoch) -> Version: + """ + Return the fork version at the given ``epoch``. + """ + if epoch >= EIP7251_FORK_EPOCH: + return EIP7251_FORK_VERSION + if epoch >= DENEB_FORK_EPOCH: + return DENEB_FORK_VERSION + if epoch >= CAPELLA_FORK_EPOCH: + return CAPELLA_FORK_VERSION + if epoch >= BELLATRIX_FORK_EPOCH: + return BELLATRIX_FORK_VERSION + if epoch >= ALTAIR_FORK_EPOCH: + return ALTAIR_FORK_VERSION + return GENESIS_FORK_VERSION +``` + +## Fork to EIP7251 + +### Fork trigger + +TBD. This fork is defined for testing purposes, the EIP may be combined with other consensus-layer upgrade. +For now, we assume the condition will be triggered at epoch `EIP7251_FORK_EPOCH`. + +Note that for the pure EIP7251 networks, we don't apply `upgrade_to_eip7251` since it starts with EIP7251 version logic. + +### Upgrading the state + +If `state.slot % SLOTS_PER_EPOCH == 0` and `compute_epoch_at_slot(state.slot) == EIP7251_FORK_EPOCH`, +an irregular state change is made to upgrade to EIP7251. + +```python +def upgrade_to_eip7251(pre: deneb.BeaconState) -> BeaconState: + post = BeaconState( + # Versioning + genesis_time=pre.genesis_time, + genesis_validators_root=pre.genesis_validators_root, + slot=pre.slot, + fork=Fork( + previous_version=pre.fork.current_version, + current_version=EIP7251_FORK_VERSION, + epoch=deneb.get_current_epoch(pre), + ), + # History + latest_block_header=pre.latest_block_header, + block_roots=pre.block_roots, + state_roots=pre.state_roots, + historical_roots=pre.historical_roots, + # Eth1 + eth1_data=pre.eth1_data, + eth1_data_votes=pre.eth1_data_votes, + eth1_deposit_index=pre.eth1_deposit_index, + # Registry + validators=pre.validators, + balances=pre.balances, + # Randomness + randao_mixes=pre.randao_mixes, + # Slashings + slashings=pre.slashings, + # Participation + previous_epoch_participation=pre.previous_epoch_participation, + current_epoch_participation=pre.current_epoch_participation, + # Finality + justification_bits=pre.justification_bits, + previous_justified_checkpoint=pre.previous_justified_checkpoint, + current_justified_checkpoint=pre.current_justified_checkpoint, + finalized_checkpoint=pre.finalized_checkpoint, + # Inactivity + inactivity_scores=pre.inactivity_scores, + # Sync + current_sync_committee=pre.current_sync_committee, + next_sync_committee=pre.next_sync_committee, + # Execution-layer + latest_execution_payload_header=pre.latest_execution_payload_header, + # Withdrawals + next_withdrawal_index=pre.next_withdrawal_index, + next_withdrawal_validator_index=pre.next_withdrawal_validator_index, + # Deep history valid from Capella onwards + historical_summaries=pre.historical_summaries, + # [New in EIP7251] + deposit_balance_to_consume=0, + exit_balance_to_consume=get_activation_exit_churn_limit(pre), + earliest_exit_epoch=max([v.exit_epoch for v in pre.validators if v.exit_epoch != FAR_FUTURE_EPOCH]) + 1, + consolidation_balance_to_consume=get_consolidation_churn_limit(pre), + earliest_consolidation_epoch=compute_activation_exit_epoch(get_current_epoch(pre)), + pending_balance_deposits=[], + pending_partial_withdrawals=[], + pending_consolidations=[], + ) + + # Ensure early adopters of compounding credentials go through the activation churn + queue_excess_active_balance(post) + for index, validator in enumerate(post.validators): + if has_compounding_withdrawal_credential(validator): + queue_excess_active_balance(post, index) + + return post +``` + diff --git a/specs/_features/eip7549/beacon-chain.md b/specs/_features/eip7549/beacon-chain.md index bf1d37c67..3b20e0c63 100644 --- a/specs/_features/eip7549/beacon-chain.md +++ b/specs/_features/eip7549/beacon-chain.md @@ -45,7 +45,7 @@ This is the beacon chain specification to move the attestation committee index o ```python class Attestation(Container): - aggregation_bits_list: List[Bitlist[MAX_VALIDATORS_PER_COMMITTEE], MAX_COMMITTEES_PER_SLOT] # [Modified in EIP7549] + aggregation_bits: Bitlist[MAX_VALIDATORS_PER_COMMITTEE * MAX_COMMITTEES_PER_SLOT] # [Modified in EIP7549] data: AttestationData committee_bits: Bitvector[MAX_COMMITTEES_PER_SLOT] # [New in EIP7549] signature: BLSSignature @@ -99,18 +99,19 @@ def get_committee_indices(commitee_bits: Bitvector) -> Sequence[CommitteeIndex]: ```python def get_attesting_indices(state: BeaconState, attestation: Attestation) -> Set[ValidatorIndex]: """ - Return the set of attesting indices corresponding to ``aggregation_bits_list`` and ``committee_bits``. + Return the set of attesting indices corresponding to ``aggregation_bits`` and ``committee_bits``. """ output = set() committee_indices = get_committee_indices(attestation.committee_bits) - aggregation_bits_list_index = 0 + committee_offset = 0 for index in committee_indices: - attesting_bits = attestation.aggregation_bits_list[aggregation_bits_list_index] - aggregation_bits_list_index += 1 committee = get_beacon_committee(state, attestation.data.slot, index) - committee_attesters = set(index for i, index in enumerate(committee) if attesting_bits[i]) + 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 ``` @@ -128,13 +129,13 @@ def process_attestation(state: BeaconState, attestation: Attestation) -> None: # [Modified in EIP7549] assert data.index == 0 committee_indices = get_committee_indices(attestation.committee_bits) - assert len(committee_indices) == len(attestation.aggregation_bits_list) - aggregation_bits_list_index = 0 + 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) - assert len(attestation.aggregation_bits_list[aggregation_bits_list_index]) == len(committee) - aggregation_bits_list_index += 1 + 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) diff --git a/specs/_features/eip7549/fork.md b/specs/_features/eip7549/fork.md index c1da373d8..9294670db 100644 --- a/specs/_features/eip7549/fork.md +++ b/specs/_features/eip7549/fork.md @@ -28,7 +28,7 @@ Warning: this configuration is not definitive. | Name | Value | | - | - | -| `EIP7549_FORK_VERSION` | `Version('0x05000000')` | +| `EIP7549_FORK_VERSION` | `Version('0x06000000')` | | `EIP7549_FORK_EPOCH` | `Epoch(18446744073709551615)` **TBD** | ## Helper functions diff --git a/specs/_features/eip7549/p2p-interface.md b/specs/_features/eip7549/p2p-interface.md index a93fb9bc9..20aa291b3 100644 --- a/specs/_features/eip7549/p2p-interface.md +++ b/specs/_features/eip7549/p2p-interface.md @@ -36,19 +36,17 @@ The `beacon_aggregate_and_proof` and `beacon_attestation_{subnet_id}` topics are The following convenience variables are re-defined - `index = get_committee_indices(aggregate.committee_bits)[0]` -- `aggregation_bits = aggregate.aggregation_bits_list[0]` The following validations are added: -* [REJECT] `len(committee_indices) == len(aggregate.attestation_bits) == 1`, where `committee_indices = get_committee_indices(aggregate)`. +* [REJECT] `len(committee_indices) == 1`, where `committee_indices = get_committee_indices(aggregate)`. * [REJECT] `aggregate.data.index == 0` ###### `beacon_attestation_{subnet_id}` The following convenience variables are re-defined - `index = get_committee_indices(attestation.committee_bits)[0]` -- `aggregation_bits = attestation.aggregation_bits_list[0]` The following validations are added: -* [REJECT] `len(committee_indices) == len(attestation.attestation_bits) == 1`, where `committee_indices = get_committee_indices(attestation)`. +* [REJECT] `len(committee_indices) == 1`, where `committee_indices = get_committee_indices(attestation)`. * [REJECT] `attestation.data.index == 0` diff --git a/specs/_features/eip7549/validator.md b/specs/_features/eip7549/validator.md index ab296eb45..d904e23f9 100644 --- a/specs/_features/eip7549/validator.md +++ b/specs/_features/eip7549/validator.md @@ -31,19 +31,36 @@ Changed the max attestations size to `MAX_ATTESTER_SLASHINGS_EIP7549`. ##### Attestations -Changed the max attestations size to `MAX_ATTESTATIONS_EIP7549`. +The network attestation aggregates contain only the assigned committee attestations. +Attestation aggregates received by the block proposer from the committee aggregators with disjoint `committee_bits` sets and equal `AttestationData` SHOULD be consolidated into a single `Attestation` object. +The proposer should run the following function to construct an on chain final aggregate form a list of network aggregates with equal `AttestationData`: -Attestations received from aggregators with disjoint `committee_bits` sets and equal `AttestationData` SHOULD be consolidated into a single `Attestation` object. +```python +def compute_on_chain_aggregate(network_aggregates: Sequence[Attestation]) -> Attestation: + aggregates = sorted(network_aggregates, key=lambda a: get_committee_indices(a.committee_bits)[0]) + + data = aggregates[0].data + aggregation_bits = Bitlist[MAX_VALIDATORS_PER_COMMITTEE * MAX_COMMITTEES_PER_SLOT]() + for a in aggregates: + for b in a.aggregation_bits: + aggregation_bits.append(b) + + signature = bls.Aggregate([a.signature for a in aggregates]) + + committee_indices = [get_committee_indices(a.committee_bits)[0] for a in aggregates] + committee_flags = [(index in committee_indices) for index in range(0, MAX_COMMITTEES_PER_SLOT)] + committee_bits = Bitvector[MAX_COMMITTEES_PER_SLOT](committee_flags) + + return Attestation(aggregation_bits, data, committee_bits, signature) +``` ### Attesting #### Construct attestation - Set `attestation_data.index = 0`. -- Let `aggregation_bits_list` be a `Bitlist[MAX_VALIDATORS_PER_COMMITTEE]` of length `len(committee)`, where the bit of the index of the validator in the `committee` is set to `0b1`. -- Set `attestation.aggregation_bits_list = [aggregation_bits_list]`, a list of length 1 -- Let `committee_bits` be a `Bitvector[MAX_COMMITTEES_PER_SLOT]`, where the bit at the index associated with the validator's committee is set to `0b1` -- Set `attestation.committee_bits = committee_bits` +- Let `attestation.aggregation_bits` be a `Bitlist[MAX_VALIDATORS_PER_COMMITTEE * MAX_COMMITTEES_PER_SLOT]` of length `len(committee)`, where the bit of the index of the validator in the `committee` is set to `0b1`. +- Let `attestation.committee_bits` be a `Bitvector[MAX_COMMITTEES_PER_SLOT]`, where the bit at the index associated with the validator's committee is set to `0b1`. *Note*: Calling `get_attesting_indices(state, attestation)` should return a list of length equal to 1, containing `validator_index`. @@ -52,6 +69,6 @@ Attestations received from aggregators with disjoint `committee_bits` sets and e #### Construct aggregate - Set `attestation_data.index = 0`. -- Let `aggregation_bits` be a `Bitlist[MAX_VALIDATORS_PER_COMMITTEE]` of length `len(committee)`, where each bit set from each individual attestation is set to `0b1`. -- Set `attestation.aggregation_bits_list = [aggregation_bits]`, a list of length 1 -- Set `attestation.committee_bits = committee_bits`, where `committee_bits` has the same value as in each individual attestation +- Let `aggregation_bits` be a `Bitlist[MAX_VALIDATORS_PER_COMMITTEE * MAX_COMMITTEES_PER_SLOT]` of length `len(committee)`, where each bit set from each individual attestation is set to `0b1`. +- Set `attestation.committee_bits = committee_bits`, where `committee_bits` has the same value as in each individual attestation. + diff --git a/specs/_features/whisk/fork.md b/specs/_features/whisk/fork.md index ef3eb0846..300d19129 100644 --- a/specs/_features/whisk/fork.md +++ b/specs/_features/whisk/fork.md @@ -41,7 +41,7 @@ Warning: this configuration is not definitive. | Name | Value | | -------------------- | ----------------------- | -| `WHISK_FORK_VERSION` | `Version('0x05000000')` | +| `WHISK_FORK_VERSION` | `Version('0x08000000')` | | `WHISK_FORK_EPOCH` | `Epoch(18446744073709551615)` **TBD** | ## Fork to Whisk diff --git a/specs/phase0/p2p-interface.md b/specs/phase0/p2p-interface.md index a34b34233..ff1bd03a1 100644 --- a/specs/phase0/p2p-interface.md +++ b/specs/phase0/p2p-interface.md @@ -245,7 +245,7 @@ The following gossipsub [parameters](https://github.com/libp2p/specs/blob/master - `fanout_ttl` (ttl for fanout maps for topics we are not subscribed to but have published to, seconds): 60 - `mcache_len` (number of windows to retain full messages in cache for `IWANT` responses): 6 - `mcache_gossip` (number of windows to gossip about): 3 -- `seen_ttl` (number of heartbeat intervals to retain message IDs): 550 +- `seen_ttl` (expiry time for cache of seen message ids, seconds): SECONDS_PER_SLOT * SLOTS_PER_EPOCH * 2 *Note*: Gossipsub v1.1 introduces a number of [additional parameters](https://github.com/libp2p/specs/blob/master/pubsub/gossipsub/gossipsub-v1.1.md#overview-of-new-parameters) @@ -367,7 +367,7 @@ The following validations MUST pass before forwarding the `signed_aggregate_and_ - _[REJECT]_ The aggregate attestation's epoch matches its target -- i.e. `aggregate.data.target.epoch == compute_epoch_at_slot(aggregate.data.slot)` - _[REJECT]_ The number of aggregation bits matches the committee size -- i.e. - `len(aggregation_bits) == len(get_beacon_committee(state, aggregate.data.slot, aggregate.data.index))`. + `len(aggregation_bits) == len(get_beacon_committee(state, aggregate.data.slot, index))`. - _[REJECT]_ The aggregate attestation has participants -- that is, `len(get_attesting_indices(state, aggregate)) >= 1`. - _[IGNORE]_ A valid aggregate attestation defined by `hash_tree_root(aggregate.data)` whose `aggregation_bits` is a non-strict superset has _not_ already been seen. diff --git a/tests/core/pyspec/eth2spec/test/context.py b/tests/core/pyspec/eth2spec/test/context.py index 43562512f..64d363578 100644 --- a/tests/core/pyspec/eth2spec/test/context.py +++ b/tests/core/pyspec/eth2spec/test/context.py @@ -8,7 +8,7 @@ from eth2spec.utils import bls from .exceptions import SkippedTest from .helpers.constants import ( PHASE0, ALTAIR, BELLATRIX, CAPELLA, DENEB, - EIP6110, EIP7002, EIP7549, EIP7594, + EIP6110, EIP7002, EIP7251, EIP7549, EIP7594, WHISK, MINIMAL, ALL_PHASES, @@ -120,8 +120,7 @@ def scaled_churn_balances_min_churn_limit(spec: Spec): def scaled_churn_balances_equal_activation_churn_limit(spec: Spec): """ Helper method to create enough validators to scale the churn limit. - (This is *firmly* over the churn limit -- thus the +2 instead of just +1) - Usage: `@with_custom_state(balances_fn=scaled_churn_balances_exceed_activation_churn_limit, ...)` + Usage: `@with_custom_state(balances_fn=scaled_churn_balances_equal_activation_churn_limit, ...)` """ num_validators = spec.config.CHURN_LIMIT_QUOTIENT * (spec.config.MAX_PER_EPOCH_ACTIVATION_CHURN_LIMIT) return [spec.MAX_EFFECTIVE_BALANCE] * num_validators @@ -137,6 +136,19 @@ def scaled_churn_balances_exceed_activation_churn_limit(spec: Spec): return [spec.MAX_EFFECTIVE_BALANCE] * num_validators +def scaled_churn_balances_exceed_activation_exit_churn_limit(spec: Spec): + """ + Helper method to create enough validators to scale the churn limit. + (The number of validators is double the amount need for the max activation/exit churn limit) + Usage: `@with_custom_state(balances_fn=scaled_churn_balances_exceed_activation_churn_limit, ...)` + """ + num_validators = ( + 2 * spec.config.CHURN_LIMIT_QUOTIENT + * spec.config.MAX_PER_EPOCH_ACTIVATION_EXIT_CHURN_LIMIT + // spec.MIN_ACTIVATION_BALANCE) + return [spec.MIN_ACTIVATION_BALANCE] * num_validators + + with_state = with_custom_state(default_balances, default_activation_threshold) @@ -512,6 +524,7 @@ with_eip7002_and_later = with_all_phases_from(EIP7002) with_eip7549_and_later = with_all_phases_from(EIP7549) with_whisk_and_later = with_all_phases_from(WHISK, all_phases=ALLOWED_TEST_RUNNER_FORKS) with_eip7594_and_later = with_all_phases_from(EIP7594, all_phases=ALLOWED_TEST_RUNNER_FORKS) +with_eip7251_and_later = with_all_phases_from(EIP7251, all_phases=ALLOWED_TEST_RUNNER_FORKS) class quoted_str(str): diff --git a/tests/core/pyspec/eth2spec/test/eip7251/__init__.py b/tests/core/pyspec/eth2spec/test/eip7251/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/core/pyspec/eth2spec/test/eip7251/block_processing/__init__.py b/tests/core/pyspec/eth2spec/test/eip7251/block_processing/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/core/pyspec/eth2spec/test/eip7251/block_processing/test_process_consolidation.py b/tests/core/pyspec/eth2spec/test/eip7251/block_processing/test_process_consolidation.py new file mode 100644 index 000000000..29ee7d05e --- /dev/null +++ b/tests/core/pyspec/eth2spec/test/eip7251/block_processing/test_process_consolidation.py @@ -0,0 +1,631 @@ +from eth2spec.test.helpers.constants import MINIMAL +from eth2spec.test.context import ( + spec_state_test, + with_eip7251_and_later, + with_presets, + always_bls, + spec_test, single_phase, + with_custom_state, + scaled_churn_balances_exceed_activation_exit_churn_limit, + default_activation_threshold, +) +from eth2spec.test.helpers.keys import pubkey_to_privkey +from eth2spec.test.helpers.consolidations import ( + run_consolidation_processing, + sign_consolidation, +) +from eth2spec.test.helpers.withdrawals import ( + set_eth1_withdrawal_credential_with_balance, + set_compounding_withdrawal_credential, +) + +# *********************** +# * CONSOLIDATION TESTS * +# *********************** + + +@with_eip7251_and_later +@with_presets([MINIMAL], "need sufficient consolidation churn limit") +@with_custom_state( + balances_fn=scaled_churn_balances_exceed_activation_exit_churn_limit, threshold_fn=default_activation_threshold) +@spec_test +@single_phase +def test_basic_consolidation(spec, state): + print(spec.config.PRESET_BASE) + # This state has 256 validators each with 32 ETH in MINIMAL preset, 128 ETH consolidation churn + consolidation_churn_limit = spec.get_consolidation_churn_limit(state) + # Set the consolidation balance to consume equal to churn limit + state.consolidation_balance_to_consume = consolidation_churn_limit + current_epoch = spec.get_current_epoch(state) + source_index = spec.get_active_validator_indices(state, current_epoch)[0] + target_index = spec.get_active_validator_indices(state, current_epoch)[1] + source_privkey = pubkey_to_privkey[state.validators[source_index].pubkey] + target_privkey = pubkey_to_privkey[state.validators[target_index].pubkey] + + # Set source and target withdrawal credentials to the same eth1 credential + set_eth1_withdrawal_credential_with_balance(spec, state, source_index) + set_eth1_withdrawal_credential_with_balance(spec, state, target_index) + + signed_consolidation = sign_consolidation(spec, state, + spec.Consolidation( + epoch=current_epoch, + source_index=source_index, + target_index=target_index), + source_privkey, target_privkey) + yield from run_consolidation_processing(spec, state, signed_consolidation) + + expected_exit_epoch = spec.compute_activation_exit_epoch(current_epoch) + # Check consolidation churn is decremented correctly + assert state.consolidation_balance_to_consume == consolidation_churn_limit - spec.MIN_ACTIVATION_BALANCE + # Check exit epoch + assert state.validators[0].exit_epoch == expected_exit_epoch + + +@with_eip7251_and_later +@with_presets([MINIMAL], "need sufficient consolidation churn limit") +@with_custom_state( + balances_fn=scaled_churn_balances_exceed_activation_exit_churn_limit, threshold_fn=default_activation_threshold) +@spec_test +@single_phase +def test_basic_consolidation_with_compounding_credential(spec, state): + # This state has 256 validators each with 32 ETH in MINIMAL preset, 128 ETH consolidation churn + consolidation_churn_limit = spec.get_consolidation_churn_limit(state) + # Set the consolidation balance to consume equal to churn limit + state.consolidation_balance_to_consume = consolidation_churn_limit + current_epoch = spec.get_current_epoch(state) + source_index = spec.get_active_validator_indices(state, current_epoch)[0] + target_index = spec.get_active_validator_indices(state, current_epoch)[1] + source_privkey = pubkey_to_privkey[state.validators[source_index].pubkey] + target_privkey = pubkey_to_privkey[state.validators[target_index].pubkey] + + # Set source and target withdrawal credentials to the same eth1 credential + set_compounding_withdrawal_credential(spec, state, source_index) + set_compounding_withdrawal_credential(spec, state, target_index) + + signed_consolidation = sign_consolidation(spec, state, + spec.Consolidation( + epoch=current_epoch, + source_index=source_index, + target_index=target_index), + source_privkey, target_privkey) + yield from run_consolidation_processing(spec, state, signed_consolidation) + + expected_exit_epoch = spec.compute_activation_exit_epoch(current_epoch) + # Check consolidation churn is decremented correctly + assert state.consolidation_balance_to_consume == consolidation_churn_limit - spec.MIN_ACTIVATION_BALANCE + # Check exit epoch + assert state.validators[0].exit_epoch == expected_exit_epoch + + +@with_eip7251_and_later +@with_presets([MINIMAL], "need sufficient consolidation churn limit") +@with_custom_state( + balances_fn=scaled_churn_balances_exceed_activation_exit_churn_limit, threshold_fn=default_activation_threshold) +@spec_test +@single_phase +def test_consolidation_churn_limit_balance(spec, state): + # This state has 256 validators each with 32 ETH in MINIMAL preset, 128 ETH consolidation churn + consolidation_churn_limit = spec.get_consolidation_churn_limit(state) + # Set the consolidation balance to consume equal to churn limit + state.consolidation_balance_to_consume = consolidation_churn_limit + current_epoch = spec.get_current_epoch(state) + + source_index = spec.get_active_validator_indices(state, current_epoch)[0] + # Set source balance to consolidation churn limit + state.balances[source_index] = consolidation_churn_limit + target_index = spec.get_active_validator_indices(state, current_epoch)[1] + source_privkey = pubkey_to_privkey[state.validators[source_index].pubkey] + target_privkey = pubkey_to_privkey[state.validators[target_index].pubkey] + + # Set source and target withdrawal credentials to the same eth1 credential + set_compounding_withdrawal_credential(spec, state, source_index) + set_compounding_withdrawal_credential(spec, state, target_index) + + signed_consolidation = sign_consolidation(spec, state, + spec.Consolidation( + epoch=current_epoch, + source_index=source_index, + target_index=target_index), + source_privkey, target_privkey) + yield from run_consolidation_processing(spec, state, signed_consolidation) + + expected_exit_epoch = spec.compute_activation_exit_epoch(current_epoch) + # Check consolidation churn is decremented correctly + assert state.consolidation_balance_to_consume == 0 + # Check exit epoch + assert state.validators[0].exit_epoch == expected_exit_epoch + + +@with_eip7251_and_later +@with_presets([MINIMAL], "need sufficient consolidation churn limit") +@with_custom_state( + balances_fn=scaled_churn_balances_exceed_activation_exit_churn_limit, threshold_fn=default_activation_threshold) +@spec_test +@single_phase +def test_consolidation_balance_larger_than_churn_limit(spec, state): + # This state has 256 validators each with 32 ETH in MINIMAL preset, 128 ETH consolidation churn + consolidation_churn_limit = spec.get_consolidation_churn_limit(state) + # Set the consolidation balance to consume equal to churn limit + state.consolidation_balance_to_consume = consolidation_churn_limit + current_epoch = spec.get_current_epoch(state) + + source_index = spec.get_active_validator_indices(state, current_epoch)[0] + # Set source balance higher than consolidation churn limit + state.balances[source_index] = consolidation_churn_limit + 1 + target_index = spec.get_active_validator_indices(state, current_epoch)[1] + source_privkey = pubkey_to_privkey[state.validators[source_index].pubkey] + target_privkey = pubkey_to_privkey[state.validators[target_index].pubkey] + + # Set source and target withdrawal credentials to the same eth1 credential + set_compounding_withdrawal_credential(spec, state, source_index) + set_compounding_withdrawal_credential(spec, state, target_index) + + signed_consolidation = sign_consolidation(spec, state, + spec.Consolidation( + epoch=current_epoch, + source_index=source_index, + target_index=target_index), + source_privkey, target_privkey) + yield from run_consolidation_processing(spec, state, signed_consolidation) + + expected_exit_epoch = spec.compute_activation_exit_epoch(current_epoch) + 1 + # Check consolidation churn is decremented correctly + assert state.consolidation_balance_to_consume == consolidation_churn_limit - 1 + # Check exit epoch + assert state.validators[0].exit_epoch == expected_exit_epoch + + +@with_eip7251_and_later +@with_presets([MINIMAL], "need sufficient consolidation churn limit") +@with_custom_state( + balances_fn=scaled_churn_balances_exceed_activation_exit_churn_limit, threshold_fn=default_activation_threshold) +@spec_test +@single_phase +def test_consolidation_balance_twice_the_churn_limit(spec, state): + # This state has 256 validators each with 32 ETH in MINIMAL preset, 128 ETH consolidation churn + consolidation_churn_limit = spec.get_consolidation_churn_limit(state) + # Set the consolidation balance to consume equal to churn limit + state.consolidation_balance_to_consume = consolidation_churn_limit + current_epoch = spec.get_current_epoch(state) + + source_index = spec.get_active_validator_indices(state, current_epoch)[0] + target_index = spec.get_active_validator_indices(state, current_epoch)[1] + source_privkey = pubkey_to_privkey[state.validators[source_index].pubkey] + target_privkey = pubkey_to_privkey[state.validators[target_index].pubkey] + + # Set source and target withdrawal credentials to the same eth1 credential + set_compounding_withdrawal_credential(spec, state, source_index) + set_compounding_withdrawal_credential(spec, state, target_index) + + # Set source balance higher than consolidation churn limit + state.balances[source_index] = 2 * consolidation_churn_limit + + signed_consolidation = sign_consolidation(spec, state, + spec.Consolidation( + epoch=current_epoch, + source_index=source_index, + target_index=target_index), + source_privkey, target_privkey) + yield from run_consolidation_processing(spec, state, signed_consolidation) + + # when exiting a multiple of the churn limit greater than 1, an extra exit epoch is added + expected_exit_epoch = spec.compute_activation_exit_epoch(current_epoch) + 2 + assert state.validators[0].exit_epoch == expected_exit_epoch + # since the earliest exit epoch moves to a new one, consolidation balance is back to full + assert state.consolidation_balance_to_consume == consolidation_churn_limit + + +@with_eip7251_and_later +@with_presets([MINIMAL], "need sufficient consolidation churn limit") +@with_custom_state( + balances_fn=scaled_churn_balances_exceed_activation_exit_churn_limit, threshold_fn=default_activation_threshold) +@spec_test +@single_phase +def test_multiple_consolidations_below_churn(spec, state): + # This state has 256 validators each with 32 ETH in MINIMAL preset, 128 ETH consolidation churn + consolidation_churn_limit = spec.get_consolidation_churn_limit(state) + # Set the consolidation balance to consume equal to churn limit + state.consolidation_balance_to_consume = consolidation_churn_limit + current_epoch = spec.get_current_epoch(state) + + yield "pre", state + # Prepare a bunch of consolidations, based on the current state + consolidations = [] + for i in range(3): + source_index = 2 * i + target_index = 2 * i + 1 + source_privkey = pubkey_to_privkey[state.validators[source_index].pubkey] + target_privkey = pubkey_to_privkey[state.validators[target_index].pubkey] + # Set source and target withdrawal credentials to the same eth1 credential + set_eth1_withdrawal_credential_with_balance(spec, state, source_index) + set_eth1_withdrawal_credential_with_balance(spec, state, target_index) + signed_consolidation = sign_consolidation(spec, state, + spec.Consolidation( + epoch=current_epoch, + source_index=source_index, + target_index=target_index), + source_privkey, target_privkey) + consolidations.append(signed_consolidation) + + # Now run all the consolidations + for consolidation in consolidations: + # the function yields data, but we are just interested in running it here, ignore yields. + for _ in run_consolidation_processing(spec, state, consolidation): + continue + + yield "post", state + + expected_exit_epoch = spec.compute_activation_exit_epoch(current_epoch) + assert state.earliest_consolidation_epoch == expected_exit_epoch + assert state.consolidation_balance_to_consume == consolidation_churn_limit - 3 * spec.MIN_ACTIVATION_BALANCE + for i in range(3): + assert state.validators[2 * i].exit_epoch == expected_exit_epoch + + +@with_eip7251_and_later +@with_presets([MINIMAL], "need sufficient consolidation churn limit") +@with_custom_state( + balances_fn=scaled_churn_balances_exceed_activation_exit_churn_limit, threshold_fn=default_activation_threshold) +@spec_test +@single_phase +def test_multiple_consolidations_equal_churn(spec, state): + # This state has 256 validators each with 32 ETH in MINIMAL preset, 128 ETH consolidation churn + consolidation_churn_limit = spec.get_consolidation_churn_limit(state) + # Set the consolidation balance to consume equal to churn limit + state.consolidation_balance_to_consume = consolidation_churn_limit + current_epoch = spec.get_current_epoch(state) + + yield "pre", state + # Prepare a bunch of consolidations, based on the current state + consolidations = [] + for i in range(4): + source_index = 2 * i + target_index = 2 * i + 1 + source_privkey = pubkey_to_privkey[state.validators[source_index].pubkey] + target_privkey = pubkey_to_privkey[state.validators[target_index].pubkey] + # Set source and target withdrawal credentials to the same eth1 credential + set_eth1_withdrawal_credential_with_balance(spec, state, source_index) + set_eth1_withdrawal_credential_with_balance(spec, state, target_index) + signed_consolidation = sign_consolidation(spec, state, + spec.Consolidation( + epoch=current_epoch, + source_index=source_index, + target_index=target_index), + source_privkey, target_privkey) + consolidations.append(signed_consolidation) + + # Now run all the consolidations + for consolidation in consolidations: + # the function yields data, but we are just interested in running it here, ignore yields. + for _ in run_consolidation_processing(spec, state, consolidation): + continue + + yield "post", state + + expected_exit_epoch = spec.compute_activation_exit_epoch(current_epoch) + assert state.earliest_consolidation_epoch == expected_exit_epoch + assert state.consolidation_balance_to_consume == 0 + for i in range(4): + assert state.validators[2 * i].exit_epoch == expected_exit_epoch + + +@with_eip7251_and_later +@with_presets([MINIMAL], "need sufficient consolidation churn limit") +@with_custom_state( + balances_fn=scaled_churn_balances_exceed_activation_exit_churn_limit, threshold_fn=default_activation_threshold) +@spec_test +@single_phase +def test_multiple_consolidations_above_churn(spec, state): + # This state has 256 validators each with 32 ETH in MINIMAL preset, 128 ETH consolidation churn + consolidation_churn_limit = spec.get_consolidation_churn_limit(state) + # Set the consolidation balance to consume equal to churn limit + state.consolidation_balance_to_consume = consolidation_churn_limit + current_epoch = spec.get_current_epoch(state) + + # Prepare a bunch of consolidations, based on the current state + consolidations = [] + for i in range(4): + source_index = 2 * i + target_index = 2 * i + 1 + source_privkey = pubkey_to_privkey[state.validators[source_index].pubkey] + target_privkey = pubkey_to_privkey[state.validators[target_index].pubkey] + # Set source and target withdrawal credentials to the same eth1 credential + set_eth1_withdrawal_credential_with_balance(spec, state, source_index) + set_eth1_withdrawal_credential_with_balance(spec, state, target_index) + signed_consolidation = sign_consolidation(spec, state, + spec.Consolidation( + epoch=current_epoch, + source_index=source_index, + target_index=target_index), + source_privkey, target_privkey) + consolidations.append(signed_consolidation) + + # Now run all the consolidations + for consolidation in consolidations: + # the function yields data, but we are just interested in running it here, ignore yields. + for _ in run_consolidation_processing(spec, state, consolidation): + continue + + # consolidate an additional validator + source_index = spec.get_active_validator_indices(state, current_epoch)[-2] + target_index = spec.get_active_validator_indices(state, current_epoch)[-1] + source_privkey = pubkey_to_privkey[state.validators[source_index].pubkey] + target_privkey = pubkey_to_privkey[state.validators[target_index].pubkey] + + # Set source and target withdrawal credentials to the same eth1 credential + set_eth1_withdrawal_credential_with_balance(spec, state, source_index) + set_eth1_withdrawal_credential_with_balance(spec, state, target_index) + + signed_consolidation = sign_consolidation(spec, state, + spec.Consolidation( + epoch=current_epoch, + source_index=source_index, + target_index=target_index), + source_privkey, target_privkey) + # This is the interesting part of the test: on a pre-state with full consolidation queue, + # when processing an additional consolidation, it results in an exit in a later epoch + yield from run_consolidation_processing(spec, state, signed_consolidation) + + expected_exit_epoch = spec.compute_activation_exit_epoch(current_epoch) + assert state.earliest_consolidation_epoch == expected_exit_epoch + 1 + assert state.consolidation_balance_to_consume == consolidation_churn_limit - spec.MIN_ACTIVATION_BALANCE + assert state.validators[source_index].exit_epoch == expected_exit_epoch + 1 + for i in range(4): + assert state.validators[2 * i].exit_epoch == expected_exit_epoch + + +@with_eip7251_and_later +@with_presets([MINIMAL], "need sufficient consolidation churn limit") +@with_custom_state( + balances_fn=scaled_churn_balances_exceed_activation_exit_churn_limit, threshold_fn=default_activation_threshold) +@spec_test +@single_phase +def test_multiple_consolidations_equal_twice_churn(spec, state): + # This state has 256 validators each with 32 ETH in MINIMAL preset, 128 ETH consolidation churn + consolidation_churn_limit = spec.get_consolidation_churn_limit(state) + # Set the consolidation balance to consume equal to churn limit + state.consolidation_balance_to_consume = consolidation_churn_limit + current_epoch = spec.get_current_epoch(state) + + yield "pre", state + # Prepare a bunch of consolidations, based on the current state + consolidations = [] + for i in range(8): + source_index = 2 * i + target_index = 2 * i + 1 + source_privkey = pubkey_to_privkey[state.validators[source_index].pubkey] + target_privkey = pubkey_to_privkey[state.validators[target_index].pubkey] + # Set source and target withdrawal credentials to the same eth1 credential + set_eth1_withdrawal_credential_with_balance(spec, state, source_index) + set_eth1_withdrawal_credential_with_balance(spec, state, target_index) + signed_consolidation = sign_consolidation(spec, state, + spec.Consolidation( + epoch=current_epoch, + source_index=source_index, + target_index=target_index), + source_privkey, target_privkey) + consolidations.append(signed_consolidation) + + # Now run all the consolidations + for consolidation in consolidations: + # the function yields data, but we are just interested in running it here, ignore yields. + for _ in run_consolidation_processing(spec, state, consolidation): + continue + + yield "post", state + + first_exit_epoch = spec.compute_activation_exit_epoch(current_epoch) + assert state.consolidation_balance_to_consume == 0 + assert state.earliest_consolidation_epoch == first_exit_epoch + 1 + for i in range(4): + assert state.validators[2 * i].exit_epoch == first_exit_epoch + for i in range(4, 8): + assert state.validators[2 * i].exit_epoch == first_exit_epoch + 1 + + +# Failing tests + +@with_eip7251_and_later +@spec_state_test +def test_invalid_source_equals_target(spec, state): + current_epoch = spec.get_current_epoch(state) + validator_index = spec.get_active_validator_indices(state, current_epoch)[0] + validator_privkey = pubkey_to_privkey[state.validators[validator_index].pubkey] + + # Set withdrawal credentials to eth1 + set_eth1_withdrawal_credential_with_balance(spec, state, validator_index) + + signed_consolidation = sign_consolidation(spec, state, + spec.Consolidation( + epoch=current_epoch, + source_index=validator_index, + target_index=validator_index), + validator_privkey, validator_privkey) + yield from run_consolidation_processing(spec, state, signed_consolidation, valid=False) + + +@with_eip7251_and_later +@spec_state_test +def test_invalid_exceed_pending_consolidations_limit(spec, state): + state.pending_consolidations = ( + [spec.PendingConsolidation(source_index=0, target_index=1)] * spec.PENDING_CONSOLIDATIONS_LIMIT + ) + current_epoch = spec.get_current_epoch(state) + source_privkey = pubkey_to_privkey[state.validators[0].pubkey] + target_privkey = pubkey_to_privkey[state.validators[1].pubkey] + # Set source and target withdrawal credentials to the same eth1 credential + set_eth1_withdrawal_credential_with_balance(spec, state, 0) + set_eth1_withdrawal_credential_with_balance(spec, state, 1) + signed_consolidation = sign_consolidation(spec, state, + spec.Consolidation(epoch=current_epoch, source_index=0, target_index=1), + source_privkey, target_privkey) + yield from run_consolidation_processing(spec, state, signed_consolidation, valid=False) + + +@with_eip7251_and_later +@spec_state_test +def test_invalid_exited_source(spec, state): + current_epoch = spec.get_current_epoch(state) + source_privkey = pubkey_to_privkey[state.validators[0].pubkey] + target_privkey = pubkey_to_privkey[state.validators[1].pubkey] + set_eth1_withdrawal_credential_with_balance(spec, state, 0) + set_eth1_withdrawal_credential_with_balance(spec, state, 1) + signed_consolidation = sign_consolidation(spec, state, + spec.Consolidation(epoch=current_epoch, source_index=0, target_index=1), + source_privkey, target_privkey) + # exit source + spec.initiate_validator_exit(state, 0) + yield from run_consolidation_processing(spec, state, signed_consolidation, valid=False) + + +@with_eip7251_and_later +@spec_state_test +def test_invalid_exited_target(spec, state): + current_epoch = spec.get_current_epoch(state) + source_privkey = pubkey_to_privkey[state.validators[0].pubkey] + target_privkey = pubkey_to_privkey[state.validators[1].pubkey] + # Set source and target withdrawal credentials to the same eth1 credential + set_eth1_withdrawal_credential_with_balance(spec, state, 0) + set_eth1_withdrawal_credential_with_balance(spec, state, 1) + signed_consolidation = sign_consolidation(spec, state, + spec.Consolidation( + epoch=current_epoch, + source_index=0, + target_index=1), + source_privkey, target_privkey) + # exit target + spec.initiate_validator_exit(state, 1) + yield from run_consolidation_processing(spec, state, signed_consolidation, valid=False) + + +@with_eip7251_and_later +@spec_state_test +def test_invalid_inactive_source(spec, state): + current_epoch = spec.get_current_epoch(state) + source_privkey = pubkey_to_privkey[state.validators[0].pubkey] + target_privkey = pubkey_to_privkey[state.validators[1].pubkey] + set_eth1_withdrawal_credential_with_balance(spec, state, 0) + set_eth1_withdrawal_credential_with_balance(spec, state, 1) + signed_consolidation = sign_consolidation(spec, state, + spec.Consolidation( + epoch=current_epoch, + source_index=0, + target_index=1), + source_privkey, target_privkey) + # set source validator as not yet activated + state.validators[0].activation_epoch = spec.FAR_FUTURE_EPOCH + yield from run_consolidation_processing(spec, state, signed_consolidation, valid=False) + + +@with_eip7251_and_later +@spec_state_test +def test_invalid_inactive_target(spec, state): + current_epoch = spec.get_current_epoch(state) + source_privkey = pubkey_to_privkey[state.validators[0].pubkey] + target_privkey = pubkey_to_privkey[state.validators[1].pubkey] + # Set source and target withdrawal credentials to the same eth1 credential + set_eth1_withdrawal_credential_with_balance(spec, state, 0) + set_eth1_withdrawal_credential_with_balance(spec, state, 1) + signed_consolidation = sign_consolidation(spec, state, + spec.Consolidation( + epoch=current_epoch, + source_index=0, + target_index=1), + source_privkey, target_privkey) + # set target validator as not yet activated + state.validators[1].activation_epoch = spec.FAR_FUTURE_EPOCH + yield from run_consolidation_processing(spec, state, signed_consolidation, valid=False) + + +@with_eip7251_and_later +@spec_state_test +def test_invalid_no_execution_withdrawal_credential(spec, state): + current_epoch = spec.get_current_epoch(state) + source_privkey = pubkey_to_privkey[state.validators[0].pubkey] + target_privkey = pubkey_to_privkey[state.validators[1].pubkey] + signed_consolidation = sign_consolidation(spec, state, + spec.Consolidation( + epoch=current_epoch, + source_index=0, + target_index=1), + source_privkey, target_privkey) + yield from run_consolidation_processing(spec, state, signed_consolidation, valid=False) + + +@with_eip7251_and_later +@spec_state_test +def test_invalid_different_credentials(spec, state): + current_epoch = spec.get_current_epoch(state) + source_privkey = pubkey_to_privkey[state.validators[0].pubkey] + target_privkey = pubkey_to_privkey[state.validators[1].pubkey] + signed_consolidation = sign_consolidation(spec, state, + spec.Consolidation( + epoch=current_epoch, + source_index=0, + target_index=1), + source_privkey, target_privkey) + # Set source and target withdrawal credentials to different eth1 credentials + set_eth1_withdrawal_credential_with_balance(spec, state, 0) + set_eth1_withdrawal_credential_with_balance(spec, state, 1, address=b'\x10' * 20) + yield from run_consolidation_processing(spec, state, signed_consolidation, valid=False) + + +@with_eip7251_and_later +@spec_state_test +@always_bls +def test_invalid_source_signature(spec, state): + current_epoch = spec.get_current_epoch(state) + source_privkey = pubkey_to_privkey[state.validators[0].pubkey] + target_privkey = pubkey_to_privkey[state.validators[1].pubkey] + # Set source and target withdrawal credentials to the same eth1 credential + set_eth1_withdrawal_credential_with_balance(spec, state, 0) + set_eth1_withdrawal_credential_with_balance(spec, state, 1) + signed_consolidation = sign_consolidation(spec, state, + spec.Consolidation( + epoch=current_epoch, + source_index=0, + target_index=1), + source_privkey, target_privkey) + # Change the pubkey of the source validator, invalidating its signature + state.validators[0].pubkey = state.validators[1].pubkey + yield from run_consolidation_processing(spec, state, signed_consolidation, valid=False) + + +@with_eip7251_and_later +@spec_state_test +@always_bls +def test_invalid_target_signature(spec, state): + current_epoch = spec.get_current_epoch(state) + source_privkey = pubkey_to_privkey[state.validators[0].pubkey] + target_privkey = pubkey_to_privkey[state.validators[1].pubkey] + # Set source and target withdrawal credentials to the same eth1 credential + set_eth1_withdrawal_credential_with_balance(spec, state, 0) + set_eth1_withdrawal_credential_with_balance(spec, state, 1) + signed_consolidation = sign_consolidation(spec, state, + spec.Consolidation( + epoch=current_epoch, + source_index=0, + target_index=1), + source_privkey, target_privkey) + # Change the pubkey of the target validator, invalidating its signature + state.validators[1].pubkey = state.validators[2].pubkey + yield from run_consolidation_processing(spec, state, signed_consolidation, valid=False) + + +@with_eip7251_and_later +@spec_state_test +def test_invalid_before_specified_epoch(spec, state): + current_epoch = spec.get_current_epoch(state) + source_privkey = pubkey_to_privkey[state.validators[0].pubkey] + target_privkey = pubkey_to_privkey[state.validators[1].pubkey] + # Set source and target withdrawal credentials to the same eth1 credential + set_eth1_withdrawal_credential_with_balance(spec, state, 0) + set_eth1_withdrawal_credential_with_balance(spec, state, 1) + # set epoch=current_epoch + 1, so it's too early to process it + signed_consolidation = sign_consolidation(spec, state, + spec.Consolidation( + epoch=current_epoch + 1, + source_index=0, + target_index=1), + source_privkey, target_privkey) + yield from run_consolidation_processing(spec, state, signed_consolidation, valid=False) diff --git a/tests/core/pyspec/eth2spec/test/eip7251/block_processing/test_process_deposit.py b/tests/core/pyspec/eth2spec/test/eip7251/block_processing/test_process_deposit.py new file mode 100644 index 000000000..0b74fd65c --- /dev/null +++ b/tests/core/pyspec/eth2spec/test/eip7251/block_processing/test_process_deposit.py @@ -0,0 +1,284 @@ +from eth2spec.test.helpers.deposits import ( + build_deposit, + prepare_state_and_deposit, + run_deposit_processing_eip7251, + run_deposit_processing_eip7251_with_specific_fork_version, + sign_deposit_data, +) +from eth2spec.test.helpers.keys import privkeys, pubkeys + +from eth2spec.test.context import ( + spec_state_test, + with_eip7251_and_later, + always_bls, +) + + +@with_eip7251_and_later +@spec_state_test +def test_new_deposit_under_min_activation_balance(spec, state): + # fresh deposit = next validator index = validator appended to registry + validator_index = len(state.validators) + # effective balance will be 1 EFFECTIVE_BALANCE_INCREMENT smaller because of this small decrement. + amount = spec.MIN_ACTIVATION_BALANCE - 1 + deposit = prepare_state_and_deposit(spec, state, validator_index, amount, signed=True) + + yield from run_deposit_processing_eip7251(spec, state, deposit, validator_index) + + +@with_eip7251_and_later +@spec_state_test +def test_new_deposit_min(spec, state): + # fresh deposit = next validator index = validator appended to registry + validator_index = len(state.validators) + amount = spec.MIN_DEPOSIT_AMOUNT + deposit = prepare_state_and_deposit(spec, state, validator_index, amount, signed=True) + yield from run_deposit_processing_eip7251(spec, state, deposit, validator_index) + + +@with_eip7251_and_later +@spec_state_test +def test_new_deposit_between_min_and_max(spec, state): + # fresh deposit = next validator index = validator appended to registry + validator_index = len(state.validators) + amount = spec.MAX_EFFECTIVE_BALANCE_EIP7251 // 2 + deposit = prepare_state_and_deposit(spec, state, validator_index, amount, signed=True) + yield from run_deposit_processing_eip7251(spec, state, deposit, validator_index) + + +@with_eip7251_and_later +@spec_state_test +def test_new_deposit_max(spec, state): + # fresh deposit = next validator index = validator appended to registry + validator_index = len(state.validators) + # effective balance will be exactly the same as balance. + amount = spec.MAX_EFFECTIVE_BALANCE_EIP7251 + deposit = prepare_state_and_deposit(spec, state, validator_index, amount, signed=True) + yield from run_deposit_processing_eip7251(spec, state, deposit, validator_index) + + +@with_eip7251_and_later +@spec_state_test +def test_new_deposit_over_max(spec, state): + # fresh deposit = next validator index = validator appended to registry + validator_index = len(state.validators) + amount = spec.MAX_EFFECTIVE_BALANCE_EIP7251 + 1 + deposit = prepare_state_and_deposit(spec, state, validator_index, amount, signed=True) + yield from run_deposit_processing_eip7251(spec, state, deposit, validator_index) + + +# @with_eip7251_and_later +# @spec_state_test +# def test_top_up__max_effective_balance(spec, state): +# validator_index = 0 +# amount = spec.MAX_EFFECTIVE_BALANCE_EIP7251 // 4 +# deposit = prepare_state_and_deposit(spec, state, validator_index, amount, signed=True) + +# state.balances[validator_index] = spec.MAX_EFFECTIVE_BALANCE_EIP7251 +# state.validators[validator_index].effective_balance = spec.MAX_EFFECTIVE_BALANCE_EIP7251 + +# yield from run_deposit_processing_eip7251(spec, state, deposit, validator_index) + +# assert state.balances[validator_index] == spec.MAX_EFFECTIVE_BALANCE_EIP7251 + amount +# assert state.validators[validator_index].effective_balance == spec.MAX_EFFECTIVE_BALANCE_EIP7251 + +@with_eip7251_and_later +@spec_state_test +@always_bls +def test_correct_sig_but_forked_state(spec, state): + validator_index = len(state.validators) + amount = spec.MAX_EFFECTIVE_BALANCE + # deposits will always be valid, regardless of the current fork + state.fork.current_version = spec.Version('0x1234abcd') + deposit = prepare_state_and_deposit(spec, state, validator_index, amount, signed=True) + yield from run_deposit_processing_eip7251(spec, state, deposit, validator_index) + + +@with_eip7251_and_later +@spec_state_test +@always_bls +def test_incorrect_sig_new_deposit(spec, state): + # fresh deposit = next validator index = validator appended to registry + validator_index = len(state.validators) + amount = spec.MIN_ACTIVATION_BALANCE + deposit = prepare_state_and_deposit(spec, state, validator_index, amount) + yield from run_deposit_processing_eip7251(spec, state, deposit, validator_index, effective=False) + + +@with_eip7251_and_later +@spec_state_test +def test_top_up__max_effective_balance(spec, state): + validator_index = 0 + amount = spec.MAX_EFFECTIVE_BALANCE // 4 + deposit = prepare_state_and_deposit(spec, state, validator_index, amount, signed=True) + + state.balances[validator_index] = spec.MAX_EFFECTIVE_BALANCE + state.validators[validator_index].effective_balance = spec.MAX_EFFECTIVE_BALANCE + + yield from run_deposit_processing_eip7251(spec, state, deposit, validator_index) + + assert state.validators[validator_index].effective_balance == spec.MAX_EFFECTIVE_BALANCE + + +@with_eip7251_and_later +@spec_state_test +def test_top_up__less_effective_balance(spec, state): + validator_index = 0 + amount = spec.MAX_EFFECTIVE_BALANCE // 4 + deposit = prepare_state_and_deposit(spec, state, validator_index, amount, signed=True) + + initial_balance = spec.MAX_EFFECTIVE_BALANCE - 1000 + initial_effective_balance = spec.MAX_EFFECTIVE_BALANCE - spec.EFFECTIVE_BALANCE_INCREMENT + state.balances[validator_index] = initial_balance + state.validators[validator_index].effective_balance = initial_effective_balance + + yield from run_deposit_processing_eip7251(spec, state, deposit, validator_index) + + # unchanged effective balance + assert state.validators[validator_index].effective_balance == initial_effective_balance + + +@with_eip7251_and_later +@spec_state_test +def test_top_up__zero_balance(spec, state): + validator_index = 0 + amount = spec.MAX_EFFECTIVE_BALANCE // 4 + deposit = prepare_state_and_deposit(spec, state, validator_index, amount, signed=True) + + initial_balance = 0 + initial_effective_balance = 0 + state.balances[validator_index] = initial_balance + state.validators[validator_index].effective_balance = initial_effective_balance + + yield from run_deposit_processing_eip7251(spec, state, deposit, validator_index) + + # unchanged effective balance + assert state.validators[validator_index].effective_balance == initial_effective_balance + + +@with_eip7251_and_later +@spec_state_test +@always_bls +def test_incorrect_sig_top_up(spec, state): + validator_index = 0 + amount = spec.MAX_EFFECTIVE_BALANCE // 4 + deposit = prepare_state_and_deposit(spec, state, validator_index, amount) + + # invalid signatures, in top-ups, are allowed! + yield from run_deposit_processing_eip7251(spec, state, deposit, validator_index) + + +@with_eip7251_and_later +@spec_state_test +def test_incorrect_withdrawal_credentials_top_up(spec, state): + validator_index = 0 + amount = spec.MAX_EFFECTIVE_BALANCE // 4 + withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.hash(b"junk")[1:] + deposit = prepare_state_and_deposit( + spec, + state, + validator_index, + amount, + withdrawal_credentials=withdrawal_credentials + ) + + # inconsistent withdrawal credentials, in top-ups, are allowed! + yield from run_deposit_processing_eip7251(spec, state, deposit, validator_index) + + +@with_eip7251_and_later +@spec_state_test +def test_invalid_wrong_deposit_for_deposit_count(spec, state): + deposit_data_leaves = [spec.DepositData() for _ in range(len(state.validators))] + + # build root for deposit_1 + index_1 = len(deposit_data_leaves) + pubkey_1 = pubkeys[index_1] + privkey_1 = privkeys[index_1] + _, _, deposit_data_leaves = build_deposit( + spec, + deposit_data_leaves, + pubkey_1, + privkey_1, + spec.MAX_EFFECTIVE_BALANCE, + withdrawal_credentials=b'\x00' * 32, + signed=True, + ) + deposit_count_1 = len(deposit_data_leaves) + + # build root for deposit_2 + index_2 = len(deposit_data_leaves) + pubkey_2 = pubkeys[index_2] + privkey_2 = privkeys[index_2] + deposit_2, root_2, deposit_data_leaves = build_deposit( + spec, + deposit_data_leaves, + pubkey_2, + privkey_2, + spec.MAX_EFFECTIVE_BALANCE, + withdrawal_credentials=b'\x00' * 32, + signed=True, + ) + + # state has root for deposit_2 but is at deposit_count for deposit_1 + state.eth1_data.deposit_root = root_2 + state.eth1_data.deposit_count = deposit_count_1 + + yield from run_deposit_processing_eip7251(spec, state, deposit_2, index_2, valid=False) + + +@with_eip7251_and_later +@spec_state_test +def test_invalid_bad_merkle_proof(spec, state): + validator_index = len(state.validators) + amount = spec.MAX_EFFECTIVE_BALANCE + deposit = prepare_state_and_deposit(spec, state, validator_index, amount) + + # mess up merkle branch + deposit.proof[5] = spec.Bytes32() + + sign_deposit_data(spec, deposit.data, privkeys[validator_index]) + + yield from run_deposit_processing_eip7251(spec, state, deposit, validator_index, valid=False) + + +@with_eip7251_and_later +@spec_state_test +def test_key_validate_invalid_subgroup(spec, state): + validator_index = len(state.validators) + amount = spec.MAX_EFFECTIVE_BALANCE + + # All-zero pubkey would not pass `bls.KeyValidate`, but `process_deposit` would not throw exception. + pubkey = b'\x00' * 48 + + deposit = prepare_state_and_deposit(spec, state, validator_index, amount, pubkey=pubkey, signed=True) + + yield from run_deposit_processing_eip7251(spec, state, deposit, validator_index) + + +@with_eip7251_and_later +@spec_state_test +def test_key_validate_invalid_decompression(spec, state): + validator_index = len(state.validators) + amount = spec.MAX_EFFECTIVE_BALANCE + + # `deserialization_fails_infinity_with_true_b_flag` BLS G1 deserialization test case. + # This pubkey would not pass `bls.KeyValidate`, but `process_deposit` would not throw exception. + pubkey_hex = 'c01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' + pubkey = bytes.fromhex(pubkey_hex) + + deposit = prepare_state_and_deposit(spec, state, validator_index, amount, pubkey=pubkey, signed=True) + + yield from run_deposit_processing_eip7251(spec, state, deposit, validator_index) + + +@with_eip7251_and_later +@spec_state_test +@always_bls +def test_ineffective_deposit_with_bad_fork_version(spec, state): + yield from run_deposit_processing_eip7251_with_specific_fork_version( + spec, + state, + fork_version=spec.Version('0xAaBbCcDd'), + effective=False, + ) diff --git a/tests/core/pyspec/eth2spec/test/eip7251/block_processing/test_process_execution_layer_withdraw_request.py b/tests/core/pyspec/eth2spec/test/eip7251/block_processing/test_process_execution_layer_withdraw_request.py new file mode 100644 index 000000000..1f62e611f --- /dev/null +++ b/tests/core/pyspec/eth2spec/test/eip7251/block_processing/test_process_execution_layer_withdraw_request.py @@ -0,0 +1,213 @@ +from eth2spec.test.context import ( + spec_state_test, + expect_assertion_error, + with_eip7251_and_later, + with_presets, +) +from eth2spec.test.helpers.constants import MINIMAL +from eth2spec.test.helpers.state import ( + get_validator_index_by_pubkey, +) +from eth2spec.test.helpers.withdrawals import ( + set_eth1_withdrawal_credential_with_balance, +) + + +# Only failing test from capella process_withdrawals is +# test_success_excess_balance_but_no_max_effective_balance + + +# Modified tests from 7002. Just testing EL-triggered exits, not partial withdrawals + +@with_eip7251_and_later +@spec_state_test +def test_basic_exit(spec, state): + # move state forward SHARD_COMMITTEE_PERIOD epochs to allow for exit + state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH + + current_epoch = spec.get_current_epoch(state) + validator_index = spec.get_active_validator_indices(state, current_epoch)[0] + validator_pubkey = state.validators[validator_index].pubkey + address = b'\x22' * 20 + set_eth1_withdrawal_credential_with_balance(spec, state, validator_index, address=address) + execution_layer_withdraw_request = spec.ExecutionLayerWithdrawRequest( + source_address=address, + validator_pubkey=validator_pubkey, + amount=0, + ) + + yield from run_execution_layer_withdraw_request_processing(spec, state, execution_layer_withdraw_request) + + +@with_eip7251_and_later +@spec_state_test +def test_incorrect_source_address(spec, state): + # move state forward SHARD_COMMITTEE_PERIOD epochs to allow for exit + state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH + + current_epoch = spec.get_current_epoch(state) + validator_index = spec.get_active_validator_indices(state, current_epoch)[0] + validator_pubkey = state.validators[validator_index].pubkey + address = b'\x22' * 20 + incorrect_address = b'\x33' * 20 + set_eth1_withdrawal_credential_with_balance(spec, state, validator_index, address=address) + execution_layer_withdraw_request = spec.ExecutionLayerWithdrawRequest( + source_address=incorrect_address, + validator_pubkey=validator_pubkey, + amount=0, + ) + + yield from run_execution_layer_withdraw_request_processing( + spec, state, execution_layer_withdraw_request, success=False + ) + + +@with_eip7251_and_later +@spec_state_test +def test_incorrect_withdrawal_credential_prefix(spec, state): + # move state forward SHARD_COMMITTEE_PERIOD epochs to allow for exit + state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH + + current_epoch = spec.get_current_epoch(state) + validator_index = spec.get_active_validator_indices(state, current_epoch)[0] + validator_pubkey = state.validators[validator_index].pubkey + address = b'\x22' * 20 + set_eth1_withdrawal_credential_with_balance(spec, state, validator_index, address=address) + # Set incorrect prefix + state.validators[validator_index].withdrawal_credentials = ( + spec.BLS_WITHDRAWAL_PREFIX + + state.validators[validator_index].withdrawal_credentials[1:] + ) + execution_layer_withdraw_request = spec.ExecutionLayerWithdrawRequest( + source_address=address, + validator_pubkey=validator_pubkey, + amount=0, + ) + + yield from run_execution_layer_withdraw_request_processing( + spec, state, execution_layer_withdraw_request, success=False + ) + + +@with_eip7251_and_later +@spec_state_test +def test_on_exit_initiated_validator(spec, state): + # move state forward SHARD_COMMITTEE_PERIOD epochs to allow for exit + state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH + + current_epoch = spec.get_current_epoch(state) + validator_index = spec.get_active_validator_indices(state, current_epoch)[0] + validator_pubkey = state.validators[validator_index].pubkey + address = b'\x22' * 20 + set_eth1_withdrawal_credential_with_balance(spec, state, validator_index, address=address) + # Initiate exit earlier + spec.initiate_validator_exit(state, validator_index) + execution_layer_withdraw_request = spec.ExecutionLayerWithdrawRequest( + source_address=address, + validator_pubkey=validator_pubkey, + amount=0, + ) + + yield from run_execution_layer_withdraw_request_processing( + spec, state, execution_layer_withdraw_request, success=False + ) + + +@with_eip7251_and_later +@spec_state_test +def test_activation_epoch_less_than_shard_committee_period(spec, state): + current_epoch = spec.get_current_epoch(state) + validator_index = spec.get_active_validator_indices(state, current_epoch)[0] + validator_pubkey = state.validators[validator_index].pubkey + address = b'\x22' * 20 + set_eth1_withdrawal_credential_with_balance(spec, state, validator_index, address=address) + execution_layer_withdraw_request = spec.ExecutionLayerWithdrawRequest( + source_address=address, + validator_pubkey=validator_pubkey, + amount=0, + ) + + assert spec.get_current_epoch(state) < ( + state.validators[validator_index].activation_epoch + spec.config.SHARD_COMMITTEE_PERIOD + ) + + yield from run_execution_layer_withdraw_request_processing( + spec, state, execution_layer_withdraw_request, success=False + ) + + +# Partial withdrawals tests + +@with_eip7251_and_later +@spec_state_test +@with_presets([MINIMAL]) +def test_partial_withdrawal_queue_full(spec, state): + state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH + current_epoch = spec.get_current_epoch(state) + validator_index = spec.get_active_validator_indices(state, current_epoch)[0] + validator_pubkey = state.validators[validator_index].pubkey + address = b'\x22' * 20 + set_eth1_withdrawal_credential_with_balance(spec, state, validator_index, address=address) + execution_layer_withdraw_request = spec.ExecutionLayerWithdrawRequest( + source_address=address, + validator_pubkey=validator_pubkey, + amount=10 ** 9, + ) + + partial_withdrawal = spec.PartialWithdrawal(index=0, amount=1, withdrawable_epoch=current_epoch) + state.pending_partial_withdrawals = [partial_withdrawal] * spec.PENDING_PARTIAL_WITHDRAWALS_LIMIT + yield from run_execution_layer_withdraw_request_processing( + spec, state, execution_layer_withdraw_request, success=False + ) + + +# +# Run processing +# + +def run_execution_layer_withdraw_request_processing( + spec, state, execution_layer_withdraw_request, valid=True, success=True +): + """ + Run ``process_execution_layer_withdraw_request``, yielding: + - pre-state ('pre') + - execution_layer_withdraw_request ('execution_layer_withdraw_request') + - post-state ('post'). + If ``valid == False``, run expecting ``AssertionError`` + If ``success == False``, it doesn't initiate exit successfully + """ + validator_index = get_validator_index_by_pubkey(state, execution_layer_withdraw_request.validator_pubkey) + + yield 'pre', state + yield 'execution_layer_withdraw_request', execution_layer_withdraw_request + + if not valid: + expect_assertion_error( + lambda: spec.process_execution_layer_withdraw_request(state, execution_layer_withdraw_request)) + yield 'post', None + return + + pre_exit_epoch = state.validators[validator_index].exit_epoch + pre_pending_partial_withdrawals = state.pending_partial_withdrawals + pre_balance = state.balances[validator_index] + + spec.process_execution_layer_withdraw_request(state, execution_layer_withdraw_request) + + yield 'post', state + + if execution_layer_withdraw_request.amount == 0: + if success: + assert pre_exit_epoch == spec.FAR_FUTURE_EPOCH + assert state.validators[validator_index].exit_epoch < spec.FAR_FUTURE_EPOCH + else: + assert state.validators[validator_index].exit_epoch == pre_exit_epoch + else: + if success: + assert state.validators[validator_index].exit_epoch == spec.FAR_FUTURE_EPOCH + assert state.balances[validator_index] == pre_balance + post_length = len(state.pending_partial_withdrawals) + assert post_length == len(pre_pending_partial_withdrawals) + 1 + assert post_length < spec.PENDING_PARTIAL_WITHDRAWALS_LIMIT + assert state.pending_partial_withdrawals[post_length - 1].validator_index == validator_index + else: + assert state.pending_partial_withdrawals == pre_pending_partial_withdrawals diff --git a/tests/core/pyspec/eth2spec/test/eip7251/block_processing/test_voluntary_exit.py b/tests/core/pyspec/eth2spec/test/eip7251/block_processing/test_voluntary_exit.py new file mode 100644 index 000000000..a8c9d1d60 --- /dev/null +++ b/tests/core/pyspec/eth2spec/test/eip7251/block_processing/test_voluntary_exit.py @@ -0,0 +1,441 @@ +from eth2spec.test.helpers.constants import (MINIMAL, MAINNET) +from eth2spec.test.context import ( + spec_state_test, + with_eip7251_and_later, + with_presets, + always_bls, + spec_test, single_phase, + with_custom_state, + scaled_churn_balances_min_churn_limit, +) +from eth2spec.test.helpers.keys import pubkey_to_privkey +from eth2spec.test.helpers.voluntary_exits import ( + run_voluntary_exit_processing, + sign_voluntary_exit, +) +# ******************** +# * EXIT QUEUE TESTS * +# ******************** + + +@with_eip7251_and_later +@spec_state_test +def test_min_balance_exit(spec, state): + # This state has 64 validators each with 32 ETH + expected_exit_epoch = spec.compute_activation_exit_epoch(spec.get_current_epoch(state)) + churn_limit = spec.get_activation_exit_churn_limit(state) + # Set the balance to consume equal to churn limit + state.exit_balance_to_consume = churn_limit + + yield "pre", state + # Exit validators, all which fit in the churn limit + spec.initiate_validator_exit(state, 0) + yield "post", state + + # Check exit queue churn is set + assert state.exit_balance_to_consume == churn_limit - spec.MIN_ACTIVATION_BALANCE + # Check exit epoch + assert state.validators[0].exit_epoch == expected_exit_epoch + + +@with_eip7251_and_later +@spec_state_test +def test_min_balance_exits_up_to_churn(spec, state): + # This state has 64 validators each with 32 ETH + single_validator_balance = spec.MIN_ACTIVATION_BALANCE + expected_exit_epoch = spec.compute_activation_exit_epoch(spec.get_current_epoch(state)) + churn_limit = spec.get_activation_exit_churn_limit(state) + # Set the balance to consume equal to churn limit + state.exit_balance_to_consume = churn_limit + + yield "pre", state + # Exit validators, all which fit in the churn limit + for i in range(churn_limit // spec.MIN_ACTIVATION_BALANCE): + validator_index = i + spec.initiate_validator_exit(state, validator_index) + yield f"post{i}", state + # Check exit queue churn is set + assert state.exit_balance_to_consume == churn_limit - single_validator_balance * (i + 1) + # Check exit epoch + assert state.validators[validator_index].exit_epoch == expected_exit_epoch + yield "post", state + + +@with_eip7251_and_later +@spec_state_test +def test_min_balance_exits_above_churn(spec, state): + # This state has 64 validators each with 32 ETH + single_validator_balance = spec.MIN_ACTIVATION_BALANCE + expected_exit_epoch = spec.compute_activation_exit_epoch(spec.get_current_epoch(state)) + churn_limit = spec.get_activation_exit_churn_limit(state) + # Set the balance to consume equal to churn limit + state.exit_balance_to_consume = churn_limit + + yield "pre", state + # Exit validators, all which fit in the churn limit + for i in range(churn_limit // spec.MIN_ACTIVATION_BALANCE): + validator_index = i + spec.initiate_validator_exit(state, validator_index) + # Check exit queue churn is set + assert state.exit_balance_to_consume == churn_limit - single_validator_balance * (i + 1) + # Check exit epoch + assert state.validators[validator_index].exit_epoch == expected_exit_epoch + + # Exit balance has been fully consumed + assert state.exit_balance_to_consume == 0 + + # Exit an additional validator, doesn't fit in the churn limit, so exit + # epoch is incremented + validator_index = churn_limit // spec.MIN_ACTIVATION_BALANCE + spec.initiate_validator_exit(state, validator_index) + + yield "post", state + # Check exit epoch + assert state.validators[validator_index].exit_epoch == expected_exit_epoch + 1 + # Check exit balance to consume is set + assert state.exit_balance_to_consume == churn_limit - single_validator_balance + + +# @with_eip7251_and_later +# @spec_state_test +# def test_exit_balance_to_consume_large_validator(spec, state): +# # Set 0th validator effective balance to 2048 ETH +# state.validators[0].effective_balance = spec.MAX_EFFECTIVE_BALANCE_EIP7251 +# churn_limit = spec.get_validator_churn_limit(state) +# expected_exit_epoch = spec.compute_activation_exit_epoch(spec.get_current_epoch(state)) +# expected_exit_epoch += spec.MAX_EFFECTIVE_BALANCE_EIP7251 // churn_limit + +# validator_index = 0 +# spec.initiate_validator_exit(state, validator_index) +# # Check exit epoch +# assert state.validators[validator_index].exit_epoch == expected_exit_epoch +# # Check exit_balance_to_consume +# assert state.exit_balance_to_consume == churn_limit - (spec.MAX_EFFECTIVE_BALANCE_EIP7251 % churn_limit) +# # Check earliest_exit_epoch +# assert state.earliest_exit_epoch == expected_exit_epoch + +@with_eip7251_and_later +@spec_state_test +@with_presets([MAINNET], "With CHURN_LIMIT_QUOTIENT=32, can't change validator balance without changing churn_limit") +def test_max_balance_exit(spec, state): + churn_limit = spec.get_activation_exit_churn_limit(state) + assert churn_limit == spec.MIN_ACTIVATION_BALANCE * spec.config.MIN_PER_EPOCH_CHURN_LIMIT + + # Set 0th validator effective balance to 2048 ETH + state.validators[0].effective_balance = spec.MAX_EFFECTIVE_BALANCE_EIP7251 + yield 'pre', state + + expected_exit_epoch = spec.compute_activation_exit_epoch(spec.get_current_epoch(state)) + # Validator consumes exit churn for 16 epochs, exits at the 17th one + expected_exit_epoch += (spec.MAX_EFFECTIVE_BALANCE_EIP7251 // churn_limit) + + validator_index = 0 + spec.initiate_validator_exit(state, validator_index) + yield 'post', state + # Check exit epoch + assert state.validators[validator_index].exit_epoch == expected_exit_epoch + # Check exit_balance_to_consume + assert state.exit_balance_to_consume == churn_limit + # Check earliest_exit_epoch + assert state.earliest_exit_epoch == expected_exit_epoch + + +@with_eip7251_and_later +@spec_state_test +@with_presets([MAINNET], "With CHURN_LIMIT_QUOTIENT=32, can't change validator balance without changing churn_limit") +def test_exit_with_balance_equal_to_churn_limit(spec, state): + churn_limit = spec.get_activation_exit_churn_limit(state) + + # Set 0th validator effective balance to churn_limit + state.validators[0].effective_balance = churn_limit + yield 'pre', state + + validator_index = 0 + spec.initiate_validator_exit(state, validator_index) + + yield 'post', state + # Validator consumes churn limit fully in the current epoch + assert (state.validators[validator_index].exit_epoch == + spec.compute_activation_exit_epoch(spec.get_current_epoch(state))) + # Check exit_balance_to_consume + assert state.exit_balance_to_consume == 0 + # Check earliest_exit_epoch + assert state.earliest_exit_epoch == state.validators[validator_index].exit_epoch + + +@with_eip7251_and_later +@spec_state_test +@with_presets([MAINNET], "With CHURN_LIMIT_QUOTIENT=32, can't change validator balance without changing churn_limit") +def test_exit_churn_limit_balance_existing_churn_(spec, state): + cl = spec.get_activation_exit_churn_limit(state) + + # set exit epoch to the first available one and set exit balance to consume to full churn limit + state.earliest_exit_epoch = spec.compute_activation_exit_epoch(spec.get_current_epoch(state)) + state.exit_balance_to_consume = cl + # consume some churn in exit epoch + state.exit_balance_to_consume -= 1000000000 + + # Set 0th validator effective balance to the churn limit + state.validators[0].effective_balance = cl + + yield 'pre', state + + # The existing 1 ETH churn will push an extra epoch + expected_exit_epoch = state.earliest_exit_epoch + 1 + + yield 'post', state + validator_index = 0 + spec.initiate_validator_exit(state, validator_index) + # Check exit epoch + assert state.validators[validator_index].exit_epoch == expected_exit_epoch + # Check balance consumed in exit epoch is the remainder 1 ETH + assert state.exit_balance_to_consume == cl - 1000000000 + # check earliest exit epoch + assert expected_exit_epoch == state.earliest_exit_epoch + + +@with_eip7251_and_later +@spec_state_test +@with_presets([MAINNET], "With CHURN_LIMIT_QUOTIENT=32, can't change validator balance without changing churn_limit") +def test_multi_epoch_exit_existing_churn(spec, state): + cl = spec.get_activation_exit_churn_limit(state) + + # set exit epoch to the first available one and set exit balance to consume to full churn limit + state.earliest_exit_epoch = spec.compute_activation_exit_epoch(spec.get_current_epoch(state)) + state.exit_balance_to_consume = cl + # consume some churn in exit epoch + state.exit_balance_to_consume -= 1000000000 + + # Set 0th validator effective balance to 2x the churn limit + state.validators[0].effective_balance = 2 * cl + + yield 'pre', state + # Two extra epochs will be necessary + expected_exit_epoch = spec.compute_activation_exit_epoch(spec.get_current_epoch(state)) + 2 + + validator_index = 0 + spec.initiate_validator_exit(state, validator_index) + yield 'post', state + # Check exit epoch + assert state.validators[validator_index].exit_epoch == expected_exit_epoch + # Check balance consumed in exit epoch is the remainder 1 ETH + assert state.exit_balance_to_consume == cl - 1000000000 + # check earliest exit epoch + assert expected_exit_epoch == state.earliest_exit_epoch + + +# Repurposed from phase0 voluntary exit tests, should disable the phase0 ones + +def run_test_success_exit_queue(spec, state): + # move state forward SHARD_COMMITTEE_PERIOD epochs to allow for exit + state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH + + current_epoch = spec.get_current_epoch(state) + churn_limit = spec.get_activation_exit_churn_limit(state) + # exit `MAX_EXITS_PER_EPOCH` + max_exits = churn_limit // spec.MIN_ACTIVATION_BALANCE + initial_indices = spec.get_active_validator_indices(state, current_epoch)[:max_exits] + + # Prepare a bunch of exits, based on the current state + exit_queue = [] + for index in initial_indices: + privkey = pubkey_to_privkey[state.validators[index].pubkey] + + signed_voluntary_exit = sign_voluntary_exit( + spec, state, spec.VoluntaryExit(epoch=current_epoch, validator_index=index), privkey) + + exit_queue.append(signed_voluntary_exit) + + # Now run all the exits + for voluntary_exit in exit_queue: + # the function yields data, but we are just interested in running it here, ignore yields. + for _ in run_voluntary_exit_processing(spec, state, voluntary_exit): + continue + + # exit an additional validator + validator_index = spec.get_active_validator_indices(state, current_epoch)[-1] + privkey = pubkey_to_privkey[state.validators[validator_index].pubkey] + + signed_voluntary_exit = sign_voluntary_exit( + spec, state, spec.VoluntaryExit(epoch=current_epoch, validator_index=validator_index), privkey) + + # This is the interesting part of the test: on a pre-state with a full exit queue, + # when processing an additional exit, it results in an exit in a later epoch + yield from run_voluntary_exit_processing(spec, state, signed_voluntary_exit) + + for index in initial_indices: + assert ( + state.validators[validator_index].exit_epoch == + state.validators[index].exit_epoch + 1 + ) + assert state.earliest_exit_epoch == state.validators[validator_index].exit_epoch + consumed_churn = spec.MIN_ACTIVATION_BALANCE * (max_exits + 1) + assert state.exit_balance_to_consume == churn_limit - (consumed_churn % churn_limit) + + +@with_eip7251_and_later +@spec_state_test +def test_success_exit_queue__min_churn(spec, state): + yield from run_test_success_exit_queue(spec, state) + + +@with_eip7251_and_later +@with_presets([MINIMAL], + reason="mainnet config leads to larger validator set than limit of public/private keys pre-generated") +@spec_test +@with_custom_state(balances_fn=scaled_churn_balances_min_churn_limit, + threshold_fn=lambda spec: spec.config.EJECTION_BALANCE) +@single_phase +def test_success_exit_queue__scaled_churn(spec, state): + churn_limit = spec.get_activation_exit_churn_limit(state) + assert churn_limit > spec.config.MIN_PER_EPOCH_CHURN_LIMIT + yield from run_test_success_exit_queue(spec, state) + + +# After here no modifications were made, can just leave them in phase0 as is + +@with_eip7251_and_later +@spec_state_test +def test_basic(spec, state): + state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH + current_epoch = spec.get_current_epoch(state) + validator_index = spec.get_active_validator_indices(state, current_epoch)[0] + privkey = pubkey_to_privkey[state.validators[validator_index].pubkey] + + signed_voluntary_exit = sign_voluntary_exit( + spec, state, spec.VoluntaryExit(epoch=current_epoch, validator_index=validator_index), privkey) + + yield from run_voluntary_exit_processing(spec, state, signed_voluntary_exit) + + assert state.validators[validator_index].exit_epoch == spec.compute_activation_exit_epoch(current_epoch) + + +@with_eip7251_and_later +@spec_state_test +@always_bls +def test_invalid_incorrect_signature(spec, state): + # move state forward SHARD_COMMITTEE_PERIOD epochs to allow for exit + state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH + + current_epoch = spec.get_current_epoch(state) + validator_index = spec.get_active_validator_indices(state, current_epoch)[0] + + voluntary_exit = spec.VoluntaryExit( + epoch=current_epoch, + validator_index=validator_index, + ) + signed_voluntary_exit = sign_voluntary_exit(spec, state, voluntary_exit, 12345) + + yield from run_voluntary_exit_processing(spec, state, signed_voluntary_exit, valid=False) + + +@with_eip7251_and_later +@spec_state_test +def test_default_exit_epoch_subsequent_exit(spec, state): + # move state forward SHARD_COMMITTEE_PERIOD epochs to allow for exit + state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH + + current_epoch = spec.get_current_epoch(state) + validator_index = spec.get_active_validator_indices(state, current_epoch)[0] + privkey = pubkey_to_privkey[state.validators[validator_index].pubkey] + + signed_voluntary_exit = sign_voluntary_exit( + spec, state, spec.VoluntaryExit(epoch=current_epoch, validator_index=validator_index), privkey) + + # Exit one validator prior to this new one + exited_index = spec.get_active_validator_indices(state, current_epoch)[-1] + state.validators[exited_index].exit_epoch = current_epoch - 1 + + yield from run_voluntary_exit_processing(spec, state, signed_voluntary_exit) + + assert state.validators[validator_index].exit_epoch == spec.compute_activation_exit_epoch(current_epoch) + + +@with_eip7251_and_later +@spec_state_test +def test_invalid_validator_exit_in_future(spec, state): + # move state forward SHARD_COMMITTEE_PERIOD epochs to allow for exit + state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH + + current_epoch = spec.get_current_epoch(state) + validator_index = spec.get_active_validator_indices(state, current_epoch)[0] + privkey = pubkey_to_privkey[state.validators[validator_index].pubkey] + + voluntary_exit = spec.VoluntaryExit( + epoch=current_epoch + 1, + validator_index=validator_index, + ) + signed_voluntary_exit = sign_voluntary_exit(spec, state, voluntary_exit, privkey) + + yield from run_voluntary_exit_processing(spec, state, signed_voluntary_exit, valid=False) + + +@with_eip7251_and_later +@spec_state_test +def test_invalid_validator_incorrect_validator_index(spec, state): + # move state forward SHARD_COMMITTEE_PERIOD epochs to allow for exit + state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH + + current_epoch = spec.get_current_epoch(state) + validator_index = spec.get_active_validator_indices(state, current_epoch)[0] + privkey = pubkey_to_privkey[state.validators[validator_index].pubkey] + + voluntary_exit = spec.VoluntaryExit( + epoch=current_epoch, + validator_index=len(state.validators), + ) + signed_voluntary_exit = sign_voluntary_exit(spec, state, voluntary_exit, privkey) + + yield from run_voluntary_exit_processing(spec, state, signed_voluntary_exit, valid=False) + + +@with_eip7251_and_later +@spec_state_test +def test_invalid_validator_not_active(spec, state): + current_epoch = spec.get_current_epoch(state) + validator_index = spec.get_active_validator_indices(state, current_epoch)[0] + privkey = pubkey_to_privkey[state.validators[validator_index].pubkey] + + state.validators[validator_index].activation_epoch = spec.FAR_FUTURE_EPOCH + + signed_voluntary_exit = sign_voluntary_exit( + spec, state, spec.VoluntaryExit(epoch=current_epoch, validator_index=validator_index), privkey) + + yield from run_voluntary_exit_processing(spec, state, signed_voluntary_exit, valid=False) + + +@with_eip7251_and_later +@spec_state_test +def test_invalid_validator_already_exited(spec, state): + # move state forward SHARD_COMMITTEE_PERIOD epochs to allow validator able to exit + state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH + + current_epoch = spec.get_current_epoch(state) + validator_index = spec.get_active_validator_indices(state, current_epoch)[0] + privkey = pubkey_to_privkey[state.validators[validator_index].pubkey] + + # but validator already has exited + state.validators[validator_index].exit_epoch = current_epoch + 2 + + signed_voluntary_exit = sign_voluntary_exit( + spec, state, spec.VoluntaryExit(epoch=current_epoch, validator_index=validator_index), privkey) + + yield from run_voluntary_exit_processing(spec, state, signed_voluntary_exit, valid=False) + + +@with_eip7251_and_later +@spec_state_test +def test_invalid_validator_not_active_long_enough(spec, state): + current_epoch = spec.get_current_epoch(state) + validator_index = spec.get_active_validator_indices(state, current_epoch)[0] + privkey = pubkey_to_privkey[state.validators[validator_index].pubkey] + + signed_voluntary_exit = sign_voluntary_exit( + spec, state, spec.VoluntaryExit(epoch=current_epoch, validator_index=validator_index), privkey) + + assert ( + current_epoch - state.validators[validator_index].activation_epoch < + spec.config.SHARD_COMMITTEE_PERIOD + ) + + yield from run_voluntary_exit_processing(spec, state, signed_voluntary_exit, valid=False) diff --git a/tests/core/pyspec/eth2spec/test/eip7251/epoch_processing/test_process_pending_balance_deposits.py b/tests/core/pyspec/eth2spec/test/eip7251/epoch_processing/test_process_pending_balance_deposits.py new file mode 100644 index 000000000..02e36a5e2 --- /dev/null +++ b/tests/core/pyspec/eth2spec/test/eip7251/epoch_processing/test_process_pending_balance_deposits.py @@ -0,0 +1,99 @@ +from eth2spec.test.helpers.epoch_processing import run_epoch_processing_with +from eth2spec.test.context import ( + spec_state_test, + with_eip7251_and_later, +) + + +@with_eip7251_and_later +@spec_state_test +def test_pending_deposit_min_activation_balance(spec, state): + index = 0 + amount = spec.MIN_ACTIVATION_BALANCE + state.pending_balance_deposits.append(spec.PendingBalanceDeposit(index=index, amount=amount)) + pre_balance = state.balances[index] + yield from run_epoch_processing_with(spec, state, 'process_pending_balance_deposits') + assert state.balances[index] == pre_balance + amount + # No leftover deposit balance to consume when there are no deposits left to process + assert state.deposit_balance_to_consume == 0 + assert state.pending_balance_deposits == [] + + +@with_eip7251_and_later +@spec_state_test +def test_pending_deposit_balance_equal_churn(spec, state): + index = 0 + amount = spec.get_activation_exit_churn_limit(state) + state.pending_balance_deposits.append(spec.PendingBalanceDeposit(index=index, amount=amount)) + pre_balance = state.balances[index] + yield from run_epoch_processing_with(spec, state, 'process_pending_balance_deposits') + assert state.balances[index] == pre_balance + amount + assert state.deposit_balance_to_consume == 0 + assert state.pending_balance_deposits == [] + + +@with_eip7251_and_later +@spec_state_test +def test_pending_deposit_balance_above_churn(spec, state): + index = 0 + amount = spec.get_activation_exit_churn_limit(state) + 1 + state.pending_balance_deposits.append(spec.PendingBalanceDeposit(index=index, amount=amount)) + pre_balance = state.balances[index] + yield from run_epoch_processing_with(spec, state, 'process_pending_balance_deposits') + # deposit was above churn, balance hasn't changed + assert state.balances[index] == pre_balance + # deposit balance to consume is the full churn limit + assert state.deposit_balance_to_consume == spec.get_activation_exit_churn_limit(state) + # deposit is still in the queue + assert state.pending_balance_deposits == [spec.PendingBalanceDeposit(index=index, amount=amount)] + + +@with_eip7251_and_later +@spec_state_test +def test_pending_deposit_preexisting_churn(spec, state): + index = 0 + amount = 10 ** 9 + 1 + state.deposit_balance_to_consume = 2 * amount + state.pending_balance_deposits.append(spec.PendingBalanceDeposit(index=index, amount=amount)) + pre_balance = state.balances[index] + yield from run_epoch_processing_with(spec, state, 'process_pending_balance_deposits') + # balance was deposited correctly + assert state.balances[index] == pre_balance + amount + # No leftover deposit balance to consume when there are no deposits left to process + assert state.deposit_balance_to_consume == 0 + # queue emptied + assert state.pending_balance_deposits == [] + + +@with_eip7251_and_later +@spec_state_test +def test_multiple_pending_deposits_below_churn(spec, state): + amount = 10**9 + state.pending_balance_deposits.append(spec.PendingBalanceDeposit(index=0, amount=amount)) + state.pending_balance_deposits.append(spec.PendingBalanceDeposit(index=1, amount=amount)) + pre_balances = state.balances + yield from run_epoch_processing_with(spec, state, 'process_pending_balance_deposits') + for i in [0, 1]: + assert state.balances[i] == pre_balances[i] + amount + # No leftover deposit balance to consume when there are no deposits left to process + assert state.deposit_balance_to_consume == 0 + assert state.pending_balance_deposits == [] + + +@with_eip7251_and_later +@spec_state_test +def test_multiple_pending_deposits_above_churn(spec, state): + # set third deposit to be over the churn + amount = (spec.get_activation_exit_churn_limit(state) // 3) + 1 + for i in [0, 1, 2]: + state.pending_balance_deposits.append(spec.PendingBalanceDeposit(index=i, amount=amount)) + pre_balances = state.balances + yield from run_epoch_processing_with(spec, state, 'process_pending_balance_deposits') + # First two deposits are processed, third is not because above churn + for i in [0, 1]: + assert state.balances[i] == pre_balances[i] + amount + assert state.balances[2] == pre_balances[2] + # Only first two subtract from the deposit balance to consume + assert state.deposit_balance_to_consume == spec.get_activation_exit_churn_limit(state) - 2 * amount + # third deposit is still in the queue + assert state.pending_balance_deposits == [spec.PendingBalanceDeposit(index=2, amount=amount)] diff --git a/tests/core/pyspec/eth2spec/test/eip7251/epoch_processing/test_process_pending_consolidations.py b/tests/core/pyspec/eth2spec/test/eip7251/epoch_processing/test_process_pending_consolidations.py new file mode 100644 index 000000000..06345f151 --- /dev/null +++ b/tests/core/pyspec/eth2spec/test/eip7251/epoch_processing/test_process_pending_consolidations.py @@ -0,0 +1,57 @@ +from eth2spec.test.helpers.epoch_processing import run_epoch_processing_with +from eth2spec.test.context import ( + spec_state_test, + with_eip7251_and_later, +) + +# *********************** +# * CONSOLIDATION TESTS * +# *********************** + + +@with_eip7251_and_later +@spec_state_test +def test_basic_pending_consolidation(spec, state): + current_epoch = spec.get_current_epoch(state) + source_index = spec.get_active_validator_indices(state, current_epoch)[0] + target_index = spec.get_active_validator_indices(state, current_epoch)[1] + # append pending consolidation + state.pending_consolidations.append(spec.PendingConsolidation(source_index=source_index, target_index=target_index)) + # Set withdrawable epoch to current epoch to allow processing + state.validators[source_index].withdrawable_epoch = spec.get_current_epoch(state) + yield from run_epoch_processing_with(spec, state, "process_pending_consolidations") + + assert state.balances[target_index] == 2 * spec.MIN_ACTIVATION_BALANCE + assert state.balances[source_index] == 0 + + +@with_eip7251_and_later +@spec_state_test +def test_skip_consolidation_when_source_slashed(spec, state): + current_epoch = spec.get_current_epoch(state) + source0_index = spec.get_active_validator_indices(state, current_epoch)[0] + target0_index = spec.get_active_validator_indices(state, current_epoch)[1] + source1_index = spec.get_active_validator_indices(state, current_epoch)[2] + target1_index = spec.get_active_validator_indices(state, current_epoch)[3] + # append pending consolidation + state.pending_consolidations.append( + spec.PendingConsolidation(source_index=source0_index, target_index=target0_index) + ) + state.pending_consolidations.append( + spec.PendingConsolidation(source_index=source1_index, target_index=target1_index) + ) + + # Set withdrawable epoch of sources to current epoch to allow processing + state.validators[source0_index].withdrawable_epoch = spec.get_current_epoch(state) + state.validators[source1_index].withdrawable_epoch = spec.get_current_epoch(state) + + # set first source as slashed + state.validators[source0_index].slashed = True + yield from run_epoch_processing_with(spec, state, "process_pending_consolidations") + + # first pending consolidation should not be processed + assert state.balances[target0_index] == spec.MIN_ACTIVATION_BALANCE + assert state.balances[source0_index] == spec.MIN_ACTIVATION_BALANCE + # second pending consolidation should be processed: first one is skipped and doesn't block the queue + assert state.balances[target1_index] == 2 * spec.MIN_ACTIVATION_BALANCE + assert state.balances[source1_index] == 0 diff --git a/tests/core/pyspec/eth2spec/test/eip7251/unittests/__init__.py b/tests/core/pyspec/eth2spec/test/eip7251/unittests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/core/pyspec/eth2spec/test/eip7251/unittests/test_config_invariants.py b/tests/core/pyspec/eth2spec/test/eip7251/unittests/test_config_invariants.py new file mode 100644 index 000000000..7acad99ef --- /dev/null +++ b/tests/core/pyspec/eth2spec/test/eip7251/unittests/test_config_invariants.py @@ -0,0 +1,12 @@ +from eth2spec.test.context import ( + single_phase, + spec_test, + with_eip7251_and_later, +) + + +@with_eip7251_and_later +@spec_test +@single_phase +def test_withdrawals(spec): + assert spec.MAX_PARTIAL_WITHDRAWALS_PER_PAYLOAD < spec.MAX_WITHDRAWALS_PER_PAYLOAD diff --git a/tests/core/pyspec/eth2spec/test/helpers/attestations.py b/tests/core/pyspec/eth2spec/test/helpers/attestations.py index 000318de4..528de7f67 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/attestations.py +++ b/tests/core/pyspec/eth2spec/test/helpers/attestations.py @@ -106,14 +106,12 @@ def get_valid_attestation(spec, beacon_committee = spec.get_beacon_committee(state, slot, index) - committee_size = len(beacon_committee) - aggregation_bits = Bitlist[spec.MAX_VALIDATORS_PER_COMMITTEE](*([0] * committee_size)) if is_post_eip7549(spec): - attestation = spec.Attestation( - aggregation_bits_list=[aggregation_bits], - data=attestation_data, - ) + # will fill aggregation_bits later + attestation = spec.Attestation(data=attestation_data) else: + committee_size = len(beacon_committee) + aggregation_bits = Bitlist[spec.MAX_VALIDATORS_PER_COMMITTEE](*([0] * committee_size)) attestation = spec.Attestation( aggregation_bits=aggregation_bits, data=attestation_data, @@ -182,17 +180,20 @@ def fill_aggregate_attestation(spec, state, attestation, committee_index, signed if filter_participant_set is not None: participants = filter_participant_set(participants) - committee_bits = spec.Bitvector[spec.MAX_COMMITTEES_PER_SLOT]() + if is_post_eip7549(spec): + attestation.committee_bits = spec.Bitvector[spec.MAX_COMMITTEES_PER_SLOT]() + attestation.committee_bits[committee_index] = True + attestation.aggregation_bits = get_empty_eip7549_aggregation_bits( + spec, state, attestation.committee_bits, attestation.data.slot) for i in range(len(beacon_committee)): if is_post_eip7549(spec): - committee_bits[committee_index] = True - attestation.aggregation_bits_list[0][i] = beacon_committee[i] in participants + offset = get_eip7549_aggregation_bits_offset( + spec, state, attestation.data.slot, attestation.committee_bits, committee_index) + aggregation_bits_index = offset + i + attestation.aggregation_bits[aggregation_bits_index] = beacon_committee[i] in participants else: attestation.aggregation_bits[i] = beacon_committee[i] in participants - if is_post_eip7549(spec): - attestation.committee_bits = committee_bits - if signed and len(participants) > 0: sign_attestation(spec, state, attestation) @@ -365,12 +366,8 @@ def prepare_state_with_attestations(spec, state, participation_fn=None): return participation_fn(state.slot, committee_index, comm) attestation = get_valid_attestation(spec, state, index=committee_index, filter_participant_set=temp_participants_filter, signed=True) - if is_post_eip7549(spec): - if any(attestation.aggregation_bits_list): - attestations.append(attestation) - else: - if any(attestation.aggregation_bits): # Only if there is at least 1 participant. - attestations.append(attestation) + if any(attestation.aggregation_bits): # Only if there is at least 1 participant. + attestations.append(attestation) # fill each created slot in state after inclusion delay if state.slot >= start_slot + spec.MIN_ATTESTATION_INCLUSION_DELAY: inclusion_slot = state.slot - spec.MIN_ATTESTATION_INCLUSION_DELAY @@ -411,3 +408,27 @@ def get_max_attestations(spec): return spec.MAX_ATTESTATIONS_EIP7549 else: return spec.MAX_ATTESTATIONS + + +def get_empty_eip7549_aggregation_bits(spec, state, committee_bits, slot): + committee_indices = spec.get_committee_indices(committee_bits) + participants_count = 0 + for index in committee_indices: + committee = spec.get_beacon_committee(state, slot, index) + participants_count += len(committee) + aggregation_bits = Bitlist[spec.MAX_VALIDATORS_PER_COMMITTEE * spec.MAX_COMMITTEES_PER_SLOT]( + [False] * participants_count + ) + return aggregation_bits + + +def get_eip7549_aggregation_bits_offset(spec, state, slot, committee_bits, committee_index): + committee_indices = spec.get_committee_indices(committee_bits) + assert committee_index in committee_indices + offset = 0 + for i in committee_indices: + if committee_index == i: + break + committee = spec.get_beacon_committee(state, slot, committee_indices[i]) + offset += len(committee) + return offset diff --git a/tests/core/pyspec/eth2spec/test/helpers/consolidations.py b/tests/core/pyspec/eth2spec/test/helpers/consolidations.py new file mode 100644 index 000000000..ccdcb9e06 --- /dev/null +++ b/tests/core/pyspec/eth2spec/test/helpers/consolidations.py @@ -0,0 +1,61 @@ +from eth2spec.utils import bls +from eth2spec.test.context import expect_assertion_error +from eth2spec.test.helpers.keys import privkeys + + +def prepare_signed_consolidations(spec, state, index_pairs, fork_version=None): + def create_signed_consolidation(source_index, target_index): + consolidation = spec.Consolidation( + epoch=spec.get_current_epoch(state), + source_index=source_index, + target_index=target_index, + ) + return sign_consolidation(spec, state, consolidation, privkeys[source_index], privkeys[target_index], + fork_version=fork_version) + + return [create_signed_consolidation(source_index, target_index) for (source_index, target_index) in index_pairs] + + +def sign_consolidation(spec, state, consolidation, source_privkey, target_privkey, fork_version=None): + domain = spec.compute_domain(spec.DOMAIN_CONSOLIDATION, genesis_validators_root=state.genesis_validators_root) + signing_root = spec.compute_signing_root(consolidation, domain) + return spec.SignedConsolidation( + message=consolidation, + signature=bls.Aggregate([bls.Sign(source_privkey, signing_root), bls.Sign(target_privkey, signing_root)]) + ) + + +def run_consolidation_processing(spec, state, signed_consolidation, valid=True): + """ + Run ``process_consolidation``, yielding: + - pre-state ('pre') + - consolidation ('consolidation') + - post-state ('post'). + If ``valid == False``, run expecting ``AssertionError`` + """ + + source_validator = state.validators[signed_consolidation.message.source_index] + target_validator = state.validators[signed_consolidation.message.target_index] + + yield 'pre', state + yield 'consolidation', signed_consolidation + + if not valid: + expect_assertion_error(lambda: spec.process_consolidation(state, signed_consolidation)) + yield 'post', None + return + + pre_exit_epoch = source_validator.exit_epoch + + spec.process_consolidation(state, signed_consolidation) + + yield 'post', state + + assert source_validator.withdrawal_credentials[1:] == target_validator.withdrawal_credentials[1:] + assert pre_exit_epoch == spec.FAR_FUTURE_EPOCH + assert state.validators[signed_consolidation.message.source_index].exit_epoch < spec.FAR_FUTURE_EPOCH + assert state.validators[signed_consolidation.message.source_index].exit_epoch == state.earliest_consolidation_epoch + assert state.pending_consolidations[len(state.pending_consolidations) - 1] == spec.PendingConsolidation( + source_index=signed_consolidation.message.source_index, + target_index=signed_consolidation.message.target_index + ) diff --git a/tests/core/pyspec/eth2spec/test/helpers/constants.py b/tests/core/pyspec/eth2spec/test/helpers/constants.py index b44ba9148..b482b3a02 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/constants.py +++ b/tests/core/pyspec/eth2spec/test/helpers/constants.py @@ -20,6 +20,7 @@ EIP6110 = SpecForkName('eip6110') EIP7002 = SpecForkName('eip7002') EIP7549 = SpecForkName('eip7549') WHISK = SpecForkName('whisk') +EIP7251 = SpecForkName('eip7251') EIP7594 = SpecForkName('eip7594') # @@ -39,6 +40,7 @@ ALL_PHASES = ( # Experimental patches EIP6110, EIP7002, + EIP7251, EIP7549, EIP7594, ) @@ -62,6 +64,7 @@ PREVIOUS_FORK_OF = { WHISK: CAPELLA, EIP7002: CAPELLA, EIP7549: DENEB, + EIP7251: DENEB, EIP7594: DENEB, } diff --git a/tests/core/pyspec/eth2spec/test/helpers/deposits.py b/tests/core/pyspec/eth2spec/test/helpers/deposits.py index 789b27c8f..846618adb 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/deposits.py +++ b/tests/core/pyspec/eth2spec/test/helpers/deposits.py @@ -390,3 +390,88 @@ def run_deposit_receipt_processing_with_specific_fork_version( valid=valid, effective=effective ) + + +# ******************** +# * EIP7251 * +# ******************** + + +def run_deposit_processing_eip7251(spec, state, deposit, validator_index, valid=True, effective=True): + """ + Run ``process_deposit``, yielding: + - pre-state ('pre') + - deposit ('deposit') + - post-state ('post'). + If ``valid == False``, run expecting ``AssertionError`` + """ + pre_validator_count = len(state.validators) + pre_pending_deposits = len(state.pending_balance_deposits) + pre_balance = 0 + pre_effective_balance = 0 + is_top_up = False + # is a top-up + if validator_index < pre_validator_count: + is_top_up = True + pre_balance = get_balance(state, validator_index) + pre_effective_balance = state.validators[validator_index].effective_balance + + yield 'pre', state + yield 'deposit', deposit + + if not valid: + expect_assertion_error(lambda: spec.process_deposit(state, deposit)) + yield 'post', None + return + + spec.process_deposit(state, deposit) + + yield 'post', state + + if not effective or not bls.KeyValidate(deposit.data.pubkey): + assert len(state.validators) == pre_validator_count + assert len(state.balances) == pre_validator_count + else: + # no balance changes on deposit processing + assert get_balance(state, validator_index) == pre_balance + assert state.validators[validator_index].effective_balance == pre_effective_balance + if is_top_up: + assert len(state.validators) == pre_validator_count + assert len(state.balances) == pre_validator_count + else: + # new validator + assert len(state.validators) == pre_validator_count + 1 + assert len(state.balances) == pre_validator_count + 1 + # new correct balance deposit has been appended + assert len(state.pending_balance_deposits) == pre_pending_deposits + 1 + assert state.pending_balance_deposits[pre_pending_deposits].amount == deposit.data.amount + assert state.pending_balance_deposits[pre_pending_deposits].index == validator_index + assert state.eth1_deposit_index == state.eth1_data.deposit_count + + +def run_deposit_processing_eip7251_with_specific_fork_version( + spec, + state, + fork_version, + valid=True, + effective=True): + validator_index = len(state.validators) + amount = spec.MAX_EFFECTIVE_BALANCE + + pubkey = pubkeys[validator_index] + privkey = privkeys[validator_index] + withdrawal_credentials = spec.BLS_WITHDRAWAL_PREFIX + spec.hash(pubkey)[1:] + + deposit_message = spec.DepositMessage(pubkey=pubkey, withdrawal_credentials=withdrawal_credentials, amount=amount) + domain = spec.compute_domain(domain_type=spec.DOMAIN_DEPOSIT, fork_version=fork_version) + deposit_data = spec.DepositData( + pubkey=pubkey, withdrawal_credentials=withdrawal_credentials, amount=amount, + signature=bls.Sign(privkey, spec.compute_signing_root(deposit_message, domain)) + ) + deposit, root, _ = deposit_from_context(spec, [deposit_data], 0) + + state.eth1_deposit_index = 0 + state.eth1_data.deposit_root = root + state.eth1_data.deposit_count = 1 + + yield from run_deposit_processing_eip7251(spec, state, deposit, validator_index, valid=valid, effective=effective) diff --git a/tests/core/pyspec/eth2spec/test/helpers/forks.py b/tests/core/pyspec/eth2spec/test/helpers/forks.py index f9d409b24..8b03c8fb7 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/forks.py +++ b/tests/core/pyspec/eth2spec/test/helpers/forks.py @@ -1,6 +1,6 @@ from .constants import ( PHASE0, ALTAIR, BELLATRIX, CAPELLA, DENEB, - EIP6110, EIP7002, EIP7549, WHISK, + EIP6110, EIP7002, EIP7251, EIP7549, WHISK, PREVIOUS_FORK_OF, ) @@ -45,6 +45,10 @@ def is_post_eip7002(spec): return is_post_fork(spec.fork, EIP7002) +def is_post_eip7251(spec): + return is_post_fork(spec.fork, EIP7251) + + def is_post_eip7549(spec): return is_post_fork(spec.fork, EIP7549) diff --git a/tests/core/pyspec/eth2spec/test/helpers/genesis.py b/tests/core/pyspec/eth2spec/test/helpers/genesis.py index ab8273f66..acf96a494 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/genesis.py +++ b/tests/core/pyspec/eth2spec/test/helpers/genesis.py @@ -6,7 +6,7 @@ from eth2spec.test.helpers.execution_payload import ( compute_el_header_block_hash, ) from eth2spec.test.helpers.forks import ( - is_post_altair, is_post_bellatrix, is_post_capella, is_post_eip6110, is_post_eip7002, is_post_whisk, + is_post_altair, is_post_bellatrix, is_post_capella, is_post_eip6110, is_post_eip7002, is_post_whisk, is_post_eip7251 ) from eth2spec.test.helpers.keys import pubkeys from eth2spec.test.helpers.whisk import compute_whisk_initial_tracker_cached, compute_whisk_initial_k_commitment_cached @@ -149,4 +149,11 @@ def create_genesis_state(spec, validator_balances, activation_threshold): for i in range(spec.WHISK_PROPOSER_TRACKERS_COUNT): state.whisk_proposer_trackers[i] = compute_whisk_initial_tracker_cached(i % vc) + if is_post_eip7251(spec): + state.deposit_balance_to_consume = 0 + state.exit_balance_to_consume = 0 + state.earliest_exit_epoch = spec.GENESIS_EPOCH + state.pending_balance_deposits = [] + state.pending_partial_withdrawals = [] + return state diff --git a/tests/core/pyspec/eth2spec/test/helpers/withdrawals.py b/tests/core/pyspec/eth2spec/test/helpers/withdrawals.py index 9ebb05ba0..cb2971e56 100644 --- a/tests/core/pyspec/eth2spec/test/helpers/withdrawals.py +++ b/tests/core/pyspec/eth2spec/test/helpers/withdrawals.py @@ -54,3 +54,11 @@ def prepare_expected_withdrawals(spec, state, set_validator_partially_withdrawable(spec, state, index) return fully_withdrawable_indices, partial_withdrawals_indices + + +def set_compounding_withdrawal_credential(spec, state, index, address=None): + if address is None: + address = b'\x11' * 20 + + validator = state.validators[index] + validator.withdrawal_credentials = spec.COMPOUNDING_WITHDRAWAL_PREFIX + b'\x00' * 11 + address diff --git a/tests/core/pyspec/eth2spec/test/phase0/block_processing/test_process_attestation.py b/tests/core/pyspec/eth2spec/test/phase0/block_processing/test_process_attestation.py index 56ee12026..4c62803be 100644 --- a/tests/core/pyspec/eth2spec/test/phase0/block_processing/test_process_attestation.py +++ b/tests/core/pyspec/eth2spec/test/phase0/block_processing/test_process_attestation.py @@ -14,7 +14,6 @@ from eth2spec.test.helpers.attestations import ( sign_attestation, compute_max_inclusion_slot, ) -from eth2spec.test.helpers.forks import is_post_eip7549 from eth2spec.test.helpers.state import ( next_slots, next_epoch_via_block, @@ -347,10 +346,7 @@ def test_invalid_too_many_aggregation_bits(spec, state): next_slots(spec, state, spec.MIN_ATTESTATION_INCLUSION_DELAY) # one too many bits - if is_post_eip7549(spec): - attestation.aggregation_bits_list[0].append(0b0) - else: - attestation.aggregation_bits.append(0b0) + attestation.aggregation_bits.append(0b0) yield from run_attestation_processing(spec, state, attestation, valid=False) @@ -361,21 +357,14 @@ def test_invalid_too_few_aggregation_bits(spec, state): attestation = get_valid_attestation(spec, state) next_slots(spec, state, spec.MIN_ATTESTATION_INCLUSION_DELAY) - if is_post_eip7549(spec): - attestation.aggregation_bits_list[0] = Bitlist[spec.MAX_VALIDATORS_PER_COMMITTEE]( - *([0b1] + [0b0] * (len(attestation.aggregation_bits_list[0]) - 1))) - else: - attestation.aggregation_bits = Bitlist[spec.MAX_VALIDATORS_PER_COMMITTEE]( - *([0b1] + [0b0] * (len(attestation.aggregation_bits) - 1)) - ) + attestation.aggregation_bits = Bitlist[spec.MAX_VALIDATORS_PER_COMMITTEE]( + *([0b1] + [0b0] * (len(attestation.aggregation_bits) - 1)) + ) sign_attestation(spec, state, attestation) # one too few bits - if is_post_eip7549(spec): - attestation.aggregation_bits_list = attestation.aggregation_bits_list[0][:-1] - else: - attestation.aggregation_bits = attestation.aggregation_bits[:-1] + attestation.aggregation_bits = attestation.aggregation_bits[:-1] yield from run_attestation_processing(spec, state, attestation, valid=False) diff --git a/tests/core/pyspec/eth2spec/test/phase0/fork_choice/test_ex_ante.py b/tests/core/pyspec/eth2spec/test/phase0/fork_choice/test_ex_ante.py index 744a47f89..7e64e647a 100644 --- a/tests/core/pyspec/eth2spec/test/phase0/fork_choice/test_ex_ante.py +++ b/tests/core/pyspec/eth2spec/test/phase0/fork_choice/test_ex_ante.py @@ -11,7 +11,6 @@ from eth2spec.test.helpers.block import ( build_empty_block, ) from eth2spec.test.helpers.constants import MAINNET -from eth2spec.test.helpers.forks import is_post_eip7549 from eth2spec.test.helpers.fork_choice import ( get_genesis_forkchoice_store_and_block, on_tick_and_append_step, @@ -33,10 +32,7 @@ def _apply_base_block_a(spec, state, store, test_steps): def _get_aggregation_bits(spec, attestation): - if is_post_eip7549(spec): - aggregation_bits = attestation.aggregation_bits_list[0] - else: - aggregation_bits = attestation.aggregation_bits + aggregation_bits = attestation.aggregation_bits return aggregation_bits diff --git a/tests/core/pyspec/eth2spec/test/phase0/unittests/test_config_invariants.py b/tests/core/pyspec/eth2spec/test/phase0/unittests/test_config_invariants.py index c58f817f7..d7e60f160 100644 --- a/tests/core/pyspec/eth2spec/test/phase0/unittests/test_config_invariants.py +++ b/tests/core/pyspec/eth2spec/test/phase0/unittests/test_config_invariants.py @@ -4,7 +4,7 @@ from eth2spec.test.context import ( ) from eth2spec.test.helpers.constants import UINT64_MAX from eth2spec.test.helpers.forks import ( - is_post_altair, is_post_bellatrix, + is_post_altair, is_post_bellatrix, is_post_eip7251, ) @@ -58,6 +58,8 @@ def test_incentives(spec, state): assert spec.MIN_SLASHING_PENALTY_QUOTIENT_BELLATRIX <= spec.WHISTLEBLOWER_REWARD_QUOTIENT elif is_post_altair(spec): assert spec.MIN_SLASHING_PENALTY_QUOTIENT_ALTAIR <= spec.WHISTLEBLOWER_REWARD_QUOTIENT + elif is_post_eip7251(spec): + assert spec.MIN_SLASHING_PENALTY_QUOTIENT_EIP7251 <= spec.WHISTLEBLOWER_REWARD_QUOTIENT_EIP7251 else: assert spec.MIN_SLASHING_PENALTY_QUOTIENT <= spec.WHISTLEBLOWER_REWARD_QUOTIENT diff --git a/tests/formats/light_client/single_merkle_proof.md b/tests/formats/light_client/single_merkle_proof.md index d2137605e..0cb4cd0d0 100644 --- a/tests/formats/light_client/single_merkle_proof.md +++ b/tests/formats/light_client/single_merkle_proof.md @@ -7,7 +7,7 @@ generation and verification of merkle proofs based on static data. Tests for each individual SSZ type are grouped into a `suite` indicating the SSZ type name. -### `object.yaml` +### `object.ssz_snappy` A SSZ-snappy encoded object from which other data is generated. The SSZ type can be determined from the test `suite` name.