utilize new randomize functions in process_inactivity_updates testing

This commit is contained in:
Danny Ryan 2021-05-13 13:44:41 -06:00
parent d38af7a158
commit 049210bd8a
No known key found for this signature in database
GPG Key ID: 2765A792E42CE07A
1 changed files with 8 additions and 32 deletions

View File

@ -8,6 +8,9 @@ from eth2spec.test.helpers.state import (
from eth2spec.test.helpers.epoch_processing import (
run_epoch_processing_with
)
from eth2spec.test.helpers.random import (
randomize_attestation_participation,
)
def set_full_participation(spec, state):
@ -16,37 +19,10 @@ def set_full_participation(spec, state):
full_flags = spec.add_flag(full_flags, flag_index)
for index in range(len(state.validators)):
state.current_epoch_participation[index] = full_flags
state.previous_epoch_participation[index] = full_flags
def randomize_flags(spec, state, rng=Random(2080)):
for index in range(len(state.validators)):
# ~1/3 have bad head or bad target or not timely enough
is_timely_correct_head = rng.randint(0, 2) != 0
flags = state.previous_epoch_participation[index]
def set_flag(index, value):
nonlocal flags
flag = spec.ParticipationFlags(2**index)
if value:
flags |= flag
else:
flags &= 0xff ^ flag
set_flag(spec.TIMELY_HEAD_FLAG_INDEX, is_timely_correct_head)
if is_timely_correct_head:
# If timely head, then must be timely target
set_flag(spec.TIMELY_TARGET_FLAG_INDEX, True)
# If timely head, then must be timely source
set_flag(spec.TIMELY_SOURCE_FLAG_INDEX, True)
else:
# ~50% of remaining have bad target or not timely enough
set_flag(spec.TIMELY_TARGET_FLAG_INDEX, rng.choice([True, False]))
# ~50% of remaining have bad source or not timely enough
set_flag(spec.TIMELY_SOURCE_FLAG_INDEX, rng.choice([True, False]))
state.previous_epoch_participation[index] = flags
def run_process_inactivity_updates(spec, state):
yield from run_epoch_processing_with(spec, state, 'process_inactivity_updates')
@ -75,7 +51,7 @@ def test_all_zero_inactivity_scores_empty_participation(spec, state):
def test_all_zero_inactivity_scores_random_participation(spec, state):
next_epoch_via_block(spec, state)
state.inactivity_scores = [0] * len(state.validators)
randomize_flags(spec, state)
randomize_attestation_participation(spec, state, rng=Random(5555))
yield from run_process_inactivity_updates(spec, state)
@ -83,8 +59,8 @@ def test_all_zero_inactivity_scores_random_participation(spec, state):
@spec_state_test
def test_all_zero_inactivity_scores_full_participation(spec, state):
next_epoch_via_block(spec, state)
state.inactivity_scores = [0] * len(state.validators)
set_full_participation(spec, state)
state.inactivity_scores = [0] * len(state.validators)
yield from run_process_inactivity_updates(spec, state)
@ -100,8 +76,8 @@ def test_random_inactivity_scores_empty_participation(spec, state):
@spec_state_test
def test_random_inactivity_scores_random_participation(spec, state):
next_epoch_via_block(spec, state)
randomize_attestation_participation(spec, state, rng=Random(22222))
randomize_inactivity_scores(spec, state, rng=Random(22222))
randomize_flags(spec, state)
yield from run_process_inactivity_updates(spec, state)
@ -109,6 +85,6 @@ def test_random_inactivity_scores_random_participation(spec, state):
@spec_state_test
def test_random_inactivity_scores_full_participation(spec, state):
next_epoch_via_block(spec, state)
randomize_inactivity_scores(spec, state, rng=Random(33333))
set_full_participation(spec, state)
randomize_inactivity_scores(spec, state, rng=Random(33333))
yield from run_process_inactivity_updates(spec, state)