Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧺 use typed errors in public interface #114

Closed
wants to merge 31 commits into from

Conversation

cratelyn
Copy link

@cratelyn cratelyn commented May 3, 2024

this updates jmt so that its public interface uses concrete, typed error variants rather than boxed dyn errors.

🚧 todo's

some todo's before i mark this as officially ready for review

  • sort out no_std compatibility (consider snafu)
  • fix non-default feature flags (this is causing tests to fail)
  • add #[non_exhaustive] to errors
  • bump Cargo.toml package version

while i sort these things out, i may be force-pushing to this branch regularly. you've been warned!

👁️ note for reviewers

a pattern that shows up in some places is replacing...

ensure!(a == b, "an invariant")

...with...

if a != b {
    return Err(SomeError::Kind);
}

...we should keep a close eye out for conditionals that are not inverted when this transformation is applied.

@cratelyn cratelyn self-assigned this May 3, 2024
@erwanor erwanor self-requested a review May 3, 2024 20:19
cratelyn added 16 commits May 4, 2024 15:13
```
pub trait TreeWriter {
    /// The kind of error that may be returned by [`TreeWriter::write_node_batch()`].
    type Error;
    /// Writes a node batch into storage.
    fn write_node_batch(&self, node_batch: &NodeBatch) -> Result<()>;
}
```

because of these changes, `StateSnapshotReceiver<H>` also has a
`FinishError` associated type. this is threaded through from the tree
writer.
as with the writer commit, this introduces an associated type so that
implementors of the reader trait may opt to use a different error type.

```
pub trait TreeReader {
    /// The type of error that may be returned by [`TreeReader`] storage lookups.
    type Error;

    /// Gets node given a node key. Returns `None` if the node does not exist.
    fn get_node_option(&self, node_key: &NodeKey) -> Result<Option<Node>, Self::Error>;

    /// Gets a value by identifier, returning the newest value whose version is *less than or
    /// equal to* the specified version.  Returns None if the value does not exist.
    fn get_value_option(
        &self,
        max_version: Version,
        key_hash: KeyHash,
    ) -> Result<Option<OwnedValue>, Self::Error>;

    /// Gets the rightmost leaf. Note that this assumes we are in the process of restoring the tree
    /// and all nodes are at the same version.
    fn get_rightmost_leaf(&self) -> Result<Option<(NodeKey, LeafNode)>, Self::Error>;
}
```

some interfaces flatten the inner `Option<T>`, from
`Result<Option<T>, E>` values into simpler `Result<T, E>` values,
so those are pulled into a standalone extension trait. this lets the
`TreeReader` trait itself be agnostic about its errors.
now that we've updated the `TreeReader` trait to include an associated
error type, the constructor for `TreeCache<'a, R>` does not need to
return an `anyhow::Error`.
previously in our `TreeWriter` work, we passed by this
`StateSnapshotReceiver` trait. this swings back around to add a concrete
error type to `JellyfishMerkleRestore`'s implementation of that trait.

a `RestoreError<E>`, parameterized across the write error `E` of backing
storage, may be returned by the `add_chunk_impl` method.
before we handle the constructor for `JellyfishMerkleRestore`, let's
make an error type for partial node recovery.

NB: despite the horizontal motion in `new()`, no code in the body is
changed, save `map_err(|_| ..)` conversions to wrap i/o errors in
`RecoveryError::Read(_)`.
this follows up on a `todo` comment from our work on `add_chunk()`.
this encapsulates the errors that can occur when we look up a value in
the merkle tree.
…pError`

this introduces some additional variants to `LookupError<E>`, and
transforms `get_with_exclusion_proof()` s.t. it now returns a typed
`LookupError<E>` as well.

some documentation is also added to the function, while we are here.
@cratelyn cratelyn force-pushed the kate/jmt-with-typed-errors branch from aa51392 to 078eafa Compare May 4, 2024 20:25
@cratelyn cratelyn closed this May 6, 2024
@cratelyn cratelyn removed the request for review from erwanor May 8, 2024 16:40
@prajwolrg
Copy link

prajwolrg commented Jun 19, 2024

Why is this closed? Are there plans to support this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants