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 440e035
Show file tree
Hide file tree
Showing 7 changed files with 1,080 additions and 26 deletions.
227 changes: 226 additions & 1 deletion noir-projects/noir-contracts/contracts/counter_contract/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use dep::aztec::macros::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 @@ -40,6 +40,56 @@ contract Counter {
let counters = storage.counters;
counters.at(owner).add(1, owner, outgoing_viewer, outgoing_viewer);
}

#[private]
fn increment_two(owner: AztecAddress, outgoing_viewer: AztecAddress) {
unsafe {
dep::aztec::oracle::debug_log::debug_log_format(
"Incrementing counter 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_three(owner: AztecAddress, outgoing_viewer: AztecAddress) {
unsafe {
dep::aztec::oracle::debug_log::debug_log_format(
"Incrementing 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_three(owner: AztecAddress, outgoing_viewer: AztecAddress) {
unsafe {
dep::aztec::oracle::debug_log::debug_log_format(
"Incrementing counter for owner {0}",
[owner.to_field()],
);
}
let counters = storage.counters;
counters.at(owner).sub(1, owner, outgoing_viewer, outgoing_viewer);
}

#[public]
fn public_emission(owner: AztecAddress, outgoing_viewer: AztecAddress) {
unsafe {
dep::aztec::oracle::debug_log::debug_log_format(
"Incrementing counter for owner {0}",
[owner.to_field()],
);
}
context.emit_unencrypted_log(1);
}

// docs:end:increment
// docs:start:get_counter
unconstrained fn get_counter(owner: AztecAddress) -> pub Field {
Expand All @@ -49,10 +99,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 +146,175 @@ contract Counter {
);
}
// docs:end:txe_test_increment

#[test]
unconstrained fn increment_test_abc() {
// Setup env, generate keys
let mut env = TestEnvironment::new();
let owner = env.create_account();
let outgoing_viewer = env.create_account();
let initial_value: Field = 5;
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.advance_block_by(1);

// docs:start:txe_test_read_notes
// Read the stored value in the note
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_inclusion(old_note);
}

#[test]
unconstrained fn inclusion_proofs() {
// Setup env, generate keys
let mut env = TestEnvironment::new();
let owner = env.create_account();
let outgoing_viewer = env.create_account();
let initial_value: Field = 5;
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.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);

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

#[test]
unconstrained fn increment_test() {
// Setup env, generate keys
let mut env = TestEnvironment::new();
let owner = env.create_account();
let outgoing_viewer = env.create_account();
let initial_value: Field = 5;
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.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}",
);

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());

let notes: BoundedVec<ValueNote, MAX_NOTES_PER_PAGE> = view_notes(owner_slot, options);
assert(notes.len() == 3);
assert(get_counter(owner) == 7);

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);

Counter::at(contract_address).increment_three(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);
assert(notes.len() == 4);

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);

Counter::at(contract_address).decrement_three(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);

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);
}

#[test]
unconstrained fn public_emission_test() {
// Setup env, generate keys
let mut env = TestEnvironment::new();
let owner = env.create_account();
let outgoing_viewer = env.create_account();
let initial_value: Field = 5;
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.advance_block_by(1);

Counter::at(contract_address).public_emission(owner, outgoing_viewer).call(&mut env.public());
}
}
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 440e035

Please sign in to comment.