Skip to content

Commit

Permalink
refactor: consolidate transaction output structs in a single module
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbinth committed Dec 26, 2023
1 parent 17077e8 commit d84f4b5
Show file tree
Hide file tree
Showing 13 changed files with 212 additions and 126 deletions.
7 changes: 4 additions & 3 deletions miden-lib/src/notes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use miden_objects::{
accounts::AccountId,
assembly::ProgramAst,
assets::Asset,
notes::{Note, NoteMetadata, NoteScript, NoteStub, NoteVault},
notes::{Note, NoteMetadata, NoteScript, NoteVault},
transaction::OutputNote,
utils::{collections::Vec, vec},
Digest, Felt, Hasher, NoteError, StarkField, Word, WORD_SIZE, ZERO,
};
Expand Down Expand Up @@ -80,7 +81,7 @@ pub fn create_note(
Note::new(note_script.clone(), &inputs, &assets, serial_num, sender, tag.unwrap_or(ZERO))
}

pub fn notes_try_from_elements(elements: &[Word]) -> Result<NoteStub, NoteError> {
pub fn notes_try_from_elements(elements: &[Word]) -> Result<OutputNote, NoteError> {
if elements.len() < CREATED_NOTE_CORE_DATA_SIZE {
return Err(NoteError::InvalidStubDataLen(elements.len()));
}
Expand All @@ -104,7 +105,7 @@ pub fn notes_try_from_elements(elements: &[Word]) -> Result<NoteStub, NoteError>
return Err(NoteError::InconsistentStubVaultHash(vault_hash, vault.hash()));
}

let stub = NoteStub::new(recipient, vault, metadata)?;
let stub = OutputNote::new(recipient, vault, metadata)?;
if stub.hash() != hash {
return Err(NoteError::InconsistentStubHash(stub.hash(), hash));
}
Expand Down
4 changes: 2 additions & 2 deletions miden-tx/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use miden_lib::{outputs::TX_SCRIPT_ROOT_WORD_IDX, transaction::extract_account_s
use miden_objects::{
accounts::{Account, AccountDelta},
assembly::ProgramAst,
transaction::{CreatedNotes, FinalAccountStub, InputNotes, TransactionScript},
transaction::{FinalAccountStub, InputNotes, OutputNotes, TransactionScript},
Felt, TransactionResultError, Word, WORD_SIZE,
};
use vm_core::{Program, StackOutputs, StarkField};
Expand Down Expand Up @@ -207,7 +207,7 @@ pub fn create_transaction_result(
// parse transaction results
let final_account_stub =
FinalAccountStub::try_from_vm_result(&stack_outputs, &stack, &map, &store)?;
let created_notes = CreatedNotes::try_from_vm_result(&stack_outputs, &stack, &map, &store)?;
let created_notes = OutputNotes::try_from_vm_result(&stack_outputs, &stack, &map, &store)?;

// assert the tx_script_root is consistent with the output stack
debug_assert_eq!(
Expand Down
6 changes: 3 additions & 3 deletions miden-tx/src/prover/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use miden_objects::transaction::{
CreatedNotes, FinalAccountStub, PreparedTransaction, ProvenTransaction, TransactionWitness,
FinalAccountStub, OutputNotes, PreparedTransaction, ProvenTransaction, TransactionWitness,
};
use miden_prover::prove;
pub use miden_prover::ProvingOptions;
Expand Down 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 = CreatedNotes::try_from_vm_result(&outputs, &stack, &map, &store)
let created_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 Down Expand Up @@ -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 = CreatedNotes::try_from_vm_result(&outputs, &stack, &map, &store)
let created_notes = OutputNotes::try_from_vm_result(&outputs, &stack, &map, &store)
.map_err(TransactionProverError::TransactionResultError)?;

Ok(ProvenTransaction::new(
Expand Down
7 changes: 3 additions & 4 deletions miden-tx/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use miden_lib::{
};
use miden_objects::{
crypto::merkle::MerkleStore,
notes::NoteStub,
transaction::{CreatedNotes, FinalAccountStub},
transaction::{FinalAccountStub, OutputNote, OutputNotes},
utils::collections::{BTreeMap, Vec},
Digest, Felt, TransactionResultError, Word, WORD_SIZE,
};
Expand All @@ -27,7 +26,7 @@ pub trait TryFromVmResult: Sized {
) -> Result<Self, Self::Error>;
}

impl TryFromVmResult for CreatedNotes {
impl TryFromVmResult for OutputNotes {
type Error = TransactionResultError;

fn try_from_vm_result(
Expand Down Expand Up @@ -56,7 +55,7 @@ impl TryFromVmResult for CreatedNotes {
let mut created_notes = Vec::new();
let mut created_note_ptr = 0;
while created_note_ptr < created_notes_data.len() {
let note_stub: NoteStub =
let note_stub: OutputNote =
notes_try_from_elements(&created_notes_data[created_note_ptr..])
.map_err(TransactionResultError::CreatedNoteDataInvalid)?;
created_notes.push(note_stub);
Expand Down
6 changes: 3 additions & 3 deletions miden-tx/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use miden_objects::{
assembly::{Assembler, ModuleAst, ProgramAst},
assets::{Asset, FungibleAsset},
block::BlockHeader,
transaction::{ChainMmr, CreatedNotes, FinalAccountStub, InputNote, InputNotes},
transaction::{ChainMmr, FinalAccountStub, InputNote, InputNotes, OutputNotes},
Felt, Word,
};
use miden_prover::ProvingOptions;
Expand Down Expand Up @@ -62,10 +62,10 @@ fn test_transaction_executor_witness() {
let final_account_stub =
FinalAccountStub::try_from_vm_result(result.stack_outputs(), &stack, &map, &store).unwrap();
let created_notes =
CreatedNotes::try_from_vm_result(result.stack_outputs(), &stack, &map, &store).unwrap();
OutputNotes::try_from_vm_result(result.stack_outputs(), &stack, &map, &store).unwrap();

assert_eq!(transaction_result.final_account_hash(), final_account_stub.0.hash());
assert_eq!(transaction_result.created_notes(), &created_notes);
assert_eq!(transaction_result.output_notes(), &created_notes);
}

#[test]
Expand Down
9 changes: 5 additions & 4 deletions miden-tx/tests/faucet_contract_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use miden_objects::{
assembly::{ModuleAst, ProgramAst},
assets::{Asset, FungibleAsset, TokenSymbol},
crypto::dsa::rpo_falcon512::{KeyPair, PublicKey},
notes::{NoteMetadata, NoteStub, NoteVault},
notes::{NoteMetadata, NoteVault},
transaction::OutputNote,
Felt, Word, ZERO,
};
use miden_tx::TransactionExecutor;
Expand Down Expand Up @@ -76,14 +77,14 @@ fn test_faucet_contract_mint_fungible_asset_succeeds() {
let fungible_asset: Asset =
FungibleAsset::new(faucet_account.id(), amount.into()).unwrap().into();

let expected_note = NoteStub::new(
let expected_note = OutputNote::new(
recipient.into(),
NoteVault::new(&[fungible_asset]).unwrap(),
NoteMetadata::new(faucet_account.id(), tag, Felt::new(1)),
)
.unwrap();

let created_note = transaction_result.created_notes().notes()[0].clone();
let created_note = transaction_result.output_notes().notes()[0].clone();
assert_eq!(created_note.recipient(), expected_note.recipient());
assert_eq!(created_note.vault(), expected_note.vault());
assert_eq!(created_note.metadata(), expected_note.metadata());
Expand Down Expand Up @@ -206,7 +207,7 @@ fn test_faucet_contract_burn_fungible_asset_succeeds() {

// check that the account burned the asset
assert_eq!(transaction_result.account_delta().nonce(), Some(Felt::new(2)));
assert_eq!(transaction_result.consumed_notes().get_note(0).note().hash(), note.hash());
assert_eq!(transaction_result.input_notes().get_note(0).note().hash(), note.hash());
}

#[test]
Expand Down
9 changes: 5 additions & 4 deletions miden-tx/tests/swap_script_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use miden_objects::{
accounts::{Account, AccountId, AccountVault, ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN},
assembly::ProgramAst,
assets::{Asset, FungibleAsset, NonFungibleAsset, NonFungibleAssetDetails},
notes::{NoteMetadata, NoteStub, NoteVault},
notes::{NoteMetadata, NoteVault},
transaction::OutputNote,
Felt,
};
use miden_tx::TransactionExecutor;
Expand Down Expand Up @@ -102,7 +103,7 @@ fn test_swap_script() {
assert_eq!(transaction_result.final_account_hash(), target_account_after.hash());

// Check if only one `Note` has been created
assert_eq!(transaction_result.created_notes().notes().len(), 1);
assert_eq!(transaction_result.output_notes().notes().len(), 1);

// Check if the created `Note` is what we expect
let recipient = Digest::new([
Expand All @@ -117,9 +118,9 @@ fn test_swap_script() {

let note_vault = NoteVault::new(&[non_fungible_asset]).unwrap();

let requested_note = NoteStub::new(recipient, note_vault, note_metadata).unwrap();
let requested_note = OutputNote::new(recipient, note_vault, note_metadata).unwrap();

let created_note = &transaction_result.created_notes().notes()[0];
let created_note = &transaction_result.output_notes().notes()[0];

assert_eq!(created_note, &requested_note);
}
3 changes: 0 additions & 3 deletions objects/src/notes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ pub use origin::{NoteInclusionProof, NoteOrigin};
mod script;
pub use script::NoteScript;

mod stub;
pub use stub::NoteStub;

mod vault;
pub use vault::NoteVault;
use vm_processor::DeserializationError;
Expand Down
84 changes: 0 additions & 84 deletions objects/src/transaction/created_notes.rs

This file was deleted.

4 changes: 2 additions & 2 deletions objects/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use super::{

mod account_stub;
mod chain_mmr;
mod created_notes;
mod event;
mod executed_tx;
mod inputs;
mod outputs;
mod prepared_tx;
mod proven_tx;
mod script;
Expand All @@ -25,10 +25,10 @@ mod utils;

pub use account_stub::FinalAccountStub;
pub use chain_mmr::ChainMmr;
pub use created_notes::CreatedNotes;
pub use event::Event;
pub use executed_tx::ExecutedTransaction;
pub use inputs::{InputNote, InputNotes, TransactionInputs};
pub use outputs::{OutputNote, OutputNotes};
pub use prepared_tx::PreparedTransaction;
pub use proven_tx::ProvenTransaction;
pub use script::TransactionScript;
Expand Down
Loading

0 comments on commit d84f4b5

Please sign in to comment.