def test_shuffle_out_of_bound(): values = [i for i in range(2**24 + 1)] with pytest.raises(ValueError): shuffle(values, b'hello')
def get_new_shuffling(*, seed: Hash32, validators: Sequence['ValidatorRecord'], crosslinking_start_shard: ShardNumber, epoch_length: int, target_committee_size: int, shard_count: int) -> Iterable[Iterable[ShardCommittee]]: """ Return shuffled ``shard_committee_for_slots`` (``[[ShardCommittee]]``) of the given active ``validators`` using ``seed`` as entropy. Two-dimensional: The first layer is ``slot`` number ``shard_committee_for_slots[slot] -> [ShardCommittee]`` The second layer is ``shard_indices`` number ``shard_committee_for_slots[slot][shard_indices] -> ShardCommittee`` Example: validators: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] After shuffling: [6, 0, 2, 12, 14, 8, 10, 4, 9, 1, 5, 13, 15, 7, 3, 11] Split by slot: [ [6, 0, 2, 12, 14], [8, 10, 4, 9, 1], [5, 13, 15, 7, 3, 11] ] Split by shard: [ [6, 0], [2, 12, 14], [8, 10], [4, 9, 1], [5, 13, 15] ,[7, 3, 11] ] Fill to output: [ # slot 0 [ ShardCommittee(shard_id=0, committee=[6, 0]), ShardCommittee(shard_id=1, committee=[2, 12, 14]), ], # slot 1 [ ShardCommittee(shard_id=2, committee=[8, 10]), ShardCommittee(shard_id=3, committee=[4, 9, 1]), ], # slot 2 [ ShardCommittee(shard_id=4, committee=[5, 13, 15]), ShardCommittee(shard_id=5, committee=[7, 3, 11]), ], ] """ active_validators = get_active_validator_indices(validators) active_validators_size = len(active_validators) committees_per_slot = clamp( 1, shard_count // epoch_length, active_validators_size // epoch_length // target_committee_size, ) # Shuffle with seed shuffled_active_validator_indices = shuffle(active_validators, seed) # Split the shuffled list into epoch_length pieces validators_per_slot = split(shuffled_active_validator_indices, epoch_length) for index, slot_indices in enumerate(validators_per_slot): # Split the shuffled list into committees_per_slot pieces shard_indices = split(slot_indices, committees_per_slot) start_shard = crosslinking_start_shard + index * committees_per_slot yield _get_shards_committees_for_shard_indices( shard_indices, start_shard, active_validators_size, shard_count, )
def test_shuffle_consistent(values, seed, expect): assert shuffle(values, seed) == expect