Skip to main content

miden_protocol/transaction/outputs/
mod.rs

1use core::fmt::Debug;
2
3use crate::Word;
4use crate::account::AccountHeader;
5use crate::block::BlockNumber;
6use crate::utils::serde::{
7    ByteReader,
8    ByteWriter,
9    Deserializable,
10    DeserializationError,
11    Serializable,
12};
13
14mod notes;
15pub use notes::{
16    OutputNote,
17    OutputNoteCollection,
18    OutputNotes,
19    PrivateOutputNote,
20    PublicOutputNote,
21    RawOutputNote,
22    RawOutputNotes,
23};
24
25#[cfg(test)]
26mod tests;
27
28// TRANSACTION OUTPUTS
29// ================================================================================================
30
31/// Describes the result of executing a transaction.
32#[derive(Debug, Clone, PartialEq, Eq)]
33pub struct TransactionOutputs {
34    /// Information related to the account's final state.
35    account: AccountHeader,
36    /// The commitment to the [`AccountPatch`](crate::account::AccountPatch) computed by the
37    /// transaction kernel.
38    account_patch_commitment: Word,
39    /// Set of output notes created by the transaction.
40    output_notes: RawOutputNotes,
41    /// Defines up to which block the transaction is considered valid.
42    expiration_block_num: BlockNumber,
43}
44
45impl TransactionOutputs {
46    // CONSTANTS
47    // --------------------------------------------------------------------------------------------
48
49    /// The element index starting from which the output notes commitment is stored on the output
50    /// stack.
51    pub const OUTPUT_NOTES_COMMITMENT_WORD_IDX: usize = 0;
52
53    /// The element index starting from which the account update commitment word is stored on the
54    /// output stack.
55    pub const ACCOUNT_UPDATE_COMMITMENT_WORD_IDX: usize = 4;
56
57    /// The index of the item at which the expiration block height is stored on the output stack.
58    pub const EXPIRATION_BLOCK_ELEMENT_IDX: usize = 8;
59
60    // CONSTRUCTOR
61    // --------------------------------------------------------------------------------------------
62
63    /// Returns a new [`TransactionOutputs`] instantiated from the provided data.
64    pub fn new(
65        account: AccountHeader,
66        account_patch_commitment: Word,
67        output_notes: RawOutputNotes,
68        expiration_block_num: BlockNumber,
69    ) -> Self {
70        Self {
71            account,
72            account_patch_commitment,
73            output_notes,
74            expiration_block_num,
75        }
76    }
77
78    // PUBLIC ACCESSORS
79    // --------------------------------------------------------------------------------------------
80
81    /// Returns the header of the account's final state.
82    pub fn account(&self) -> &AccountHeader {
83        &self.account
84    }
85
86    /// Returns the commitment to the patch computed by the transaction kernel.
87    pub fn account_patch_commitment(&self) -> Word {
88        self.account_patch_commitment
89    }
90
91    /// Returns the set of output notes created by the transaction.
92    pub fn output_notes(&self) -> &RawOutputNotes {
93        &self.output_notes
94    }
95
96    /// Returns the block number at which the transaction will expire.
97    pub fn expiration_block_num(&self) -> BlockNumber {
98        self.expiration_block_num
99    }
100
101    // CONVERSIONS
102    // --------------------------------------------------------------------------------------------
103
104    /// Consumes self and returns the individual parts (that are non-Copy).
105    pub fn into_parts(self) -> (AccountHeader, RawOutputNotes) {
106        (self.account, self.output_notes)
107    }
108}
109
110impl Serializable for TransactionOutputs {
111    fn write_into<W: ByteWriter>(&self, target: &mut W) {
112        self.account.write_into(target);
113        self.account_patch_commitment.write_into(target);
114        self.output_notes.write_into(target);
115        self.expiration_block_num.write_into(target);
116    }
117}
118
119impl Deserializable for TransactionOutputs {
120    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
121        let account = AccountHeader::read_from(source)?;
122        let account_patch_commitment = Word::read_from(source)?;
123        let output_notes = RawOutputNotes::read_from(source)?;
124        let expiration_block_num = BlockNumber::read_from(source)?;
125
126        Ok(Self {
127            account,
128            account_patch_commitment,
129            output_notes,
130            expiration_block_num,
131        })
132    }
133}