@aztec/native 0.0.1-commit.2eb6648a → 0.0.1-commit.2f68f620

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.
@@ -3,7 +3,6 @@ import type { MessageReceiver } from './msgpack_channel.js';
3
3
  interface NativeClassCtor {
4
4
  new (...args: unknown[]): MessageReceiver;
5
5
  }
6
- export declare const NativeWorldState: NativeClassCtor;
7
6
  export declare const NativeLMDBStore: NativeClassCtor;
8
7
  /**
9
8
  * Contract provider interface for callbacks to fetch contract data.
@@ -76,26 +75,41 @@ export declare function createCancellationToken(): CancellationToken;
76
75
  * @param token - The cancellation token previously passed to avmSimulate
77
76
  */
78
77
  export declare function cancelSimulation(token: CancellationToken): void;
78
+ /**
79
+ * Maximum number of concurrent AVM simulations. Each simulation spawns a dedicated OS thread,
80
+ * so this controls resource usage. Defaults to 4. Set to 0 for unlimited.
81
+ */
82
+ export declare const AVM_MAX_CONCURRENT_SIMULATIONS: number;
79
83
  /**
80
84
  * AVM simulation function that takes serialized inputs and a contract provider.
81
85
  * The contract provider enables C++ to callback to TypeScript for contract data during simulation.
86
+ *
87
+ * Simulations run on dedicated std::threads (not the libuv thread pool), so there is no risk
88
+ * of libuv thread pool exhaustion or deadlock from C++ BlockingCall callbacks.
89
+ * Concurrency is limited by AVM_MAX_CONCURRENT_SIMULATIONS (default 4, 0 = unlimited).
90
+ *
82
91
  * @param inputs - Msgpack-serialized AvmFastSimulationInputs buffer
83
92
  * @param contractProvider - Object with callbacks for fetching contract instances and classes
84
- * @param worldStateHandle - Native handle to WorldState instance
93
+ * @param wsdbIpcPath - IPC path of the running aztec-wsdb process. The C++ AVM connects per
94
+ * simulation and constructs an IPC-backed merkle DB.
85
95
  * @param logLevel - Optional log level to control C++ verbosity (only used if loggerFunction is provided)
86
96
  * @param logger - Optional logger object for C++ logging callbacks
87
97
  * @param cancellationToken - Optional token to enable cancellation support
88
98
  * @returns Promise resolving to msgpack-serialized AvmCircuitPublicInputs buffer
89
99
  */
90
- export declare function avmSimulate(inputs: Buffer, contractProvider: ContractProvider, worldStateHandle: any, logLevel?: LogLevel, logger?: Logger, cancellationToken?: CancellationToken): Promise<Buffer>;
100
+ export declare function avmSimulate(inputs: Buffer, contractProvider: ContractProvider, wsdbIpcPath: string, logLevel?: LogLevel, logger?: Logger, cancellationToken?: CancellationToken): Promise<Buffer>;
91
101
  /**
92
102
  * AVM simulation function that uses pre-collected hints from TypeScript simulation.
93
103
  * All contract data and merkle tree hints are included in the AvmCircuitInputs, so no runtime
94
104
  * callbacks to TS or WS pointer are needed.
105
+ *
106
+ * Simulations run on dedicated std::threads (not the libuv thread pool).
107
+ * Concurrency is limited by AVM_MAX_CONCURRENT_SIMULATIONS (default 4, 0 = unlimited).
108
+ *
95
109
  * @param inputs - Msgpack-serialized AvmCircuitInputs (AvmProvingInputs in C++) buffer
96
110
  * @param logLevel - Log level to control C++ verbosity
97
111
  * @returns Promise resolving to msgpack-serialized simulation results buffer
98
112
  */
99
113
  export declare function avmSimulateWithHintedDbs(inputs: Buffer, logLevel?: LogLevel): Promise<Buffer>;
100
114
  export {};
101
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibmF0aXZlX21vZHVsZS5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vc3JjL25hdGl2ZV9tb2R1bGUudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQ0EsT0FBTyxFQUFFLEtBQUssUUFBUSxFQUFhLEtBQUssTUFBTSxFQUFFLE1BQU0sdUJBQXVCLENBQUM7QUFLOUUsT0FBTyxLQUFLLEVBQUUsZUFBZSxFQUFFLE1BQU0sc0JBQXNCLENBQUM7QUFFNUQsVUFBVSxlQUFlO0lBQ3ZCLEtBQUssR0FBRyxJQUFJLEVBQUUsT0FBTyxFQUFFLEdBQUcsZUFBZSxDQUFDO0NBQzNDO0FBYUQsZUFBTyxNQUFNLGdCQUFnQ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CAE/D;AAED;;;GAGG;AACH,eAAO,MAAM,8BAA8B,QAAkE,CAAC;AAgB9G;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,WAAW,CACzB,MAAM,EAAE,MAAM,EACd,gBAAgB,EAAE,gBAAgB,EAClC,WAAW,EAAE,MAAM,EACnB,QAAQ,GAAE,QAAiB,EAC3B,MAAM,CAAC,EAAE,MAAM,EACf,iBAAiB,CAAC,EAAE,iBAAiB,GACpC,OAAO,CAAC,MAAM,CAAC,CAWjB;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,wBAAwB,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,GAAE,QAAiB,GAAG,OAAO,CAAC,MAAM,CAAC,CAErG"}
@@ -11,7 +11,6 @@ function loadNativeModule() {
11
11
  return require(napiPath);
12
12
  }
