Skip to content

Commit

Permalink
feat: Concrete error type for merkleized storage
Browse files Browse the repository at this point in the history
  • Loading branch information
netrome committed Feb 18, 2025
1 parent 41ccea0 commit 455c401
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 57 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions crates/proof_system/global_merkle_root/service/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ use crate::ports::{
pub struct Service<B, S>(ServiceRunner<UpdateMerkleRootTask<B, S>>)
where
B: BlockStream + Send + 'static,
B::Error: std::error::Error + Send + Sync + 'static,
B::Error: core::error::Error + Send + Sync + 'static,
S: ServiceStorage + Send + 'static;

impl<B, S> Service<B, S>
where
B: BlockStream + Send + 'static,
B::Error: std::error::Error + Send + Sync + 'static,
B::Error: core::error::Error + Send + Sync + 'static,
S: ServiceStorage + Send + 'static,
{
/// Construct a new service.
Expand All @@ -40,7 +40,7 @@ where
impl<B, S> Deref for Service<B, S>
where
B: BlockStream + Send + 'static,
B::Error: std::error::Error + Send + Sync + 'static,
B::Error: core::error::Error + Send + Sync + 'static,
S: ServiceStorage + Send + 'static,
{
type Target = ServiceRunner<UpdateMerkleRootTask<B, S>>;
Expand Down Expand Up @@ -72,7 +72,7 @@ impl<B, S> UpdateMerkleRootTask<B, S> {
impl<B, S> RunnableService for UpdateMerkleRootTask<B, S>
where
B: BlockStream + Send,
B::Error: std::error::Error + Send + Sync + 'static,
B::Error: core::error::Error + Send + Sync + 'static,
S: ServiceStorage + Send,
{
const NAME: &'static str = "MerkleRootService";
Expand All @@ -97,7 +97,7 @@ where
impl<B, S> RunnableTask for UpdateMerkleRootTask<B, S>
where
B: BlockStream + Send,
B::Error: std::error::Error + Send + Sync + 'static,
B::Error: core::error::Error + Send + Sync + 'static,
S: ServiceStorage + Send,
{
#[tracing::instrument(skip(self, watcher))]
Expand All @@ -121,7 +121,7 @@ where
impl<B, S> UpdateMerkleRootTask<B, S>
where
B: BlockStream,
B::Error: std::error::Error + Send + Sync + 'static,
B::Error: core::error::Error + Send + Sync + 'static,
S: ServiceStorage,
{
#[tracing::instrument(skip(self))]
Expand Down
4 changes: 2 additions & 2 deletions crates/proof_system/global_merkle_root/storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repository = { workspace = true }
version = { workspace = true }

[dependencies]
anyhow = { workspace = true }
derive_more = { workspace = true }
enum-iterator = { workspace = true }
fuel-core-storage = { workspace = true, features = ["alloc"] }
fuel-core-types = { workspace = true, default-features = false, features = [
Expand Down Expand Up @@ -43,7 +43,7 @@ rand = { workspace = true }

[features]
std = ["fuel-core-storage/std", "fuel-core-types/std"]
test-helpers = ["dep:rand"]
test-helpers = ["dep:rand", "std"]
fault-proving = [
"fuel-core-types/fault-proving",
"fuel-core-storage/fault-proving",
Expand Down
71 changes: 71 additions & 0 deletions crates/proof_system/global_merkle_root/storage/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use fuel_core_types::fuel_tx::{
self,
TxId,
UtxoId,
};

/// Errors that can occur in the merkleized storage.
#[derive(Debug, derive_more::Display, derive_more::From)]
#[non_exhaustive]
pub enum Error {
/// Errors indicating that the block is invalid.
#[from]
InvalidBlock(InvalidBlock),
/// Errors originating from storage operations.
#[from]
Storage(fuel_core_storage::Error),
/// Errors that can only occur if there is a bug in the code.
#[from]
InvariantViolation(InvariantViolation),
}

/// Error indicating that a block is invalid
#[derive(Debug, derive_more::Display, derive_more::From)]
#[non_exhaustive]
pub enum InvalidBlock {
/// The block has too many transactions.
TooManyTransactions,
/// A transaction has too many outputs.
TooManyOutputs,
/// An output contains an invalid contract input index.
InvalidContractInputIndex(u16),
/// A transaction tries to increment the consensus parameter version beyond the supported range.
ConsensusParametersVersionOutOfBounds,
/// A transaction tries to increment the state transition bytecode version beyond the supported range.
StateTransitionBytecodeVersionOutOfBounds,
/// A transaction already exists.
DuplicateTransaction(TxId),
/// A transaction is missing a referenced witness.
MissingWitness(usize),
/// A create transaction is missing a contract created output.
ContractCreatedOutputNotFound,
/// An upload transaction tries to upload code that has already been completely uploaded.
BytecodeAlreadyCompleted,
/// An upload transaction tries to upload the wrong subsection.
#[display(fmt = "wrong subsection index: expected {expected}, got {observed}")]
WrongSubsectionIndex {
/// The expected subsection.
expected: u16,
/// The observed subsection.
observed: u16,
},
/// An upload transaction tries to upload a subsection index outside of the supported range.
SubsectionNumberOverflow,
/// A transaction doesn't satisfy the transaction validity rules
#[from]
TxValidityError(fuel_tx::ValidityError),
}

/// Error indicating that there is a bug in the code.
#[derive(Debug, derive_more::Display, derive_more::From)]
#[non_exhaustive]
pub enum InvariantViolation {
/// We try to store a coin that already exists.
CoinAlreadyExists(UtxoId),
}

impl core::error::Error for Error {}
impl core::error::Error for InvalidBlock {}

/// Convenience alias for merkleized storage results
pub type Result<T> = core::result::Result<T, Error>;
8 changes: 8 additions & 0 deletions crates/proof_system/global_merkle_root/storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ pub mod structured_storage;
/// Update logic
pub mod update;

/// Error type
pub mod error;

pub use error::{
Error,
Result,
};

/// Test helpers
#[cfg(feature = "test-helpers")]
pub mod test_helpers;
Expand Down
Loading

0 comments on commit 455c401

Please sign in to comment.