@aztec/native 3.0.0-devnet.6 → 3.0.0-devnet.6-patch.1

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/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  export * from './native_module.js';
2
2
  export { type RoundtripDuration, MsgpackChannel } from './msgpack_channel.js';
3
- //# sourceMappingURL=index.d.ts.map
3
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxjQUFjLG9CQUFvQixDQUFDO0FBQ25DLE9BQU8sRUFBRSxLQUFLLGlCQUFpQixFQUFFLGNBQWMsRUFBRSxNQUFNLHNCQUFzQixDQUFDIn0=
@@ -22,4 +22,4 @@ export declare class MsgpackChannel<M extends number = number, Req extends Messa
22
22
  }>;
23
23
  }
24
24
  export {};
25
- //# sourceMappingURL=msgpack_channel.d.ts.map
25
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibXNncGFja19jaGFubmVsLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvbXNncGFja19jaGFubmVsLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQU1BLE1BQU0sV0FBVyxlQUFlO0lBQzlCLElBQUksQ0FBQyxHQUFHLEVBQUUsTUFBTSxHQUFHLFVBQVUsR0FBRyxPQUFPLENBQUMsTUFBTSxHQUFHLFVBQVUsQ0FBQyxDQUFDO0NBQzlEO0FBRUQsTUFBTSxNQUFNLGlCQUFpQixHQUFHO0lBQzlCLFVBQVUsRUFBRSxNQUFNLENBQUM7SUFDbkIsTUFBTSxFQUFFLE1BQU0sQ0FBQztJQUNmLFVBQVUsRUFBRSxNQUFNL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,MAAM,EACzB,GAAG,SAAS,WAAW,CAAC,CAAC,CAAC,GAAG,GAAG,EAChC,IAAI,SAAS,WAAW,CAAC,CAAC,CAAC,GAAG,GAAG;IAYd,OAAO,CAAC,IAAI;IAV/B,mCAAmC;IACnC,OAAO,CAAC,OAAO,CAKZ;IAEH,OAAO,CAAC,KAAK,CAAK;IAElB,YAA2B,IAAI,EAAE,eAAe,EAAI;IAEvC,WAAW,CAAC,CAAC,SAAS,CAAC,EAClC,OAAO,EAAE,CAAC,EACV,IAAI,EAAE,GAAG,CAAC,CAAC,CAAC,GACX,OAAO,CAAC;QAAE,QAAQ,EAAE,iBAAiB,CAAC;QAAC,QAAQ,EAAE,IAAI,CAAC,CAAC,CAAC,CAAA;KAAE,CAAC,CA0D7D;CACF"}
@@ -1,4 +1,4 @@
1
- import { Fr } from '@aztec/foundation/fields';
1
+ import { Fr } from '@aztec/foundation/curves/bn254';
2
2
  import { MessageHeader, TypedMessage } from '@aztec/foundation/message';
3
3
  import { Encoder, addExtension } from 'msgpackr';
4
4
  import { isAnyArrayBuffer } from 'util/types';
@@ -1,16 +1,81 @@
1
+ import { type LogLevel } from '@aztec/foundation/log';
1
2
  import type { MessageReceiver } from './msgpack_channel.js';
2
3
  interface NativeClassCtor {
3
4
  new (...args: unknown[]): MessageReceiver;
4
5
  }
5
6
  export declare const NativeWorldState: NativeClassCtor;
6
7
  export declare const NativeLMDBStore: NativeClassCtor;