13
13
  const nativeModule = loadNativeModule();
14
- export const NativeWorldState = nativeModule.WorldState;
15
14
  export const NativeLMDBStore = nativeModule.LMDBStore;
16
15
  // Internal native functions with numeric log level
17
16
  const nativeAvmSimulate = nativeModule.avmSimulate;
@@ -33,48 +32,51 @@ const nativeCancelSimulation = nativeModule.cancelSimulation;
33
32
  nativeCancelSimulation(token);
34
33
  }
35
34
  /**
36
- * Concurrency limiting for C++ AVM simulation to prevent libuv thread pool exhaustion.
37
- *
38
- * The C++ simulator uses NAPI BlockingCall to callback to TypeScript for contract data.
39
- * This blocks the libuv thread while waiting for the callback to complete. If all libuv
40
- * threads are blocked waiting for callbacks, no threads remain to service those callbacks,
41
- * causing deadlock.
42
- *
43
- * We limit concurrent simulations to UV_THREADPOOL_SIZE / 2 to ensure threads remain
44
- * available for callback processing.
45
- */ const UV_THREADPOOL_SIZE = parseInt(process.env.UV_THREADPOOL_SIZE ?? '4', 10);
46
- const MAX_CONCURRENT_AVM_SIMULATIONS = Math.max(1, Math.floor(UV_THREADPOOL_SIZE / 2));
47
- const avmSimulationSemaphore = new Semaphore(MAX_CONCURRENT_AVM_SIMULATIONS);
35
+ * Maximum number of concurrent AVM simulations. Each simulation spawns a dedicated OS thread,
36
+ * so this controls resource usage. Defaults to 4. Set to 0 for unlimited.
37
+ */ export const AVM_MAX_CONCURRENT_SIMULATIONS = parseInt(process.env.AVM_MAX_CONCURRENT_SIMULATIONS ?? '4', 10);
38
+ const avmSimulationSemaphore = AVM_MAX_CONCURRENT_SIMULATIONS > 0 ? new Semaphore(AVM_MAX_CONCURRENT_SIMULATIONS) : null;
39
+ async function withAvmConcurrencyLimit(fn) {
40
+ if (!avmSimulationSemaphore) {
41
+ return fn();
42
+ }
43
+ await avmSimulationSemaphore.acquire();
44
+ try {
45
+ return await fn();
46
+ } finally{
47
+ avmSimulationSemaphore.release();
48
+ }
49
+ }
48
50
  /**
49
51
  * AVM simulation function that takes serialized inputs and a contract provider.
50
52
  * The contract provider enables C++ to callback to TypeScript for contract data during simulation.
53
+ *
54
+ * Simulations run on dedicated std::threads (not the libuv thread pool), so there is no risk
55
+ * of libuv thread pool exhaustion or deadlock from C++ BlockingCall callbacks.
56
+ * Concurrency is limited by AVM_MAX_CONCURRENT_SIMULATIONS (default 4, 0 = unlimited).
57
+ *
51
58
  * @param inputs - Msgpack-serialized AvmFastSimulationInputs buffer
52
59
  * @param contractProvider - Object with callbacks for fetching contract instances and classes
53
- * @param worldStateHandle - Native handle to WorldState instance
60
+ * @param wsdbIpcPath - IPC path of the running aztec-wsdb process. The C++ AVM connects per
61
+ * simulation and constructs an IPC-backed merkle DB.
54
62
  * @param logLevel - Optional log level to control C++ verbosity (only used if loggerFunction is provided)
55
63
  * @param logger - Optional logger object for C++ logging callbacks
56
64
  * @param cancellationToken - Optional token to enable cancellation support
57
65
  * @returns Promise resolving to msgpack-serialized AvmCircuitPublicInputs buffer
58
- */ export async function avmSimulate(inputs, contractProvider, worldStateHandle, logLevel = 'info', logger, cancellationToken) {
59
- await avmSimulationSemaphore.acquire();
60
- try {
61
- return await nativeAvmSimulate(inputs, contractProvider, worldStateHandle, LogLevels.indexOf(logLevel), logger ? (level, msg)=>logger[level](msg) : null, cancellationToken);
62
- } finally{
63
- avmSimulationSemaphore.release();
64
- }
66
+ */ export function avmSimulate(inputs, contractProvider, wsdbIpcPath, logLevel = 'info', logger, cancellationToken) {
67
+ return withAvmConcurrencyLimit(()=>nativeAvmSimulate(inputs, contractProvider, wsdbIpcPath, LogLevels.indexOf(logLevel), logger ? (level, msg)=>logger[level](msg) : null, cancellationToken));
65
68
  }
66
69
  /**
67
70
  * AVM simulation function that uses pre-collected hints from TypeScript simulation.
68
71
  * All contract data and merkle tree hints are included in the AvmCircuitInputs, so no runtime
69
72
  * callbacks to TS or WS pointer are needed.
73
+ *
74
+ * Simulations run on dedicated std::threads (not the libuv thread pool).
75
+ * Concurrency is limited by AVM_MAX_CONCURRENT_SIMULATIONS (default 4, 0 = unlimited).
76
+ *
70
77
  * @param inputs - Msgpack-serialized AvmCircuitInputs (AvmProvingInputs in C++) buffer
71
78
  * @param logLevel - Log level to control C++ verbosity
72
79
  * @returns Promise resolving to msgpack-serialized simulation results buffer
73
- */ export async function avmSimulateWithHintedDbs(inputs, logLevel = 'info') {
74
- await avmSimulationSemaphore.acquire();
75
- try {
76
- return await nativeAvmSimulateWithHintedDbs(inputs, LogLevels.indexOf(logLevel));
77
- } finally{
78
- avmSimulationSemaphore.release();
79
- }
80
+ */ export function avmSimulateWithHintedDbs(inputs, logLevel = 'info') {
81
+ return withAvmConcurrencyLimit(()=>nativeAvmSimulateWithHintedDbs(inputs, LogLevels.indexOf(logLevel)));
80
82
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aztec/native",
3
- "version": "0.0.1-commit.2eb6648a",
3
+ "version": "0.0.1-commit.2f68f620",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./dest/index.js"
@@ -15,8 +15,8 @@
15
15
  "../package.common.json"
16
16
  ],
17
17
  "dependencies": {
18
- "@aztec/bb.js": "0.0.1-commit.2eb6648a",
19
- "@aztec/foundation": "0.0.1-commit.2eb6648a",
18
+ "@aztec/bb.js": "0.0.1-commit.2f68f620",
19
+ "@aztec/foundation": "0.0.1-commit.2f68f620",
20
20
  "msgpackr": "^1.11.2"
21
21
  },
22
22
  "devDependencies": {
@@ -21,7 +21,6 @@ function loadNativeModule(): Record<string, NativeClassCtor> {
21
21
 
22
22
  const nativeModule: Record<string, NativeClassCtor | Function> = loadNativeModule();
23
23
 
24
- export const NativeWorldState: NativeClassCtor = nativeModule.WorldState as NativeClassCtor;
25
24
  export const NativeLMDBStore: NativeClassCtor = nativeModule.LMDBStore as NativeClassCtor;
26
25
 
27
26
  /**
@@ -88,7 +87,7 @@ export interface ContractProvider {
88
87
  const nativeAvmSimulate = nativeModule.avmSimulate as (
89
88
  inputs: Buffer,
90
89
  contractProvider: ContractProvider,
91
- worldStateHandle: any,
90
+ wsdbIpcPath: string,
92
91
  logLevel: number,
93
92
  logFunction?: any,
94
93
  cancellationToken?: any,
@@ -128,68 +127,74 @@ export function cancelSimulation(token: CancellationToken): void {
128
127
  }
129
128
 
130
129
  /**
131
- * Concurrency limiting for C++ AVM simulation to prevent libuv thread pool exhaustion.
132
- *
133
- * The C++ simulator uses NAPI BlockingCall to callback to TypeScript for contract data.
134
- * This blocks the libuv thread while waiting for the callback to complete. If all libuv
135
- * threads are blocked waiting for callbacks, no threads remain to service those callbacks,
136
- * causing deadlock.
137
- *
138
- * We limit concurrent simulations to UV_THREADPOOL_SIZE / 2 to ensure threads remain
139
- * available for callback processing.
130
+ * Maximum number of concurrent AVM simulations. Each simulation spawns a dedicated OS thread,
131
+ * so this controls resource usage. Defaults to 4. Set to 0 for unlimited.
140
132
  */
141
- const UV_THREADPOOL_SIZE = parseInt(process.env.UV_THREADPOOL_SIZE ?? '4', 10);
142
- const MAX_CONCURRENT_AVM_SIMULATIONS = Math.max(1, Math.floor(UV_THREADPOOL_SIZE / 2));
143
- const avmSimulationSemaphore = new Semaphore(MAX_CONCURRENT_AVM_SIMULATIONS);
133
+ export const AVM_MAX_CONCURRENT_SIMULATIONS = parseInt(process.env.AVM_MAX_CONCURRENT_SIMULATIONS ?? '4', 10);
134
+ const avmSimulationSemaphore =
135
+ AVM_MAX_CONCURRENT_SIMULATIONS > 0 ? new Semaphore(AVM_MAX_CONCURRENT_SIMULATIONS) : null;
136
+
137
+ async function withAvmConcurrencyLimit<T>(fn: () => Promise<T>): Promise<T> {
138
+ if (!avmSimulationSemaphore) {
139
+ return fn();
140
+ }
141
+ await avmSimulationSemaphore.acquire();
142
+ try {
143
+ return await fn();
144
+ } finally {
145
+ avmSimulationSemaphore.release();
146
+ }
147
+ }
144
148
 
145
149
  /**
146
150
  * AVM simulation function that takes serialized inputs and a contract provider.
147
151
  * The contract provider enables C++ to callback to TypeScript for contract data during simulation.
152
+ *
153
+ * Simulations run on dedicated std::threads (not the libuv thread pool), so there is no risk
154
+ * of libuv thread pool exhaustion or deadlock from C++ BlockingCall callbacks.
155
+ * Concurrency is limited by AVM_MAX_CONCURRENT_SIMULATIONS (default 4, 0 = unlimited).
156
+ *
148
157
  * @param inputs - Msgpack-serialized AvmFastSimulationInputs buffer
149
158
  * @param contractProvider - Object with callbacks for fetching contract instances and classes
150
- * @param worldStateHandle - Native handle to WorldState instance
159
+ * @param wsdbIpcPath - IPC path of the running aztec-wsdb process. The C++ AVM connects per
160
+ * simulation and constructs an IPC-backed merkle DB.
151
161
  * @param logLevel - Optional log level to control C++ verbosity (only used if loggerFunction is provided)
152
162
  * @param logger - Optional logger object for C++ logging callbacks
153
163
  * @param cancellationToken - Optional token to enable cancellation support
154
164
  * @returns Promise resolving to msgpack-serialized AvmCircuitPublicInputs buffer
155
165
  */
156
- export async function avmSimulate(
166
+ export function avmSimulate(
157
167
  inputs: Buffer,
158
168
  contractProvider: ContractProvider,
159
- worldStateHandle: any,
169
+ wsdbIpcPath: string,
160
170
  logLevel: LogLevel = 'info',
161
171
  logger?: Logger,
162
172
  cancellationToken?: CancellationToken,
163
173
  ): Promise<Buffer> {
164
- await avmSimulationSemaphore.acquire();
165
-
166
- try {
167
- return await nativeAvmSimulate(
174
+ return withAvmConcurrencyLimit(() =>
175
+ nativeAvmSimulate(
168
176
  inputs,
169
177
  contractProvider,
170
- worldStateHandle,
178
+ wsdbIpcPath,
171
179
  LogLevels.indexOf(logLevel),
172
180
  logger ? (level: LogLevel, msg: string) => logger[level](msg) : null,
173
181
  cancellationToken,
174
- );
175
- } finally {
176
- avmSimulationSemaphore.release();
177
- }
182
+ ),
183
+ );
178
184
  }
179
185
 
180
186
  /**
181
187
  * AVM simulation function that uses pre-collected hints from TypeScript simulation.
182
188
  * All contract data and merkle tree hints are included in the AvmCircuitInputs, so no runtime
183
189
  * callbacks to TS or WS pointer are needed.
190
+ *
191
+ * Simulations run on dedicated std::threads (not the libuv thread pool).
192
+ * Concurrency is limited by AVM_MAX_CONCURRENT_SIMULATIONS (default 4, 0 = unlimited).
193
+ *
184
194
  * @param inputs - Msgpack-serialized AvmCircuitInputs (AvmProvingInputs in C++) buffer
185
195
  * @param logLevel - Log level to control C++ verbosity
186
196
  * @returns Promise resolving to msgpack-serialized simulation results buffer
187
197
  */
188
- export async function avmSimulateWithHintedDbs(inputs: Buffer, logLevel: LogLevel = 'info'): Promise<Buffer> {
189
- await avmSimulationSemaphore.acquire();
190
- try {
191
- return await nativeAvmSimulateWithHintedDbs(inputs, LogLevels.indexOf(logLevel));
192
- } finally {
193
- avmSimulationSemaphore.release();
194
- }
198
+ export function avmSimulateWithHintedDbs(inputs: Buffer, logLevel: LogLevel = 'info'): Promise<Buffer> {
199
+ return withAvmConcurrencyLimit(() => nativeAvmSimulateWithHintedDbs(inputs, LogLevels.indexOf(logLevel)));
195
200
  }