Skip to content

Commit fa03f6c

Browse files
authored
chore: update docs on comparators (AztecProtocol#4281)
1 parent b15d61d commit fa03f6c

3 files changed

Lines changed: 21 additions & 4 deletions

File tree

docs/docs/developers/contracts/syntax/storage/private_state.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ You can view the implementation [here](https://github.com/AztecProtocol/aztec-pa
250250

251251
#### `selects: BoundedVec<Option<Select>, N>`
252252

253-
`selects` is a collection of filtering criteria, specified by `Select { field_index: u8, value: Field }` structs. It instructs the data oracle to find notes whose (`field_index`)th field matches the provided `value`.
253+
`selects` is a collection of filtering criteria, specified by `Select { field_index: u8, value: Field, comparator: u3 }` structs. It instructs the data oracle to find notes whose (`field_index`)th field matches the provided `value`, according to the `comparator`.
254254

255255
#### `sorts: BoundedVec<Option<Sort>, N>`
256256

@@ -304,6 +304,8 @@ This method sets the offset value, which determines where to start retrieving no
304304

305305
#### Examples
306306

307+
##### Example 1
308+
307309
The following code snippet creates an instance of `NoteGetterOptions`, which has been configured to find the cards that belong to `account_address`. The returned cards are sorted by their points in descending order, and the first `offset` cards with the highest points are skipped.
308310

309311
#include_code state_vars-NoteGetterOptionsSelectSortOffset /yarn-project/noir-contracts/contracts/docs_example_contract/src/options.nr rust
@@ -314,24 +316,35 @@ The first value of `.select` and `.sort` is the index of a field in a note type.
314316

315317
The indices are: 0 for `points`, 1 for `secret`, and 2 for `owner`.
316318

317-
In the example, `.select(2, account_address)` matches the 2nd field of `CardNote`, which is `owner`, and returns the cards whose `owner` field equals `account_address`.
319+
In the example, `.select(2, account_address, Option::none())` matches the 2nd field of `CardNote`, which is `owner`, and returns the cards whose `owner` field equals `account_address`, equality is the comparator used because because including no comparator (the third argument) defaults to using the equality comparator. The current possible values of Comparator are specified in the Note Getter Options implementation linked above.
318320

319321
`.sort(0, SortOrder.DESC)` sorts the 0th field of `CardNote`, which is `points`, in descending order.
320322

321323
There can be as many conditions as the number of fields a note type has. The following example finds cards whose fields match the three given values:
322324

323325
#include_code state_vars-NoteGetterOptionsMultiSelects /yarn-project/noir-contracts/contracts/docs_example_contract/src/options.nr rust
324326

325-
While `selects` lets us find notes with specific values, `filter` lets us find notes in a more dynamic way. The function below picks the cards whose points are at least `min_points`:
327+
While `selects` lets us find notes with specific values, `filter` lets us find notes in a more dynamic way. The function below picks the cards whose points are at least `min_points`, although this now can be done by using the select function with a GTE comparator:
326328

327329
#include_code state_vars-OptionFilter /yarn-project/noir-contracts/contracts/docs_example_contract/src/options.nr rust
328330

329331
We can use it as a filter to further reduce the number of the final notes:
330332

331333
#include_code state_vars-NoteGetterOptionsFilter /yarn-project/noir-contracts/contracts/docs_example_contract/src/options.nr rust
332334

333-
One thing to remember is, `filter` will be applied on the notes after they are picked from the database. Therefore, it's possible that the actual notes we end up getting are fewer than the limit.
335+
One thing to remember is, `filter` will be applied on the notes after they are picked from the database, so it is more efficient to use select with comparators where possible. Another side effect of this is that it's possible that the actual notes we end up getting are fewer than the limit.
334336

335337
The limit is `MAX_READ_REQUESTS_PER_CALL` by default. But we can set it to any value **smaller** than that:
336338

337339
#include_code state_vars-NoteGetterOptionsPickOne /yarn-project/noir-contracts/contracts/docs_example_contract/src/options.nr rust
340+
341+
##### Example 2
342+
343+
An example of how we can use a Comparator to select notes when calling a Noir contract from aztec.js is below.
344+
345+
#include_code state_vars-NoteGetterOptionsComparatorExampleTs /yarn-project/end-to-end/src/e2e_note_getter.test.ts typescript
346+
347+
In this example, we use the above typescript code to invoke a call to our Noir contract below. This Noir contract function takes an input to match with, and a comparator to use when fetching and selecting notes from storage.
348+
349+
#include_code state_vars-NoteGetterOptionsComparatorExampleNoir /yarn-project/noir-contracts/contracts/docs_example_contract/src/main.nr rust
350+

yarn-project/end-to-end/src/e2e_note_getter.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ describe('e2e_note_getter', () => {
4949
contract.methods.read_note(5, Comparator.LT).view(),
5050
contract.methods.read_note(5, Comparator.GT).view(),
5151
contract.methods.read_note(5, Comparator.LTE).view(),
52+
// docs:start:state_vars-NoteGetterOptionsComparatorExampleTs
5253
contract.methods.read_note(5, Comparator.GTE).view(),
54+
// docs:end:state_vars-NoteGetterOptionsComparatorExampleTs
5355
]);
5456

5557
expect(

yarn-project/noir-contracts/contracts/docs_example_contract/src/main.nr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,14 @@ contract DocsExample {
109109
storage.test.insert(&mut note, true);
110110
}
111111

112+
// docs:start:state_vars-NoteGetterOptionsComparatorExampleNoir
112113
unconstrained fn read_note(amount: Field, comparator: u3) -> pub [Option<CardNote>; 10] {
113114
let options = NoteViewerOptions::new().select(0, amount, Option::some(comparator));
114115
let notes = storage.test.view_notes(options);
115116

116117
notes
117118
}
119+
// docs:end:state_vars-NoteGetterOptionsComparatorExampleNoir
118120

119121
#[aztec(private)]
120122
fn update_legendary_card(randomness: Field, points: u8) {

0 commit comments

Comments
 (0)