From 4c02dbadd00fb726672d4f346e723e0b4368ffc0 Mon Sep 17 00:00:00 2001 From: protolambda Date: Sat, 22 May 2021 18:43:29 +0200 Subject: [PATCH 1/5] enable inactivity updates test generator output, implement new participation flag updates testing --- ...test_process_participation_flag_updates.py | 110 ++++++++++++++++++ tests/generators/epoch_processing/main.py | 2 + 2 files changed, 112 insertions(+) create mode 100644 tests/core/pyspec/eth2spec/test/altair/epoch_processing/test_process_participation_flag_updates.py diff --git a/tests/core/pyspec/eth2spec/test/altair/epoch_processing/test_process_participation_flag_updates.py b/tests/core/pyspec/eth2spec/test/altair/epoch_processing/test_process_participation_flag_updates.py new file mode 100644 index 000000000..b98713bfe --- /dev/null +++ b/tests/core/pyspec/eth2spec/test/altair/epoch_processing/test_process_participation_flag_updates.py @@ -0,0 +1,110 @@ +from random import Random + +from eth2spec.test.helpers.constants import MINIMAL +from eth2spec.test.context import ( + with_altair_and_later, + with_custom_state, + spec_test, spec_state_test, + with_presets, + single_phase, +) +from eth2spec.test.helpers.state import next_epoch_via_block +from eth2spec.test.helpers.epoch_processing import run_epoch_processing_with + + +def run_process_participation_flag_updates(spec, state): + yield from run_epoch_processing_with(spec, state, 'process_participation_flag_updates') + + +@with_altair_and_later +@spec_state_test +def test_zeroed(spec, state): + next_epoch_via_block(spec, state) + state.current_epoch_participation = [0] * len(state.validators) + state.previous_epoch_participation = [0] * len(state.validators) + yield from run_process_participation_flag_updates(spec, state) + + +@with_altair_and_later +@spec_state_test +def test_filled(spec, state): + next_epoch_via_block(spec, state) + + full_flags = spec.ParticipationFlags(0) + for flag_index in range(len(spec.PARTICIPATION_FLAG_WEIGHTS)): + full_flags = spec.add_flag(full_flags, flag_index) + + state.previous_epoch_participation = [full_flags] * len(state.validators) + state.current_epoch_participation = [full_flags] * len(state.validators) + + yield from run_process_participation_flag_updates(spec, state) + + +def random_flags(spec, state, seed: int, previous=True, current=True): + rng = Random(seed) + count = len(state.validators) + max_flag_value_excl = 2**len(spec.PARTICIPATION_FLAG_WEIGHTS) + if previous: + state.previous_epoch_participation = [rng.randrange(0, max_flag_value_excl) for _ in range(count)] + if current: + state.current_epoch_participation = [rng.randrange(0, max_flag_value_excl) for _ in range(count)] + + +@with_altair_and_later +@spec_state_test +def test_random(spec, state): + next_epoch_via_block(spec, state) + random_flags(spec, state, 10) + yield from run_process_participation_flag_updates(spec, state) + + +@with_altair_and_later +@spec_state_test +def test_random_genesis(spec, state): + random_flags(spec, state, 11) + yield from run_process_participation_flag_updates(spec, state) + + +@with_altair_and_later +@spec_state_test +def test_zeroing(spec, state): + next_epoch_via_block(spec, state) + random_flags(spec, state, 12, current=False) + state.current_epoch_participation = [0] * len(state.validators) + yield from run_process_participation_flag_updates(spec, state) + + +@with_altair_and_later +@spec_state_test +def test_prev_zeroed(spec, state): + next_epoch_via_block(spec, state) + random_flags(spec, state, 13, previous=False) + state.previous_epoch_participation = [0] * len(state.validators) + yield from run_process_participation_flag_updates(spec, state) + + +def custom_validator_count(factor: float): + def initializer(spec): + num_validators = spec.SLOTS_PER_EPOCH * spec.MAX_COMMITTEES_PER_SLOT * spec.TARGET_COMMITTEE_SIZE + return [spec.MAX_EFFECTIVE_BALANCE] * int(float(int(num_validators)) * factor) + return initializer + + +@with_altair_and_later +@with_presets([MINIMAL], reason="mainnet config requires too many pre-generated public/private keys") +@spec_test +@with_custom_state(balances_fn=custom_validator_count(1.3), threshold_fn=lambda spec: spec.config.EJECTION_BALANCE) +@single_phase +def test_slightly_larger_random(spec, state): + random_flags(spec, state, 14) + yield from run_process_participation_flag_updates(spec, state) + + +@with_altair_and_later +@with_presets([MINIMAL], reason="mainnet config requires too many pre-generated public/private keys") +@spec_test +@with_custom_state(balances_fn=custom_validator_count(2.6), threshold_fn=lambda spec: spec.config.EJECTION_BALANCE) +@single_phase +def test_large_random(spec, state): + random_flags(spec, state, 15) + yield from run_process_participation_flag_updates(spec, state) diff --git a/tests/generators/epoch_processing/main.py b/tests/generators/epoch_processing/main.py index a3d0f82be..7203bac93 100644 --- a/tests/generators/epoch_processing/main.py +++ b/tests/generators/epoch_processing/main.py @@ -17,6 +17,8 @@ if __name__ == "__main__": ]} altair_mods = { **{key: 'eth2spec.test.altair.epoch_processing.test_process_' + key for key in [ + 'inactivity_updates', + 'participation_flag_updates', 'sync_committee_updates', ]}, **phase_0_mods, From ed912f599e9a4c87bac0c3fb4f58776f6bb827ee Mon Sep 17 00:00:00 2001 From: protolambda Date: Sat, 22 May 2021 18:52:29 +0200 Subject: [PATCH 2/5] add assertions on flag updates --- .../test_process_participation_flag_updates.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/core/pyspec/eth2spec/test/altair/epoch_processing/test_process_participation_flag_updates.py b/tests/core/pyspec/eth2spec/test/altair/epoch_processing/test_process_participation_flag_updates.py index b98713bfe..287525608 100644 --- a/tests/core/pyspec/eth2spec/test/altair/epoch_processing/test_process_participation_flag_updates.py +++ b/tests/core/pyspec/eth2spec/test/altair/epoch_processing/test_process_participation_flag_updates.py @@ -13,7 +13,10 @@ from eth2spec.test.helpers.epoch_processing import run_epoch_processing_with def run_process_participation_flag_updates(spec, state): + old = state.current_epoch_participation yield from run_epoch_processing_with(spec, state, 'process_participation_flag_updates') + assert state.current_epoch_participation == [0] * len(state.validators) + assert state.previous_epoch_participation == old @with_altair_and_later From 139117d64784b5ff0d5242436917a548999fabd4 Mon Sep 17 00:00:00 2001 From: protolambda Date: Sat, 22 May 2021 19:00:55 +0200 Subject: [PATCH 3/5] update epoch processing test vector format docs --- tests/formats/epoch_processing/README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/formats/epoch_processing/README.md b/tests/formats/epoch_processing/README.md index d9abcaf98..33ec79290 100644 --- a/tests/formats/epoch_processing/README.md +++ b/tests/formats/epoch_processing/README.md @@ -33,7 +33,7 @@ The provided pre-state is already transitioned to just before the specific sub-t Sub-transitions: - `justification_and_finalization` -- `inactivity_penalty_updates` +- `inactivity_penalty_updates` (Altair) - `rewards_and_penalties` - `registry_updates` - `slashings` @@ -42,7 +42,8 @@ Sub-transitions: - `slashings_reset` - `randao_mixes_reset` - `historical_roots_update` -- `participation_record_updates` -- `sync_committee_updates` +- `participation_record_updates` (Phase 0 only) +- `participation_flag_updates` (Altair) +- `sync_committee_updates` (Altair) The resulting state should match the expected `post` state. From 6f7e04ef5c6d71fd20cd175e94b1cb37a415534b Mon Sep 17 00:00:00 2001 From: Danny Ryan Date: Mon, 7 Jun 2021 07:56:52 -0600 Subject: [PATCH 4/5] Apply suggestions from hwwhww code review Co-authored-by: Hsiao-Wei Wang --- .../test_process_participation_flag_updates.py | 6 +++--- tests/formats/epoch_processing/README.md | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/core/pyspec/eth2spec/test/altair/epoch_processing/test_process_participation_flag_updates.py b/tests/core/pyspec/eth2spec/test/altair/epoch_processing/test_process_participation_flag_updates.py index 287525608..3e322ad42 100644 --- a/tests/core/pyspec/eth2spec/test/altair/epoch_processing/test_process_participation_flag_updates.py +++ b/tests/core/pyspec/eth2spec/test/altair/epoch_processing/test_process_participation_flag_updates.py @@ -21,7 +21,7 @@ def run_process_participation_flag_updates(spec, state): @with_altair_and_later @spec_state_test -def test_zeroed(spec, state): +def test_all_zeroed(spec, state): next_epoch_via_block(spec, state) state.current_epoch_participation = [0] * len(state.validators) state.previous_epoch_participation = [0] * len(state.validators) @@ -70,7 +70,7 @@ def test_random_genesis(spec, state): @with_altair_and_later @spec_state_test -def test_zeroing(spec, state): +def test_current_epoch_zeroed(spec, state): next_epoch_via_block(spec, state) random_flags(spec, state, 12, current=False) state.current_epoch_participation = [0] * len(state.validators) @@ -79,7 +79,7 @@ def test_zeroing(spec, state): @with_altair_and_later @spec_state_test -def test_prev_zeroed(spec, state): +def test_previous_epoch_zeroed(spec, state): next_epoch_via_block(spec, state) random_flags(spec, state, 13, previous=False) state.previous_epoch_participation = [0] * len(state.validators) diff --git a/tests/formats/epoch_processing/README.md b/tests/formats/epoch_processing/README.md index 33ec79290..1032026a6 100644 --- a/tests/formats/epoch_processing/README.md +++ b/tests/formats/epoch_processing/README.md @@ -33,7 +33,7 @@ The provided pre-state is already transitioned to just before the specific sub-t Sub-transitions: - `justification_and_finalization` -- `inactivity_penalty_updates` (Altair) +- `inactivity_updates` (Altair) - `rewards_and_penalties` - `registry_updates` - `slashings` From 9c78de9d420c36f99f69ad5bced9e9ee8a85534f Mon Sep 17 00:00:00 2001 From: Danny Ryan Date: Mon, 7 Jun 2021 08:00:03 -0600 Subject: [PATCH 5/5] pr review --- .../test_process_participation_flag_updates.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/core/pyspec/eth2spec/test/altair/epoch_processing/test_process_participation_flag_updates.py b/tests/core/pyspec/eth2spec/test/altair/epoch_processing/test_process_participation_flag_updates.py index 3e322ad42..82acba322 100644 --- a/tests/core/pyspec/eth2spec/test/altair/epoch_processing/test_process_participation_flag_updates.py +++ b/tests/core/pyspec/eth2spec/test/altair/epoch_processing/test_process_participation_flag_updates.py @@ -13,7 +13,7 @@ from eth2spec.test.helpers.epoch_processing import run_epoch_processing_with def run_process_participation_flag_updates(spec, state): - old = state.current_epoch_participation + old = state.current_epoch_participation.copy() yield from run_epoch_processing_with(spec, state, 'process_participation_flag_updates') assert state.current_epoch_participation == [0] * len(state.validators) assert state.previous_epoch_participation == old @@ -99,6 +99,7 @@ def custom_validator_count(factor: float): @with_custom_state(balances_fn=custom_validator_count(1.3), threshold_fn=lambda spec: spec.config.EJECTION_BALANCE) @single_phase def test_slightly_larger_random(spec, state): + next_epoch_via_block(spec, state) random_flags(spec, state, 14) yield from run_process_participation_flag_updates(spec, state) @@ -109,5 +110,6 @@ def test_slightly_larger_random(spec, state): @with_custom_state(balances_fn=custom_validator_count(2.6), threshold_fn=lambda spec: spec.config.EJECTION_BALANCE) @single_phase def test_large_random(spec, state): + next_epoch_via_block(spec, state) random_flags(spec, state, 15) yield from run_process_participation_flag_updates(spec, state)