Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add comparators to get/view note (#3136) #4205

Merged
merged 8 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions yarn-project/acir-simulator/src/acvm/oracle/oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ export class Oracle {
[numSelects]: ACVMField[],
selectBy: ACVMField[],
selectValues: ACVMField[],
selectComparators: ACVMField[],
sortBy: ACVMField[],
sortOrder: ACVMField[],
[limit]: ACVMField[],
Expand All @@ -178,6 +179,7 @@ export class Oracle {
+numSelects,
selectBy.map(s => +s),
selectValues.map(fromACVMField),
selectComparators.map(s => +s),
sortBy.map(s => +s),
sortOrder.map(s => +s),
+limit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export abstract class TypedOracle {
_numSelects: number,
_selectBy: number[],
_selectValues: Fr[],
_selectComparators: number[],
_sortBy: number[],
_sortOrder: number[],
_limit: number,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ export class ClientExecutionContext extends ViewDataOracle {
* @param numSelects - The number of valid selects in selectBy and selectValues.
* @param selectBy - An array of indices of the fields to selects.
* @param selectValues - The values to match.
* @param selectComparators - The comparators to match by.
* @param sortBy - An array of indices of the fields to sort.
* @param sortOrder - The order of the corresponding index in sortBy. (1: DESC, 2: ASC, 0: Do nothing)
* @param limit - The number of notes to retrieve per query.
Expand All @@ -196,6 +197,7 @@ export class ClientExecutionContext extends ViewDataOracle {
numSelects: number,
selectBy: number[],
selectValues: Fr[],
selectComparators: number[],
sortBy: number[],
sortOrder: number[],
limit: number,
Expand All @@ -209,7 +211,7 @@ export class ClientExecutionContext extends ViewDataOracle {
const dbNotesFiltered = dbNotes.filter(n => !pendingNullifiers.has((n.siloedNullifier as Fr).value));

const notes = pickNotes<NoteData>([...dbNotesFiltered, ...pendingNotes], {
selects: selectBy.slice(0, numSelects).map((index, i) => ({ index, value: selectValues[i] })),
selects: selectBy.slice(0, numSelects).map((index, i) => ({ index, value: selectValues[i], comparator: selectComparators[i] })),
sorts: sortBy.map((index, i) => ({ index, order: sortOrder[i] })),
limit,
offset,
Expand Down
102 changes: 92 additions & 10 deletions yarn-project/acir-simulator/src/client/pick_notes.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Note } from '@aztec/circuit-types';
import { Note, Comparator } from '@aztec/circuit-types';
import { Fr } from '@aztec/foundation/fields';

import { SortOrder, pickNotes } from './pick_notes.js';
Expand Down Expand Up @@ -141,7 +141,7 @@ describe('getNotes', () => {
];

{
const options = { selects: [{ index: 0, value: new Fr(2n) }] };
const options = { selects: [{ index: 0, value: new Fr(2n), comparator: Comparator.EQ }] };
const result = pickNotes(notes, options);
expectNotes(result, [
[2n, 1n, 3n],
Expand All @@ -153,8 +153,8 @@ describe('getNotes', () => {
{
const options = {
selects: [
{ index: 0, value: new Fr(2n) },
{ index: 2, value: new Fr(3n) },
{ index: 0, value: new Fr(2n), comparator: Comparator.EQ },
{ index: 2, value: new Fr(3n), comparator: Comparator.EQ },
],
};
const result = pickNotes(notes, options);
Expand All @@ -167,25 +167,25 @@ describe('getNotes', () => {
{
const options = {
selects: [
{ index: 1, value: new Fr(2n) },
{ index: 2, value: new Fr(3n) },
{ index: 1, value: new Fr(2n), comparator: Comparator.EQ },
{ index: 2, value: new Fr(3n), comparator: Comparator.EQ },
],
};
const result = pickNotes(notes, options);
expectNotes(result, [[1n, 2n, 3n]]);
}

{
const options = { selects: [{ index: 1, value: new Fr(5n) }] };
const options = { selects: [{ index: 1, value: new Fr(5n), comparator: Comparator.EQ }] };
const result = pickNotes(notes, options);
expectNotes(result, []);
}

{
const options = {
selects: [
{ index: 0, value: new Fr(2n) },
{ index: 1, value: new Fr(5n) },
{ index: 0, value: new Fr(2n), comparator: Comparator.EQ },
{ index: 1, value: new Fr(5n), comparator: Comparator.EQ },
],
};
const result = pickNotes(notes, options);
Expand All @@ -203,7 +203,7 @@ describe('getNotes', () => {
createNote([6n, 5n, 8n]),
];

const options = { selects: [{ index: 2, value: new Fr(8n) }], sorts: [{ index: 1, order: SortOrder.ASC }] };
const options = { selects: [{ index: 2, value: new Fr(8n), comparator: Comparator.EQ }], sorts: [{ index: 1, order: SortOrder.ASC }] };
const result = pickNotes(notes, options);
expectNotes(result, [
[0n, 0n, 8n],
Expand All @@ -212,4 +212,86 @@ describe('getNotes', () => {
[7n, 6n, 8n],
]);
});

it('should get sorted matching notes with GTE and LTE', () => {
const notes = [
createNote([2n, 1n, 3n]),
createNote([4n, 5n, 8n]),
createNote([7n, 6n, 8n]),
createNote([6n, 5n, 2n]),
createNote([0n, 0n, 8n]),
createNote([6n, 5n, 8n]),
];

const options = {
selects: [{
index: 2,
value: new Fr(7n),
comparator: Comparator.GTE,
}, {
index: 2,
value: new Fr(8n),
comparator: Comparator.LTE,
}],
sorts: [{
index: 1, order: SortOrder.ASC
}],
};
const result = pickNotes(notes, options);
expectNotes(result, [
[0n, 0n, 8n],
[4n, 5n, 8n],
[6n, 5n, 8n],
[7n, 6n, 8n],
]);
});

it('should get sorted matching notes with GTE and LTE', () => {
const notes = [
createNote([2n, 1n, 1n]),
createNote([4n, 5n, 2n]),
createNote([7n, 6n, 3n]),
createNote([6n, 5n, 4n]),
createNote([0n, 0n, 5n]),
createNote([6n, 5n, 6n]),
];

const options1 = {
selects: [{
index: 2,
value: new Fr(3n),
comparator: Comparator.GT,
}],
sorts: [{
index: 1, order: SortOrder.ASC
}],
};

const result1 = pickNotes(notes, options1);

expectNotes(result1, [
[0n, 0n, 5n],
[6n, 5n, 4n],
[6n, 5n, 6n],
]);

const options2 = {
selects: [{
index: 2,
value: new Fr(4n),
comparator: Comparator.LT,
}],
sorts: [{
index: 1, order: SortOrder.ASC
}],
};

const result2 = pickNotes(notes, options2);

expectNotes(result2, [
[2n, 1n, 1n],
[4n, 5n, 2n],
[7n, 6n, 3n],
]);
});
});
19 changes: 17 additions & 2 deletions yarn-project/acir-simulator/src/client/pick_notes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Note } from '@aztec/circuit-types';
import { Comparator, Note } from '@aztec/circuit-types';
import { Fr } from '@aztec/foundation/fields';

/**
Expand All @@ -13,6 +13,10 @@ export interface Select {
* Required value of the field.
*/
value: Fr;
/**
* The comparator to use
*/
comparator: Comparator;
}

/**
Expand Down Expand Up @@ -75,7 +79,18 @@ interface ContainsNote {
}

const selectNotes = <T extends ContainsNote>(noteDatas: T[], selects: Select[]): T[] =>
noteDatas.filter(noteData => selects.every(({ index, value }) => noteData.note.items[index]?.equals(value)));
noteDatas.filter(noteData => selects.every(({ index, value, comparator }) => {
const comparatorSelector = {
[Comparator.EQ]: () => noteData.note.items[index].equals(value),
[Comparator.NEQ]: () => !noteData.note.items[index].equals(value),
[Comparator.LT]: () => noteData.note.items[index].lt(value),
[Comparator.LTE]: () => noteData.note.items[index].lt(value) || noteData.note.items[index].equals(value),
[Comparator.GT]: () => !noteData.note.items[index].lt(value) && !noteData.note.items[index].equals(value),
[Comparator.GTE]: () => !noteData.note.items[index].lt(value),
}

return comparatorSelector[comparator]();
}));

const sortNotes = (a: Fr[], b: Fr[], sorts: Sort[], level = 0): number => {
if (sorts[level] === undefined) {
Expand Down
4 changes: 3 additions & 1 deletion yarn-project/acir-simulator/src/client/view_data_oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ export class ViewDataOracle extends TypedOracle {
* @param numSelects - The number of valid selects in selectBy and selectValues.
* @param selectBy - An array of indices of the fields to selects.
* @param selectValues - The values to match.
* @param selectComparators - The comparators to use to match values.
* @param sortBy - An array of indices of the fields to sort.
* @param sortOrder - The order of the corresponding index in sortBy. (1: DESC, 2: ASC, 0: Do nothing)
* @param limit - The number of notes to retrieve per query.
Expand All @@ -209,14 +210,15 @@ export class ViewDataOracle extends TypedOracle {
numSelects: number,
selectBy: number[],
selectValues: Fr[],
selectComparators: number[],
sortBy: number[],
sortOrder: number[],
limit: number,
offset: number,
): Promise<NoteData[]> {
const dbNotes = await this.db.getNotes(this.contractAddress, storageSlot);
return pickNotes<NoteData>(dbNotes, {
selects: selectBy.slice(0, numSelects).map((index, i) => ({ index, value: selectValues[i] })),
selects: selectBy.slice(0, numSelects).map((index, i) => ({ index, value: selectValues[i], comparator: selectComparators[i] })),
sorts: sortBy.map((index, i) => ({ index, order: sortOrder[i] })),
limit,
offset,
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/archiver/src/archiver/eth_log_handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ async function getBlockFromCallData(
if (functionName !== 'process') {
throw new Error(`Unexpected method called ${functionName}`);
}
const [headerHex, archiveRootHex, , bodyHex] = args! as [Hex, Hex, Hex, Hex, Hex];
const [headerHex, archiveRootHex, , bodyHex, ] = args! as [Hex, Hex, Hex, Hex, Hex];
const blockBuffer = Buffer.concat([
Buffer.from(hexToBytes(headerHex)),
Buffer.from(hexToBytes(archiveRootHex)), // L2Block.archive.root
Expand Down
35 changes: 29 additions & 6 deletions yarn-project/aztec-nr/aztec/src/note/note_getter.nr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use dep::protocol_types::constants::{
};
use crate::context::PrivateContext;
use crate::note::{
note_getter_options::{NoteGetterOptions, Select, Sort, SortOrder},
note_getter_options::{NoteGetterOptions, Select, Sort, SortOrder, Comparator},
note_interface::NoteInterface,
note_viewer_options::NoteViewerOptions,
utils::compute_note_hash_for_read_or_nullify,
Expand All @@ -32,7 +32,24 @@ fn check_note_header<Note, N>(
fn check_note_fields<N>(fields: [Field; N], selects: BoundedVec<Option<Select>, N>) {
for i in 0..selects.len {
let select = selects.get_unchecked(i).unwrap_unchecked();
assert(fields[select.field_index] == select.value, "Mismatch return note field.");

// Values are computed ahead of time because circuits evaluate all branches
let isEqual = fields[select.field_index] == select.value;
let isLt = fields[select.field_index].lt(select.value);

if (select.comparator == Comparator.EQ) {
assert(isEqual, "Mismatch return note field.");
} else if (select.comparator == Comparator.NEQ) {
assert(!isEqual, "Mismatch return note field.");
} else if (select.comparator == Comparator.LT) {
assert(isLt, "Mismatch return note field.");
} else if (select.comparator == Comparator.LTE) {
assert(isLt | isEqual, "Mismatch return note field.");
} else if (select.comparator == Comparator.GT) {
assert(!isLt & !isEqual, "Mismatch return note field.");
} else if (select.comparator == Comparator.GTE) {
assert(!isLt, "Mismatch return note field.");
}
}
}

Expand Down Expand Up @@ -115,6 +132,7 @@ unconstrained fn get_note_internal<Note, N>(storage_slot: Field, note_interface:
[],
[],
[],
[],
1, // limit
0, // offset
placeholder_note,
Expand All @@ -127,7 +145,7 @@ unconstrained fn get_notes_internal<Note, N, FILTER_ARGS>(
note_interface: NoteInterface<Note, N>,
options: NoteGetterOptions<Note, N, FILTER_ARGS>
) -> [Option<Note>; MAX_READ_REQUESTS_PER_CALL] {
let (num_selects, select_by, select_values, sort_by, sort_order) = flatten_options(options.selects, options.sorts);
let (num_selects, select_by, select_values, select_comparators, sort_by, sort_order) = flatten_options(options.selects, options.sorts);
let placeholder_opt_notes = [Option::none(); MAX_READ_REQUESTS_PER_CALL];
let placeholder_fields = [0; GET_NOTES_ORACLE_RETURN_LENGTH];
let opt_notes = oracle::notes::get_notes(
Expand All @@ -136,6 +154,7 @@ unconstrained fn get_notes_internal<Note, N, FILTER_ARGS>(
num_selects,
select_by,
select_values,
select_comparators,
sort_by,
sort_order,
options.limit,
Expand All @@ -154,7 +173,7 @@ unconstrained pub fn view_notes<Note, N>(
note_interface: NoteInterface<Note, N>,
options: NoteViewerOptions<Note, N>
) -> [Option<Note>; MAX_NOTES_PER_PAGE] {
let (num_selects, select_by, select_values, sort_by, sort_order) = flatten_options(options.selects, options.sorts);
let (num_selects, select_by, select_values, select_comparators, sort_by, sort_order) = flatten_options(options.selects, options.sorts);
let placeholder_opt_notes = [Option::none(); MAX_NOTES_PER_PAGE];
let placeholder_fields = [0; VIEW_NOTE_ORACLE_RETURN_LENGTH];
oracle::notes::get_notes(
Expand All @@ -163,6 +182,7 @@ unconstrained pub fn view_notes<Note, N>(
num_selects,
select_by,
select_values,
select_comparators,
sort_by,
sort_order,
options.limit,
Expand All @@ -175,15 +195,18 @@ unconstrained pub fn view_notes<Note, N>(
unconstrained fn flatten_options<Note, N>(
selects: BoundedVec<Option<Select>, N>,
sorts: BoundedVec<Option<Sort>, N>
) -> (u8, [u8; N], [Field; N], [u8; N], [u2; N]) {
) -> (u8, [u8; N], [Field; N], [u3; N], [u8; N], [u2; N]) {
let mut num_selects = 0;
let mut select_by = [0; N];
let mut select_values = [0; N];
let mut select_comparators = [0; N];

for i in 0..selects.len {
let select = selects.get(i);
if select.is_some() {
select_by[num_selects] = select.unwrap_unchecked().field_index;
select_values[num_selects] = select.unwrap_unchecked().value;
select_comparators[num_selects] = select.unwrap_unchecked().comparator;
num_selects += 1;
};
}
Expand All @@ -198,5 +221,5 @@ unconstrained fn flatten_options<Note, N>(
};
}

(num_selects, select_by, select_values, sort_by, sort_order)
(num_selects, select_by, select_values, select_comparators, sort_by, sort_order)
}
Loading