Skip to content

Commit

Permalink
Fix prior tests with new EIP-7251 logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ralexstokes committed Apr 8, 2024
1 parent 6d140dd commit 5d6ffbd
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
next_slot,
)
from eth2spec.test.helpers.withdrawals import (
get_expected_withdrawals,
prepare_expected_withdrawals,
set_eth1_withdrawal_credential_with_balance,
set_validator_fully_withdrawable,
Expand Down Expand Up @@ -62,7 +63,7 @@ def run_withdrawals_processing(spec, state, execution_payload, num_expected_with
- post-state ('post').
If ``valid == False``, run expecting ``AssertionError``
"""
expected_withdrawals = spec.get_expected_withdrawals(state)
expected_withdrawals = get_expected_withdrawals(spec, state)
assert len(expected_withdrawals) <= spec.MAX_WITHDRAWALS_PER_PAYLOAD
if num_expected_withdrawals is not None:
assert len(expected_withdrawals) == num_expected_withdrawals
Expand All @@ -87,7 +88,7 @@ def run_withdrawals_processing(spec, state, execution_payload, num_expected_with
assert state.next_withdrawal_validator_index == next_withdrawal_validator_index % len(state.validators)
elif len(expected_withdrawals) <= spec.MAX_WITHDRAWALS_PER_PAYLOAD:
bound = min(spec.MAX_VALIDATORS_PER_WITHDRAWALS_SWEEP, spec.MAX_WITHDRAWALS_PER_PAYLOAD)
assert len(spec.get_expected_withdrawals(state)) <= bound
assert len(get_expected_withdrawals(spec, state)) <= bound
elif len(expected_withdrawals) > spec.MAX_WITHDRAWALS_PER_PAYLOAD:
raise ValueError('len(expected_withdrawals) should not be greater than MAX_WITHDRAWALS_PER_PAYLOAD')

Expand All @@ -100,7 +101,7 @@ def run_withdrawals_processing(spec, state, execution_payload, num_expected_with
@with_capella_and_later
@spec_state_test
def test_success_zero_expected_withdrawals(spec, state):
assert len(spec.get_expected_withdrawals(state)) == 0
assert len(get_expected_withdrawals(spec, state)) == 0

next_slot(spec, state)
execution_payload = build_empty_execution_payload(spec, state)
Expand Down
21 changes: 13 additions & 8 deletions tests/core/pyspec/eth2spec/test/capella/sanity/test_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
next_epoch_with_attestations,
)
from eth2spec.test.helpers.withdrawals import (
get_expected_withdrawals,
set_eth1_withdrawal_credential_with_balance,
set_validator_fully_withdrawable,
set_validator_partially_withdrawable,
Expand Down Expand Up @@ -202,7 +203,7 @@ def test_full_withdrawal_in_epoch_transition(spec, state):
index = 0
current_epoch = spec.get_current_epoch(state)
set_validator_fully_withdrawable(spec, state, index, current_epoch)
assert len(spec.get_expected_withdrawals(state)) == 1
assert len(get_expected_withdrawals(spec, state)) == 1

yield 'pre', state

Expand All @@ -214,7 +215,7 @@ def test_full_withdrawal_in_epoch_transition(spec, state):
yield 'post', state

assert state.balances[index] == 0
assert len(spec.get_expected_withdrawals(state)) == 0
assert len(get_expected_withdrawals(spec, state)) == 0


@with_capella_and_later
Expand All @@ -224,7 +225,7 @@ def test_partial_withdrawal_in_epoch_transition(spec, state):
set_validator_partially_withdrawable(spec, state, index, excess_balance=1000000000000)
pre_balance = state.balances[index]

assert len(spec.get_expected_withdrawals(state)) == 1
assert len(get_expected_withdrawals(spec, state)) == 1

yield 'pre', state

Expand All @@ -238,7 +239,7 @@ def test_partial_withdrawal_in_epoch_transition(spec, state):
assert state.balances[index] < pre_balance
# Potentially less than due to sync committee penalty
assert state.balances[index] <= spec.MAX_EFFECTIVE_BALANCE
assert len(spec.get_expected_withdrawals(state)) == 0
assert len(get_expected_withdrawals(spec, state)) == 0


@with_capella_and_later
Expand All @@ -250,7 +251,7 @@ def test_many_partial_withdrawals_in_epoch_transition(spec, state):
index = (i + state.next_withdrawal_index) % len(state.validators)
set_validator_partially_withdrawable(spec, state, index, excess_balance=1000000000000)

assert len(spec.get_expected_withdrawals(state)) == spec.MAX_WITHDRAWALS_PER_PAYLOAD
assert (len(get_expected_withdrawals(spec, state)) == spec.MAX_WITHDRAWALS_PER_PAYLOAD)

yield 'pre', state

Expand All @@ -261,7 +262,7 @@ def test_many_partial_withdrawals_in_epoch_transition(spec, state):
yield 'blocks', [signed_block]
yield 'post', state

assert len(spec.get_expected_withdrawals(state)) == 1
assert len(get_expected_withdrawals(spec, state)) == 1


def _perform_valid_withdrawal(spec, state):
Expand All @@ -272,7 +273,7 @@ def _perform_valid_withdrawal(spec, state):
next_slot(spec, state)
pre_next_withdrawal_index = state.next_withdrawal_index

expected_withdrawals = spec.get_expected_withdrawals(state)
expected_withdrawals = get_expected_withdrawals(spec, state)

pre_state = state.copy()

Expand Down Expand Up @@ -357,7 +358,11 @@ def test_top_up_and_partial_withdrawable_validator(spec, state):

signed_block = state_transition_and_sign_block(spec, state, block)

yield 'blocks', [signed_block]
# ensure we go through an epoch transition, to account for post-EIP-7251 behavior
block_in_next_epoch = build_empty_block(spec, state, slot=state.slot + spec.SLOTS_PER_EPOCH)
signed_block_in_next_epoch = state_transition_and_sign_block(spec, state, block_in_next_epoch)

yield 'blocks', [signed_block, signed_block_in_next_epoch]
yield 'post', state

# Since withdrawals happen before deposits, it becomes partially withdrawable after state transition.
Expand Down
19 changes: 18 additions & 1 deletion tests/core/pyspec/eth2spec/test/helpers/deposits.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from random import Random

from eth2spec.test.context import expect_assertion_error
from eth2spec.test.helpers.forks import is_post_altair
from eth2spec.test.helpers.forks import is_post_altair, is_post_eip7251
from eth2spec.test.helpers.keys import pubkeys, privkeys
from eth2spec.test.helpers.state import get_balance
from eth2spec.utils import bls
Expand Down Expand Up @@ -241,6 +241,9 @@ def run_deposit_processing(spec, state, deposit, validator_index, valid=True, ef
pre_balance = get_balance(state, validator_index)
pre_effective_balance = state.validators[validator_index].effective_balance

if is_post_eip7251(spec):
assert len(state.pending_balance_deposits) == 0

yield 'pre', state
yield 'deposit', deposit

Expand All @@ -253,6 +256,20 @@ def run_deposit_processing(spec, state, deposit, validator_index, valid=True, ef

yield 'post', state

if is_post_eip7251(spec):
# balance additions are now routed through "pending balance deposits"
# process them here along with any effective balance updates
updates_count = len(state.pending_balance_deposits)
if updates_count != 0:
assert updates_count == 1
pending_balance_deposit = state.pending_balance_deposits[0]
assert pending_balance_deposit.index == validator_index
assert pending_balance_deposit.amount == deposit.data.amount
spec.increase_balance(state, validator_index, deposit.data.amount)
if not is_top_up:
# run effective balance update for new validators
spec.process_effective_balance_updates(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
Expand Down
3 changes: 2 additions & 1 deletion tests/core/pyspec/eth2spec/test/helpers/execution_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from rlp.sedes import big_endian_int, Binary, List

from eth2spec.debug.random_value import get_random_bytes_list
from eth2spec.test.helpers.withdrawals import get_expected_withdrawals
from eth2spec.test.helpers.forks import (
is_post_capella,
is_post_deneb,
Expand Down Expand Up @@ -227,7 +228,7 @@ def build_empty_execution_payload(spec, state, randao_mix=None):
transactions=empty_txs,
)
if is_post_capella(spec):
payload.withdrawals = spec.get_expected_withdrawals(state)
payload.withdrawals = get_expected_withdrawals(spec, state)
if is_post_deneb(spec):
payload.blob_gas_used = 0
payload.excess_blob_gas = 0
Expand Down
9 changes: 9 additions & 0 deletions tests/core/pyspec/eth2spec/test/helpers/withdrawals.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import random
from eth2spec.test.helpers.forks import is_post_eip7251


def get_expected_withdrawals(spec, state):
if is_post_eip7251(spec):
withdrawals, _ = spec.get_expected_withdrawals(state)
return withdrawals
else:
return spec.get_exepected_withdrawals(state)


def set_validator_fully_withdrawable(spec, state, index, withdrawable_epoch=None):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from eth2spec.test.helpers.deposits import mock_deposit
from eth2spec.test.helpers.state import next_epoch, next_slots
from eth2spec.test.helpers.forks import is_post_eip7251
from eth2spec.test.helpers.constants import MINIMAL
from eth2spec.test.context import (
spec_test, spec_state_test,
Expand Down Expand Up @@ -364,9 +365,13 @@ def test_invalid_large_withdrawable_epoch(spec, state):
assert spec.is_active_validator(state.validators[0], spec.get_current_epoch(state))
assert spec.is_active_validator(state.validators[1], spec.get_current_epoch(state))

state.validators[0].exit_epoch = spec.FAR_FUTURE_EPOCH - 1
exit_epoch = spec.FAR_FUTURE_EPOCH - 1
state.validators[0].exit_epoch = exit_epoch
state.validators[1].effective_balance = spec.config.EJECTION_BALANCE

if is_post_eip7251(spec):
state.earliest_exit_epoch = exit_epoch

try:
yield from run_process_registry_updates(spec, state)
except ValueError:
Expand Down

0 comments on commit 5d6ffbd

Please sign in to comment.