Skip to content

Commit

Permalink
feat: make token transfer be recursive (#7730)
Browse files Browse the repository at this point in the history
Replaces AztecProtocol/aztec-packages#7271
Closes AztecProtocol/aztec-packages#7142
Closes AztecProtocol/aztec-packages#7362

This is quite similar to the implementation in #7271: `transfer`
consumes two notes, and if their amount is insufficient for the desired
transfer it calls a second internal function which recursively consumes
8 notes per iteration until either the amount is reached, or no more
notes are found. If the total amount consumed exceeds the transfer
amount, a change note is created.

This results in a much smaller transfer function for the scenario in
which two notes suffice, since we're using a `limit` value of 2 and
therefore only doing two iterations of note constraining (the expensive
part). Two notes is a good amount as it provides a way for change notes
to be consumed in follow-up calls.

The recursive call has a much lower gate count, since it doesn't need to
fetch keys, create notes, emit events, etc., and so we can afford to
read more notes here while still resulting in a relatively small
circuit.

I created a new e2e test in which the number of notes and their amounts
are quite controlled in order to properly test this. The tests are
unfortunately currently interdependent, but given the relative
simplicity of the test suite it should be hopefully simple to maintain
and expand, and maybe eventually fix.
  • Loading branch information
nventuro authored and AztecBot committed Aug 7, 2024
1 parent 396874e commit 684e950
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions value-note/src/filter.nr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub fn filter_notes_min_sum(
min_sum: Field
) -> [Option<ValueNote>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL] {
let mut selected = [Option::none(); MAX_NOTE_HASH_READ_REQUESTS_PER_CALL];

let mut sum = U128::from_integer(0);
for i in 0..notes.len() {
if notes[i].is_some() & (sum < U128::from_integer(min_sum)) {
Expand All @@ -14,5 +15,6 @@ pub fn filter_notes_min_sum(
sum += U128::from_integer(note.value);
}
}

selected
}

0 comments on commit 684e950

Please sign in to comment.