Skip to content

refactor(blockchain): use Store::head_state in update_safe_target - #571

Merged
MegaRedHand merged 3 commits into
mainfrom
refactor/safe-target-head-state
Aug 7, 2026
Merged

refactor(blockchain): use Store::head_state in update_safe_target#571
MegaRedHand merged 3 commits into
mainfrom
refactor/safe-target-head-state

Conversation

@MegaRedHand

Copy link
Copy Markdown
Collaborator

🗒️ Description / Motivation

Extracted from #561. Independent of the heartbeat work, so it lands on its own.

update_safe_target open-coded the get_state(&head()) chain that Store::head_state() already encapsulates, carrying an extra unwrap for no reason.

What Changed

  • crates/blockchain/src/store.rsupdate_safe_target reads the validator count through store.head_state().

Correctness / Behavior Guarantees

Equivalent, checked rather than assumed: Store::head_state() (crates/storage/src/store.rs:1696) is exactly self.get_state(&self.head().expect(..)).expect(..).unwrap() — the same Result→panic, Result→panic, Option→panic chain the inline version had. Only the expect messages differ.

This is the only site in the codebase that open-coded the chain.

Tests Added / Run

No new tests — no behavior change to cover.

make fmt
make lint
make leanSpec/fixtures     # fixtures were absent in the worktree
cargo test --workspace --profile release-fast --no-fail-fast

Related Issues / PRs

✅ Verification Checklist

  • Ran make fmt — clean
  • Ran make lint (clippy with -D warnings) — clean
  • Ran make test (cargo test --workspace --profile release-fast) — all passing

The accessor already exists and does exactly this get_state(&head()) chain, so
the open-coded version was carrying a redundant unwrap for no reason.
@greptile-apps

greptile-apps Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR simplifies safe-target calculation by using the existing Store::head_state() accessor instead of open-coding the same head-state retrieval chain.

  • Replaces explicit head(), get_state(), and unwrap operations in update_safe_target.
  • Preserves validator-count and safe-target threshold behavior.

Confidence Score: 5/5

The PR appears safe to merge because the refactor preserves the existing head-state lookup and validator-threshold calculation.

The changed expression delegates the same head-state retrieval to the existing accessor without altering safe-target inputs, control flow, or state mutation.

Important Files Changed

Filename Overview
crates/blockchain/src/store.rs Refactors update_safe_target to retrieve the current head state through the equivalent storage accessor; no behavioral issue identified.

Reviews (1): Last reviewed commit: "refactor(blockchain): use Store::head_st..." | Re-trigger Greptile

@github-actions

github-actions Bot commented Aug 6, 2026

Copy link
Copy Markdown

🤖 Kimi Code Review

Review of PR #571

crates/blockchain/src/store.rs

This is a straightforward refactoring that improves readability by replacing manual head state lookup with a helper method. No consensus logic changes.

Observations:

  1. Panic Safety (Lines 151-152): The new store.head_state() call consolidates three potential panic points (head().unwrap(), get_state().expect(), and head_state.unwrap()) into one. Verify that head_state() panics with a descriptive message if the invariant is violated (e.g., "head state must exist in store"). If it returns an Option, this code will fail to compile, which would be caught by CI.

  2. Performance: Ensure head_state() returns a reference (&BeaconState) rather than cloning the state. States are large (validator sets), and cloning would be a significant regression. The usage pattern (immediate .validators.len()) suggests it returns a reference, but confirm the method signature doesn't return an owned value or Arc clone unnecessarily.

  3. Correctness: The calculation (num_validators * 2).div_ceil(3) correctly implements the 2/3 Byzantine threshold required for the safe target rule. This matches the consensus spec for the "safety against live participation collapse" mechanism referenced in the comment.

Verdict: LGTM. The change reduces unwrap nesting and improves maintainability. No security issues introduced.


Automated review by Kimi (Moonshot AI) · kimi-k2.5 · custom prompt

@github-actions

github-actions Bot commented Aug 6, 2026

Copy link
Copy Markdown

🤖 Codex Code Review

No findings.

crates/blockchain/src/store.rs:151 is a pure refactor to Store::head_state(), and crates/storage/src/store.rs:1696-1699 is functionally the same sequence as the deleted code: resolve current head, load that state, and panic if it is unexpectedly missing. I don’t see any change to fork-choice inputs, safe-target scoring, attestation handling, SSZ behavior, or signature verification.

The only observable effect is readability: the helper centralizes the invariant that the head state must exist. Performance is unchanged in practice because both paths still clone/load the full State before taking validators.len().


Automated review by OpenAI Codex · gpt-5.4 · custom prompt

@github-actions

github-actions Bot commented Aug 6, 2026

Copy link
Copy Markdown

🤖 Claude Code Review

This is a clean, minimal, low-risk refactor. No functional changes, no correctness concerns.

Review

Summary: This PR replaces a 4-line open-coded get_state(&head()) chain in update_safe_target with the equivalent Store::head_state() accessor. The diff is a straight simplification: -4/+1 lines.

Correctness: Verified. Store::head_state() (crates/storage/src/store.rs:1696-1700) performs the identical get_state(&self.head().expect(...)).expect(...).unwrap() chain that was previously open-coded in update_safe_target. Panic behavior is unchanged; only the expect message text differs ("head state exists" / implicit unwrap panic vs. "head block exists" / "head state is always available"). No behavioral change.

Style/idioms: Matches the codebase's stated preference for reusing existing accessors over re-deriving them, and removes a redundant unwrap as noted in the description. No new imports needed since Store was already imported.

Scope: Tight and well-justified — this is the only site in the codebase that had open-coded this chain, so there's no remaining duplication to clean up elsewhere.

No issues found. This is a good, safe cleanup — approve as-is.


Automated review by Claude (Anthropic) · sonnet · custom prompt

@MegaRedHand
MegaRedHand merged commit 0b10743 into main Aug 7, 2026
2 checks passed
@MegaRedHand
MegaRedHand deleted the refactor/safe-target-head-state branch August 7, 2026 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants