Skip to content

Commit

Permalink
feat: add map transactions fn (#1827)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Dec 23, 2024
1 parent 7b86e20 commit 044ba71
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions crates/consensus/src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,42 @@ impl<T, H> Block<T, H> {
pub fn into_body(self) -> BlockBody<T> {
self.body
}

/// 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,
body: BlockBody {
transactions: self.body.transactions.into_iter().map(f).collect(),
ommers: self.body.ommers,
withdrawals: self.body.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,
body: BlockBody {
transactions: self
.body
.transactions
.into_iter()
.map(f)
.collect::<Result<_, _>>()?,
ommers: self.body.ommers,
withdrawals: self.body.withdrawals,
},
})
}
}

impl<T, H> Default for Block<T, H>
Expand Down

0 comments on commit 044ba71

Please sign in to comment.