Document process_slots jump-to-target equivalence with spec#213
Document process_slots jump-to-target equivalence with spec#213MegaRedHand merged 1 commit intomainfrom
Conversation
The spec iterates slot-by-slot in a while loop, but the code jumps directly to target_slot. This is equivalent because the only side effect beyond slot increment is state root caching on the first iteration (when state_root is zero). Add a doc comment explaining the equivalence proof and noting it must be revisited if the spec adds per-slot side effects.
🤖 Kimi Code ReviewReview SummaryThe optimization in Critical IssuesFile:
Example Bug ScenarioIf
This breaks consensus because validators' balances and the beacon state's justification/finalization won't be updated correctly. Required FixRevert to the spec-compliant implementation that processes slots sequentially: pub fn process_slots(state: &mut State, target_slot: u64) -> Result<(), Error> {
let _timing = metrics::time_slots_processing();
while state.slot < target_slot {
process_slot(state)?;
if (state.slot + 1) % SLOTS_PER_EPOCH == 0 {
process_epoch(state)?;
}
state.slot += 1;
}
Ok(())
}The current optimization is unsafe and must be removed entirely. Automated review by Kimi (Moonshot AI) · custom prompt |
🤖 Codex Code ReviewNo blocking findings.
Residual risk/testing gap: since this is documentation-only, there’s no runtime regression risk from the patch itself; the only risk is comment/spec drift over time. Automated review by OpenAI Codex · custom prompt |
🤖 Claude Code ReviewReview: Document
|
Greptile SummaryThis PR is a documentation-only change that adds a doc comment to Key changes:
Confidence Score: 5/5
|
| Filename | Overview |
|---|---|
| crates/blockchain/state_transition/src/lib.rs | Documentation-only change: adds a doc comment to process_slots explaining the spec-equivalence proof for the slot-jump optimisation, and removes a now-redundant inline comment. No logic altered. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["process_slots(state, target_slot)"] --> B{state.slot >= target_slot?}
B -->|Yes| C[Return Err: StateSlotIsNewer]
B -->|No| D{latest_block_header.state_root == ZERO?}
D -->|Yes| E["Compute & cache state root\n(tree_hash_root of state at current slot)"]
D -->|No| F[Skip — already cached]
E --> G["Jump: state.slot = target_slot\n(equivalent to spec's slot-by-slot loop)"]
F --> G
G --> H[Increment metrics counter]
H --> I[Return Ok]
subgraph spec_loop ["Spec's while loop (equivalent)"]
S1["Iteration 1: check state_root == ZERO\n→ compute hash, then slot += 1"]
S2["Iterations 2..N: state_root != ZERO\n→ only slot += 1"]
S1 --> S2
end
G -.->|"logically equivalent to"| spec_loop
Prompt To Fix All With AI
This is a comment left during a code review.
Path: crates/blockchain/state_transition/src/lib.rs
Line: 70-71
Comment:
**Missing spec URL for verifiability**
The doc comment references `state.py process_slots` by name only, while the adjacent `state_transition` function pins its spec reference with a full permalink (line 39). Without a URL, future readers have no way to quickly check whether the loop-body invariants (especially point 3: "no other state fields are modified") still hold after a spec update.
Consider adding a pinned URL, e.g.:
```suggestion
/// The spec (`state.py` process_slots, https://github.com/leanEthereum/leanSpec/blob/bf0f606a75095cf1853529bc770516b1464d9716/src/lean_spec/subspecs/containers/state/state.py)
/// iterates slot-by-slot in a while loop.
```
How can I resolve this? If you propose a fix, please make it concise.Last reviewed commit: ddeb011
Motivation
A spec-to-code compliance audit (FINDING-001) flagged that
process_slotsjumps directly totarget_slotinstead of iterating slot-by-slot as the spec does. Verification confirmed this is functionally equivalent — not a divergence — but the code lacked any explanation of why.Description
Adds a doc comment on
process_slotsexplaining the equivalence proof:state_root == ZERO)Also notes that this must be revisited if the spec ever adds per-slot side effects.
How to Test
Documentation-only change.
cargo test --workspace --releasepasses.