miden_objects/block/
block_inputs.rs

1use alloc::collections::BTreeMap;
2
3use crate::{
4    account::AccountId,
5    block::{AccountWitness, BlockHeader, NullifierWitness},
6    note::{NoteId, NoteInclusionProof, Nullifier},
7    transaction::ChainMmr,
8};
9
10// BLOCK INPUTS
11// ================================================================================================
12
13/// The required inputs for building a [`ProposedBlock`](crate::block::ProposedBlock).
14#[derive(Clone, Debug)]
15pub struct BlockInputs {
16    /// The previous block header that the block should reference.
17    prev_block_header: BlockHeader,
18
19    /// The chain state at the previous block with authentication paths for:
20    /// - each block referenced by a batch in the block,
21    /// - each block referenced by a note inclusion proof for an unauthenticated note.
22    chain_mmr: ChainMmr,
23
24    /// The state commitments of the accounts in the block and their authentication paths.
25    account_witnesses: BTreeMap<AccountId, AccountWitness>,
26
27    /// The nullifiers of the notes consumed in the block and their authentication paths.
28    nullifier_witnesses: BTreeMap<Nullifier, NullifierWitness>,
29
30    /// Note inclusion proofs for all unauthenticated notes in the block that are not erased (i.e.
31    /// created and consumed within the block).
32    unauthenticated_note_proofs: BTreeMap<NoteId, NoteInclusionProof>,
33}
34
35impl BlockInputs {
36    /// Creates new [`BlockInputs`] from the provided parts.
37    pub fn new(
38        prev_block_header: BlockHeader,
39        chain_mmr: ChainMmr,
40        account_witnesses: BTreeMap<AccountId, AccountWitness>,
41        nullifier_witnesses: BTreeMap<Nullifier, NullifierWitness>,
42        unauthenticated_note_proofs: BTreeMap<NoteId, NoteInclusionProof>,
43    ) -> Self {
44        Self {
45            prev_block_header,
46            chain_mmr,
47            account_witnesses,
48            nullifier_witnesses,
49            unauthenticated_note_proofs,
50        }
51    }
52
53    /// Returns a reference to the previous block header.
54    pub fn prev_block_header(&self) -> &BlockHeader {
55        &self.prev_block_header
56    }
57
58    /// Returns a reference to the [`ChainMmr`].
59    pub fn chain_mmr(&self) -> &ChainMmr {
60        &self.chain_mmr
61    }
62
63    /// Returns a reference to the account witnesses.
64    pub fn account_witnesses(&self) -> &BTreeMap<AccountId, AccountWitness> {
65        &self.account_witnesses
66    }
67
68    /// Returns a reference to the nullifier witnesses.
69    pub fn nullifier_witnesses(&self) -> &BTreeMap<Nullifier, NullifierWitness> {
70        &self.nullifier_witnesses
71    }
72
73    /// Returns a reference to the note inclusion proofs.
74    pub fn unauthenticated_note_proofs(&self) -> &BTreeMap<NoteId, NoteInclusionProof> {
75        &self.unauthenticated_note_proofs
76    }
77
78    /// Consumes self and returns the underlying parts.
79    #[allow(clippy::type_complexity)]
80    pub fn into_parts(
81        self,
82    ) -> (
83        BlockHeader,
84        ChainMmr,
85        BTreeMap<AccountId, AccountWitness>,
86        BTreeMap<Nullifier, NullifierWitness>,
87        BTreeMap<NoteId, NoteInclusionProof>,
88    ) {
89        (
90            self.prev_block_header,
91            self.chain_mmr,
92            self.account_witnesses,
93            self.nullifier_witnesses,
94            self.unauthenticated_note_proofs,
95        )
96    }
97
98    // TESTING
99    // --------------------------------------------------------------------------------------------
100
101    /// Returns a mutable reference to the [`ChainMmr`].
102    ///
103    /// Allows mutating the inner chain MMR for testing purposes.
104    #[cfg(any(feature = "testing", test))]
105    pub fn chain_mmr_mut(&mut self) -> &mut ChainMmr {
106        &mut self.chain_mmr
107    }
108
109    /// Returns a mutable reference to the note inclusion proofs.
110    ///
111    /// Allows mutating the inner note proofs map for testing purposes.
112    #[cfg(any(feature = "testing", test))]
113    pub fn unauthenticated_note_proofs_mut(&mut self) -> &mut BTreeMap<NoteId, NoteInclusionProof> {
114        &mut self.unauthenticated_note_proofs
115    }
116
117    /// Returns a mutable reference to the nullifier witnesses.
118    ///
119    /// Allows mutating the inner nullifier witnesses map for testing purposes.
120    #[cfg(any(feature = "testing", test))]
121    pub fn nullifier_witnesses_mut(&mut self) -> &mut BTreeMap<Nullifier, NullifierWitness> {
122        &mut self.nullifier_witnesses
123    }
124
125    /// Returns a mutable reference to the account witnesses.
126    ///
127    /// Allows mutating the inner account witnesses map for testing purposes.
128    #[cfg(any(feature = "testing", test))]
129    pub fn account_witnesses_mut(&mut self) -> &mut BTreeMap<AccountId, AccountWitness> {
130        &mut self.account_witnesses
131    }
132}