Skip to main content

miden_protocol/transaction/
effects.rs

1use super::{ExecutedTransaction, InputNote, InputNotes, RawOutputNotes, TransactionId};
2use crate::Word;
3use crate::account::AccountPatch;
4use crate::block::BlockNumber;
5
6// TRANSACTION EFFECTS
7// ================================================================================================
8
9/// The effects of an executed transaction.
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct TransactionEffects {
12    transaction_id: TransactionId,
13    initial_state_commitment: Word,
14    final_state_commitment: Word,
15    account_patch: AccountPatch,
16    input_notes: InputNotes<InputNote>,
17    output_notes: RawOutputNotes,
18    ref_block_number: BlockNumber,
19    ref_block_commitment: Word,
20    expiration_block_num: BlockNumber,
21}
22
23impl TransactionEffects {
24    // CONSTRUCTOR
25    // --------------------------------------------------------------------------------------------
26
27    /// Returns new [`TransactionEffects`] instantiated from the provided data.
28    ///
29    /// The [`TransactionId`] is computed from the account state commitments and the note
30    /// commitments, so it cannot disagree with the rest of the effects.
31    pub fn new(
32        initial_state_commitment: Word,
33        final_state_commitment: Word,
34        account_patch: AccountPatch,
35        input_notes: InputNotes<InputNote>,
36        output_notes: RawOutputNotes,
37        ref_block_number: BlockNumber,
38        ref_block_commitment: Word,
39        expiration_block_num: BlockNumber,
40    ) -> Self {
41        let transaction_id = TransactionId::new(
42            initial_state_commitment,
43            final_state_commitment,
44            input_notes.commitment(),
45            output_notes.commitment(),
46        );
47
48        Self {
49            transaction_id,
50            initial_state_commitment,
51            final_state_commitment,
52            account_patch,
53            input_notes,
54            output_notes,
55            ref_block_number,
56            ref_block_commitment,
57            expiration_block_num,
58        }
59    }
60
61    // PUBLIC ACCESSORS
62    // --------------------------------------------------------------------------------------------
63
64    /// Returns the unique identifier of the transaction that produced these effects.
65    pub fn transaction_id(&self) -> TransactionId {
66        self.transaction_id
67    }
68
69    /// Returns the commitment to the account state before the transaction was executed.
70    pub fn initial_state_commitment(&self) -> Word {
71        self.initial_state_commitment
72    }
73
74    /// Returns the commitment to the account state after the transaction was executed.
75    pub fn final_state_commitment(&self) -> Word {
76        self.final_state_commitment
77    }
78
79    /// Returns the patch describing the update from the initial to the final account state.
80    pub fn account_patch(&self) -> &AccountPatch {
81        &self.account_patch
82    }
83
84    /// Returns the notes consumed by the transaction.
85    pub fn input_notes(&self) -> &InputNotes<InputNote> {
86        &self.input_notes
87    }
88
89    /// Returns the notes created by the transaction.
90    pub fn output_notes(&self) -> &RawOutputNotes {
91        &self.output_notes
92    }
93
94    /// Returns the number of the block against which the transaction was executed.
95    pub fn ref_block_number(&self) -> BlockNumber {
96        self.ref_block_number
97    }
98
99    /// Returns the commitment to the block against which the transaction was executed.
100    pub fn ref_block_commitment(&self) -> Word {
101        self.ref_block_commitment
102    }
103
104    /// Returns the block number at which the transaction will expire.
105    pub fn expiration_block_num(&self) -> BlockNumber {
106        self.expiration_block_num
107    }
108}
109
110impl From<&ExecutedTransaction> for TransactionEffects {
111    fn from(tx: &ExecutedTransaction) -> Self {
112        Self::new(
113            tx.initial_account().initial_commitment(),
114            tx.final_account().to_commitment(),
115            tx.account_patch().clone(),
116            tx.input_notes().clone(),
117            tx.output_notes().clone(),
118            tx.tx_inputs().ref_block(),
119            tx.block_header().commitment(),
120            tx.expiration_block_num(),
121        )
122    }
123}