From 11fd5aa45ee21a1a2da3600d2347eb7906282fad Mon Sep 17 00:00:00 2001 From: Emilia Hane Date: Mon, 11 Nov 2024 17:21:39 +0100 Subject: [PATCH] chore(sdk): define new `BlockHeader` trait (#12452) --- crates/evm/Cargo.toml | 1 + crates/evm/src/lib.rs | 7 +-- crates/primitives-traits/src/block/header.rs | 49 ++++++++++++++++++++ crates/primitives-traits/src/block/mod.rs | 10 ++-- crates/primitives-traits/src/lib.rs | 8 +++- 5 files changed, 65 insertions(+), 10 deletions(-) create mode 100644 crates/primitives-traits/src/block/header.rs diff --git a/crates/evm/Cargo.toml b/crates/evm/Cargo.toml index c895110209..9d6a616af9 100644 --- a/crates/evm/Cargo.toml +++ b/crates/evm/Cargo.toml @@ -30,6 +30,7 @@ revm-primitives.workspace = true # alloy alloy-primitives.workspace = true alloy-eips.workspace = true +alloy-consensus.workspace = true auto_impl.workspace = true futures-util.workspace = true diff --git a/crates/evm/src/lib.rs b/crates/evm/src/lib.rs index e30ff9b1a7..d20dbe4594 100644 --- a/crates/evm/src/lib.rs +++ b/crates/evm/src/lib.rs @@ -17,13 +17,15 @@ extern crate alloc; -use crate::builder::RethEvmBuilder; +use alloy_consensus::BlockHeader as _; use alloy_primitives::{Address, Bytes, B256, U256}; use reth_primitives::TransactionSigned; use reth_primitives_traits::BlockHeader; use revm::{Database, Evm, GetInspector}; use revm_primitives::{BlockEnv, CfgEnvWithHandlerCfg, Env, EnvWithHandlerCfg, SpecId, TxEnv}; +use crate::builder::RethEvmBuilder; + pub mod builder; pub mod either; pub mod execute; @@ -33,7 +35,6 @@ pub mod noop; pub mod provider; pub mod state_change; pub mod system_calls; - #[cfg(any(test, feature = "test-utils"))] /// test helpers for mocking executor pub mod test_utils; @@ -155,7 +156,7 @@ pub trait ConfigureEvmEnv: Send + Sync + Unpin + Clone + 'static { block_env.coinbase = header.beneficiary(); block_env.timestamp = U256::from(header.timestamp()); if after_merge { - block_env.prevrandao = Some(header.mix_hash()); + block_env.prevrandao = header.mix_hash(); block_env.difficulty = U256::ZERO; } else { block_env.difficulty = header.difficulty(); diff --git a/crates/primitives-traits/src/block/header.rs b/crates/primitives-traits/src/block/header.rs new file mode 100644 index 0000000000..8ad85a5961 --- /dev/null +++ b/crates/primitives-traits/src/block/header.rs @@ -0,0 +1,49 @@ +//! Block header data primitive. + +use core::fmt; + +use alloy_primitives::Sealable; +use reth_codecs::Compact; + +/// Helper trait that unifies all behaviour required by block header to support full node +/// operations. +pub trait FullBlockHeader: BlockHeader + Compact {} + +impl FullBlockHeader for T where T: BlockHeader + Compact {} + +/// Abstraction of a block header. +pub trait BlockHeader: + Send + + Sync + + Unpin + + Clone + + Default + + fmt::Debug + + PartialEq + + Eq + + serde::Serialize + + for<'de> serde::Deserialize<'de> + + alloy_rlp::Encodable + + alloy_rlp::Decodable + + alloy_consensus::BlockHeader + + Sealable +{ +} + +impl BlockHeader for T where + T: Send + + Sync + + Unpin + + Clone + + Default + + fmt::Debug + + PartialEq + + Eq + + serde::Serialize + + for<'de> serde::Deserialize<'de> + + alloy_rlp::Encodable + + alloy_rlp::Decodable + + alloy_consensus::BlockHeader + + Sealable +{ +} diff --git a/crates/primitives-traits/src/block/mod.rs b/crates/primitives-traits/src/block/mod.rs index 125d587e42..185b61e978 100644 --- a/crates/primitives-traits/src/block/mod.rs +++ b/crates/primitives-traits/src/block/mod.rs @@ -1,19 +1,19 @@ //! Block abstraction. pub mod body; +pub mod header; use alloc::{fmt, vec::Vec}; -use alloy_consensus::BlockHeader; -use alloy_primitives::{Address, Sealable, B256}; +use alloy_primitives::{Address, B256}; use reth_codecs::Compact; -use crate::BlockBody; +use crate::{BlockBody, BlockHeader, FullBlockHeader}; /// Helper trait that unifies all behaviour required by block to support full node operations. pub trait FullBlock: Block + Compact {} -impl FullBlock for T where T: Block + Compact {} +impl FullBlock for T where T: Block + Compact {} /// Abstraction of block data type. // todo: make sealable super-trait, depends on @@ -34,7 +34,7 @@ pub trait Block: + Into<(Self::Header, Self::Body)> { /// Header part of the block. - type Header: BlockHeader + Sealable; + type Header: BlockHeader; /// The block's body contains the transactions in the block. type Body: BlockBody; diff --git a/crates/primitives-traits/src/lib.rs b/crates/primitives-traits/src/lib.rs index 6208f913c9..3d8aea04e3 100644 --- a/crates/primitives-traits/src/lib.rs +++ b/crates/primitives-traits/src/lib.rs @@ -34,7 +34,11 @@ mod integer_list; pub use integer_list::{IntegerList, IntegerListError}; pub mod block; -pub use block::{body::BlockBody, Block, FullBlock}; +pub use block::{ + body::BlockBody, + header::{BlockHeader, FullBlockHeader}, + Block, FullBlock, +}; mod withdrawal; pub use withdrawal::Withdrawal; @@ -56,7 +60,7 @@ pub use tx_type::TxType; pub mod header; #[cfg(any(test, feature = "arbitrary", feature = "test-utils"))] pub use header::test_utils; -pub use header::{BlockHeader, Header, HeaderError, SealedHeader}; +pub use header::{Header, HeaderError, SealedHeader}; /// Bincode-compatible serde implementations for common abstracted types in Reth. ///