diff --git a/crates/primitives/src/header.rs b/crates/primitives/src/header.rs index b69ea4941c..951d1f46de 100644 --- a/crates/primitives/src/header.rs +++ b/crates/primitives/src/header.rs @@ -289,16 +289,33 @@ pub struct SealedHeader { hash: BlockHash, } +impl SealedHeader { + /// Extract raw header that can be modified. + pub fn unseal(self) -> Header { + self.header + } + + /// Return header/block hash. + pub fn hash(&self) -> BlockHash { + self.hash + } + + /// Return the number hash tuple. + pub fn num_hash(&self) -> (BlockNumber, BlockHash) { + (self.number, self.hash) + } +} + #[cfg(any(test, feature = "arbitrary"))] impl proptest::arbitrary::Arbitrary for SealedHeader { type Parameters = (); - type Strategy = proptest::strategy::BoxedStrategy; - fn arbitrary_with(_: Self::Parameters) -> Self::Strategy { use proptest::prelude::{any, Strategy}; any::<(Header, BlockHash)>().prop_map(move |(header, _)| header.seal_slow()).boxed() } + + type Strategy = proptest::strategy::BoxedStrategy; } #[cfg(any(test, feature = "arbitrary"))] @@ -308,21 +325,33 @@ impl<'a> arbitrary::Arbitrary<'a> for SealedHeader { } } -impl From> for SealedHeader { - fn from(block: Block) -> Self { - let header = Header { +impl From<&Block> for Header { + fn from(block: &Block) -> Self { + Header { + parent_hash: block.parent_hash.0.into(), number: block.number.unwrap().as_u64(), gas_limit: block.gas_limit.as_u64(), difficulty: block.difficulty.into(), nonce: block.nonce.unwrap().to_low_u64_be(), - extra_data: block.extra_data.0.into(), + extra_data: block.extra_data.0.clone().into(), state_root: block.state_root.0.into(), + transactions_root: block.transactions_root.0.into(), + receipts_root: block.receipts_root.0.into(), timestamp: block.timestamp.as_u64(), mix_hash: block.mix_hash.unwrap().0.into(), beneficiary: block.author.unwrap().0.into(), base_fee_per_gas: block.base_fee_per_gas.map(|fee| fee.as_u64()), - ..Default::default() - }; + ommers_hash: block.uncles_hash.0.into(), + gas_used: block.gas_used.as_u64(), + withdrawals_root: None, + logs_bloom: block.logs_bloom.unwrap_or_default().0.into(), + } + } +} + +impl From<&Block> for SealedHeader { + fn from(block: &Block) -> Self { + let header = Header::from(block); match block.hash { Some(hash) => header.seal(hash.0.into()), None => header.seal_slow(), @@ -375,23 +404,6 @@ impl Deref for SealedHeader { } } -impl SealedHeader { - /// Extract raw header that can be modified. - pub fn unseal(self) -> Header { - self.header - } - - /// Return header/block hash. - pub fn hash(&self) -> BlockHash { - self.hash - } - - /// Return the number hash tuple. - pub fn num_hash(&self) -> (BlockNumber, BlockHash) { - (self.number, self.hash) - } -} - /// Represents the direction for a headers request depending on the `reverse` field of the request. /// > The response must contain a number of block headers, of rising number when reverse is 0, /// > falling when 1 diff --git a/crates/staged-sync/tests/sync.rs b/crates/staged-sync/tests/sync.rs index fd4e91282b..4bb5d07f0f 100644 --- a/crates/staged-sync/tests/sync.rs +++ b/crates/staged-sync/tests/sync.rs @@ -101,7 +101,7 @@ async fn init_geth() -> (CliqueGethInstance, ChainSpec) { .clone() .expect("clique should be configured with a genesis") .into(); - let remote_genesis = SealedHeader::from(clique.provider.remote_genesis_block().await.unwrap()); + let remote_genesis = SealedHeader::from(&clique.provider.remote_genesis_block().await.unwrap()); let local_genesis = chainspec.genesis_header().seal(chainspec.genesis_hash()); assert_eq!(local_genesis, remote_genesis, "genesis blocks should match, we computed {local_genesis:#?} but geth computed {remote_genesis:#?}");