Skip to content

starknet_api: avoid double-parse in TryFrom<ProofFacts> for SnosProofFacts error path#14611

Open
gkaempfer wants to merge 3 commits into
main-v0.14.3from
claude/loving-pascal-7efrfo
Open

starknet_api: avoid double-parse in TryFrom<ProofFacts> for SnosProofFacts error path#14611
gkaempfer wants to merge 3 commits into
main-v0.14.3from
claude/loving-pascal-7efrfo

Conversation

@gkaempfer

Copy link
Copy Markdown
Contributor

Summary

Follow-up optimization to #14598.

PR #14598 introduced a custom Debug impl for ProofFacts that internally calls ProofFactsVariant::try_from. This exposed a latent inefficiency in TryFrom<ProofFacts> for SnosProofFacts: its error arm used {proof_facts:?} in the format string, which now triggers a redundant second call to ProofFactsVariant::try_from — the inner error from the first call was being discarded and the data re-parsed just to build the error message.

Change

  • Replace the wildcard _ error arm with explicit matching: propagate the inner Err via ? (reusing its already-descriptive message), and handle the Empty variant separately with a clear message.
  • Add three tests: success path, empty-input rejection, and inner-error propagation (ensuring the generic wrapper string is no longer produced).

Before / After

Before — error path triggered two parses:

match ProofFactsVariant::try_from(&proof_facts) {
    Ok(ProofFactsVariant::Snos(snos)) => Ok(snos),
    _ => Err(StarknetApiError::InvalidProofFacts(format!(
        "Invalid SNOS proof facts: {proof_facts:?}",  // ← second try_from here
    ))),
}

After — inner error propagated directly:

match ProofFactsVariant::try_from(&proof_facts)? {
    ProofFactsVariant::Snos(snos) => Ok(snos),
    ProofFactsVariant::Empty => Err(StarknetApiError::InvalidProofFacts(
        "expected SNOS proof facts, got empty".to_string(),
    )),
}

Expected impact

Eliminates one redundant ProofFactsVariant::try_from call on every error path through TryFrom<ProofFacts> for SnosProofFacts. Also produces more specific error messages (the inner parse error explains exactly what was wrong, rather than a generic wrapper).

Test plan

RUSTC_WRAPPER="" SEED=0 cargo test -p starknet_api --features testing -- fields_test

All 9 tests pass, including 3 new ones added by this PR.


Generated by Claude Code

AvivYossef-starkware and others added 2 commits June 23, 2026 12:03
…Facts error path

After #14598 introduced a custom Debug impl for ProofFacts that calls
ProofFactsVariant::try_from internally, the error arm of
TryFrom<ProofFacts> for SnosProofFacts was triggering a redundant second
parse via the {proof_facts:?} format argument. Propagate the inner error
from the first try_from call directly instead, and handle the Empty
variant explicitly with a descriptive message. Also adds tests covering
the success path, the empty-input rejection, and the inner-error
propagation.
@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

…tion

- Capitalize 'Expected SNOS proof facts, got empty' to match the style
  of all other InvalidProofFacts messages in this file.
- Rename test to drop the unverifiable 'without_reparsing' claim and
  add a positive assertion that the propagated inner error message
  contains 'Expected first field', pinning the actual content rather
  than only checking absence of the old wrapper string.
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.

4 participants