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 as to whether the arguments correctly correspond
37    /// to each other, which could cause errors downstream.
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 body of the block.
58    pub fn body(&self) -> &BlockBody {
59        &self.body
60    }
61
62    /// Returns the Validator's signature over the block header.
63    pub fn signature(&self) -> &Signature {
64        &self.signature
65    }
66
67    /// Returns the proof of the block.
68    pub fn proof(&self) -> &BlockProof {
69        &self.proof
70    }
71
72    /// Destructures this proven block into individual parts.
73    pub fn into_parts(self) -> (BlockHeader, BlockBody, Signature, BlockProof) {
74        (self.header, self.body, self.signature, self.proof)
75    }
76}
77
78// SERIALIZATION
79// ================================================================================================
80
81impl Serializable for ProvenBlock {
82    fn write_into<W: ByteWriter>(&self, target: &mut W) {
83        self.header.write_into(target);
84        self.body.write_into(target);
85        self.signature.write_into(target);
86        self.proof.write_into(target);
87    }
88}
89
90impl Deserializable for ProvenBlock {
91    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
92        let block = Self {
93            header: BlockHeader::read_from(source)?,
94            body: BlockBody::read_from(source)?,
95            signature: Signature::read_from(source)?,
96            proof: BlockProof::read_from(source)?,
97        };
98
99        Ok(block)
100    }
101}