From b5f55d9a2b36f6994d9e91b82f22c1980cdf8359 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Tue, 18 Jun 2024 17:33:04 +0200 Subject: [PATCH] chore: make reth-revm compile with no-std (#8931) --- crates/revm/Cargo.toml | 2 ++ crates/revm/src/batch.rs | 9 ++++++--- crates/revm/src/database.rs | 2 +- crates/revm/src/lib.rs | 4 ++++ crates/revm/src/state_change.rs | 14 +++++++++++--- 5 files changed, 24 insertions(+), 7 deletions(-) diff --git a/crates/revm/Cargo.toml b/crates/revm/Cargo.toml index e8204ee25..5462da738 100644 --- a/crates/revm/Cargo.toml +++ b/crates/revm/Cargo.toml @@ -36,4 +36,6 @@ tracing.workspace = true reth-trie.workspace = true [features] +default = ["std"] +std = [] test-utils = ["dep:reth-trie"] diff --git a/crates/revm/src/batch.rs b/crates/revm/src/batch.rs index 6f1b55b21..a9a44bff1 100644 --- a/crates/revm/src/batch.rs +++ b/crates/revm/src/batch.rs @@ -1,13 +1,16 @@ //! Helper for handling execution of multiple blocks. use crate::{precompile::Address, primitives::alloy_primitives::BlockNumber}; +use core::time::Duration; use reth_execution_errors::BlockExecutionError; use reth_primitives::{Receipt, Receipts, Request, Requests}; use reth_prune_types::{PruneMode, PruneModes, PruneSegmentError, MINIMUM_PRUNING_DISTANCE}; use revm::db::states::bundle_state::BundleRetention; -use std::time::Duration; use tracing::debug; +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; + /// Takes care of: /// - recording receipts during execution of multiple blocks. /// - pruning receipts according to the pruning configuration. @@ -78,7 +81,7 @@ impl BlockBatchRecord { /// Returns all recorded receipts. pub fn take_receipts(&mut self) -> Receipts { - std::mem::take(&mut self.receipts) + core::mem::take(&mut self.receipts) } /// Returns the recorded requests. @@ -88,7 +91,7 @@ impl BlockBatchRecord { /// Returns all recorded requests. pub fn take_requests(&mut self) -> Vec { - std::mem::take(&mut self.requests) + core::mem::take(&mut self.requests) } /// Returns the [`BundleRetention`] for the given block based on the configured prune modes. diff --git a/crates/revm/src/database.rs b/crates/revm/src/database.rs index fc40f474a..3b31788db 100644 --- a/crates/revm/src/database.rs +++ b/crates/revm/src/database.rs @@ -1,4 +1,5 @@ use crate::primitives::alloy_primitives::{BlockNumber, StorageKey, StorageValue}; +use core::ops::{Deref, DerefMut}; use reth_primitives::{Account, Address, B256, KECCAK_EMPTY, U256}; use reth_storage_errors::provider::{ProviderError, ProviderResult}; use revm::{ @@ -6,7 +7,6 @@ use revm::{ primitives::{AccountInfo, Bytecode}, Database, }; -use std::ops::{Deref, DerefMut}; /// A helper trait responsible for providing that necessary state for the EVM execution. /// diff --git a/crates/revm/src/lib.rs b/crates/revm/src/lib.rs index 8e5419567..4fb6c30d1 100644 --- a/crates/revm/src/lib.rs +++ b/crates/revm/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; /// Contains glue code for integrating reth database into revm's [Database]. pub mod database; diff --git a/crates/revm/src/state_change.rs b/crates/revm/src/state_change.rs index 947c08da7..b9fba6b25 100644 --- a/crates/revm/src/state_change.rs +++ b/crates/revm/src/state_change.rs @@ -22,6 +22,14 @@ use revm::{ }, Database, DatabaseCommit, Evm, }; + +// reuse revm's hashbrown implementation for no-std +#[cfg(not(feature = "std"))] +use crate::precompile::HashMap; +#[cfg(not(feature = "std"))] +use alloc::{boxed::Box, format, string::ToString, vec::Vec}; + +#[cfg(feature = "std")] use std::collections::HashMap; /// Collect all balance changes at the end of the block. @@ -86,7 +94,7 @@ pub fn apply_blockhashes_update + DatabaseCo parent_block_hash: B256, ) -> Result<(), BlockExecutionError> where - DB::Error: std::fmt::Display, + DB::Error: core::fmt::Display, { // If Prague is not activated or this is the genesis block, no hashes are added. if !chain_spec.is_prague_active_at_timestamp(block_timestamp) || block_number == 0 { @@ -153,7 +161,7 @@ pub fn apply_beacon_root_contract_call( evm: &mut Evm<'_, EXT, DB>, ) -> Result<(), BlockExecutionError> where - DB::Error: std::fmt::Display, + DB::Error: core::fmt::Display, { if !chain_spec.is_cancun_active_at_timestamp(block_timestamp) { return Ok(()) @@ -256,7 +264,7 @@ pub fn apply_withdrawal_requests_contract_call, ) -> Result, BlockExecutionError> where - DB::Error: std::fmt::Display, + DB::Error: core::fmt::Display, { // get previous env let previous_env = Box::new(evm.context.env().clone());