From 044ba712795722cdcb69eadcf4bdca27ea346443 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 23 Dec 2024 12:34:54 +0100 Subject: [PATCH] feat: add map transactions fn (#1827) --- crates/consensus/src/block/mod.rs | 36 +++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/crates/consensus/src/block/mod.rs b/crates/consensus/src/block/mod.rs index 300cfe14de4..cb316f3ed60 100644 --- a/crates/consensus/src/block/mod.rs +++ b/crates/consensus/src/block/mod.rs @@ -43,6 +43,42 @@ impl Block { pub fn into_body(self) -> BlockBody { 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(self, f: impl FnMut(T) -> U) -> Block { + 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( + self, + f: impl FnMut(T) -> Result, + ) -> Result, E> { + Ok(Block { + header: self.header, + body: BlockBody { + transactions: self + .body + .transactions + .into_iter() + .map(f) + .collect::>()?, + ommers: self.body.ommers, + withdrawals: self.body.withdrawals, + }, + }) + } } impl Default for Block