Skip to content

Commit

Permalink
chore: add note getter test to cci (#4236)
Browse files Browse the repository at this point in the history
  • Loading branch information
sklppy88 authored Jan 27, 2024
1 parent e1aad81 commit e1184ff
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 30 deletions.
13 changes: 13 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,17 @@ jobs:
name: "Test"
command: cond_spot_run_compose end-to-end 4 ./scripts/docker-compose.yml TEST=e2e_2_pxes.test.ts

e2e-note-getter:
docker:
- image: aztecprotocol/alpine-build-image
resource_class: small
steps:
- *checkout
- *setup_env
- run:
name: "Test"
command: cond_spot_run_compose end-to-end 4 ./scripts/docker-compose.yml TEST=e2e_note_getter.test.ts

e2e-multiple-accounts-1-enc-key:
docker:
- image: aztecprotocol/alpine-build-image
Expand Down Expand Up @@ -1221,6 +1232,7 @@ workflows:
- cli
<<: *defaults
- e2e-2-pxes: *e2e_test
- e2e-note-getter: *e2e_test
- e2e-deploy-contract: *e2e_test
- e2e-lending-contract: *e2e_test
- e2e-token-contract: *e2e_test
Expand Down Expand Up @@ -1260,6 +1272,7 @@ workflows:
requires:
- mainnet-fork
- e2e-2-pxes
- e2e-note-getter
- e2e-deploy-contract
- e2e-lending-contract
- e2e-token-contract
Expand Down
55 changes: 36 additions & 19 deletions yarn-project/end-to-end/src/e2e_note_getter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ interface NoirOption<T> {
_value: T;
}

const sortFunc = (a: any, b: any) =>
a.points > b.points ? 1 : a.points < b.points ? -1 : a.randomness > b.randomness ? 1 : -1;

function unwrapOptions<T>(options: NoirOption<T>[]): T[] {
return options.filter((option: any) => option._is_some).map((option: any) => option._value);
}
Expand All @@ -28,9 +31,17 @@ describe('e2e_note_getter', () => {
afterAll(() => teardown());

it('inserts notes from 0-9, then makes multiple queries specifying the total suite of comparators', async () => {
const numbers = [...Array(10).keys()];
await Promise.all(numbers.map(number => contract.methods.insert_note(number).send().wait()));
await contract.methods.insert_note(5).send().wait();
// ISSUE #4243
// Calling this function does not work like this
// const numbers = [...Array(10).keys()];
// await Promise.all(numbers.map(number => contract.methods.insert_note(number).send().wait()));
// It causes a race condition complaining about root mismatch

await contract.methods
.insert_notes([...Array(10).keys()])
.send()
.wait();
await contract.methods.insert_note(5, Fr.ZERO).send().wait();

const [returnEq, returnNeq, returnLt, returnGt, returnLte, returnGte] = await Promise.all([
contract.methods.read_note(5, Comparator.EQ).view(),
Expand All @@ -41,15 +52,21 @@ describe('e2e_note_getter', () => {
contract.methods.read_note(5, Comparator.GTE).view(),
]);

expect(unwrapOptions(returnEq).map(({ points, randomness }: any) => ({ points, randomness }))).toStrictEqual([
{ points: 5n, randomness: 1n },
{ points: 5n, randomness: 1n },
]);
expect(
unwrapOptions(returnEq)
.map(({ points, randomness }: any) => ({ points, randomness }))
.sort(sortFunc),
).toStrictEqual(
[
{ points: 5n, randomness: 1n },
{ points: 5n, randomness: 0n },
].sort(sortFunc),
);

expect(
unwrapOptions(returnNeq)
.map(({ points, randomness }: any) => ({ points, randomness }))
.sort((a: any, b: any) => (a.points > b.points ? 1 : -1)),
.sort(sortFunc),
).toStrictEqual(
[
{ points: 0n, randomness: 1n },
Expand All @@ -61,65 +78,65 @@ describe('e2e_note_getter', () => {
{ points: 8n, randomness: 1n },
{ points: 4n, randomness: 1n },
{ points: 3n, randomness: 1n },
].sort((a: any, b: any) => (a.points > b.points ? 1 : -1)),
].sort(sortFunc),
);

expect(
unwrapOptions(returnLt)
.map(({ points, randomness }: any) => ({ points, randomness }))
.sort((a: any, b: any) => (a.points > b.points ? 1 : -1)),
.sort(sortFunc),
).toStrictEqual(
[
{ points: 0n, randomness: 1n },
{ points: 1n, randomness: 1n },
{ points: 2n, randomness: 1n },
{ points: 4n, randomness: 1n },
{ points: 3n, randomness: 1n },
].sort((a: any, b: any) => (a.points > b.points ? 1 : -1)),
].sort(sortFunc),
);

expect(
unwrapOptions(returnGt)
.map(({ points, randomness }: any) => ({ points, randomness }))
.sort((a: any, b: any) => (a.points > b.points ? 1 : -1)),
.sort(sortFunc),
).toStrictEqual(
[
{ points: 7n, randomness: 1n },
{ points: 9n, randomness: 1n },
{ points: 6n, randomness: 1n },
{ points: 8n, randomness: 1n },
].sort((a: any, b: any) => (a.points > b.points ? 1 : -1)),
].sort(sortFunc),
);

expect(
unwrapOptions(returnLte)
.map(({ points, randomness }: any) => ({ points, randomness }))
.sort((a: any, b: any) => (a.points > b.points ? 1 : -1)),
.sort(sortFunc),
).toStrictEqual(
[
{ points: 5n, randomness: 1n },
{ points: 5n, randomness: 1n },
{ points: 5n, randomness: 0n },
{ points: 0n, randomness: 1n },
{ points: 1n, randomness: 1n },
{ points: 2n, randomness: 1n },
{ points: 4n, randomness: 1n },
{ points: 3n, randomness: 1n },
].sort((a: any, b: any) => (a.points > b.points ? 1 : -1)),
].sort(sortFunc),
);

expect(
unwrapOptions(returnGte)
.map(({ points, randomness }: any) => ({ points, randomness }))
.sort((a: any, b: any) => (a.points > b.points ? 1 : -1)),
.sort(sortFunc),
).toStrictEqual(
[
{ points: 5n, randomness: 1n },
{ points: 5n, randomness: 0n },
{ points: 5n, randomness: 1n },
{ points: 7n, randomness: 1n },
{ points: 9n, randomness: 1n },
{ points: 6n, randomness: 1n },
{ points: 8n, randomness: 1n },
].sort((a: any, b: any) => (a.points > b.points ? 1 : -1)),
].sort(sortFunc),
);
}, 300_000);
});
Original file line number Diff line number Diff line change
Expand Up @@ -96,24 +96,23 @@ contract DocsExample {
}

#[aztec(private)]
fn insert_note(amount: u8) {
let mut note = CardNote::new(amount, 1, context.msg_sender());
fn insert_notes(amounts: [u8; 10]) {
for i in 0..amounts.len() {
let mut note = CardNote::new(amounts[i], 1, context.msg_sender());
storage.test.insert(&mut note, true);
}
}

#[aztec(private)]
fn insert_note(amount: u8, randomness: Field) {
let mut note = CardNote::new(amount, randomness, context.msg_sender());
storage.test.insert(&mut note, true);
}

unconstrained fn read_note(amount: Field, comparator: u3) -> pub [Option<CardNote>; 10] {
let options = NoteViewerOptions::new().select(0, amount, Option::some(comparator));
let notes = storage.test.view_notes(options);

for i in 0..notes.len() {
if notes[i].is_some() {
debug_log_format(
"NOTES THAT MATCH: {0}",
[notes[i].unwrap_unchecked().points as Field]
);
}
}

notes
}

Expand Down

0 comments on commit e1184ff

Please sign in to comment.