From 8b2c3a4a3413ef6ce9baa487a1b588ee332b0bf9 Mon Sep 17 00:00:00 2001 From: sprites0 <199826320+sprites0@users.noreply.github.com> Date: Sun, 5 Oct 2025 23:20:55 +0000 Subject: [PATCH] refactor: Move primitives into files --- src/node/primitives/block.rs | 52 +++ src/node/primitives/body.rs | 73 ++++ src/node/primitives/mod.rs | 333 +----------------- src/node/primitives/rlp.rs | 142 ++++++++ src/node/primitives/serde_bincode_compat.rs | 64 ++++ .../{tx_wrapper.rs => transaction.rs} | 0 src/node/storage/mod.rs | 2 +- 7 files changed, 342 insertions(+), 324 deletions(-) create mode 100644 src/node/primitives/block.rs create mode 100644 src/node/primitives/body.rs create mode 100644 src/node/primitives/rlp.rs create mode 100644 src/node/primitives/serde_bincode_compat.rs rename src/node/primitives/{tx_wrapper.rs => transaction.rs} (100%) diff --git a/src/node/primitives/block.rs b/src/node/primitives/block.rs new file mode 100644 index 000000000..e8b73276d --- /dev/null +++ b/src/node/primitives/block.rs @@ -0,0 +1,52 @@ +use crate::node::primitives::HlBlockBody; +use alloy_consensus::Header; +use alloy_rlp::Encodable; +use reth_primitives_traits::{Block, InMemorySize}; +use serde::{Deserialize, Serialize}; +use std::borrow::Cow; + +use crate::node::primitives::rlp; + +/// Block for HL +#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] +pub struct HlBlock { + pub header: Header, + pub body: HlBlockBody, +} + +impl InMemorySize for HlBlock { + fn size(&self) -> usize { + self.header.size() + self.body.size() + } +} + +impl Block for HlBlock { + type Header = Header; + type Body = HlBlockBody; + + fn new(header: Self::Header, body: Self::Body) -> Self { + Self { header, body } + } + fn header(&self) -> &Self::Header { + &self.header + } + fn body(&self) -> &Self::Body { + &self.body + } + fn split(self) -> (Self::Header, Self::Body) { + (self.header, self.body) + } + + fn rlp_length(header: &Self::Header, body: &Self::Body) -> usize { + rlp::BlockHelper { + header: Cow::Borrowed(header), + transactions: Cow::Borrowed(&body.inner.transactions), + ommers: Cow::Borrowed(&body.inner.ommers), + withdrawals: body.inner.withdrawals.as_ref().map(Cow::Borrowed), + sidecars: body.sidecars.as_ref().map(Cow::Borrowed), + read_precompile_calls: body.read_precompile_calls.as_ref().map(Cow::Borrowed), + highest_precompile_address: body.highest_precompile_address.as_ref().map(Cow::Borrowed), + } + .length() + } +} diff --git a/src/node/primitives/body.rs b/src/node/primitives/body.rs new file mode 100644 index 000000000..f9abb291c --- /dev/null +++ b/src/node/primitives/body.rs @@ -0,0 +1,73 @@ +use alloy_consensus::{BlobTransactionSidecar, Header}; +use alloy_primitives::Address; +use reth_primitives_traits::{BlockBody as BlockBodyTrait, InMemorySize}; +use serde::{Deserialize, Serialize}; + +use crate::node::primitives::{BlockBody, TransactionSigned}; +pub use crate::node::types::{ReadPrecompileCall, ReadPrecompileCalls}; + +/// Block body for HL. It is equivalent to Ethereum [`BlockBody`] but additionally stores sidecars +/// for blob transactions. +#[derive( + Debug, + Clone, + Default, + PartialEq, + Eq, + Serialize, + Deserialize, + derive_more::Deref, + derive_more::DerefMut, +)] +pub struct HlBlockBody { + #[serde(flatten)] + #[deref] + #[deref_mut] + pub inner: BlockBody, + pub sidecars: Option>, + pub read_precompile_calls: Option, + pub highest_precompile_address: Option
, +} + +impl InMemorySize for HlBlockBody { + fn size(&self) -> usize { + self.inner.size() + + self.sidecars + .as_ref() + .map_or(0, |s| s.capacity() * core::mem::size_of::()) + + self.read_precompile_calls + .as_ref() + .map_or(0, |s| s.0.capacity() * core::mem::size_of::()) + } +} + +impl BlockBodyTrait for HlBlockBody { + type Transaction = TransactionSigned; + type OmmerHeader = Header; + + fn transactions(&self) -> &[Self::Transaction] { + BlockBodyTrait::transactions(&self.inner) + } + fn into_ethereum_body(self) -> BlockBody { + self.inner + } + fn into_transactions(self) -> Vec { + self.inner.into_transactions() + } + fn withdrawals(&self) -> Option<&alloy_rpc_types::Withdrawals> { + self.inner.withdrawals() + } + fn ommers(&self) -> Option<&[Self::OmmerHeader]> { + self.inner.ommers() + } + + fn calculate_tx_root(&self) -> alloy_primitives::B256 { + alloy_consensus::proofs::calculate_transaction_root( + &self + .transactions() + .iter() + .filter(|tx| !tx.is_system_transaction()) + .collect::>(), + ) + } +} diff --git a/src/node/primitives/mod.rs b/src/node/primitives/mod.rs index c76efa70a..aa2029efa 100644 --- a/src/node/primitives/mod.rs +++ b/src/node/primitives/mod.rs @@ -1,17 +1,17 @@ -#![allow(clippy::owned_cow)] -use alloy_consensus::{BlobTransactionSidecar, Header}; -use alloy_primitives::Address; -use alloy_rlp::{Encodable, RlpDecodable, RlpEncodable}; +use alloy_consensus::Header; use reth_ethereum_primitives::Receipt; use reth_primitives::NodePrimitives; -use reth_primitives_traits::{Block, BlockBody as BlockBodyTrait, InMemorySize}; -use serde::{Deserialize, Serialize}; -use std::borrow::Cow; -use crate::node::types::{ReadPrecompileCall, ReadPrecompileCalls}; +pub mod transaction; +pub use transaction::{BlockBody, TransactionSigned}; -pub mod tx_wrapper; -pub use tx_wrapper::{BlockBody, TransactionSigned}; +pub mod block; +pub use block::HlBlock; +pub mod body; +pub use body::HlBlockBody; + +pub mod rlp; +pub mod serde_bincode_compat; /// Primitive types for HyperEVM. #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] @@ -25,316 +25,3 @@ impl NodePrimitives for HlPrimitives { type SignedTx = TransactionSigned; type Receipt = Receipt; } - -/// Block body for HL. It is equivalent to Ethereum [`BlockBody`] but additionally stores sidecars -/// for blob transactions. -#[derive( - Debug, - Clone, - Default, - PartialEq, - Eq, - Serialize, - Deserialize, - derive_more::Deref, - derive_more::DerefMut, -)] -pub struct HlBlockBody { - #[serde(flatten)] - #[deref] - #[deref_mut] - pub inner: BlockBody, - pub sidecars: Option>, - pub read_precompile_calls: Option, - pub highest_precompile_address: Option
, -} - -impl InMemorySize for HlBlockBody { - fn size(&self) -> usize { - self.inner.size() + - self.sidecars - .as_ref() - .map_or(0, |s| s.capacity() * core::mem::size_of::()) + - self.read_precompile_calls - .as_ref() - .map_or(0, |s| s.0.capacity() * core::mem::size_of::()) - } -} - -impl BlockBodyTrait for HlBlockBody { - type Transaction = TransactionSigned; - type OmmerHeader = Header; - - fn transactions(&self) -> &[Self::Transaction] { - BlockBodyTrait::transactions(&self.inner) - } - fn into_ethereum_body(self) -> BlockBody { - self.inner - } - fn into_transactions(self) -> Vec { - self.inner.into_transactions() - } - fn withdrawals(&self) -> Option<&alloy_rpc_types::Withdrawals> { - self.inner.withdrawals() - } - fn ommers(&self) -> Option<&[Self::OmmerHeader]> { - self.inner.ommers() - } - - fn calculate_tx_root(&self) -> alloy_primitives::B256 { - alloy_consensus::proofs::calculate_transaction_root( - &self - .transactions() - .iter() - .filter(|tx| !tx.is_system_transaction()) - .collect::>(), - ) - } -} - -/// Block for HL -#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] -pub struct HlBlock { - pub header: Header, - pub body: HlBlockBody, -} - -impl InMemorySize for HlBlock { - fn size(&self) -> usize { - self.header.size() + self.body.size() - } -} - -impl Block for HlBlock { - type Header = Header; - type Body = HlBlockBody; - - fn new(header: Self::Header, body: Self::Body) -> Self { - Self { header, body } - } - fn header(&self) -> &Self::Header { - &self.header - } - fn body(&self) -> &Self::Body { - &self.body - } - fn split(self) -> (Self::Header, Self::Body) { - (self.header, self.body) - } - - fn rlp_length(header: &Self::Header, body: &Self::Body) -> usize { - rlp::BlockHelper { - header: Cow::Borrowed(header), - transactions: Cow::Borrowed(&body.inner.transactions), - ommers: Cow::Borrowed(&body.inner.ommers), - withdrawals: body.inner.withdrawals.as_ref().map(Cow::Borrowed), - sidecars: body.sidecars.as_ref().map(Cow::Borrowed), - read_precompile_calls: body.read_precompile_calls.as_ref().map(Cow::Borrowed), - highest_precompile_address: body.highest_precompile_address.as_ref().map(Cow::Borrowed), - } - .length() - } -} - -mod rlp { - use super::*; - use alloy_eips::eip4895::Withdrawals; - use alloy_rlp::Decodable; - - #[derive(RlpEncodable, RlpDecodable)] - #[rlp(trailing)] - struct BlockBodyHelper<'a> { - transactions: Cow<'a, Vec>, - ommers: Cow<'a, Vec
>, - withdrawals: Option>, - sidecars: Option>>, - read_precompile_calls: Option>, - highest_precompile_address: Option>, - } - - #[derive(RlpEncodable, RlpDecodable)] - #[rlp(trailing)] - pub(crate) struct BlockHelper<'a> { - pub(crate) header: Cow<'a, Header>, - pub(crate) transactions: Cow<'a, Vec>, - pub(crate) ommers: Cow<'a, Vec
>, - pub(crate) withdrawals: Option>, - pub(crate) sidecars: Option>>, - pub(crate) read_precompile_calls: Option>, - pub(crate) highest_precompile_address: Option>, - } - - impl<'a> From<&'a HlBlockBody> for BlockBodyHelper<'a> { - fn from(value: &'a HlBlockBody) -> Self { - let HlBlockBody { - inner: BlockBody { transactions, ommers, withdrawals }, - sidecars, - read_precompile_calls, - highest_precompile_address, - } = value; - Self { - transactions: Cow::Borrowed(transactions), - ommers: Cow::Borrowed(ommers), - withdrawals: withdrawals.as_ref().map(Cow::Borrowed), - sidecars: sidecars.as_ref().map(Cow::Borrowed), - read_precompile_calls: read_precompile_calls.as_ref().map(Cow::Borrowed), - highest_precompile_address: highest_precompile_address.as_ref().map(Cow::Borrowed), - } - } - } - - impl<'a> From<&'a HlBlock> for BlockHelper<'a> { - fn from(value: &'a HlBlock) -> Self { - let HlBlock { - header, - body: - HlBlockBody { - inner: BlockBody { transactions, ommers, withdrawals }, - sidecars, - read_precompile_calls, - highest_precompile_address, - }, - } = value; - Self { - header: Cow::Borrowed(header), - transactions: Cow::Borrowed(transactions), - ommers: Cow::Borrowed(ommers), - withdrawals: withdrawals.as_ref().map(Cow::Borrowed), - sidecars: sidecars.as_ref().map(Cow::Borrowed), - read_precompile_calls: read_precompile_calls.as_ref().map(Cow::Borrowed), - highest_precompile_address: highest_precompile_address.as_ref().map(Cow::Borrowed), - } - } - } - - impl Encodable for HlBlockBody { - fn encode(&self, out: &mut dyn bytes::BufMut) { - BlockBodyHelper::from(self).encode(out); - } - fn length(&self) -> usize { - BlockBodyHelper::from(self).length() - } - } - - impl Decodable for HlBlockBody { - fn decode(buf: &mut &[u8]) -> alloy_rlp::Result { - let BlockBodyHelper { - transactions, - ommers, - withdrawals, - sidecars, - read_precompile_calls, - highest_precompile_address, - } = BlockBodyHelper::decode(buf)?; - Ok(Self { - inner: BlockBody { - transactions: transactions.into_owned(), - ommers: ommers.into_owned(), - withdrawals: withdrawals.map(|w| w.into_owned()), - }, - sidecars: sidecars.map(|s| s.into_owned()), - read_precompile_calls: read_precompile_calls.map(|s| s.into_owned()), - highest_precompile_address: highest_precompile_address.map(|s| s.into_owned()), - }) - } - } - - impl Encodable for HlBlock { - fn encode(&self, out: &mut dyn bytes::BufMut) { - BlockHelper::from(self).encode(out); - } - fn length(&self) -> usize { - BlockHelper::from(self).length() - } - } - - impl Decodable for HlBlock { - fn decode(buf: &mut &[u8]) -> alloy_rlp::Result { - let BlockHelper { - header, - transactions, - ommers, - withdrawals, - sidecars, - read_precompile_calls, - highest_precompile_address, - } = BlockHelper::decode(buf)?; - Ok(Self { - header: header.into_owned(), - body: HlBlockBody { - inner: BlockBody { - transactions: transactions.into_owned(), - ommers: ommers.into_owned(), - withdrawals: withdrawals.map(|w| w.into_owned()), - }, - sidecars: sidecars.map(|s| s.into_owned()), - read_precompile_calls: read_precompile_calls.map(|s| s.into_owned()), - highest_precompile_address: highest_precompile_address.map(|s| s.into_owned()), - }, - }) - } - } -} - -pub mod serde_bincode_compat { - use super::*; - use reth_primitives_traits::serde_bincode_compat::{BincodeReprFor, SerdeBincodeCompat}; - - #[derive(Debug, Serialize, Deserialize)] - pub struct HlBlockBodyBincode<'a> { - inner: BincodeReprFor<'a, BlockBody>, - sidecars: Option>>, - read_precompile_calls: Option>, - highest_precompile_address: Option>, - } - - #[derive(Debug, Serialize, Deserialize)] - pub struct HlBlockBincode<'a> { - header: BincodeReprFor<'a, Header>, - body: BincodeReprFor<'a, HlBlockBody>, - } - - impl SerdeBincodeCompat for HlBlockBody { - type BincodeRepr<'a> = HlBlockBodyBincode<'a>; - - fn as_repr(&self) -> Self::BincodeRepr<'_> { - HlBlockBodyBincode { - inner: self.inner.as_repr(), - sidecars: self.sidecars.as_ref().map(Cow::Borrowed), - read_precompile_calls: self.read_precompile_calls.as_ref().map(Cow::Borrowed), - highest_precompile_address: self - .highest_precompile_address - .as_ref() - .map(Cow::Borrowed), - } - } - - fn from_repr(repr: Self::BincodeRepr<'_>) -> Self { - let HlBlockBodyBincode { - inner, - sidecars, - read_precompile_calls, - highest_precompile_address, - } = repr; - Self { - inner: BlockBody::from_repr(inner), - sidecars: sidecars.map(|s| s.into_owned()), - read_precompile_calls: read_precompile_calls.map(|s| s.into_owned()), - highest_precompile_address: highest_precompile_address.map(|s| s.into_owned()), - } - } - } - - impl SerdeBincodeCompat for HlBlock { - type BincodeRepr<'a> = HlBlockBincode<'a>; - - fn as_repr(&self) -> Self::BincodeRepr<'_> { - HlBlockBincode { header: self.header.as_repr(), body: self.body.as_repr() } - } - - fn from_repr(repr: Self::BincodeRepr<'_>) -> Self { - let HlBlockBincode { header, body } = repr; - Self { header: Header::from_repr(header), body: HlBlockBody::from_repr(body) } - } - } -} diff --git a/src/node/primitives/rlp.rs b/src/node/primitives/rlp.rs new file mode 100644 index 000000000..1af433eed --- /dev/null +++ b/src/node/primitives/rlp.rs @@ -0,0 +1,142 @@ +#![allow(clippy::owned_cow)] +use super::{HlBlock, HlBlockBody, TransactionSigned}; +use crate::node::types::ReadPrecompileCalls; +use alloy_consensus::{BlobTransactionSidecar, BlockBody, Header}; +use alloy_eips::eip4895::Withdrawals; +use alloy_primitives::Address; +use alloy_rlp::{Decodable, Encodable, RlpDecodable, RlpEncodable}; +use std::borrow::Cow; + +#[derive(RlpEncodable, RlpDecodable)] +#[rlp(trailing)] +struct BlockBodyHelper<'a> { + transactions: Cow<'a, Vec>, + ommers: Cow<'a, Vec
>, + withdrawals: Option>, + sidecars: Option>>, + read_precompile_calls: Option>, + highest_precompile_address: Option>, +} + +#[derive(RlpEncodable, RlpDecodable)] +#[rlp(trailing)] +pub(crate) struct BlockHelper<'a> { + pub(crate) header: Cow<'a, Header>, + pub(crate) transactions: Cow<'a, Vec>, + pub(crate) ommers: Cow<'a, Vec
>, + pub(crate) withdrawals: Option>, + pub(crate) sidecars: Option>>, + pub(crate) read_precompile_calls: Option>, + pub(crate) highest_precompile_address: Option>, +} + +impl<'a> From<&'a HlBlockBody> for BlockBodyHelper<'a> { + fn from(value: &'a HlBlockBody) -> Self { + let HlBlockBody { + inner: BlockBody { transactions, ommers, withdrawals }, + sidecars, + read_precompile_calls, + highest_precompile_address, + } = value; + Self { + transactions: Cow::Borrowed(transactions), + ommers: Cow::Borrowed(ommers), + withdrawals: withdrawals.as_ref().map(Cow::Borrowed), + sidecars: sidecars.as_ref().map(Cow::Borrowed), + read_precompile_calls: read_precompile_calls.as_ref().map(Cow::Borrowed), + highest_precompile_address: highest_precompile_address.as_ref().map(Cow::Borrowed), + } + } +} + +impl<'a> From<&'a HlBlock> for BlockHelper<'a> { + fn from(value: &'a HlBlock) -> Self { + let HlBlock { + header, + body: + HlBlockBody { + inner: BlockBody { transactions, ommers, withdrawals }, + sidecars, + read_precompile_calls, + highest_precompile_address, + }, + } = value; + Self { + header: Cow::Borrowed(header), + transactions: Cow::Borrowed(transactions), + ommers: Cow::Borrowed(ommers), + withdrawals: withdrawals.as_ref().map(Cow::Borrowed), + sidecars: sidecars.as_ref().map(Cow::Borrowed), + read_precompile_calls: read_precompile_calls.as_ref().map(Cow::Borrowed), + highest_precompile_address: highest_precompile_address.as_ref().map(Cow::Borrowed), + } + } +} + +impl Encodable for HlBlockBody { + fn encode(&self, out: &mut dyn bytes::BufMut) { + BlockBodyHelper::from(self).encode(out); + } + fn length(&self) -> usize { + BlockBodyHelper::from(self).length() + } +} + +impl Decodable for HlBlockBody { + fn decode(buf: &mut &[u8]) -> alloy_rlp::Result { + let BlockBodyHelper { + transactions, + ommers, + withdrawals, + sidecars, + read_precompile_calls, + highest_precompile_address, + } = BlockBodyHelper::decode(buf)?; + Ok(Self { + inner: BlockBody { + transactions: transactions.into_owned(), + ommers: ommers.into_owned(), + withdrawals: withdrawals.map(|w| w.into_owned()), + }, + sidecars: sidecars.map(|s| s.into_owned()), + read_precompile_calls: read_precompile_calls.map(|s| s.into_owned()), + highest_precompile_address: highest_precompile_address.map(|s| s.into_owned()), + }) + } +} + +impl Encodable for HlBlock { + fn encode(&self, out: &mut dyn bytes::BufMut) { + BlockHelper::from(self).encode(out); + } + fn length(&self) -> usize { + BlockHelper::from(self).length() + } +} + +impl Decodable for HlBlock { + fn decode(buf: &mut &[u8]) -> alloy_rlp::Result { + let BlockHelper { + header, + transactions, + ommers, + withdrawals, + sidecars, + read_precompile_calls, + highest_precompile_address, + } = BlockHelper::decode(buf)?; + Ok(Self { + header: header.into_owned(), + body: HlBlockBody { + inner: BlockBody { + transactions: transactions.into_owned(), + ommers: ommers.into_owned(), + withdrawals: withdrawals.map(|w| w.into_owned()), + }, + sidecars: sidecars.map(|s| s.into_owned()), + read_precompile_calls: read_precompile_calls.map(|s| s.into_owned()), + highest_precompile_address: highest_precompile_address.map(|s| s.into_owned()), + }, + }) + } +} diff --git a/src/node/primitives/serde_bincode_compat.rs b/src/node/primitives/serde_bincode_compat.rs new file mode 100644 index 000000000..af876d003 --- /dev/null +++ b/src/node/primitives/serde_bincode_compat.rs @@ -0,0 +1,64 @@ +#![allow(clippy::owned_cow)] +use alloy_consensus::{BlobTransactionSidecar, Header}; +use alloy_primitives::Address; +use reth_primitives_traits::serde_bincode_compat::{BincodeReprFor, SerdeBincodeCompat}; +use serde::{Deserialize, Serialize}; +use std::borrow::Cow; + +use super::{HlBlock, HlBlockBody}; +use crate::node::{primitives::BlockBody, types::ReadPrecompileCalls}; + +#[derive(Debug, Serialize, Deserialize)] +pub struct HlBlockBodyBincode<'a> { + inner: BincodeReprFor<'a, BlockBody>, + sidecars: Option>>, + read_precompile_calls: Option>, + highest_precompile_address: Option>, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct HlBlockBincode<'a> { + header: BincodeReprFor<'a, Header>, + body: BincodeReprFor<'a, HlBlockBody>, +} + +impl SerdeBincodeCompat for HlBlockBody { + type BincodeRepr<'a> = HlBlockBodyBincode<'a>; + + fn as_repr(&self) -> Self::BincodeRepr<'_> { + HlBlockBodyBincode { + inner: self.inner.as_repr(), + sidecars: self.sidecars.as_ref().map(Cow::Borrowed), + read_precompile_calls: self.read_precompile_calls.as_ref().map(Cow::Borrowed), + highest_precompile_address: self.highest_precompile_address.as_ref().map(Cow::Borrowed), + } + } + + fn from_repr(repr: Self::BincodeRepr<'_>) -> Self { + let HlBlockBodyBincode { + inner, + sidecars, + read_precompile_calls, + highest_precompile_address, + } = repr; + Self { + inner: BlockBody::from_repr(inner), + sidecars: sidecars.map(|s| s.into_owned()), + read_precompile_calls: read_precompile_calls.map(|s| s.into_owned()), + highest_precompile_address: highest_precompile_address.map(|s| s.into_owned()), + } + } +} + +impl SerdeBincodeCompat for HlBlock { + type BincodeRepr<'a> = HlBlockBincode<'a>; + + fn as_repr(&self) -> Self::BincodeRepr<'_> { + HlBlockBincode { header: self.header.as_repr(), body: self.body.as_repr() } + } + + fn from_repr(repr: Self::BincodeRepr<'_>) -> Self { + let HlBlockBincode { header, body } = repr; + Self { header: Header::from_repr(header), body: HlBlockBody::from_repr(body) } + } +} diff --git a/src/node/primitives/tx_wrapper.rs b/src/node/primitives/transaction.rs similarity index 100% rename from src/node/primitives/tx_wrapper.rs rename to src/node/primitives/transaction.rs diff --git a/src/node/storage/mod.rs b/src/node/storage/mod.rs index 73f3d8bf6..8ee49ba27 100644 --- a/src/node/storage/mod.rs +++ b/src/node/storage/mod.rs @@ -1,7 +1,7 @@ use crate::{ HlBlock, HlBlockBody, HlPrimitives, node::{ - primitives::tx_wrapper::{convert_to_eth_block_body, convert_to_hl_block_body}, + primitives::transaction::{convert_to_eth_block_body, convert_to_hl_block_body}, types::HlExtras, }, };