Skip to main content

miden_protocol/batch/
batch_id.rs

1use alloc::string::String;
2use alloc::vec::Vec;
3
4use miden_protocol_macros::WordWrapper;
5
6use crate::account::AccountId;
7use crate::transaction::{ProvenTransaction, TransactionId};
8use crate::utils::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable};
9use crate::{Felt, Hasher, Word, ZERO};
10
11// BATCH ID
12// ================================================================================================
13
14/// Uniquely identifies a batch of transactions, i.e. both
15/// [`ProposedBatch`](crate::batch::ProposedBatch) and [`ProvenBatch`](crate::batch::ProvenBatch).
16///
17/// This is a sequential hash of the tuple `(TRANSACTION_ID || [account_id_prefix,
18/// account_id_suffix, 0, 0])` of all transactions and the accounts their executed against in the
19/// batch.
20#[derive(Debug, Copy, Clone, Eq, Ord, PartialEq, PartialOrd, Hash, WordWrapper)]
21pub struct BatchId(Word);
22
23impl BatchId {
24    /// Calculates a batch ID from the given set of transactions.
25    pub fn from_transactions<'tx, T>(txs: T) -> Self
26    where
27        T: Iterator<Item = &'tx ProvenTransaction>,
28    {
29        Self::from_ids(txs.map(|tx| (tx.id(), tx.account_id())))
30    }
31
32    /// Calculates a batch ID from the given transaction ID and account ID tuple.
33    pub fn from_ids(iter: impl IntoIterator<Item = (TransactionId, AccountId)>) -> Self {
34        let mut elements: Vec<Felt> = Vec::new();
35        for (tx_id, account_id) in iter {
36            elements.extend_from_slice(tx_id.as_elements());
37            let [account_id_prefix, account_id_suffix] = <[Felt; 2]>::from(account_id);
38            elements.extend_from_slice(&[account_id_prefix, account_id_suffix, ZERO, ZERO]);
39        }
40
41        Self(Hasher::hash_elements(&elements))
42    }
43}
44
45impl core::fmt::Display for BatchId {
46    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
47        write!(f, "{}", self.to_hex())
48    }
49}
50
51// SERIALIZATION
52// ================================================================================================
53
54impl Serializable for BatchId {
55    fn write_into<W: ByteWriter>(&self, target: &mut W) {
56        self.0.write_into(target);
57    }
58}
59
60impl Deserializable for BatchId {
61    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
62        Ok(Self(Word::read_from(source)?))
63    }
64}