1use ethereum_types::{Bloom, H160, H256, H64, U256};
2use sha3::{Digest, Keccak256};
3
4use crate::Bytes;
5
6#[derive(Clone, Debug, PartialEq, Eq)]
8#[derive(rlp::RlpEncodable, rlp::RlpDecodable)]
9#[cfg_attr(
10 feature = "with-scale",
11 derive(scale_codec::Encode, scale_codec::Decode, scale_info::TypeInfo)
12)]
13#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
14pub struct Header {
15 pub parent_hash: H256,
16 pub ommers_hash: H256,
17 pub beneficiary: H160,
18 pub state_root: H256,
19 pub transactions_root: H256,
20 pub receipts_root: H256,
21 pub logs_bloom: Bloom,
22 pub difficulty: U256,
23 pub number: U256,
24 pub gas_limit: U256,
25 pub gas_used: U256,
26 pub timestamp: u64,
27 pub extra_data: Bytes,
28 pub mix_hash: H256,
29 pub nonce: H64,
30}
31
32impl Header {
33 #[must_use]
34 pub fn new(partial_header: PartialHeader, ommers_hash: H256, transactions_root: H256) -> Self {
35 Self {
36 parent_hash: partial_header.parent_hash,
37 ommers_hash,
38 beneficiary: partial_header.beneficiary,
39 state_root: partial_header.state_root,
40 transactions_root,
41 receipts_root: partial_header.receipts_root,
42 logs_bloom: partial_header.logs_bloom,
43 difficulty: partial_header.difficulty,
44 number: partial_header.number,
45 gas_limit: partial_header.gas_limit,
46 gas_used: partial_header.gas_used,
47 timestamp: partial_header.timestamp,
48 extra_data: partial_header.extra_data,
49 mix_hash: partial_header.mix_hash,
50 nonce: partial_header.nonce,
51 }
52 }
53
54 #[must_use]
55 pub fn hash(&self) -> H256 {
56 H256::from_slice(Keccak256::digest(rlp::encode(self)).as_slice())
57 }
58}
59
60#[derive(Clone, Debug, PartialEq, Eq)]
62pub struct PartialHeader {
63 pub parent_hash: H256,
64 pub beneficiary: H160,
65 pub state_root: H256,
66 pub receipts_root: H256,
67 pub logs_bloom: Bloom,
68 pub difficulty: U256,
69 pub number: U256,
70 pub gas_limit: U256,
71 pub gas_used: U256,
72 pub timestamp: u64,
73 pub extra_data: Bytes,
74 pub mix_hash: H256,
75 pub nonce: H64,
76}
77
78impl From<Header> for PartialHeader {
79 fn from(header: Header) -> PartialHeader {
80 Self {
81 parent_hash: header.parent_hash,
82 beneficiary: header.beneficiary,
83 state_root: header.state_root,
84 receipts_root: header.receipts_root,
85 logs_bloom: header.logs_bloom,
86 difficulty: header.difficulty,
87 number: header.number,
88 gas_limit: header.gas_limit,
89 gas_used: header.gas_used,
90 timestamp: header.timestamp,
91 extra_data: header.extra_data,
92 mix_hash: header.mix_hash,
93 nonce: header.nonce,
94 }
95 }
96}