|
| 1 | +"""Helpers for the State container.""" |
| 2 | + |
| 3 | +from typing import Dict, List |
| 4 | + |
| 5 | +from lean_spec.subspecs.containers.state.types import ( |
| 6 | + JustificationRoots, |
| 7 | + JustificationValidators, |
| 8 | +) |
| 9 | +from lean_spec.types import Boolean, Bytes32 |
| 10 | + |
| 11 | + |
| 12 | +def get_justifications_map( |
| 13 | + justifications_roots: JustificationRoots, |
| 14 | + justifications_validators: JustificationValidators, |
| 15 | + validator_count: int, |
| 16 | +) -> Dict[Bytes32, List[Boolean]]: |
| 17 | + """ |
| 18 | + Reconstruct the justifications map from the state's flat data structures. |
| 19 | +
|
| 20 | + Parameters |
| 21 | + ---------- |
| 22 | + justifications_roots : JustificationRoots |
| 23 | + The block roots in alphabetical order. |
| 24 | + justifications_validators : JustificationValidators |
| 25 | + The list of validator justifications for each block root concatenated in the same order |
| 26 | + as the list of block roots. |
| 27 | + validator_count : int |
| 28 | + The number of validators in the state. |
| 29 | +
|
| 30 | + Returns: |
| 31 | + ------- |
| 32 | + Dict[Bytes32, List[Boolean]] |
| 33 | + A mapping from a block root to the list of validator justifications for that root. |
| 34 | + """ |
| 35 | + # No justified roots means no justifications to reconstruct. |
| 36 | + if not justifications_roots: |
| 37 | + return {} |
| 38 | + |
| 39 | + # Extract the flattened validator justifications. |
| 40 | + flat_justifications = list(justifications_validators) |
| 41 | + |
| 42 | + # Reconstruct the map: each root gets its corresponding justification slice. |
| 43 | + return { |
| 44 | + root: flat_justifications[i * validator_count : (i + 1) * validator_count] |
| 45 | + for i, root in enumerate(justifications_roots) |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | +def flatten_justifications_map( |
| 50 | + justifications_map: Dict[Bytes32, List[Boolean]], validator_count: int |
| 51 | +) -> tuple[JustificationRoots, JustificationValidators]: |
| 52 | + """ |
| 53 | + Flatten a map of validator justifications into the state's flat data structures |
| 54 | + for SSZ compatibility. |
| 55 | +
|
| 56 | + Parameters |
| 57 | + ---------- |
| 58 | + justifications_map : Dict[Bytes32, List[Boolean]] |
| 59 | + A mapping from a block root to the list of validator justifications for that root. |
| 60 | + validator_count : int |
| 61 | + The number of validators in the state. |
| 62 | +
|
| 63 | + Returns: |
| 64 | + ------- |
| 65 | + JustificationRoots |
| 66 | + The block roots in alphabetical order. |
| 67 | + JustificationValidators |
| 68 | + The list of validator justifications for each block root concatenated in the same order |
| 69 | + as the list of block roots. |
| 70 | + """ |
| 71 | + # Build the flattened lists from the map, with sorted keys for deterministic order. |
| 72 | + roots_list = [] |
| 73 | + justifications_list = [] |
| 74 | + |
| 75 | + for root in sorted(justifications_map.keys()): |
| 76 | + justifications = justifications_map[root] |
| 77 | + |
| 78 | + # Validate that the justifications list has the expected length. |
| 79 | + if len(justifications) != validator_count: |
| 80 | + raise AssertionError(f"Justifications list for root {root.hex()} has incorrect length") |
| 81 | + |
| 82 | + # Add the root to the roots list. |
| 83 | + roots_list.append(root) |
| 84 | + # Extend the flattened list with the justifications for this root. |
| 85 | + justifications_list.extend(justifications) |
| 86 | + |
| 87 | + # Return a new state object with the updated fields. |
| 88 | + return JustificationRoots(data=roots_list), JustificationValidators(data=justifications_list) |
0 commit comments