This commit is contained in:
Alex Stokes 2024-04-15 14:30:56 -06:00
parent 55c5b10286
commit 05a891f801
No known key found for this signature in database
7 changed files with 847 additions and 385 deletions

View File

@ -4,7 +4,8 @@ from eth2spec.test.context import (
with_eip7251_and_later,
with_presets,
always_bls,
spec_test, single_phase,
spec_test,
single_phase,
with_custom_state,
scaled_churn_balances_exceed_activation_exit_churn_limit,
default_activation_threshold,
@ -27,7 +28,9 @@ from eth2spec.test.helpers.withdrawals import (
@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)
balances_fn=scaled_churn_balances_exceed_activation_exit_churn_limit,
threshold_fn=default_activation_threshold,
)
@spec_test
@single_phase
def test_basic_consolidation_in_current_consolidation_epoch(spec, state):
@ -42,13 +45,16 @@ def test_basic_consolidation_in_current_consolidation_epoch(spec, state):
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)
signed_consolidation = sign_consolidation(
spec,
state,
spec.Consolidation(
epoch=current_epoch, source_index=source_index, target_index=target_index
),
source_privkey,
target_privkey,
)
# Set earliest consolidation epoch to the expected exit epoch
expected_exit_epoch = spec.compute_activation_exit_epoch(current_epoch)
state.earliest_consolidation_epoch = expected_exit_epoch
@ -59,14 +65,20 @@ def test_basic_consolidation_in_current_consolidation_epoch(spec, state):
yield from run_consolidation_processing(spec, state, signed_consolidation)
# Check consolidation churn is decremented correctly
assert state.consolidation_balance_to_consume == consolidation_churn_limit - spec.MIN_ACTIVATION_BALANCE
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)
balances_fn=scaled_churn_balances_exceed_activation_exit_churn_limit,
threshold_fn=default_activation_threshold,
)
@spec_test
@single_phase
def test_basic_consolidation_in_new_consolidation_epoch(spec, state):
@ -83,19 +95,25 @@ def test_basic_consolidation_in_new_consolidation_epoch(spec, state):
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)
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
# consolidation_balance_to_consume is replenished to the churn limit since we move to a new consolidation epoch
consolidation_churn_limit = spec.get_consolidation_churn_limit(state)
assert state.consolidation_balance_to_consume == consolidation_churn_limit - spec.MIN_ACTIVATION_BALANCE
assert (
state.consolidation_balance_to_consume
== consolidation_churn_limit - spec.MIN_ACTIVATION_BALANCE
)
# Check exit epochs
assert state.validators[0].exit_epoch == expected_exit_epoch
@ -103,7 +121,9 @@ def test_basic_consolidation_in_new_consolidation_epoch(spec, state):
@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)
balances_fn=scaled_churn_balances_exceed_activation_exit_churn_limit,
threshold_fn=default_activation_threshold,
)
@spec_test
@single_phase
def test_basic_consolidation_with_preexisting_churn(spec, state):
@ -118,24 +138,30 @@ def test_basic_consolidation_with_preexisting_churn(spec, state):
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)
signed_consolidation = sign_consolidation(
spec,
state,
spec.Consolidation(
epoch=current_epoch, source_index=source_index, target_index=target_index
),
source_privkey,
target_privkey,
)
# Set earliest consolidation epoch to the expected exit epoch
expected_exit_epoch = spec.compute_activation_exit_epoch(current_epoch)
state.earliest_consolidation_epoch = expected_exit_epoch
# Set some nonzero preexisting churn lower than churn limit and sufficient to process the consolidation
preexisting_churn = 2*spec.MIN_ACTIVATION_BALANCE
preexisting_churn = 2 * spec.MIN_ACTIVATION_BALANCE
state.consolidation_balance_to_consume = preexisting_churn
yield from run_consolidation_processing(spec, state, signed_consolidation)
# Check consolidation churn is decremented correctly
assert state.consolidation_balance_to_consume == preexisting_churn - spec.MIN_ACTIVATION_BALANCE
assert (
state.consolidation_balance_to_consume
== preexisting_churn - spec.MIN_ACTIVATION_BALANCE
)
# Check exit epoch
assert state.validators[0].exit_epoch == expected_exit_epoch
@ -143,7 +169,9 @@ def test_basic_consolidation_with_preexisting_churn(spec, state):
@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)
balances_fn=scaled_churn_balances_exceed_activation_exit_churn_limit,
threshold_fn=default_activation_threshold,
)
@spec_test
@single_phase
def test_basic_consolidation_with_insufficient_preexisting_churn(spec, state):
@ -158,15 +186,20 @@ def test_basic_consolidation_with_insufficient_preexisting_churn(spec, state):
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)
signed_consolidation = sign_consolidation(
spec,
state,
spec.Consolidation(
epoch=current_epoch, source_index=source_index, target_index=target_index
),
source_privkey,
target_privkey,
)
# Set earliest consolidation epoch to the first available epoch
state.earliest_consolidation_epoch = spec.compute_activation_exit_epoch(current_epoch)
state.earliest_consolidation_epoch = spec.compute_activation_exit_epoch(
current_epoch
)
# Set preexisting churn lower than required to process the consolidation
preexisting_churn = spec.MIN_ACTIVATION_BALANCE - spec.EFFECTIVE_BALANCE_INCREMENT
state.consolidation_balance_to_consume = preexisting_churn
@ -178,7 +211,9 @@ def test_basic_consolidation_with_insufficient_preexisting_churn(spec, state):
# Check consolidation churn is decremented correctly
consolidation_churn_limit = spec.get_consolidation_churn_limit(state)
remainder = spec.MIN_ACTIVATION_BALANCE % preexisting_churn
assert state.consolidation_balance_to_consume == consolidation_churn_limit - remainder
assert (
state.consolidation_balance_to_consume == consolidation_churn_limit - remainder
)
# Check exit epoch
assert state.validators[0].exit_epoch == expected_exit_epoch
@ -186,7 +221,9 @@ def test_basic_consolidation_with_insufficient_preexisting_churn(spec, state):
@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)
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):
@ -204,17 +241,23 @@ def test_basic_consolidation_with_compounding_credential(spec, state):
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)
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
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
@ -222,7 +265,9 @@ def test_basic_consolidation_with_compounding_credential(spec, state):
@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)
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):
@ -243,18 +288,24 @@ def test_consolidation_churn_limit_balance(spec, state):
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)
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)
# validator's effective balance fits into the churn, exit as soon as possible
expected_exit_epoch = spec.compute_activation_exit_epoch(current_epoch)
# Check consolidation churn is decremented correctly
assert state.consolidation_balance_to_consume == updated_consolidation_churn_limit - consolidation_churn_limit
assert (
state.consolidation_balance_to_consume
== updated_consolidation_churn_limit - consolidation_churn_limit
)
# Check exit epoch
assert state.validators[0].exit_epoch == expected_exit_epoch
@ -262,7 +313,9 @@ def test_consolidation_churn_limit_balance(spec, state):
@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)
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):
@ -286,12 +339,15 @@ def test_consolidation_balance_larger_than_churn_limit(spec, state):
remainder = state.validators[source_index].effective_balance % new_churn_limit
expected_balance = new_churn_limit - remainder
signed_consolidation = sign_consolidation(spec, state,
spec.Consolidation(
epoch=current_epoch,
source_index=source_index,
target_index=target_index),
source_privkey, target_privkey)
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
@ -304,7 +360,9 @@ def test_consolidation_balance_larger_than_churn_limit(spec, state):
@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)
balances_fn=scaled_churn_balances_exceed_activation_exit_churn_limit,
threshold_fn=default_activation_threshold,
)
@spec_test
@single_phase
def test_consolidation_balance_through_two_churn_epochs(spec, state):
@ -328,12 +386,15 @@ def test_consolidation_balance_through_two_churn_epochs(spec, state):
remainder = state.validators[source_index].effective_balance % new_churn_limit
expected_balance = new_churn_limit - remainder
signed_consolidation = sign_consolidation(spec, state,
spec.Consolidation(
epoch=current_epoch,
source_index=source_index,
target_index=target_index),
source_privkey, target_privkey)
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
@ -346,7 +407,9 @@ def test_consolidation_balance_through_two_churn_epochs(spec, state):
@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)
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):
@ -367,12 +430,17 @@ def test_multiple_consolidations_below_churn(spec, state):
# 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)
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
@ -385,7 +453,10 @@ def test_multiple_consolidations_below_churn(spec, 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
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
@ -393,7 +464,9 @@ def test_multiple_consolidations_below_churn(spec, state):
@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)
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):
@ -414,12 +487,17 @@ def test_multiple_consolidations_equal_churn(spec, state):
# 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)
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
@ -440,7 +518,9 @@ def test_multiple_consolidations_equal_churn(spec, state):
@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)
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):
@ -460,12 +540,17 @@ def test_multiple_consolidations_above_churn(spec, state):
# 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)
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
@ -484,19 +569,25 @@ def test_multiple_consolidations_above_churn(spec, state):
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)
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.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
@ -505,7 +596,9 @@ def test_multiple_consolidations_above_churn(spec, state):
@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)
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):
@ -526,12 +619,17 @@ def test_multiple_consolidations_equal_twice_churn(spec, state):
# 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)
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
@ -553,6 +651,7 @@ def test_multiple_consolidations_equal_twice_churn(spec, state):
# Failing tests
@with_eip7251_and_later
@spec_state_test
def test_invalid_source_equals_target(spec, state):
@ -563,47 +662,70 @@ def test_invalid_source_equals_target(spec, state):
# 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)
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
)
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)
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_not_enough_consolidation_churn_available(spec, state):
state.validators = state.validators[0:2]
state.pending_consolidations = [spec.PendingConsolidation(source_index=0, target_index=1)]
state.pending_consolidations = [
spec.PendingConsolidation(source_index=0, target_index=1)
]
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)
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
@ -613,12 +735,18 @@ def test_invalid_exited_source(spec, state):
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)
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)
yield from run_consolidation_processing(
spec, state, signed_consolidation, valid=False
)
@with_eip7251_and_later
@ -630,15 +758,18 @@ def test_invalid_exited_target(spec, state):
# 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)
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)
yield from run_consolidation_processing(
spec, state, signed_consolidation, valid=False
)
@with_eip7251_and_later
@ -649,15 +780,18 @@ def test_invalid_inactive_source(spec, state):
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)
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)
yield from run_consolidation_processing(
spec, state, signed_consolidation, valid=False
)
@with_eip7251_and_later
@ -669,15 +803,18 @@ def test_invalid_inactive_target(spec, state):
# 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)
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)
yield from run_consolidation_processing(
spec, state, signed_consolidation, valid=False
)
@with_eip7251_and_later
@ -686,13 +823,16 @@ 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)
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
@ -701,16 +841,19 @@ 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)
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)
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
@ -723,15 +866,18 @@ def test_invalid_source_signature(spec, state):
# 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)
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)
yield from run_consolidation_processing(
spec, state, signed_consolidation, valid=False
)
@with_eip7251_and_later
@ -744,15 +890,18 @@ def test_invalid_target_signature(spec, state):
# 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)
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)
yield from run_consolidation_processing(
spec, state, signed_consolidation, valid=False
)
@with_eip7251_and_later
@ -765,10 +914,13 @@ def test_invalid_before_specified_epoch(spec, state):
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)
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
)

