Skip to content

Commit

Permalink
fix: quick fix of #6405 by removing context from value note utils (#6509
Browse files Browse the repository at this point in the history
)

Please read [contributing guidelines](CONTRIBUTING.md) and remove this
line.
  • Loading branch information
sklppy88 authored May 17, 2024
1 parent 237952e commit 3a4d828
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 33 deletions.
35 changes: 10 additions & 25 deletions noir-projects/aztec-nr/value-note/src/utils.nr
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@ pub fn create_note_getter_options_for_decreasing_balance(amount: Field) -> NoteG

// Creates a new note for the recipient.
// Inserts it to the recipient's set of notes.
pub fn increment(
mut context: PrivateContext,
balance: PrivateSet<ValueNote>,
amount: Field,
recipient: AztecAddress
) {
let recipient_npk_m_hash = get_npk_m_hash(&mut context, recipient);
let recipient_ivpk_m = get_ivpk_m(&mut context, recipient);
pub fn increment(balance: PrivateSet<ValueNote>, amount: Field, recipient: AztecAddress) {
let context = balance.context.private.unwrap();
let recipient_npk_m_hash = get_npk_m_hash(context, recipient);
let recipient_ivpk_m = get_ivpk_m(context, recipient);

let mut note = ValueNote::new(amount, recipient_npk_m_hash);
// Insert the new note to the owner's set of notes and emit the log if value is non-zero.
Expand All @@ -30,13 +26,8 @@ pub fn increment(
// Remove those notes.
// If the value of the removed notes exceeds the requested `amount`, create a new note containing the excess value, so that exactly `amount` is removed.
// Fail if the sum of the selected notes is less than the amount.
pub fn decrement(
mut context: PrivateContext,
balance: PrivateSet<ValueNote>,
amount: Field,
owner: AztecAddress
) {
let sum = decrement_by_at_most(context, balance, amount, owner);
pub fn decrement(balance: PrivateSet<ValueNote>, amount: Field, owner: AztecAddress) {
let sum = decrement_by_at_most(balance, amount, owner);
assert(sum == amount, "Balance too low");
}

Expand All @@ -49,7 +40,6 @@ pub fn decrement(
//
// It returns the decremented amount, which should be less than or equal to max_amount.
pub fn decrement_by_at_most(
mut context: PrivateContext,
balance: PrivateSet<ValueNote>,
max_amount: Field,
owner: AztecAddress
Expand All @@ -60,7 +50,7 @@ pub fn decrement_by_at_most(
let mut decremented = 0;
for i in 0..opt_notes.len() {
if opt_notes[i].is_some() {
decremented += destroy_note(context, balance, owner, opt_notes[i].unwrap_unchecked());
decremented += destroy_note(balance, owner, opt_notes[i].unwrap_unchecked());
}
}

Expand All @@ -70,22 +60,17 @@ pub fn decrement_by_at_most(
change_value = decremented - max_amount;
decremented -= change_value;
}
increment(context, balance, change_value, owner);
increment(balance, change_value, owner);

decremented
}

// Removes the note from the owner's set of notes.
// Returns the value of the destroyed note.
pub fn destroy_note(
mut context: PrivateContext,
balance: PrivateSet<ValueNote>,
owner: AztecAddress,
note: ValueNote
) -> Field {
pub fn destroy_note(balance: PrivateSet<ValueNote>, owner: AztecAddress, note: ValueNote) -> Field {
// Ensure the note is actually owned by the owner (to prevent user from generating a valid proof while
// spending someone else's notes).
let owner_npk_m_hash = get_npk_m_hash(&mut context, owner);
let owner_npk_m_hash = get_npk_m_hash(balance.context.private.unwrap(), owner);

// TODO (#6312): This will break with key rotation. Fix this. Will not be able to pass this after rotating keys.
assert(note.npk_m_hash.eq(owner_npk_m_hash));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ contract Benchmarking {
// Creates a new value note for the target owner. Use this method to seed an initial set of notes.
#[aztec(private)]
fn create_note(owner: AztecAddress, value: Field) {
increment(context, storage.notes.at(owner), value, owner);
increment(storage.notes.at(owner), value, owner);
}

// Deletes a note at a specific index in the set and creates a new one with the same value.
Expand All @@ -33,7 +33,7 @@ contract Benchmarking {
let notes = owner_notes.get_notes(getter_options.set_limit(1).set_offset(index));
let note = notes[0].unwrap_unchecked();
owner_notes.remove(note);
increment(context, owner_notes, note.value, owner);
increment(owner_notes, note.value, owner);
}

// Reads and writes to public storage and enqueues a call to another public function.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ contract StatefulTest {
fn create_note(owner: AztecAddress, value: Field) {
if (value != 0) {
let loc = storage.notes.at(owner);
increment(context, loc, value, owner);
increment(loc, value, owner);
}
}

Expand All @@ -47,7 +47,7 @@ contract StatefulTest {
if (value != 0) {
let loc = storage.notes.at(owner);
// TODO (#6312): This will break with key rotation. Fix this. Will not be able to spend / increment any notes after rotating key.
increment(context, loc, value, owner);
increment(loc, value, owner);
}
}

Expand All @@ -57,10 +57,10 @@ contract StatefulTest {
let sender = context.msg_sender();

let sender_notes = storage.notes.at(sender);
decrement(context, sender_notes, amount, sender);
decrement(sender_notes, amount, sender);

let recipient_notes = storage.notes.at(recipient);
increment(context, recipient_notes, amount, recipient);
increment(recipient_notes, amount, recipient);
}

#[aztec(private)]
Expand All @@ -69,10 +69,10 @@ contract StatefulTest {
let sender = context.msg_sender();

let sender_notes = storage.notes.at(sender);
decrement(context, sender_notes, amount, sender);
decrement(sender_notes, amount, sender);

let recipient_notes = storage.notes.at(recipient);
increment(context, recipient_notes, amount, recipient);
increment(recipient_notes, amount, recipient);
}

#[aztec(public)]
Expand Down

0 comments on commit 3a4d828

Please sign in to comment.