Merge pull request #2889 from jtraglia/remove-set-sampling

Convert sets to sorted lists prior to sampling
This commit is contained in:
Hsiao-Wei Wang 2022-05-10 16:12:28 +02:00 committed by GitHub
commit 96de0fe646
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 10 deletions

View File

@ -281,7 +281,7 @@ def test_transition_with_random_three_quarters_participation(state, fork_epoch,
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 test_transition_with_random_half_participation(state, fork_epoch, spec, post
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,

View File

@ -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(

View File

@ -200,7 +200,10 @@ def run_with_participation(spec, state, participation_fn):
@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))
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,14 +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(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(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
@ -223,14 +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(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(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
@ -238,7 +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(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

View File

@ -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