Skip to main content

miden_protocol/transaction/outputs/
mod.rs

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