View File

@ -20,6 +20,7 @@ from eth2spec.test.helpers.withdrawals import (
# 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):
@ -29,15 +30,19 @@ def test_basic_exit(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)
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=spec.FULL_EXIT_REQUEST_AMOUNT,
)
yield from run_execution_layer_withdraw_request_processing(spec, state, execution_layer_withdraw_request)
yield from run_execution_layer_withdraw_request_processing(
spec, state, execution_layer_withdraw_request
)
@with_eip7251_and_later
@ -49,7 +54,7 @@ def test_basic_exit_with_compounding_credentials(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
address = b"\x22" * 20
set_compounding_withdrawal_credential(spec, state, validator_index, address=address)
execution_layer_withdraw_request = spec.ExecutionLayerWithdrawRequest(
source_address=address,
@ -57,7 +62,9 @@ def test_basic_exit_with_compounding_credentials(spec, state):
amount=spec.FULL_EXIT_REQUEST_AMOUNT,
)
yield from run_execution_layer_withdraw_request_processing(spec, state, execution_layer_withdraw_request)
yield from run_execution_layer_withdraw_request_processing(
spec, state, execution_layer_withdraw_request
)
@with_eip7251_and_later
@ -68,8 +75,10 @@ def test_basic_exit_with_full_partial_withdrawal_queue(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)
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,
@ -77,17 +86,24 @@ def test_basic_exit_with_full_partial_withdrawal_queue(spec, state):
)
# Fill the partial withdrawal queue to the max (with a different validator index)
partial_withdrawal = spec.PendingPartialWithdrawal(index=1, amount=1, withdrawable_epoch=current_epoch)
state.pending_partial_withdrawals = [partial_withdrawal] * spec.PENDING_PARTIAL_WITHDRAWALS_LIMIT
partial_withdrawal = spec.PendingPartialWithdrawal(
index=1, amount=1, withdrawable_epoch=current_epoch
)
state.pending_partial_withdrawals = [
partial_withdrawal
] * spec.PENDING_PARTIAL_WITHDRAWALS_LIMIT
# Exit should still be processed
yield from run_execution_layer_withdraw_request_processing(
spec, state, execution_layer_withdraw_request,
spec,
state,
execution_layer_withdraw_request,
)
# Invalid tests
@with_eip7251_and_later
@spec_state_test
def test_incorrect_source_address(spec, state):
@ -97,9 +113,11 @@ def test_incorrect_source_address(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
incorrect_address = b'\x33' * 20
set_eth1_withdrawal_credential_with_balance(spec, state, validator_index, address=address)
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,
@ -120,8 +138,10 @@ def test_incorrect_withdrawal_credential_prefix(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)
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
@ -147,8 +167,10 @@ def test_on_exit_initiated_validator(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)
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(
@ -168,8 +190,10 @@ 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)
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,
@ -177,7 +201,8 @@ def test_activation_epoch_less_than_shard_committee_period(spec, state):
)
assert spec.get_current_epoch(state) < (
state.validators[validator_index].activation_epoch + spec.config.SHARD_COMMITTEE_PERIOD
state.validators[validator_index].activation_epoch
+ spec.config.SHARD_COMMITTEE_PERIOD
)
yield from run_execution_layer_withdraw_request_processing(
@ -185,9 +210,9 @@ def test_activation_epoch_less_than_shard_committee_period(spec, state):
)
# Partial withdrawals tests
@with_eip7251_and_later
@spec_state_test
@with_presets([MINIMAL])
@ -196,7 +221,7 @@ def test_basic_partial_withdrawal_request(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
address = b"\x22" * 20
amount = spec.EFFECTIVE_BALANCE_INCREMENT
# Set excess balance exactly to the requested amount
state.balances[validator_index] += amount
@ -209,11 +234,16 @@ def test_basic_partial_withdrawal_request(spec, state):
)
yield from run_execution_layer_withdraw_request_processing(
spec, state, execution_layer_withdraw_request,
spec,
state,
execution_layer_withdraw_request,
)
# Check that the assigned exit epoch is correct
assert state.earliest_exit_epoch == spec.compute_activation_exit_epoch(current_epoch)
assert state.earliest_exit_epoch == spec.compute_activation_exit_epoch(
current_epoch
)
@with_eip7251_and_later
@spec_state_test
@ -223,7 +253,7 @@ def test_basic_partial_withdrawal_request_higher_excess_balance(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
address = b"\x22" * 20
amount = spec.EFFECTIVE_BALANCE_INCREMENT
# Set excess balance higher than requested amount
state.balances[validator_index] += 2 * amount
@ -236,11 +266,16 @@ def test_basic_partial_withdrawal_request_higher_excess_balance(spec, state):
)
yield from run_execution_layer_withdraw_request_processing(
spec, state, execution_layer_withdraw_request,
spec,
state,
execution_layer_withdraw_request,
)
# Check that the assigned exit epoch is correct
assert state.earliest_exit_epoch == spec.compute_activation_exit_epoch(current_epoch)
assert state.earliest_exit_epoch == spec.compute_activation_exit_epoch(
current_epoch
)
@with_eip7251_and_later
@spec_state_test
@ -250,7 +285,7 @@ def test_basic_partial_withdrawal_request_lower_than_excess_balance(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
address = b"\x22" * 20
excess_balance = spec.EFFECTIVE_BALANCE_INCREMENT
amount = 2 * excess_balance
# Set excess balance higher than requested amount
@ -264,11 +299,16 @@ def test_basic_partial_withdrawal_request_lower_than_excess_balance(spec, state)
)
yield from run_execution_layer_withdraw_request_processing(
spec, state, execution_layer_withdraw_request,
spec,
state,
execution_layer_withdraw_request,
)
# Check that the assigned exit epoch is correct
assert state.earliest_exit_epoch == spec.compute_activation_exit_epoch(current_epoch)
assert state.earliest_exit_epoch == spec.compute_activation_exit_epoch(
current_epoch
)
@with_eip7251_and_later
@spec_state_test
@ -278,7 +318,7 @@ def test_partial_withdrawal_request_with_pending_withdrawals(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
address = b"\x22" * 20
amount = spec.EFFECTIVE_BALANCE_INCREMENT
set_compounding_withdrawal_credential(spec, state, validator_index, address=address)
@ -289,30 +329,37 @@ def test_partial_withdrawal_request_with_pending_withdrawals(spec, state):
)
# Add pending withdrawals
partial_withdrawal = spec.PendingPartialWithdrawal(index=validator_index,
amount=amount,
withdrawable_epoch=current_epoch)
partial_withdrawal = spec.PendingPartialWithdrawal(
index=validator_index, amount=amount, withdrawable_epoch=current_epoch
)
state.pending_partial_withdrawals = [partial_withdrawal] * 2
# Set balance so that the validator still has excess balance even with the pending withdrawals
state.balances[validator_index] += 3*amount
state.balances[validator_index] += 3 * amount
yield from run_execution_layer_withdraw_request_processing(
spec, state, execution_layer_withdraw_request,
spec,
state,
execution_layer_withdraw_request,
)
# Check that the assigned exit epoch is correct
assert state.earliest_exit_epoch == spec.compute_activation_exit_epoch(current_epoch)
assert state.earliest_exit_epoch == spec.compute_activation_exit_epoch(
current_epoch
)
@with_eip7251_and_later
@spec_state_test
@with_presets([MINIMAL])
def test_partial_withdrawal_request_with_pending_withdrawals_and_high_amount(spec, state):
def test_partial_withdrawal_request_with_pending_withdrawals_and_high_amount(
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
address = b"\x22" * 20
amount = spec.UINT64_MAX
set_compounding_withdrawal_credential(spec, state, validator_index, address=address)
@ -323,16 +370,22 @@ def test_partial_withdrawal_request_with_pending_withdrawals_and_high_amount(spe
)
# Add many pending withdrawals
partial_withdrawal = spec.PendingPartialWithdrawal(index=validator_index,
amount=spec.EFFECTIVE_BALANCE_INCREMENT,
withdrawable_epoch=current_epoch)
state.pending_partial_withdrawals = [partial_withdrawal] * (spec.PENDING_PARTIAL_WITHDRAWALS_LIMIT-1)
partial_withdrawal = spec.PendingPartialWithdrawal(
index=validator_index,
amount=spec.EFFECTIVE_BALANCE_INCREMENT,
withdrawable_epoch=current_epoch,
)
state.pending_partial_withdrawals = [partial_withdrawal] * (
spec.PENDING_PARTIAL_WITHDRAWALS_LIMIT - 1
)
# Set balance so that the validator still has excess balance even with the pending withdrawals
state.balances[validator_index] = spec.MAX_EFFECTIVE_BALANCE_EIP7251
yield from run_execution_layer_withdraw_request_processing(
spec, state, execution_layer_withdraw_request,
spec,
state,
execution_layer_withdraw_request,
)
@ -344,10 +397,12 @@ def test_partial_withdrawal_request_with_high_balance(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
address = b"\x22" * 20
amount = spec.MAX_EFFECTIVE_BALANCE_EIP7251
state.balances[validator_index] = 3 * spec.MAX_EFFECTIVE_BALANCE_EIP7251
state.validators[validator_index].effective_balance = spec.MAX_EFFECTIVE_BALANCE_EIP7251
state.validators[validator_index].effective_balance = (
spec.MAX_EFFECTIVE_BALANCE_EIP7251
)
set_compounding_withdrawal_credential(spec, state, validator_index, address=address)
execution_layer_withdraw_request = spec.ExecutionLayerWithdrawRequest(
@ -359,13 +414,18 @@ def test_partial_withdrawal_request_with_high_balance(spec, state):
churn_limit = spec.get_activation_exit_churn_limit(state)
yield from run_execution_layer_withdraw_request_processing(
spec, state, execution_layer_withdraw_request,
spec,
state,
execution_layer_withdraw_request,
)
# Check that the assigned exit epoch is correct
exit_epoch = spec.compute_activation_exit_epoch(current_epoch) + amount // churn_limit
exit_epoch = (
spec.compute_activation_exit_epoch(current_epoch) + amount // churn_limit
)
assert state.earliest_exit_epoch == exit_epoch
@with_eip7251_and_later
@spec_state_test
@with_presets([MINIMAL])
@ -374,7 +434,7 @@ def test_partial_withdrawal_request_with_high_amount(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
address = b"\x22" * 20
# Set high amount requested to withdraw
amount = spec.UINT64_MAX
# Give the validator some excess balance to withdraw
@ -388,11 +448,15 @@ def test_partial_withdrawal_request_with_high_amount(spec, state):
)
yield from run_execution_layer_withdraw_request_processing(
spec, state, execution_layer_withdraw_request,
spec,
state,
execution_layer_withdraw_request,
)
# Check that the assigned exit epoch is correct
assert state.earliest_exit_epoch == spec.compute_activation_exit_epoch(current_epoch)
assert state.earliest_exit_epoch == spec.compute_activation_exit_epoch(
current_epoch
)
@with_eip7251_and_later
@ -403,7 +467,7 @@ def test_partial_withdrawal_request_with_low_amount(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
address = b"\x22" * 20
amount = 1
# Give the validator some excess balance to withdraw
state.balances[validator_index] += amount
@ -416,14 +480,20 @@ def test_partial_withdrawal_request_with_low_amount(spec, state):
)
yield from run_execution_layer_withdraw_request_processing(
spec, state, execution_layer_withdraw_request,
spec,
state,
execution_layer_withdraw_request,
)
# Check that the assigned exit epoch is correct
assert state.earliest_exit_epoch == spec.compute_activation_exit_epoch(current_epoch)
assert state.earliest_exit_epoch == spec.compute_activation_exit_epoch(
current_epoch
)
# No-op partial withdrawal tests
@with_eip7251_and_later
@spec_state_test
@with_presets([MINIMAL], "need full partial withdrawal queue")
@ -432,10 +502,10 @@ def test_partial_withdrawal_queue_full(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
address = b"\x22" * 20
amount = spec.EFFECTIVE_BALANCE_INCREMENT
# Ensure that the validator has sufficient excess balance
state.balances[validator_index] += 2*amount
state.balances[validator_index] += 2 * amount
set_compounding_withdrawal_credential(spec, state, validator_index, address=address)
execution_layer_withdraw_request = spec.ExecutionLayerWithdrawRequest(
source_address=address,
@ -444,8 +514,12 @@ def test_partial_withdrawal_queue_full(spec, state):
)
# Fill the partial withdrawal queue to the max
partial_withdrawal = spec.PendingPartialWithdrawal(index=1, amount=1, withdrawable_epoch=current_epoch)
state.pending_partial_withdrawals = [partial_withdrawal] * spec.PENDING_PARTIAL_WITHDRAWALS_LIMIT
partial_withdrawal = spec.PendingPartialWithdrawal(
index=1, 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
)
@ -458,12 +532,14 @@ def test_no_compounding_credentials(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
address = b"\x22" * 20
amount = spec.EFFECTIVE_BALANCE_INCREMENT
# Ensure that the validator has sufficient excess balance
state.balances[validator_index] += 2*amount
state.balances[validator_index] += 2 * amount
set_eth1_withdrawal_credential_with_balance(spec, state, validator_index, address=address)
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,
@ -471,7 +547,10 @@ def test_no_compounding_credentials(spec, state):
)
yield from run_execution_layer_withdraw_request_processing(
spec, state, execution_layer_withdraw_request, success=False,
spec,
state,
execution_layer_withdraw_request,
success=False,
)
@ -482,7 +561,7 @@ def test_no_excess_balance(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
address = b"\x22" * 20
amount = spec.EFFECTIVE_BALANCE_INCREMENT
set_compounding_withdrawal_credential(spec, state, validator_index, address=address)
@ -496,6 +575,7 @@ def test_no_excess_balance(spec, state):
spec, state, execution_layer_withdraw_request, success=False
)
@with_eip7251_and_later
@spec_state_test
def test_pending_withdrawals_consume_all_excess_balance(spec, state):
@ -503,7 +583,7 @@ def test_pending_withdrawals_consume_all_excess_balance(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
address = b"\x22" * 20
amount = spec.EFFECTIVE_BALANCE_INCREMENT
# Add excess balance
state.balances[validator_index] += 10 * amount
@ -516,15 +596,16 @@ def test_pending_withdrawals_consume_all_excess_balance(spec, state):
)
# Add pending withdrawals totalling an amount equal to the excess balance
partial_withdrawal = spec.PendingPartialWithdrawal(index=validator_index,
amount=amount,
withdrawable_epoch=current_epoch)
partial_withdrawal = spec.PendingPartialWithdrawal(
index=validator_index, amount=amount, withdrawable_epoch=current_epoch
)
state.pending_partial_withdrawals = [partial_withdrawal] * 10
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_insufficient_effective_balance(spec, state):
@ -532,10 +613,12 @@ def test_insufficient_effective_balance(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
address = b"\x22" * 20
amount = spec.EFFECTIVE_BALANCE_INCREMENT
# Make effective balance insufficient
state.validators[validator_index].effective_balance -= spec.EFFECTIVE_BALANCE_INCREMENT
state.validators[
validator_index
].effective_balance -= spec.EFFECTIVE_BALANCE_INCREMENT
set_compounding_withdrawal_credential(spec, state, validator_index, address=address)
execution_layer_withdraw_request = spec.ExecutionLayerWithdrawRequest(
@ -545,7 +628,10 @@ def test_insufficient_effective_balance(spec, state):
)
yield from run_execution_layer_withdraw_request_processing(
spec, state, execution_layer_withdraw_request, success=False,
spec,
state,
execution_layer_withdraw_request,
success=False,
)
@ -558,11 +644,11 @@ def test_partial_withdrawal_incorrect_source_address(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
incorrect_address = b'\x33' * 20
address = b"\x22" * 20
incorrect_address = b"\x33" * 20
amount = spec.EFFECTIVE_BALANCE_INCREMENT
state.balances[validator_index] += 2 * amount
set_compounding_withdrawal_credential(spec, state, validator_index, address=address)
execution_layer_withdraw_request = spec.ExecutionLayerWithdrawRequest(
source_address=incorrect_address,
@ -584,7 +670,7 @@ def test_partial_withdrawal_incorrect_withdrawal_credential_prefix(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
address = b"\x22" * 20
amount = spec.EFFECTIVE_BALANCE_INCREMENT
state.balances[validator_index] += 2 * amount
set_compounding_withdrawal_credential(spec, state, validator_index, address=address)
@ -613,7 +699,7 @@ def test_partial_withdrawal_on_exit_initiated_validator(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
address = b"\x22" * 20
amount = spec.EFFECTIVE_BALANCE_INCREMENT
state.balances[validator_index] += 2 * amount
set_compounding_withdrawal_credential(spec, state, validator_index, address=address)
@ -632,11 +718,13 @@ def test_partial_withdrawal_on_exit_initiated_validator(spec, state):
@with_eip7251_and_later
@spec_state_test
def test_partial_withdrawal_activation_epoch_less_than_shard_committee_period(spec, state):
def test_partial_withdrawal_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
address = b"\x22" * 20
amount = spec.EFFECTIVE_BALANCE_INCREMENT
state.balances[validator_index] += 2 * amount
set_compounding_withdrawal_credential(spec, state, validator_index, address=address)
@ -647,7 +735,8 @@ def test_partial_withdrawal_activation_epoch_less_than_shard_committee_period(sp
)
assert spec.get_current_epoch(state) < (
state.validators[validator_index].activation_epoch + spec.config.SHARD_COMMITTEE_PERIOD
state.validators[validator_index].activation_epoch
+ spec.config.SHARD_COMMITTEE_PERIOD
)
yield from run_execution_layer_withdraw_request_processing(
@ -655,11 +744,11 @@ def test_partial_withdrawal_activation_epoch_less_than_shard_committee_period(sp
)
#
# Run processing
#
def run_execution_layer_withdraw_request_processing(
spec, state, execution_layer_withdraw_request, valid=True, success=True
):
@ -671,15 +760,20 @@ def run_execution_layer_withdraw_request_processing(
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)
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
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
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
@ -687,18 +781,24 @@ def run_execution_layer_withdraw_request_processing(
pre_balance = state.balances[validator_index]
pre_effective_balance = state.validators[validator_index].effective_balance
pre_state = state
expected_amount_to_withdraw = compute_amount_to_withdraw(spec, state, validator_index, execution_layer_withdraw_request.amount)
expected_amount_to_withdraw = compute_amount_to_withdraw(
spec, state, validator_index, execution_layer_withdraw_request.amount
)
spec.process_execution_layer_withdraw_request(state, execution_layer_withdraw_request)
spec.process_execution_layer_withdraw_request(
state, execution_layer_withdraw_request
)
yield 'post', state
yield "post", state
if not success:
# No-op
assert pre_state == state
else:
assert state.balances[validator_index] == pre_balance
assert state.validators[validator_index].effective_balance == pre_effective_balance
assert (
state.validators[validator_index].effective_balance == pre_effective_balance
)
# Full exit request
if execution_layer_withdraw_request.amount == spec.FULL_EXIT_REQUEST_AMOUNT:
assert pre_exit_epoch == spec.FAR_FUTURE_EPOCH
@ -708,16 +808,26 @@ def run_execution_layer_withdraw_request_processing(
# Partial withdrawal request
else:
assert state.validators[validator_index].exit_epoch == spec.FAR_FUTURE_EPOCH
expected_withdrawable_epoch = state.earliest_exit_epoch + spec.config.MIN_VALIDATOR_WITHDRAWABILITY_DELAY
expected_partial_withdrawal = spec.PendingPartialWithdrawal(index=validator_index,
amount=expected_amount_to_withdraw,
withdrawable_epoch=expected_withdrawable_epoch)
assert state.pending_partial_withdrawals == pre_pending_partial_withdrawals + [expected_partial_withdrawal]
expected_withdrawable_epoch = (
state.earliest_exit_epoch
+ spec.config.MIN_VALIDATOR_WITHDRAWABILITY_DELAY
)
expected_partial_withdrawal = spec.PendingPartialWithdrawal(
index=validator_index,
amount=expected_amount_to_withdraw,
withdrawable_epoch=expected_withdrawable_epoch,
)
assert (
state.pending_partial_withdrawals
== pre_pending_partial_withdrawals + [expected_partial_withdrawal]
)
def compute_amount_to_withdraw(spec, state, index, amount):
pending_balance_to_withdraw = spec.get_pending_balance_to_withdraw(state, index)
return min(
state.balances[index] - spec.MIN_ACTIVATION_BALANCE - pending_balance_to_withdraw,
amount
)
state.balances[index]
- spec.MIN_ACTIVATION_BALANCE
- pending_balance_to_withdraw,
amount,
)

View File

@ -9,6 +9,7 @@ from eth2spec.test.helpers.voluntary_exits import (
run_voluntary_exit_processing,
sign_voluntary_exit,
)
# ********************
# * EXIT QUEUE TESTS *
# ********************
@ -21,18 +22,26 @@ def test_min_balance_exit(spec, state):
# This state has 64 validators each with 32 ETH
current_epoch = spec.get_current_epoch(state)
expected_exit_epoch = spec.compute_activation_exit_epoch(current_epoch)
expected_withdrawable_epoch = expected_exit_epoch + spec.config.MIN_VALIDATOR_WITHDRAWABILITY_DELAY
expected_withdrawable_epoch = (
expected_exit_epoch + spec.config.MIN_VALIDATOR_WITHDRAWABILITY_DELAY
)
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
validator_index = spec.get_active_validator_indices(state, spec.get_current_epoch(state))[0]
validator_index = spec.get_active_validator_indices(
state, spec.get_current_epoch(state)
)[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)
spec,
state,
spec.VoluntaryExit(epoch=current_epoch, validator_index=validator_index),
privkey,
)
yield from run_voluntary_exit_processing(spec, state, signed_voluntary_exit)
# Check exit queue churn is set correctly
assert state.exit_balance_to_consume == churn_limit - spec.MIN_ACTIVATION_BALANCE
# Check exit epoch and withdrawable epoch
@ -54,24 +63,43 @@ def test_min_balance_exits_up_to_churn(spec, state):
num_to_exit = churn_limit // single_validator_balance
# Exit all but 1 validators, all fit in the churn limit
for i in range(num_to_exit-1):
validator_index = spec.get_active_validator_indices(state, spec.get_current_epoch(state))[i]
for i in range(num_to_exit - 1):
validator_index = spec.get_active_validator_indices(
state, spec.get_current_epoch(state)
)[i]
spec.initiate_validator_exit(state, validator_index)
validator_index = spec.get_active_validator_indices(state, spec.get_current_epoch(state))[num_to_exit]
validator_index = spec.get_active_validator_indices(
state, spec.get_current_epoch(state)
)[num_to_exit]
privkey = pubkey_to_privkey[state.validators[validator_index].pubkey]
signed_voluntary_exit = sign_voluntary_exit(
spec, state, spec.VoluntaryExit(epoch=spec.get_current_epoch(state), validator_index=validator_index), privkey)
spec,
state,
spec.VoluntaryExit(
epoch=spec.get_current_epoch(state), validator_index=validator_index
),
privkey,
)
yield from run_voluntary_exit_processing(spec, state, signed_voluntary_exit)
# Last validator also fits in the churn limit
expected_exit_epoch = spec.compute_activation_exit_epoch(spec.get_current_epoch(state))
expected_withdrawable_epoch = expected_exit_epoch + spec.config.MIN_VALIDATOR_WITHDRAWABILITY_DELAY
expected_exit_epoch = spec.compute_activation_exit_epoch(
spec.get_current_epoch(state)
)
expected_withdrawable_epoch = (
expected_exit_epoch + spec.config.MIN_VALIDATOR_WITHDRAWABILITY_DELAY
)
# Check exit epoch and withdrawable epoch
assert state.validators[num_to_exit].exit_epoch == expected_exit_epoch
assert state.validators[num_to_exit].withdrawable_epoch == expected_withdrawable_epoch
assert (
state.validators[num_to_exit].withdrawable_epoch == expected_withdrawable_epoch
)
# Check exit queue churn is set
assert state.exit_balance_to_consume == churn_limit - single_validator_balance * num_to_exit
assert (
state.exit_balance_to_consume
== churn_limit - single_validator_balance * num_to_exit
)
# Check earliest_exit_epoch
assert state.earliest_exit_epoch == expected_exit_epoch
@ -82,8 +110,12 @@ def test_min_balance_exits_above_churn(spec, state):
state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
# 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))
expected_withdrawable_epoch = expected_exit_epoch + spec.config.MIN_VALIDATOR_WITHDRAWABILITY_DELAY
expected_exit_epoch = spec.compute_activation_exit_epoch(
spec.get_current_epoch(state)
)
expected_withdrawable_epoch = (
expected_exit_epoch + spec.config.MIN_VALIDATOR_WITHDRAWABILITY_DELAY
)
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
@ -91,29 +123,45 @@ def test_min_balance_exits_above_churn(spec, state):
# Exit all but 1 validators, all fit in the churn limit
for i in range(num_to_exit):
validator_index = spec.get_active_validator_indices(state, spec.get_current_epoch(state))[i]
validator_index = spec.get_active_validator_indices(
state, spec.get_current_epoch(state)
)[i]
spec.initiate_validator_exit(state, validator_index)
# Exit one more validator, not fitting in the churn limit
validator_index = spec.get_active_validator_indices(state, spec.get_current_epoch(state))[num_to_exit]
validator_index = spec.get_active_validator_indices(
state, spec.get_current_epoch(state)
)[num_to_exit]
privkey = pubkey_to_privkey[state.validators[validator_index].pubkey]
signed_voluntary_exit = sign_voluntary_exit(
spec, state, spec.VoluntaryExit(epoch=spec.get_current_epoch(state), validator_index=validator_index), privkey)
spec,
state,
spec.VoluntaryExit(
epoch=spec.get_current_epoch(state), validator_index=validator_index
),
privkey,
)
yield from run_voluntary_exit_processing(spec, state, signed_voluntary_exit)
# Check exit epoch and withdrawable epoch. Last validator exits one epoch later
assert state.validators[num_to_exit].exit_epoch == expected_exit_epoch + 1
assert state.validators[num_to_exit].withdrawable_epoch == expected_withdrawable_epoch + 1
assert (
state.validators[num_to_exit].withdrawable_epoch
== expected_withdrawable_epoch + 1
)
# Check exit balance to consume is set correctly
remainder = (num_to_exit + 1) * single_validator_balance % churn_limit
assert state.exit_balance_to_consume == churn_limit - remainder
assert state.exit_balance_to_consume == churn_limit - remainder
# Check earliest_exit_epoch
assert state.earliest_exit_epoch == expected_exit_epoch + 1
@with_eip7251_and_later
@spec_state_test
@with_presets([MAINNET], "With CHURN_LIMIT_QUOTIENT=32, can't change validator balance without changing churn_limit")
@with_presets(
[MAINNET],
"With CHURN_LIMIT_QUOTIENT=32, can't change validator balance without changing churn_limit",
)
def test_max_balance_exit(spec, state):
state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
current_epoch = spec.get_current_epoch(state)
@ -125,15 +173,26 @@ def test_max_balance_exit(spec, state):
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)
spec,
state,
spec.VoluntaryExit(epoch=current_epoch, validator_index=validator_index),
privkey,
)
yield from run_voluntary_exit_processing(spec, state, signed_voluntary_exit)
# Check exit epoch and withdrawable epoch
expected_exit_epoch = spec.compute_activation_exit_epoch(spec.get_current_epoch(state))
expected_exit_epoch = spec.compute_activation_exit_epoch(
spec.get_current_epoch(state)
)
expected_exit_epoch += to_exit // churn_limit
expected_withdrawable_epoch = expected_exit_epoch + spec.config.MIN_VALIDATOR_WITHDRAWABILITY_DELAY
expected_withdrawable_epoch = (
expected_exit_epoch + spec.config.MIN_VALIDATOR_WITHDRAWABILITY_DELAY
)
assert state.validators[validator_index].exit_epoch == expected_exit_epoch
assert state.validators[validator_index].withdrawable_epoch == expected_withdrawable_epoch
assert (
state.validators[validator_index].withdrawable_epoch
== expected_withdrawable_epoch
)
# Check exit_balance_to_consume
remainder = to_exit % churn_limit
assert state.exit_balance_to_consume == churn_limit - remainder
@ -143,7 +202,10 @@ def test_max_balance_exit(spec, state):
@with_eip7251_and_later
@spec_state_test
@with_presets([MAINNET], "With CHURN_LIMIT_QUOTIENT=32, can't change validator balance without changing churn_limit")
@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):
state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
current_epoch = spec.get_current_epoch(state)
@ -154,14 +216,25 @@ def test_exit_with_balance_equal_to_churn_limit(spec, state):
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)
spec,
state,
spec.VoluntaryExit(epoch=current_epoch, validator_index=validator_index),
privkey,
)
yield from run_voluntary_exit_processing(spec, state, signed_voluntary_exit)
# Validator consumes churn limit fully in the current epoch
expected_exit_epoch = spec.compute_activation_exit_epoch(spec.get_current_epoch(state))
expected_withdrawable_epoch = expected_exit_epoch + spec.config.MIN_VALIDATOR_WITHDRAWABILITY_DELAY
expected_exit_epoch = spec.compute_activation_exit_epoch(
spec.get_current_epoch(state)
)
expected_withdrawable_epoch = (
expected_exit_epoch + spec.config.MIN_VALIDATOR_WITHDRAWABILITY_DELAY
)
assert state.validators[validator_index].exit_epoch == expected_exit_epoch
assert state.validators[validator_index].withdrawable_epoch == expected_withdrawable_epoch
assert (
state.validators[validator_index].withdrawable_epoch
== expected_withdrawable_epoch
)
# Check exit_balance_to_consume
assert state.exit_balance_to_consume == 0
# Check earliest_exit_epoch
@ -170,7 +243,10 @@ def test_exit_with_balance_equal_to_churn_limit(spec, state):
@with_eip7251_and_later
@spec_state_test
@with_presets([MAINNET], "With CHURN_LIMIT_QUOTIENT=32, can't change validator balance without changing churn_limit")
@with_presets(
[MAINNET],
"With CHURN_LIMIT_QUOTIENT=32, can't change validator balance without changing churn_limit",
)
def test_exit_with_balance_multiple_of_churn_limit(spec, state):
state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
current_epoch = spec.get_current_epoch(state)
@ -182,30 +258,44 @@ def test_exit_with_balance_multiple_of_churn_limit(spec, state):
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)
spec,
state,
spec.VoluntaryExit(epoch=current_epoch, validator_index=validator_index),
privkey,
)
yield from run_voluntary_exit_processing(spec, state, signed_voluntary_exit)
# Validator consumes churn limit fully in the next 3 epochs (current included)
expected_exit_epoch = spec.compute_activation_exit_epoch(spec.get_current_epoch(state)) + epochs_to_consume
expected_withdrawable_epoch = expected_exit_epoch + spec.config.MIN_VALIDATOR_WITHDRAWABILITY_DELAY
expected_exit_epoch = (
spec.compute_activation_exit_epoch(spec.get_current_epoch(state))
+ epochs_to_consume
)
expected_withdrawable_epoch = (
expected_exit_epoch + spec.config.MIN_VALIDATOR_WITHDRAWABILITY_DELAY
)
assert state.validators[validator_index].exit_epoch == expected_exit_epoch
assert state.validators[validator_index].withdrawable_epoch == expected_withdrawable_epoch
assert (
state.validators[validator_index].withdrawable_epoch
== expected_withdrawable_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")
@with_presets(
[MAINNET],
"With CHURN_LIMIT_QUOTIENT=32, can't change validator balance without changing churn_limit",
)
def test_exit_existing_churn_and_churn_limit_balance(spec, state):
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)
validator_index = spec.get_active_validator_indices(state, current_epoch)[0]
# set exit epoch to the first available one and set exit balance to consume to full churn limit
earliest_exit_epoch = spec.compute_activation_exit_epoch(current_epoch)
state.earliest_exit_epoch = earliest_exit_epoch
@ -218,24 +308,35 @@ def test_exit_existing_churn_and_churn_limit_balance(spec, state):
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)
spec,
state,
spec.VoluntaryExit(epoch=current_epoch, validator_index=validator_index),
privkey,
)
yield from run_voluntary_exit_processing(spec, state, signed_voluntary_exit)
expected_exit_epoch = earliest_exit_epoch + 1
expected_withdrawable_epoch = expected_exit_epoch + spec.config.MIN_VALIDATOR_WITHDRAWABILITY_DELAY
expected_exit_epoch = earliest_exit_epoch + 1
expected_withdrawable_epoch = (
expected_exit_epoch + spec.config.MIN_VALIDATOR_WITHDRAWABILITY_DELAY
)
# Check exit epoch
assert state.validators[validator_index].exit_epoch == expected_exit_epoch
assert state.validators[validator_index].withdrawable_epoch == expected_withdrawable_epoch
assert (
state.validators[validator_index].withdrawable_epoch
== expected_withdrawable_epoch
)
# Check balance consumed in exit epoch is the remainder 1 ETH
assert state.exit_balance_to_consume == churn_limit - existing_churn
# 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")
@with_presets(
[MAINNET],
"With CHURN_LIMIT_QUOTIENT=32, can't change validator balance without changing churn_limit",
)
def test_exit_existing_churn_and_balance_multiple_of_churn_limit(spec, state):
state.slot += spec.config.SHARD_COMMITTEE_PERIOD * spec.SLOTS_PER_EPOCH
current_epoch = spec.get_current_epoch(state)
@ -252,32 +353,41 @@ def test_exit_existing_churn_and_balance_multiple_of_churn_limit(spec, state):
# Set validator effective balance to a multiple of churn_limit
epochs_to_consume = 3
state.validators[validator_index].effective_balance = epochs_to_consume * churn_limit
state.validators[validator_index].effective_balance = (
epochs_to_consume * churn_limit
)
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)
spec,
state,
spec.VoluntaryExit(epoch=current_epoch, validator_index=validator_index),
privkey,
)
yield from run_voluntary_exit_processing(spec, state, signed_voluntary_exit)
# Validator fully consumes epochs_to_consume and gets into the next one
expected_exit_epoch = earliest_exit_epoch + epochs_to_consume
expected_withdrawable_epoch = expected_exit_epoch + spec.config.MIN_VALIDATOR_WITHDRAWABILITY_DELAY
expected_withdrawable_epoch = (
expected_exit_epoch + spec.config.MIN_VALIDATOR_WITHDRAWABILITY_DELAY
)
assert state.validators[validator_index].exit_epoch == expected_exit_epoch
assert state.validators[validator_index].withdrawable_epoch == expected_withdrawable_epoch
assert (
state.validators[validator_index].withdrawable_epoch
== expected_withdrawable_epoch
)
# Check exit_balance_to_consume
assert state.exit_balance_to_consume == churn_limit - existing_churn
# Check earliest_exit_epoch
assert state.earliest_exit_epoch == expected_exit_epoch
@with_eip7251_and_later
@spec_state_test
def test_invalid_validator_has_pending_withdrawal(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]
@ -288,10 +398,14 @@ def test_invalid_validator_has_pending_withdrawal(spec, state):
)
signed_voluntary_exit = sign_voluntary_exit(spec, state, voluntary_exit, privkey)
state.pending_partial_withdrawals.append(spec.PendingPartialWithdrawal(
index=validator_index,
amount=1,
withdrawable_epoch=spec.compute_activation_exit_epoch(current_epoch),
))
state.pending_partial_withdrawals.append(
spec.PendingPartialWithdrawal(
index=validator_index,
amount=1,
withdrawable_epoch=spec.compute_activation_exit_epoch(current_epoch),
)
)
yield from run_voluntary_exit_processing(spec, state, signed_voluntary_exit, valid=False)
yield from run_voluntary_exit_processing(
spec, state, signed_voluntary_exit, valid=False
)

