Skip to content

Commit c01bfd8

Browse files
authored
state helper: rm useless get_justifications_map (leanEthereum#179)
1 parent 41b4048 commit c01bfd8

2 files changed

Lines changed: 30 additions & 44 deletions

File tree

src/lean_spec/subspecs/containers/state/helpers.py

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,6 @@
99
from lean_spec.types import Boolean, Bytes32
1010

1111

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-
4912
def flatten_justifications_map(
5013
justifications_map: Dict[Bytes32, List[Boolean]], validator_count: int
5114
) -> tuple[JustificationRoots, JustificationValidators]:

src/lean_spec/subspecs/containers/state/state.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from ..checkpoint import Checkpoint
2323
from ..config import Config
2424
from ..slot import Slot
25-
from .helpers import flatten_justifications_map, get_justifications_map
25+
from .helpers import flatten_justifications_map
2626
from .types import (
2727
HistoricalBlockHashes,
2828
JustificationRoots,
@@ -337,12 +337,35 @@ def process_attestations(
337337
State
338338
A new state with updated justification/finalization.
339339
"""
340-
# Get justifications, justified slots and historical block hashes are already up to
341-
# date as per the processing in process_block_header
342-
justifications = get_justifications_map(
343-
justifications_roots=self.justifications_roots,
344-
justifications_validators=self.justifications_validators,
345-
validator_count=self.validators.count,
340+
# NOTE:
341+
# The state already contains three pieces of data:
342+
# 1. A list of block roots that have received justification votes.
343+
# 2. A long sequence of boolean entries representing all validator votes,
344+
# flattened into a single list.
345+
# 3. The total number of validators.
346+
#
347+
# The flattened vote list is organized so that votes from all validators for
348+
# each block root appear together, and those groups are simply placed back-to-back.
349+
#
350+
# To work with attestations, we must rebuild the intuitive structure:
351+
# “for each block root, here is the list of validator votes for it”.
352+
#
353+
# Reconstructing this is done by cutting the long vote list into consecutive
354+
# segments, where:
355+
# - each segment corresponds to one block root,
356+
# - each segment has length equal to the number of validators,
357+
# - and the ordering of block roots is preserved.
358+
flat_justifications = list(self.justifications_validators)
359+
360+
justifications = (
361+
{
362+
root: flat_justifications[
363+
i * self.validators.count : (i + 1) * self.validators.count
364+
]
365+
for i, root in enumerate(self.justifications_roots)
366+
}
367+
if self.justifications_roots
368+
else {}
346369
)
347370

348371
# Track state changes to be applied at the end

0 commit comments

Comments
 (0)