Skip to content

Commit

Permalink
addressing feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
sklppy88 committed Feb 7, 2025
1 parent 3f298d3 commit 955f6b0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 345 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ pub unconstrained fn setup(
let owner = env.create_account(1);
env.impersonate(owner);

// Advance a block so we know that at block 2 our contract has not been deployed yet.
env.advance_block_by(2);
// We advance one block here, because we want to deploy our contract in block 3.
// This is because we will do tests later that prove the non inclusion of values and of the contract itself at block 2.
env.advance_block_by(1);

// Deploy contract and initialize
let initializer = InclusionProofs::interface().constructor(initial_value);
Expand All @@ -30,42 +31,46 @@ pub unconstrained fn setup(
#[test]
unconstrained fn note_flow() {
let (env, contract_address, owner) = setup(INITIAL_VALUE);

env.impersonate(owner);

let block_number = env.block_number();

let NOTE_VALUE = 69;
InclusionProofs::at(contract_address).create_note(owner, NOTE_VALUE).call(&mut env.private());

env.advance_block_by(2);
env.advance_block_by(1);

let note_creation_block_number = env.block_number();

// We advance by another block to make sure that the note creation block number != our current block number
env.advance_block_by(1);

let current_contract_address = get_contract_address();
cheatcodes::set_contract_address(contract_address);

// We fetch the note we created and make sure that it is valid.
let note = InclusionProofs::get_note(owner);
cheatcodes::set_contract_address(current_contract_address);

assert(note.owner.eq(owner));
assert(note.value.eq(NOTE_VALUE));

// Each of these tests (note inclusion, note non-nullification, and validity (inclusion & non-nullification)) check the assertion at the block of creation of note, as well as at the "current" block
InclusionProofs::at(contract_address)
.test_note_inclusion(owner, true, block_number, false)
.test_note_inclusion(owner, true, note_creation_block_number, false)
.call(&mut env.private());
InclusionProofs::at(contract_address).test_note_inclusion(owner, false, 0, false).call(
&mut env.private(),
);

InclusionProofs::at(contract_address)
.test_note_not_nullified(owner, true, block_number, false)
.test_note_not_nullified(owner, true, note_creation_block_number, false)
.call(&mut env.private());
InclusionProofs::at(contract_address).test_note_not_nullified(owner, false, 0, false).call(
&mut env.private(),
);

InclusionProofs::at(contract_address).test_note_validity(owner, true, block_number, false).call(
&mut env.private(),
);
InclusionProofs::at(contract_address)
.test_note_validity(owner, true, note_creation_block_number, false)
.call(&mut env.private());
InclusionProofs::at(contract_address).test_note_validity(owner, false, 0, false).call(
&mut env.private(),
);
Expand All @@ -74,19 +79,19 @@ unconstrained fn note_flow() {
#[test]
unconstrained fn nullify_note_flow() {
let (env, contract_address, owner) = setup(INITIAL_VALUE);

env.impersonate(owner);

let note_valid_block_number = env.block_number();

InclusionProofs::at(contract_address).create_note(owner, 5).call(&mut env.private());

env.advance_block_by(1);

let note_valid_block_number = env.block_number();

InclusionProofs::at(contract_address).nullify_note(owner).call(&mut env.private());

env.advance_block_by(1);

// We test note inclusion at the note creation block and at current block
InclusionProofs::at(contract_address)
.test_note_inclusion(owner, true, note_valid_block_number, true)
.call(&mut env.private());
Expand All @@ -95,6 +100,7 @@ unconstrained fn nullify_note_flow() {
&mut env.private(),
);

// We test note non-nullification and validity at the note creation block
InclusionProofs::at(contract_address)
.test_note_not_nullified(owner, true, note_valid_block_number, true)
.call(&mut env.private());
Expand All @@ -106,7 +112,6 @@ unconstrained fn nullify_note_flow() {
#[test(should_fail_with = "Assertion failed: Proving nullifier non-inclusion failed: low_nullifier.value < nullifier.value check failed")]
unconstrained fn note_not_nullified_after_nullified() {
let (env, contract_address, owner) = setup(INITIAL_VALUE);

env.impersonate(owner);

InclusionProofs::at(contract_address).create_note(owner, 5).call(&mut env.private());
Expand All @@ -117,17 +122,14 @@ unconstrained fn note_not_nullified_after_nullified() {

env.advance_block_by(1);

// TODO: env.block_number() should actually return the private context inputs block number, not the block that is currently being built !
// Change env.block_number to subtract one, then just use `env.block_number()`.
InclusionProofs::at(contract_address)
.test_note_not_nullified(owner, true, env.block_number() - 1, true)
.test_note_not_nullified(owner, true, env.block_number(), true)
.call(&mut env.private());
}

#[test(should_fail_with = "Assertion failed: Proving nullifier non-inclusion failed: low_nullifier.value < nullifier.value check failed")]
unconstrained fn note_not_nullified_after_nullified_no_block_number() {
let (env, contract_address, owner) = setup(INITIAL_VALUE);

env.impersonate(owner);

InclusionProofs::at(contract_address).create_note(owner, 5).call(&mut env.private());
Expand All @@ -146,7 +148,6 @@ unconstrained fn note_not_nullified_after_nullified_no_block_number() {
#[test(should_fail_with = "Assertion failed: Proving nullifier non-inclusion failed: low_nullifier.value < nullifier.value check failed")]
unconstrained fn validity_after_nullified() {
let (env, contract_address, owner) = setup(INITIAL_VALUE);

env.impersonate(owner);

InclusionProofs::at(contract_address).create_note(owner, 5).call(&mut env.private());
Expand All @@ -158,14 +159,13 @@ unconstrained fn validity_after_nullified() {
env.advance_block_by(1);

InclusionProofs::at(contract_address)
.test_note_validity(owner, true, env.block_number() - 1, true)
.test_note_validity(owner, true, env.block_number(), true)
.call(&mut env.private());
}

#[test(should_fail_with = "Assertion failed: Proving nullifier non-inclusion failed: low_nullifier.value < nullifier.value check failed")]
unconstrained fn validity_after_nullified_no_block_number() {
let (env, contract_address, owner) = setup(INITIAL_VALUE);

env.impersonate(owner);

InclusionProofs::at(contract_address).create_note(owner, 5).call(&mut env.private());
Expand All @@ -184,8 +184,8 @@ unconstrained fn validity_after_nullified_no_block_number() {
#[test(should_fail_with = "not found in NOTE_HASH_TREE")]
unconstrained fn note_inclusion_fail_case() {
let (env, contract_address, owner) = setup(INITIAL_VALUE);

env.impersonate(owner);

let random_owner = AztecAddress::from_field(dep::aztec::oracle::random::random());

let block_number = env.block_number();
Expand All @@ -200,8 +200,8 @@ unconstrained fn note_inclusion_fail_case() {
#[test(should_fail_with = "not found in NOTE_HASH_TREE")]
unconstrained fn note_inclusion_fail_case_no_block_number() {
let (env, contract_address, owner) = setup(INITIAL_VALUE);

env.impersonate(owner);

let random_owner = AztecAddress::from_field(dep::aztec::oracle::random::random());

env.advance_block_by(1);
Expand Down Expand Up @@ -231,7 +231,6 @@ unconstrained fn nullifier_inclusion() {
#[test]
unconstrained fn nullifier_inclusion_public() {
let (env, contract_address, owner) = setup(INITIAL_VALUE);

env.impersonate(owner);

let unsiloed_nullifier = 0xffffff;
Expand All @@ -248,9 +247,10 @@ unconstrained fn nullifier_inclusion_public() {
#[test(should_fail_with = "Nullifier witness not found for nullifier")]
unconstrained fn nullifier_non_existence() {
let (env, contract_address, owner) = setup(INITIAL_VALUE);
env.impersonate(owner);

let block_number = env.block_number() - 1;
env.impersonate(owner);

let random_nullifier = dep::aztec::oracle::random::random();

InclusionProofs::at(contract_address)
Expand All @@ -261,8 +261,8 @@ unconstrained fn nullifier_non_existence() {
#[test(should_fail_with = "Nullifier witness not found for nullifier")]
unconstrained fn nullifier_non_existence_no_block_number() {
let (env, contract_address, owner) = setup(INITIAL_VALUE);

env.impersonate(owner);

let random_nullifier = dep::aztec::oracle::random::random();

InclusionProofs::at(contract_address).test_nullifier_inclusion(random_nullifier, false, 0).call(
Expand All @@ -273,10 +273,9 @@ unconstrained fn nullifier_non_existence_no_block_number() {
#[test]
unconstrained fn historical_reads() {
let (env, contract_address, owner) = setup(INITIAL_VALUE);

env.impersonate(owner);

let block_number = env.block_number() - 1;
let block_number = env.block_number();

InclusionProofs::at(contract_address)
.test_storage_historical_read(INITIAL_VALUE, true, block_number)
Expand All @@ -298,10 +297,9 @@ unconstrained fn historical_reads() {
#[test]
unconstrained fn contract_flow() {
let (env, contract_address, owner) = setup(INITIAL_VALUE);

env.impersonate(owner);

let block_number = env.block_number() - 1;
let block_number = env.block_number();

InclusionProofs::at(contract_address)
.test_contract_inclusion(contract_address, block_number, true, true)
Expand All @@ -316,7 +314,6 @@ unconstrained fn contract_flow() {
#[test(should_fail_with = "Nullifier witness not found for nullifier")]
unconstrained fn test_contract_not_initialized() {
let (env, contract_address, owner) = setup(INITIAL_VALUE);

env.impersonate(owner);

// We are using block number 2 because we know the contract has not been deployed nor initialized at this point.
Expand All @@ -328,11 +325,32 @@ unconstrained fn test_contract_not_initialized() {
#[test(should_fail_with = "Nullifier witness not found for nullifier")]
unconstrained fn test_contract_not_deployed() {
let (env, contract_address, owner) = setup(INITIAL_VALUE);

env.impersonate(owner);

// We are using block number 2 because we know the contract has not been deployed nor initialized at this point.
InclusionProofs::at(contract_address)
.test_contract_inclusion(contract_address, 2, false, true)
.call(&mut env.private());
}

#[test]
unconstrained fn test_deployed_contract_not_deployed() {
let (env, contract_address, owner) = setup(INITIAL_VALUE);
env.impersonate(owner);

// We are using block number 2 because we know the contract has been deployed nor initialized at this point.
InclusionProofs::at(contract_address)
.test_contract_non_inclusion(contract_address, 2, true, false)
.call(&mut env.private());
}

#[test]
unconstrained fn test_deployed_contract_not_initialized() {
let (env, contract_address, owner) = setup(INITIAL_VALUE);
env.impersonate(owner);

// We are using block number 2 because we know the contract has been deployed nor initialized at this point.
InclusionProofs::at(contract_address)
.test_contract_non_inclusion(contract_address, 2, false, true)
.call(&mut env.private());
}
Loading

0 comments on commit 955f6b0

Please sign in to comment.