diff --git a/crates/consensus/src/block/mod.rs b/crates/consensus/src/block/mod.rs index cb316f3ed60..5e54f9e2d2e 100644 --- a/crates/consensus/src/block/mod.rs +++ b/crates/consensus/src/block/mod.rs @@ -44,6 +44,16 @@ impl Block { self.body } + /// Converts the block's header type by applying a function to it. + pub fn map_header(self, f: impl FnOnce(H) -> U) -> Block { + Block { header: f(self.header), body: self.body } + } + + /// Converts the block's header type by applying a fallible function to it. + pub fn try_map_header(self, f: impl FnOnce(H) -> Result) -> Result, E> { + Ok(Block { header: f(self.header)?, body: self.body }) + } + /// Converts the block's transaction type by applying a function to each transaction. /// /// Returns the block with the new transaction type. diff --git a/crates/rpc-types-eth/src/block.rs b/crates/rpc-types-eth/src/block.rs index 4225798a1c4..1ad362f0fa2 100644 --- a/crates/rpc-types-eth/src/block.rs +++ b/crates/rpc-types-eth/src/block.rs @@ -56,6 +56,26 @@ impl Default for Block { } impl Block { + /// Converts the block's header type by applying a function to it. + pub fn map_header(self, f: impl FnOnce(H) -> U) -> Block { + Block { + header: f(self.header), + uncles: self.uncles, + transactions: self.transactions, + withdrawals: self.withdrawals, + } + } + + /// Converts the block's header type by applying a fallible function to it. + pub fn try_map_header(self, f: impl FnOnce(H) -> Result) -> Result, E> { + Ok(Block { + header: f(self.header)?, + uncles: self.uncles, + transactions: self.transactions, + withdrawals: self.withdrawals, + }) + } + /// Converts the block's transaction type by applying a function to each transaction. /// /// Returns the block with the new transaction type.