dusk_node_data/ledger/
header.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4//
5// Copyright (c) DUSK NETWORK. All rights reserved.
6
7use serde::Serialize;
8
9use super::*;
10use crate::message::ConsensusHeader;
11
12pub type Seed = Signature;
13#[derive(Eq, PartialEq, Clone, Serialize)]
14#[cfg_attr(any(feature = "faker", test), derive(Dummy))]
15pub struct Header {
16    // Hashable fields
17    pub version: u8,
18    pub height: u64,
19    pub timestamp: u64,
20    #[serde(serialize_with = "crate::serialize_hex")]
21    pub prev_block_hash: Hash,
22    pub seed: Seed,
23    #[serde(serialize_with = "crate::serialize_hex")]
24    pub state_hash: Hash,
25    #[serde(serialize_with = "crate::serialize_hex")]
26    pub event_bloom: Bloom,
27    pub generator_bls_pubkey: PublicKeyBytes,
28    #[serde(serialize_with = "crate::serialize_hex")]
29    pub txroot: Hash,
30    #[serde(serialize_with = "crate::serialize_hex")]
31    pub faultroot: Hash,
32    pub gas_limit: u64,
33    #[cfg_attr(any(feature = "faker", test), dummy(faker = "0..50"))]
34    pub iteration: u8,
35    pub prev_block_cert: Attestation,
36    pub failed_iterations: IterationsInfo,
37
38    // Block hash
39    #[serde(serialize_with = "crate::serialize_hex")]
40    pub hash: Hash,
41
42    pub signature: Signature,
43
44    // Non-hashable fields
45    #[serde(skip_serializing)]
46    pub att: Attestation,
47}
48
49impl Default for Header {
50    fn default() -> Self {
51        Self {
52            version: Default::default(),
53            height: Default::default(),
54            timestamp: Default::default(),
55            prev_block_hash: Default::default(),
56            seed: Default::default(),
57            state_hash: Default::default(),
58            event_bloom: [0u8; 256],
59            generator_bls_pubkey: Default::default(),
60            txroot: Default::default(),
61            faultroot: Default::default(),
62            gas_limit: Default::default(),
63            iteration: Default::default(),
64            prev_block_cert: Default::default(),
65            failed_iterations: Default::default(),
66            hash: Default::default(),
67            signature: Default::default(),
68            att: Default::default(),
69        }
70    }
71}
72
73impl Header {
74    pub fn size(&self) -> usize {
75        let mut buf = vec![];
76        self.write(&mut buf).expect("write to vec should not fail");
77        buf.len()
78    }
79}
80
81impl std::fmt::Debug for Header {
82    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83        let timestamp =
84            chrono::DateTime::from_timestamp(self.timestamp as i64, 0)
85                .map_or_else(|| "unknown".to_owned(), |v| v.to_rfc2822());
86
87        f.debug_struct("Header")
88            .field("version", &self.version)
89            .field("height", &self.height)
90            .field("iteration", &self.iteration)
91            .field("timestamp", &timestamp)
92            .field("prev_block_hash", &to_str(&self.prev_block_hash))
93            .field("seed", &to_str(self.seed.inner()))
94            .field("state_hash", &to_str(&self.state_hash))
95            .field("event_hash", &to_str(&self.event_bloom))
96            .field("gen_bls_pubkey", &to_str(self.generator_bls_pubkey.inner()))
97            .field("gas_limit", &self.gas_limit)
98            .field("hash", &to_str(&self.hash))
99            .field("signature", &to_str(self.signature.inner()))
100            .field("att", &self.att)
101            .field("tx_root", &to_str(&self.txroot))
102            .field("fault_root", &to_str(&self.faultroot))
103            .finish()
104    }
105}
106
107impl Header {
108    /// Return the corresponding ConsensusHeader
109    pub fn to_consensus_header(&self) -> ConsensusHeader {
110        ConsensusHeader {
111            prev_block_hash: self.prev_block_hash,
112            round: self.height,
113            iteration: self.iteration,
114        }
115    }
116
117    /// Marshal hashable fields.
118    pub(crate) fn marshal_hashable<W: Write>(
119        &self,
120        w: &mut W,
121    ) -> io::Result<()> {
122        w.write_all(&self.version.to_le_bytes())?;
123        w.write_all(&self.height.to_le_bytes())?;
124        w.write_all(&self.timestamp.to_le_bytes())?;
125        w.write_all(&self.prev_block_hash)?;
126
127        w.write_all(self.seed.inner())?;
128
129        w.write_all(&self.state_hash)?;
130        w.write_all(&self.event_bloom)?;
131        w.write_all(self.generator_bls_pubkey.inner())?;
132        w.write_all(&self.txroot)?;
133        w.write_all(&self.faultroot)?;
134        w.write_all(&self.gas_limit.to_le_bytes())?;
135        w.write_all(&self.iteration.to_le_bytes())?;
136        self.prev_block_cert.write(w)?;
137        self.failed_iterations.write(w)?;
138
139        Ok(())
140    }
141
142    pub(crate) fn unmarshal_hashable<R: Read>(r: &mut R) -> io::Result<Self> {
143        let version = Self::read_u8(r)?;
144        let height = Self::read_u64_le(r)?;
145        let timestamp = Self::read_u64_le(r)?;
146
147        let prev_block_hash = Self::read_bytes(r)?;
148        let seed = Self::read_bytes(r)?;
149        let state_hash = Self::read_bytes(r)?;
150        let event_hash = Self::read_bytes(r)?;
151        let generator_bls_pubkey = Self::read_bytes(r)?;
152        let txroot = Self::read_bytes(r)?;
153        let faultroot = Self::read_bytes(r)?;
154        let gas_limit = Self::read_u64_le(r)?;
155        let iteration = Self::read_u8(r)?;
156
157        let prev_block_cert = Attestation::read(r)?;
158        let failed_iterations = IterationsInfo::read(r)?;
159
160        Ok(Header {
161            version,
162            height,
163            timestamp,
164            gas_limit,
165            prev_block_hash,
166            seed: Seed::from(seed),
167            generator_bls_pubkey: PublicKeyBytes(generator_bls_pubkey),
168            iteration,
169            state_hash,
170            event_bloom: event_hash,
171            txroot,
172            faultroot,
173            hash: [0; 32],
174            att: Default::default(),
175            prev_block_cert,
176            failed_iterations,
177            signature: Default::default(),
178        })
179    }
180}