Skip to content

Commit

Permalink
feat: add map transactions to rpc block type (#1835)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Dec 23, 2024
1 parent 073f9ee commit 91ae632
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
27 changes: 27 additions & 0 deletions crates/network-primitives/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,33 @@ impl<T> BlockTransactions<T> {
matches!(self, Self::Full(_))
}

/// Converts the transaction type by applying a function to each transaction.
///
/// Returns the block with the new transaction type.
pub fn map<U>(self, f: impl FnMut(T) -> U) -> BlockTransactions<U> {
match self {
Self::Full(txs) => BlockTransactions::Full(txs.into_iter().map(f).collect()),
Self::Hashes(hashes) => BlockTransactions::Hashes(hashes),
Self::Uncle => BlockTransactions::Uncle,
}
}

/// Converts the transaction type by applying a fallible function to each transaction.
///
/// Returns the block with the new transaction type if all transactions were successfully.
pub fn try_map<U, E>(
self,
f: impl FnMut(T) -> Result<U, E>,
) -> Result<BlockTransactions<U>, E> {
match self {
Self::Full(txs) => {
Ok(BlockTransactions::Full(txs.into_iter().map(f).collect::<Result<_, _>>()?))
}
Self::Hashes(hashes) => Ok(BlockTransactions::Hashes(hashes)),
Self::Uncle => Ok(BlockTransactions::Uncle),
}
}

/// Fallibly cast to a slice of transactions.
///
/// Returns `None` if the enum variant is not `Full`.
Expand Down
31 changes: 30 additions & 1 deletion crates/rpc-types-eth/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub use alloy_eips::{
BlockNumberOrTag, ForkBlock, RpcBlockHash,
};

/// Block representation
/// Block representation for RPC.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
Expand Down Expand Up @@ -54,6 +54,35 @@ impl<T, H: Default> Default for Block<T, H> {
}
}

impl<T, H> Block<T, H> {
/// Converts the block's transaction type by applying a function to each transaction.
///
/// Returns the block with the new transaction type.
pub fn map_transactions<U>(self, f: impl FnMut(T) -> U) -> Block<U, H> {
Block {
header: self.header,
uncles: self.uncles,
transactions: self.transactions.map(f),
withdrawals: self.withdrawals,
}
}

/// Converts the block's transaction type by applying a fallible function to each transaction.
///
/// Returns the block with the new transaction type if all transactions were successfully.
pub fn try_map_transactions<U, E>(
self,
f: impl FnMut(T) -> Result<U, E>,
) -> Result<Block<U, H>, E> {
Ok(Block {
header: self.header,
uncles: self.uncles,
transactions: self.transactions.try_map(f)?,
withdrawals: self.withdrawals,
})
}
}

impl<T: TransactionResponse, H> Block<T, H> {
/// Converts a block with Tx hashes into a full block.
pub fn into_full_block(self, txs: Vec<T>) -> Self {
Expand Down

0 comments on commit 91ae632

Please sign in to comment.