From 3059214ecf7f552caa52ae54107d0af6d0a622d4 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 23 Dec 2024 12:29:18 +0100 Subject: [PATCH] feat: add tryfrom for anyheader to header (#1826) --- crates/consensus-any/src/block/header.rs | 128 +++++++++++++++++++++++ 1 file changed, 128 insertions(+) diff --git a/crates/consensus-any/src/block/header.rs b/crates/consensus-any/src/block/header.rs index 212e23f7a54..96a9ce5799e 100644 --- a/crates/consensus-any/src/block/header.rs +++ b/crates/consensus-any/src/block/header.rs @@ -107,6 +107,126 @@ pub struct AnyHeader { pub target_blobs_per_block: Option, } +impl AnyHeader { + /// Attempts to convert this header into a `Header`. + /// + /// This can fail if the header is missing required fields: + /// - nonce + /// - mix_hash + /// + /// If the conversion fails, the original [`AnyHeader`] is returned. + pub fn try_into_header(self) -> Result { + if self.nonce.is_none() || self.mix_hash.is_none() { + return Err(self); + } + + let Self { + parent_hash, + ommers_hash, + beneficiary, + state_root, + transactions_root, + receipts_root, + logs_bloom, + difficulty, + number, + gas_limit, + gas_used, + timestamp, + extra_data, + mix_hash, + nonce, + base_fee_per_gas, + withdrawals_root, + blob_gas_used, + excess_blob_gas, + parent_beacon_block_root, + requests_hash, + target_blobs_per_block, + } = self; + + Ok(Header { + parent_hash, + ommers_hash, + beneficiary, + state_root, + transactions_root, + receipts_root, + logs_bloom, + difficulty, + number, + gas_limit, + gas_used, + timestamp, + extra_data, + mix_hash: mix_hash.unwrap(), + nonce: nonce.unwrap(), + base_fee_per_gas, + withdrawals_root, + blob_gas_used, + excess_blob_gas, + parent_beacon_block_root, + requests_hash, + target_blobs_per_block, + }) + } + + /// Converts this header into a [`Header`] with default values for missing mandatory fields: + /// - mix_hash + /// - nonce + pub fn into_header_with_defaults(self) -> Header { + let Self { + parent_hash, + ommers_hash, + beneficiary, + state_root, + transactions_root, + receipts_root, + logs_bloom, + difficulty, + number, + gas_limit, + gas_used, + timestamp, + extra_data, + mix_hash, + nonce, + base_fee_per_gas, + withdrawals_root, + blob_gas_used, + excess_blob_gas, + parent_beacon_block_root, + requests_hash, + target_blobs_per_block, + } = self; + + Header { + parent_hash, + ommers_hash, + beneficiary, + state_root, + transactions_root, + receipts_root, + logs_bloom, + difficulty, + number, + gas_limit, + gas_used, + timestamp, + extra_data, + mix_hash: mix_hash.unwrap_or_default(), + nonce: nonce.unwrap_or_default(), + base_fee_per_gas, + withdrawals_root, + blob_gas_used, + excess_blob_gas, + parent_beacon_block_root, + requests_hash, + target_blobs_per_block, + } + } +} + impl BlockHeader for AnyHeader { fn parent_hash(&self) -> B256 { self.parent_hash @@ -250,3 +370,11 @@ impl From
for AnyHeader { } } } + +impl TryFrom for Header { + type Error = AnyHeader; + + fn try_from(value: AnyHeader) -> Result { + value.try_into_header() + } +}