From 1687b3fd3a098b9580bb8dc26d0b49437b33855a Mon Sep 17 00:00:00 2001 From: Bobbin Threadbare Date: Tue, 26 Dec 2023 10:57:24 -0800 Subject: [PATCH] refactor: use TransactionInputs in ExecutedTransaction --- miden-lib/src/tests/test_account.rs | 2 +- miden-lib/src/tests/test_epilogue.rs | 12 ++--- miden-tx/src/prover/mod.rs | 8 ++-- mock/src/mock/transaction.rs | 26 +++++------ objects/src/transaction/executed_tx.rs | 64 +++++++------------------- objects/src/transaction/prepared_tx.rs | 25 ++-------- objects/src/transaction/utils.rs | 58 ++++++++++++----------- 7 files changed, 76 insertions(+), 119 deletions(-) diff --git a/miden-lib/src/tests/test_account.rs b/miden-lib/src/tests/test_account.rs index 2b0c58b80..9332278c5 100644 --- a/miden-lib/src/tests/test_account.rs +++ b/miden-lib/src/tests/test_account.rs @@ -67,7 +67,7 @@ pub fn test_set_code_succeeds() { let executed_transaction = mock_executed_tx(AssetPreservationStatus::Preserved); let created_notes_data_procedure = - created_notes_data_procedure(executed_transaction.created_notes()); + created_notes_data_procedure(executed_transaction.output_notes()); let code = format!( " diff --git a/miden-lib/src/tests/test_epilogue.rs b/miden-lib/src/tests/test_epilogue.rs index 5033af199..1d1263c04 100644 --- a/miden-lib/src/tests/test_epilogue.rs +++ b/miden-lib/src/tests/test_epilogue.rs @@ -21,7 +21,7 @@ fn test_epilogue() { let executed_transaction = mock_executed_tx(AssetPreservationStatus::Preserved); let created_notes_data_procedure = - created_notes_data_procedure(executed_transaction.created_notes()); + created_notes_data_procedure(executed_transaction.output_notes()); let imports = "use.miden::sat::internal::prologue\n"; let code = format!( @@ -81,9 +81,9 @@ fn test_compute_created_note_hash() { let executed_transaction = mock_executed_tx(AssetPreservationStatus::Preserved); let created_notes_data_procedure = - created_notes_data_procedure(executed_transaction.created_notes()); + created_notes_data_procedure(executed_transaction.output_notes()); - for (note, i) in executed_transaction.created_notes().iter().zip(0u32..) { + for (note, i) in executed_transaction.output_notes().iter().zip(0u32..) { let imports = "use.miden::sat::internal::prologue\n"; let test = format!( " @@ -132,7 +132,7 @@ fn test_epilogue_asset_preservation_violation() { let executed_transaction = mock_executed_tx(asset_preservation); let created_notes_data_procedure = - created_notes_data_procedure(executed_transaction.created_notes()); + created_notes_data_procedure(executed_transaction.output_notes()); let imports = "use.miden::sat::internal::prologue\n"; let code = format!( @@ -166,7 +166,7 @@ fn test_epilogue_increment_nonce_success() { let executed_transaction = mock_executed_tx(AssetPreservationStatus::Preserved); let created_notes_data_procedure = - created_notes_data_procedure(executed_transaction.created_notes()); + created_notes_data_procedure(executed_transaction.output_notes()); let imports = "use.miden::sat::internal::prologue\n"; let code = format!( @@ -198,7 +198,7 @@ fn test_epilogue_increment_nonce_violation() { let executed_transaction = mock_executed_tx(AssetPreservationStatus::Preserved); let created_notes_data_procedure = - created_notes_data_procedure(executed_transaction.created_notes()); + created_notes_data_procedure(executed_transaction.output_notes()); let imports = "use.miden::sat::internal::prologue\n"; let code = format!( diff --git a/miden-tx/src/prover/mod.rs b/miden-tx/src/prover/mod.rs index 6a5a3638b..2a7f94ac5 100644 --- a/miden-tx/src/prover/mod.rs +++ b/miden-tx/src/prover/mod.rs @@ -50,7 +50,7 @@ impl TransactionProver { let final_account_stub = FinalAccountStub::try_from_vm_result(&outputs, &stack, &map, &store) .map_err(TransactionProverError::TransactionResultError)?; - let created_notes = OutputNotes::try_from_vm_result(&outputs, &stack, &map, &store) + let output_notes = OutputNotes::try_from_vm_result(&outputs, &stack, &map, &store) .map_err(TransactionProverError::TransactionResultError)?; let (account, block_header, _chain, input_notes, _tx_program, tx_script) = @@ -61,7 +61,7 @@ impl TransactionProver { account.hash(), final_account_stub.0.hash(), input_notes.nullifiers().collect(), - created_notes.envelopes().collect(), + output_notes.envelopes().collect(), tx_script.map(|tx_script| *tx_script.hash()), block_header.hash(), proof, @@ -105,7 +105,7 @@ impl TransactionProver { let final_account_stub = FinalAccountStub::try_from_vm_result(&outputs, &stack, &map, &store) .map_err(TransactionProverError::TransactionResultError)?; - let created_notes = OutputNotes::try_from_vm_result(&outputs, &stack, &map, &store) + let output_notes = OutputNotes::try_from_vm_result(&outputs, &stack, &map, &store) .map_err(TransactionProverError::TransactionResultError)?; Ok(ProvenTransaction::new( @@ -113,7 +113,7 @@ impl TransactionProver { initial_account_hash, final_account_stub.0.hash(), consumed_notes_info, - created_notes.envelopes().collect(), + output_notes.envelopes().collect(), tx_script_root, block_hash, proof, diff --git a/mock/src/mock/transaction.rs b/mock/src/mock/transaction.rs index 1ff17c7a7..8477b50f7 100644 --- a/mock/src/mock/transaction.rs +++ b/mock/src/mock/transaction.rs @@ -2,7 +2,7 @@ use miden_lib::assembler::assembler; use miden_objects::{ accounts::Account, notes::Note, - transaction::{ChainMmr, ExecutedTransaction, InputNote}, + transaction::{ChainMmr, ExecutedTransaction, InputNote, InputNotes, TransactionInputs}, utils::collections::Vec, BlockHeader, Felt, FieldElement, }; @@ -109,26 +109,24 @@ pub fn mock_executed_tx(asset_preservation: AssetPreservationStatus) -> Executed let (consumed_notes, created_notes) = mock_notes(&assembler, &asset_preservation); // Chain data - let (chain_mmr, recorded_notes) = mock_chain_data(consumed_notes); + let (block_chain, input_notes) = mock_chain_data(consumed_notes); // Block header let block_header = mock_block_header( 4, - Some(chain_mmr.peaks().hash_peaks()), + Some(block_chain.peaks().hash_peaks()), None, &[initial_account.clone()], ); - // Executed Transaction - ExecutedTransaction::new( - initial_account, - None, - final_account, - recorded_notes, - created_notes, - None, + let tx_inputs = TransactionInputs { + account: initial_account, + account_seed: None, block_header, - chain_mmr, - ) - .unwrap() + block_chain, + input_notes: InputNotes::new(input_notes).unwrap(), + }; + + // Executed Transaction + ExecutedTransaction::new(tx_inputs, final_account, created_notes, None).unwrap() } diff --git a/objects/src/transaction/executed_tx.rs b/objects/src/transaction/executed_tx.rs index c438e894b..bc5df64d8 100644 --- a/objects/src/transaction/executed_tx.rs +++ b/objects/src/transaction/executed_tx.rs @@ -1,56 +1,43 @@ use vm_core::StackOutputs; -use super::TransactionScript; +use super::{TransactionInputs, TransactionScript}; use crate::{ accounts::validate_account_seed, - transaction::{ - utils, Account, AdviceInputs, BlockHeader, ChainMmr, Digest, InputNote, InputNotes, Note, - StackInputs, Vec, Word, - }, + transaction::{utils, Account, AdviceInputs, Digest, InputNotes, Note, StackInputs, Vec, Word}, ExecutedTransactionError, }; +// EXECUTED TRANSACTION +// ================================================================================================ + #[derive(Debug)] pub struct ExecutedTransaction { - initial_account: Account, - initial_account_seed: Option, + tx_inputs: TransactionInputs, final_account: Account, - consumed_notes: InputNotes, created_notes: Vec, tx_script: Option, - block_header: BlockHeader, - block_chain: ChainMmr, } impl ExecutedTransaction { /// Constructs a new [ExecutedTransaction] instance. - #[allow(clippy::too_many_arguments)] pub fn new( - initial_account: Account, - initial_account_seed: Option, + tx_inputs: TransactionInputs, final_account: Account, - consumed_notes: Vec, created_notes: Vec, tx_script: Option, - block_header: BlockHeader, - block_chain: ChainMmr, ) -> Result { - Self::validate_new_account_seed(&initial_account, initial_account_seed)?; + Self::validate_new_account_seed(&tx_inputs.account, tx_inputs.account_seed)?; Ok(Self { - initial_account, - initial_account_seed, + tx_inputs, final_account, - consumed_notes: InputNotes::new(consumed_notes).unwrap(), created_notes, tx_script, - block_header, - block_chain, }) } /// Returns the initial account. pub fn initial_account(&self) -> &Account { - &self.initial_account + &self.tx_inputs.account } /// Returns the final account. @@ -59,12 +46,12 @@ impl ExecutedTransaction { } /// Returns the consumed notes. - pub fn consumed_notes(&self) -> &InputNotes { - &self.consumed_notes + pub fn input_notes(&self) -> &InputNotes { + &self.tx_inputs.input_notes } /// Returns the created notes. - pub fn created_notes(&self) -> &[Note] { + pub fn output_notes(&self) -> &[Note] { &self.created_notes } @@ -75,39 +62,22 @@ impl ExecutedTransaction { /// Returns the block hash. pub fn block_hash(&self) -> Digest { - self.block_header.hash() + self.tx_inputs.block_header.hash() } /// Returns the stack inputs required when executing the transaction. pub fn stack_inputs(&self) -> StackInputs { - let initial_acct_hash = if self.initial_account.is_new() { - Digest::default() - } else { - self.initial_account.hash() - }; - utils::generate_stack_inputs( - &self.initial_account.id(), - initial_acct_hash, - self.consumed_notes.commitment(), - &self.block_header, - ) + utils::generate_stack_inputs(&self.tx_inputs) } /// Returns the consumed notes commitment. pub fn consumed_notes_commitment(&self) -> Digest { - self.consumed_notes.commitment() + self.input_notes().commitment() } /// Returns the advice inputs required when executing the transaction. pub fn advice_provider_inputs(&self) -> AdviceInputs { - utils::generate_advice_provider_inputs( - &self.initial_account, - self.initial_account_seed, - &self.block_header, - &self.block_chain, - &self.consumed_notes, - &self.tx_script, - ) + utils::generate_advice_provider_inputs(&self.tx_inputs, &self.tx_script) } /// Returns the stack outputs produced as a result of executing a transaction. diff --git a/objects/src/transaction/prepared_tx.rs b/objects/src/transaction/prepared_tx.rs index 9d7d733fc..f92f1b251 100644 --- a/objects/src/transaction/prepared_tx.rs +++ b/objects/src/transaction/prepared_tx.rs @@ -1,6 +1,6 @@ use super::{ - utils, Account, AdviceInputs, BlockHeader, ChainMmr, Digest, InputNotes, - PreparedTransactionError, Program, StackInputs, TransactionInputs, TransactionScript, Word, + utils, Account, AdviceInputs, BlockHeader, ChainMmr, InputNotes, PreparedTransactionError, + Program, StackInputs, TransactionInputs, TransactionScript, Word, }; use crate::accounts::validate_account_seed; @@ -69,29 +69,12 @@ impl PreparedTransaction { /// Returns the stack inputs required when executing the transaction. pub fn stack_inputs(&self) -> StackInputs { - let initial_acct_hash = if self.account().is_new() { - Digest::default() - } else { - self.account().hash() - }; - utils::generate_stack_inputs( - &self.account().id(), - initial_acct_hash, - self.input_notes().commitment(), - self.block_header(), - ) + utils::generate_stack_inputs(&self.tx_inputs) } /// Returns the advice inputs required when executing the transaction. pub fn advice_provider_inputs(&self) -> AdviceInputs { - utils::generate_advice_provider_inputs( - self.account(), - self.tx_inputs.account_seed, - self.block_header(), - self.block_chain(), - self.input_notes(), - &self.tx_script, - ) + utils::generate_advice_provider_inputs(&self.tx_inputs, &self.tx_script) } // CONSUMERS diff --git a/objects/src/transaction/utils.rs b/objects/src/transaction/utils.rs index 0efd2292d..880f33501 100644 --- a/objects/src/transaction/utils.rs +++ b/objects/src/transaction/utils.rs @@ -1,10 +1,13 @@ use vm_core::utils::IntoBytes; use super::{ - Account, AccountId, AdviceInputs, BlockHeader, ChainMmr, Digest, Felt, Hasher, InputNotes, - Note, StackInputs, StackOutputs, ToAdviceInputs, TransactionScript, Vec, Word, ZERO, + AdviceInputs, Digest, Felt, Hasher, Note, StackInputs, StackOutputs, ToAdviceInputs, + TransactionInputs, TransactionScript, Vec, Word, ZERO, }; +// ADVICE INPUT CONSTRUCTORS +// ================================================================================================ + /// Returns the advice inputs required when executing a transaction. /// This includes the initial account, an optional account seed (required for new accounts), the /// number of consumed notes, the core consumed note data, and the consumed note inputs. @@ -46,26 +49,22 @@ use super::{ /// - PEAK_N is the n'th peak in the block chain MMR from the last known block. /// - ACT_ID_SEED3..0 is the account id seed. pub fn generate_advice_provider_inputs( - account: &Account, - account_id_seed: Option, - block_header: &BlockHeader, - block_chain: &ChainMmr, - notes: &InputNotes, + tx_inputs: &TransactionInputs, tx_script: &Option, ) -> AdviceInputs { let mut advice_inputs = AdviceInputs::default(); // insert block data - block_header.to_advice_inputs(&mut advice_inputs); + (&tx_inputs.block_header).to_advice_inputs(&mut advice_inputs); // insert block chain mmr - block_chain.to_advice_inputs(&mut advice_inputs); + (&tx_inputs.block_chain).to_advice_inputs(&mut advice_inputs); // insert account data - account.to_advice_inputs(&mut advice_inputs); + tx_inputs.account.to_advice_inputs(&mut advice_inputs); // insert consumed notes data to advice stack - notes.to_advice_inputs(&mut advice_inputs); + (tx_inputs.input_notes).to_advice_inputs(&mut advice_inputs); if let Some(tx_script) = tx_script.as_ref() { // populate the advice inputs with the transaction script data @@ -77,9 +76,9 @@ pub fn generate_advice_provider_inputs( } // insert account id seed into advice map - if let Some(seed) = account_id_seed { + if let Some(seed) = tx_inputs.account_seed { advice_inputs.extend_map(vec![( - [account.id().into(), ZERO, ZERO, ZERO].into_bytes(), + [tx_inputs.account.id().into(), ZERO, ZERO, ZERO].into_bytes(), seed.to_vec(), )]); } @@ -87,9 +86,12 @@ pub fn generate_advice_provider_inputs( advice_inputs } +// INPUT STACK CONSTRUCTOR +// ================================================================================================ + /// Returns the stack inputs required when executing a transaction. -/// This includes the consumed notes commitment, the account hash, the account id, and the block -/// reference. +/// +/// This includes the input notes commitment, the account hash, the account id, and the block hash. /// /// Stack: [BH, acct_id, IAH, NC] /// @@ -97,21 +99,25 @@ pub fn generate_advice_provider_inputs( /// - acct_id is the account id of the account that the transaction is being executed against. /// - IAH is the initial account hash of the account that the transaction is being executed against. /// - NC is the nullifier commitment of the transaction. This is a sequential hash of all -/// (nullifier, script_root) pairs for the notes consumed in the transaction. -pub fn generate_stack_inputs( - account_id: &AccountId, - account_hash: Digest, - consumed_notes_commitment: Digest, - block_header: &BlockHeader, -) -> StackInputs { +/// (nullifier, ZERO) tuples for the notes consumed in the transaction. +pub fn generate_stack_inputs(tx_inputs: &TransactionInputs) -> StackInputs { + let initial_acct_hash = if tx_inputs.account.is_new() { + Digest::default() + } else { + tx_inputs.account.hash() + }; + let mut inputs: Vec = Vec::with_capacity(13); - inputs.extend(*consumed_notes_commitment); - inputs.extend_from_slice(account_hash.as_elements()); - inputs.push((*account_id).into()); - inputs.extend_from_slice(block_header.hash().as_elements()); + inputs.extend(tx_inputs.input_notes.commitment()); + inputs.extend_from_slice(initial_acct_hash.as_elements()); + inputs.push((tx_inputs.account.id()).into()); + inputs.extend_from_slice(tx_inputs.block_header.hash().as_elements()); StackInputs::new(inputs) } +// OUTPUT STACK CONSTRUCTOR +// ================================================================================================ + /// Returns the stack outputs produced as a result of executing a transaction. This includes the /// final account hash and created notes commitment. ///