-
Notifications
You must be signed in to change notification settings - Fork 327
/
Copy pathe2e_pending_note_hashes_contract.test.ts
312 lines (259 loc) · 11.9 KB
/
e2e_pending_note_hashes_contract.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import { type AztecAddress, type AztecNode, type DebugLogger, Fr, type Wallet } from '@aztec/aztec.js';
import {
MAX_NOTE_HASHES_PER_CALL,
MAX_NOTE_HASHES_PER_TX,
MAX_NOTE_HASH_READ_REQUESTS_PER_CALL,
MAX_NOTE_HASH_READ_REQUESTS_PER_TX,
} from '@aztec/circuits.js';
import { PendingNoteHashesContract } from '@aztec/noir-contracts.js/PendingNoteHashes';
import { EncryptedNoteTxL2Logs } from '../../circuit-types/src/logs/tx_l2_logs.js';
import { setup } from './fixtures/utils.js';
describe('e2e_pending_note_hashes_contract', () => {
let aztecNode: AztecNode;
let wallet: Wallet;
let logger: DebugLogger;
let owner: AztecAddress;
let teardown: () => Promise<void>;
let contract: PendingNoteHashesContract;
beforeEach(async () => {
({ teardown, aztecNode, wallet, logger } = await setup(2));
owner = wallet.getAddress();
});
afterEach(() => teardown());
const expectNoteHashesSquashedExcept = async (exceptFirstFew: number) => {
const blockNum = await aztecNode.getBlockNumber();
const block = (await aztecNode.getBlocks(blockNum, 1))[0];
const noteHashes = block.body.txEffects.flatMap(txEffect => txEffect.noteHashes);
// all new note hashes should be zero (should be squashed)
for (let c = 0; c < exceptFirstFew; c++) {
expect(noteHashes[c]).not.toEqual(Fr.ZERO);
}
for (let c = exceptFirstFew; c < noteHashes.length; c++) {
expect(noteHashes[c]).toEqual(Fr.ZERO);
}
};
const expectNullifiersSquashedExcept = async (exceptFirstFew: number) => {
const blockNum = await aztecNode.getBlockNumber();
const block = (await aztecNode.getBlocks(blockNum, 1))[0];
const nullifierArray = block.body.txEffects.flatMap(txEffect => txEffect.nullifiers);
// 0th nullifier should be nonzero (txHash), all others should be zero (should be squashed)
for (let n = 0; n < exceptFirstFew + 1; n++) {
logger.info(`Expecting nullifier ${n} to be nonzero`);
expect(nullifierArray[n]).not.toEqual(Fr.ZERO); // 0th nullifier is txHash
}
for (let n = exceptFirstFew + 1; n < nullifierArray.length; n++) {
expect(nullifierArray[n]).toEqual(Fr.ZERO);
}
};
const expectNoteLogsSquashedExcept = async (exceptFirstFew: number) => {
const blockNum = await aztecNode.getBlockNumber();
const block = (await aztecNode.getBlocks(blockNum, 1))[0];
const logArray = block.body.txEffects.flatMap(txEffect => txEffect.noteEncryptedLogs);
for (let l = 0; l < exceptFirstFew + 1; l++) {
expect(logArray[l]).not.toEqual(EncryptedNoteTxL2Logs.empty());
}
for (let l = exceptFirstFew + 1; l < logArray.length; l++) {
expect(logArray[l]).toEqual(EncryptedNoteTxL2Logs.empty());
}
};
const deployContract = async () => {
logger.debug(`Deploying L2 contract...`);
contract = await PendingNoteHashesContract.deploy(wallet).send().deployed();
logger.info(`L2 contract deployed at ${contract.address}`);
return contract;
};
it('Aztec.nr function can "get" notes it just "inserted"', async () => {
const mintAmount = 65n;
const deployedContract = await deployContract();
const outgoingViewer = owner;
await deployedContract.methods
.test_insert_then_get_then_nullify_flat(mintAmount, owner, outgoingViewer)
.send()
.wait();
});
it('Squash! Aztec.nr function can "create" and "nullify" note in the same TX', async () => {
// Kernel will squash the noteHash and its nullifier.
// Realistic way to describe this test is "Mint note A, then burn note A in the same transaction"
const mintAmount = 65n;
const deployedContract = await deployContract();
const outgoingViewer = owner;
await deployedContract.methods
.test_insert_then_get_then_nullify_all_in_nested_calls(
mintAmount,
owner,
outgoingViewer,
deployedContract.methods.insert_note.selector,
deployedContract.methods.get_then_nullify_note.selector,
)
.send()
.wait();
await deployedContract.methods.get_note_zero_balance(owner).send().wait();
await expectNoteHashesSquashedExcept(0);
await expectNullifiersSquashedExcept(0);
await expectNoteLogsSquashedExcept(0);
});
it('Squash! Aztec.nr function can "create" and "nullify" note in the same TX with 2 note logs', async () => {
// Kernel will squash the noteHash and its nullifier and both note logs
// Realistic way to describe this test is "Mint note A, then burn note A in the same transaction"
const mintAmount = 65n;
const deployedContract = await deployContract();
const outgoingViewer = owner;
await deployedContract.methods
.test_insert_then_get_then_nullify_all_in_nested_calls(
mintAmount,
owner,
outgoingViewer,
deployedContract.methods.insert_note_extra_emit.selector,
deployedContract.methods.get_then_nullify_note.selector,
)
.send()
.wait();
await expectNoteHashesSquashedExcept(0);
await expectNullifiersSquashedExcept(0);
await expectNoteLogsSquashedExcept(0);
});
it('Squash! Aztec.nr function can "create" 2 notes and "nullify" both in the same TX', async () => {
// Kernel will squash both noteHashes and their nullifier.
// Realistic way to describe this test is "Mint notes A and B, then burn both in the same transaction"
const mintAmount = 65n;
const deployedContract = await deployContract();
const outgoingViewer = owner;
await deployedContract.methods
.test_insert2_then_get2_then_nullify2_all_in_nested_calls(
mintAmount,
owner,
outgoingViewer,
deployedContract.methods.insert_note.selector,
deployedContract.methods.get_then_nullify_note.selector,
)
.send()
.wait();
await expectNoteHashesSquashedExcept(0);
await expectNullifiersSquashedExcept(0);
await expectNoteLogsSquashedExcept(0);
});
it('Squash! Aztec.nr function can "create" 2 notes and "nullify" 1 in the same TX (kernel will squash one note + nullifier)', async () => {
// Kernel will squash one noteHash and its nullifier.
// The other note will become persistent!
// Realistic way to describe this test is "Mint notes A and B, then burn note A in the same transaction"
const mintAmount = 65n;
const deployedContract = await deployContract();
const outgoingViewer = owner;
await deployedContract.methods
.test_insert2_then_get2_then_nullify1_all_in_nested_calls(
mintAmount,
owner,
outgoingViewer,
deployedContract.methods.insert_note.selector,
deployedContract.methods.get_then_nullify_note.selector,
)
.send()
.wait();
await expectNoteHashesSquashedExcept(1);
await expectNullifiersSquashedExcept(0);
await expectNoteLogsSquashedExcept(1);
});
it('Squash! Aztec.nr function can "create" 2 notes with the same inner note hash and "nullify" 1 in the same TX', async () => {
// Kernel will squash one noteHash and its nullifier, where two notes with the same inner hash exist.
// The other note will become persistent!
// Realistic way to describe this test is "Mint notes A and B, then burn note A in the same transaction"
const mintAmount = 65n;
const deployedContract = await deployContract();
const outgoingViewer = owner;
await deployedContract.methods
.test_insert2_then_get2_then_nullify1_all_in_nested_calls(
mintAmount,
owner,
outgoingViewer,
deployedContract.methods.insert_note_static_randomness.selector,
deployedContract.methods.get_then_nullify_note.selector,
)
.send()
.wait();
await expectNoteHashesSquashedExcept(1);
await expectNullifiersSquashedExcept(0);
await expectNoteLogsSquashedExcept(1);
});
it('Squash! Aztec.nr function can nullify a pending note and a persistent in the same TX', async () => {
// Create 1 note in isolated TX.
// Then, in a separate TX, create 1 new note and nullify BOTH notes.
// In this second TX, the kernel will squash one note + nullifier,
// but the nullifier for the persistent note (from the first TX) will itself become persistent.
// Realistic way to describe this test is "Mint note A, then burn note A in the same transaction"
const mintAmount = 65n;
const deployedContract = await deployContract();
// create persistent note
const outgoingViewer = owner;
await deployedContract.methods.insert_note(mintAmount, owner, outgoingViewer).send().wait();
await expectNoteHashesSquashedExcept(1); // first TX just creates 1 persistent note
await expectNullifiersSquashedExcept(0);
await expectNoteLogsSquashedExcept(1);
// create another note, and nullify it and AND nullify the above-created note in the same TX
await deployedContract.methods
.test_insert1_then_get2_then_nullify2_all_in_nested_calls(
mintAmount,
owner,
outgoingViewer,
deployedContract.methods.insert_note.selector,
deployedContract.methods.get_then_nullify_note.selector,
)
.send()
.wait();
await deployedContract.methods.get_note_zero_balance(owner).send().wait();
// second TX creates 1 note, but it is squashed!
await expectNoteHashesSquashedExcept(0);
// the nullifier corresponding to this transient note is squashed, but the
// other nullifier corresponding to the persistent note becomes persistent itself.
await expectNullifiersSquashedExcept(1);
await expectNoteLogsSquashedExcept(0);
});
it('get_notes function filters a nullified note created in a previous transaction', async () => {
// Create a note in an isolated transaction.
// In a subsequent transaction, we nullify the note and a call to 'get note' should
// not return anything.
// Remark: This test can be seen as a simplification of the previous one but has the merit to
// isolate the simplest 'get note' filtering with a pending nullifier on a persistent note.
const mintAmount = 65n;
const deployedContract = await deployContract();
const outgoingViewer = owner;
await deployedContract.methods.insert_note(mintAmount, owner, outgoingViewer).send().wait();
// There is a single new note hash.
await expectNoteHashesSquashedExcept(1);
await expectNoteLogsSquashedExcept(1);
await deployedContract.methods
.test_insert_then_get_then_nullify_all_in_nested_calls(
mintAmount,
owner,
outgoingViewer,
deployedContract.methods.dummy.selector,
deployedContract.methods.get_then_nullify_note.selector,
)
.send()
.wait();
// There is a single new nullifier.
await expectNullifiersSquashedExcept(1);
});
it('Should handle overflowing the kernel data structures in nested calls', async () => {
// Setting the outgoing viewer to owner not have to bother with setting up another account.
const outgoingViewer = owner;
const notesPerIteration = Math.min(MAX_NOTE_HASHES_PER_CALL, MAX_NOTE_HASH_READ_REQUESTS_PER_CALL);
const minToNeedReset = Math.min(MAX_NOTE_HASHES_PER_TX, MAX_NOTE_HASH_READ_REQUESTS_PER_TX) + 1;
const deployedContract = await deployContract();
await deployedContract.methods
.test_recursively_create_notes(owner, outgoingViewer, Math.ceil(minToNeedReset / notesPerIteration))
.send()
.wait();
});
it('Should drop note log for non existent note', async () => {
const deployedContract = await deployContract();
const outgoingViewer = owner;
// Add a note of value 10, with a note log
// Then emit another note log with the same counter as the one above, but with value 5
await deployedContract.methods.test_emit_bad_note_log(owner, outgoingViewer).send().wait();
const syncStats = await wallet.getSyncStats();
// Expect two incoming decryptable note logs to be emitted
expect(syncStats[owner.toString()].decryptedIncoming).toEqual(2);
// Expect one note log to be dropped
expect(syncStats[owner.toString()].failed).toEqual(1);
});
});