Skip to content

Improve DataStore::get_note_script API #2029

@partylikeits1983

Description

@partylikeits1983

What should be done?

Follow up to #1995

Opening this issue as a result of: #1995 (comment)

The current DataStore::get_note_script API returns Result<NoteScript, DataStoreError>, which makes it difficult to distinguish between "script not found" errors and other internal data store errors.

Current Implementation

fn get_note_script(
    &self,
    script_root: Word,
) -> impl FutureMaybeSend<Result<NoteScript, DataStoreError>>;

When calling this method, consumers must pattern match on the error type to differentiate between:

  • DataStoreError::NoteScriptNotFound(_) - The script doesn't exist in the store
  • Other DataStoreError variants - Internal errors that should be propagated

Proposed Solution

Change the API to return Option<NoteScript> wrapped in a Result, making the distinction explicit:

fn get_note_script(
    &self,
    script_root: Word,
) -> impl FutureMaybeSend<Result<Option<NoteScript>, DataStoreError>>;

This would allow consumers to handle the cases more cleanly:

match store.get_note_script(script_root).await? {
    Some(script) => {
        // Script found - process it
    }
    None => {
        // Script not found - handle gracefully
    }
}

Benefits

  1. Clearer Intent: The API signature explicitly communicates that "not found" is a valid, expected outcome
  2. Simpler Error Handling: Consumers can use ? to propagate internal errors while handling None separately
  3. Better Separation of Concerns: Distinguishes between "data not present" (represented by None) and "error retrieving data" (represented by Err)
  4. Consistency: Follows Rust idioms where Option represents presence/absence and Result represents success/failure

Context

This issue was identified during code review of PR implementing public note creation with script fetching from the data store. The current workaround involves pattern matching on DataStoreError::NoteScriptNotFound(_) to handle the "not found" case separately from other errors.

See: https://github.com/0xPolygonMiden/miden-base/pull/[PR_NUMBER]

How should it be done?

  1. Update the DataStore trait definition
  2. Update all implementations of DataStore to return Result<Option<NoteScript>, DataStoreError>
  3. Update call sites in exec_host.rs and other consumers to use the new API
  4. Remove the DataStoreError::NoteScriptNotFound variant

When is this task done?

Change the DataStore::get_note_script API to returns Option<NoteScript> wrapped in a Result, making the distinction explicit. Something like this:

fn get_note_script(
    &self,
    script_root: Word,
) -> impl FutureMaybeSend<Result<Option<NoteScript>, DataStoreError>>;

Additional context

#1995 (comment)

Metadata

Metadata

Labels

rustIssues that affect or pull requests that update Rust code

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions