Skip to main content

miden_protocol/batch/
ordered_batches.rs

1use alloc::vec::Vec;
2
3use crate::batch::ProvenBatch;
4use crate::crypto::SequentialCommit;
5use crate::transaction::OrderedTransactionHeaders;
6use crate::utils::serde::{
7    ByteReader,
8    ByteWriter,
9    Deserializable,
10    DeserializationError,
11    Serializable,
12};
13use crate::{Felt, Word};
14
15// ORDERED BATCHES
16// ================================================================================================
17
18/// The ordered set of batches in a [`ProposedBlock`](crate::block::ProposedBlock).
19///
20/// This is a newtype wrapper representing the set of batches in a proposed block. It can only be
21/// retrieved from a proposed block. This type exists only to encapsulate the conversion to
22/// [`OrderedTransactionHeaders`].
23#[derive(Debug, Clone, PartialEq, Eq)]
24pub struct OrderedBatches(Vec<ProvenBatch>);
25
26impl OrderedBatches {
27    /// Creates a new set of ordered batches from the provided vector.
28    pub fn new(batches: Vec<ProvenBatch>) -> Self {
29        Self(batches)
30    }
31
32    /// Returns a reference to the underlying proven batches.
33    pub fn as_slice(&self) -> &[ProvenBatch] {
34        &self.0
35    }
36
37    /// Converts the transactions in batches into ordered transaction headers.
38    pub fn to_transactions(&self) -> OrderedTransactionHeaders {
39        OrderedTransactionHeaders::new_unchecked(
40            self.0
41                .iter()
42                .flat_map(|batch| batch.transactions().as_slice().iter())
43                .cloned()
44                .collect(),
45        )
46    }
47
48    /// Consumes self and converts the transactions in batches into ordered transaction headers.
49    pub fn into_transactions(self) -> OrderedTransactionHeaders {
50        OrderedTransactionHeaders::new_unchecked(
51            self.0
52                .into_iter()
53                .flat_map(|batch| batch.into_transactions().into_vec().into_iter())
54                .collect(),
55        )
56    }
57
58    /// Returns the sum of created notes across all batches.
59    pub fn num_created_notes(&self) -> usize {
60        self.0.as_slice().iter().fold(0, |acc, batch| acc + batch.output_notes().len())
61    }
62
63    /// Consumes self and returns the underlying vector of batches.
64    pub fn into_vec(self) -> Vec<ProvenBatch> {
65        self.0
66    }
67}
68
69impl SequentialCommit for OrderedBatches {
70    type Commitment = Word;
71
72    /// Returns batch IDs represented as a vector of field elements, in order.
73    fn to_elements(&self) -> Vec<Felt> {
74        let mut elements = Vec::with_capacity(self.0.len() * Word::NUM_ELEMENTS);
75        for batch in self.0.iter() {
76            elements.extend_from_slice(batch.id().as_word().as_elements());
77        }
78
79        elements
80    }
81}
82
83// SERIALIZATION
84// ================================================================================================
85
86impl Serializable for OrderedBatches {
87    fn write_into<W: ByteWriter>(&self, target: &mut W) {
88        self.0.write_into(target)
89    }
90}
91
92impl Deserializable for OrderedBatches {
93    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
94        source.read().map(OrderedBatches::new)
95    }
96}