Skip to main content

miden_protocol/block/
block_inputs.rs

1use alloc::collections::BTreeMap;
2
3use crate::account::AccountId;
4use crate::block::BlockHeader;
5use crate::block::account_tree::AccountWitness;
6use crate::block::nullifier_tree::NullifierWitness;
7use crate::note::{NoteId, NoteInclusionProof, Nullifier};
8use crate::transaction::PartialBlockchain;
9use crate::utils::serde::{
10    ByteReader,
11    ByteWriter,
12    Deserializable,
13    DeserializationError,
14    Serializable,
15};
16
17// BLOCK INPUTS
18// ================================================================================================
19
20/// The required inputs for building a [`ProposedBlock`](crate::block::ProposedBlock).
21#[derive(Clone, Debug)]
22pub struct BlockInputs {
23    /// The previous block header that the block should reference.
24    prev_block_header: BlockHeader,
25
26    /// The chain state at the previous block with authentication paths for:
27    /// - each block referenced by a batch in the block,
28    /// - each block referenced by a note inclusion proof for an unauthenticated note.
29    partial_blockchain: PartialBlockchain,
30
31    /// The state commitments of the accounts in the block and their authentication paths.
32    account_witnesses: BTreeMap<AccountId, AccountWitness>,
33
34    /// The nullifiers of the notes consumed in the block and their authentication paths.
35    nullifier_witnesses: BTreeMap<Nullifier, NullifierWitness>,
36
37    /// Note inclusion proofs for all unauthenticated notes in the block that are not erased (i.e.
38    /// created and consumed within the block).
39    unauthenticated_note_proofs: BTreeMap<NoteId, NoteInclusionProof>,
40}
41
42impl BlockInputs {
43    /// Creates new [`BlockInputs`] from the provided parts.
44    pub fn new(
45        prev_block_header: BlockHeader,
46        partial_blockchain: PartialBlockchain,
47        account_witnesses: BTreeMap<AccountId, AccountWitness>,
48        nullifier_witnesses: BTreeMap<Nullifier, NullifierWitness>,
49        unauthenticated_note_proofs: BTreeMap<NoteId, NoteInclusionProof>,
50    ) -> Self {
51        Self {
52            prev_block_header,
53            partial_blockchain,
54            account_witnesses,
55            nullifier_witnesses,
56            unauthenticated_note_proofs,
57        }
58    }
59
60    /// Returns a reference to the previous block header.
61    pub fn prev_block_header(&self) -> &BlockHeader {
62        &self.prev_block_header
63    }
64
65    /// Returns a reference to the [`PartialBlockchain`].
66    pub fn partial_blockchain(&self) -> &PartialBlockchain {
67        &self.partial_blockchain
68    }
69
70    /// Returns a reference to the account witnesses.
71    pub fn account_witnesses(&self) -> &BTreeMap<AccountId, AccountWitness> {
72        &self.account_witnesses
73    }
74
75    /// Returns a reference to the nullifier witnesses.
76    pub fn nullifier_witnesses(&self) -> &BTreeMap<Nullifier, NullifierWitness> {
77        &self.nullifier_witnesses
78    }
79
80    /// Returns a reference to the note inclusion proofs.
81    pub fn unauthenticated_note_proofs(&self) -> &BTreeMap<NoteId, NoteInclusionProof> {
82        &self.unauthenticated_note_proofs
83    }
84
85    /// Consumes self and returns the underlying parts.
86    #[allow(clippy::type_complexity)]
87    pub fn into_parts(
88        self,
89    ) -> (
90        BlockHeader,
91        PartialBlockchain,
92        BTreeMap<AccountId, AccountWitness>,
93        BTreeMap<Nullifier, NullifierWitness>,
94        BTreeMap<NoteId, NoteInclusionProof>,
95    ) {
96        (
97            self.prev_block_header,
98            self.partial_blockchain,
99            self.account_witnesses,
100            self.nullifier_witnesses,
101            self.unauthenticated_note_proofs,
102        )
103    }
104
105    // TESTING
106    // --------------------------------------------------------------------------------------------
107
108    /// Returns a mutable reference to the [`PartialBlockchain`].
109    ///
110    /// Allows mutating the inner partial blockchain for testing purposes.
111    #[cfg(any(feature = "testing", test))]
112    pub fn partial_blockchain_mut(&mut self) -> &mut PartialBlockchain {
113        &mut self.partial_blockchain
114    }
115
116    /// Returns a mutable reference to the note inclusion proofs.
117    ///
118    /// Allows mutating the inner note proofs map for testing purposes.
119    #[cfg(any(feature = "testing", test))]
120    pub fn unauthenticated_note_proofs_mut(&mut self) -> &mut BTreeMap<NoteId, NoteInclusionProof> {
121        &mut self.unauthenticated_note_proofs
122    }
123
124    /// Returns a mutable reference to the nullifier witnesses.
125    ///
126    /// Allows mutating the inner nullifier witnesses map for testing purposes.
127    #[cfg(any(feature = "testing", test))]
128    pub fn nullifier_witnesses_mut(&mut self) -> &mut BTreeMap<Nullifier, NullifierWitness> {
129        &mut self.nullifier_witnesses
130    }
131
132    /// Returns a mutable reference to the account witnesses.
133    ///
134    /// Allows mutating the inner account witnesses map for testing purposes.
135    #[cfg(any(feature = "testing", test))]
136    pub fn account_witnesses_mut(&mut self) -> &mut BTreeMap<AccountId, AccountWitness> {
137        &mut self.account_witnesses
138    }
139}
140
141// SERIALIZATION
142// ================================================================================================
143
144impl Serializable for BlockInputs {
145    fn write_into<W: ByteWriter>(&self, target: &mut W) {
146        self.prev_block_header.write_into(target);
147        self.partial_blockchain.write_into(target);
148        self.account_witnesses.write_into(target);
149        self.nullifier_witnesses.write_into(target);
150        self.unauthenticated_note_proofs.write_into(target);
151    }
152}
153
154impl Deserializable for BlockInputs {
155    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
156        let prev_block_header = BlockHeader::read_from(source)?;
157        let partial_blockchain = PartialBlockchain::read_from(source)?;
158        let account_witnesses = BTreeMap::<AccountId, AccountWitness>::read_from(source)?;
159        let nullifier_witnesses = BTreeMap::<Nullifier, NullifierWitness>::read_from(source)?;
160        let unauthenticated_note_proofs =
161            BTreeMap::<NoteId, NoteInclusionProof>::read_from(source)?;
162
163        Ok(Self::new(
164            prev_block_header,
165            partial_blockchain,
166            account_witnesses,
167            nullifier_witnesses,
168            unauthenticated_note_proofs,
169        ))
170    }
171}