Skip to content

Commit 85902d8

Browse files
author
sklppy88
committed
addressing feedback
1 parent 8e21cdf commit 85902d8

File tree

2 files changed

+64
-348
lines changed

2 files changed

+64
-348
lines changed

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

+64-36
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ 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.
19-
env.advance_block_by(2);
18+
// We advance one block here, because we want to deploy our contract in 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.
20+
env.advance_block_by(1);
2021

2122
// Deploy contract and initialize
2223
let initializer = InclusionProofs::interface().constructor(initial_value);
@@ -29,64 +30,72 @@ pub unconstrained fn setup(
2930

3031
#[test]
3132
unconstrained fn note_flow() {
33+
// This test creates a note and checks certain properties about it, namely its inclusion in the trees,
34+
// the non-inclusion of its nullifier, and its validity (the combination of the previous two)
3235
let (env, contract_address, owner) = setup(INITIAL_VALUE);
33-
3436
env.impersonate(owner);
3537

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

41-
env.advance_block_by(2);
41+
env.advance_block_by(1);
42+
43+
let note_creation_block_number = env.block_number();
44+
45+
// We advance by another block to make sure that the note creation block number != our current block number
46+
env.advance_block_by(1);
4247

4348
let current_contract_address = get_contract_address();
4449
cheatcodes::set_contract_address(contract_address);
4550

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

4955
assert(note.owner.eq(owner));
5056
assert(note.value.eq(NOTE_VALUE));
5157

58+
// 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
5259
InclusionProofs::at(contract_address)
53-
.test_note_inclusion(owner, true, block_number, false)
60+
.test_note_inclusion(owner, true, note_creation_block_number, false)
5461
.call(&mut env.private());
5562
InclusionProofs::at(contract_address).test_note_inclusion(owner, false, 0, false).call(
5663
&mut env.private(),
5764
);
5865

5966
InclusionProofs::at(contract_address)
60-
.test_note_not_nullified(owner, true, block_number, false)
67+
.test_note_not_nullified(owner, true, note_creation_block_number, false)
6168
.call(&mut env.private());
6269
InclusionProofs::at(contract_address).test_note_not_nullified(owner, false, 0, false).call(
6370
&mut env.private(),
6471
);
6572

66-
InclusionProofs::at(contract_address).test_note_validity(owner, true, block_number, false).call(
67-
&mut env.private(),
68-
);
73+
InclusionProofs::at(contract_address)
74+
.test_note_validity(owner, true, note_creation_block_number, false)
75+
.call(&mut env.private());
6976
InclusionProofs::at(contract_address).test_note_validity(owner, false, 0, false).call(
7077
&mut env.private(),
7178
);
7279
}
7380

7481
#[test]
7582
unconstrained fn nullify_note_flow() {
83+
// This test creates a note, nullifies it, and checks certain properties about it, namely its inclusion in the trees,
84+
// the non-inclusion of its nullifier, and its validity (the combination of the previous two). These properties are checked before nullification.
7685
let (env, contract_address, owner) = setup(INITIAL_VALUE);
77-
7886
env.impersonate(owner);
7987

80-
let note_valid_block_number = env.block_number();
81-
8288
InclusionProofs::at(contract_address).create_note(owner, 5).call(&mut env.private());
8389

8490
env.advance_block_by(1);
8591

92+
let note_valid_block_number = env.block_number();
93+
8694
InclusionProofs::at(contract_address).nullify_note(owner).call(&mut env.private());
8795

8896
env.advance_block_by(1);
8997

98+
// We test note inclusion at the note creation block and at current block
9099
InclusionProofs::at(contract_address)
91100
.test_note_inclusion(owner, true, note_valid_block_number, true)
92101
.call(&mut env.private());
@@ -95,6 +104,7 @@ unconstrained fn nullify_note_flow() {
95104
&mut env.private(),
96105
);
97106

107+
// We test note non-nullification and validity at the note creation block
98108
InclusionProofs::at(contract_address)
99109
.test_note_not_nullified(owner, true, note_valid_block_number, true)
100110
.call(&mut env.private());
@@ -106,7 +116,6 @@ unconstrained fn nullify_note_flow() {
106116
#[test(should_fail_with = "Assertion failed: Proving nullifier non-inclusion failed: low_nullifier.value < nullifier.value check failed")]
107117
unconstrained fn note_not_nullified_after_nullified() {
108118
let (env, contract_address, owner) = setup(INITIAL_VALUE);
109-
110119
env.impersonate(owner);
111120

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

118127
env.advance_block_by(1);
119128

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

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

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

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

160165
InclusionProofs::at(contract_address)
161-
.test_note_validity(owner, true, env.block_number() - 1, true)
166+
.test_note_validity(owner, true, env.block_number(), true)
162167
.call(&mut env.private());
163168
}
164169

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

171175
InclusionProofs::at(contract_address).create_note(owner, 5).call(&mut env.private());
@@ -184,8 +188,8 @@ unconstrained fn validity_after_nullified_no_block_number() {
184188
#[test(should_fail_with = "not found in NOTE_HASH_TREE")]
185189
unconstrained fn note_inclusion_fail_case() {
186190
let (env, contract_address, owner) = setup(INITIAL_VALUE);
187-
188191
env.impersonate(owner);
192+
189193
let random_owner = AztecAddress::from_field(dep::aztec::oracle::random::random());
190194

191195
let block_number = env.block_number();
@@ -200,8 +204,8 @@ unconstrained fn note_inclusion_fail_case() {
200204
#[test(should_fail_with = "not found in NOTE_HASH_TREE")]
201205
unconstrained fn note_inclusion_fail_case_no_block_number() {
202206
let (env, contract_address, owner) = setup(INITIAL_VALUE);
203-
204207
env.impersonate(owner);
208+
205209
let random_owner = AztecAddress::from_field(dep::aztec::oracle::random::random());
206210

207211
env.advance_block_by(1);
@@ -231,7 +235,6 @@ unconstrained fn nullifier_inclusion() {
231235
#[test]
232236
unconstrained fn nullifier_inclusion_public() {
233237
let (env, contract_address, owner) = setup(INITIAL_VALUE);
234-
235238
env.impersonate(owner);
236239

237240
let unsiloed_nullifier = 0xffffff;
@@ -248,9 +251,10 @@ unconstrained fn nullifier_inclusion_public() {
248251
#[test(should_fail_with = "Nullifier witness not found for nullifier")]
249252
unconstrained fn nullifier_non_existence() {
250253
let (env, contract_address, owner) = setup(INITIAL_VALUE);
254+
env.impersonate(owner);
251255

252256
let block_number = env.block_number() - 1;
253-
env.impersonate(owner);
257+
254258
let random_nullifier = dep::aztec::oracle::random::random();
255259

256260
InclusionProofs::at(contract_address)
@@ -261,8 +265,8 @@ unconstrained fn nullifier_non_existence() {
261265
#[test(should_fail_with = "Nullifier witness not found for nullifier")]
262266
unconstrained fn nullifier_non_existence_no_block_number() {
263267
let (env, contract_address, owner) = setup(INITIAL_VALUE);
264-
265268
env.impersonate(owner);
269+
266270
let random_nullifier = dep::aztec::oracle::random::random();
267271

268272
InclusionProofs::at(contract_address).test_nullifier_inclusion(random_nullifier, false, 0).call(
@@ -273,10 +277,9 @@ unconstrained fn nullifier_non_existence_no_block_number() {
273277
#[test]
274278
unconstrained fn historical_reads() {
275279
let (env, contract_address, owner) = setup(INITIAL_VALUE);
276-
277280
env.impersonate(owner);
278281

279-
let block_number = env.block_number() - 1;
282+
let block_number = env.block_number();
280283

281284
InclusionProofs::at(contract_address)
282285
.test_storage_historical_read(INITIAL_VALUE, true, block_number)
@@ -297,11 +300,11 @@ unconstrained fn historical_reads() {
297300

298301
#[test]
299302
unconstrained fn contract_flow() {
303+
// This test deploys a contract and tests for its inclusion of deployment and initialization nullifier. It also checks non-inclusion before its deployment.
300304
let (env, contract_address, owner) = setup(INITIAL_VALUE);
301-
302305
env.impersonate(owner);
303306

304-
let block_number = env.block_number() - 1;
307+
let block_number: u32 = env.block_number();
305308

306309
InclusionProofs::at(contract_address)
307310
.test_contract_inclusion(contract_address, block_number, true, true)
@@ -314,25 +317,50 @@ unconstrained fn contract_flow() {
314317
}
315318

316319
#[test(should_fail_with = "Nullifier witness not found for nullifier")]
317-
unconstrained fn test_contract_not_initialized() {
320+
unconstrained fn contract_not_initialized() {
318321
let (env, contract_address, owner) = setup(INITIAL_VALUE);
319-
320322
env.impersonate(owner);
321323

322324
// We are using block number 2 because we know the contract has not been deployed nor initialized at this point.
325+
// We split the deployment check and the initialization check (true, false), because each one checks for the inclusion of the deployment / initialization nullifier specifically;
326+
// We can't set `true, true` to check for both in one test because the first failing condition won't let us check the other in isolation.
327+
// The above statements apply to the rest of the contract inclusion tests.
323328
InclusionProofs::at(contract_address)
324329
.test_contract_inclusion(contract_address, 2, true, false)
325330
.call(&mut env.private());
326331
}
327332

328333
#[test(should_fail_with = "Nullifier witness not found for nullifier")]
329-
unconstrained fn test_contract_not_deployed() {
334+
unconstrained fn contract_not_deployed() {
330335
let (env, contract_address, owner) = setup(INITIAL_VALUE);
331-
332336
env.impersonate(owner);
333337

334-
// We are using block number 2 because we know the contract has not been deployed nor initialized at this point.
338+
// This checks for the inclusion of the initializer nullifier specifically.
335339
InclusionProofs::at(contract_address)
336340
.test_contract_inclusion(contract_address, 2, false, true)
337341
.call(&mut env.private());
338342
}
343+
344+
#[test(should_fail_with = "Assertion failed: Proving nullifier non-inclusion failed")]
345+
unconstrained fn contract_deployed_non_inclusion() {
346+
let (env, contract_address, owner) = setup(INITIAL_VALUE);
347+
env.impersonate(owner);
348+
349+
let block_number: u32 = env.block_number();
350+
351+
InclusionProofs::at(contract_address)
352+
.test_contract_non_inclusion(contract_address, block_number, true, false)
353+
.call(&mut env.private());
354+
}
355+
356+
#[test(should_fail_with = "Assertion failed: Proving nullifier non-inclusion failed")]
357+
unconstrained fn contract_initialized_non_inclusion() {
358+
let (env, contract_address, owner) = setup(INITIAL_VALUE);
359+
env.impersonate(owner);
360+
361+
let block_number: u32 = env.block_number();
362+
363+
InclusionProofs::at(contract_address)
364+
.test_contract_non_inclusion(contract_address, block_number, false, true)
365+
.call(&mut env.private());
366+
}

0 commit comments

Comments
 (0)