Skip to content

Commit

Permalink
feat: add more builder style fns (#1850)
Browse files Browse the repository at this point in the history
* feat: add more builder style fns

* Update crates/network-primitives/src/block.rs

Co-authored-by: DaniPopes <[email protected]>

---------

Co-authored-by: DaniPopes <[email protected]>
  • Loading branch information
mattsse and DaniPopes authored Dec 27, 2024
1 parent f56c035 commit 81f3f0e
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
5 changes: 5 additions & 0 deletions crates/network-primitives/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ impl<T> BlockTransactions<T> {
}

impl<T: TransactionResponse> BlockTransactions<T> {
/// Creates a new [`BlockTransactions::Hashes`] variant from the given iterator of transactions.
pub fn new_hashes(txs: impl IntoIterator<Item = impl AsRef<T>>) -> Self {
Self::Hashes(txs.into_iter().map(|tx| tx.as_ref().tx_hash()).collect())
}

/// Converts `self` into `Hashes`.
#[inline]
pub fn convert_to_hashes(&mut self) {
Expand Down
73 changes: 73 additions & 0 deletions crates/rpc-types-eth/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,47 @@ impl<T, H: Default> Default for Block<T, H> {
}

impl<T, H> Block<T, H> {
/// 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::<Transaction>::Full(vec![]),
/// )
/// .with_withdrawals(Some(Withdrawals::default()));
/// ```
pub const fn new(header: H, transactions: BlockTransactions<T>) -> Self {
Self { header, uncles: vec![], transactions, withdrawals: None }
}

/// Sets the transactions for the block.
pub fn with_transactions(mut self, transactions: BlockTransactions<T>) -> Self {
self.transactions = transactions;
self
}

/// Sets the withdrawals for the block.
pub fn with_withdrawals(mut self, withdrawals: Option<Withdrawals>) -> Self {
self.withdrawals = withdrawals;
self
}

/// Sets the uncles for the block.
pub fn with_uncles(mut self, uncles: Vec<B256>) -> Self {
self.uncles = uncles;
self
}

/// Converts the block's header type by applying a function to it.
pub fn map_header<U>(self, f: impl FnOnce(H) -> U) -> Block<T, U> {
Block {
Expand Down Expand Up @@ -150,6 +191,8 @@ impl<T> Block<T> {
}

/// 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))]
Expand All @@ -171,6 +214,24 @@ pub struct Header<H = alloy_consensus::Header> {
}

impl<H> Header<H> {
/// 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<H>) -> 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<H>,
Expand All @@ -180,6 +241,18 @@ impl<H> Header<H> {
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<U256>) -> Self {
self.total_difficulty = total_difficulty;
self
}

/// Set the size of the block.
pub const fn with_size(mut self, size: Option<U256>) -> Self {
self.size = size;
self
}
}

impl<H> Deref for Header<H> {
Expand Down

0 comments on commit 81f3f0e

Please sign in to comment.