23
23
)
24
24
25
25
from eth .beacon .block_committees_info import BlockCommitteesInfo
26
+ from eth .beacon .enums .validator_status_codes import (
27
+ ValidatorStatusCode ,
28
+ )
26
29
from eth .beacon .types .shard_and_committees import (
27
30
ShardAndCommittee ,
28
31
)
37
40
from eth .beacon .types .attestation_records import AttestationRecord # noqa: F401
38
41
from eth .beacon .types .blocks import BaseBeaconBlock # noqa: F401
39
42
from eth .beacon .types .crystallized_states import CrystallizedState # noqa: F401
43
+ from eth .beacon .types .states import BeaconState # noqa: F401
40
44
from eth .beacon .types .validator_records import ValidatorRecord # noqa: F401
41
45
42
46
@@ -170,6 +174,9 @@ def get_shards_and_committees_for_slot(
170
174
crystallized_state : 'CrystallizedState' ,
171
175
slot : int ,
172
176
cycle_length : int ) -> Iterable [ShardAndCommittee ]:
177
+ """
178
+ FIXME
179
+ """
173
180
if len (crystallized_state .shard_and_committee_for_slots ) != cycle_length * 2 :
174
181
raise ValueError (
175
182
"Length of shard_and_committee_for_slots != cycle_length * 2"
@@ -192,6 +199,7 @@ def get_attestation_indices(crystallized_state: 'CrystallizedState',
192
199
attestation : 'AttestationRecord' ,
193
200
cycle_length : int ) -> Iterable [int ]:
194
201
"""
202
+ FIXME
195
203
Return committee of the given attestation.
196
204
"""
197
205
shard_id = attestation .shard_id
@@ -207,64 +215,30 @@ def get_attestation_indices(crystallized_state: 'CrystallizedState',
207
215
yield from shard_and_committee .committee
208
216
209
217
210
- @to_tuple
211
- def get_active_validator_indices (dynasty : int ,
212
- validators : Iterable ['ValidatorRecord' ]) -> Iterable [int ]:
218
+ def get_active_validator_indices (validators : Sequence ['ValidatorRecord' ]) -> Tuple [int , ...]:
213
219
"""
214
- TODO: Logic changed in the latest spec, will have to update when we add validator
215
- rotation logic.
216
- https://github.com/ethereum/eth2.0-specs/commit/52cf7f943dc99cfd27db9fb2c03c692858e2a789#diff-a08ecec277db4a6ed0b3635cfadc9af1 # noqa: E501
220
+ Gets indices of active validators from ``validators``.
217
221
"""
218
- o = []
219
- for index , validator in enumerate (validators ):
220
- if (validator .start_dynasty <= dynasty and dynasty < validator .end_dynasty ):
221
- o .append (index )
222
- return o
222
+ return tuple (
223
+ i for i , v in enumerate (validators )
224
+ if v .status in [ValidatorStatusCode .ACTIVE , ValidatorStatusCode .PENDING_EXIT ]
225
+ )
223
226
224
227
225
228
#
226
229
# Shuffling
227
230
#
228
- def _get_shuffling_committee_slot_portions (
229
- active_validators_size : int ,
230
- cycle_length : int ,
231
- min_committee_size : int ,
232
- shard_count : int ) -> Tuple [int , int ]:
233
- """
234
- Return committees number per slot and slots number per committee.
235
- """
236
- # If there are enough active validators to form committees for every slot
237
- if active_validators_size >= cycle_length * min_committee_size :
238
- # One slot can be attested by many committees, but not more than shard_count // cycle_length
239
- committees_per_slot = min (
240
- active_validators_size // cycle_length // (min_committee_size * 2 ) + 1 ,
241
- shard_count // cycle_length
242
- )
243
- # One committee only has to attest one slot
244
- slots_per_committee = 1
245
- else :
246
- # One slot can only be asttested by one committee
247
- committees_per_slot = 1
248
- # One committee has to asttest more than one slot
249
- slots_per_committee = 1
250
- bound = cycle_length * min (min_committee_size , active_validators_size )
251
- while (active_validators_size * slots_per_committee < bound ):
252
- slots_per_committee *= 2
253
-
254
- return committees_per_slot , slots_per_committee
255
-
256
-
257
231
@to_tuple
258
232
def _get_shards_and_committees_for_shard_indices (
259
233
shard_indices : Sequence [Sequence [int ]],
260
- shard_start : int ,
234
+ start_shard : int ,
261
235
shard_count : int ) -> Iterable [ShardAndCommittee ]:
262
236
"""
263
237
Returns filled [ShardAndCommittee] tuple.
264
238
"""
265
239
for index , indices in enumerate (shard_indices ):
266
240
yield ShardAndCommittee (
267
- shard = (shard_start + index ) % shard_count ,
241
+ shard = (start_shard + index ) % shard_count ,
268
242
committee = indices
269
243
)
270
244
@@ -273,14 +247,13 @@ def _get_shards_and_committees_for_shard_indices(
273
247
def get_new_shuffling (* ,
274
248
seed : Hash32 ,
275
249
validators : Sequence ['ValidatorRecord' ],
276
- dynasty : int ,
277
250
crosslinking_start_shard : int ,
278
251
cycle_length : int ,
279
- min_committee_size : int ,
252
+ target_committee_size : int ,
280
253
shard_count : int ) -> Iterable [Iterable [ShardAndCommittee ]]:
281
254
"""
282
255
Return shuffled ``shard_and_committee_for_slots`` (``[[ShardAndCommittee]]``) of
283
- the given active ``validators``.
256
+ the given active ``validators`` using ``seed`` as entropy .
284
257
285
258
Two-dimensional:
286
259
The first layer is ``slot`` number
@@ -319,27 +292,26 @@ def get_new_shuffling(*,
319
292
ShardAndCommittee(shard_id=5, committee=[7, 3, 11]),
320
293
],
321
294
]
322
-
323
- NOTE: The spec might be updated to output an array rather than an array of arrays.
324
295
"""
325
- active_validators = get_active_validator_indices (dynasty , validators )
296
+ active_validators = get_active_validator_indices (validators )
326
297
active_validators_size = len (active_validators )
327
298
committees_per_slot = clamp (
328
299
1 ,
329
300
shard_count // cycle_length ,
330
- active_validators_size // cycle_length // ( min_committee_size * 2 ) + 1 ,
301
+ active_validators_size // cycle_length // target_committee_size ,
331
302
)
303
+ # Shuffle with seed
332
304
shuffled_active_validator_indices = shuffle (active_validators , seed )
333
305
334
- # Split the shuffled list into cycle_length pieces
306
+ # Split the shuffled list into epoch_length pieces
335
307
validators_per_slot = split (shuffled_active_validator_indices , cycle_length )
336
308
for index , slot_indices in enumerate (validators_per_slot ):
337
309
# Split the shuffled list into committees_per_slot pieces
338
310
shard_indices = split (slot_indices , committees_per_slot )
339
- shard_id_start = crosslinking_start_shard + index * committees_per_slot
311
+ start_shard = crosslinking_start_shard + index * committees_per_slot
340
312
yield _get_shards_and_committees_for_shard_indices (
341
313
shard_indices ,
342
- shard_id_start ,
314
+ start_shard ,
343
315
shard_count ,
344
316
)
345
317
@@ -356,6 +328,7 @@ def get_block_committees_info(parent_block: 'BaseBeaconBlock',
356
328
cycle_length ,
357
329
)
358
330
"""
331
+ FIXME
359
332
Return the block committees and proposer info with BlockCommitteesInfo pack.
360
333
"""
361
334
# `proposer_index_in_committee` th attester in `shard_and_committee`
0 commit comments