Skip to content

Commit 353748e

Browse files
author
sklppy88
committed
addressing feedback
1 parent fce71db commit 353748e

File tree

1 file changed

+43
-22
lines changed
  • noir-projects/noir-contracts/contracts/inclusion_proofs_contract/src

1 file changed

+43
-22
lines changed

noir-projects/noir-contracts/contracts/inclusion_proofs_contract/src/test.nr

+43-22
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ pub unconstrained fn setup(
1515
let owner = env.create_account(1);
1616
env.impersonate(owner);
1717

18-
// Advance a block so we know that at block 2 our contract has not been deployed yet.
18+
// We need to advance two blocks here, because we want to deploy our contract at block 3.
19+
// This is because we will do tests later that prove the non inclusion of values and of the contract itself at block 2.
1920
env.advance_block_by(2);
2021

2122
// Deploy contract and initialize
@@ -30,42 +31,47 @@ pub unconstrained fn setup(
3031
#[test]
3132
unconstrained fn note_flow() {
3233
let (env, contract_address, owner) = setup(INITIAL_VALUE);
33-
3434
env.impersonate(owner);
3535

36-
let block_number = env.block_number();
36+
let note_creation_block_number = env.block_number();
3737

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

41+
// We have to advance two blocks here because if we only advanced it by one, the current block would be 4, and the note creation block would be 3.
42+
// But when we do not use the block_number, env.private would get the last block header, which is still 3. Fix this !
43+
// TODO: env.block_number() should actually return the private context inputs block number, not the block that is currently being built !
44+
// Change env.block_number to subtract one, then just use `env.block_number()`.
4145
env.advance_block_by(2);
4246

4347
let current_contract_address = get_contract_address();
4448
cheatcodes::set_contract_address(contract_address);
4549

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

4954
assert(note.owner.eq(owner));
5055
assert(note.value.eq(NOTE_VALUE));
5156

57+
// 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
5258
InclusionProofs::at(contract_address)
53-
.test_note_inclusion(owner, true, block_number, false)
59+
.test_note_inclusion(owner, true, note_creation_block_number, false)
5460
.call(&mut env.private());
5561
InclusionProofs::at(contract_address).test_note_inclusion(owner, false, 0, false).call(
5662
&mut env.private(),
5763
);
5864

5965
InclusionProofs::at(contract_address)
60-
.test_note_not_nullified(owner, true, block_number, false)
66+
.test_note_not_nullified(owner, true, note_creation_block_number, false)
6167
.call(&mut env.private());
6268
InclusionProofs::at(contract_address).test_note_not_nullified(owner, false, 0, false).call(
6369
&mut env.private(),
6470
);
6571

66-
InclusionProofs::at(contract_address).test_note_validity(owner, true, block_number, false).call(
67-
&mut env.private(),
68-
);
72+
InclusionProofs::at(contract_address)
73+
.test_note_validity(owner, true, note_creation_block_number, false)
74+
.call(&mut env.private());
6975
InclusionProofs::at(contract_address).test_note_validity(owner, false, 0, false).call(
7076
&mut env.private(),
7177
);
@@ -74,7 +80,6 @@ unconstrained fn note_flow() {
7480
#[test]
7581
unconstrained fn nullify_note_flow() {
7682
let (env, contract_address, owner) = setup(INITIAL_VALUE);
77-
7883
env.impersonate(owner);
7984

8085
let note_valid_block_number = env.block_number();
@@ -87,6 +92,7 @@ unconstrained fn nullify_note_flow() {
8792

8893
env.advance_block_by(1);
8994

95+
// We test note inclusion at the note creation block and at current block
9096
InclusionProofs::at(contract_address)
9197
.test_note_inclusion(owner, true, note_valid_block_number, true)
9298
.call(&mut env.private());
@@ -95,6 +101,7 @@ unconstrained fn nullify_note_flow() {
95101
&mut env.private(),
96102
);
97103

104+
// We test note non-nullification and validity at the note creation block
98105
InclusionProofs::at(contract_address)
99106
.test_note_not_nullified(owner, true, note_valid_block_number, true)
100107
.call(&mut env.private());
@@ -106,7 +113,6 @@ unconstrained fn nullify_note_flow() {
106113
#[test(should_fail_with = "Assertion failed: Proving nullifier non-inclusion failed: low_nullifier.value < nullifier.value check failed")]
107114
unconstrained fn note_not_nullified_after_nullified() {
108115
let (env, contract_address, owner) = setup(INITIAL_VALUE);
109-
110116
env.impersonate(owner);
111117

112118
InclusionProofs::at(contract_address).create_note(owner, 5).call(&mut env.private());
@@ -127,7 +133,6 @@ unconstrained fn note_not_nullified_after_nullified() {
127133
#[test(should_fail_with = "Assertion failed: Proving nullifier non-inclusion failed: low_nullifier.value < nullifier.value check failed")]
128134
unconstrained fn note_not_nullified_after_nullified_no_block_number() {
129135
let (env, contract_address, owner) = setup(INITIAL_VALUE);
130-
131136
env.impersonate(owner);
132137

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

152156
InclusionProofs::at(contract_address).create_note(owner, 5).call(&mut env.private());
@@ -165,7 +169,6 @@ unconstrained fn validity_after_nullified() {
165169
#[test(should_fail_with = "Assertion failed: Proving nullifier non-inclusion failed: low_nullifier.value < nullifier.value check failed")]
166170
unconstrained fn validity_after_nullified_no_block_number() {
167171
let (env, contract_address, owner) = setup(INITIAL_VALUE);
168-
169172
env.impersonate(owner);
170173

171174
InclusionProofs::at(contract_address).create_note(owner, 5).call(&mut env.private());
@@ -184,8 +187,8 @@ unconstrained fn validity_after_nullified_no_block_number() {
184187
#[test(should_fail_with = "not found in NOTE_HASH_TREE")]
185188
unconstrained fn note_inclusion_fail_case() {
186189
let (env, contract_address, owner) = setup(INITIAL_VALUE);
187-
188190
env.impersonate(owner);
191+
189192
let random_owner = AztecAddress::from_field(dep::aztec::oracle::random::random());
190193

191194
let block_number = env.block_number();
@@ -200,8 +203,8 @@ unconstrained fn note_inclusion_fail_case() {
200203
#[test(should_fail_with = "not found in NOTE_HASH_TREE")]
201204
unconstrained fn note_inclusion_fail_case_no_block_number() {
202205
let (env, contract_address, owner) = setup(INITIAL_VALUE);
203-
204206
env.impersonate(owner);
207+
205208
let random_owner = AztecAddress::from_field(dep::aztec::oracle::random::random());
206209

207210
env.advance_block_by(1);
@@ -231,7 +234,6 @@ unconstrained fn nullifier_inclusion() {
231234
#[test]
232235
unconstrained fn nullifier_inclusion_public() {
233236
let (env, contract_address, owner) = setup(INITIAL_VALUE);
234-
235237
env.impersonate(owner);
236238

237239
let unsiloed_nullifier = 0xffffff;
@@ -248,9 +250,10 @@ unconstrained fn nullifier_inclusion_public() {
248250
#[test(should_fail_with = "Nullifier witness not found for nullifier")]
249251
unconstrained fn nullifier_non_existence() {
250252
let (env, contract_address, owner) = setup(INITIAL_VALUE);
253+
env.impersonate(owner);
251254

252255
let block_number = env.block_number() - 1;
253-
env.impersonate(owner);
256+
254257
let random_nullifier = dep::aztec::oracle::random::random();
255258

256259
InclusionProofs::at(contract_address)
@@ -261,8 +264,8 @@ unconstrained fn nullifier_non_existence() {
261264
#[test(should_fail_with = "Nullifier witness not found for nullifier")]
262265
unconstrained fn nullifier_non_existence_no_block_number() {
263266
let (env, contract_address, owner) = setup(INITIAL_VALUE);
264-
265267
env.impersonate(owner);
268+
266269
let random_nullifier = dep::aztec::oracle::random::random();
267270

268271
InclusionProofs::at(contract_address).test_nullifier_inclusion(random_nullifier, false, 0).call(
@@ -273,7 +276,6 @@ unconstrained fn nullifier_non_existence_no_block_number() {
273276
#[test]
274277
unconstrained fn historical_reads() {
275278
let (env, contract_address, owner) = setup(INITIAL_VALUE);
276-
277279
env.impersonate(owner);
278280

279281
let block_number = env.block_number() - 1;
@@ -298,7 +300,6 @@ unconstrained fn historical_reads() {
298300
#[test]
299301
unconstrained fn contract_flow() {
300302
let (env, contract_address, owner) = setup(INITIAL_VALUE);
301-
302303
env.impersonate(owner);
303304

304305
let block_number = env.block_number() - 1;
@@ -316,7 +317,6 @@ unconstrained fn contract_flow() {
316317
#[test(should_fail_with = "Nullifier witness not found for nullifier")]
317318
unconstrained fn test_contract_not_initialized() {
318319
let (env, contract_address, owner) = setup(INITIAL_VALUE);
319-
320320
env.impersonate(owner);
321321

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

334333
// We are using block number 2 because we know the contract has not been deployed nor initialized at this point.
335334
InclusionProofs::at(contract_address)
336335
.test_contract_inclusion(contract_address, 2, false, true)
337336
.call(&mut env.private());
338337
}
338+
339+
#[test]
340+
unconstrained fn test_deployed_contract_not_deployed() {
341+
let (env, contract_address, owner) = setup(INITIAL_VALUE);
342+
env.impersonate(owner);
343+
344+
// We are using block number 2 because we know the contract has been deployed nor initialized at this point.
345+
InclusionProofs::at(contract_address)
346+
.test_contract_non_inclusion(contract_address, 3, true, false)
347+
.call(&mut env.private());
348+
}
349+
350+
#[test]
351+
unconstrained fn test_deployed_contract_not_initialized() {
352+
let (env, contract_address, owner) = setup(INITIAL_VALUE);
353+
env.impersonate(owner);
354+
355+
// We are using block number 2 because we know the contract has been deployed nor initialized at this point.
356+
InclusionProofs::at(contract_address)
357+
.test_contract_non_inclusion(contract_address, 3, false, true)
358+
.call(&mut env.private());
359+
}

0 commit comments

Comments
 (0)