From a7bda480fe9c747349c92248385d0a2a809b0b66 Mon Sep 17 00:00:00 2001 From: Justin Traglia Date: Mon, 9 May 2022 14:09:27 -0500 Subject: [PATCH 1/3] Convert sets to sorted lists prior to sampling This is because sampling of sets has been deprecated in Python 3.9. I used sorted() instead of list() so that things are deterministic. --- .../test/altair/transition/test_transition.py | 4 ++-- .../altair/unittests/validator/test_validator.py | 2 +- .../test_process_rewards_and_penalties.py | 12 ++++++------ .../test/phase0/fork_choice/test_on_block.py | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/core/pyspec/eth2spec/test/altair/transition/test_transition.py b/tests/core/pyspec/eth2spec/test/altair/transition/test_transition.py index 2ac8bfb485..32861b866f 100644 --- a/tests/core/pyspec/eth2spec/test/altair/transition/test_transition.py +++ b/tests/core/pyspec/eth2spec/test/altair/transition/test_transition.py @@ -281,7 +281,7 @@ def _drop_random_quarter(_slot, _index, indices): assert committee_len >= 4 filter_len = committee_len // 4 participant_count = committee_len - filter_len - return rng.sample(indices, participant_count) + return rng.sample(sorted(indices), participant_count) yield from _run_transition_test_with_attestations( state, @@ -304,7 +304,7 @@ def _drop_random_half(_slot, _index, indices): assert committee_len >= 2 filter_len = committee_len // 2 participant_count = committee_len - filter_len - return rng.sample(indices, participant_count) + return rng.sample(sorted(indices), participant_count) yield from _run_transition_test_with_attestations( state, diff --git a/tests/core/pyspec/eth2spec/test/altair/unittests/validator/test_validator.py b/tests/core/pyspec/eth2spec/test/altair/unittests/validator/test_validator.py index dd9214040e..1efeca5471 100644 --- a/tests/core/pyspec/eth2spec/test/altair/unittests/validator/test_validator.py +++ b/tests/core/pyspec/eth2spec/test/altair/unittests/validator/test_validator.py @@ -59,7 +59,7 @@ def test_is_assigned_to_sync_committee(spec, state): if disqualified_pubkeys: sample_size = 3 assert validator_count >= sample_size - some_pubkeys = rng.sample(disqualified_pubkeys, sample_size) + some_pubkeys = rng.sample(sorted(disqualified_pubkeys), sample_size) for pubkey in some_pubkeys: validator_index = active_pubkeys.index(pubkey) is_current = spec.is_assigned_to_sync_committee( diff --git a/tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_rewards_and_penalties.py b/tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_rewards_and_penalties.py index 7ff4e83d3b..10772ca824 100644 --- a/tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_rewards_and_penalties.py +++ b/tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_rewards_and_penalties.py @@ -200,7 +200,7 @@ def participation_tracker(slot, comm_index, comm): @spec_state_test def test_almost_empty_attestations(spec, state): rng = Random(1234) - yield from run_with_participation(spec, state, lambda slot, comm_index, comm: rng.sample(comm, 1)) + yield from run_with_participation(spec, state, lambda slot, comm_index, comm: rng.sample(sorted(comm), 1)) @with_all_phases @@ -208,14 +208,14 @@ def test_almost_empty_attestations(spec, state): @leaking() def test_almost_empty_attestations_with_leak(spec, state): rng = Random(1234) - yield from run_with_participation(spec, state, lambda slot, comm_index, comm: rng.sample(comm, 1)) + yield from run_with_participation(spec, state, lambda slot, comm_index, comm: rng.sample(sorted(comm), 1)) @with_all_phases @spec_state_test def test_random_fill_attestations(spec, state): rng = Random(4567) - yield from run_with_participation(spec, state, lambda slot, comm_index, comm: rng.sample(comm, len(comm) // 3)) + yield from run_with_participation(spec, state, lambda slot, comm_index, comm: rng.sample(sorted(comm), len(comm) // 3)) @with_all_phases @@ -223,14 +223,14 @@ def test_random_fill_attestations(spec, state): @leaking() def test_random_fill_attestations_with_leak(spec, state): rng = Random(4567) - yield from run_with_participation(spec, state, lambda slot, comm_index, comm: rng.sample(comm, len(comm) // 3)) + yield from run_with_participation(spec, state, lambda slot, comm_index, comm: rng.sample(sorted(comm), len(comm) // 3)) @with_all_phases @spec_state_test def test_almost_full_attestations(spec, state): rng = Random(8901) - yield from run_with_participation(spec, state, lambda slot, comm_index, comm: rng.sample(comm, len(comm) - 1)) + yield from run_with_participation(spec, state, lambda slot, comm_index, comm: rng.sample(sorted(comm), len(comm) - 1)) @with_all_phases @@ -238,7 +238,7 @@ def test_almost_full_attestations(spec, state): @leaking() def test_almost_full_attestations_with_leak(spec, state): rng = Random(8901) - yield from run_with_participation(spec, state, lambda slot, comm_index, comm: rng.sample(comm, len(comm) - 1)) + yield from run_with_participation(spec, state, lambda slot, comm_index, comm: rng.sample(sorted(comm), len(comm) - 1)) @with_all_phases diff --git a/tests/core/pyspec/eth2spec/test/phase0/fork_choice/test_on_block.py b/tests/core/pyspec/eth2spec/test/phase0/fork_choice/test_on_block.py index f57522ad76..eede246302 100644 --- a/tests/core/pyspec/eth2spec/test/phase0/fork_choice/test_on_block.py +++ b/tests/core/pyspec/eth2spec/test/phase0/fork_choice/test_on_block.py @@ -38,7 +38,7 @@ def _drop_random_one_third(_slot, _index, indices): assert committee_len >= 3 filter_len = committee_len // 3 participant_count = committee_len - filter_len - return rng.sample(indices, participant_count) + return rng.sample(sorted(indices), participant_count) @with_all_phases From 9dcb2eecbc6bf077900997ddb9b48379d88ea833 Mon Sep 17 00:00:00 2001 From: Justin Traglia Date: Mon, 9 May 2022 14:28:54 -0500 Subject: [PATCH 2/3] Fix linter warnings --- .../test_process_rewards_and_penalties.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_rewards_and_penalties.py b/tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_rewards_and_penalties.py index 10772ca824..ee1a751d25 100644 --- a/tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_rewards_and_penalties.py +++ b/tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_rewards_and_penalties.py @@ -215,7 +215,8 @@ def test_almost_empty_attestations_with_leak(spec, state): @spec_state_test def test_random_fill_attestations(spec, state): rng = Random(4567) - yield from run_with_participation(spec, state, lambda slot, comm_index, comm: rng.sample(sorted(comm), len(comm) // 3)) + yield from run_with_participation(spec, state, + lambda slot, comm_index, comm: rng.sample(sorted(comm), len(comm) // 3)) @with_all_phases @@ -223,14 +224,16 @@ def test_random_fill_attestations(spec, state): @leaking() def test_random_fill_attestations_with_leak(spec, state): rng = Random(4567) - yield from run_with_participation(spec, state, lambda slot, comm_index, comm: rng.sample(sorted(comm), len(comm) // 3)) + yield from run_with_participation(spec, state, + lambda slot, comm_index, comm: rng.sample(sorted(comm), len(comm) // 3)) @with_all_phases @spec_state_test def test_almost_full_attestations(spec, state): rng = Random(8901) - yield from run_with_participation(spec, state, lambda slot, comm_index, comm: rng.sample(sorted(comm), len(comm) - 1)) + yield from run_with_participation(spec, state, + lambda slot, comm_index, comm: rng.sample(sorted(comm), len(comm) - 1)) @with_all_phases @@ -238,7 +241,8 @@ def test_almost_full_attestations(spec, state): @leaking() def test_almost_full_attestations_with_leak(spec, state): rng = Random(8901) - yield from run_with_participation(spec, state, lambda slot, comm_index, comm: rng.sample(sorted(comm), len(comm) - 1)) + yield from run_with_participation(spec, state, + lambda slot, comm_index, comm: rng.sample(sorted(comm), len(comm) - 1)) @with_all_phases From 90c182563756f15873cfb7f89b33df71cad84ef4 Mon Sep 17 00:00:00 2001 From: Justin Traglia Date: Mon, 9 May 2022 15:53:36 -0500 Subject: [PATCH 3/3] Convert participation_fn from lambda to def I felt that the the lambda was a little too complicated. --- .../test_process_rewards_and_penalties.py | 34 +++++++++++++------ 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_rewards_and_penalties.py b/tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_rewards_and_penalties.py index ee1a751d25..2bb2974980 100644 --- a/tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_rewards_and_penalties.py +++ b/tests/core/pyspec/eth2spec/test/phase0/epoch_processing/test_process_rewards_and_penalties.py @@ -200,7 +200,10 @@ def participation_tracker(slot, comm_index, comm): @spec_state_test def test_almost_empty_attestations(spec, state): rng = Random(1234) - yield from run_with_participation(spec, state, lambda slot, comm_index, comm: rng.sample(sorted(comm), 1)) + + def participation_fn(slot, comm_index, comm): + return rng.sample(sorted(comm), 1) + yield from run_with_participation(spec, state, participation_fn) @with_all_phases @@ -208,15 +211,20 @@ def test_almost_empty_attestations(spec, state): @leaking() def test_almost_empty_attestations_with_leak(spec, state): rng = Random(1234) - yield from run_with_participation(spec, state, lambda slot, comm_index, comm: rng.sample(sorted(comm), 1)) + + def participation_fn(slot, comm_index, comm): + return rng.sample(sorted(comm), 1) + yield from run_with_participation(spec, state, participation_fn) @with_all_phases @spec_state_test def test_random_fill_attestations(spec, state): rng = Random(4567) - yield from run_with_participation(spec, state, - lambda slot, comm_index, comm: rng.sample(sorted(comm), len(comm) // 3)) + + def participation_fn(slot, comm_index, comm): + return rng.sample(sorted(comm), len(comm) // 3) + yield from run_with_participation(spec, state, participation_fn) @with_all_phases @@ -224,16 +232,20 @@ def test_random_fill_attestations(spec, state): @leaking() def test_random_fill_attestations_with_leak(spec, state): rng = Random(4567) - yield from run_with_participation(spec, state, - lambda slot, comm_index, comm: rng.sample(sorted(comm), len(comm) // 3)) + + def participation_fn(slot, comm_index, comm): + return rng.sample(sorted(comm), len(comm) // 3) + yield from run_with_participation(spec, state, participation_fn) @with_all_phases @spec_state_test def test_almost_full_attestations(spec, state): rng = Random(8901) - yield from run_with_participation(spec, state, - lambda slot, comm_index, comm: rng.sample(sorted(comm), len(comm) - 1)) + + def participation_fn(slot, comm_index, comm): + return rng.sample(sorted(comm), len(comm) - 1) + yield from run_with_participation(spec, state, participation_fn) @with_all_phases @@ -241,8 +253,10 @@ def test_almost_full_attestations(spec, state): @leaking() def test_almost_full_attestations_with_leak(spec, state): rng = Random(8901) - yield from run_with_participation(spec, state, - lambda slot, comm_index, comm: rng.sample(sorted(comm), len(comm) - 1)) + + def participation_fn(slot, comm_index, comm): + return rng.sample(sorted(comm), len(comm) - 1) + yield from run_with_participation(spec, state, participation_fn) @with_all_phases