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 0a29e02
Show file tree
Hide file tree
Showing 16 changed files with 286 additions and 166 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
10 changes: 5 additions & 5 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 @@ -193,7 +193,7 @@ impl<D: DataStore> TransactionExecutor<D> {
/// Creates a new [TransactionResult] from the provided data, advice provider and stack outputs.
pub fn create_transaction_result(
initial_account: Account,
consumed_notes: InputNotes,
input_notes: InputNotes,
block_hash: Digest,
program: Program,
tx_script_root: Option<Digest>,
Expand All @@ -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 output_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 Expand Up @@ -243,8 +243,8 @@ pub fn create_transaction_result(
initial_account,
final_account_stub,
account_delta,
consumed_notes,
created_notes,
input_notes,
output_notes,
block_hash,
program,
tx_script_root,
Expand Down
12 changes: 6 additions & 6 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 All @@ -61,7 +61,7 @@ impl TransactionProver {
account.hash(),
final_account_stub.0.hash(),
input_notes.nullifiers().collect(),
created_notes.into(),
created_notes.envelopes().collect(),
tx_script.map(|tx_script| *tx_script.hash()),
block_header.hash(),
proof,
Expand All @@ -81,7 +81,7 @@ impl TransactionProver {
// extract required data from the transaction witness
let stack_inputs = tx_witness.get_stack_inputs();
let consumed_notes_info = tx_witness
.consumed_notes_info()
.input_notes_info()
.map_err(TransactionProverError::CorruptTransactionWitnessConsumedNoteData)?;
let (
account_id,
Expand All @@ -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 = 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(
account_id,
initial_account_hash,
final_account_stub.0.hash(),
consumed_notes_info,
created_notes.into(),
created_notes.envelopes().collect(),
tx_script_root,
block_hash,
proof,
Expand Down
15 changes: 7 additions & 8 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 All @@ -50,22 +49,22 @@ impl TryFromVmResult for CreatedNotes {
let created_notes_data = group_slice_elements::<Felt, WORD_SIZE>(
advice_map
.get(&created_notes_commitment.as_bytes())
.ok_or(TransactionResultError::CreatedNoteDataNotFound)?,
.ok_or(TransactionResultError::OutputNoteDataNotFound)?,
);

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)?;
.map_err(TransactionResultError::OutputNoteDataInvalid)?;
created_notes.push(note_stub);
created_note_ptr += NOTE_MEM_SIZE as usize;
}

let created_notes = Self::new(created_notes);
let created_notes = Self::new(created_notes)?;
if created_notes_commitment != created_notes.commitment() {
return Err(TransactionResultError::CreatedNotesCommitmentInconsistent(
return Err(TransactionResultError::OutputNotesCommitmentInconsistent(
created_notes_commitment,
created_notes.commitment(),
));
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
12 changes: 6 additions & 6 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,13 @@ 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().get_note(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 +206,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().num_notes(), 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);

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

assert_eq!(created_note, &requested_note);
}
8 changes: 5 additions & 3 deletions objects/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,15 +346,17 @@ impl std::error::Error for ExecutedTransactionError {}
// ================================================================================================
#[derive(Debug)]
pub enum TransactionResultError {
CreatedNoteDataNotFound,
CreatedNoteDataInvalid(NoteError),
CreatedNotesCommitmentInconsistent(Digest, Digest),
OutputNoteDataNotFound,
OutputNoteDataInvalid(NoteError),
OutputNotesCommitmentInconsistent(Digest, Digest),
DuplicateOutputNote(Digest),
FinalAccountDataNotFound,
FinalAccountStubDataInvalid(AccountError),
InconsistentAccountCodeHash(Digest, Digest),
ExtractAccountStorageSlotsDeltaFailed(MerkleError),
ExtractAccountStorageStoreDeltaFailed(MerkleError),
ExtractAccountVaultLeavesDeltaFailed(MerkleError),
TooManyOutputNotes { max: usize, actual: usize },
UpdatedAccountCodeInvalid(AccountError),
}

Expand Down
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.

Loading

0 comments on commit 0a29e02

Please sign in to comment.