@aztec/simulator 0.87.2 → 0.87.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dest/client.d.ts +2 -0
- package/dest/client.d.ts.map +1 -1
- package/dest/client.js +2 -0
- package/dest/private/acvm/acvm.d.ts +4 -0
- package/dest/private/acvm/acvm.d.ts.map +1 -1
- package/dest/private/acvm/oracle/oracle.d.ts +1 -1
- package/dest/private/acvm/oracle/oracle.d.ts.map +1 -1
- package/dest/private/acvm/oracle/oracle.js +2 -2
- package/dest/private/acvm/oracle/typed_oracle.d.ts +1 -1
- package/dest/private/acvm/oracle/typed_oracle.d.ts.map +1 -1
- package/dest/private/acvm/oracle/typed_oracle.js +2 -2
- package/dest/private/private_execution.d.ts.map +1 -1
- package/dest/private/private_execution.js +2 -1
- package/dest/private/private_execution_oracle.d.ts +1 -1
- package/dest/private/private_execution_oracle.d.ts.map +1 -1
- package/dest/private/private_execution_oracle.js +1 -1
- package/dest/private/providers/circuit_recording/circuit_recorder.d.ts +39 -19
- package/dest/private/providers/circuit_recording/circuit_recorder.d.ts.map +1 -1
- package/dest/private/providers/circuit_recording/circuit_recorder.js +90 -126
- package/dest/private/providers/circuit_recording/file_circuit_recorder.d.ts +31 -0
- package/dest/private/providers/circuit_recording/file_circuit_recorder.d.ts.map +1 -0
- package/dest/private/providers/circuit_recording/file_circuit_recorder.js +135 -0
- package/dest/private/providers/circuit_recording/memory_circuit_recorder.d.ts +5 -0
- package/dest/private/providers/circuit_recording/memory_circuit_recorder.d.ts.map +1 -0
- package/dest/private/providers/circuit_recording/memory_circuit_recorder.js +9 -0
- package/dest/private/providers/circuit_recording/simulation_provider_recorder_wrapper.d.ts +3 -1
- package/dest/private/providers/circuit_recording/simulation_provider_recorder_wrapper.d.ts.map +1 -1
- package/dest/private/providers/circuit_recording/simulation_provider_recorder_wrapper.js +16 -11
- package/dest/private/utility_execution_oracle.d.ts +1 -1
- package/dest/private/utility_execution_oracle.d.ts.map +1 -1
- package/dest/private/utility_execution_oracle.js +1 -1
- package/dest/public/avm/fixtures/base_avm_simulation_tester.d.ts +1 -0
- package/dest/public/avm/fixtures/base_avm_simulation_tester.d.ts.map +1 -1
- package/dest/public/avm/fixtures/base_avm_simulation_tester.js +6 -0
- package/dest/public/public_tx_simulator/apps_tests/amm_test.d.ts.map +1 -1
- package/dest/public/public_tx_simulator/apps_tests/amm_test.js +27 -55
- package/dest/server.d.ts +2 -0
- package/dest/server.d.ts.map +1 -1
- package/dest/server.js +2 -0
- package/dest/testing.d.ts +1 -1
- package/dest/testing.d.ts.map +1 -1
- package/dest/testing.js +1 -1
- package/package.json +15 -15
- package/src/client.ts +2 -0
- package/src/private/acvm/acvm.ts +3 -0
- package/src/private/acvm/oracle/oracle.ts +2 -2
- package/src/private/acvm/oracle/typed_oracle.ts +2 -2
- package/src/private/private_execution.ts +1 -0
- package/src/private/private_execution_oracle.ts +1 -1
- package/src/private/providers/circuit_recording/circuit_recorder.ts +114 -136
- package/src/private/providers/circuit_recording/file_circuit_recorder.ts +158 -0
- package/src/private/providers/circuit_recording/memory_circuit_recorder.ts +11 -0
- package/src/private/providers/circuit_recording/simulation_provider_recorder_wrapper.ts +22 -14
- package/src/private/utility_execution_oracle.ts +1 -1
- package/src/public/avm/fixtures/base_avm_simulation_tester.ts +5 -0
- package/src/public/public_tx_simulator/apps_tests/amm_test.ts +49 -44
- package/src/server.ts +2 -0
- package/src/testing.ts +1 -1
|
@@ -500,7 +500,7 @@ export class PrivateExecutionOracle extends UtilityExecutionOracle {
|
|
|
500
500
|
await this.executionDataProvider.incrementAppTaggingSecretIndexAsSender(this.contractAddress, sender, recipient);
|
|
501
501
|
}
|
|
502
502
|
|
|
503
|
-
public override async
|
|
503
|
+
public override async fetchTaggedLogs(pendingTaggedLogArrayBaseSlot: Fr) {
|
|
504
504
|
await this.executionDataProvider.syncTaggedLogs(this.contractAddress, pendingTaggedLogArrayBaseSlot, this.scopes);
|
|
505
505
|
|
|
506
506
|
await this.executionDataProvider.removeNullifiedNotes(this.contractAddress);
|
|
@@ -1,28 +1,59 @@
|
|
|
1
|
+
import { sha512 } from '@aztec/foundation/crypto';
|
|
1
2
|
import { createLogger } from '@aztec/foundation/log';
|
|
3
|
+
import { Timer } from '@aztec/foundation/timer';
|
|
2
4
|
import type { ForeignCallHandler, ForeignCallInput, ForeignCallOutput } from '@aztec/noir-acvm_js';
|
|
3
5
|
|
|
4
|
-
import { createHash } from 'crypto';
|
|
5
|
-
import fs from 'fs/promises';
|
|
6
|
-
import path from 'path';
|
|
7
|
-
|
|
8
6
|
import type { ACIRCallback } from '../../acvm/acvm.js';
|
|
9
7
|
import type { ACVMWitness } from '../../acvm/acvm_types.js';
|
|
10
8
|
import { Oracle } from '../../acvm/oracle/oracle.js';
|
|
11
9
|
|
|
10
|
+
export type OracleCall = {
|
|
11
|
+
name: string;
|
|
12
|
+
inputs: unknown[];
|
|
13
|
+
outputs: unknown;
|
|
14
|
+
time: number;
|
|
15
|
+
// Due to the recursive nature of the simulator, we might have
|
|
16
|
+
// oracle calls performed after a foreign call (which is itself an oracle call)
|
|
17
|
+
// We keep track of the stack depth in this variable to ensure the recorded oracle
|
|
18
|
+
// calls are correctly associated with the right circuit.
|
|
19
|
+
// This is only use as a debugging tool
|
|
20
|
+
stackDepth: number;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export class CircuitRecording {
|
|
24
|
+
circuitName: string;
|
|
25
|
+
functionName: string;
|
|
26
|
+
bytecodeSHA512Hash: string;
|
|
27
|
+
timestamp: number;
|
|
28
|
+
inputs: Record<string, string>;
|
|
29
|
+
oracleCalls: OracleCall[];
|
|
30
|
+
error?: string;
|
|
31
|
+
parent?: CircuitRecording;
|
|
32
|
+
|
|
33
|
+
constructor(circuitName: string, functionName: string, bytecodeSHA512Hash: string, inputs: Record<string, string>) {
|
|
34
|
+
this.circuitName = circuitName;
|
|
35
|
+
this.functionName = functionName;
|
|
36
|
+
this.bytecodeSHA512Hash = bytecodeSHA512Hash;
|
|
37
|
+
this.timestamp = Date.now();
|
|
38
|
+
this.inputs = inputs;
|
|
39
|
+
this.oracleCalls = [];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
setParent(recording?: CircuitRecording): void {
|
|
43
|
+
this.parent = recording;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
12
47
|
/**
|
|
13
48
|
* Class responsible for recording circuit inputs necessary to replay the circuit. These inputs are the initial witness
|
|
14
49
|
* map and the oracle calls made during the circuit execution/witness generation.
|
|
15
50
|
*
|
|
16
|
-
*
|
|
17
|
-
* a counter to ensure unique filenames. The file is stored in the `recordDir` directory provided as a parameter to
|
|
18
|
-
* CircuitRecorder.start().
|
|
19
|
-
*
|
|
20
|
-
* Example recording file:
|
|
51
|
+
* Example recording object:
|
|
21
52
|
* ```json
|
|
22
53
|
* {
|
|
23
54
|
* "circuitName": "AMM",
|
|
24
55
|
* "functionName": "add_liquidity",
|
|
25
|
-
* "
|
|
56
|
+
* "bytecodeSHA512Hash": "b46c640ed38f20eac5f61a5e41d8dd1e",
|
|
26
57
|
* "timestamp": 1740691464360,
|
|
27
58
|
* "inputs": {
|
|
28
59
|
* "0": "0x1e89de1f0ad5204263733b7ddf65bec45b8f44714a4da85a46474dad677679ef",
|
|
@@ -51,7 +82,7 @@ import { Oracle } from '../../acvm/oracle/oracle.js';
|
|
|
51
82
|
* ]
|
|
52
83
|
* },
|
|
53
84
|
* {
|
|
54
|
-
* "name": "
|
|
85
|
+
* "name": "fetchTaggedLogs",
|
|
55
86
|
* "inputs": []
|
|
56
87
|
* }
|
|
57
88
|
* ]
|
|
@@ -59,10 +90,14 @@ import { Oracle } from '../../acvm/oracle/oracle.js';
|
|
|
59
90
|
* ```
|
|
60
91
|
*/
|
|
61
92
|
export class CircuitRecorder {
|
|
62
|
-
|
|
63
|
-
private isFirstCall = true;
|
|
93
|
+
protected readonly logger = createLogger('simulator:acvm:recording');
|
|
64
94
|
|
|
65
|
-
|
|
95
|
+
protected recording?: CircuitRecording;
|
|
96
|
+
|
|
97
|
+
private stackDepth: number = 0;
|
|
98
|
+
private newCircuit: boolean = true;
|
|
99
|
+
|
|
100
|
+
protected constructor() {}
|
|
66
101
|
|
|
67
102
|
/**
|
|
68
103
|
* Initializes a new circuit recording session.
|
|
@@ -72,82 +107,20 @@ export class CircuitRecorder {
|
|
|
72
107
|
* @param circuitName - Name of the circuit
|
|
73
108
|
* @param functionName - Name of the circuit function (defaults to 'main'). This is meaningful only for
|
|
74
109
|
* contracts as protocol circuits artifacts always contain a single entrypoint function called 'main'.
|
|
75
|
-
* @returns A new CircuitRecorder instance
|
|
76
110
|
*/
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
functionName: functionName,
|
|
87
|
-
bytecodeMd5Hash: createHash('md5').update(circuitBytecode).digest('hex'),
|
|
88
|
-
timestamp: Date.now(),
|
|
89
|
-
inputs: Object.fromEntries(input),
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
const recordingStringWithoutClosingBracket = JSON.stringify(recording, null, 2).slice(0, -2);
|
|
93
|
-
|
|
94
|
-
try {
|
|
95
|
-
// Check if the recording directory exists and is a directory
|
|
96
|
-
const stats = await fs.stat(recordDir);
|
|
97
|
-
if (!stats.isDirectory()) {
|
|
98
|
-
throw new Error(`Recording path ${recordDir} exists but is not a directory`);
|
|
99
|
-
}
|
|
100
|
-
} catch (err) {
|
|
101
|
-
if ((err as NodeJS.ErrnoException).code === 'ENOENT') {
|
|
102
|
-
// The directory does not exist so we create it
|
|
103
|
-
await fs.mkdir(recordDir, { recursive: true });
|
|
104
|
-
} else {
|
|
105
|
-
throw err;
|
|
106
|
-
}
|
|
111
|
+
start(input: ACVMWitness, circuitBytecode: Buffer, circuitName: string, functionName: string): Promise<void> {
|
|
112
|
+
const parentRef = this.recording;
|
|
113
|
+
if (this.newCircuit) {
|
|
114
|
+
this.recording = new CircuitRecording(
|
|
115
|
+
circuitName,
|
|
116
|
+
functionName,
|
|
117
|
+
sha512(circuitBytecode).toString('hex'),
|
|
118
|
+
Object.fromEntries(input),
|
|
119
|
+
);
|
|
107
120
|
}
|
|
121
|
+
this.recording!.setParent(parentRef);
|
|
108
122
|
|
|
109
|
-
|
|
110
|
-
recordDir,
|
|
111
|
-
circuitName,
|
|
112
|
-
functionName,
|
|
113
|
-
recordingStringWithoutClosingBracket,
|
|
114
|
-
);
|
|
115
|
-
return new CircuitRecorder(filePath);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Computes a unique file path for the recording by trying different counter values.
|
|
120
|
-
* This is needed because multiple recordings of the same circuit could be happening simultaneously or an older
|
|
121
|
-
* recording might be present.
|
|
122
|
-
* @param recordDir - Directory to store the recording
|
|
123
|
-
* @param circuitName - Name of the circuit
|
|
124
|
-
* @param functionName - Name of the circuit function
|
|
125
|
-
* @param recordingContent - Initial recording content
|
|
126
|
-
* @returns A unique file path for the recording
|
|
127
|
-
*/
|
|
128
|
-
static async #computeFilePathAndStoreInitialRecording(
|
|
129
|
-
recordDir: string,
|
|
130
|
-
circuitName: string,
|
|
131
|
-
functionName: string,
|
|
132
|
-
recordingContent: string,
|
|
133
|
-
): Promise<string> {
|
|
134
|
-
let counter = 0;
|
|
135
|
-
while (true) {
|
|
136
|
-
try {
|
|
137
|
-
const filePath = getFilePath(recordDir, circuitName, functionName, counter);
|
|
138
|
-
// Write the initial recording content to the file
|
|
139
|
-
await fs.writeFile(filePath, recordingContent + ',\n "oracleCalls": [\n', {
|
|
140
|
-
flag: 'wx', // wx flag fails if file exists
|
|
141
|
-
});
|
|
142
|
-
return filePath;
|
|
143
|
-
} catch (err) {
|
|
144
|
-
if ((err as NodeJS.ErrnoException).code === 'EEXIST') {
|
|
145
|
-
counter++;
|
|
146
|
-
continue;
|
|
147
|
-
}
|
|
148
|
-
throw err;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
123
|
+
return Promise.resolve();
|
|
151
124
|
}
|
|
152
125
|
|
|
153
126
|
/**
|
|
@@ -187,15 +160,38 @@ export class CircuitRecorder {
|
|
|
187
160
|
throw new Error(`Oracle method ${name} not found when setting up recording callback`);
|
|
188
161
|
}
|
|
189
162
|
|
|
163
|
+
const isExternalCall = (name as keyof ACIRCallback) === 'callPrivateFunction';
|
|
164
|
+
|
|
190
165
|
recordingCallback[name as keyof ACIRCallback] = (...args: ForeignCallInput[]): ReturnType<typeof fn> => {
|
|
166
|
+
const timer = new Timer();
|
|
167
|
+
// If we're entering another circuit via `callPrivateFunction`, we increase the stack depth and set the
|
|
168
|
+
// newCircuit variable to ensure we are creating a new recording object.
|
|
169
|
+
if (isExternalCall) {
|
|
170
|
+
this.stackDepth++;
|
|
171
|
+
this.newCircuit = true;
|
|
172
|
+
}
|
|
191
173
|
const result = fn.call(callback, ...args);
|
|
192
174
|
if (result instanceof Promise) {
|
|
193
175
|
return result.then(async r => {
|
|
194
|
-
|
|
176
|
+
// Once we leave the nested circuit, we decrease the stack depth and set newCircuit to false
|
|
177
|
+
// since we are going back to the "parent" circuit which can never be new
|
|
178
|
+
if (isExternalCall) {
|
|
179
|
+
this.stackDepth--;
|
|
180
|
+
this.newCircuit = false;
|
|
181
|
+
this.recording = this.recording!.parent;
|
|
182
|
+
}
|
|
183
|
+
await this.recordCall(name, args, r, timer.ms(), this.stackDepth);
|
|
195
184
|
return r;
|
|
196
185
|
}) as ReturnType<typeof fn>;
|
|
197
186
|
}
|
|
198
|
-
|
|
187
|
+
// Once we leave the nested circuit, we decrease the stack depth and set newCircuit to false
|
|
188
|
+
// since we are going back to the "parent" circuit which can never be new
|
|
189
|
+
if (isExternalCall) {
|
|
190
|
+
this.stackDepth--;
|
|
191
|
+
this.newCircuit = false;
|
|
192
|
+
this.recording = this.recording!.parent;
|
|
193
|
+
}
|
|
194
|
+
void this.recordCall(name, args, result, timer.ms(), this.stackDepth);
|
|
199
195
|
return result;
|
|
200
196
|
};
|
|
201
197
|
}
|
|
@@ -210,8 +206,9 @@ export class CircuitRecorder {
|
|
|
210
206
|
*/
|
|
211
207
|
#wrapProtocolCircuitCallback(callback: ForeignCallHandler): ForeignCallHandler {
|
|
212
208
|
return async (name: string, inputs: ForeignCallInput[]): Promise<ForeignCallOutput[]> => {
|
|
209
|
+
const timer = new Timer();
|
|
213
210
|
const result = await callback(name, inputs);
|
|
214
|
-
await this
|
|
211
|
+
await this.recordCall(name, inputs, result, timer.ms(), 0);
|
|
215
212
|
return result;
|
|
216
213
|
};
|
|
217
214
|
}
|
|
@@ -222,62 +219,43 @@ export class CircuitRecorder {
|
|
|
222
219
|
* @param inputs - Input arguments
|
|
223
220
|
* @param outputs - Output results
|
|
224
221
|
*/
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
} catch (err) {
|
|
236
|
-
this.logger.error('Failed to log circuit call', { error: err });
|
|
237
|
-
}
|
|
222
|
+
recordCall(name: string, inputs: unknown[], outputs: unknown, time: number, stackDepth: number): Promise<OracleCall> {
|
|
223
|
+
const entry = {
|
|
224
|
+
name,
|
|
225
|
+
inputs,
|
|
226
|
+
outputs,
|
|
227
|
+
time,
|
|
228
|
+
stackDepth,
|
|
229
|
+
};
|
|
230
|
+
this.recording!.oracleCalls.push(entry);
|
|
231
|
+
return Promise.resolve(entry);
|
|
238
232
|
}
|
|
239
233
|
|
|
240
234
|
/**
|
|
241
|
-
* Finalizes the recording
|
|
242
|
-
* incomplete and it fails to parse.
|
|
235
|
+
* Finalizes the recording by resetting the state and returning the recording object.
|
|
243
236
|
*/
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
this.
|
|
237
|
+
finish(): Promise<CircuitRecording> {
|
|
238
|
+
const result = this.recording;
|
|
239
|
+
// If this is the top-level circuit recording, we reset the state for the next simulator call
|
|
240
|
+
if (!result!.parent) {
|
|
241
|
+
this.newCircuit = true;
|
|
242
|
+
this.recording = undefined;
|
|
249
243
|
}
|
|
244
|
+
return Promise.resolve(result!);
|
|
250
245
|
}
|
|
251
246
|
|
|
252
247
|
/**
|
|
253
|
-
* Finalizes the recording
|
|
254
|
-
* the recording file is incomplete and it fails to parse.
|
|
248
|
+
* Finalizes the recording by resetting the state and returning the recording object with an attached error.
|
|
255
249
|
* @param error - The error that occurred during circuit execution
|
|
256
250
|
*/
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
this.logger.error('Failed to finalize recording file with error', { error: err });
|
|
251
|
+
finishWithError(error: unknown): Promise<CircuitRecording> {
|
|
252
|
+
const result = this.recording;
|
|
253
|
+
// If this is the top-level circuit recording, we reset the state for the next simulator call
|
|
254
|
+
if (!result!.parent) {
|
|
255
|
+
this.newCircuit = true;
|
|
256
|
+
this.recording = undefined;
|
|
264
257
|
}
|
|
258
|
+
result!.error = JSON.stringify(error);
|
|
259
|
+
return Promise.resolve(result!);
|
|
265
260
|
}
|
|
266
261
|
}
|
|
267
|
-
|
|
268
|
-
/**
|
|
269
|
-
* Generates a file path for storing circuit recordings. The format of the filename is:
|
|
270
|
-
* `circuit_name_circuit_function_name_YYYY-MM-DD_N.json` where N is a counter to ensure unique filenames.
|
|
271
|
-
* @param recordDir - Base directory for recordings
|
|
272
|
-
* @param circuitName - Name of the circuit
|
|
273
|
-
* @param functionName - Name of the circuit function
|
|
274
|
-
* @param counter - Counter to ensure unique filenames. This is expected to be incremented in a loop until there is no
|
|
275
|
-
* existing file with the same name.
|
|
276
|
-
* @returns A file path for the recording.
|
|
277
|
-
*/
|
|
278
|
-
function getFilePath(recordDir: string, circuitName: string, functionName: string, counter: number): string {
|
|
279
|
-
const date = new Date();
|
|
280
|
-
const formattedDate = date.toISOString().split('T')[0];
|
|
281
|
-
const filename = `${circuitName}_${functionName}_${formattedDate}_${counter}.json`;
|
|
282
|
-
return path.join(recordDir, filename);
|
|
283
|
-
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import fs from 'fs/promises';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
|
|
4
|
+
import type { ACVMWitness } from '../../acvm/acvm_types.js';
|
|
5
|
+
import { CircuitRecorder, type CircuitRecording } from './circuit_recorder.js';
|
|
6
|
+
|
|
7
|
+
export class FileCircuitRecorder extends CircuitRecorder {
|
|
8
|
+
declare recording?: CircuitRecording & { filePath: string; isFirstCall: boolean };
|
|
9
|
+
|
|
10
|
+
constructor(private readonly recordDir: string) {
|
|
11
|
+
super();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
override async start(
|
|
15
|
+
input: ACVMWitness,
|
|
16
|
+
circuitBytecode: Buffer,
|
|
17
|
+
circuitName: string,
|
|
18
|
+
functionName: string = 'main',
|
|
19
|
+
) {
|
|
20
|
+
await super.start(input, circuitBytecode, circuitName, functionName);
|
|
21
|
+
|
|
22
|
+
const recordingStringWithoutClosingBracket = JSON.stringify(
|
|
23
|
+
{ ...this.recording, isFirstCall: undefined, parent: undefined, oracleCalls: undefined, filePath: undefined },
|
|
24
|
+
null,
|
|
25
|
+
2,
|
|
26
|
+
).slice(0, -2);
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
// Check if the recording directory exists and is a directory
|
|
30
|
+
const stats = await fs.stat(this.recordDir);
|
|
31
|
+
if (!stats.isDirectory()) {
|
|
32
|
+
throw new Error(`Recording path ${this.recordDir} exists but is not a directory`);
|
|
33
|
+
}
|
|
34
|
+
} catch (err) {
|
|
35
|
+
if ((err as NodeJS.ErrnoException).code === 'ENOENT') {
|
|
36
|
+
// The directory does not exist so we create it
|
|
37
|
+
await fs.mkdir(this.recordDir, { recursive: true });
|
|
38
|
+
} else {
|
|
39
|
+
throw err;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
this.recording!.isFirstCall = true;
|
|
44
|
+
this.recording!.filePath = await FileCircuitRecorder.#computeFilePathAndStoreInitialRecording(
|
|
45
|
+
this.recordDir,
|
|
46
|
+
this.recording!.circuitName,
|
|
47
|
+
this.recording!.functionName,
|
|
48
|
+
recordingStringWithoutClosingBracket,
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Computes a unique file path for the recording by trying different counter values.
|
|
54
|
+
* This is needed because multiple recordings of the same circuit could be happening simultaneously or an older
|
|
55
|
+
* recording might be present.
|
|
56
|
+
* @param recordDir - Directory to store the recording
|
|
57
|
+
* @param circuitName - Name of the circuit
|
|
58
|
+
* @param functionName - Name of the circuit function
|
|
59
|
+
* @param recordingContent - Initial recording content
|
|
60
|
+
* @returns A unique file path for the recording
|
|
61
|
+
*/
|
|
62
|
+
static async #computeFilePathAndStoreInitialRecording(
|
|
63
|
+
recordDir: string,
|
|
64
|
+
circuitName: string,
|
|
65
|
+
functionName: string,
|
|
66
|
+
recordingContent: string,
|
|
67
|
+
): Promise<string> {
|
|
68
|
+
let counter = 0;
|
|
69
|
+
while (true) {
|
|
70
|
+
try {
|
|
71
|
+
const filePath = getFilePath(recordDir, circuitName, functionName, counter);
|
|
72
|
+
// Write the initial recording content to the file
|
|
73
|
+
await fs.writeFile(filePath, recordingContent + ',\n "oracleCalls": [\n', {
|
|
74
|
+
flag: 'wx', // wx flag fails if file exists
|
|
75
|
+
});
|
|
76
|
+
return filePath;
|
|
77
|
+
} catch (err) {
|
|
78
|
+
if ((err as NodeJS.ErrnoException).code === 'EEXIST') {
|
|
79
|
+
counter++;
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
throw err;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Records a single oracle/foreign call with its inputs and outputs.
|
|
89
|
+
* @param name - Name of the call
|
|
90
|
+
* @param inputs - Input arguments
|
|
91
|
+
* @param outputs - Output results
|
|
92
|
+
*/
|
|
93
|
+
override async recordCall(name: string, inputs: unknown[], outputs: unknown, time: number, stackDepth: number) {
|
|
94
|
+
const entry = await super.recordCall(name, inputs, outputs, time, stackDepth);
|
|
95
|
+
try {
|
|
96
|
+
const prefix = this.recording!.isFirstCall ? ' ' : ' ,';
|
|
97
|
+
this.recording!.isFirstCall = false;
|
|
98
|
+
await fs.appendFile(this.recording!.filePath, prefix + JSON.stringify(entry) + '\n');
|
|
99
|
+
} catch (err) {
|
|
100
|
+
this.logger.error('Failed to log circuit call', { error: err });
|
|
101
|
+
}
|
|
102
|
+
return entry;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Finalizes the recording file by adding closing brackets. Without calling this method, the recording file is
|
|
107
|
+
* incomplete and it fails to parse.
|
|
108
|
+
*/
|
|
109
|
+
override async finish(): Promise<CircuitRecording> {
|
|
110
|
+
// Finish sets the recording to undefined if we are at the topmost circuit,
|
|
111
|
+
// so we save the current file path before that
|
|
112
|
+
const filePath = this.recording!.filePath;
|
|
113
|
+
const result = await super.finish();
|
|
114
|
+
try {
|
|
115
|
+
await fs.appendFile(filePath, ' ]\n}\n');
|
|
116
|
+
} catch (err) {
|
|
117
|
+
this.logger.error('Failed to finalize recording file', { error: err });
|
|
118
|
+
}
|
|
119
|
+
return result!;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Finalizes the recording file by adding the error and closing brackets. Without calling this method or `finish`,
|
|
124
|
+
* the recording file is incomplete and it fails to parse.
|
|
125
|
+
* @param error - The error that occurred during circuit execution
|
|
126
|
+
*/
|
|
127
|
+
override async finishWithError(error: unknown): Promise<CircuitRecording> {
|
|
128
|
+
// Finish sets the recording to undefined if we are at the topmost circuit,
|
|
129
|
+
// so we save the current file path before that
|
|
130
|
+
const filePath = this.recording!.filePath;
|
|
131
|
+
const result = await super.finishWithError(error);
|
|
132
|
+
try {
|
|
133
|
+
await fs.appendFile(filePath, ' ],\n');
|
|
134
|
+
await fs.appendFile(filePath, ` "error": ${JSON.stringify(error)}\n`);
|
|
135
|
+
await fs.appendFile(filePath, '}\n');
|
|
136
|
+
} catch (err) {
|
|
137
|
+
this.logger.error('Failed to finalize recording file with error', { error: err });
|
|
138
|
+
}
|
|
139
|
+
return result!;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Generates a file path for storing circuit recordings. The format of the filename is:
|
|
145
|
+
* `circuit_name_circuit_function_name_YYYY-MM-DD_N.json` where N is a counter to ensure unique filenames.
|
|
146
|
+
* @param recordDir - Base directory for recordings
|
|
147
|
+
* @param circuitName - Name of the circuit
|
|
148
|
+
* @param functionName - Name of the circuit function
|
|
149
|
+
* @param counter - Counter to ensure unique filenames. This is expected to be incremented in a loop until there is no
|
|
150
|
+
* existing file with the same name.
|
|
151
|
+
* @returns A file path for the recording.
|
|
152
|
+
*/
|
|
153
|
+
function getFilePath(recordDir: string, circuitName: string, functionName: string, counter: number): string {
|
|
154
|
+
const date = new Date();
|
|
155
|
+
const formattedDate = date.toISOString().split('T')[0];
|
|
156
|
+
const filename = `${circuitName}_${functionName}_${formattedDate}_${counter}.json`;
|
|
157
|
+
return path.join(recordDir, filename);
|
|
158
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { CircuitRecorder } from './circuit_recorder.js';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* In memory circuit recorder uses the default implementation. This is kept
|
|
5
|
+
* while we decide the fate of the FileCircuitRecorder
|
|
6
|
+
*/
|
|
7
|
+
export class MemoryCircuitRecorder extends CircuitRecorder {
|
|
8
|
+
constructor() {
|
|
9
|
+
super();
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -2,18 +2,21 @@ import type { ForeignCallHandler } from '@aztec/noir-protocol-circuits-types/typ
|
|
|
2
2
|
import type { FunctionArtifactWithContractName } from '@aztec/stdlib/abi';
|
|
3
3
|
import type { NoirCompiledCircuitWithName } from '@aztec/stdlib/noir';
|
|
4
4
|
|
|
5
|
-
import type { ACIRCallback, ACIRExecutionResult } from '../../acvm/acvm.js';
|
|
5
|
+
import type { ACIRCallback, ACIRCallbackStats, ACIRExecutionResult } from '../../acvm/acvm.js';
|
|
6
6
|
import type { ACVMWitness } from '../../acvm/acvm_types.js';
|
|
7
7
|
import type { ACVMSuccess } from '../acvm_native.js';
|
|
8
8
|
import type { SimulationProvider } from '../simulation_provider.js';
|
|
9
|
-
import { CircuitRecorder } from './circuit_recorder.js';
|
|
9
|
+
import type { CircuitRecorder } from './circuit_recorder.js';
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* Takes a simulation provider and wraps it in a circuit recorder. See CircuitRecorder for more details on how circuit
|
|
13
13
|
* recording works.
|
|
14
14
|
*/
|
|
15
15
|
export class SimulationProviderRecorderWrapper implements SimulationProvider {
|
|
16
|
-
constructor(
|
|
16
|
+
constructor(
|
|
17
|
+
private simulator: SimulationProvider,
|
|
18
|
+
private recorder: CircuitRecorder,
|
|
19
|
+
) {}
|
|
17
20
|
|
|
18
21
|
executeProtocolCircuit(
|
|
19
22
|
input: ACVMWitness,
|
|
@@ -47,7 +50,7 @@ export class SimulationProviderRecorderWrapper implements SimulationProvider {
|
|
|
47
50
|
);
|
|
48
51
|
}
|
|
49
52
|
|
|
50
|
-
async #simulate<C extends ACIRCallback | ForeignCallHandler | undefined, T>(
|
|
53
|
+
async #simulate<C extends ACIRCallback | ForeignCallHandler | undefined, T extends ACIRExecutionResult | ACVMSuccess>(
|
|
51
54
|
simulateFn: (wrappedCallback: C) => Promise<T>,
|
|
52
55
|
input: ACVMWitness,
|
|
53
56
|
bytecode: Buffer,
|
|
@@ -55,28 +58,33 @@ export class SimulationProviderRecorderWrapper implements SimulationProvider {
|
|
|
55
58
|
functionName: string,
|
|
56
59
|
callback: C,
|
|
57
60
|
): Promise<T> {
|
|
58
|
-
const recordDir = process.env.CIRCUIT_RECORD_DIR;
|
|
59
|
-
if (!recordDir) {
|
|
60
|
-
// Recording is not enabled so we just execute the circuit
|
|
61
|
-
return simulateFn(callback);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
61
|
// Start recording circuit execution
|
|
65
|
-
|
|
62
|
+
await this.recorder.start(input, bytecode, contractName, functionName);
|
|
66
63
|
|
|
67
64
|
// If callback was provided, we wrap it in a circuit recorder callback wrapper
|
|
68
|
-
const wrappedCallback = recorder.wrapCallback(callback);
|
|
65
|
+
const wrappedCallback = this.recorder.wrapCallback(callback);
|
|
69
66
|
let result: T;
|
|
70
67
|
try {
|
|
71
68
|
result = await simulateFn(wrappedCallback as C);
|
|
72
69
|
} catch (error) {
|
|
73
70
|
// If an error occurs, we finalize the recording file with the error
|
|
74
|
-
await recorder.finishWithError(error);
|
|
71
|
+
await this.recorder.finishWithError(error);
|
|
75
72
|
throw error;
|
|
76
73
|
}
|
|
77
74
|
|
|
78
75
|
// Witness generation is complete so we finish the circuit recorder
|
|
79
|
-
await recorder.finish();
|
|
76
|
+
const recording = await this.recorder.finish();
|
|
77
|
+
|
|
78
|
+
(result as ACIRExecutionResult).oracles = recording.oracleCalls?.reduce(
|
|
79
|
+
(acc, { time, name }) => {
|
|
80
|
+
if (!acc[name]) {
|
|
81
|
+
acc[name] = { times: [] };
|
|
82
|
+
}
|
|
83
|
+
acc[name].times.push(time);
|
|
84
|
+
return acc;
|
|
85
|
+
},
|
|
86
|
+
{} as Record<string, ACIRCallbackStats>,
|
|
87
|
+
);
|
|
80
88
|
|
|
81
89
|
return result;
|
|
82
90
|
}
|
|
@@ -274,7 +274,7 @@ export class UtilityExecutionOracle extends TypedOracle {
|
|
|
274
274
|
return await this.executionDataProvider.getIndexedTaggingSecretAsSender(this.contractAddress, sender, recipient);
|
|
275
275
|
}
|
|
276
276
|
|
|
277
|
-
public override async
|
|
277
|
+
public override async fetchTaggedLogs(pendingTaggedLogArrayBaseSlot: Fr) {
|
|
278
278
|
await this.executionDataProvider.syncTaggedLogs(this.contractAddress, pendingTaggedLogArrayBaseSlot, this.scopes);
|
|
279
279
|
|
|
280
280
|
await this.executionDataProvider.removeNullifiedNotes(this.contractAddress);
|
|
@@ -100,4 +100,9 @@ export abstract class BaseAvmSimulationTester {
|
|
|
100
100
|
);
|
|
101
101
|
await this.merkleTrees.sequentialInsert(MerkleTreeId.NULLIFIER_TREE, [contractAddressNullifier.toBuffer()]);
|
|
102
102
|
}
|
|
103
|
+
|
|
104
|
+
async insertNullifier(contractThatEmitted: AztecAddress, nullifier: Fr) {
|
|
105
|
+
const siloedNullifier = await siloNullifier(contractThatEmitted, nullifier);
|
|
106
|
+
await this.merkleTrees.sequentialInsert(MerkleTreeId.NULLIFIER_TREE, [siloedNullifier.toBuffer()]);
|
|
107
|
+
}
|
|
103
108
|
}
|