Skip to main content

miden_protocol/block/
proven_block.rs

1use miden_crypto::dsa::ecdsa_k256_keccak::Signature;
2
3use crate::MIN_PROOF_SECURITY_LEVEL;
4use crate::block::{BlockBody, BlockHeader, BlockProof};
5use crate::utils::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable};
6
7// PROVEN BLOCK
8// ================================================================================================
9
10/// Represents a block in the Miden blockchain that has been signed and proven.
11///
12/// Blocks transition through proposed, signed, and proven states. This struct represents the final,
13/// proven state of a block.
14///
15/// Proven blocks are the final, canonical blocks in the chain.
16#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct ProvenBlock {
18    /// The header of the proven block.
19    header: BlockHeader,
20
21    /// The body of the proven block.
22    body: BlockBody,
23
24    /// The validator's signature over the block header.
25    signature: Signature,
26
27    /// The proof of the block.
28    proof: BlockProof,
29}
30
31impl ProvenBlock {
32    /// Returns a new [`ProvenBlock`] instantiated from the provided components.
33    ///
34    /// # Warning
35    ///
36    /// This constructor does not do any validation, so passing incorrect values may lead to later
37    /// panics.
38    pub fn new_unchecked(
39        header: BlockHeader,
40        body: BlockBody,
41        signature: Signature,
42        proof: BlockProof,
43    ) -> Self {
44        Self { header, signature, body, proof }
45    }
46
47    /// Returns the proof security level of the block.
48    pub fn proof_security_level(&self) -> u32 {
49        MIN_PROOF_SECURITY_LEVEL
50    }
51
52    /// Returns the header of the block.
53    pub fn header(&self) -> &BlockHeader {
54        &self.header
55    }
56
57    /// Returns the validator's signature over the block header.
58    pub fn signature(&self) -> &Signature {
59        &self.signature
60    }
61
62    /// Returns the body of the block.
63    pub fn body(&self) -> &BlockBody {
64        &self.body
65    }
66
67    /// Returns the proof of the block.
68    pub fn proof(&self) -> &BlockProof {
69        &self.proof
70    }
71}
72
73// SERIALIZATION
74// ================================================================================================
75
76impl Serializable for ProvenBlock {
77    fn write_into<W: ByteWriter>(&self, target: &mut W) {
78        self.header.write_into(target);
79        self.body.write_into(target);
80        self.signature.write_into(target);
81        self.proof.write_into(target);
82    }
83}
84
85impl Deserializable for ProvenBlock {
86    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
87        let block = Self {
88            header: BlockHeader::read_from(source)?,
89            body: BlockBody::read_from(source)?,
90            signature: Signature::read_from(source)?,
91            proof: BlockProof::read_from(source)?,
92        };
93
94        Ok(block)
95    }
96}