Skip to content

Commit 13bdd94

Browse files
committed
Fix get_shard_committees_at_slot
1 parent fdf04a7 commit 13bdd94

File tree

2 files changed

+124
-36
lines changed

2 files changed

+124
-36
lines changed

eth/beacon/helpers.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -170,30 +170,42 @@ def get_new_recent_block_hashes(old_block_hashes: Sequence[Hash32],
170170
# Get shards_committees or indices
171171
#
172172
@to_tuple
173-
def get_shards_committees_for_slot(
174-
crystallized_state: 'CrystallizedState',
173+
def _get_shard_committees_at_slot(
174+
latest_state_recalculation_slot: int,
175+
shard_committees_at_slots: Sequence[Sequence[ShardCommittee]],
175176
slot: int,
176177
epoch_length: int) -> Iterable[ShardCommittee]:
177-
"""
178-
FIXME
179-
"""
180-
if len(crystallized_state.shard_committee_for_slots) != epoch_length * 2:
178+
if len(shard_committees_at_slots) != epoch_length * 2:
181179
raise ValueError(
182-
"Length of shard_committee_for_slots != epoch_length * 2"
180+
"Length of shard_committees_at_slots != epoch_length * 2"
183181
"\texpected: %s, found: %s" % (
184-
epoch_length * 2, len(crystallized_state.shard_committee_for_slots)
182+
epoch_length * 2, len(shard_committees_at_slots)
185183
)
186184
)
187185

188-
slot_relative_position = crystallized_state.last_state_recalc - epoch_length
186+
slot_relative_position = latest_state_recalculation_slot - epoch_length
189187

190188
yield from _get_element_from_recent_list(
191-
crystallized_state.shard_committee_for_slots,
189+
shard_committees_at_slots,
192190
slot,
193191
slot_relative_position,
194192
)
195193

196194

195+
def get_shard_committees_at_slot(state: 'BeaconState',
196+
slot: int,
197+
epoch_length: int) -> Tuple[ShardCommittee]:
198+
"""
199+
Returns the ``ShardCommittee`` for the ``slot``.
200+
"""
201+
return _get_shard_committees_at_slot(
202+
latest_state_recalculation_slot=state.latest_state_recalculation_slot,
203+
shard_committees_at_slots=state.shard_committees_at_slots,
204+
slot=slot,
205+
epoch_length=epoch_length,
206+
)
207+
208+
197209
@to_tuple
198210
def get_attestation_indices(crystallized_state: 'CrystallizedState',
199211
attestation: 'AttestationRecord',
@@ -204,7 +216,7 @@ def get_attestation_indices(crystallized_state: 'CrystallizedState',
204216
"""
205217
shard_id = attestation.shard_id
206218

207-
shards_committees_for_slot = get_shards_committees_for_slot(
219+
shards_committees_for_slot = get_shard_committees_at_slot(
208220
crystallized_state,
209221
attestation.slot,
210222
epoch_length,
@@ -325,7 +337,7 @@ def get_new_shuffling(*,
325337
def get_block_committees_info(parent_block: 'BaseBeaconBlock',
326338
crystallized_state: 'CrystallizedState',
327339
epoch_length: int) -> BlockCommitteesInfo:
328-
shards_committees = get_shards_committees_for_slot(
340+
shards_committees = get_shard_committees_at_slot(
329341
crystallized_state,
330342
parent_block.slot_number,
331343
epoch_length,

tests/beacon/test_helpers.py

Lines changed: 100 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
get_hashes_from_recent_block_hashes,
1515
get_hashes_to_sign,
1616
get_new_shuffling,
17-
get_shards_committees_for_slot,
17+
_get_shard_committees_at_slot,
1818
get_signed_parent_hashes,
1919
get_block_committees_info,
2020
)
@@ -24,6 +24,21 @@
2424
)
2525

2626

27+
def get_sample_shard_committees_at_slots(num_slot,
28+
num_shard_committee_per_slot,
29+
sample_shard_committee_params):
30+
31+
return tuple(
32+
[
33+
[
34+
ShardCommittee(**sample_shard_committee_params)
35+
for _ in range(num_shard_committee_per_slot)
36+
]
37+
for _ in range(num_slot)
38+
]
39+
)
40+
41+
2742
def generate_mock_recent_block_hashes(
2843
genesis_block,
2944
current_block_number,
@@ -202,39 +217,100 @@ def test_get_new_recent_block_hashes(genesis_block,
202217
#
203218
# Get shards_committees or indices
204219
#
205-
@pytest.mark.xfail(reason="Need to be fixed")
206220
@pytest.mark.parametrize(
207221
(
208-
'num_validators,slot,success'
222+
'num_validators,'
223+
'cycle_length,'
224+
'latest_state_recalculation_slot,'
225+
'num_slot,'
226+
'num_shard_committee_per_slot,'
227+
'slot,'
228+
'success'
209229
),
210230
[
211-
(100, 0, True),
212-
(100, 63, True),
213-
(100, 64, False),
231+
(
232+
100,
233+
64,
234+
0,
235+
128,
236+
10,
237+
0,
238+
True,
239+
),
240+
(
241+
100,
242+
64,
243+
64,
244+
128,
245+
10,
246+
64,
247+
True,
248+
),
249+
# The length of shard_committees_at_slots != epoch_length * 2
250+
(
251+
100,
252+
64,
253+
64,
254+
127,
255+
10,
256+
0,
257+
False,
258+
),
259+
# slot is too small
260+
(
261+
100,
262+
64,
263+
128,
264+
128,
265+
10,
266+
0,
267+
False,
268+
),
269+
# slot is too large
270+
(
271+
100,
272+
64,
273+
0,
274+
128,
275+
10,
276+
64,
277+
False,
278+
),
214279
],
215280
)
216281
def test_get_shard_committee_for_slot(
217-
genesis_crystallized_state,
218282
num_validators,
283+
cycle_length,
284+
latest_state_recalculation_slot,
285+
num_slot,
286+
num_shard_committee_per_slot,
219287
slot,
220288
success,
221-
epoch_length):
222-
crystallized_state = genesis_crystallized_state
289+
epoch_length,
290+
sample_shard_committee_params):
291+
292+
shard_committees_at_slots = get_sample_shard_committees_at_slots(
293+
num_slot,
294+
num_shard_committee_per_slot,
295+
sample_shard_committee_params
296+
)
223297

224298
if success:
225-
shards_committees_for_slot = get_shards_committees_for_slot(
226-
crystallized_state,
227-
slot,
228-
epoch_length,
299+
shard_committees = _get_shard_committees_at_slot(
300+
latest_state_recalculation_slot=latest_state_recalculation_slot,
301+
shard_committees_at_slots=shard_committees_at_slots,
302+
slot=slot,
303+
epoch_length=epoch_length,
229304
)
230-
assert len(shards_committees_for_slot) > 0
231-
assert len(shards_committees_for_slot[0].committee) > 0
305+
assert len(shard_committees) > 0
306+
assert len(shard_committees[0].committee) > 0
232307
else:
233308
with pytest.raises(ValueError):
234-
get_shards_committees_for_slot(
235-
crystallized_state,
236-
slot,
237-
epoch_length,
309+
_get_shard_committees_at_slot(
310+
latest_state_recalculation_slot=latest_state_recalculation_slot,
311+
shard_committees_at_slots=shard_committees_at_slots,
312+
slot=slot,
313+
epoch_length=epoch_length,
238314
)
239315

240316

@@ -363,17 +439,17 @@ def test_get_block_committees_info(monkeypatch,
363439
epoch_length):
364440
from eth.beacon import helpers
365441

366-
def mock_get_shards_committees_for_slot(parent_block,
367-
crystallized_state,
368-
epoch_length):
442+
def mock_get_shard_committees_at_slot(parent_block,
443+
crystallized_state,
444+
epoch_length):
369445
return [
370446
ShardCommittee(shard_id=1, committee=committee),
371447
]
372448

373449
monkeypatch.setattr(
374450
helpers,
375-
'get_shards_committees_for_slot',
376-
mock_get_shards_committees_for_slot
451+
'_get_shard_committees_at_slot',
452+
mock__get_shard_committees_at_slot
377453
)
378454

379455
parent_block = genesis_block

0 commit comments

Comments
 (0)