@aztec/simulator 0.87.5 → 0.87.7
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 +5 -3
- package/dest/client.d.ts.map +1 -1
- package/dest/client.js +4 -2
- package/dest/private/acvm/acvm.d.ts +5 -2
- package/dest/private/acvm/acvm.d.ts.map +1 -1
- package/dest/private/acvm/index.d.ts +0 -1
- package/dest/private/acvm/index.d.ts.map +1 -1
- package/dest/private/acvm/index.js +0 -1
- package/dest/private/{providers/acvm_native.d.ts → acvm_native.d.ts} +4 -4
- package/dest/private/acvm_native.d.ts.map +1 -0
- package/dest/private/{providers/acvm_wasm.d.ts → acvm_wasm.d.ts} +4 -4
- package/dest/private/acvm_wasm.d.ts.map +1 -0
- package/dest/private/{providers/acvm_wasm.js → acvm_wasm.js} +2 -2
- package/dest/private/{providers/acvm_wasm_with_blobs.d.ts → acvm_wasm_with_blobs.d.ts} +5 -5
- package/dest/private/acvm_wasm_with_blobs.d.ts.map +1 -0
- package/dest/private/{providers/acvm_wasm_with_blobs.js → acvm_wasm_with_blobs.js} +2 -2
- package/dest/private/{providers/circuit_recording → circuit_recording}/circuit_recorder.d.ts +41 -21
- package/dest/private/circuit_recording/circuit_recorder.d.ts.map +1 -0
- package/dest/private/circuit_recording/circuit_recorder.js +209 -0
- package/dest/private/circuit_recording/file_circuit_recorder.d.ts +31 -0
- package/dest/private/circuit_recording/file_circuit_recorder.d.ts.map +1 -0
- package/dest/private/circuit_recording/file_circuit_recorder.js +135 -0
- package/dest/private/circuit_recording/memory_circuit_recorder.d.ts +5 -0
- package/dest/private/circuit_recording/memory_circuit_recorder.d.ts.map +1 -0
- package/dest/private/circuit_recording/memory_circuit_recorder.js +9 -0
- package/dest/private/{providers/circuit_recording/simulation_provider_recorder_wrapper.d.ts → circuit_recording/simulator_recorder_wrapper.d.ts} +9 -7
- package/dest/private/circuit_recording/simulator_recorder_wrapper.d.ts.map +1 -0
- package/dest/private/{providers/circuit_recording/simulation_provider_recorder_wrapper.js → circuit_recording/simulator_recorder_wrapper.js} +18 -13
- package/dest/private/{providers/simulation_provider.d.ts → circuit_simulator.d.ts} +4 -4
- package/dest/private/circuit_simulator.d.ts.map +1 -0
- package/dest/private/{providers/simulation_provider.js → circuit_simulator.js} +1 -1
- package/dest/private/factory.d.ts +12 -0
- package/dest/private/factory.d.ts.map +1 -0
- package/dest/private/{providers/factory.js → factory.js} +2 -2
- 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 +6 -3
- package/dest/server.d.ts.map +1 -1
- package/dest/server.js +5 -2
- 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 +5 -3
- package/src/private/acvm/acvm.ts +5 -3
- package/src/private/acvm/index.ts +0 -1
- package/src/private/{providers/acvm_native.ts → acvm_native.ts} +4 -4
- package/src/private/{providers/acvm_wasm.ts → acvm_wasm.ts} +4 -4
- package/src/private/{providers/acvm_wasm_with_blobs.ts → acvm_wasm_with_blobs.ts} +5 -5
- package/src/private/circuit_recording/circuit_recorder.ts +260 -0
- package/src/private/circuit_recording/file_circuit_recorder.ts +158 -0
- package/src/private/circuit_recording/memory_circuit_recorder.ts +11 -0
- package/src/private/{providers/circuit_recording/simulation_provider_recorder_wrapper.ts → circuit_recording/simulator_recorder_wrapper.ts} +26 -18
- package/src/private/{providers/simulation_provider.ts → circuit_simulator.ts} +3 -3
- package/src/private/{providers/factory.ts → factory.ts} +6 -6
- 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 +6 -3
- package/src/testing.ts +1 -1
- package/dest/private/acvm/oracle/index.d.ts +0 -14
- package/dest/private/acvm/oracle/index.d.ts.map +0 -1
- package/dest/private/acvm/oracle/index.js +0 -2
- package/dest/private/acvm/oracle/oracle.d.ts +0 -52
- package/dest/private/acvm/oracle/oracle.d.ts.map +0 -1
- package/dest/private/acvm/oracle/oracle.js +0 -312
- package/dest/private/acvm/oracle/typed_oracle.d.ts +0 -83
- package/dest/private/acvm/oracle/typed_oracle.d.ts.map +0 -1
- package/dest/private/acvm/oracle/typed_oracle.js +0 -138
- package/dest/private/execution_data_provider.d.ts +0 -301
- package/dest/private/execution_data_provider.d.ts.map +0 -1
- package/dest/private/execution_data_provider.js +0 -14
- package/dest/private/execution_note_cache.d.ts +0 -93
- package/dest/private/execution_note_cache.d.ts.map +0 -1
- package/dest/private/execution_note_cache.js +0 -180
- package/dest/private/hashed_values_cache.d.ts +0 -28
- package/dest/private/hashed_values_cache.d.ts.map +0 -1
- package/dest/private/hashed_values_cache.js +0 -36
- package/dest/private/index.d.ts +0 -16
- package/dest/private/index.d.ts.map +0 -1
- package/dest/private/index.js +0 -15
- package/dest/private/message_load_oracle_inputs.d.ts +0 -19
- package/dest/private/message_load_oracle_inputs.d.ts.map +0 -1
- package/dest/private/message_load_oracle_inputs.js +0 -24
- package/dest/private/pick_notes.d.ts +0 -85
- package/dest/private/pick_notes.d.ts.map +0 -1
- package/dest/private/pick_notes.js +0 -51
- package/dest/private/private_execution.d.ts +0 -25
- package/dest/private/private_execution.d.ts.map +0 -1
- package/dest/private/private_execution.js +0 -95
- package/dest/private/private_execution_oracle.d.ts +0 -187
- package/dest/private/private_execution_oracle.d.ts.map +0 -1
- package/dest/private/private_execution_oracle.js +0 -318
- package/dest/private/providers/acvm_native.d.ts.map +0 -1
- package/dest/private/providers/acvm_wasm.d.ts.map +0 -1
- package/dest/private/providers/acvm_wasm_with_blobs.d.ts.map +0 -1
- package/dest/private/providers/circuit_recording/circuit_recorder.d.ts.map +0 -1
- package/dest/private/providers/circuit_recording/circuit_recorder.js +0 -246
- package/dest/private/providers/circuit_recording/simulation_provider_recorder_wrapper.d.ts.map +0 -1
- package/dest/private/providers/factory.d.ts +0 -12
- package/dest/private/providers/factory.d.ts.map +0 -1
- package/dest/private/providers/simulation_provider.d.ts.map +0 -1
- package/dest/private/simulator.d.ts +0 -36
- package/dest/private/simulator.d.ts.map +0 -1
- package/dest/private/simulator.js +0 -104
- package/dest/private/utility_execution_oracle.d.ts +0 -163
- package/dest/private/utility_execution_oracle.d.ts.map +0 -1
- package/dest/private/utility_execution_oracle.js +0 -257
- package/dest/test/utils.d.ts +0 -13
- package/dest/test/utils.d.ts.map +0 -1
- package/dest/test/utils.js +0 -22
- package/src/private/acvm/oracle/index.ts +0 -16
- package/src/private/acvm/oracle/oracle.ts +0 -523
- package/src/private/acvm/oracle/typed_oracle.ts +0 -273
- package/src/private/execution_data_provider.ts +0 -388
- package/src/private/execution_note_cache.ts +0 -217
- package/src/private/hashed_values_cache.ts +0 -47
- package/src/private/index.ts +0 -19
- package/src/private/message_load_oracle_inputs.ts +0 -23
- package/src/private/pick_notes.ts +0 -141
- package/src/private/private_execution.ts +0 -159
- package/src/private/private_execution_oracle.ts +0 -508
- package/src/private/providers/circuit_recording/circuit_recorder.ts +0 -283
- package/src/private/simulator.ts +0 -170
- package/src/private/utility_execution_oracle.ts +0 -384
- package/src/test/utils.ts +0 -36
- /package/dest/private/{providers/acvm_native.js → acvm_native.js} +0 -0
|
@@ -1,283 +0,0 @@
|
|
|
1
|
-
import { createLogger } from '@aztec/foundation/log';
|
|
2
|
-
import type { ForeignCallHandler, ForeignCallInput, ForeignCallOutput } from '@aztec/noir-acvm_js';
|
|
3
|
-
|
|
4
|
-
import { createHash } from 'crypto';
|
|
5
|
-
import fs from 'fs/promises';
|
|
6
|
-
import path from 'path';
|
|
7
|
-
|
|
8
|
-
import type { ACIRCallback } from '../../acvm/acvm.js';
|
|
9
|
-
import type { ACVMWitness } from '../../acvm/acvm_types.js';
|
|
10
|
-
import { Oracle } from '../../acvm/oracle/oracle.js';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Class responsible for recording circuit inputs necessary to replay the circuit. These inputs are the initial witness
|
|
14
|
-
* map and the oracle calls made during the circuit execution/witness generation.
|
|
15
|
-
*
|
|
16
|
-
* The recording is stored in a JSON file called `circuit_name_circuit_function_name_YYYY-MM-DD_N.json` where N is
|
|
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:
|
|
21
|
-
* ```json
|
|
22
|
-
* {
|
|
23
|
-
* "circuitName": "AMM",
|
|
24
|
-
* "functionName": "add_liquidity",
|
|
25
|
-
* "bytecodeMd5Hash": "b46c640ed38f20eac5f61a5e41d8dd1e",
|
|
26
|
-
* "timestamp": 1740691464360,
|
|
27
|
-
* "inputs": {
|
|
28
|
-
* "0": "0x1e89de1f0ad5204263733b7ddf65bec45b8f44714a4da85a46474dad677679ef",
|
|
29
|
-
* "1": "0x00f4d59c0ff773427bb0fed5b422557ca4dc5655abe53d31fa9408cb3c5a672f",
|
|
30
|
-
* "5": "0x000000000000000000000000000000000000000000000000000000000000000f"
|
|
31
|
-
* },
|
|
32
|
-
* "oracleCalls": [
|
|
33
|
-
* {
|
|
34
|
-
* "name": "loadCapsule",
|
|
35
|
-
* "inputs": [
|
|
36
|
-
* [
|
|
37
|
-
* "0x102422483bad6abd385948435667e144ac4c272576e325e7563608876cd446fd"
|
|
38
|
-
* ],
|
|
39
|
-
* [
|
|
40
|
-
* "0x000000000000000000000000000000000000000000000000000000000000004d"
|
|
41
|
-
* ],
|
|
42
|
-
* [
|
|
43
|
-
* "0x0000000000000000000000000000000000000000000000000000000000000001"
|
|
44
|
-
* ]
|
|
45
|
-
* ],
|
|
46
|
-
* "outputs": [
|
|
47
|
-
* "0x0000000000000000000000000000000000000000000000000000000000000000",
|
|
48
|
-
* [
|
|
49
|
-
* "0x0000000000000000000000000000000000000000000000000000000000000000"
|
|
50
|
-
* ]
|
|
51
|
-
* ]
|
|
52
|
-
* },
|
|
53
|
-
* {
|
|
54
|
-
* "name": "syncPrivateState",
|
|
55
|
-
* "inputs": []
|
|
56
|
-
* }
|
|
57
|
-
* ]
|
|
58
|
-
* }
|
|
59
|
-
* ```
|
|
60
|
-
*/
|
|
61
|
-
export class CircuitRecorder {
|
|
62
|
-
private readonly logger = createLogger('simulator:acvm:recording');
|
|
63
|
-
private isFirstCall = true;
|
|
64
|
-
|
|
65
|
-
private constructor(private readonly filePath: string) {}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Initializes a new circuit recording session.
|
|
69
|
-
* @param recordDir - Directory to store the recording
|
|
70
|
-
* @param input - Circuit input witness
|
|
71
|
-
* @param circuitBytecode - Compiled circuit bytecode
|
|
72
|
-
* @param circuitName - Name of the circuit
|
|
73
|
-
* @param functionName - Name of the circuit function (defaults to 'main'). This is meaningful only for
|
|
74
|
-
* contracts as protocol circuits artifacts always contain a single entrypoint function called 'main'.
|
|
75
|
-
* @returns A new CircuitRecorder instance
|
|
76
|
-
*/
|
|
77
|
-
static async start(
|
|
78
|
-
recordDir: string,
|
|
79
|
-
input: ACVMWitness,
|
|
80
|
-
circuitBytecode: Buffer,
|
|
81
|
-
circuitName: string,
|
|
82
|
-
functionName: string = 'main',
|
|
83
|
-
): Promise<CircuitRecorder> {
|
|
84
|
-
const recording = {
|
|
85
|
-
circuitName: circuitName,
|
|
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
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const filePath = await CircuitRecorder.#computeFilePathAndStoreInitialRecording(
|
|
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
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* Wraps a callback to record all oracle/foreign calls.
|
|
155
|
-
* @param callback - The original callback to wrap, either a user circuit callback or protocol circuit callback.
|
|
156
|
-
* @returns A wrapped callback that records all oracle interactions.
|
|
157
|
-
*/
|
|
158
|
-
wrapCallback(callback: ACIRCallback | ForeignCallHandler | undefined): ACIRCallback | ForeignCallHandler | undefined {
|
|
159
|
-
if (!callback) {
|
|
160
|
-
return undefined;
|
|
161
|
-
}
|
|
162
|
-
if (this.#isACIRCallback(callback)) {
|
|
163
|
-
return this.#wrapUserCircuitCallback(callback);
|
|
164
|
-
}
|
|
165
|
-
return this.#wrapProtocolCircuitCallback(callback);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* Type guard to check if a callback is an ACIRCallback.
|
|
170
|
-
*/
|
|
171
|
-
#isACIRCallback(callback: ACIRCallback | ForeignCallHandler): callback is ACIRCallback {
|
|
172
|
-
return typeof callback === 'object' && callback !== null && !('call' in callback);
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Wraps a user circuit callback to record all oracle calls.
|
|
177
|
-
* @param callback - The original circuit callback.
|
|
178
|
-
* @returns A wrapped callback that records all oracle interactions which is to be provided to the ACVM.
|
|
179
|
-
*/
|
|
180
|
-
#wrapUserCircuitCallback(callback: ACIRCallback): ACIRCallback {
|
|
181
|
-
const recordingCallback: ACIRCallback = {} as ACIRCallback;
|
|
182
|
-
const oracleMethods = Object.getOwnPropertyNames(Oracle.prototype).filter(name => name !== 'constructor');
|
|
183
|
-
|
|
184
|
-
for (const name of oracleMethods) {
|
|
185
|
-
const fn = callback[name as keyof ACIRCallback];
|
|
186
|
-
if (!fn) {
|
|
187
|
-
throw new Error(`Oracle method ${name} not found when setting up recording callback`);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
recordingCallback[name as keyof ACIRCallback] = (...args: ForeignCallInput[]): ReturnType<typeof fn> => {
|
|
191
|
-
const result = fn.call(callback, ...args);
|
|
192
|
-
if (result instanceof Promise) {
|
|
193
|
-
return result.then(async r => {
|
|
194
|
-
await this.#recordCall(name, args, r);
|
|
195
|
-
return r;
|
|
196
|
-
}) as ReturnType<typeof fn>;
|
|
197
|
-
}
|
|
198
|
-
void this.#recordCall(name, args, result);
|
|
199
|
-
return result;
|
|
200
|
-
};
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
return recordingCallback;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* Wraps a protocol circuit callback to record all oracle calls.
|
|
208
|
-
* @param callback - The original oracle circuit callback.
|
|
209
|
-
* @returns A wrapped handler that records all oracle interactions which is to be provided to the ACVM.
|
|
210
|
-
*/
|
|
211
|
-
#wrapProtocolCircuitCallback(callback: ForeignCallHandler): ForeignCallHandler {
|
|
212
|
-
return async (name: string, inputs: ForeignCallInput[]): Promise<ForeignCallOutput[]> => {
|
|
213
|
-
const result = await callback(name, inputs);
|
|
214
|
-
await this.#recordCall(name, inputs, result);
|
|
215
|
-
return result;
|
|
216
|
-
};
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* Records a single oracle/foreign call with its inputs and outputs.
|
|
221
|
-
* @param name - Name of the call
|
|
222
|
-
* @param inputs - Input arguments
|
|
223
|
-
* @param outputs - Output results
|
|
224
|
-
*/
|
|
225
|
-
async #recordCall(name: string, inputs: unknown[], outputs: unknown) {
|
|
226
|
-
try {
|
|
227
|
-
const entry = {
|
|
228
|
-
name,
|
|
229
|
-
inputs,
|
|
230
|
-
outputs,
|
|
231
|
-
};
|
|
232
|
-
const prefix = this.isFirstCall ? ' ' : ' ,';
|
|
233
|
-
this.isFirstCall = false;
|
|
234
|
-
await fs.appendFile(this.filePath, prefix + JSON.stringify(entry) + '\n');
|
|
235
|
-
} catch (err) {
|
|
236
|
-
this.logger.error('Failed to log circuit call', { error: err });
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
/**
|
|
241
|
-
* Finalizes the recording file by adding closing brackets. Without calling this method, the recording file is
|
|
242
|
-
* incomplete and it fails to parse.
|
|
243
|
-
*/
|
|
244
|
-
async finish(): Promise<void> {
|
|
245
|
-
try {
|
|
246
|
-
await fs.appendFile(this.filePath, ' ]\n}\n');
|
|
247
|
-
} catch (err) {
|
|
248
|
-
this.logger.error('Failed to finalize recording file', { error: err });
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
/**
|
|
253
|
-
* Finalizes the recording file by adding the error and closing brackets. Without calling this method or `finish`,
|
|
254
|
-
* the recording file is incomplete and it fails to parse.
|
|
255
|
-
* @param error - The error that occurred during circuit execution
|
|
256
|
-
*/
|
|
257
|
-
async finishWithError(error: unknown): Promise<void> {
|
|
258
|
-
try {
|
|
259
|
-
await fs.appendFile(this.filePath, ' ],\n');
|
|
260
|
-
await fs.appendFile(this.filePath, ` "error": ${JSON.stringify(error)}\n`);
|
|
261
|
-
await fs.appendFile(this.filePath, '}\n');
|
|
262
|
-
} catch (err) {
|
|
263
|
-
this.logger.error('Failed to finalize recording file with error', { error: err });
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
}
|
|
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
|
-
}
|
package/src/private/simulator.ts
DELETED
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
import { Fr } from '@aztec/foundation/fields';
|
|
2
|
-
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
3
|
-
import type { AbiDecoded, FunctionCall } from '@aztec/stdlib/abi';
|
|
4
|
-
import { FunctionSelector, FunctionType, decodeFromAbi } from '@aztec/stdlib/abi';
|
|
5
|
-
import type { AuthWitness } from '@aztec/stdlib/auth-witness';
|
|
6
|
-
import { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
7
|
-
import { CallContext, HashedValues, PrivateExecutionResult, TxExecutionRequest, collectNested } from '@aztec/stdlib/tx';
|
|
8
|
-
|
|
9
|
-
import { ExecutionError, createSimulationError, resolveAssertionMessageFromError } from '../common/errors.js';
|
|
10
|
-
import { Oracle, extractCallStack, toACVMWitness, witnessMapToFields } from './acvm/index.js';
|
|
11
|
-
import type { ExecutionDataProvider } from './execution_data_provider.js';
|
|
12
|
-
import { ExecutionNoteCache } from './execution_note_cache.js';
|
|
13
|
-
import { HashedValuesCache } from './hashed_values_cache.js';
|
|
14
|
-
import { executePrivateFunction, verifyCurrentClassId } from './private_execution.js';
|
|
15
|
-
import { PrivateExecutionOracle } from './private_execution_oracle.js';
|
|
16
|
-
import type { SimulationProvider } from './providers/simulation_provider.js';
|
|
17
|
-
import { UtilityExecutionOracle } from './utility_execution_oracle.js';
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* The ACIR simulator.
|
|
21
|
-
*/
|
|
22
|
-
export class AcirSimulator {
|
|
23
|
-
private log: Logger;
|
|
24
|
-
|
|
25
|
-
constructor(
|
|
26
|
-
private executionDataProvider: ExecutionDataProvider,
|
|
27
|
-
private simulationProvider: SimulationProvider,
|
|
28
|
-
) {
|
|
29
|
-
this.log = createLogger('simulator');
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Runs a private function.
|
|
34
|
-
* @param request - The transaction request.
|
|
35
|
-
* @param entryPointArtifact - The artifact of the entry point function.
|
|
36
|
-
* @param contractAddress - The address of the contract (should match request.origin)
|
|
37
|
-
* @param msgSender - The address calling the function. This can be replaced to simulate a call from another contract or a specific account.
|
|
38
|
-
* @param scopes - The accounts whose notes we can access in this call. Currently optional and will default to all.
|
|
39
|
-
* @returns The result of the execution.
|
|
40
|
-
*/
|
|
41
|
-
public async run(
|
|
42
|
-
request: TxExecutionRequest,
|
|
43
|
-
contractAddress: AztecAddress,
|
|
44
|
-
selector: FunctionSelector,
|
|
45
|
-
msgSender = AztecAddress.fromField(Fr.MAX_FIELD_VALUE),
|
|
46
|
-
scopes?: AztecAddress[],
|
|
47
|
-
): Promise<PrivateExecutionResult> {
|
|
48
|
-
const header = await this.executionDataProvider.getBlockHeader();
|
|
49
|
-
|
|
50
|
-
await verifyCurrentClassId(contractAddress, this.executionDataProvider);
|
|
51
|
-
const entryPointArtifact = await this.executionDataProvider.getFunctionArtifact(contractAddress, selector);
|
|
52
|
-
|
|
53
|
-
if (entryPointArtifact.functionType !== FunctionType.PRIVATE) {
|
|
54
|
-
throw new Error(`Cannot run ${entryPointArtifact.functionType} function as private`);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
if (request.origin !== contractAddress) {
|
|
58
|
-
this.log.warn(
|
|
59
|
-
`Request origin does not match contract address in simulation. Request origin: ${request.origin}, contract address: ${contractAddress}`,
|
|
60
|
-
);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// reserve the first side effect for the tx hash (inserted by the private kernel)
|
|
64
|
-
const startSideEffectCounter = 1;
|
|
65
|
-
|
|
66
|
-
const callContext = new CallContext(
|
|
67
|
-
msgSender,
|
|
68
|
-
contractAddress,
|
|
69
|
-
await FunctionSelector.fromNameAndParameters(entryPointArtifact.name, entryPointArtifact.parameters),
|
|
70
|
-
entryPointArtifact.isStatic,
|
|
71
|
-
);
|
|
72
|
-
|
|
73
|
-
const txRequestHash = await request.toTxRequest().hash();
|
|
74
|
-
const noteCache = new ExecutionNoteCache(txRequestHash);
|
|
75
|
-
|
|
76
|
-
const context = new PrivateExecutionOracle(
|
|
77
|
-
request.firstCallArgsHash,
|
|
78
|
-
request.txContext,
|
|
79
|
-
callContext,
|
|
80
|
-
header,
|
|
81
|
-
request.authWitnesses,
|
|
82
|
-
request.capsules,
|
|
83
|
-
HashedValuesCache.create(request.argsOfCalls),
|
|
84
|
-
noteCache,
|
|
85
|
-
this.executionDataProvider,
|
|
86
|
-
this.simulationProvider,
|
|
87
|
-
/*totalPublicArgsCount=*/ 0,
|
|
88
|
-
startSideEffectCounter,
|
|
89
|
-
undefined,
|
|
90
|
-
scopes,
|
|
91
|
-
);
|
|
92
|
-
|
|
93
|
-
try {
|
|
94
|
-
const executionResult = await executePrivateFunction(
|
|
95
|
-
this.simulationProvider,
|
|
96
|
-
context,
|
|
97
|
-
entryPointArtifact,
|
|
98
|
-
contractAddress,
|
|
99
|
-
request.functionSelector,
|
|
100
|
-
);
|
|
101
|
-
const { usedTxRequestHashForNonces } = noteCache.finish();
|
|
102
|
-
const firstNullifierHint = usedTxRequestHashForNonces ? Fr.ZERO : noteCache.getAllNullifiers()[0];
|
|
103
|
-
|
|
104
|
-
const publicCallRequests = collectNested([executionResult], r => [
|
|
105
|
-
...r.publicInputs.publicCallRequests.map(r => r.inner),
|
|
106
|
-
r.publicInputs.publicTeardownCallRequest,
|
|
107
|
-
]).filter(r => !r.isEmpty());
|
|
108
|
-
const publicFunctionsCalldata = await Promise.all(
|
|
109
|
-
publicCallRequests.map(async r => {
|
|
110
|
-
const calldata = await context.loadFromExecutionCache(r.calldataHash);
|
|
111
|
-
return new HashedValues(calldata, r.calldataHash);
|
|
112
|
-
}),
|
|
113
|
-
);
|
|
114
|
-
|
|
115
|
-
return new PrivateExecutionResult(executionResult, firstNullifierHint, publicFunctionsCalldata);
|
|
116
|
-
} catch (err) {
|
|
117
|
-
throw createSimulationError(err instanceof Error ? err : new Error('Unknown error during private execution'));
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// docs:start:execute_utility_function
|
|
122
|
-
/**
|
|
123
|
-
* Runs a utility function.
|
|
124
|
-
* @param call - The function call to execute.
|
|
125
|
-
* @param authwits - Authentication witnesses required for the function call.
|
|
126
|
-
* @param scopes - Optional array of account addresses whose notes can be accessed in this call. Defaults to all
|
|
127
|
-
* accounts if not specified.
|
|
128
|
-
* @returns A decoded ABI value containing the function's return data.
|
|
129
|
-
*/
|
|
130
|
-
public async runUtility(call: FunctionCall, authwits: AuthWitness[], scopes?: AztecAddress[]): Promise<AbiDecoded> {
|
|
131
|
-
await verifyCurrentClassId(call.to, this.executionDataProvider);
|
|
132
|
-
const entryPointArtifact = await this.executionDataProvider.getFunctionArtifact(call.to, call.selector);
|
|
133
|
-
|
|
134
|
-
if (entryPointArtifact.functionType !== FunctionType.UTILITY) {
|
|
135
|
-
throw new Error(`Cannot run ${entryPointArtifact.functionType} function as utility`);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
const oracle = new UtilityExecutionOracle(call.to, authwits, [], this.executionDataProvider, undefined, scopes);
|
|
139
|
-
|
|
140
|
-
try {
|
|
141
|
-
this.log.verbose(`Executing utility function ${entryPointArtifact.name}`, {
|
|
142
|
-
contract: call.to,
|
|
143
|
-
selector: call.selector,
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
const initialWitness = toACVMWitness(0, call.args);
|
|
147
|
-
const acirExecutionResult = await this.simulationProvider
|
|
148
|
-
.executeUserCircuit(initialWitness, entryPointArtifact, new Oracle(oracle))
|
|
149
|
-
.catch((err: Error) => {
|
|
150
|
-
err.message = resolveAssertionMessageFromError(err, entryPointArtifact);
|
|
151
|
-
throw new ExecutionError(
|
|
152
|
-
err.message,
|
|
153
|
-
{
|
|
154
|
-
contractAddress: call.to,
|
|
155
|
-
functionSelector: call.selector,
|
|
156
|
-
},
|
|
157
|
-
extractCallStack(err, entryPointArtifact.debug),
|
|
158
|
-
{ cause: err },
|
|
159
|
-
);
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
const returnWitness = witnessMapToFields(acirExecutionResult.returnWitness);
|
|
163
|
-
this.log.verbose(`Utility simulation for ${call.to}.${call.selector} completed`);
|
|
164
|
-
return decodeFromAbi(entryPointArtifact.returnTypes, returnWitness);
|
|
165
|
-
} catch (err) {
|
|
166
|
-
throw createSimulationError(err instanceof Error ? err : new Error('Unknown error during private execution'));
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
// docs:end:execute_utility_function
|
|
170
|
-
}
|