8
+ /**
9
+ * Contract provider interface for callbacks to fetch contract data.
10
+ * These callbacks are invoked by C++ during simulation when contract data is needed.
11
+ */
12
+ export interface ContractProvider {
13
+ /**
14
+ * Fetch a contract instance by address.
15
+ * @param address - The contract address as a string (hex format)
16
+ * @returns Promise resolving to msgpack-serialized ContractInstanceHint buffer, or undefined if not found
17
+ */
18
+ getContractInstance(address: string): Promise<Buffer | undefined>;
19
+ /**
20
+ * Fetch a contract class by class ID.
21
+ * @param classId - The contract class ID as a string (hex format)
22
+ * @returns Promise resolving to msgpack-serialized ContractClassHint buffer, or undefined if not found
23
+ */
24
+ getContractClass(classId: string): Promise<Buffer | undefined>;
25
+ /**
26
+ * Add contracts from deployment data.
27
+ * @param contractDeploymentData - Msgpack-serialized ContractDeploymentData buffer
28
+ * @returns Promise that resolves when contracts are added
29
+ */
30
+ addContracts(contractDeploymentData: Buffer): Promise<void>;
31
+ /**
32
+ * Fetch the bytecode commitment for a contract class.
33
+ * @param classId - The contract class ID as a string (hex format)
34
+ * @returns Promise resolving to msgpack-serialized Fr buffer, or undefined if not found
35
+ */
36
+ getBytecodeCommitment(classId: string): Promise<Buffer | undefined>;
37
+ /**
38
+ * Fetch the debug function name for a contract function.
39
+ * @param address - The contract address as a string (hex format)
40
+ * @param selector - The function selector as a string (hex format)
41
+ * @returns Promise resolving to function name string, or undefined if not found
42
+ */
43
+ getDebugFunctionName(address: string, selector: string): Promise<string | undefined>;
44
+ /**
45
+ * Create a new checkpoint for the contract database state.
46
+ * Enables rollback to current state in case of a revert.
47
+ * @returns Promise that resolves when checkpoint is created
48
+ */
49
+ createCheckpoint(): Promise<void>;
50
+ /**
51
+ * Commit the current checkpoint, accepting its state as latest.
52
+ * @returns Promise that resolves when checkpoint is committed
53
+ */
54
+ commitCheckpoint(): Promise<void>;
55
+ /**
56
+ * Revert the current checkpoint, discarding its state and rolling back.
57
+ * @returns Promise that resolves when checkpoint is reverted
58
+ */
59
+ revertCheckpoint(): Promise<void>;
60
+ }
61
+ /**
62
+ * AVM simulation function that takes serialized inputs and a contract provider.
63
+ * The contract provider enables C++ to callback to TypeScript for contract data during simulation.
64
+ * @param inputs - Msgpack-serialized AvmFastSimulationInputs buffer
65
+ * @param contractProvider - Object with callbacks for fetching contract instances and classes
66
+ * @param worldStateHandle - Native handle to WorldState instance
67
+ * @param logLevel - Log level to control C++ verbosity
68
+ * @returns Promise resolving to msgpack-serialized AvmCircuitPublicInputs buffer
69
+ */
70
+ export declare function avmSimulate(inputs: Buffer, contractProvider: ContractProvider, worldStateHandle: any, logLevel?: LogLevel): Promise<Buffer>;
7
71
  /**
8
72
  * AVM simulation function that uses pre-collected hints from TypeScript simulation.
9
73
  * All contract data and merkle tree hints are included in the AvmCircuitInputs, so no runtime
10
74
  * callbacks to TS or WS pointer are needed.
11
75
  * @param inputs - Msgpack-serialized AvmCircuitInputs (AvmProvingInputs in C++) buffer
76
+ * @param logLevel - Log level to control C++ verbosity
12
77
  * @returns Promise resolving to msgpack-serialized simulation results buffer
13
78
  */
14
- export declare const avmSimulateWithHintedDbs: (inputs: Buffer) => Promise<Buffer>;
79
+ export declare function avmSimulateWithHintedDbs(inputs: Buffer, logLevel?: LogLevel): Promise<Buffer>;
15
80
  export {};
