Skip to content

Commit

Permalink
get_notes cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Aug 7, 2024
1 parent 18f44a3 commit f6f5075
Show file tree
Hide file tree
Showing 19 changed files with 33 additions and 58 deletions.
8 changes: 0 additions & 8 deletions noir-projects/aztec-nr/aztec/src/note/note_getter/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,6 @@ pub fn get_notes<Note, let N: u32, let M: u32, FILTER_ARGS>(
context: &mut PrivateContext,
storage_slot: Field,
options: NoteGetterOptions<Note, N, M, FILTER_ARGS>
) -> BoundedVec<Note, MAX_NOTE_HASH_READ_REQUESTS_PER_CALL> where Note: NoteInterface<N, M> + Eq {
get_notes_and_hashes(context, storage_slot, options).map(|note_and_hash: NoteAndHash<Note>| note_and_hash.note)
}

pub fn get_notes_and_hashes<Note, let N: u32, let M: u32, FILTER_ARGS>(
context: &mut PrivateContext,
storage_slot: Field,
options: NoteGetterOptions<Note, N, M, FILTER_ARGS>
) -> BoundedVec<NoteAndHash<Note>, MAX_NOTE_HASH_READ_REQUESTS_PER_CALL> where Note: NoteInterface<N, M> + Eq {
let opt_notes = get_notes_internal(storage_slot, options);

Expand Down
11 changes: 2 additions & 9 deletions noir-projects/aztec-nr/aztec/src/state_vars/private_set.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use dep::protocol_types::{constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, abis:
use crate::context::{PrivateContext, PublicContext, UnconstrainedContext};
use crate::note::{
constants::MAX_NOTES_PER_PAGE, lifecycle::{create_note, create_note_hash_from_public, destroy_note},
note_getter::{get_notes_and_hashes, get_notes, view_notes}, note_getter_options::NoteGetterOptions,
note_getter::{get_notes, view_notes}, note_getter_options::NoteGetterOptions,
note_header::NoteHeader, note_interface::NoteInterface, note_viewer_options::NoteViewerOptions,
utils::compute_note_hash_for_read_request, note_emission::NoteEmission, note_and_hash::NoteAndHash
};
Expand Down Expand Up @@ -54,17 +54,10 @@ impl<Note, let N: u32, let M: u32> PrivateSet<Note, &mut PrivateContext> where N
pub fn get_notes<FILTER_ARGS>(
self,
options: NoteGetterOptions<Note, N, M, FILTER_ARGS>
) -> BoundedVec<Note, MAX_NOTE_HASH_READ_REQUESTS_PER_CALL> {
) -> BoundedVec<NoteAndHash<Note>, MAX_NOTE_HASH_READ_REQUESTS_PER_CALL> {
get_notes(self.context, self.storage_slot, options)
}
// docs:end:get_notes

pub fn get_notes_and_hashes<FILTER_ARGS>(
self,
options: NoteGetterOptions<Note, N, M, FILTER_ARGS>
) -> BoundedVec<NoteAndHash<Note>, MAX_NOTE_HASH_READ_REQUESTS_PER_CALL> {
get_notes_and_hashes(self.context, self.storage_slot, options)
}
}

impl<Note, let N: u32, let M: u32> PrivateSet<Note, UnconstrainedContext> where Note: NoteInterface<N, M> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl<Context> EasyPrivateUint<&mut PrivateContext> {

// docs:start:get_notes
let options = NoteGetterOptions::with_filter(filter_notes_min_sum, subtrahend as Field);
let notes_and_hashes = self.set.get_notes_and_hashes(options);
let notes_and_hashes = self.set.get_notes(options);
// docs:end:get_notes

let mut minuend: u64 = 0;
Expand Down
16 changes: 3 additions & 13 deletions noir-projects/aztec-nr/value-note/src/utils.nr
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,15 @@ pub fn decrement_by_at_most(
outgoing_viewer: AztecAddress
) -> Field {
let options = create_note_getter_options_for_decreasing_balance(max_amount);
let notes_and_hashes = balance.get_notes_and_hashes(options);
let notes_and_hashes = balance.get_notes(options);

let mut decremented = 0;
for i in 0..options.limit {
if i < notes_and_hashes.len() {
let note_and_hash = notes_and_hashes.get_unchecked(i);

decremented += destroy_note(balance, note_and_hash);
balance.remove(note_and_hash);
decremented += note_and_hash.note.value;
}
}

Expand All @@ -76,14 +77,3 @@ pub fn decrement_by_at_most(

decremented
}

// Removes the note from the owner's set of notes.
// Returns the value of the destroyed note.
pub fn destroy_note(
balance: PrivateSet<ValueNote, &mut PrivateContext>,
note_and_hash: NoteAndHash<ValueNote>
) -> Field {
balance.remove(note_and_hash);

note_and_hash.note.value
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ contract Benchmarking {
fn recreate_note(owner: AztecAddress, outgoing_viewer: AztecAddress, index: u32) {
let owner_notes = storage.notes.at(owner);
let mut getter_options = NoteGetterOptions::new();
let notes_and_hashes = owner_notes.get_notes_and_hashes(getter_options.set_limit(1).set_offset(index));
let notes_and_hashes = owner_notes.get_notes(getter_options.set_limit(1).set_offset(index));
let note_and_hash = notes_and_hashes.get(0);
owner_notes.remove(note_and_hash);
increment(owner_notes, note_and_hash.note.value, owner, outgoing_viewer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl Deck<&mut PrivateContext> {

pub fn get_cards_and_note_hashes<N>(&mut self, cards: [Card; N]) -> ([CardNote; N], [Field; N]) {
let options = NoteGetterOptions::with_filter(filter_cards, cards);
let notes_and_hashes = self.set.get_notes_and_hashes(options);
let notes_and_hashes = self.set.get_notes(options);

// This array will hold the notes that correspond to each of the requested cards. It begins empty (with all the
// options being none) and we gradually fill it up as we find the matching notes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ contract Child {
let mut options = NoteGetterOptions::new();
options = options.select(ValueNote::properties().value, amount, Option::none()).set_limit(1);
let notes = storage.a_map_with_private_values.at(owner).get_notes(options);
notes.get(0).value
notes.get(0).note.value
}

// Increments `current_value` by `new_value`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ contract DelegatedOn {
let mut options = NoteGetterOptions::new();
options = options.select(ValueNote::properties().value, amount, Option::none()).set_limit(1);
let notes = storage.a_map_with_private_values.at(owner).get_notes(options);
notes.get(0).value
notes.get(0).note.value
}

unconstrained fn view_public_value() -> pub Field {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ contract Delegator {
let mut options = NoteGetterOptions::new();
options = options.select(ValueNote::properties().value, amount, Option::none()).set_limit(1);
let notes = storage.a_map_with_private_values.at(owner).get_notes(options);
notes.get_unchecked(0).value
notes.get_unchecked(0).note.value
}

unconstrained fn view_public_value() -> pub Field {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ contract InclusionProofs {
if (nullified) {
options = options.set_status(NoteStatus.ACTIVE_OR_NULLIFIED);
}
let note = private_values.get_notes(options).get(0);
let note = private_values.get_notes(options).get(0).note;
// docs:end:get_note_from_pxe

// docs:start:prove_note_inclusion
Expand Down Expand Up @@ -104,7 +104,7 @@ contract InclusionProofs {
if (fail_case) {
options = options.set_status(NoteStatus.ACTIVE_OR_NULLIFIED);
}
let note = private_values.get_notes(options).get(0);
let note = private_values.get_notes(options).get(0).note;

let header = if (use_block_number) {
context.get_header_at(block_number)
Expand All @@ -130,7 +130,7 @@ contract InclusionProofs {
if (nullified) {
options = options.set_status(NoteStatus.ACTIVE_OR_NULLIFIED);
}
let note = private_values.get_notes(options).get(0);
let note = private_values.get_notes(options).get(0).note;

// 2) Prove the note validity
let header = if (use_block_number) {
Expand All @@ -149,7 +149,7 @@ contract InclusionProofs {
let private_values = storage.private_values.at(owner);
let mut options = NoteGetterOptions::new();
options = options.set_limit(1);
let notes_and_hashes = private_values.get_notes_and_hashes(options);
let notes_and_hashes = private_values.get_notes(options);
let note_and_hash = notes_and_hashes.get(0);

private_values.remove(note_and_hash);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ contract PendingNoteHashes {

let options = NoteGetterOptions::with_filter(filter_notes_min_sum, amount);
// get note inserted above
let notes_and_hashes = owner_balance.get_notes_and_hashes(options);
let notes_and_hashes = owner_balance.get_notes(options);

let note_and_hash0 = notes_and_hashes.get(0);
assert(note.value == note_and_hash0.note.value);
Expand Down Expand Up @@ -137,7 +137,7 @@ contract PendingNoteHashes {

let mut options = NoteGetterOptions::new();
options = options.set_limit(1);
let note_and_hash = owner_balance.get_notes_and_hashes(options).get(0);
let note_and_hash = owner_balance.get_notes(options).get(0);

assert(expected_value == note_and_hash.note.value);

Expand Down Expand Up @@ -378,7 +378,7 @@ contract PendingNoteHashes {
#[contract_library_method]
fn destroy_max_notes(owner: AztecAddress, storage: Storage<&mut PrivateContext>) {
let owner_balance = storage.balances.at(owner);
let notes_and_hashes = owner_balance.get_notes_and_hashes(NoteGetterOptions::new());
let notes_and_hashes = owner_balance.get_notes(NoteGetterOptions::new());

for i in 0..max_notes_per_call() {
let note_and_hash = notes_and_hashes.get(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ contract StaticChild {
Option::none()
).set_limit(1);
let notes = storage.a_private_value.get_notes(options);
notes.get(0).value
notes.get(0).note.value
}

// Increments `current_value` by `new_value`
Expand Down
16 changes: 8 additions & 8 deletions noir-projects/noir-contracts/contracts/test_contract/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ contract Test {
hash::{pedersen_hash, compute_secret_hash, ArgsHasher},
note::{
lifecycle::{create_note, destroy_note}, note_getter::{get_notes, view_notes},
note_getter_options::NoteStatus
note_getter_options::NoteStatus, note_and_hash::NoteAndHash
},
deploy::deploy_contract as aztec_deploy_contract,
oracle::{encryption::aes128_encrypt, unsafe_rand::unsafe_rand}
Expand Down Expand Up @@ -117,9 +117,9 @@ contract Test {
options = options.set_status(NoteStatus.ACTIVE_OR_NULLIFIED);
}

let notes: BoundedVec<ValueNote, MAX_NOTE_HASH_READ_REQUESTS_PER_CALL> = get_notes(&mut context, storage_slot, options);
let notes: BoundedVec<NoteAndHash<ValueNote>, MAX_NOTE_HASH_READ_REQUESTS_PER_CALL> = get_notes(&mut context, storage_slot, options);

notes.get(0).value
notes.get(0).note.value
}

#[aztec(private)]
Expand All @@ -133,9 +133,9 @@ contract Test {
options = options.set_status(NoteStatus.ACTIVE_OR_NULLIFIED);
}

let notes: BoundedVec<ValueNote, MAX_NOTE_HASH_READ_REQUESTS_PER_CALL> = get_notes(&mut context, storage_slot, options);
let notes: BoundedVec<NoteAndHash<ValueNote>, MAX_NOTE_HASH_READ_REQUESTS_PER_CALL> = get_notes(&mut context, storage_slot, options);

[notes.get(0).value, notes.get(1).value]
[notes.get(0).note.value, notes.get(1).note.value]
}

unconstrained fn call_view_notes(storage_slot: Field, active_or_nullified: bool) -> pub Field {
Expand Down Expand Up @@ -175,9 +175,9 @@ contract Test {
);

let options = NoteGetterOptions::new();
let notes: BoundedVec<ValueNote, MAX_NOTE_HASH_READ_REQUESTS_PER_CALL> = get_notes(&mut context, storage_slot, options);
let notes: BoundedVec<NoteAndHash<ValueNote>, MAX_NOTE_HASH_READ_REQUESTS_PER_CALL> = get_notes(&mut context, storage_slot, options);

let note = notes.get(0);
let note = notes.get(0).note;

destroy_note(&mut context, note);
}
Expand Down Expand Up @@ -474,7 +474,7 @@ contract Test {
let secret_hash = compute_secret_hash(secret);
let mut options = NoteGetterOptions::new();
options = options.select(TestNote::properties().value, secret_hash, Option::none()).set_limit(1);
let notes_and_hashes = notes_set.get_notes_and_hashes(options);
let notes_and_hashes = notes_set.get_notes(options);
let note_and_hash = notes_and_hashes.get(0);
notes_set.remove(note_and_hash);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ contract TokenBlacklist {
secret_hash,
Option::none()
).set_limit(1);
let notes_and_hashes = pending_shields.get_notes_and_hashes(options);
let notes_and_hashes = pending_shields.get_notes(options);
let note_and_hash = notes_and_hashes.get(0);
// Remove the note from the pending shields set
pending_shields.remove(note_and_hash);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl<T> BalancesMap<T, &mut PrivateContext> {
) -> OuterNoteEmission<T> where T: NoteInterface<T_SERIALIZED_LEN, T_SERIALIZED_BYTES_LEN> + OwnedNote + Eq {
// docs:start:get_notes
let options = NoteGetterOptions::with_filter(filter_notes_min_sum, subtrahend);
let notes_and_hashes = self.map.at(owner).get_notes_and_hashes(options);
let notes_and_hashes = self.map.at(owner).get_notes(options);
// docs:end:get_notes

let mut minuend: U128 = U128::from_integer(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ contract Token {
secret_hash,
Option::none()
).set_limit(1);
let notes_and_hashes = pending_shields.get_notes_and_hashes(options);
let notes_and_hashes = pending_shields.get_notes(options);
let note_and_hash = notes_and_hashes.get(0);
// Remove the note from the pending shields set
pending_shields.remove(note_and_hash);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl<T> BalancesMap<T, &mut PrivateContext> {
) -> U128 where T: NoteInterface<T_SERIALIZED_LEN, T_SERIALIZED_BYTES_LEN> + OwnedNote + Eq {
// docs:start:get_notes
let options = NoteGetterOptions::with_filter(filter_notes_min_sum, target_amount).set_limit(max_notes);
let notes_and_hashes = self.map.at(owner).get_notes_and_hashes(options);
let notes_and_hashes = self.map.at(owner).get_notes(options);
// docs:end:get_notes

let mut subtracted = U128::from_integer(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ contract TokenWithRefunds {
secret_hash,
Option::none()
).set_limit(1);
let notes_and_hashes = pending_shields.get_notes_and_hashes(options);
let notes_and_hashes = pending_shields.get_notes(options);
let note_and_hash = notes_and_hashes.get_unchecked(0);
// Remove the note from the pending shields set
pending_shields.remove(note_and_hash);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl<T> BalancesMap<T, &mut PrivateContext> {
) -> OuterNoteEmission<T> where T: NoteInterface<T_SERIALIZED_LEN, T_SERIALIZED_BYTES_LEN> + OwnedNote + Eq {
// docs:start:get_notes
let options = NoteGetterOptions::with_filter(filter_notes_min_sum, subtrahend);
let notes_and_hashes = self.map.at(owner).get_notes_and_hashes(options);
let notes_and_hashes = self.map.at(owner).get_notes(options);
// docs:end:get_notes

let mut minuend: U128 = U128::from_integer(0);
Expand Down

0 comments on commit f6f5075

Please sign in to comment.