View File

@ -1,9 +1,12 @@
from eth2spec.test.context import spec_state_test, with_eip7251_and_later
from eth2spec.test.phase0.epoch_processing.test_process_effective_balance_updates import run_test_effective_balance_hysteresis
from eth2spec.test.phase0.epoch_processing.test_process_effective_balance_updates import (
run_test_effective_balance_hysteresis,
)
@with_eip7251_and_later
@spec_state_test
def test_effective_balance_hysteresis_with_compounding_credentials(spec, state):
run_test_effective_balance_hysteresis(spec, state, with_compounding_credentials=True)
run_test_effective_balance_hysteresis(
spec, state, with_compounding_credentials=True
)

View File

@ -10,9 +10,13 @@ from eth2spec.test.context import (
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))
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')
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
@ -24,9 +28,13 @@ def test_pending_deposit_min_activation_balance(spec, state):
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))
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')
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 == []
@ -37,26 +45,38 @@ def test_pending_deposit_balance_equal_churn(spec, state):
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))
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')
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)
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)]
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
amount = 10**9 + 1
state.deposit_balance_to_consume = 2 * amount
state.pending_balance_deposits.append(spec.PendingBalanceDeposit(index=index, amount=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')
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
@ -69,10 +89,16 @@ def test_pending_deposit_preexisting_churn(spec, state):
@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))
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')
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
@ -86,14 +112,23 @@ 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))
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')
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
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)]
assert state.pending_balance_deposits == [
spec.PendingBalanceDeposit(index=2, amount=amount)
]

