Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
sklppy88 committed Dec 8, 2024
1 parent 621cbaf commit d9c7276
Show file tree
Hide file tree
Showing 8 changed files with 1,014 additions and 26 deletions.
149 changes: 148 additions & 1 deletion noir-projects/noir-contracts/contracts/counter_contract/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// docs:start:setup
use dep::aztec::macros::aztec;
use test::utils;

#[aztec]
contract Counter {
// docs:end:setup
// docs:start:imports
use aztec::macros::{functions::{initializer, private}, storage::storage};
use aztec::macros::{functions::{initializer, private, public}, storage::storage};
use aztec::prelude::{AztecAddress, Map};
use easy_private_state::EasyPrivateUint;
use value_note::{balance_utils, value_note::ValueNote};
Expand Down Expand Up @@ -41,6 +42,45 @@ contract Counter {
counters.at(owner).add(1, owner, outgoing_viewer, outgoing_viewer);
}
// docs:end:increment

#[private]
fn increment_twice(owner: AztecAddress, outgoing_viewer: AztecAddress) {
unsafe {
dep::aztec::oracle::debug_log::debug_log_format(
"Incrementing counter twice for owner {0}",
[owner.to_field()],
);
}
let counters = storage.counters;
counters.at(owner).add(1, owner, outgoing_viewer, outgoing_viewer);
counters.at(owner).add(1, owner, outgoing_viewer, outgoing_viewer);
}

#[private]
fn increment_and_decrement(owner: AztecAddress, outgoing_viewer: AztecAddress) {
unsafe {
dep::aztec::oracle::debug_log::debug_log_format(
"Incrementing and decrementing counter for owner {0}",
[owner.to_field()],
);
}
let counters = storage.counters;
counters.at(owner).add(1, owner, outgoing_viewer, outgoing_viewer);
counters.at(owner).sub(1, owner, outgoing_viewer, outgoing_viewer);
}

#[private]
fn decrement(owner: AztecAddress, outgoing_viewer: AztecAddress) {
unsafe {
dep::aztec::oracle::debug_log::debug_log_format(
"Decrementing counter for owner {0}",
[owner.to_field()],
);
}
let counters = storage.counters;
counters.at(owner).sub(1, owner, outgoing_viewer, outgoing_viewer);
}

// docs:start:get_counter
unconstrained fn get_counter(owner: AztecAddress) -> pub Field {
let counters = storage.counters;
Expand All @@ -49,10 +89,14 @@ contract Counter {

// docs:end:get_counter
// docs:start:test_imports
use dep::aztec::note::lifecycle::destroy_note;
use dep::aztec::note::note_getter::{MAX_NOTES_PER_PAGE, view_notes};
use dep::aztec::note::note_viewer_options::NoteViewerOptions;

use dep::aztec::protocol_types::debug_log::debug_log_format;
use dep::aztec::protocol_types::storage::map::derive_storage_slot_in_map;
use dep::aztec::test::helpers::{cheatcodes, test_environment::TestEnvironment};

// docs:end:test_imports
// docs:start:txe_test_increment
#[test]
Expand Down Expand Up @@ -92,4 +136,107 @@ contract Counter {
);
}
// docs:end:txe_test_increment

#[test]
unconstrained fn inclusion_proofs() {
let (env, contract_address, owner, outgoing_viewer) = utils::setup(5);
env.advance_block_by(1);

env.impersonate(contract_address);
let counter_slot = Counter::storage_layout().counters.slot;
let owner_slot = derive_storage_slot_in_map(counter_slot, owner);
let mut options = NoteViewerOptions::new();
let notes: BoundedVec<ValueNote, MAX_NOTES_PER_PAGE> = view_notes(owner_slot, options);
let initial_note_value = notes.get(0).value;
assert(
initial_note_value == initial_value,
f"Expected {initial_value} but got {initial_note_value}",
);

let old_note = notes.get(0);

env.private().get_header().prove_note_validity(old_note, &mut env.private());

destroy_note(&mut env.private(), old_note);
env.advance_block_by(1);

env.private().get_header().prove_note_is_nullified(old_note, &mut env.private());
env.private().get_header().prove_note_inclusion(old_note);

let notes: BoundedVec<ValueNote, MAX_NOTES_PER_PAGE> = view_notes(owner_slot, options);

assert(notes.len() == 0);
}

#[test]
unconstrained fn extended_incrementing_and_decrementing() {
let (env, contract_address, owner, outgoing_viewer) = utils::setup(5);

// Checking from the note cache
env.impersonate(contract_address);
let counter_slot = Counter::storage_layout().counters.slot;
let owner_slot = derive_storage_slot_in_map(counter_slot, owner);
let mut options = NoteViewerOptions::new();
let notes: BoundedVec<ValueNote, MAX_NOTES_PER_PAGE> = view_notes(owner_slot, options);
let initial_note_value = notes.get(0).value;
assert(
initial_note_value == initial_value,
f"Expected {initial_value} but got {initial_note_value}",
);

// Checking that the note was discovered from private logs
env.advance_block_by(1);
env.impersonate(contract_address);
let counter_slot = Counter::storage_layout().counters.slot;
let owner_slot = derive_storage_slot_in_map(counter_slot, owner);
let mut options = NoteViewerOptions::new();
let notes: BoundedVec<ValueNote, MAX_NOTES_PER_PAGE> = view_notes(owner_slot, options);
let initial_note_value = notes.get(0).value;
assert(
initial_note_value == initial_value,
f"Expected {initial_value} but got {initial_note_value}",
);

Counter::at(contract_address).increment_two(owner, outgoing_viewer).call(&mut env.private());

// Checking from the note cache
let notes: BoundedVec<ValueNote, MAX_NOTES_PER_PAGE> = view_notes(owner_slot, options);
assert(notes.len() == 3);
assert(get_counter(owner) == 7);

// Checking that the note was discovered from private logs
env.advance_block_by(1);
let notes: BoundedVec<ValueNote, MAX_NOTES_PER_PAGE> = view_notes(owner_slot, options);
assert(get_counter(owner) == 7);
assert(notes.len() == 3);

// Checking from the note cache
Counter::at(contract_address).increment_and_decrement(owner, outgoing_viewer).call(
&mut env.private(),
);
let notes: BoundedVec<ValueNote, MAX_NOTES_PER_PAGE> = view_notes(owner_slot, options);
assert(get_counter(owner) == 7);
// We have a change note of 0
assert(notes.len() == 4);

// Checking that the note was discovered from private logs
env.advance_block_by(1);
let notes: BoundedVec<ValueNote, MAX_NOTES_PER_PAGE> = view_notes(owner_slot, options);
assert(notes.len() == 4);
assert(get_counter(owner) == 7);

// Checking from the note cache, note that we MUST advance the block to get a correct and updated value.
Counter::at(contract_address).decrement(owner, outgoing_viewer).call(
&mut env.private(),
);
let notes: BoundedVec<ValueNote, MAX_NOTES_PER_PAGE> = view_notes(owner_slot, options);
assert(get_counter(owner) == 11);
assert(notes.len() == 5);

// We advance the block here to have the nullification of the prior note be applied. Should we try nullifiying notes in our DB on notifyNullifiedNote ?
env.advance_block_by(1);
let notes: BoundedVec<ValueNote, MAX_NOTES_PER_PAGE> = view_notes(owner_slot, options);
assert(get_counter(owner) == 6);
assert(notes.len() == 4);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use dep::aztec::{prelude::AztecAddress, test::helpers::test_environment::TestEnvironment};

pub unconstrained fn setup(initial_value: Field) -> (&mut TestEnvironment, AztecAddress, AztecAddress, AztecAddress) {
// Setup env, generate keys
let mut env = TestEnvironment::new();
let owner = env.create_account();
let outgoing_viewer = env.create_account();
env.impersonate(owner);
// Deploy contract and initialize
let initializer =
Counter::interface().initialize(initial_value as u64, owner, outgoing_viewer);
let counter_contract = env.deploy_self("Counter").with_private_initializer(initializer);
let contract_address = counter_contract.to_address();
(env, contract_address, owner, outgoing_viewer)
}
1 change: 1 addition & 0 deletions yarn-project/pxe/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export * from '@aztec/key-store';
export * from './database/index.js';
export { ContractDataOracle } from './contract_data_oracle/index.js';
export { PrivateFunctionsTree } from './contract_data_oracle/private_functions_tree.js';
export { SimulatorOracle } from './simulator_oracle/index.js';
14 changes: 12 additions & 2 deletions yarn-project/simulator/src/client/execution_note_cache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { computeNoteHashNonce, computeUniqueNoteHash, siloNoteHash, siloNullifier } from '@aztec/circuits.js/hash';
import { type AztecAddress } from '@aztec/foundation/aztec-address';
import { type Fr } from '@aztec/foundation/fields';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { Fr } from '@aztec/foundation/fields';

import { type NoteData } from '../acvm/index.js';

Expand Down Expand Up @@ -146,4 +146,14 @@ export class ExecutionNoteCache {
notes.push(note);
this.noteMap.set(note.note.contractAddress.toBigInt(), notes);
}

getAllNotes(): PendingNote[] {
return this.notes;
}

getAllNullifiers(): Fr[] {
return [...this.nullifierMap.values()].flatMap(nullifierArray =>
[...nullifierArray.values()].map(val => new Fr(val)),
);
}
}
Loading

0 comments on commit d9c7276

Please sign in to comment.