Skip to content

Commit 30e6cea

Browse files
committed
Refactor and add testcases of test_get_shuffling_is_complete
1 parent 5850e0b commit 30e6cea

File tree

3 files changed

+48
-39
lines changed

3 files changed

+48
-39
lines changed

tests/beacon/conftest.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import pytest
22
import rlp
33

4-
from eth_utils import (
5-
to_tuple,
6-
)
7-
84
from eth.constants import (
95
ZERO_HASH32,
106
)
@@ -379,6 +375,7 @@ def ten_validators_state(empty_beacon_state, max_deposit, far_future_slot):
379375
mock_validator_record(
380376
pubkey=pubkey,
381377
far_future_slot=far_future_slot,
378+
is_active=True,
382379
)
383380
for pubkey in range(validator_count)
384381
),
@@ -592,7 +589,7 @@ def max_exits():
592589
#
593590
@pytest.fixture
594591
def genesis_state(sample_beacon_state_params,
595-
genesis_validators,
592+
activated_genesis_validators,
596593
genesis_balances,
597594
epoch_length,
598595
target_committee_size,
@@ -601,15 +598,15 @@ def genesis_state(sample_beacon_state_params,
601598
latest_block_roots_length):
602599
initial_shuffling = get_shuffling(
603600
seed=ZERO_HASH32,
604-
validators=genesis_validators,
601+
validators=activated_genesis_validators,
605602
crosslinking_start_shard=0,
606603
slot=genesis_slot,
607604
epoch_length=epoch_length,
608605
target_committee_size=target_committee_size,
609606
shard_count=shard_count
610607
)
611608
return BeaconState(**sample_beacon_state_params).copy(
612-
validator_registry=genesis_validators,
609+
validator_registry=activated_genesis_validators,
613610
validator_balances=genesis_balances,
614611
shard_committees_at_slots=initial_shuffling + initial_shuffling,
615612
latest_block_roots=tuple(ZERO_HASH32 for _ in range(latest_block_roots_length)),
@@ -624,29 +621,35 @@ def genesis_state(sample_beacon_state_params,
624621

625622

626623
@pytest.fixture
627-
def genesis_validators(init_validator_pubkeys,
624+
def initial_validators(init_validator_pubkeys,
628625
init_randao,
629626
max_deposit,
630627
far_future_slot):
628+
"""
629+
Inactive
630+
"""
631631
return tuple(
632632
mock_validator_record(
633633
pubkey=pubkey,
634634
far_future_slot=far_future_slot,
635635
withdrawal_credentials=ZERO_HASH32,
636636
randao_commitment=init_randao,
637637
status_flags=0,
638+
is_active=False,
638639
)
639640
for pubkey in init_validator_pubkeys
640641
)
641642

642643

643644
@pytest.fixture
644-
@to_tuple
645-
def activated_genesis_validators(genesis_validators, genesis_slot, entry_exit_delay):
646-
for validator in genesis_validators:
647-
yield validator.copy(
648-
activation_slot=genesis_slot
649-
)
645+
def activated_genesis_validators(initial_validators, genesis_slot):
646+
"""
647+
Active
648+
"""
649+
validators = tuple()
650+
for validator in initial_validators:
651+
validators += (validator.copy(activation_slot=genesis_slot),)
652+
return validators
650653

651654

652655
@pytest.fixture

tests/beacon/test_helpers.py

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -256,24 +256,28 @@ def test_get_shard_committees_at_slot(
256256
'num_validators,'
257257
'epoch_length,'
258258
'target_committee_size,'
259-
'shard_count'
259+
'shard_count,'
260+
'slot'
260261
),
261262
[
262-
(1000, 20, 10, 100),
263-
(100, 50, 10, 10),
264-
(20, 10, 3, 10), # active_validators_size < epoch_length * target_committee_size
265-
# TODO: other slot cases
263+
(1000, 20, 10, 100, 0),
264+
(1000, 20, 10, 100, 5),
265+
(1000, 20, 10, 100, 30),
266+
(20, 10, 3, 10, 0), # active_validators_size < epoch_length * target_committee_size
267+
(20, 10, 3, 10, 5),
268+
(20, 10, 3, 10, 30),
266269
],
267270
)
268271
def test_get_shuffling_is_complete(activated_genesis_validators,
269272
epoch_length,
270273
target_committee_size,
271-
shard_count):
274+
shard_count,
275+
slot):
272276
shuffling = get_shuffling(
273277
seed=b'\x35' * 32,
274278
validators=activated_genesis_validators,
275279
crosslinking_start_shard=0,
276-
slot=0,
280+
slot=slot,
277281
epoch_length=epoch_length,
278282
target_committee_size=target_committee_size,
279283
shard_count=shard_count,
@@ -288,6 +292,7 @@ def test_get_shuffling_is_complete(activated_genesis_validators,
288292
for validator_index in shard_committee.committee:
289293
validators.add(validator_index)
290294

295+
assert len(activated_genesis_validators) > 0
291296
assert len(validators) == len(activated_genesis_validators)
292297

293298

@@ -304,13 +309,13 @@ def test_get_shuffling_is_complete(activated_genesis_validators,
304309
(20, 10, 3, 10),
305310
],
306311
)
307-
def test_get_shuffling_handles_shard_wrap(genesis_validators,
312+
def test_get_shuffling_handles_shard_wrap(activated_genesis_validators,
308313
epoch_length,
309314
target_committee_size,
310315
shard_count):
311316
shuffling = get_shuffling(
312317
seed=b'\x35' * 32,
313-
validators=genesis_validators,
318+
validators=activated_genesis_validators,
314319
crosslinking_start_shard=shard_count - 1,
315320
slot=0,
316321
epoch_length=epoch_length,
@@ -677,16 +682,16 @@ def _generate_some_indices(data, max_value_for_list):
677682

678683

679684
@given(st.data())
680-
def test_get_pubkey_for_indices(genesis_validators, data):
681-
max_value_for_list = len(genesis_validators) - 1
685+
def test_get_pubkey_for_indices(activated_genesis_validators, data):
686+
max_value_for_list = len(activated_genesis_validators) - 1
682687
indices = _generate_some_indices(data, max_value_for_list)
683-
pubkeys = get_pubkey_for_indices(genesis_validators, indices)
688+
pubkeys = get_pubkey_for_indices(activated_genesis_validators, indices)
684689

685690
assert len(indices) == len(pubkeys)
686691

687692
for index, pubkey in enumerate(pubkeys):
688693
validator_index = indices[index]
689-
assert genesis_validators[validator_index].pubkey == pubkey
694+
assert activated_genesis_validators[validator_index].pubkey == pubkey
690695

691696

692697
def _list_and_index(data, max_size=None, elements=st.integers()):
@@ -699,8 +704,10 @@ def _list_and_index(data, max_size=None, elements=st.integers()):
699704

700705

701706
@given(st.data())
702-
def test_generate_aggregate_pubkeys(genesis_validators, sample_slashable_vote_data_params, data):
703-
max_value_for_list = len(genesis_validators) - 1
707+
def test_generate_aggregate_pubkeys(activated_genesis_validators,
708+
sample_slashable_vote_data_params,
709+
data):
710+
max_value_for_list = len(activated_genesis_validators) - 1
704711
(indices, some_index) = _list_and_index(
705712
data,
706713
elements=st.integers(
@@ -718,13 +725,13 @@ def test_generate_aggregate_pubkeys(genesis_validators, sample_slashable_vote_da
718725

719726
votes = SlashableVoteData(**sample_slashable_vote_data_params)
720727

721-
keys = generate_aggregate_pubkeys(genesis_validators, votes)
728+
keys = generate_aggregate_pubkeys(activated_genesis_validators, votes)
722729
assert len(keys) == 2
723730

724731
(poc_0_key, poc_1_key) = keys
725732

726-
poc_0_keys = get_pubkey_for_indices(genesis_validators, custody_bit_0_indices)
727-
poc_1_keys = get_pubkey_for_indices(genesis_validators, custody_bit_1_indices)
733+
poc_0_keys = get_pubkey_for_indices(activated_genesis_validators, custody_bit_0_indices)
734+
poc_1_keys = get_pubkey_for_indices(activated_genesis_validators, custody_bit_1_indices)
728735

729736
assert bls.aggregate_pubkeys(poc_0_keys) == poc_0_key
730737
assert bls.aggregate_pubkeys(poc_1_keys) == poc_1_key
@@ -840,13 +847,13 @@ def _create_slashable_vote_data_messages(params):
840847
def test_verify_slashable_vote_data_signature(num_validators,
841848
privkeys,
842849
sample_beacon_state_params,
843-
genesis_validators,
850+
activated_genesis_validators,
844851
genesis_balances,
845852
sample_slashable_vote_data_params,
846853
sample_fork_data_params):
847854
state = BeaconState(**sample_beacon_state_params).copy(
848-
validator_registry=genesis_validators,
849-
validator_balances=genesis_balances,
855+
validator_registry=activated_genesis_validators,
856+
validator_balances=activated_genesis_validators,
850857
fork_data=ForkData(**sample_fork_data_params),
851858
)
852859

@@ -907,13 +914,13 @@ def test_verify_slashable_vote_data(num_validators,
907914
needs_fork_data,
908915
privkeys,
909916
sample_beacon_state_params,
910-
genesis_validators,
917+
activated_genesis_validators,
911918
genesis_balances,
912919
sample_slashable_vote_data_params,
913920
sample_fork_data_params,
914921
max_casper_votes):
915922
state = BeaconState(**sample_beacon_state_params).copy(
916-
validator_registry=genesis_validators,
923+
validator_registry=activated_genesis_validators,
917924
validator_balances=genesis_balances,
918925
fork_data=ForkData(**sample_fork_data_params),
919926
)

tests/beacon/types/test_validator_record.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,11 @@ def test_is_active(sample_validator_record_params,
3434
assert validator.is_active(slot) == expected
3535

3636

37-
def test_get_pending_validator():
37+
def test_get_pending_validator(far_future_slot):
3838
pubkey = 123
3939
withdrawal_credentials = b'\x11' * 32
4040
randao_commitment = b'\x22' * 32
4141
custody_commitment = b'\x33' * 32
42-
far_future_slot = 1000
4342

4443
validator = ValidatorRecord.get_pending_validator(
4544
pubkey=pubkey,

0 commit comments

Comments
 (0)