Skip to content

Commit

Permalink
using preprocessor in token contract
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Aug 9, 2024
1 parent 3979ac6 commit 2b15586
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
12 changes: 10 additions & 2 deletions noir-projects/aztec-nr/aztec/src/note/note_getter/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,17 @@ pub fn get_notes<Note, let N: u32, let M: u32, PREPROCESSOR_ARGS, FILTER_ARGS>(
) -> BoundedVec<Note, MAX_NOTE_HASH_READ_REQUESTS_PER_CALL> where Note: NoteInterface<N, M> + Eq {
let opt_notes = get_notes_internal(storage_slot, options);

// apply_filter_hin(...)
let preprocessed_notes = apply_preprocessor(opt_notes, options.preprocessor, options.preprocessor_args);

constrain_get_notes_internal(context, storage_slot, opt_notes, options)
constrain_get_notes_internal(context, storage_slot, preprocessed_notes, options)
}

unconstrained fn apply_preprocessor<Note, let N: u32, let M: u32, PREPROCESSOR_ARGS>(
notes: [Option<Note>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL],
preprocessor: fn([Option<Note>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL], PREPROCESSOR_ARGS) -> [Option<Note>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL],
preprocessor_args: PREPROCESSOR_ARGS
) -> [Option<Note>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL] {
preprocessor(notes, preprocessor_args)
}

fn constrain_get_notes_internal<Note, let N: u32, let M: u32, PREPROCESSOR_ARGS, FILTER_ARGS>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl<T> BalancesMap<T, &mut PrivateContext> {
target_amount: U128,
max_notes: u32
) -> U128 where T: NoteInterface<T_SERIALIZED_LEN, T_SERIALIZED_BYTES_LEN> + OwnedNote + Eq {
let options = NoteGetterOptions::with_filter(filter_notes_min_sum, target_amount).set_limit(max_notes);
let options = NoteGetterOptions::with_preprocessor(preprocess_notes_min_sum, target_amount).set_limit(max_notes);
let notes = self.map.at(owner).pop_notes(options);

let mut subtracted = U128::from_integer(0);
Expand All @@ -127,7 +127,7 @@ impl<T> BalancesMap<T, &mut PrivateContext> {
// The filter does not check if total sum is larger or equal to 'min_sum' - all it does is remove extra notes if it does
// reach that value.
// Note that proper usage of this filter requires for notes to be sorted in descending order.
pub fn filter_notes_min_sum<T, T_SERIALIZED_LEN, T_SERIALIZED_BYTES_LEN>(
pub fn preprocess_notes_min_sum<T, T_SERIALIZED_LEN, T_SERIALIZED_BYTES_LEN>(
notes: [Option<T>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL],
min_sum: U128
) -> [Option<T>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL] where T: NoteInterface<T_SERIALIZED_LEN, T_SERIALIZED_BYTES_LEN> + OwnedNote {
Expand Down

0 comments on commit 2b15586

Please sign in to comment.