diff --git a/specs/_features/eip7251/beacon-chain.md b/specs/_features/eip7251/beacon-chain.md index 70186b600..806b8d691 100644 --- a/specs/_features/eip7251/beacon-chain.md +++ b/specs/_features/eip7251/beacon-chain.md @@ -764,7 +764,7 @@ def apply_deposit(state: BeaconState, else: # Increase balance by deposit amount index = ValidatorIndex(validator_pubkeys.index(pubkey)) - state.pending_balance_deposits.append(PendingBalanceDeposit(index, amount)) # [Modified in EIP-7251] + state.pending_balance_deposits.append(PendingBalanceDeposit(index=index, amount=amount)) # [Modified in EIP-7251] ``` #### Modified `add_validator_to_registry` @@ -775,13 +775,13 @@ def add_validator_to_registry(state: BeaconState, withdrawal_credentials: Bytes32, amount: uint64) -> None: index = get_index_for_new_validator(state) - validator = get_validator_from_deposit(pubkey, withdrawal_credentials, amount) + 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, amount)) # [New in EIP7251] + state.pending_balance_deposits.append(PendingBalanceDeposit(index=index, amount=amount)) # [New in EIP7251] ``` ###### Updated `get_validator_from_deposit` diff --git a/specs/_features/eip7251/fork.md b/specs/_features/eip7251/fork.md index 2e501fc6b..277e43862 100644 --- a/specs/_features/eip7251/fork.md +++ b/specs/_features/eip7251/fork.md @@ -141,5 +141,5 @@ def queue_excess_active_balance(state: BeaconState): if has_compounding_withdrawal_credential(validator) and balance > MAX_EFFECTIVE_BALANCE: excess_balance = balance - MAX_EFFECTIVE_BALANCE state.balances[index] = balance - excess_balance - state.pending_balance_deposits.append(PendingBalanceDeposit(index, excess_balance)) + state.pending_balance_deposits.append(PendingBalanceDeposit(index=index, amount=excess_balance)) ``` diff --git a/tests/core/pyspec/eth2spec/test/context.py b/tests/core/pyspec/eth2spec/test/context.py index 4d12de351..9465c2a50 100644 --- a/tests/core/pyspec/eth2spec/test/context.py +++ b/tests/core/pyspec/eth2spec/test/context.py @@ -8,8 +8,8 @@ from eth2spec.utils import bls from .exceptions import SkippedTest from .helpers.constants import ( PHASE0, ALTAIR, BELLATRIX, CAPELLA, DENEB, - EIP6110, EIP7002, EIP7594, EIP7251, - WHISK, + EIP6110, EIP7002, EIP7594, + WHISK, EIP7251, MINIMAL, ALL_PHASES, POST_FORK_OF, @@ -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 @@ -136,6 +135,17 @@ def scaled_churn_balances_exceed_activation_churn_limit(spec: Spec): num_validators = spec.config.CHURN_LIMIT_QUOTIENT * (spec.config.MAX_PER_EPOCH_ACTIVATION_CHURN_LIMIT + 2) 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) 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..0b27d4a68 --- /dev/null +++ b/tests/core/pyspec/eth2spec/test/eip7251/block_processing/test_process_consolidation.py @@ -0,0 +1,555 @@ +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) \ No newline at end of file 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..9d153d1e8 --- /dev/null +++ b/tests/core/pyspec/eth2spec/test/eip7251/block_processing/test_process_deposit.py @@ -0,0 +1,283 @@ +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..5c72f236e --- /dev/null +++ b/tests/core/pyspec/eth2spec/test/eip7251/block_processing/test_process_execution_layer_withdraw_request.py @@ -0,0 +1,212 @@ +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 ( + next_epoch, + next_slot, +) +from eth2spec.test.helpers.withdrawals import ( + prepare_expected_withdrawals, + set_eth1_withdrawal_credential_with_balance, + set_validator_fully_withdrawable, + set_validator_partially_withdrawable, +) + +from eth2spec.test.context import expect_assertion_error +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..b331f1394 --- /dev/null +++ b/tests/core/pyspec/eth2spec/test/eip7251/block_processing/test_voluntary_exit.py @@ -0,0 +1,444 @@ +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..accb41140 --- /dev/null +++ b/tests/core/pyspec/eth2spec/test/eip7251/epoch_processing/test_process_pending_balance_deposits.py @@ -0,0 +1,97 @@ +from eth2spec.test.helpers.epoch_processing import run_epoch_processing_with +from eth2spec.test.context import ( + spec_state_test, + with_eip7251_and_later, + with_presets, +) + +@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..9f35f8275 --- /dev/null +++ b/tests/core/pyspec/eth2spec/test/eip7251/epoch_processing/test_process_pending_consolidations.py @@ -0,0 +1,71 @@ +from eth2spec.test.helpers.constants import MINIMAL +from eth2spec.test.helpers.epoch_processing import run_epoch_processing_with +from eth2spec.test.helpers.state import next_epoch +from eth2spec.test.context import ( + spec_state_test, + with_eip7251_and_later, + with_presets, + 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 +@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/helpers/consolidations.py b/tests/core/pyspec/eth2spec/test/helpers/consolidations.py new file mode 100644 index 000000000..b63381130 --- /dev/null +++ b/tests/core/pyspec/eth2spec/test/helpers/consolidations.py @@ -0,0 +1,61 @@ +from random import Random +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/deposits.py b/tests/core/pyspec/eth2spec/test/helpers/deposits.py index 789b27c8f..8e9d8176f 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/genesis.py b/tests/core/pyspec/eth2spec/test/helpers/genesis.py index ab8273f66..f6de59507 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,13 @@ 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..9a05b7959 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 \ No newline at end of file