Skip to main content

miden_node_store/state/view/
transaction_inputs.rs

1//! Transaction input query for the block producer's transaction validation.
2
3use std::collections::HashSet;
4use std::ops::ControlFlow;
5
6use miden_node_tracing::instrument;
7use miden_protocol::Word;
8use miden_protocol::account::AccountId;
9use miden_protocol::note::{NoteId, Nullifier};
10
11use super::StateView;
12use crate::db::NullifierInfo;
13use crate::errors::DatabaseError;
14
15/// Store-level inputs for validating a proven transaction.
16#[derive(Debug, Default)]
17pub struct TransactionInputs {
18    pub account_commitment: Word,
19    pub nullifiers: Vec<NullifierInfo>,
20    pub found_unauthenticated_notes: HashSet<NoteId>,
21    pub new_account_id_prefix_is_unique: Option<bool>,
22}
23
24impl StateView {
25    /// Returns data needed by the block producer to verify transaction validity.
26    #[instrument(
27        fields(
28            account.id = account_id,
29            note.nullifiers = nullifiers,
30        ),
31    )]
32    pub async fn get_transaction_inputs(
33        &self,
34        account_id: AccountId,
35        nullifiers: &[Nullifier],
36        unauthenticated_note_ids: Vec<NoteId>,
37    ) -> Result<TransactionInputs, DatabaseError> {
38        let tree_inputs = self.with_inner_read_blocking(|inner| {
39            let account_commitment = inner.account_tree.get_latest_commitment(account_id);
40
41            let new_account_id_prefix_is_unique = if account_commitment.is_empty() {
42                Some(!inner.account_tree.contains_account_id_prefix_in_latest(account_id.prefix()))
43            } else {
44                None
45            };
46
47            // Non-unique account Id prefixes for new accounts are not allowed, so the transaction
48            // cannot be valid and the response is already complete.
49            if let Some(false) = new_account_id_prefix_is_unique {
50                return ControlFlow::Break(TransactionInputs {
51                    new_account_id_prefix_is_unique,
52                    ..Default::default()
53                });
54            }
55
56            let nullifiers = nullifiers
57                .iter()
58                .map(|nullifier| NullifierInfo {
59                    nullifier: *nullifier,
60                    block_num: inner.nullifier_tree.get_block_num(nullifier).unwrap_or_default(),
61                })
62                .collect();
63
64            ControlFlow::Continue((account_commitment, nullifiers, new_account_id_prefix_is_unique))
65        });
66        // `Break` carries a complete response (duplicate account ID prefix), so it is returned
67        // as-is without the note lookup below; `Continue` carries the tree reads needed to build
68        // the full response.
69        let (account_commitment, nullifiers, new_account_id_prefix_is_unique) = match tree_inputs {
70            ControlFlow::Continue(inputs) => inputs,
71            ControlFlow::Break(response) => return Ok(response),
72        };
73
74        // Scope the note lookup by the view's tip so the result is consistent with the tree reads
75        // above: mid-apply, the DB may already contain notes from a block the snapshot does not
76        // include yet.
77        let found_unauthenticated_notes =
78            self.db.select_existing_note_ids(unauthenticated_note_ids, self.tip()).await?;
79
80        Ok(TransactionInputs {
81            account_commitment,
82            nullifiers,
83            found_unauthenticated_notes,
84            new_account_id_prefix_is_unique,
85        })
86    }
87}