Skip to content

Commit 4fdd3b2

Browse files
committed
Update helpers.py
1. Fix `get_hashes_to_sign` 2. Remove multiple slots per committee option 3. Rename `get_proposer_position` to `get_block_committees_info`, it will return `BlockCommitteesInfo` as a simple structure of the committee information context.
1 parent 96fc4ed commit 4fdd3b2

File tree

3 files changed

+98
-37
lines changed

3 files changed

+98
-37
lines changed

eth/beacon/block_committees_info.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from typing import (
2+
Iterable,
3+
TYPE_CHECKING,
4+
)
5+
6+
if TYPE_CHECKING:
7+
from eth.beacon.types.shard_and_committees import ShardAndCommittee # noqa: F401
8+
9+
10+
class BlockCommitteesInfo:
11+
_proposer_index = None # validator index
12+
_proposer_index_in_committee = None
13+
_proposer_shard_id = None
14+
_proposer_committee_size = None
15+
_shards_and_committees = None
16+
17+
def __init__(self,
18+
proposer_index: int,
19+
proposer_index_in_committee: int,
20+
proposer_shard_id: int,
21+
proposer_committee_size: int,
22+
shards_and_committees: Iterable['ShardAndCommittee']) -> None:
23+
self._proposer_index = proposer_index
24+
self._proposer_index_in_committee = proposer_index_in_committee
25+
self._proposer_shard_id = proposer_shard_id
26+
self._proposer_committee_size = proposer_committee_size
27+
self._shards_and_committees = shards_and_committees
28+
29+
@property
30+
def proposer_index(self) -> int:
31+
return self._proposer_index
32+
33+
@property
34+
def proposer_index_in_committee(self) -> int:
35+
return self._proposer_index_in_committee
36+
37+
@property
38+
def proposer_shard_id(self) -> int:
39+
return self._proposer_shard_id
40+
41+
@property
42+
def proposer_committee_size(self) -> int:
43+
return self._proposer_committee_size
44+
45+
@property
46+
def shards_and_committees(self) -> Iterable['ShardAndCommittee']:
47+
return self._shards_and_committees

eth/beacon/helpers.py

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
Hash32,
1818
)
1919

20-
from eth.utils.blake import (
21-
blake,
22-
)
20+
from eth.beacon.block_committees_info import BlockCommitteesInfo
2321
from eth.beacon.types.shard_and_committees import (
2422
ShardAndCommittee,
2523
)
@@ -125,7 +123,7 @@ def get_hashes_to_sign(recent_block_hashes: Sequence[Hash32],
125123
to_slot=block.slot_number - 1,
126124
cycle_length=cycle_length,
127125
)
128-
yield blake(block.hash)
126+
yield block.hash
129127

130128

