Skip to content

Commit

Permalink
docs + fix wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
fgimenez committed Nov 15, 2024
1 parent 86a4e7c commit 7387f1e
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions crates/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ extern crate alloc;

use alloy_consensus::BlockHeader as _;
use alloy_primitives::{Address, Bytes, B256, U256};
use core::{any::TypeId, mem::size_of, ptr};
use reth_primitives::TransactionSigned;
use reth_primitives_traits::BlockHeader;
use revm::{Database, Evm, GetInspector};
Expand Down Expand Up @@ -213,15 +214,23 @@ pub struct NextBlockEnvAttributes {
pub prev_randao: B256,
}

/// A trait for types that can be converted into any `EvmConfig`
/// A trait for types that can be converted into any type implementing `ConfigureEvm`.
/// To be used to convert the same type between different generic representations in
/// trait definitions.
///
/// This is specially useful for the implementations of `BlockExecutionStrategyFactory`,
/// They usually have a generic type `EvmCongif` and the associated `Strategy` type in
/// the trait definition has also another `EvmConfig`. These types are the same for the
/// strategy factory and this trait helps us to convince the compiler.
pub trait IntoEvmConfig: Sized {
/// Converts a type into an EvmConfig
/// Converts a type into something implementing `ConfigureEvm`.
fn into_evm_config<T>(self) -> T
where
Self: ConfigureEvmEnv,
T: ConfigureEvm<Header = <Self as ConfigureEvmEnv>::Header>;
}

// Blanket implementation for all types that implement `ConfigureEvm`
impl<T> IntoEvmConfig for T
where
T: ConfigureEvm + Sized,
Expand All @@ -231,19 +240,13 @@ where
U: ConfigureEvm<Header = T::Header> + Sized,
{
// When T and U are the same type, return self
if std::any::TypeId::of::<T>() == std::any::TypeId::of::<U>() {
let size = std::mem::size_of::<T>();
assert_eq!(
size,
std::mem::size_of::<U>(),
"Cannot transmute between types of different sizes"
);
if TypeId::of::<T>() == TypeId::of::<U>() {
let size = size_of::<T>();
assert_eq!(size, size_of::<U>(), "Cannot transmute between types of different sizes");
// Safety: We've verified the types are the same and have same size
unsafe { std::ptr::read(&self as *const T as *const U) }
unsafe { ptr::read(&self as *const T as *const U) }
} else {
unimplemented!("Conversion between different ConfigureEvm implementations must be explicitly defined")
}
}
}

// Blanket implementation for all types that implement ConfigureEvm

0 comments on commit 7387f1e

Please sign in to comment.