|
1 | 1 | """Store checks model for selective validation in fork choice tests.""" |
2 | 2 |
|
3 | | -from typing import TYPE_CHECKING |
| 3 | +from typing import TYPE_CHECKING, Literal |
4 | 4 |
|
5 | 5 | from lean_spec.subspecs.containers.slot import Slot |
6 | | -from lean_spec.types import Bytes32, CamelModel, Uint64 |
| 6 | +from lean_spec.types import Bytes32, CamelModel, Uint64, ValidatorIndex |
7 | 7 |
|
8 | 8 | if TYPE_CHECKING: |
| 9 | + from lean_spec.subspecs.containers import SignedAttestation |
9 | 10 | from lean_spec.subspecs.forkchoice.store import Store |
10 | 11 |
|
11 | 12 |
|
| 13 | +class AttestationCheck(CamelModel): |
| 14 | + """ |
| 15 | + Validation checks for a specific validator's attestation. |
| 16 | +
|
| 17 | + All fields optional - only check fields explicitly set. |
| 18 | + Used to validate attestation content beyond just counting. |
| 19 | + """ |
| 20 | + |
| 21 | + validator: ValidatorIndex |
| 22 | + """Which validator's attestation to check.""" |
| 23 | + |
| 24 | + attestation_slot: Slot | None = None |
| 25 | + """Expected attestation data slot.""" |
| 26 | + |
| 27 | + head_slot: Slot | None = None |
| 28 | + """Expected head checkpoint slot.""" |
| 29 | + |
| 30 | + source_slot: Slot | None = None |
| 31 | + """Expected source checkpoint slot.""" |
| 32 | + |
| 33 | + target_slot: Slot | None = None |
| 34 | + """Expected target checkpoint slot.""" |
| 35 | + |
| 36 | + location: Literal["new", "known"] |
| 37 | + """ |
| 38 | + Expected attestation location: |
| 39 | + - "new" for `latest_new_attestations` |
| 40 | + - "known" for `latest_known_attestations` |
| 41 | + """ |
| 42 | + |
| 43 | + def validate_attestation( |
| 44 | + self, attestation: "SignedAttestation", location: str, step_index: int |
| 45 | + ) -> None: |
| 46 | + """Validate attestation properties.""" |
| 47 | + fields_to_check = self.model_fields_set - {"validator", "location"} |
| 48 | + |
| 49 | + for field_name in fields_to_check: |
| 50 | + expected = getattr(self, field_name) |
| 51 | + |
| 52 | + if field_name == "attestation_slot": |
| 53 | + actual = attestation.message.data.slot |
| 54 | + if actual != expected: |
| 55 | + raise AssertionError( |
| 56 | + f"Step {step_index}: validator {self.validator} {location} " |
| 57 | + f"attestation slot = {actual}, expected {expected}" |
| 58 | + ) |
| 59 | + |
| 60 | + elif field_name == "head_slot": |
| 61 | + actual = attestation.message.data.head.slot |
| 62 | + if actual != expected: |
| 63 | + raise AssertionError( |
| 64 | + f"Step {step_index}: validator {self.validator} {location} " |
| 65 | + f"head slot = {actual}, expected {expected}" |
| 66 | + ) |
| 67 | + |
| 68 | + elif field_name == "source_slot": |
| 69 | + actual = attestation.message.data.source.slot |
| 70 | + if actual != expected: |
| 71 | + raise AssertionError( |
| 72 | + f"Step {step_index}: validator {self.validator} {location} " |
| 73 | + f"source slot = {actual}, expected {expected}" |
| 74 | + ) |
| 75 | + |
| 76 | + elif field_name == "target_slot": |
| 77 | + actual = attestation.message.data.target.slot |
| 78 | + if actual != expected: |
| 79 | + raise AssertionError( |
| 80 | + f"Step {step_index}: validator {self.validator} {location} " |
| 81 | + f"target slot = {actual}, expected {expected}" |
| 82 | + ) |
| 83 | + |
| 84 | + |
12 | 85 | class StoreChecks(CamelModel): |
13 | 86 | """ |
14 | 87 | Store state checks for fork choice tests. |
@@ -51,6 +124,9 @@ class StoreChecks(CamelModel): |
51 | 124 | safe_target: Bytes32 | None = None |
52 | 125 | """Expected safe target root.""" |
53 | 126 |
|
| 127 | + attestation_checks: list[AttestationCheck] | None = None |
| 128 | + """Optional list of attestation content checks for specific validators.""" |
| 129 | + |
54 | 130 | def validate_against_store(self, store: "Store", step_index: int) -> None: |
55 | 131 | """ |
56 | 132 | Validate these checks against actual Store state. |
@@ -137,3 +213,27 @@ def validate_against_store(self, store: "Store", step_index: int) -> None: |
137 | 213 | f"Step {step_index}: safe_target = 0x{actual_root.hex()}, " |
138 | 214 | f"expected 0x{expected_value.hex()}" |
139 | 215 | ) |
| 216 | + |
| 217 | + elif field_name == "attestation_checks": |
| 218 | + # Validate specific attestation contents |
| 219 | + for check in expected_value: |
| 220 | + validator_idx = check.validator |
| 221 | + |
| 222 | + # Check attestation location |
| 223 | + if check.location == "new": |
| 224 | + if validator_idx not in store.latest_new_attestations: |
| 225 | + raise AssertionError( |
| 226 | + f"Step {step_index}: validator {validator_idx} not found " |
| 227 | + f"in latest_new_attestations" |
| 228 | + ) |
| 229 | + attestation = store.latest_new_attestations[validator_idx] |
| 230 | + check.validate_attestation(attestation, "in latest_new", step_index) |
| 231 | + |
| 232 | + else: # check.location == "known" |
| 233 | + if validator_idx not in store.latest_known_attestations: |
| 234 | + raise AssertionError( |
| 235 | + f"Step {step_index}: validator {validator_idx} not found " |
| 236 | + f"in latest_known_attestations" |
| 237 | + ) |
| 238 | + attestation = store.latest_known_attestations[validator_idx] |
| 239 | + check.validate_attestation(attestation, "in latest_known", step_index) |
0 commit comments