espresso_types/v0/v0_4/
header.rs

1use crate::{v0::impls::StakeTableHash, v0_3::RewardAmount, v0_4::RewardMerkleCommitmentV2, NsTable, TimestampMillis};
2
3use super::{
4    BlockMerkleCommitment, BuilderSignature, FeeInfo, FeeMerkleCommitment, L1BlockInfo,
5    ResolvableChainConfig,
6};
7use ark_serialize::CanonicalSerialize;
8use committable::{Commitment, Committable, RawCommitmentBuilder};
9use hotshot_types::{data::VidCommitment, utils::BuilderCommitment};
10use serde::{Deserialize, Serialize};
11
12/// A header is like a [`Block`] with the body replaced by a digest.
13#[derive(Clone, Debug, Deserialize, Serialize, Hash, PartialEq, Eq)]
14pub struct Header {
15    /// A commitment to a ChainConfig or a full ChainConfig.
16    pub(crate) chain_config: ResolvableChainConfig,
17    pub(crate) height: u64,
18    pub(crate) timestamp: u64,
19    pub(crate) timestamp_millis: TimestampMillis,
20    pub(crate) l1_head: u64,
21    pub(crate) l1_finalized: Option<L1BlockInfo>,
22    pub(crate) payload_commitment: VidCommitment,
23    pub(crate) builder_commitment: BuilderCommitment,
24    pub(crate) ns_table: NsTable,
25    pub(crate) block_merkle_tree_root: BlockMerkleCommitment,
26    pub(crate) fee_merkle_tree_root: FeeMerkleCommitment,
27    pub(crate) fee_info: FeeInfo,
28    pub(crate) builder_signature: Option<BuilderSignature>,
29    pub(crate) reward_merkle_tree_root: RewardMerkleCommitmentV2,
30    pub(crate) total_reward_distributed: RewardAmount,
31    pub(crate) next_stake_table_hash: Option<StakeTableHash>,
32}
33
34impl Committable for Header {
35    fn commit(&self) -> Commitment<Self> {
36        let mut bmt_bytes = vec![];
37        self.block_merkle_tree_root
38            .serialize_with_mode(&mut bmt_bytes, ark_serialize::Compress::Yes)
39            .unwrap();
40        let mut fmt_bytes = vec![];
41        self.fee_merkle_tree_root
42            .serialize_with_mode(&mut fmt_bytes, ark_serialize::Compress::Yes)
43            .unwrap();
44
45        let mut rwd_bytes = vec![];
46        self.reward_merkle_tree_root
47            .serialize_with_mode(&mut rwd_bytes, ark_serialize::Compress::Yes)
48            .unwrap();
49
50        let mut cb = RawCommitmentBuilder::new(&Self::tag())
51            .field("chain_config", self.chain_config.commit())
52            .u64_field("height", self.height)
53            .u64_field("timestamp", self.timestamp)
54            .u64_field("timestamp_millis", self.timestamp_millis.u64())
55            .u64_field("l1_head", self.l1_head)
56            .optional("l1_finalized", &self.l1_finalized)
57            .constant_str("payload_commitment")
58            .fixed_size_bytes(self.payload_commitment.as_ref())
59            .constant_str("builder_commitment")
60            .fixed_size_bytes(self.builder_commitment.as_ref())
61            .field("ns_table", self.ns_table.commit())
62            .var_size_field("block_merkle_tree_root", &bmt_bytes)
63            .var_size_field("fee_merkle_tree_root", &fmt_bytes)
64            .field("fee_info", self.fee_info.commit())
65            .var_size_field("reward_merkle_tree_root", &rwd_bytes)
66            .var_size_field("total_reward_distributed", &self.total_reward_distributed.to_fixed_bytes());
67
68        if let Some(next_stake_table_hash) = self.next_stake_table_hash {
69            cb = cb.field("next_stake_table_hash", next_stake_table_hash);
70        }
71
72        cb.finalize()
73    }
74
75    fn tag() -> String {
76        crate::v0_1::Header::tag()
77    }
78}