Skip to content

Commit 044ba71

Browse files
authored
feat: add map transactions fn (#1827)
1 parent 7b86e20 commit 044ba71

File tree

1 file changed

+36
-0
lines changed
  • crates/consensus/src/block

1 file changed

+36
-0
lines changed

crates/consensus/src/block/mod.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,42 @@ impl<T, H> Block<T, H> {
4343
pub fn into_body(self) -> BlockBody<T> {
4444
self.body
4545
}
46+
47+
/// Converts the block's transaction type by applying a function to each transaction.
48+
///
49+
/// Returns the block with the new transaction type.
50+
pub fn map_transactions<U>(self, f: impl FnMut(T) -> U) -> Block<U, H> {
51+
Block {
52+
header: self.header,
53+
body: BlockBody {
54+
transactions: self.body.transactions.into_iter().map(f).collect(),
55+
ommers: self.body.ommers,
56+
withdrawals: self.body.withdrawals,
57+
},
58+
}
59+
}
60+
61+
/// Converts the block's transaction type by applying a fallible function to each transaction.
62+
///
63+
/// Returns the block with the new transaction type if all transactions were successfully.
64+
pub fn try_map_transactions<U, E>(
65+
self,
66+
f: impl FnMut(T) -> Result<U, E>,
67+
) -> Result<Block<U, H>, E> {
68+
Ok(Block {
69+
header: self.header,
70+
body: BlockBody {
71+
transactions: self
72+
.body
73+
.transactions
74+
.into_iter()
75+
.map(f)
76+
.collect::<Result<_, _>>()?,
77+
ommers: self.body.ommers,
78+
withdrawals: self.body.withdrawals,
79+
},
80+
})
81+
}
4682
}
4783

4884
impl<T, H> Default for Block<T, H>

0 commit comments

Comments
 (0)