miden_protocol/block/blockchain.rs
1use alloc::collections::BTreeSet;
2
3use miden_crypto::merkle::mmr::{Forest, Mmr, MmrError, MmrPeaks, MmrProof, PartialMmr};
4
5use crate::Word;
6use crate::block::BlockNumber;
7use crate::utils::serde::{
8 ByteReader,
9 ByteWriter,
10 Deserializable,
11 DeserializationError,
12 Serializable,
13};
14
15/// The [Merkle Mountain Range](Mmr) defining the Miden blockchain.
16///
17/// The values of the leaves in the MMR are the commitments of blocks, i.e.
18/// [`BlockHeader::commitment`](crate::block::BlockHeader::commitment).
19///
20/// Each new block updates the blockchain by adding **the previous block's commitment** to the MMR.
21/// This means the chain commitment found in block 10's header commits to all blocks 0..=9, but not
22/// 10 itself. This results from the fact that block 10 cannot compute its own block commitment
23/// and thus cannot add itself to the chain. Hence, the blockchain is lagging behind by one block.
24///
25/// Some APIs take a _checkpoint_ which is equivalent to the concept of _forest_ of the underlying
26/// MMR. As an example, if the blockchain has 20 blocks in total, and the checkpoint is 10, then
27/// the API works in the context of the chain at the time it had 10 blocks, i.e. it contains blocks
28/// 0..=9. This is useful, for example, to retrieve proofs that are valid when verified against the
29/// chain commitment of block 10.
30///
31/// The maximum number of supported blocks is [`u32::MAX`]. This is not validated however.
32#[derive(Debug, Clone)]
33pub struct Blockchain {
34 mmr: Mmr,
35}
36
37impl Blockchain {
38 // CONSTRUCTORS
39 // --------------------------------------------------------------------------------------------
40
41 /// Returns a new, empty blockchain.
42 pub fn new() -> Self {
43 Self { mmr: Mmr::new() }
44 }
45
46 /// Construct a new blockchain from an [`Mmr`] without validation.
47 pub fn from_mmr_unchecked(mmr: Mmr) -> Self {
48 Self { mmr }
49 }
50
51 // PUBLIC ACCESSORS
52 // --------------------------------------------------------------------------------------------
53
54 /// Returns the number of blocks in the chain.
55 pub fn num_blocks(&self) -> u32 {
56 // SAFETY: The chain should never contain more than u32::MAX blocks, so a non-panicking cast
57 // should be fine.
58 self.mmr.forest().num_leaves() as u32
59 }
60
61 /// Returns the tip of the chain, i.e. the number of the latest block in the chain, unless the
62 /// chain is empty.
63 pub fn chain_tip(&self) -> Option<BlockNumber> {
64 if self.num_blocks() == 0 {
65 return None;
66 }
67
68 Some(BlockNumber::from(self.num_blocks() - 1))
69 }
70
71 /// Returns the chain commitment.
72 pub fn commitment(&self) -> Word {
73 self.peaks().hash_peaks()
74 }
75
76 /// Returns the current peaks of the MMR.
77 pub fn peaks(&self) -> MmrPeaks {
78 self.mmr.peaks()
79 }
80
81 /// Returns the peaks of the chain at the state of the given block.
82 ///
83 /// Note that this represents the state of the chain where the block at the given number **is
84 /// not yet** in the chain. For example, if the given block number is 5, then the returned peaks
85 /// represent the chain whose latest block is 4. See the type-level documentation for why this
86 /// is the case.
87 ///
88 /// # Errors
89 ///
90 /// Returns an error if the specified `block` exceeds the number of blocks in the chain.
91 pub fn peaks_at(&self, checkpoint: BlockNumber) -> Result<MmrPeaks, MmrError> {
92 self.mmr.peaks_at(Forest::new(checkpoint.as_usize()))
93 }
94
95 /// Returns an [`MmrProof`] for the `block` with the given number.
96 ///
97 /// # Errors
98 ///
99 /// Returns an error if:
100 /// - The specified block number does not exist in the chain.
101 pub fn open(&self, block: BlockNumber) -> Result<MmrProof, MmrError> {
102 self.mmr.open(block.as_usize())
103 }
104
105 /// Returns an [`MmrProof`] for the `block` with the given number at the state of the given
106 /// `checkpoint`.
107 ///
108 /// # Errors
109 ///
110 /// Returns an error if:
111 /// - The specified block number does not exist in the chain.
112 /// - The specified checkpoint number exceeds the number of blocks in the chain.
113 pub fn open_at(
114 &self,
115 block: BlockNumber,
116 checkpoint: BlockNumber,
117 ) -> Result<MmrProof, MmrError> {
118 self.mmr.open_at(block.as_usize(), Forest::new(checkpoint.as_usize()))
119 }
120
121 /// Returns a reference to the underlying [`Mmr`].
122 pub fn as_mmr(&self) -> &Mmr {
123 &self.mmr
124 }
125
126 /// Creates a [`PartialMmr`] at the state of the given block. This means the hashed peaks of the
127 /// returned partial MMR will match the checkpoint's chain commitment. This MMR will include
128 /// authentication paths for all blocks in the provided `blocks` set.
129 ///
130 /// # Errors
131 ///
132 /// Returns an error if:
133 /// - the specified `latest_block_number` exceeds the number of blocks in the chain.
134 /// - any block in `blocks` is not in the state of the chain specified by `latest_block_number`.
135 pub fn partial_mmr_from_blocks(
136 &self,
137 blocks: &BTreeSet<BlockNumber>,
138 checkpoint: BlockNumber,
139 ) -> Result<PartialMmr, MmrError> {
140 // Using latest block as the target state means we take the state of the MMR one before
141 // the latest block.
142 let peaks = self.peaks_at(checkpoint)?;
143
144 // Track the merkle paths of the requested blocks in the partial MMR.
145 let mut partial_mmr = PartialMmr::from_peaks(peaks);
146 for block_num in blocks.iter() {
147 let leaf = self.mmr.get(block_num.as_usize())?;
148 let proof = self.open_at(*block_num, checkpoint)?;
149
150 // SAFETY: We should be able to fill the partial MMR with data from the partial
151 // blockchain without errors, otherwise it indicates the blockchain is
152 // invalid.
153 partial_mmr
154 .track(block_num.as_usize(), leaf, proof.merkle_path())
155 .expect("filling partial mmr with data from mmr should succeed");
156 }
157
158 Ok(partial_mmr)
159 }
160
161 // PUBLIC MUTATORS
162 // --------------------------------------------------------------------------------------------
163
164 /// Adds a block commitment to the MMR.
165 ///
166 /// The caller must ensure that this commitent is the one for the next block in the chain.
167 pub fn push(&mut self, block_commitment: Word) {
168 self.mmr.add(block_commitment);
169 }
170}
171
172impl Default for Blockchain {
173 fn default() -> Self {
174 Self::new()
175 }
176}
177
178// SERIALIZATION
179// ================================================================================================
180
181impl Serializable for Blockchain {
182 fn write_into<W: ByteWriter>(&self, target: &mut W) {
183 self.mmr.write_into(target);
184 }
185}
186
187impl Deserializable for Blockchain {
188 fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
189 let chain = Mmr::read_from(source)?;
190 Ok(Self::from_mmr_unchecked(chain))
191 }
192}