From e329796e3778a655178c8cc0ec5a2ae1b5e84520 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Fri, 27 Dec 2024 15:01:45 +0100 Subject: [PATCH 1/2] feat: add more builder style fns --- crates/network-primitives/src/block.rs | 5 ++ crates/rpc-types-eth/src/block.rs | 73 ++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/crates/network-primitives/src/block.rs b/crates/network-primitives/src/block.rs index 02d66a905ee..c403b21050e 100644 --- a/crates/network-primitives/src/block.rs +++ b/crates/network-primitives/src/block.rs @@ -130,6 +130,11 @@ impl BlockTransactions { } impl BlockTransactions { + /// Creates a new [`BlockTransactions::Hashes`] variant from the given iterator of transactions. + pub fn new_hashes(txs: impl IntoIterator) -> Self { + Self::Hashes(txs.into_iter().map(|tx| tx.tx_hash()).collect()) + } + /// Converts `self` into `Hashes`. #[inline] pub fn convert_to_hashes(&mut self) { diff --git a/crates/rpc-types-eth/src/block.rs b/crates/rpc-types-eth/src/block.rs index 1ad362f0fa2..90a225126d5 100644 --- a/crates/rpc-types-eth/src/block.rs +++ b/crates/rpc-types-eth/src/block.rs @@ -56,6 +56,47 @@ impl Default for Block { } impl Block { + /// Creates a new empty block (without transactions). + pub const fn empty(header: H) -> Self { + Self::new(header, BlockTransactions::Full(vec![])) + } + + /// Creates a new [`Block`] with the given header and transactions. + /// + /// Note: This does not set the withdrawals for the block. + /// + /// ``` + /// use alloy_eips::eip4895::Withdrawals; + /// use alloy_network_primitives::BlockTransactions; + /// use alloy_rpc_types_eth::{Block, Header, Transaction}; + /// let block = Block::new( + /// Header::new(alloy_consensus::Header::default()), + /// BlockTransactions::::Full(vec![]), + /// ) + /// .with_withdrawals(Some(Withdrawals::default())); + /// ``` + pub const fn new(header: H, transactions: BlockTransactions) -> Self { + Self { header, uncles: vec![], transactions, withdrawals: None } + } + + /// Sets the transactions for the block. + pub fn with_transactions(mut self, transactions: BlockTransactions) -> Self { + self.transactions = transactions; + self + } + + /// Sets the withdrawals for the block. + pub fn with_withdrawals(mut self, withdrawals: Option) -> Self { + self.withdrawals = withdrawals; + self + } + + /// Sets the uncles for the block. + pub fn with_uncles(mut self, uncles: Vec) -> Self { + self.uncles = uncles; + self + } + /// Converts the block's header type by applying a function to it. pub fn map_header(self, f: impl FnOnce(H) -> U) -> Block { Block { @@ -150,6 +191,8 @@ impl Block { } /// RPC representation of block header, wrapping a consensus header. +/// +/// This wraps the consensus header and adds additional fields for RPC. #[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))] #[derive(Clone, Debug, Default, PartialEq, Eq, Hash)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] @@ -171,6 +214,24 @@ pub struct Header { } impl Header { + /// Create a new [`Header`] from a consensus header. + /// + /// Note: This will compute the hash of the header. + pub fn new(inner: H) -> Self + where + H: Sealable, + { + Self::from_sealed(Sealed::new(inner)) + } + + /// Create a new [`Header`] from a sealed consensus header. + /// + /// Note: This does not set the total difficulty or size of the block. + pub fn from_sealed(header: Sealed) -> Self { + let (inner, hash) = header.into_parts(); + Self { hash, inner, total_difficulty: None, size: None } + } + /// Create a new [`Header`] from a sealed consensus header and additional fields. pub fn from_consensus( header: Sealed, @@ -180,6 +241,18 @@ impl Header { let (inner, hash) = header.into_parts(); Self { hash, inner, total_difficulty, size } } + + /// Set the total difficulty of the block. + pub const fn with_total_difficulty(mut self, total_difficulty: Option) -> Self { + self.total_difficulty = total_difficulty; + self + } + + /// Set the size of the block. + pub const fn with_size(mut self, size: Option) -> Self { + self.size = size; + self + } } impl Deref for Header { From 63569efe290a75239b411a613c49f6776e444021 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Fri, 27 Dec 2024 15:13:06 +0100 Subject: [PATCH 2/2] Update crates/network-primitives/src/block.rs Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com> --- crates/network-primitives/src/block.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/network-primitives/src/block.rs b/crates/network-primitives/src/block.rs index c403b21050e..c271df10ef4 100644 --- a/crates/network-primitives/src/block.rs +++ b/crates/network-primitives/src/block.rs @@ -131,8 +131,8 @@ impl BlockTransactions { impl BlockTransactions { /// Creates a new [`BlockTransactions::Hashes`] variant from the given iterator of transactions. - pub fn new_hashes(txs: impl IntoIterator) -> Self { - Self::Hashes(txs.into_iter().map(|tx| tx.tx_hash()).collect()) + pub fn new_hashes(txs: impl IntoIterator>) -> Self { + Self::Hashes(txs.into_iter().map(|tx| tx.as_ref().tx_hash()).collect()) } /// Converts `self` into `Hashes`.