Merge pull request #2189 from ralexstokes/duplicate-sync-committee-patch

test refactor to use helper for duplicates in light client committees
This commit is contained in:
Hsiao-Wei Wang 2021-01-22 09:55:52 +08:00 committed by GitHub
commit 1e7c33810f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 5 deletions

View File

@ -19,6 +19,27 @@ from eth2spec.test.context import (
with_configs, with_configs,
spec_state_test, spec_state_test,
) )
from eth2spec.utils.hash_function import hash
def get_committee_indices(spec, state, duplicates=False):
'''
This utility function allows the caller to ensure there are or are not
duplicate validator indices in the returned committee based on
the boolean ``duplicates``.
'''
state = state.copy()
current_epoch = spec.get_current_epoch(state)
randao_index = current_epoch % spec.EPOCHS_PER_HISTORICAL_VECTOR
while True:
committee = spec.get_sync_committee_indices(state, spec.get_current_epoch(state))
if duplicates:
if len(committee) != len(set(committee)):
return committee
else:
if len(committee) == len(set(committee)):
return committee
state.randao_mixes[randao_index] = hash(state.randao_mixes[randao_index])
@with_all_phases_except([PHASE0, PHASE1]) @with_all_phases_except([PHASE0, PHASE1])
@ -78,12 +99,11 @@ def compute_sync_committee_participant_reward(spec, state, participant_index, ac
@with_configs([MINIMAL], reason="to create nonduplicate committee") @with_configs([MINIMAL], reason="to create nonduplicate committee")
@spec_state_test @spec_state_test
def test_sync_committee_rewards_nonduplicate_committee(spec, state): def test_sync_committee_rewards_nonduplicate_committee(spec, state):
committee = spec.get_sync_committee_indices(state, spec.get_current_epoch(state)) committee = get_committee_indices(spec, state, duplicates=False)
committee_size = len(committee) committee_size = len(committee)
active_validator_count = len(spec.get_active_validator_indices(state, spec.get_current_epoch(state))) active_validator_count = len(spec.get_active_validator_indices(state, spec.get_current_epoch(state)))
# Preconditions of this test case # Preconditions of this test case
# Note that the committee members MAY still be duplicate even with enough active validator count probabilistically.
assert active_validator_count >= spec.SYNC_COMMITTEE_SIZE assert active_validator_count >= spec.SYNC_COMMITTEE_SIZE
assert committee_size == len(set(committee)) assert committee_size == len(set(committee))
@ -127,13 +147,11 @@ def test_sync_committee_rewards_nonduplicate_committee(spec, state):
@with_configs([MAINNET], reason="to create duplicate committee") @with_configs([MAINNET], reason="to create duplicate committee")
@spec_state_test @spec_state_test
def test_sync_committee_rewards_duplicate_committee(spec, state): def test_sync_committee_rewards_duplicate_committee(spec, state):
committee = spec.get_sync_committee_indices(state, spec.get_current_epoch(state)) committee = get_committee_indices(spec, state, duplicates=True)
committee_size = len(committee) committee_size = len(committee)
active_validator_count = len(spec.get_active_validator_indices(state, spec.get_current_epoch(state))) active_validator_count = len(spec.get_active_validator_indices(state, spec.get_current_epoch(state)))
# Preconditions of this test case # Preconditions of this test case
# With mainnet config, where active validators are less than SYNC_COMMITTEE_SIZE,
# the committee members SHOULD be duplicate.
assert active_validator_count < spec.SYNC_COMMITTEE_SIZE assert active_validator_count < spec.SYNC_COMMITTEE_SIZE
assert committee_size > len(set(committee)) assert committee_size > len(set(committee))