|
6 | 6 | from eth.beacon.types.attestation_records import AttestationRecord
|
7 | 7 | from eth.beacon.types.blocks import BaseBeaconBlock
|
8 | 8 | from eth.beacon.types.shard_committees import ShardCommittee
|
| 9 | +from eth.beacon.types.states import BeaconState |
9 | 10 | from eth.beacon.types.validator_records import ValidatorRecord
|
10 | 11 | from eth.beacon.helpers import (
|
11 | 12 | _get_element_from_recent_list,
|
12 | 13 | get_active_validator_indices,
|
13 | 14 | get_attestation_indices,
|
| 15 | + get_beacon_proposer_index, |
14 | 16 | get_block_hash,
|
15 | 17 | get_hashes_from_latest_block_hashes,
|
16 | 18 | get_hashes_to_sign,
|
|
25 | 27 | )
|
26 | 28 |
|
27 | 29 |
|
| 30 | +@pytest.fixture() |
| 31 | +def sample_block(sample_beacon_block_params): |
| 32 | + return BaseBeaconBlock(**sample_beacon_block_params) |
| 33 | + |
| 34 | + |
| 35 | +@pytest.fixture() |
| 36 | +def sample_state(sample_beacon_state_params): |
| 37 | + return BeaconState(**sample_beacon_state_params) |
| 38 | + |
| 39 | + |
28 | 40 | def get_sample_shard_committees_at_slots(num_slot,
|
29 | 41 | num_shard_committee_per_slot,
|
30 | 42 | sample_shard_committee_params):
|
@@ -107,9 +119,7 @@ def test_get_block_hash(
|
107 | 119 | target_slot,
|
108 | 120 | success,
|
109 | 121 | epoch_length,
|
110 |
| - sample_beacon_block_params): |
111 |
| - sample_block = BaseBeaconBlock(**sample_beacon_block_params) |
112 |
| - |
| 122 | + sample_block): |
113 | 123 | blocks, latest_block_hashes = generate_mock_latest_block_hashes(
|
114 | 124 | sample_block,
|
115 | 125 | current_block_number,
|
@@ -476,6 +486,75 @@ def mock_get_shard_committees_at_slot(parent_block,
|
476 | 486 | )
|
477 | 487 |
|
478 | 488 |
|
| 489 | +@pytest.mark.parametrize( |
| 490 | + ( |
| 491 | + 'num_validators,' |
| 492 | + 'cycle_length,' |
| 493 | + 'committee,' |
| 494 | + 'slot,' |
| 495 | + 'success,' |
| 496 | + ), |
| 497 | + [ |
| 498 | + ( |
| 499 | + 100, |
| 500 | + 64, |
| 501 | + (10, 11, 12), |
| 502 | + 0, |
| 503 | + True, |
| 504 | + ), |
| 505 | + ( |
| 506 | + 100, |
| 507 | + 64, |
| 508 | + (), |
| 509 | + 0, |
| 510 | + False, |
| 511 | + ), |
| 512 | + ] |
| 513 | +) |
| 514 | +def test_get_beacon_proposer_index( |
| 515 | + monkeypatch, |
| 516 | + num_validators, |
| 517 | + cycle_length, |
| 518 | + committee, |
| 519 | + slot, |
| 520 | + success, |
| 521 | + epoch_length, |
| 522 | + sample_state): |
| 523 | + |
| 524 | + from eth.beacon import helpers |
| 525 | + |
| 526 | + def mock_get_shard_committees_at_slot(state, |
| 527 | + slot, |
| 528 | + epoch_length): |
| 529 | + return ( |
| 530 | + ShardCommittee( |
| 531 | + shard=1, |
| 532 | + committee=committee, |
| 533 | + total_validator_count=num_validators, |
| 534 | + ), |
| 535 | + ) |
| 536 | + |
| 537 | + monkeypatch.setattr( |
| 538 | + helpers, |
| 539 | + 'get_shard_committees_at_slot', |
| 540 | + mock_get_shard_committees_at_slot |
| 541 | + ) |
| 542 | + if success: |
| 543 | + proposer_index = get_beacon_proposer_index( |
| 544 | + sample_state, |
| 545 | + slot, |
| 546 | + epoch_length |
| 547 | + ) |
| 548 | + assert proposer_index == committee[slot % len(committee)] |
| 549 | + else: |
| 550 | + with pytest.raises(ValueError): |
| 551 | + get_beacon_proposer_index( |
| 552 | + sample_state, |
| 553 | + slot, |
| 554 | + epoch_length |
| 555 | + ) |
| 556 | + |
| 557 | + |
479 | 558 | def test_get_active_validator_indices(sample_validator_record_params):
|
480 | 559 | # 3 validators are ACTIVE by default.
|
481 | 560 | validators = [
|
|
0 commit comments