Skip to content

Commit 163f287

Browse files
committed
Fix prior tests with new EIP-7251 logic
1 parent 112924c commit 163f287

File tree

10 files changed

+141
-46
lines changed

10 files changed

+141
-46
lines changed

tests/core/pyspec/eth2spec/test/capella/block_processing/test_process_withdrawals.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
next_slot,
2020
)
2121
from eth2spec.test.helpers.withdrawals import (
22+
get_expected_withdrawals,
2223
prepare_expected_withdrawals,
2324
set_eth1_withdrawal_credential_with_balance,
2425
set_validator_fully_withdrawable,
@@ -62,7 +63,7 @@ def run_withdrawals_processing(spec, state, execution_payload, num_expected_with
6263
- post-state ('post').
6364
If ``valid == False``, run expecting ``AssertionError``
6465
"""
65-
expected_withdrawals = spec.get_expected_withdrawals(state)
66+
expected_withdrawals = get_expected_withdrawals(spec, state)
6667
assert len(expected_withdrawals) <= spec.MAX_WITHDRAWALS_PER_PAYLOAD
6768
if num_expected_withdrawals is not None:
6869
assert len(expected_withdrawals) == num_expected_withdrawals
@@ -87,7 +88,7 @@ def run_withdrawals_processing(spec, state, execution_payload, num_expected_with
8788
assert state.next_withdrawal_validator_index == next_withdrawal_validator_index % len(state.validators)
8889
elif len(expected_withdrawals) <= spec.MAX_WITHDRAWALS_PER_PAYLOAD:
8990
bound = min(spec.MAX_VALIDATORS_PER_WITHDRAWALS_SWEEP, spec.MAX_WITHDRAWALS_PER_PAYLOAD)
90-
assert len(spec.get_expected_withdrawals(state)) <= bound
91+
assert len(get_expected_withdrawals(spec, state)) <= bound
9192
elif len(expected_withdrawals) > spec.MAX_WITHDRAWALS_PER_PAYLOAD:
9293
raise ValueError('len(expected_withdrawals) should not be greater than MAX_WITHDRAWALS_PER_PAYLOAD')
9394

@@ -100,7 +101,7 @@ def run_withdrawals_processing(spec, state, execution_payload, num_expected_with
100101
@with_capella_and_later
101102
@spec_state_test
102103
def test_success_zero_expected_withdrawals(spec, state):
103-
assert len(spec.get_expected_withdrawals(state)) == 0
104+
assert len(get_expected_withdrawals(spec, state)) == 0
104105

105106
next_slot(spec, state)
106107
execution_payload = build_empty_execution_payload(spec, state)

tests/core/pyspec/eth2spec/test/capella/sanity/test_blocks.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from eth2spec.test.helpers.constants import MINIMAL
2+
from eth2spec.test.helpers.forks import is_post_eip7251
23
from eth2spec.test.context import (
34
with_capella_and_later,
45
spec_state_test,
@@ -20,6 +21,7 @@
2021
next_epoch_with_attestations,
2122
)
2223
from eth2spec.test.helpers.withdrawals import (
24+
get_expected_withdrawals,
2325
set_eth1_withdrawal_credential_with_balance,
2426
set_validator_fully_withdrawable,
2527
set_validator_partially_withdrawable,
@@ -202,7 +204,7 @@ def test_full_withdrawal_in_epoch_transition(spec, state):
202204
index = 0
203205
current_epoch = spec.get_current_epoch(state)
204206
set_validator_fully_withdrawable(spec, state, index, current_epoch)
205-
assert len(spec.get_expected_withdrawals(state)) == 1
207+
assert len(get_expected_withdrawals(spec, state)) == 1
206208

207209
yield 'pre', state
208210

@@ -214,7 +216,7 @@ def test_full_withdrawal_in_epoch_transition(spec, state):
214216
yield 'post', state
215217

216218
assert state.balances[index] == 0
217-
assert len(spec.get_expected_withdrawals(state)) == 0
219+
assert len(get_expected_withdrawals(spec, state)) == 0
218220

219221

220222
@with_capella_and_later
@@ -224,7 +226,7 @@ def test_partial_withdrawal_in_epoch_transition(spec, state):
224226
set_validator_partially_withdrawable(spec, state, index, excess_balance=1000000000000)
225227
pre_balance = state.balances[index]
226228

227-
assert len(spec.get_expected_withdrawals(state)) == 1
229+
assert len(get_expected_withdrawals(spec, state)) == 1
228230

229231
yield 'pre', state
230232

@@ -238,7 +240,7 @@ def test_partial_withdrawal_in_epoch_transition(spec, state):
238240
assert state.balances[index] < pre_balance
239241
# Potentially less than due to sync committee penalty
240242
assert state.balances[index] <= spec.MAX_EFFECTIVE_BALANCE
241-
assert len(spec.get_expected_withdrawals(state)) == 0
243+
assert len(get_expected_withdrawals(spec, state)) == 0
242244

243245

244246
@with_capella_and_later
@@ -250,7 +252,7 @@ def test_many_partial_withdrawals_in_epoch_transition(spec, state):
250252
index = (i + state.next_withdrawal_index) % len(state.validators)
251253
set_validator_partially_withdrawable(spec, state, index, excess_balance=1000000000000)
252254

253-
assert len(spec.get_expected_withdrawals(state)) == spec.MAX_WITHDRAWALS_PER_PAYLOAD
255+
assert (len(get_expected_withdrawals(spec, state)) == spec.MAX_WITHDRAWALS_PER_PAYLOAD)
254256

255257
yield 'pre', state
256258

@@ -261,7 +263,7 @@ def test_many_partial_withdrawals_in_epoch_transition(spec, state):
261263
yield 'blocks', [signed_block]
262264
yield 'post', state
263265

264-
assert len(spec.get_expected_withdrawals(state)) == 1
266+
assert len(get_expected_withdrawals(spec, state)) == 1
265267

266268

267269
def _perform_valid_withdrawal(spec, state):
@@ -272,7 +274,7 @@ def _perform_valid_withdrawal(spec, state):
272274
next_slot(spec, state)
273275
pre_next_withdrawal_index = state.next_withdrawal_index
274276

275-
expected_withdrawals = spec.get_expected_withdrawals(state)
277+
expected_withdrawals = get_expected_withdrawals(spec, state)
276278

277279
pre_state = state.copy()
278280

@@ -357,7 +359,11 @@ def test_top_up_and_partial_withdrawable_validator(spec, state):
357359

358360
signed_block = state_transition_and_sign_block(spec, state, block)
359361

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

363369
# Since withdrawals happen before deposits, it becomes partially withdrawable after state transition.
@@ -395,9 +401,13 @@ def test_top_up_to_fully_withdrawn_validator(spec, state):
395401

396402
signed_block_1 = state_transition_and_sign_block(spec, state, block)
397403

404+
balance = state.balances[validator_index]
405+
if is_post_eip7251(spec):
406+
balance += state.pending_balance_deposits[0].amount
407+
398408
assert spec.is_fully_withdrawable_validator(
399409
state.validators[validator_index],
400-
state.balances[validator_index],
410+
balance,
401411
spec.get_current_epoch(state)
402412
)
403413

tests/core/pyspec/eth2spec/test/deneb/epoch_processing/test_process_registry_updates.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from eth2spec.test.helpers.keys import pubkeys
2+
from eth2spec.test.helpers.forks import is_post_eip7251
23
from eth2spec.test.helpers.constants import MINIMAL
34
from eth2spec.test.context import (
45
with_deneb_and_later,
@@ -47,7 +48,9 @@ def run_test_activation_churn_limit(spec, state):
4748
# Half should churn in first run of registry update
4849
for i in range(mock_activations):
4950
index = validator_count_0 + i
50-
if index < validator_count_0 + churn_limit_0:
51+
# NOTE: activations are gated different after EIP-7251
52+
# all eligible validators have been activated
53+
if index < validator_count_0 + churn_limit_0 or is_post_eip7251(spec):
5154
# The eligible validators within the activation churn limit should have been activated
5255
assert state.validators[index].activation_epoch < spec.FAR_FUTURE_EPOCH
5356
else:

tests/core/pyspec/eth2spec/test/helpers/deposits.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from random import Random
22

33
from eth2spec.test.context import expect_assertion_error
4-
from eth2spec.test.helpers.forks import is_post_altair
4+
from eth2spec.test.helpers.forks import is_post_altair, is_post_eip7251
55
from eth2spec.test.helpers.keys import pubkeys, privkeys
66
from eth2spec.test.helpers.state import get_balance
77
from eth2spec.utils import bls
@@ -241,6 +241,9 @@ def run_deposit_processing(spec, state, deposit, validator_index, valid=True, ef
241241
pre_balance = get_balance(state, validator_index)
242242
pre_effective_balance = state.validators[validator_index].effective_balance
243243

244+
if is_post_eip7251(spec):
245+
assert len(state.pending_balance_deposits) == 0
246+
244247
yield 'pre', state
245248
yield 'deposit', deposit
246249

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

254257
yield 'post', state
255258

259+
if is_post_eip7251(spec):
260+
# balance additions are now routed through "pending balance deposits"
261+
# process them here along with any effective balance updates
262+
updates_count = len(state.pending_balance_deposits)
263+
if updates_count != 0:
264+
assert updates_count == 1
265+
pending_balance_deposit = state.pending_balance_deposits[0]
266+
assert pending_balance_deposit.index == validator_index
267+
assert pending_balance_deposit.amount == deposit.data.amount
268+
spec.increase_balance(state, validator_index, deposit.data.amount)
269+
if not is_top_up:
270+
# run effective balance update for new validators
271+
spec.process_effective_balance_updates(state)
272+
256273
if not effective or not bls.KeyValidate(deposit.data.pubkey):
257274
assert len(state.validators) == pre_validator_count
258275
assert len(state.balances) == pre_validator_count

tests/core/pyspec/eth2spec/test/helpers/execution_payload.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from rlp.sedes import big_endian_int, Binary, List
55

66
from eth2spec.debug.random_value import get_random_bytes_list
7+
from eth2spec.test.helpers.withdrawals import get_expected_withdrawals
78
from eth2spec.test.helpers.forks import (
89
is_post_capella,
910
is_post_deneb,
@@ -227,7 +228,7 @@ def build_empty_execution_payload(spec, state, randao_mix=None):
227228
transactions=empty_txs,
228229
)
229230
if is_post_capella(spec):
230-
payload.withdrawals = spec.get_expected_withdrawals(state)
231+
payload.withdrawals = get_expected_withdrawals(spec, state)
231232
if is_post_deneb(spec):
232233
payload.blob_gas_used = 0
233234
payload.excess_blob_gas = 0

tests/core/pyspec/eth2spec/test/helpers/proposer_slashings.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from eth2spec.test.helpers.block_header import sign_block_header
2-
from eth2spec.test.helpers.forks import is_post_altair, is_post_bellatrix
2+
from eth2spec.test.helpers.forks import is_post_altair, is_post_bellatrix, is_post_eip7251
33
from eth2spec.test.helpers.keys import pubkey_to_privkey
44
from eth2spec.test.helpers.state import get_balance
55
from eth2spec.test.helpers.sync_committee import (
@@ -9,14 +9,23 @@
99

1010

1111
def get_min_slashing_penalty_quotient(spec):
12-
if is_post_bellatrix(spec):
12+
if is_post_eip7251(spec):
13+
return spec.MIN_SLASHING_PENALTY_QUOTIENT_EIP7251
14+
elif is_post_bellatrix(spec):
1315
return spec.MIN_SLASHING_PENALTY_QUOTIENT_BELLATRIX
1416
elif is_post_altair(spec):
1517
return spec.MIN_SLASHING_PENALTY_QUOTIENT_ALTAIR
1618
else:
1719
return spec.MIN_SLASHING_PENALTY_QUOTIENT
1820

1921

22+
def get_whistleblower_reward_quotient(spec):
23+
if is_post_eip7251(spec):
24+
return spec.WHISTLEBLOWER_REWARD_QUOTIENT_EIP7251
25+
else:
26+
return spec.WHISTLEBLOWER_REWARD_QUOTIENT
27+
28+
2029
def check_proposer_slashing_effect(spec, pre_state, state, slashed_index, block=None):
2130
slashed_validator = state.validators[slashed_index]
2231
assert slashed_validator.slashed
@@ -25,7 +34,7 @@ def check_proposer_slashing_effect(spec, pre_state, state, slashed_index, block=
2534

2635
proposer_index = spec.get_beacon_proposer_index(state)
2736
slash_penalty = state.validators[slashed_index].effective_balance // get_min_slashing_penalty_quotient(spec)
28-
whistleblower_reward = state.validators[slashed_index].effective_balance // spec.WHISTLEBLOWER_REWARD_QUOTIENT
37+
whistleblower_reward = state.validators[slashed_index].effective_balance // get_whistleblower_reward_quotient(spec)
2938

3039
# Altair introduces sync committee (SC) reward and penalty
3140
sc_reward_for_slashed = sc_penalty_for_slashed = sc_reward_for_proposer = sc_penalty_for_proposer = 0

tests/core/pyspec/eth2spec/test/helpers/withdrawals.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
import random
2+
from eth2spec.test.helpers.forks import is_post_eip7251
3+
4+
5+
def get_expected_withdrawals(spec, state):
6+
if is_post_eip7251(spec):
7+
withdrawals, _ = spec.get_expected_withdrawals(state)
8+
return withdrawals
9+
else:
10+
return spec.get_expected_withdrawals(state)
211

312

413
def set_validator_fully_withdrawable(spec, state, index, withdrawable_epoch=None):

tests/core/pyspec/eth2spec/test/phase0/block_processing/test_process_attester_slashing.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
get_valid_attester_slashing, get_valid_attester_slashing_by_indices,
1111
get_indexed_attestation_participants, get_attestation_2_data, get_attestation_1_data,
1212
)
13-
from eth2spec.test.helpers.proposer_slashings import get_min_slashing_penalty_quotient
13+
from eth2spec.test.helpers.proposer_slashings import (
14+
get_min_slashing_penalty_quotient,
15+
get_whistleblower_reward_quotient,
16+
)
1417
from eth2spec.test.helpers.state import (
1518
get_balance,
1619
next_epoch_via_block,
@@ -49,7 +52,7 @@ def run_attester_slashing_processing(spec, state, attester_slashing, valid=True)
4952
}
5053

5154
total_proposer_rewards = sum(
52-
effective_balance // spec.WHISTLEBLOWER_REWARD_QUOTIENT
55+
effective_balance // get_whistleblower_reward_quotient(spec)
5356
for effective_balance in pre_slashing_effectives.values()
5457
)
5558

@@ -71,7 +74,9 @@ def run_attester_slashing_processing(spec, state, attester_slashing, valid=True)
7174
assert slashed_validator.withdrawable_epoch == expected_withdrawable_epoch
7275
else:
7376
assert slashed_validator.withdrawable_epoch < spec.FAR_FUTURE_EPOCH
74-
assert get_balance(state, slashed_index) < pre_slashing_balances[slashed_index]
77+
if slashed_index != proposer_index:
78+
# NOTE: check proposer balances below
79+
assert get_balance(state, slashed_index) < pre_slashing_balances[slashed_index]
7580

7681
if proposer_index not in slashed_indices:
7782
# gained whistleblower reward

0 commit comments

Comments
 (0)