def test_get_shuffling_is_complete(activated_genesis_validators, slots_per_epoch, target_committee_size, committee_config, epoch): shuffling = get_shuffling( seed=b'\x35' * 32, validators=activated_genesis_validators, epoch=epoch, committee_config=committee_config, ) assert len(shuffling) == slots_per_epoch assert len(shuffling) > 0 for committee in shuffling: assert len(committee) <= target_committee_size assert len(committee) > 0 validator_indices = tuple( itertools.chain([ validator_index for committee in shuffling for validator_index in committee ])) assert isdistinct(validator_indices) activated_genesis_validator_indices = tuple( index for index in range(len(activated_genesis_validators))) assert sorted(validator_indices) == sorted( activated_genesis_validator_indices)
def test_get_shuffling_cache(activated_genesis_validators, committee_config, epoch): start_time = time.time() get_shuffling( seed=b'\x55' * 32, validators=activated_genesis_validators, epoch=epoch, committee_config=committee_config, ) one_shuffle_time = time.time() - start_time start_time = time.time() for _ in range(100): get_shuffling( seed=b'\x66' * 32, validators=activated_genesis_validators, epoch=epoch, committee_config=committee_config, ) one_hundred_shuffles_time = time.time() - start_time assert one_hundred_shuffles_time < one_shuffle_time * 2
def test_get_crosslink_committees_at_slot( monkeypatch, genesis_slot, n_validators_state, current_slot, slot, slots_per_epoch, target_committee_size, shard_count, genesis_epoch, committee_config, registry_change, should_reseed, previous_shuffling_epoch, current_shuffling_epoch, shuffling_epoch): # Mock generate_seed new_seed = b'\x88' * 32 def mock_generate_seed(state, epoch, committee_config): return new_seed monkeypatch.setattr('eth2.beacon.helpers.generate_seed', mock_generate_seed) state = n_validators_state.copy( slot=current_slot, previous_shuffling_epoch=previous_shuffling_epoch, current_shuffling_epoch=current_shuffling_epoch, previous_shuffling_seed=b'\x11' * 32, current_shuffling_seed=b'\x22' * 32, ) crosslink_committees_at_slot = get_crosslink_committees_at_slot( state=state, slot=slot, committee_config=committee_config, registry_change=registry_change, ) assert len(crosslink_committees_at_slot) > 0 for crosslink_committee in crosslink_committees_at_slot: committee, shard = crosslink_committee assert len(committee) > 0 assert shard < shard_count # # Check shuffling_start_shard # offset = slot % slots_per_epoch result_slot_start_shard = crosslink_committees_at_slot[0][1] current_committees_per_epoch = get_current_epoch_committee_count( state=state, shard_count=shard_count, slots_per_epoch=slots_per_epoch, target_committee_size=target_committee_size, ) if registry_change: committees_per_epoch = current_committees_per_epoch shuffling_start_shard = (state.current_shuffling_start_shard + current_committees_per_epoch) % shard_count elif should_reseed: committees_per_epoch = get_next_epoch_committee_count( state=state, shard_count=shard_count, slots_per_epoch=slots_per_epoch, target_committee_size=target_committee_size, ) shuffling_start_shard = state.current_shuffling_start_shard else: committees_per_epoch = current_committees_per_epoch shuffling_start_shard = state.current_shuffling_start_shard committees_per_slot = committees_per_epoch // slots_per_epoch assert result_slot_start_shard == ( shuffling_start_shard + committees_per_slot * offset) % shard_count # # Check seed # epoch = slot_to_epoch(slot, slots_per_epoch) current_epoch = state.current_epoch(slots_per_epoch) previous_epoch = state.previous_epoch(slots_per_epoch) next_epoch = current_epoch + 1 if epoch == current_epoch: seed = state.current_shuffling_seed elif epoch == previous_epoch: seed = state.previous_shuffling_seed elif epoch == next_epoch: if registry_change or should_reseed: seed = new_seed else: seed = state.current_shuffling_seed shuffling = get_shuffling( seed=seed, validators=state.validator_registry, epoch=shuffling_epoch, committee_config=committee_config, ) assert shuffling[committees_per_slot * offset] == crosslink_committees_at_slot[0][0]