|
| 1 | +use std::collections::HashMap; |
| 2 | +use std::sync::Arc; |
| 3 | + |
| 4 | +use miden_objects::Word; |
| 5 | +use miden_objects::account::AccountId; |
| 6 | +use miden_objects::batch::BatchId; |
| 7 | +use miden_objects::transaction::TransactionId; |
| 8 | + |
| 9 | +use crate::domain::transaction::AuthenticatedTransaction; |
| 10 | + |
| 11 | +// SELECTED BATCH |
| 12 | +// ================================================================================================ |
| 13 | + |
| 14 | +/// A sequence of transactions selected by the [`Mempool`] to be processed by the |
| 15 | +/// [`BatchBuilder`] into a [`ProposedBatch`], and then finally into a [`ProvenBatch`]. |
| 16 | +/// |
| 17 | +/// [Mempool]: crate::mempool::Mempool |
| 18 | +/// [BatchBuilder]: crate::batch_builder::BatchBuilder |
| 19 | +/// [ProposedBatch]: miden_objects::batch::ProposedBatch |
| 20 | +/// [ProvenBatch]: miden_objects::batch::ProvenBatch |
| 21 | +#[derive(Clone, Debug, PartialEq)] |
| 22 | +pub(crate) struct SelectedBatch { |
| 23 | + txs: Vec<Arc<AuthenticatedTransaction>>, |
| 24 | + id: BatchId, |
| 25 | + account_updates: HashMap<AccountId, (Word, Word)>, |
| 26 | +} |
| 27 | + |
| 28 | +impl SelectedBatch { |
| 29 | + pub(crate) fn builder() -> SelectedBatchBuilder { |
| 30 | + SelectedBatchBuilder::default() |
| 31 | + } |
| 32 | + |
| 33 | + pub(crate) fn id(&self) -> BatchId { |
| 34 | + self.id |
| 35 | + } |
| 36 | + |
| 37 | + pub(crate) fn txs(&self) -> &[Arc<AuthenticatedTransaction>] { |
| 38 | + &self.txs |
| 39 | + } |
| 40 | + |
| 41 | + pub(crate) fn into_transactions(self) -> Vec<Arc<AuthenticatedTransaction>> { |
| 42 | + self.txs |
| 43 | + } |
| 44 | + |
| 45 | + /// The aggregated list of account transitions this batch causes given as tuples of `(AccountId, |
| 46 | + /// initial commitment, final commitment)`. |
| 47 | + /// |
| 48 | + /// Note that the updates are aggregated, i.e. only a single update per account is possible, and |
| 49 | + /// transaction updates to an account of `a -> b -> c` will result in a single `a -> c`. |
| 50 | + pub(crate) fn account_updates(&self) -> impl Iterator<Item = (AccountId, Word, Word)> { |
| 51 | + self.account_updates.iter().map(|(account, (from, to))| (*account, *from, *to)) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +/// A builder to construct a [`SelectedBatch`]. |
| 56 | +#[derive(Clone, Default)] |
| 57 | +pub(crate) struct SelectedBatchBuilder { |
| 58 | + pub(crate) txs: Vec<Arc<AuthenticatedTransaction>>, |
| 59 | + pub(crate) account_updates: HashMap<AccountId, (Word, Word)>, |
| 60 | +} |
| 61 | + |
| 62 | +impl SelectedBatchBuilder { |
| 63 | + /// Appends the given transaction to the current batch. |
| 64 | + /// |
| 65 | + /// # Panics |
| 66 | + /// |
| 67 | + /// Panics if the new transaction's account update is inconsistent with the current account |
| 68 | + /// state within the batch i.e. if the transaction's initial account commitment does not |
| 69 | + /// match the account update's final account commitment within the batch (if any). |
| 70 | + pub(crate) fn push(&mut self, tx: Arc<AuthenticatedTransaction>) { |
| 71 | + let update = tx.account_update(); |
| 72 | + self.account_updates |
| 73 | + .entry(update.account_id()) |
| 74 | + .and_modify(|(_, to)| { |
| 75 | + assert!( |
| 76 | + to == &update.initial_state_commitment(), |
| 77 | + "Cannot select transaction {} as its initial commitment {} for account {} does \ |
| 78 | +not match the current commitment {}", |
| 79 | + tx.id(), |
| 80 | + update.initial_state_commitment(), |
| 81 | + update.account_id(), |
| 82 | + to |
| 83 | + ); |
| 84 | + |
| 85 | + *to = update.final_state_commitment(); |
| 86 | + }) |
| 87 | + .or_insert((update.initial_state_commitment(), update.final_state_commitment())); |
| 88 | + |
| 89 | + self.txs.push(tx); |
| 90 | + } |
| 91 | + |
| 92 | + /// Returns `true` if the batch contains the given transaction already. |
| 93 | + pub(crate) fn contains(&self, target: &TransactionId) -> bool { |
| 94 | + self.txs.iter().any(|tx| &tx.id() == target) |
| 95 | + } |
| 96 | + |
| 97 | + /// Returns `true` if it contains no transactions. |
| 98 | + pub(crate) fn is_empty(&self) -> bool { |
| 99 | + self.txs.is_empty() |
| 100 | + } |
| 101 | + |
| 102 | + /// Finalizes the batch selection. |
| 103 | + pub(crate) fn build(self) -> SelectedBatch { |
| 104 | + let Self { txs, account_updates } = self; |
| 105 | + let id = BatchId::from_ids(txs.iter().map(|tx| (tx.id(), tx.account_id()))); |
| 106 | + |
| 107 | + SelectedBatch { txs, id, account_updates } |
| 108 | + } |
| 109 | +} |
0 commit comments