1
1
from itertools import (
2
2
repeat ,
3
3
)
4
+
4
5
from typing import (
5
6
Any ,
6
7
Iterable ,
17
18
Hash32 ,
18
19
)
19
20
20
- from eth .utils .blake import (
21
- blake ,
21
+ from eth .utils .numeric import (
22
+ clamp ,
22
23
)
24
+
25
+ from eth .beacon .block_committees_info import BlockCommitteesInfo
23
26
from eth .beacon .types .shard_and_committees import (
24
27
ShardAndCommittee ,
25
28
)
@@ -42,7 +45,7 @@ def _get_element_from_recent_list(
42
45
target_slot : int ,
43
46
slot_relative_position : int ) -> Any :
44
47
"""
45
- Returns the element from ``target_list`` by the ``target_slot`` number,
48
+ Return the element from ``target_list`` by the ``target_slot`` number,
46
49
where the the element should be at ``target_slot - slot_relative_position``th
47
50
element of the given ``target_list``.
48
51
"""
@@ -72,7 +75,7 @@ def get_block_hash(
72
75
slot : int ,
73
76
cycle_length : int ) -> Hash32 :
74
77
"""
75
- Returns the blockhash from ``ActiveState.recent_block_hashes`` by
78
+ Return the blockhash from ``ActiveState.recent_block_hashes`` by
76
79
``current_block_slot_number``.
77
80
"""
78
81
if len (recent_block_hashes ) != cycle_length * 2 :
@@ -125,7 +128,7 @@ def get_hashes_to_sign(recent_block_hashes: Sequence[Hash32],
125
128
to_slot = block .slot_number - 1 ,
126
129
cycle_length = cycle_length ,
127
130
)
128
- yield blake ( block .hash )
131
+ yield block .hash
129
132
130
133
131
134
@to_tuple
@@ -189,7 +192,7 @@ def get_attestation_indices(crystallized_state: 'CrystallizedState',
189
192
attestation : 'AttestationRecord' ,
190
193
cycle_length : int ) -> Iterable [int ]:
191
194
"""
192
- Returns committee of the given attestation.
195
+ Return committee of the given attestation.
193
196
"""
194
197
shard_id = attestation .shard_id
195
198
@@ -228,7 +231,7 @@ def _get_shuffling_committee_slot_portions(
228
231
min_committee_size : int ,
229
232
shard_count : int ) -> Tuple [int , int ]:
230
233
"""
231
- Returns committees number per slot and slots number per committee.
234
+ Return committees number per slot and slots number per committee.
232
235
"""
233
236
# If there are enough active validators to form committees for every slot
234
237
if active_validators_size >= cycle_length * min_committee_size :
@@ -276,7 +279,7 @@ def get_new_shuffling(*,
276
279
min_committee_size : int ,
277
280
shard_count : int ) -> Iterable [Iterable [ShardAndCommittee ]]:
278
281
"""
279
- Returns shuffled ``shard_and_committee_for_slots`` (``[[ShardAndCommittee]]``) of
282
+ Return shuffled ``shard_and_committee_for_slots`` (``[[ShardAndCommittee]]``) of
280
283
the given active ``validators``.
281
284
282
285
Two-dimensional:
@@ -321,24 +324,19 @@ def get_new_shuffling(*,
321
324
"""
322
325
active_validators = get_active_validator_indices (dynasty , validators )
323
326
active_validators_size = len (active_validators )
324
-
325
- committees_per_slot , slots_per_committee = _get_shuffling_committee_slot_portions (
326
- active_validators_size ,
327
- cycle_length ,
328
- min_committee_size ,
329
- shard_count ,
327
+ committees_per_slot = clamp (
328
+ 1 ,
329
+ shard_count // cycle_length ,
330
+ active_validators_size // cycle_length // (min_committee_size * 2 ) + 1 ,
330
331
)
331
-
332
332
shuffled_active_validator_indices = shuffle (active_validators , seed )
333
333
334
334
# Split the shuffled list into cycle_length pieces
335
335
validators_per_slot = split (shuffled_active_validator_indices , cycle_length )
336
- for slot , slot_indices in enumerate (validators_per_slot ):
336
+ for index , slot_indices in enumerate (validators_per_slot ):
337
337
# Split the shuffled list into committees_per_slot pieces
338
338
shard_indices = split (slot_indices , committees_per_slot )
339
- shard_id_start = crosslinking_start_shard + (
340
- slot * committees_per_slot // slots_per_committee
341
- )
339
+ shard_id_start = crosslinking_start_shard + index * committees_per_slot
342
340
yield _get_shards_and_committees_for_shard_indices (
343
341
shard_indices ,
344
342
shard_id_start ,
@@ -349,31 +347,42 @@ def get_new_shuffling(*,
349
347
#
350
348
# Get proposer postition
351
349
#
352
- def get_proposer_position (parent_block : 'BaseBeaconBlock' ,
353
- crystallized_state : 'CrystallizedState' ,
354
- cycle_length : int ) -> Tuple [ int , int ] :
350
+ def get_block_committees_info (parent_block : 'BaseBeaconBlock' ,
351
+ crystallized_state : 'CrystallizedState' ,
352
+ cycle_length : int ) -> BlockCommitteesInfo :
355
353
shards_and_committees = get_shards_and_committees_for_slot (
356
354
crystallized_state ,
357
355
parent_block .slot_number ,
358
356
cycle_length ,
359
357
)
360
358
"""
361
- Returns the proposer index in committee and the ``shard_id`` .
359
+ Return the block committees and proposer info with BlockCommitteesInfo pack .
362
360
"""
363
- if len (shards_and_committees ) <= 0 :
364
- raise ValueError ("shards_and_committees should not be empty." )
365
-
366
361
# `proposer_index_in_committee` th attester in `shard_and_committee`
367
362
# is the proposer of the parent block.
368
- shard_and_committee = shards_and_committees [0 ]
369
- if len (shard_and_committee .committee ) <= 0 :
363
+ try :
364
+ shard_and_committee = shards_and_committees [0 ]
365
+ except IndexError :
366
+ raise ValueError ("shards_and_committees should not be empty." )
367
+
368
+ proposer_committee_size = len (shard_and_committee .committee )
369
+ if proposer_committee_size <= 0 :
370
370
raise ValueError (
371
371
"The first committee should not be empty"
372
372
)
373
373
374
374
proposer_index_in_committee = (
375
375
parent_block .slot_number %
376
- len ( shard_and_committee . committee )
376
+ proposer_committee_size
377
377
)
378
378
379
- return proposer_index_in_committee , shard_and_committee .shard_id
379
+ # The index in CrystallizedState.validators
380
+ proposer_index = shard_and_committee .committee [proposer_index_in_committee ]
381
+
382
+ return BlockCommitteesInfo (
383
+ proposer_index = proposer_index ,
384
+ proposer_index_in_committee = proposer_index_in_committee ,
385
+ proposer_shard_id = shard_and_committee .shard_id ,
386
+ proposer_committee_size = proposer_committee_size ,
387
+ shards_and_committees = shards_and_committees ,
388
+ )
0 commit comments