Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add map transactions to rpc block type #1835

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading