Skip to content

chore(storage): Add error handling on Store::get_forkchoice_store#362

Open
Aliemeka wants to merge 3 commits into
lambdaclass:mainfrom
Aliemeka:chore/add-error-handling-to-get_forkchoice_store
Open

chore(storage): Add error handling on Store::get_forkchoice_store#362
Aliemeka wants to merge 3 commits into
lambdaclass:mainfrom
Aliemeka:chore/add-error-handling-to-get_forkchoice_store

Conversation

@Aliemeka
Copy link
Copy Markdown

🗒️ Description / Motivation

Replace the assert_eq! in Store::get_forkchoice_store with a typed Result so callers see a descriptive error instead of a panic. The previous message ("block header doesn't match state's latest_block_header") gave no insight into which field actually differed, which made misconfigured anchor state/block pairs hard to diagnose. PR closes #360.

What Changed

  • crates/storage/Cargo.toml — add thiserror dependency.
  • crates/storage/src/store.rs — introduce pub enum GetForkchoiceStoreError with a HeaderMismatch { state_header, block_header } variant; change Store::get_forkchoice_store to return Result<Self, GetForkchoiceStoreError>; replace the assertion with an explicit comparison that returns Err; update the doc comment (# Panics# Errors).

Correctness / Behavior Guarantees

  • The validation rule is unchanged: anchor block header must equal the state's latest_block_header after zeroing state_root. Mismatches that previously aborted via assert_eq! now return Err(HeaderMismatch { … }), surfacing both full headers (boxed to keep the variant small) for diff-friendly debugging.
  • Store::from_anchor_state is untouched. Internal panics in init_store (state_root mismatch and storage backend operations) are intentionally left as-is — they represent infrastructure invariants shared between both constructors and are out of scope for this change.

Tests Added / Run

  • No new tests; the type change is enforced by the compiler and existing fork-choice / signature spec suites continue to exercise the success path.
  • Ran make fmt, make lint, cargo test --workspace --release (unit tests pass; spec tests require make leanSpec/fixtures for fixture generation)
  • Also ran make test

Related Issues / PRs

✅ Verification Checklist

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

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 12, 2026

Greptile Summary

This PR converts Store::get_forkchoice_store from a panicking function into one that returns Result<Self, GetForkchoiceStoreError>, surfacing a descriptive HeaderMismatch error (with both headers boxed) instead of an opaque assert_eq! abort.

  • crates/storage/src/store.rs: Introduces GetForkchoiceStoreError via thiserror, replaces assert_eq! with an explicit if returning Err(HeaderMismatch { … }), and updates the doc comment from # Panics to # Errors. The validation logic (zero state_root before comparison) is unchanged.
  • crates/storage/src/lib.rs: Re-exports GetForkchoiceStoreError so downstream crates can pattern-match the variant.
  • crates/blockchain/…: All three call sites (unit test, forkchoice spec tests, signature spec tests) updated to .expect("anchor state and block must match"), preserving the panic behavior in test/internal contexts where a mismatch is always a programmer error.

Confidence Score: 5/5

Safe to merge — the change is a straightforward replacement of a single assert_eq! with a typed Result return; no validation logic is altered.

The diff is narrow and mechanical: one assertion becomes an if-guard returning Err, one new error enum is introduced with thiserror, and every call site is updated. The zeroing of state_root before comparison — the only subtlety in the original code — is preserved exactly. The remaining assert! calls inside init_store are explicitly out of scope and untouched.

No files require special attention.

Important Files Changed

Filename Overview
crates/storage/src/store.rs Introduces GetForkchoiceStoreError::HeaderMismatch and converts get_forkchoice_store from a panicking function to Result<Self, GetForkchoiceStoreError>; logic is unchanged.
crates/storage/src/lib.rs Re-exports the new GetForkchoiceStoreError type so downstream crates can match on the error variant.
crates/storage/Cargo.toml Adds thiserror as a workspace dependency to support the new error derive macro.
crates/blockchain/src/store.rs Updates the internal unit test call site to use .expect(...) on the now-fallible get_forkchoice_store.
crates/blockchain/tests/forkchoice_spectests.rs Updates spec-test harness call site to .expect(...) on the fallible constructor.
crates/blockchain/tests/signature_spectests.rs Updates signature spec-test harness call site to .expect(...) on the fallible constructor.

Sequence Diagram

sequenceDiagram
    participant Caller
    participant get_forkchoice_store
    participant init_store

    Caller->>get_forkchoice_store: (backend, anchor_state, anchor_block)
    get_forkchoice_store->>get_forkchoice_store: zero state_root on both headers
    alt headers match
        get_forkchoice_store->>init_store: (backend, anchor_state, Some(anchor_block.body))
        init_store-->>get_forkchoice_store: Store
        get_forkchoice_store-->>Caller: Ok(Store)
    else headers differ
        get_forkchoice_store-->>Caller: "Err(HeaderMismatch { state_header, block_header })"
    end
Loading

Reviews (1): Last reviewed commit: "chore(storage): Add error handling on `S..." | Re-trigger Greptile

@pablodeymo
Copy link
Copy Markdown
Collaborator

Hi @Aliemeka. Thanks for your contribution.

You must sign your commits. You can check that in here:
https://github.com/lambdaclass/ethlambda/blob/main/CONTRIBUTING.md#commit-signature-verification

@Aliemeka Aliemeka force-pushed the chore/add-error-handling-to-get_forkchoice_store branch from ff86b5b to f0743c6 Compare May 12, 2026 20:07
@Aliemeka Aliemeka force-pushed the chore/add-error-handling-to-get_forkchoice_store branch from f0743c6 to 6e362d0 Compare May 12, 2026 20:08
@Aliemeka
Copy link
Copy Markdown
Author

So sorry for force pushing. I just got confused about signing my commit

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.

Add error handling on Store::get_forkchoice_store

3 participants