From 2bc16420491eaced802938ffd666ab52f29cffd8 Mon Sep 17 00:00:00 2001 From: 0xAtreides <103257861+JackG-eth@users.noreply.github.com> Date: Wed, 12 Jun 2024 00:21:48 +0100 Subject: [PATCH] feat: support `no_std` for `reth-execution-errors` (#8729) Co-authored-by: Matthias Seitz --- Cargo.lock | 22 +++++++++++++++++++++- Cargo.toml | 1 + crates/evm/execution-errors/Cargo.toml | 7 ++++++- crates/evm/execution-errors/src/lib.rs | 20 ++++++++++++++------ crates/evm/execution-errors/src/trie.rs | 2 +- 5 files changed, 43 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9f1c5ba7e5d6..2c4cfb1b669d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6979,7 +6979,7 @@ dependencies = [ "reth-primitives", "reth-prune-types", "reth-storage-errors", - "thiserror", + "thiserror-no-std", ] [[package]] @@ -9544,6 +9544,26 @@ dependencies = [ "syn 2.0.66", ] +[[package]] +name = "thiserror-impl-no-std" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58e6318948b519ba6dc2b442a6d0b904ebfb8d411a3ad3e07843615a72249758" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "thiserror-no-std" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3ad459d94dd517257cc96add8a43190ee620011bb6e6cdc82dafd97dfafafea" +dependencies = [ + "thiserror-impl-no-std", +] + [[package]] name = "thread_local" version = "1.1.8" diff --git a/Cargo.toml b/Cargo.toml index b8269979a504..76deae8c50ee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -381,6 +381,7 @@ generic-array = "0.14" tracing = "0.1.0" tracing-appender = "0.2" thiserror = "1.0" +thiserror-no-std = { version = "2.0.2", default-features = false } serde_json = "1.0.94" serde = { version = "1.0", default-features = false } serde_with = "3.3.0" diff --git a/crates/evm/execution-errors/Cargo.toml b/crates/evm/execution-errors/Cargo.toml index 4a23156848a3..920eb7a95e0b 100644 --- a/crates/evm/execution-errors/Cargo.toml +++ b/crates/evm/execution-errors/Cargo.toml @@ -16,4 +16,9 @@ reth-consensus.workspace = true reth-primitives.workspace = true reth-storage-errors.workspace = true reth-prune-types.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/evm/execution-errors/src/lib.rs b/crates/evm/execution-errors/src/lib.rs index 3c5088028fbd..3954fe982657 100644 --- a/crates/evm/execution-errors/src/lib.rs +++ b/crates/evm/execution-errors/src/lib.rs @@ -7,19 +7,24 @@ )] #![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; use reth_consensus::ConsensusError; use reth_primitives::{revm_primitives::EVMError, BlockNumHash, B256}; use reth_prune_types::PruneSegmentError; use reth_storage_errors::provider::ProviderError; -use std::fmt::Display; -use thiserror::Error; + +#[cfg(not(feature = "std"))] +use alloc::{boxed::Box, string::String}; pub mod trie; pub use trie::{StateRootError, StorageRootError}; /// Transaction validation errors -#[derive(Error, Debug, Clone, PartialEq, Eq)] +#[derive(thiserror_no_std::Error, Debug, Clone, PartialEq, Eq)] pub enum BlockValidationError { /// EVM error with transaction hash and message #[error("EVM reported invalid transaction ({hash}): {error}")] @@ -99,7 +104,7 @@ pub enum BlockValidationError { } /// `BlockExecutor` Errors -#[derive(Error, Debug)] +#[derive(thiserror_no_std::Error, Debug)] pub enum BlockExecutionError { /// Validation error, transparently wrapping `BlockValidationError` #[error(transparent)] @@ -119,7 +124,7 @@ pub enum BlockExecutionError { /// Transaction error on commit with inner details #[error("transaction error on commit: {inner}")] CanonicalCommit { - /// The inner error message + /// The inner error message. inner: String, }, /// Error when appending chain on fork is not possible @@ -136,12 +141,14 @@ pub enum BlockExecutionError { #[error(transparent)] LatestBlock(#[from] ProviderError), /// Arbitrary Block Executor Errors + #[cfg(feature = "std")] #[error(transparent)] Other(Box), } impl BlockExecutionError { /// Create a new `BlockExecutionError::Other` variant. + #[cfg(feature = "std")] pub fn other(error: E) -> Self where E: std::error::Error + Send + Sync + 'static, @@ -150,7 +157,8 @@ impl BlockExecutionError { } /// Create a new [`BlockExecutionError::Other`] from a given message. - pub fn msg(msg: impl Display) -> Self { + #[cfg(feature = "std")] + pub fn msg(msg: impl std::fmt::Display) -> Self { Self::Other(msg.to_string().into()) } diff --git a/crates/evm/execution-errors/src/trie.rs b/crates/evm/execution-errors/src/trie.rs index ee511611b998..fd3533977ab2 100644 --- a/crates/evm/execution-errors/src/trie.rs +++ b/crates/evm/execution-errors/src/trie.rs @@ -1,7 +1,7 @@ //! Errors when computing the state root. use reth_storage_errors::db::DatabaseError; -use thiserror::Error; +use thiserror_no_std::Error; /// State root errors. #[derive(Error, Debug, PartialEq, Eq, Clone)]