|
| 1 | +""" |
| 2 | +Tests for the State container. |
| 3 | +""" |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from lean_spec.subspecs.containers import ( |
| 8 | + BlockHeader, |
| 9 | + Checkpoint, |
| 10 | + Config, |
| 11 | + State, |
| 12 | +) |
| 13 | +from lean_spec.types import Bytes32, Uint64, ValidatorIndex |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def sample_config() -> Config: |
| 18 | + """A sample configuration with 10 validators.""" |
| 19 | + return Config(num_validators=10, genesis_time=0) |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def sample_block_header() -> BlockHeader: |
| 24 | + """A sample, empty block header.""" |
| 25 | + return BlockHeader( |
| 26 | + slot=0, |
| 27 | + proposer_index=0, |
| 28 | + parent_root=Bytes32(b"\x00" * 32), |
| 29 | + state_root=Bytes32(b"\x00" * 32), |
| 30 | + body_root=Bytes32(b"\x00" * 32), |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture |
| 35 | +def sample_checkpoint() -> Checkpoint: |
| 36 | + """A sample, empty checkpoint.""" |
| 37 | + return Checkpoint(root=Bytes32(b"\x00" * 32), slot=0) |
| 38 | + |
| 39 | + |
| 40 | +def test_is_proposer( |
| 41 | + sample_config: Config, |
| 42 | + sample_block_header: BlockHeader, |
| 43 | + sample_checkpoint: Checkpoint, |
| 44 | +) -> None: |
| 45 | + """ |
| 46 | + Test the `is_proposer` method with various slots and validator indices. |
| 47 | + """ |
| 48 | + |
| 49 | + def create_state_at_slot(slot: int) -> State: |
| 50 | + """Create a state object at a given slot.""" |
| 51 | + return State( |
| 52 | + config=sample_config, |
| 53 | + slot=Uint64(slot), |
| 54 | + latest_block_header=sample_block_header, |
| 55 | + latest_justified=sample_checkpoint, |
| 56 | + latest_finalized=sample_checkpoint, |
| 57 | + historical_block_hashes=[], |
| 58 | + justified_slots=[], |
| 59 | + justifications_roots=[], |
| 60 | + justifications_validators=[], |
| 61 | + ) |
| 62 | + |
| 63 | + # Slot 0 |
| 64 | + state_slot_0 = create_state_at_slot(0) |
| 65 | + assert state_slot_0.is_proposer(ValidatorIndex(0)) is True |
| 66 | + assert state_slot_0.is_proposer(ValidatorIndex(1)) is False |
| 67 | + |
| 68 | + # Slot 7 |
| 69 | + state_slot_7 = create_state_at_slot(7) |
| 70 | + assert state_slot_7.is_proposer(ValidatorIndex(7)) is True |
| 71 | + assert state_slot_7.is_proposer(ValidatorIndex(8)) is False |
| 72 | + |
| 73 | + # Slot 10 (wraps around) |
| 74 | + state_slot_10 = create_state_at_slot(10) |
| 75 | + assert state_slot_10.is_proposer(ValidatorIndex(0)) is True |
| 76 | + assert state_slot_10.is_proposer(ValidatorIndex(1)) is False |
| 77 | + |
| 78 | + # Slot 23 (wraps around) |
| 79 | + state_slot_23 = create_state_at_slot(23) |
| 80 | + assert state_slot_23.is_proposer(ValidatorIndex(3)) is True |
| 81 | + assert state_slot_23.is_proposer(ValidatorIndex(2)) is False |
0 commit comments