View File

@ -16,21 +16,29 @@ def test_basic_pending_consolidation(spec, 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))
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 = current_epoch
# Set the target withdrawal credential to eth1
eth1_withdrawal_credential = spec.ETH1_ADDRESS_WITHDRAWAL_PREFIX + b'\x00' * 11 + b'\x11' * 20
eth1_withdrawal_credential = (
spec.ETH1_ADDRESS_WITHDRAWAL_PREFIX + b"\x00" * 11 + b"\x11" * 20
)
state.validators[target_index].withdrawal_credentials = eth1_withdrawal_credential
yield from run_epoch_processing_with(spec, state, "process_pending_consolidations")
# Pending consolidation was successfully processed
assert state.validators[target_index].withdrawal_credentials[:1] == spec.COMPOUNDING_WITHDRAWAL_PREFIX
assert (
state.validators[target_index].withdrawal_credentials[:1]
== spec.COMPOUNDING_WITHDRAWAL_PREFIX
)
assert state.balances[target_index] == 2 * spec.MIN_ACTIVATION_BALANCE
assert state.balances[source_index] == 0
assert state.pending_consolidations == []
@with_eip7251_and_later
@spec_state_test
def test_consolidation_not_yet_withdrawable_validator(spec, state):
@ -38,16 +46,22 @@ def test_consolidation_not_yet_withdrawable_validator(spec, 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))
state.pending_consolidations.append(
spec.PendingConsolidation(source_index=source_index, target_index=target_index)
)
# Set the target to eth1 withdrawal credentials
eth1_withdrawal_credential = spec.ETH1_ADDRESS_WITHDRAWAL_PREFIX + b'\x00' * 11 + b'\x11' * 20
eth1_withdrawal_credential = (
spec.ETH1_ADDRESS_WITHDRAWAL_PREFIX + b"\x00" * 11 + b"\x11" * 20
)
state.validators[target_index].withdrawal_credentials = eth1_withdrawal_credential
# Initiate exit of source validator
spec.initiate_validator_exit(state, source_index)
pre_pending_consolidations = state.pending_consolidations.copy()
pre_balances = state.balances.copy()
pre_target_withdrawal_credential = state.validators[target_index].withdrawal_credentials[:1]
pre_target_withdrawal_credential = state.validators[
target_index
].withdrawal_credentials[:1]
yield from run_epoch_processing_with(spec, state, "process_pending_consolidations")
@ -56,12 +70,14 @@ def test_consolidation_not_yet_withdrawable_validator(spec, state):
assert state.balances[source_index] == pre_balances[0]
assert state.balances[target_index] == pre_balances[1]
# Target withdrawal credential is unchanged
assert state.validators[target_index].withdrawal_credentials[:1] == pre_target_withdrawal_credential
assert (
state.validators[target_index].withdrawal_credentials[:1]
== pre_target_withdrawal_credential
)
# Pending consolidation is still in the queue
assert state.pending_consolidations == pre_pending_consolidations
@with_eip7251_and_later
@spec_state_test
def test_skip_consolidation_when_source_slashed(spec, state):
@ -72,13 +88,19 @@ def test_skip_consolidation_when_source_slashed(spec, state):
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)
spec.PendingConsolidation(
source_index=source0_index, target_index=target0_index
)
)
state.pending_consolidations.append(
spec.PendingConsolidation(source_index=source1_index, target_index=target1_index)
spec.PendingConsolidation(
source_index=source1_index, target_index=target1_index
)
)
eth1_withdrawal_credential = spec.ETH1_ADDRESS_WITHDRAWAL_PREFIX + b'\x00' * 11 + b'\x11' * 20
eth1_withdrawal_credential = (
spec.ETH1_ADDRESS_WITHDRAWAL_PREFIX + b"\x00" * 11 + b"\x11" * 20
)
state.validators[target0_index].withdrawal_credentials = eth1_withdrawal_credential
state.validators[target1_index].withdrawal_credentials = eth1_withdrawal_credential
@ -93,47 +115,73 @@ def test_skip_consolidation_when_source_slashed(spec, state):
# 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
assert state.validators[target0_index].withdrawal_credentials[:1] == spec.ETH1_ADDRESS_WITHDRAWAL_PREFIX
assert (
state.validators[target0_index].withdrawal_credentials[:1]
== spec.ETH1_ADDRESS_WITHDRAWAL_PREFIX
)
# 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
assert state.validators[target1_index].withdrawal_credentials[:1] == spec.COMPOUNDING_WITHDRAWAL_PREFIX
assert (
state.validators[target1_index].withdrawal_credentials[:1]
== spec.COMPOUNDING_WITHDRAWAL_PREFIX
)
@with_eip7251_and_later
@spec_state_test
def test_all_consolidation_cases_together(spec, state):
current_epoch = spec.get_current_epoch(state)
source_index = [spec.get_active_validator_indices(state, current_epoch)[i] for i in range(4)]
target_index = [spec.get_active_validator_indices(state, current_epoch)[4+i] for i in range(4)]
state.pending_consolidations = [spec.PendingConsolidation(source_index=source_index[i],
target_index=target_index[i]) for i in range(4)]
source_index = [
spec.get_active_validator_indices(state, current_epoch)[i] for i in range(4)
]
target_index = [
spec.get_active_validator_indices(state, current_epoch)[4 + i] for i in range(4)
]
state.pending_consolidations = [
spec.PendingConsolidation(
source_index=source_index[i], target_index=target_index[i]
)
for i in range(4)
]
# Set withdrawable epoch to current epoch for first and last source validators
for i in [0,2]:
for i in [0, 2]:
state.validators[source_index[i]].withdrawable_epoch = current_epoch
# Set second source validator as slashed
state.validators[source_index[1]].slashed = True
# Set targets withdrawal credentials to eth1
eth1_withdrawal_credential = spec.ETH1_ADDRESS_WITHDRAWAL_PREFIX + b'\x00' * 11 + b'\x11' * 20
eth1_withdrawal_credential = (
spec.ETH1_ADDRESS_WITHDRAWAL_PREFIX + b"\x00" * 11 + b"\x11" * 20
)
for i in range(4):
state.validators[target_index[i]].withdrawal_credentials = eth1_withdrawal_credential
state.validators[target_index[i]].withdrawal_credentials = (
eth1_withdrawal_credential
)
# Initiate exit of third source validator
spec.initiate_validator_exit(state, 2)
pre_balances = state.balances.copy()
pre_target_withdrawal_prefixes = [state.validators[target_index[i]].withdrawal_credentials[:1] for i in [0,1,2,3]]
pre_target_withdrawal_prefixes = [
state.validators[target_index[i]].withdrawal_credentials[:1]
for i in [0, 1, 2, 3]
]
pre_pending_consolidations = state.pending_consolidations.copy()
yield from run_epoch_processing_with(spec, state, "process_pending_consolidations")
# First consolidation is successfully processed
assert state.validators[target_index[0]].withdrawal_credentials[:1] == spec.COMPOUNDING_WITHDRAWAL_PREFIX
assert (
state.validators[target_index[0]].withdrawal_credentials[:1]
== spec.COMPOUNDING_WITHDRAWAL_PREFIX
)
assert state.balances[target_index[0]] == 2 * spec.MIN_ACTIVATION_BALANCE
assert state.balances[source_index[0]] == 0
# All other consolidations are not processed
for i in [1,2,3]:
assert state.validators[target_index[i]].withdrawal_credentials[:1] == pre_target_withdrawal_prefixes[i]
for i in [1, 2, 3]:
assert (
state.validators[target_index[i]].withdrawal_credentials[:1]
== pre_target_withdrawal_prefixes[i]
)
assert state.balances[source_index[i]] == pre_balances[source_index[i]]
assert state.balances[target_index[i]] == pre_balances[target_index[i]]
# First consolidation is processed, second is skipped, last two are left in the queue
state.pending_consolidations = pre_pending_consolidations[2:]
state.pending_consolidations = pre_pending_consolidations[2:]

View File

@ -5,13 +5,13 @@ from eth2spec.test.helpers.withdrawals import (
)
from eth2spec.test.helpers.forks import is_post_eip7251
@with_all_phases
@spec_state_test
def test_effective_balance_hysteresis(spec, state):
run_test_effective_balance_hysteresis(spec, state)
def run_test_effective_balance_hysteresis(spec, state, with_compounding_credentials=False):
assert is_post_eip7251(spec) or not with_compounding_credentials
# Prepare state up to the final-updates.