Skip to content

Commit

Permalink
refactor: update Note object (#664)
Browse files Browse the repository at this point in the history
* Rename NoteEnvelope into NoteHeader
* Change Note struct to be based on header and details
  • Loading branch information
bobbinth committed May 14, 2024
1 parent f51f205 commit ff95253
Show file tree
Hide file tree
Showing 13 changed files with 244 additions and 154 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

## 0.3.0 (TBD)

* Introduce the `miden-bench-tx` crate used for transactions benchmarking (#577).
* [BREAKING] Removed the transaction script root output from the transaction kernel (#608).
* [BREAKING] Refactored account update details, moved `Block` to `miden-objects` (#618, #621).
* [BREAKING] Changed type of `version` and `timestamp` fields to `u32`, moved `version` to the beginning of block header
(#639).
* Introduce the `miden-bench-tx` crate used for transactions benchmarking (#577).
* [BREAKING] Changed type of `version` and `timestamp` fields to `u32`, moved `version` to the beginning of block header (#639).
* [BREAKING] Renamed `NoteEnvelope` into `NoteHeader` and introduced `NoteDetails` (#664).

## 0.2.3 (2024-04-26) - `miden-tx` crate only

Expand Down
4 changes: 2 additions & 2 deletions miden-lib/src/tests/test_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,12 @@ fn test_get_output_notes_hash() {
end
",
PUBLIC_NOTE = NoteType::Public as u8,
recipient_1 = prepare_word(&output_note_1.recipient_digest()),
recipient_1 = prepare_word(&output_note_1.recipient().digest()),
tag_1 = output_note_1.metadata().tag(),
asset_1 = prepare_word(&Word::from(
**output_note_1.assets().iter().take(1).collect::<Vec<_>>().first().unwrap()
)),
recipient_2 = prepare_word(&output_note_2.recipient_digest()),
recipient_2 = prepare_word(&output_note_2.recipient().digest()),
tag_2 = output_note_2.metadata().tag(),
asset_2 = prepare_word(&Word::from(
**output_note_2.assets().iter().take(1).collect::<Vec<_>>().first().unwrap()
Expand Down
8 changes: 3 additions & 5 deletions miden-tx/src/host/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use miden_objects::{
accounts::{AccountDelta, AccountId, AccountStorage, AccountStub},
assets::Asset,
notes::{
Note, NoteAssets, NoteEnvelope, NoteId, NoteInputs, NoteMetadata, NoteRecipient,
NoteScript, NoteTag, NoteType,
Note, NoteAssets, NoteHeader, NoteId, NoteInputs, NoteMetadata, NoteRecipient, NoteScript,
NoteTag, NoteType,
},
transaction::OutputNote,
Digest, Hasher,
Expand Down Expand Up @@ -145,9 +145,7 @@ impl<A: AdviceProvider, T: TransactionAuthenticator> TransactionHost<A, T> {
OutputNote::Public(Note::new(vault, metadata, recipient))
} else {
let note_id = NoteId::new(recipient, vault.commitment());
OutputNote::Private(
NoteEnvelope::new(note_id, metadata).expect("NoteType checked above"),
)
OutputNote::Private(NoteHeader::new(note_id, metadata))
};

self.output_notes.push(note);
Expand Down
7 changes: 2 additions & 5 deletions miden-tx/tests/integration/scripts/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use miden_objects::{
assembly::ProgramAst,
assets::{Asset, AssetVault, FungibleAsset, NonFungibleAsset, NonFungibleAssetDetails},
crypto::rand::RpoRandomCoin,
notes::{NoteAssets, NoteEnvelope, NoteExecutionHint, NoteId, NoteMetadata, NoteTag, NoteType},
notes::{NoteAssets, NoteExecutionHint, NoteHeader, NoteId, NoteMetadata, NoteTag, NoteType},
transaction::TransactionArgs,
Felt, ZERO,
};
Expand Down Expand Up @@ -105,8 +105,5 @@ fn prove_swap_script() {
let note_id = NoteId::new(recipient, assets.commitment());

let created_note = executed_transaction.output_notes().get_note(0);
assert_eq!(
NoteEnvelope::from(created_note),
NoteEnvelope::new(note_id, note_metadata).unwrap()
);
assert_eq!(NoteHeader::from(created_note), NoteHeader::new(note_id, note_metadata));
}
6 changes: 3 additions & 3 deletions mock/src/mock/notes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ pub fn mock_notes(
end
",
PUBLIC_NOTE = NoteType::Public as u8,
recipient0 = prepare_word(&created_note_1.recipient_digest()),
recipient0 = prepare_word(&created_note_1.recipient().digest()),
tag0 = created_note_1.metadata().tag(),
asset0 = prepare_assets(created_note_1.assets())[0],
recipient1 = prepare_word(&created_note_2.recipient_digest()),
recipient1 = prepare_word(&created_note_2.recipient().digest()),
tag1 = created_note_2.metadata().tag(),
asset1 = prepare_assets(created_note_2.assets())[0],
);
Expand All @@ -132,7 +132,7 @@ pub fn mock_notes(
end
",
PUBLIC_NOTE = NoteType::Public as u8,
recipient = prepare_word(&created_note_3.recipient_digest()),
recipient = prepare_word(&created_note_3.recipient().digest()),
tag = created_note_3.metadata().tag(),
asset = prepare_assets(created_note_3.assets())[0],
);
Expand Down
97 changes: 97 additions & 0 deletions objects/src/notes/details.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
use miden_crypto::{
utils::{ByteReader, ByteWriter, Deserializable, Serializable},
Word,
};
use vm_processor::DeserializationError;

use super::{NoteAssets, NoteId, NoteInputs, NoteRecipient, NoteScript, Nullifier};

// NOTE DETAILS
// ================================================================================================

/// Details of a note consisting of assets, script, inputs, and a serial number.
///
/// See [super::Note] for more details.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct NoteDetails {
assets: NoteAssets,
recipient: NoteRecipient,
}

impl NoteDetails {
// CONSTRUCTOR
// --------------------------------------------------------------------------------------------

/// Returns a new note created with the specified parameters.
pub fn new(assets: NoteAssets, recipient: NoteRecipient) -> Self {
Self { assets, recipient }
}

// PUBLIC ACCESSORS
// --------------------------------------------------------------------------------------------

/// Returns the note's unique identifier.
///
/// This value is both an unique identifier and a commitment to the note.
pub fn id(&self) -> NoteId {
NoteId::from(self)
}

/// Returns the note's assets.
pub fn assets(&self) -> &NoteAssets {
&self.assets
}

/// Returns the note's recipient serial_num, the secret required to consume the note.
pub fn serial_num(&self) -> Word {
self.recipient.serial_num()
}

/// Returns the note's recipient script which locks the assets of this note.
pub fn script(&self) -> &NoteScript {
self.recipient.script()
}

/// Returns the note's recipient inputs which customizes the script's behavior.
pub fn inputs(&self) -> &NoteInputs {
self.recipient.inputs()
}

/// Returns the note's recipient.
pub fn recipient(&self) -> &NoteRecipient {
&self.recipient
}

/// Returns the note's nullifier.
///
/// This is public data, used to prevent double spend.
pub fn nullifier(&self) -> Nullifier {
Nullifier::from(self)
}

/// Decomposes note details into underlying assets and recipient.
pub fn into_parts(self) -> (NoteAssets, NoteRecipient) {
(self.assets, self.recipient)
}
}

// SERIALIZATION
// ================================================================================================

impl Serializable for NoteDetails {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
let Self { assets, recipient } = self;

assets.write_into(target);
recipient.write_into(target);
}
}

impl Deserializable for NoteDetails {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let assets = NoteAssets::read_from(source)?;
let recipient = NoteRecipient::read_from(source)?;
Ok(Self::new(assets, recipient))
}
}
Loading

0 comments on commit ff95253

Please sign in to comment.