miden_protocol/transaction/ordered_transactions.rs
1use alloc::vec::Vec;
2
3use crate::account::AccountId;
4use crate::transaction::{TransactionHeader, TransactionId};
5use crate::utils::serde::{
6 ByteReader,
7 ByteWriter,
8 Deserializable,
9 DeserializationError,
10 Serializable,
11};
12use crate::{Felt, Hasher, Word, ZERO};
13
14// ORDERED TRANSACTION HEADERS
15// ================================================================================================
16
17/// The ordered set of transaction headers in a [`ProvenBatch`](crate::batch::ProvenBatch) or
18/// [`ProvenBlock`](crate::block::ProvenBlock).
19///
20/// This is a newtype wrapper representing either:
21/// - the set of transactions in a **batch**,
22/// - or the flattened sets of transactions of each proven batch in a **block**.
23///
24/// This type cannot be constructed directly, but can be retrieved through:
25/// - [`ProposedBatch::transaction_headers`](crate::batch::ProposedBatch::transaction_headers),
26/// - [`OrderedBatches::into_transactions`](crate::batch::OrderedBatches::into_transactions).
27///
28/// The rationale for this requirement is that it allows a client to cheaply validate the
29/// correctness of the transactions in a proven block returned by a remote prover.
30#[derive(Debug, Clone, PartialEq, Eq)]
31pub struct OrderedTransactionHeaders(Vec<TransactionHeader>);
32
33impl OrderedTransactionHeaders {
34 /// Creates a new set of ordered transaction headers from the provided vector.
35 ///
36 /// # Warning
37 ///
38 /// See the type-level documentation for the requirements of the passed transactions.
39 pub fn new_unchecked(transactions: Vec<TransactionHeader>) -> Self {
40 Self(transactions)
41 }
42
43 /// Computes a commitment to the list of transactions.
44 ///
45 /// This is a sequential hash over each transaction's ID and its account ID.
46 pub fn commitment(&self) -> Word {
47 Self::compute_commitment(self.0.as_slice().iter().map(|tx| (tx.id(), tx.account_id())))
48 }
49
50 /// Returns a reference to the underlying transaction headers.
51 pub fn as_slice(&self) -> &[TransactionHeader] {
52 &self.0
53 }
54
55 /// Consumes self and returns the underlying vector of transaction headers.
56 pub fn into_vec(self) -> Vec<TransactionHeader> {
57 self.0
58 }
59
60 // PUBLIC HELPERS
61 // --------------------------------------------------------------------------------------------
62
63 /// Computes a commitment to the provided list of transactions.
64 ///
65 /// Each transaction is represented by a transaction ID and an account ID which it was executed
66 /// against. The commitment is a sequential hash over (transaction_id, account_id) tuples.
67 pub fn compute_commitment(
68 transactions: impl Iterator<Item = (TransactionId, AccountId)>,
69 ) -> Word {
70 let mut elements = vec![];
71 for (transaction_id, account_id) in transactions {
72 let [account_id_prefix, account_id_suffix] = <[Felt; 2]>::from(account_id);
73 elements.extend_from_slice(transaction_id.as_elements());
74 elements.extend_from_slice(&[account_id_prefix, account_id_suffix, ZERO, ZERO]);
75 }
76
77 Hasher::hash_elements(&elements)
78 }
79}
80
81// SERIALIZATION
82// ================================================================================================
83
84impl Serializable for OrderedTransactionHeaders {
85 fn write_into<W: ByteWriter>(&self, target: &mut W) {
86 self.0.write_into(target)
87 }
88}
89
90impl Deserializable for OrderedTransactionHeaders {
91 fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
92 source.read().map(OrderedTransactionHeaders::new_unchecked)
93 }
94}