16
- //# sourceMappingURL=native_module.d.ts.map
81
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibmF0aXZlX21vZHVsZS5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vc3JjL25hdGl2ZV9tb2R1bGUudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQ0EsT0FBTyxFQUFFLEtBQUssUUFBUSxFQUFhLE1BQU0sdUJBQXVCLENBQUM7QUFLakUsT0FBTyxLQUFLLEVBQUUsZUFBZSxFQUFFLE1BQU0sc0JBQXNCLENBQUM7QUFFNUQsVUFBVSxlQUFlO0lBQ3ZCLEtBQUssR0FBRyxJQUFJLEVBQUUsT0FBTyxFQUFFLEdBQUcsZUFBZSxDQUFDO0NBQzNDO0FBYUQsZUFBTyxNQUFNLGdCQUFnQixFQUFFLGVBQTRELENBQUM7QUFDNUYsZUFBTyxNQUFNLGVBQWUsRUFBRSxlQUEyRCxDQUFDO0FBRTFGOzs7R0FHRztBQUNILE1BQU0sV0FBVyxnQkFBZ0I7SUFDL0I7Ozs7T0FJRztJQUNILG1CQUFtQixDQUFDLE9BQU8sRUFBRSxNQUFNLEdBQUcsT0FBTyxDQUFDLE1BQU0sR0FBRyxTQUFTLENBQUMsQ0FBQztJQUNsRT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xDQUFDLENBTzNHIn0=
@@ -1 +1 @@
1
- {"version":3,"file":"native_module.d.ts","sourceRoot":"","sources":["../src/native_module.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAE5D,UAAU,eAAe;IACvB,KAAK,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,eAAe,CAAC;CAC3C;AA2BD,eAAO,MAAM,gBAAgB,EAAE,eAA4D,CAAC;AAC5F,eAAO,MAAM,eAAe,EAAE,eAA2D,CAAC;AAE1F;;;;;;GAMG;AACH,eAAO,MAAM,wBAAwB,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAErD,CAAC"}
1
+ {"version":3,"file":"native_module.d.ts","sourceRoot":"","sources":["../src/native_module.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,QAAQ,EAAa,MAAM,uBAAuB,CAAC;AAKjE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAE5D,UAAU,eAAe;IACvB,KAAK,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,eAAe,CAAC;CAC3C;AAaD,eAAO,MAAM,gBAAgB,EAAE,eAA4D,CAAC;AAC5F,eAAO,MAAM,eAAe,EAAE,eAA2D,CAAC;AAE1F;;;GAGG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;;;OAIG;IACH,mBAAmB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;IAClE;;;;OAIG;IACH,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;IAE/D;;;;OAIG;IACH,YAAY,CAAC,sBAAsB,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAE5D;;;;OAIG;IACH,qBAAqB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;IAEpE;;;;;OAKG;IACH,oBAAoB,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;IAErF;;;;OAIG;IACH,gBAAgB,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAElC;;;OAGG;IACH,gBAAgB,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAElC;;;OAGG;IACH,gBAAgB,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACnC;AA8BD;;;;;;;;GAQG;AACH,wBAAsB,WAAW,CAC/B,MAAM,EAAE,MAAM,EACd,gBAAgB,EAAE,gBAAgB,EAClC,gBAAgB,EAAE,GAAG,EACrB,QAAQ,GAAE,QAAiB,GAC1B,OAAO,CAAC,MAAM,CAAC,CAOjB;AAED;;;;;;;GAOG;AACH,wBAAsB,wBAAwB,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,GAAE,QAAiB,GAAG,OAAO,CAAC,MAAM,CAAC,CAO3G"}
@@ -1,28 +1,62 @@
1
+ import { findNapiBinary } from '@aztec/bb.js';
2
+ import { LogLevels } from '@aztec/foundation/log';
3
+ import { Semaphore } from '@aztec/foundation/queue';
1
4
  import { createRequire } from 'module';
2
- import { dirname, join } from 'path';
3
- import { fileURLToPath } from 'url';
4
5
  function loadNativeModule() {
5
- const __filename = fileURLToPath(import.meta.url);
6
- const __dirname = dirname(__filename);
7
- // Map Node.js platform/arch to build directory names
8
- const arch = process.arch === 'x64' ? 'amd64' : process.arch;
9
- const platform = process.platform === 'darwin' ? 'macos' : process.platform;
10
- const variant = `${arch}-${platform}`;
11
- const modulePath = join(__dirname, '..', 'build', variant, 'nodejs_module.node');
12
- try {
13
- const require = createRequire(import.meta.url);
14
- return require(modulePath);
15
- } catch (error) {
16
- throw new Error(`Failed to load native module for ${variant} from ${modulePath}. ` + `Supported: amd64-linux, arm64-linux, amd64-macos, arm64-macos. ` + `Error: ${error}`);
6
+ const require = createRequire(import.meta.url);
7
+ const napiPath = findNapiBinary();
8
+ if (!napiPath) {
9
+ throw new Error('NAPI binary not found for current platform.');
17
10
  }
11
+ return require(napiPath);
18
12
  }
19
13
  const nativeModule = loadNativeModule();
20
14
  export const NativeWorldState = nativeModule.WorldState;
21
15
  export const NativeLMDBStore = nativeModule.LMDBStore;
16
+ // Internal native functions with numeric log level
17
+ const nativeAvmSimulate = nativeModule.avmSimulate;
18
+ const nativeAvmSimulateWithHintedDbs = nativeModule.avmSimulateWithHintedDbs;
19
+ /**
20
+ * Concurrency limiting for C++ AVM simulation to prevent libuv thread pool exhaustion.
21
+ *
22
+ * The C++ simulator uses NAPI BlockingCall to callback to TypeScript for contract data.
23
+ * This blocks the libuv thread while waiting for the callback to complete. If all libuv
24
+ * threads are blocked waiting for callbacks, no threads remain to service those callbacks,
25
+ * causing deadlock.
26
+ *
27
+ * We limit concurrent simulations to UV_THREADPOOL_SIZE / 2 to ensure threads remain
28
+ * available for callback processing.
29
+ */ const UV_THREADPOOL_SIZE = parseInt(process.env.UV_THREADPOOL_SIZE ?? '4', 10);
30
+ const MAX_CONCURRENT_AVM_SIMULATIONS = Math.max(1, Math.floor(UV_THREADPOOL_SIZE / 2));
31
+ const avmSimulationSemaphore = new Semaphore(MAX_CONCURRENT_AVM_SIMULATIONS);
32
+ /**
33
+ * AVM simulation function that takes serialized inputs and a contract provider.
34
+ * The contract provider enables C++ to callback to TypeScript for contract data during simulation.
35
+ * @param inputs - Msgpack-serialized AvmFastSimulationInputs buffer
36
+ * @param contractProvider - Object with callbacks for fetching contract instances and classes
37
+ * @param worldStateHandle - Native handle to WorldState instance
38
+ * @param logLevel - Log level to control C++ verbosity
39
+ * @returns Promise resolving to msgpack-serialized AvmCircuitPublicInputs buffer
40
+ */ export async function avmSimulate(inputs, contractProvider, worldStateHandle, logLevel = 'info') {
41
+ await avmSimulationSemaphore.acquire();
42
+ try {
43
+ return await nativeAvmSimulate(inputs, contractProvider, worldStateHandle, LogLevels.indexOf(logLevel));
44
+ } finally{
45
+ avmSimulationSemaphore.release();
46
+ }
47
+ }
22
48
  /**
23
49
  * AVM simulation function that uses pre-collected hints from TypeScript simulation.
24
50
  * All contract data and merkle tree hints are included in the AvmCircuitInputs, so no runtime
25
51
  * callbacks to TS or WS pointer are needed.
26
52
  * @param inputs - Msgpack-serialized AvmCircuitInputs (AvmProvingInputs in C++) buffer
53
+ * @param logLevel - Log level to control C++ verbosity
27
54
  * @returns Promise resolving to msgpack-serialized simulation results buffer
28
- */ export const avmSimulateWithHintedDbs = nativeModule.avmSimulateWithHintedDbs;
55
+ */ export async function avmSimulateWithHintedDbs(inputs, logLevel = 'info') {
56
+ await avmSimulationSemaphore.acquire();
57
+ try {
58
+ return await nativeAvmSimulateWithHintedDbs(inputs, LogLevels.indexOf(logLevel));
59
+ } finally{
60
+ avmSimulationSemaphore.release();
61
+ }
62
+ }
package/package.json CHANGED
@@ -1,31 +1,29 @@
1
1
  {
2
2
  "name": "@aztec/native",
3
- "version": "3.0.0-devnet.6",
3
+ "version": "3.0.0-devnet.6-patch.1",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./dest/index.js"
7
7
  },
8
8
  "scripts": {
9
- "build": "yarn clean && yarn generate && tsc -b",
10
- "build:dev": "tsc -b --watch",
11
- "build:cpp": "PROJECT=$(pwd); cd $(git rev-parse --show-toplevel)/barretenberg/cpp; cmake --preset ${PRESET:-clang16-pic} && cmake --build --preset ${PRESET:-clang16-pic} --target nodejs_module && cd $PROJECT && yarn generate",
12
- "clean:cpp": "rm -rf $(git rev-parse --show-toplevel)/barretenberg/cpp/build-pic",
9
+ "build": "yarn clean && ../scripts/tsc.sh",
10
+ "build:dev": "../scripts/tsc.sh --watch",
13
11
  "clean": "rm -rf ./dest .tsbuildinfo",
14
- "test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules ../node_modules/.bin/jest --passWithNoTests --maxWorkers=${JEST_MAX_WORKERS:-8}",
15
- "generate": "bash scripts/copy-modules.sh"
12
+ "test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules ../node_modules/.bin/jest --passWithNoTests --maxWorkers=${JEST_MAX_WORKERS:-8}"
16
13
  },
17
14
  "inherits": [
18
- "../package.common.json",
19
- "./package.local.json"
15
+ "../package.common.json"
20
16
  ],
21
17
  "dependencies": {
22
- "@aztec/foundation": "3.0.0-devnet.6",
18
+ "@aztec/bb.js": "3.0.0-devnet.6-patch.1",
19
+ "@aztec/foundation": "3.0.0-devnet.6-patch.1",
23
20
  "msgpackr": "^1.11.2"
24
21
  },
25
22
  "devDependencies": {
26
23
  "@jest/globals": "^30.0.0",
27
24
  "@types/jest": "^30.0.0",
28
25
  "@types/node": "^22.15.17",
26
+ "@typescript/native-preview": "7.0.0-dev.20251126.1",
29
27
  "jest": "^30.0.0",
30
28
  "ts-node": "^10.9.1",
31
29
  "typescript": "^5.3.3"
@@ -1,4 +1,4 @@
1
- import { Fr } from '@aztec/foundation/fields';
1
+ import { Fr } from '@aztec/foundation/curves/bn254';
2
2
  import { MessageHeader, TypedMessage } from '@aztec/foundation/message';
3
3
 
4
4
  import { Encoder, addExtension } from 'msgpackr';
@@ -1,6 +1,8 @@
1
+ import { findNapiBinary } from '@aztec/bb.js';
2
+ import { type LogLevel, LogLevels } from '@aztec/foundation/log';
3
+ import { Semaphore } from '@aztec/foundation/queue';
4
+
1
5
  import { createRequire } from 'module';
2
- import { dirname, join } from 'path';
3
- import { fileURLToPath } from 'url';
4
6
 
5
7
  import type { MessageReceiver } from './msgpack_channel.js';
6
8
 
@@ -9,26 +11,12 @@ interface NativeClassCtor {
9
11
  }
10
12
 
11
13
  function loadNativeModule(): Record<string, NativeClassCtor> {
12
- const __filename = fileURLToPath(import.meta.url);
13
- const __dirname = dirname(__filename);
14
-
15
- // Map Node.js platform/arch to build directory names
16
- const arch = process.arch === 'x64' ? 'amd64' : process.arch;
17
- const platform = process.platform === 'darwin' ? 'macos' : process.platform;
18
- const variant = `${arch}-${platform}`;
19
-
20
- const modulePath = join(__dirname, '..', 'build', variant, 'nodejs_module.node');
21
-
22
- try {
23
- const require = createRequire(import.meta.url);
24
- return require(modulePath);
25
- } catch (error) {
26
- throw new Error(
27
- `Failed to load native module for ${variant} from ${modulePath}. ` +
28
- `Supported: amd64-linux, arm64-linux, amd64-macos, arm64-macos. ` +
29
- `Error: ${error}`,
30
- );
14
+ const require = createRequire(import.meta.url);
15
+ const napiPath = findNapiBinary();
16
+ if (!napiPath) {
17
+ throw new Error('NAPI binary not found for current platform.');
31
18
  }
19
+ return require(napiPath);
32
20
  }
33
21
 
34
22
  const nativeModule: Record<string, NativeClassCtor | Function> = loadNativeModule();
@@ -36,13 +24,130 @@ const nativeModule: Record<string, NativeClassCtor | Function> = loadNativeModul
36
24
  export const NativeWorldState: NativeClassCtor = nativeModule.WorldState as NativeClassCtor;
37
25
  export const NativeLMDBStore: NativeClassCtor = nativeModule.LMDBStore as NativeClassCtor;
38
26
 
27
+ /**
28
+ * Contract provider interface for callbacks to fetch contract data.
29
+ * These callbacks are invoked by C++ during simulation when contract data is needed.
30
+ */
31
+ export interface ContractProvider {
32
+ /**
33
+ * Fetch a contract instance by address.
34
+ * @param address - The contract address as a string (hex format)
35
+ * @returns Promise resolving to msgpack-serialized ContractInstanceHint buffer, or undefined if not found
36
+ */
37
+ getContractInstance(address: string): Promise<Buffer | undefined>;
38
+ /**
39
+ * Fetch a contract class by class ID.
40
+ * @param classId - The contract class ID as a string (hex format)
41
+ * @returns Promise resolving to msgpack-serialized ContractClassHint buffer, or undefined if not found
42
+ */
43
+ getContractClass(classId: string): Promise<Buffer | undefined>;
44
+
45
+ /**
46
+ * Add contracts from deployment data.
47
+ * @param contractDeploymentData - Msgpack-serialized ContractDeploymentData buffer
48
+ * @returns Promise that resolves when contracts are added
49
+ */
50
+ addContracts(contractDeploymentData: Buffer): Promise<void>;
51
+
52
+ /**
53
+ * Fetch the bytecode commitment for a contract class.
54
+ * @param classId - The contract class ID as a string (hex format)
55
+ * @returns Promise resolving to msgpack-serialized Fr buffer, or undefined if not found
56
+ */
57
+ getBytecodeCommitment(classId: string): Promise<Buffer | undefined>;
58
+
59
+ /**
60
+ * Fetch the debug function name for a contract function.
61
+ * @param address - The contract address as a string (hex format)
62
+ * @param selector - The function selector as a string (hex format)
63
+ * @returns Promise resolving to function name string, or undefined if not found
64
+ */
65
+ getDebugFunctionName(address: string, selector: string): Promise<string | undefined>;
66
+
67
+ /**
68
+ * Create a new checkpoint for the contract database state.
69
+ * Enables rollback to current state in case of a revert.
70
+ * @returns Promise that resolves when checkpoint is created
71
+ */
72
+ createCheckpoint(): Promise<void>;
73
+
74
+ /**
75
+ * Commit the current checkpoint, accepting its state as latest.
76
+ * @returns Promise that resolves when checkpoint is committed
77
+ */
78
+ commitCheckpoint(): Promise<void>;
79
+
80
+ /**
81
+ * Revert the current checkpoint, discarding its state and rolling back.
82
+ * @returns Promise that resolves when checkpoint is reverted
83
+ */
84
+ revertCheckpoint(): Promise<void>;
85
+ }
86
+
87
+ // Internal native functions with numeric log level
88
+ const nativeAvmSimulate = nativeModule.avmSimulate as (
89
+ inputs: Buffer,
90
+ contractProvider: ContractProvider,
91
+ worldStateHandle: any,
92
+ logLevel: number,
93
+ ) => Promise<Buffer>;
94
+
95
+ const nativeAvmSimulateWithHintedDbs = nativeModule.avmSimulateWithHintedDbs as (
96
+ inputs: Buffer,
97
+ logLevel: number,
98
+ ) => Promise<Buffer>;
99
+
100
+ /**
101
+ * Concurrency limiting for C++ AVM simulation to prevent libuv thread pool exhaustion.
102
+ *
103
+ * The C++ simulator uses NAPI BlockingCall to callback to TypeScript for contract data.
104
+ * This blocks the libuv thread while waiting for the callback to complete. If all libuv
105
+ * threads are blocked waiting for callbacks, no threads remain to service those callbacks,
106
+ * causing deadlock.
107
+ *
108
+ * We limit concurrent simulations to UV_THREADPOOL_SIZE / 2 to ensure threads remain
109
+ * available for callback processing.
110
+ */
111
+ const UV_THREADPOOL_SIZE = parseInt(process.env.UV_THREADPOOL_SIZE ?? '4', 10);
112
+ const MAX_CONCURRENT_AVM_SIMULATIONS = Math.max(1, Math.floor(UV_THREADPOOL_SIZE / 2));
113
+ const avmSimulationSemaphore = new Semaphore(MAX_CONCURRENT_AVM_SIMULATIONS);
114
+
115
+ /**
116
+ * AVM simulation function that takes serialized inputs and a contract provider.
117
+ * The contract provider enables C++ to callback to TypeScript for contract data during simulation.
118
+ * @param inputs - Msgpack-serialized AvmFastSimulationInputs buffer
119
+ * @param contractProvider - Object with callbacks for fetching contract instances and classes
120
+ * @param worldStateHandle - Native handle to WorldState instance
121
+ * @param logLevel - Log level to control C++ verbosity
122
+ * @returns Promise resolving to msgpack-serialized AvmCircuitPublicInputs buffer
123
+ */
124
+ export async function avmSimulate(
125
+ inputs: Buffer,
126
+ contractProvider: ContractProvider,
127
+ worldStateHandle: any,
128
+ logLevel: LogLevel = 'info',
129
+ ): Promise<Buffer> {
130
+ await avmSimulationSemaphore.acquire();
131
+ try {
132
+ return await nativeAvmSimulate(inputs, contractProvider, worldStateHandle, LogLevels.indexOf(logLevel));
133
+ } finally {
134
+ avmSimulationSemaphore.release();
135
+ }
136
+ }
137
+
39
138
  /**
40
139
  * AVM simulation function that uses pre-collected hints from TypeScript simulation.
41
140
  * All contract data and merkle tree hints are included in the AvmCircuitInputs, so no runtime
42
141
  * callbacks to TS or WS pointer are needed.
43
142
  * @param inputs - Msgpack-serialized AvmCircuitInputs (AvmProvingInputs in C++) buffer
143
+ * @param logLevel - Log level to control C++ verbosity
44
144
  * @returns Promise resolving to msgpack-serialized simulation results buffer
45
145
  */
46
- export const avmSimulateWithHintedDbs: (inputs: Buffer) => Promise<Buffer> = nativeModule.avmSimulateWithHintedDbs as (
47
- inputs: Buffer,
48
- ) => Promise<Buffer>;
146
+ export async function avmSimulateWithHintedDbs(inputs: Buffer, logLevel: LogLevel = 'info'): Promise<Buffer> {
147
+ await avmSimulationSemaphore.acquire();
148
+ try {
149
+ return await nativeAvmSimulateWithHintedDbs(inputs, LogLevels.indexOf(logLevel));
150
+ } finally {
151
+ avmSimulationSemaphore.release();
152
+ }
153
+ }