-
Notifications
You must be signed in to change notification settings - Fork 115
Description
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
DataStoreErrorvariants - 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
- Clearer Intent: The API signature explicitly communicates that "not found" is a valid, expected outcome
- Simpler Error Handling: Consumers can use
?to propagate internal errors while handlingNoneseparately - Better Separation of Concerns: Distinguishes between "data not present" (represented by
None) and "error retrieving data" (represented byErr) - Consistency: Follows Rust idioms where
Optionrepresents presence/absence andResultrepresents 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?
- Update the
DataStoretrait definition - Update all implementations of
DataStoreto returnResult<Option<NoteScript>, DataStoreError> - Update call sites in
exec_host.rsand other consumers to use the new API - Remove the
DataStoreError::NoteScriptNotFoundvariant
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>>;