Skip to content

Commit c7c2ce1

Browse files
Copilotmmagician
authored andcommitted
feat: Simplify NoteMetadata constructor by making tag optional (0xMiden#2384)
* Update NoteMetadata::new API calls in miden-testing crate Changed from NoteMetadata::new(sender, note_type, tag) to NoteMetadata::new(sender, note_type).with_tag(tag) across all files in the miden-testing crate. Updated files: - tests/agglayer/bridge_out.rs (2 occurrences) - tests/agglayer/bridge_in.rs (1 occurrence) - tests/scripts/faucet.rs (2 occurrences) - tests/scripts/send_note.rs (2 occurrences) - tests/scripts/swap.rs (1 occurrence) - tests/lib.rs (1 occurrence) - src/kernel_tests/tx/test_output_note.rs (3 occurrences) - src/kernel_tests/tx/test_tx.rs (2 occurrences) - src/kernel_tests/tx/test_account_interface.rs (1 occurrence) - src/kernel_tests/tx/test_note.rs (2 occurrences) - src/kernel_tests/tx/test_active_note.rs (1 occurrence) - src/standards/network_account_target.rs (1 occurrence) * Change NoteMetadata constructor to not take tag by default - Modified NoteMetadata::new() to only take sender and note_type - Added with_tag() method for setting tag explicitly - Updated all call sites across protocol, testing, standards, tx, and agglayer crates - Tag now defaults to NoteTag::default() if not explicitly set Co-authored-by: mmagician <8402446+mmagician@users.noreply.github.com> * Fix formatting and missed call site Co-authored-by: mmagician <8402446+mmagician@users.noreply.github.com> * Improve documentation for mutator methods Co-authored-by: mmagician <8402446+mmagician@users.noreply.github.com> * Add changelog entry for PR 0xMiden#2384 Co-authored-by: mmagician <8402446+mmagician@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: mmagician <8402446+mmagician@users.noreply.github.com> Co-authored-by: Marti <marti@miden.team>
1 parent 822cf99 commit c7c2ce1

25 files changed

Lines changed: 83 additions & 55 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Enable `CodeBuilder` to add advice map entries to compiled scripts ([#2275](http
1818

1919
- Removed redundant note storage item count from advice map ([#2376](https://github.com/0xMiden/miden-base/pull/2376)).
2020
- [BREAKING] Prefixed transaction kernel events with `miden::protocol` ([#2364](https://github.com/0xMiden/miden-base/pull/2364)).
21+
- [BREAKING] Simplified `NoteMetadata::new()` constructor to not require tag parameter; tag defaults to zero and can be set via `with_tag()` builder method ([#2384](https://github.com/0xMiden/miden-base/pull/2384)).
2122
- [BREAKING] Renamed `WellKnownComponent` to `StandardAccountComponent`, `WellKnownNote` to `StandardNote`, and `WellKnownNoteAttachment` to `StandardNoteAttachment` ([#2332](https://github.com/0xMiden/miden-base/pull/2332)).
2223
- Skip requests to the `DataStore` for asset vault witnesses which are already in transaction inputs ([#2298](https://github.com/0xMiden/miden-base/pull/2298)).
2324
- [BREAKING] Refactored `TransactionAuthenticator::get_public_key()` method to return `Arc<PublicKey> `instead of `&PublicKey` ([#2304](https://github.com/0xMiden/miden-base/pull/2304)).

crates/miden-agglayer/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,8 @@ pub fn create_claim_note<R: FeltRng>(params: ClaimNoteParams<'_, R>) -> Result<N
518518
.map_err(|e| NoteError::other(e.to_string()))?
519519
.into();
520520
// Use a default sender since we don't have sender anymore - create from destination address
521-
let metadata = NoteMetadata::new(params.claim_note_creator_account_id, note_type, tag)
521+
let metadata = NoteMetadata::new(params.claim_note_creator_account_id, note_type)
522+
.with_tag(tag)
522523
.with_attachment(attachment);
523524
let assets = NoteAssets::new(vec![])?;
524525
let recipient = NoteRecipient::new(serial_num, claim_script, inputs);

crates/miden-protocol/src/note/file.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ mod tests {
171171
let recipient = NoteRecipient::new(serial_num, script, note_storage);
172172

173173
let asset = Asset::Fungible(FungibleAsset::new(faucet, 100).unwrap());
174-
let metadata = NoteMetadata::new(faucet, NoteType::Public, NoteTag::from(123));
174+
let metadata = NoteMetadata::new(faucet, NoteType::Public).with_tag(NoteTag::from(123));
175175

176176
Note::new(NoteAssets::new(vec![asset]).unwrap(), metadata, recipient)
177177
}

crates/miden-protocol/src/note/metadata.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,14 @@ impl NoteMetadata {
7777
// --------------------------------------------------------------------------------------------
7878

7979
/// Returns a new [`NoteMetadata`] instantiated with the specified parameters.
80-
pub fn new(sender: AccountId, note_type: NoteType, tag: NoteTag) -> Self {
80+
///
81+
/// The tag defaults to [`NoteTag::default()`]. Use [`NoteMetadata::with_tag`] to set a
82+
/// specific tag if needed.
83+
pub fn new(sender: AccountId, note_type: NoteType) -> Self {
8184
Self {
8285
sender,
8386
note_type,
84-
tag,
87+
tag: NoteTag::default(),
8588
attachment: NoteAttachment::default(),
8689
}
8790
}
@@ -153,12 +156,27 @@ impl NoteMetadata {
153156
// MUTATORS
154157
// --------------------------------------------------------------------------------------------
155158

156-
/// Overwrites the note's attachment with the provided one.
159+
/// Mutates the note's tag by setting it to the provided value.
160+
pub fn set_tag(&mut self, tag: NoteTag) {
161+
self.tag = tag;
162+
}
163+
164+
/// Returns a new [`NoteMetadata`] with the tag set to the provided value.
165+
///
166+
/// This is a builder method that consumes self and returns a new instance for method chaining.
167+
pub fn with_tag(mut self, tag: NoteTag) -> Self {
168+
self.tag = tag;
169+
self
170+
}
171+
172+
/// Mutates the note's attachment by setting it to the provided value.
157173
pub fn set_attachment(&mut self, attachment: NoteAttachment) {
158174
self.attachment = attachment;
159175
}
160176

161-
/// Overwrites the note's attachment with the provided one.
177+
/// Returns a new [`NoteMetadata`] with the attachment set to the provided value.
178+
///
179+
/// This is a builder method that consumes self and returns a new instance for method chaining.
162180
pub fn with_attachment(mut self, attachment: NoteAttachment) -> Self {
163181
self.attachment = attachment;
164182
self
@@ -184,7 +202,7 @@ impl Deserializable for NoteMetadata {
184202
let tag = NoteTag::read_from(source)?;
185203
let attachment = NoteAttachment::read_from(source)?;
186204

187-
Ok(NoteMetadata::new(sender, note_type, tag).with_attachment(attachment))
205+
Ok(NoteMetadata::new(sender, note_type).with_tag(tag).with_attachment(attachment))
188206
}
189207
}
190208

@@ -351,7 +369,8 @@ mod tests {
351369
let sender = AccountId::try_from(ACCOUNT_ID_MAX_ONES).unwrap();
352370
let note_type = NoteType::Public;
353371
let tag = NoteTag::new(u32::MAX);
354-
let metadata = NoteMetadata::new(sender, note_type, tag).with_attachment(attachment);
372+
let metadata =
373+
NoteMetadata::new(sender, note_type).with_tag(tag).with_attachment(attachment);
355374

356375
// Serialization Roundtrip
357376
let deserialized = NoteMetadata::read_from_bytes(&metadata.to_bytes())?;

crates/miden-protocol/src/testing/note.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,8 @@ impl Note {
2424
let note_script = NoteScript::mock();
2525
let assets =
2626
NoteAssets::new(vec![FungibleAsset::mock(200)]).expect("note assets should be valid");
27-
let metadata = NoteMetadata::new(
28-
sender_id,
29-
NoteType::Private,
30-
NoteTag::with_account_target(sender_id),
31-
);
27+
let metadata = NoteMetadata::new(sender_id, NoteType::Private)
28+
.with_tag(NoteTag::with_account_target(sender_id));
3229
let inputs = NoteStorage::new(Vec::new()).unwrap();
3330
let recipient = NoteRecipient::new(serial_num, note_script, inputs);
3431

crates/miden-standards/src/account/interface/test.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ fn test_basic_wallet_custom_notes() {
253253
let sender_account_id = ACCOUNT_ID_REGULAR_PUBLIC_ACCOUNT_IMMUTABLE_CODE_2.try_into().unwrap();
254254
let serial_num = RpoRandomCoin::new(Word::from([1, 2, 3, 4u32])).draw_word();
255255
let tag = NoteTag::with_account_target(wallet_account.id());
256-
let metadata = NoteMetadata::new(sender_account_id, NoteType::Public, tag);
256+
let metadata = NoteMetadata::new(sender_account_id, NoteType::Public).with_tag(tag);
257257
let vault = NoteAssets::new(vec![FungibleAsset::mock(100)]).unwrap();
258258

259259
let compatible_source_code = "
@@ -336,7 +336,7 @@ fn test_basic_fungible_faucet_custom_notes() {
336336
let sender_account_id = ACCOUNT_ID_REGULAR_PUBLIC_ACCOUNT_IMMUTABLE_CODE_2.try_into().unwrap();
337337
let serial_num = RpoRandomCoin::new(Word::from([1, 2, 3, 4u32])).draw_word();
338338
let tag = NoteTag::with_account_target(faucet_account.id());
339-
let metadata = NoteMetadata::new(sender_account_id, NoteType::Public, tag);
339+
let metadata = NoteMetadata::new(sender_account_id, NoteType::Public).with_tag(tag);
340340
let vault = NoteAssets::new(vec![FungibleAsset::mock(100)]).unwrap();
341341

342342
let compatible_source_code = "
@@ -439,7 +439,7 @@ fn test_custom_account_custom_notes() {
439439

440440
let serial_num = RpoRandomCoin::new(Word::from([1, 2, 3, 4u32])).draw_word();
441441
let tag = NoteTag::with_account_target(target_account.id());
442-
let metadata = NoteMetadata::new(sender_account.id(), NoteType::Public, tag);
442+
let metadata = NoteMetadata::new(sender_account.id(), NoteType::Public).with_tag(tag);
443443
let vault = NoteAssets::new(vec![FungibleAsset::mock(100)]).unwrap();
444444

445445
let compatible_source_code = "
@@ -543,7 +543,7 @@ fn test_custom_account_multiple_components_custom_notes() {
543543

544544
let serial_num = RpoRandomCoin::new(Word::from([1, 2, 3, 4u32])).draw_word();
545545
let tag = NoteTag::with_account_target(target_account.id());
546-
let metadata = NoteMetadata::new(sender_account.id(), NoteType::Public, tag);
546+
let metadata = NoteMetadata::new(sender_account.id(), NoteType::Public).with_tag(tag);
547547
let vault = NoteAssets::new(vec![FungibleAsset::mock(100)]).unwrap();
548548

549549
let compatible_source_code = "

crates/miden-standards/src/note/burn.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ impl BurnNote {
9999
let inputs = NoteStorage::new(vec![])?;
100100
let tag = NoteTag::with_account_target(faucet_id);
101101

102-
let metadata = NoteMetadata::new(sender, note_type, tag).with_attachment(attachment);
102+
let metadata =
103+
NoteMetadata::new(sender, note_type).with_tag(tag).with_attachment(attachment);
103104
let assets = NoteAssets::new(vec![fungible_asset])?; // BURN notes contain the asset to burn
104105
let recipient = NoteRecipient::new(serial_num, note_script, inputs);
105106

crates/miden-standards/src/note/mint.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ impl MintNote {
104104

105105
let tag = NoteTag::with_account_target(faucet_id);
106106

107-
let metadata = NoteMetadata::new(sender, note_type, tag).with_attachment(attachment);
107+
let metadata =
108+
NoteMetadata::new(sender, note_type).with_tag(tag).with_attachment(attachment);
108109
let assets = NoteAssets::new(vec![])?; // MINT notes have no assets
109110
let recipient = NoteRecipient::new(serial_num, note_script, storage);
110111

crates/miden-standards/src/note/p2id.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ impl P2idNote {
8787

8888
let tag = NoteTag::with_account_target(target);
8989

90-
let metadata = NoteMetadata::new(sender, note_type, tag).with_attachment(attachment);
90+
let metadata =
91+
NoteMetadata::new(sender, note_type).with_tag(tag).with_attachment(attachment);
9192
let vault = NoteAssets::new(assets)?;
9293

9394
Ok(Note::new(vault, metadata, recipient))

crates/miden-standards/src/note/p2ide.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ impl P2ideNote {
9292
let recipient = Self::build_recipient(target, reclaim_height, timelock_height, serial_num)?;
9393
let tag = NoteTag::with_account_target(target);
9494

95-
let metadata = NoteMetadata::new(sender, note_type, tag).with_attachment(attachment);
95+
let metadata =
96+
NoteMetadata::new(sender, note_type).with_tag(tag).with_attachment(attachment);
9697
let vault = NoteAssets::new(assets)?;
9798

9899
Ok(Note::new(vault, metadata, recipient))

0 commit comments

Comments
 (0)