Skip to content

Commit

Permalink
refactor: use TransactionInputs in ExecutedTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbinth committed Dec 26, 2023
1 parent 61ad737 commit 1687b3f
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 119 deletions.
2 changes: 1 addition & 1 deletion miden-lib/src/tests/test_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
"
Expand Down
12 changes: 6 additions & 6 deletions miden-lib/src/tests/test_epilogue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down Expand Up @@ -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!(
"
Expand Down Expand Up @@ -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!(
Expand Down Expand Up @@ -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!(
Expand Down Expand Up @@ -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!(
Expand Down
8 changes: 4 additions & 4 deletions miden-tx/src/prover/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand All @@ -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,
Expand Down Expand Up @@ -105,15 +105,15 @@ 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(
account_id,
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,
Expand Down
26 changes: 12 additions & 14 deletions mock/src/mock/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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()
}
64 changes: 17 additions & 47 deletions objects/src/transaction/executed_tx.rs
Original file line number Diff line number Diff line change
@@ -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<Word>,
tx_inputs: TransactionInputs,
final_account: Account,
consumed_notes: InputNotes,
created_notes: Vec<Note>,
tx_script: Option<TransactionScript>,
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<Word>,
tx_inputs: TransactionInputs,
final_account: Account,
consumed_notes: Vec<InputNote>,
created_notes: Vec<Note>,
tx_script: Option<TransactionScript>,
block_header: BlockHeader,
block_chain: ChainMmr,
) -> Result<Self, ExecutedTransactionError> {
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.
Expand All @@ -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
}

Expand All @@ -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.
Expand Down
25 changes: 4 additions & 21 deletions objects/src/transaction/prepared_tx.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 1687b3f

Please sign in to comment.