131129
@to_tuple
@@ -222,6 +220,18 @@ def get_active_validator_indices(dynasty: int,
222220
#
223221
# Shuffling
224222
#
223+
def clamp(minval: int, x: int, maxval: int) -> int:
224+
"""
225+
Bound the given ``x`` between ``minval`` and ``maxval``
226+
"""
227+
if x <= minval:
228+
return minval
229+
elif x >= maxval:
230+
return maxval
231+
else:
232+
return x
233+
234+
225235
def _get_shuffling_committee_slot_portions(
226236
active_validators_size: int,
227237
cycle_length: int,
@@ -321,24 +331,19 @@ def get_new_shuffling(*,
321331
"""
322332
active_validators = get_active_validator_indices(dynasty, validators)
323333
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,
334+
committees_per_slot = clamp(
335+
1,
336+
active_validators_size // cycle_length // (min_committee_size * 2) + 1,
337+
shard_count // cycle_length
330338
)
331-
332339
shuffled_active_validator_indices = shuffle(active_validators, seed)
333340

334341
# Split the shuffled list into cycle_length pieces
335342
validators_per_slot = split(shuffled_active_validator_indices, cycle_length)
336-
for slot, slot_indices in enumerate(validators_per_slot):
343+
for index, slot_indices in enumerate(validators_per_slot):
337344
# Split the shuffled list into committees_per_slot pieces
338345
shard_indices = split(slot_indices, committees_per_slot)
339-
shard_id_start = crosslinking_start_shard + (
340-
slot * committees_per_slot // slots_per_committee
341-
)
346+
shard_id_start = crosslinking_start_shard + index * committees_per_slot
342347
yield _get_shards_and_committees_for_shard_indices(
343348
shard_indices,
344349
shard_id_start,
@@ -349,9 +354,9 @@ def get_new_shuffling(*,
349354
#
350355
# Get proposer postition
351356
#
352-
def get_proposer_position(parent_block: 'BaseBeaconBlock',
353-
crystallized_state: 'CrystallizedState',
354-
cycle_length: int) -> Tuple[int, int]:
357+
def get_block_committees_info(parent_block: 'BaseBeaconBlock',
358+
crystallized_state: 'CrystallizedState',
359+
cycle_length: int) -> BlockCommitteesInfo:
355360
shards_and_committees = get_shards_and_committees_for_slot(
356361
crystallized_state,
357362
parent_block.slot_number,
@@ -366,14 +371,24 @@ def get_proposer_position(parent_block: 'BaseBeaconBlock',
366371
# `proposer_index_in_committee` th attester in `shard_and_committee`
367372
# is the proposer of the parent block.
368373
shard_and_committee = shards_and_committees[0]
369-
if len(shard_and_committee.committee) <= 0:
374+
proposer_committee_size = len(shard_and_committee.committee)
375+
if proposer_committee_size <= 0:
370376
raise ValueError(
371377
"The first committee should not be empty"
372378
)
373379

374380
proposer_index_in_committee = (
375381
parent_block.slot_number %
376-
len(shard_and_committee.committee)
382+
proposer_committee_size
377383
)
378384

379-
return proposer_index_in_committee, shard_and_committee.shard_id
385+
# The index in CrystallizedState.validators
386+
proposer_index = shard_and_committee.committee[proposer_index_in_committee]
387+
388+
return BlockCommitteesInfo(
389+
proposer_index=proposer_index,
390+
proposer_index_in_committee=proposer_index_in_committee,
391+
proposer_shard_id=shard_and_committee.shard_id,
392+
proposer_committee_size=proposer_committee_size,
393+
shards_and_committees=shards_and_committees,
394+
)

tests/beacon/test_helpers.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@
1111
get_new_shuffling,
1212
get_shards_and_committees_for_slot,
1313
get_signed_parent_hashes,
14-
get_proposer_position,
15-
)
16-
from eth.utils.blake import (
17-
blake,
14+
get_block_committees_info,
1815
)
1916

2017
from tests.beacon.helpers import (
@@ -163,7 +160,7 @@ def test_get_hashes_to_sign(genesis_block, cycle_length):
163160
cycle_length,
164161
)
165162
assert len(result) == cycle_length
166-
assert result[-1] == blake(block.hash)
163+
assert result[-1] == block.hash
167164

168165

169166
def test_get_new_recent_block_hashes(genesis_block,
@@ -347,13 +344,13 @@ def test_get_new_shuffling_handles_shard_wrap(genesis_validators,
347344
([], 1, ValueError()),
348345
],
349346
)
350-
def test_get_proposer_position(monkeypatch,
351-
genesis_block,
352-
genesis_crystallized_state,
353-
committee,
354-
parent_block_number,
355-
result_proposer_index_in_committee,
356-
cycle_length):
347+
def test_get_block_committees_info(monkeypatch,
348+
genesis_block,
349+
genesis_crystallized_state,
350+
committee,
351+
parent_block_number,
352+
result_proposer_index_in_committee,
353+
cycle_length):
357354
from eth.beacon import helpers
358355

359356
def mock_get_shards_and_committees_for_slot(parent_block,
@@ -376,16 +373,18 @@ def mock_get_shards_and_committees_for_slot(parent_block,
376373

377374
if isinstance(result_proposer_index_in_committee, Exception):
378375
with pytest.raises(ValueError):
379-
get_proposer_position(
376+
get_block_committees_info(
380377
parent_block,
381378
genesis_crystallized_state,
382379
cycle_length,
383380
)
384381
else:
385-
proposer_index_in_committee, _ = get_proposer_position(
382+
block_committees_info = get_block_committees_info(
386383
parent_block,
387384
genesis_crystallized_state,
388385
cycle_length,
389386
)
390-
391-
assert proposer_index_in_committee == result_proposer_index_in_committee
387+
assert (
388+
block_committees_info.proposer_index_in_committee ==
389+
result_proposer_index_in_committee
390+
)

0 commit comments

Comments
 (0)