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