From 46c8255fa887dcc9d9b65aa9c1c47eeeabc4764d Mon Sep 17 00:00:00 2001 From: 0xAtreides <103257861+JackG-eth@users.noreply.github.com> Date: Thu, 13 Jun 2024 00:29:36 +0100 Subject: [PATCH] feat: support `no_std` for `reth-storage-errors` (#8790) Co-authored-by: Matthias Seitz --- Cargo.lock | 2 +- crates/storage/errors/Cargo.toml | 6 ++++-- crates/storage/errors/src/db.rs | 23 +++++++++++++++++------ crates/storage/errors/src/lib.rs | 4 ++++ crates/storage/errors/src/lockfile.rs | 6 ++++-- crates/storage/errors/src/provider.rs | 16 ++++++++++++---- 6 files changed, 42 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f8646ae64..07421f72b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8218,7 +8218,7 @@ version = "1.0.0-rc.1" dependencies = [ "reth-fs-util", "reth-primitives", - "thiserror", + "thiserror-no-std", ] [[package]] diff --git a/crates/storage/errors/Cargo.toml b/crates/storage/errors/Cargo.toml index 5fa806345..d8e699f8d 100644 --- a/crates/storage/errors/Cargo.toml +++ b/crates/storage/errors/Cargo.toml @@ -14,6 +14,8 @@ workspace = true reth-primitives.workspace = true reth-fs-util.workspace = true -thiserror.workspace = true +thiserror-no-std = { workspace = true, default-features = false } - +[features] +default = ["std"] +std = ["thiserror-no-std/std"] \ No newline at end of file diff --git a/crates/storage/errors/src/db.rs b/crates/storage/errors/src/db.rs index b731e4d24..8b4896d23 100644 --- a/crates/storage/errors/src/db.rs +++ b/crates/storage/errors/src/db.rs @@ -1,8 +1,19 @@ -use std::{fmt::Display, str::FromStr}; -use thiserror::Error; +#[cfg(feature = "std")] +use std::{fmt::Display, str::FromStr, string::String}; + +#[cfg(not(feature = "std"))] +use alloc::{ + boxed::Box, + format, + string::{String, ToString}, + vec::Vec, +}; + +#[cfg(not(feature = "std"))] +use core::{fmt::Display, str::FromStr}; /// Database error type. -#[derive(Clone, Debug, PartialEq, Eq, Error)] +#[derive(Clone, Debug, PartialEq, Eq, thiserror_no_std::Error)] pub enum DatabaseError { /// Failed to open the database. #[error("failed to open the database: {0}")] @@ -43,7 +54,7 @@ pub enum DatabaseError { } /// Common error struct to propagate implementation-specific error information. -#[derive(Debug, Error, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq, thiserror_no_std::Error)] #[error("{message} ({code})")] pub struct DatabaseErrorInfo { /// Human-readable error message. @@ -70,7 +81,7 @@ impl From for DatabaseError { } /// Database write error. -#[derive(Clone, Debug, PartialEq, Eq, Error)] +#[derive(Clone, Debug, PartialEq, Eq, thiserror_no_std::Error)] #[error( "write operation {operation:?} failed for key \"{key}\" in table {table_name:?}: {info}", key = reth_primitives::hex::encode(key), @@ -179,7 +190,7 @@ impl FromStr for LogLevel { "debug" => Ok(Self::Debug), "trace" => Ok(Self::Trace), "extra" => Ok(Self::Extra), - _ => Err(format!("Invalid log level: {}", s)), + _ => Err(format!("Invalid log level: {s}")), } } } diff --git a/crates/storage/errors/src/lib.rs b/crates/storage/errors/src/lib.rs index 8247c6352..dc8d24a16 100644 --- a/crates/storage/errors/src/lib.rs +++ b/crates/storage/errors/src/lib.rs @@ -7,6 +7,10 @@ )] #![cfg_attr(not(test), warn(unused_crate_dependencies))] #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] +#![cfg_attr(not(feature = "std"), no_std)] + +#[cfg(not(feature = "std"))] +extern crate alloc; /// Database error pub mod db; diff --git a/crates/storage/errors/src/lockfile.rs b/crates/storage/errors/src/lockfile.rs index 674485457..db27cb6e2 100644 --- a/crates/storage/errors/src/lockfile.rs +++ b/crates/storage/errors/src/lockfile.rs @@ -1,8 +1,10 @@ use reth_fs_util::FsPathError; -use thiserror::Error; -#[derive(Error, Debug, Clone, PartialEq, Eq)] +#[cfg(not(feature = "std"))] +use alloc::string::{String, ToString}; + /// Storage lock error. +#[derive(Debug, Clone, PartialEq, Eq, thiserror_no_std::Error)] pub enum StorageLockError { /// Write lock taken #[error("storage directory is currently in use as read-write by another process: PID {0}")] diff --git a/crates/storage/errors/src/provider.rs b/crates/storage/errors/src/provider.rs index 7472d500c..52a010474 100644 --- a/crates/storage/errors/src/provider.rs +++ b/crates/storage/errors/src/provider.rs @@ -2,14 +2,21 @@ use reth_primitives::{ Address, BlockHash, BlockHashOrNumber, BlockNumber, GotExpected, StaticFileSegment, TxHashOrNumber, TxNumber, B256, U256, }; + +#[cfg(feature = "std")] use std::path::PathBuf; -use thiserror::Error; + +#[cfg(not(feature = "std"))] +use alloc::{ + boxed::Box, + string::{String, ToString}, +}; /// Provider result type. pub type ProviderResult = Result; /// Bundled errors variants thrown by various providers. -#[derive(Clone, Debug, Error, PartialEq, Eq)] +#[derive(Clone, Debug, thiserror_no_std::Error, PartialEq, Eq)] pub enum ProviderError { /// Database error. #[error(transparent)] @@ -108,6 +115,7 @@ pub enum ProviderError { #[error("this provider does not support this request")] UnsupportedProvider, /// Static File is not found at specified path. + #[cfg(feature = "std")] #[error("not able to find {0} static file at {1}")] MissingStaticFilePath(StaticFileSegment, PathBuf), /// Static File is not found for requested block. @@ -143,7 +151,7 @@ impl From for ProviderError { } /// A root mismatch error at a given block height. -#[derive(Clone, Debug, Error, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq, thiserror_no_std::Error)] #[error("root mismatch at #{block_number} ({block_hash}): {root}")] pub struct RootMismatch { /// The target block root diff. @@ -155,7 +163,7 @@ pub struct RootMismatch { } /// Consistent database view error. -#[derive(Clone, Debug, Error, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq, thiserror_no_std::Error)] pub enum ConsistentViewError { /// Error thrown on attempt to initialize provider while node is still syncing. #[error("node is syncing. best block: {best_block:?}")]