Skip to content

Commit 7cbefbb

Browse files
committed
feat(avm): TS triggers C++ simulation with an associated log level
1 parent faf8cb1 commit 7cbefbb

File tree

5 files changed

+85
-32
lines changed

5 files changed

+85
-32
lines changed

barretenberg/cpp/src/barretenberg/nodejs_module/avm_simulate/avm_simulate_napi.cpp

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@
1414
namespace bb::nodejs {
1515

1616
namespace {
17+
// Log levels from TS foundation/src/log/log-levels.ts: ['silent', 'fatal', 'error', 'warn', 'info', 'verbose', 'debug',
18+
// 'trace'] Map: 0=silent, 1=fatal, 2=error, 3=warn, 4=info, 5=verbose, 6=debug, 7=trace
19+
constexpr int LOG_LEVEL_VERBOSE = 5;
20+
constexpr int LOG_LEVEL_TRACE = 7;
21+
22+
// Helper to set logging flags based on TS log level
23+
inline void set_logging_from_level(int log_level)
24+
{
25+
// Turn verbose_logging on if log level is verbose (5) or above
26+
verbose_logging = (log_level >= LOG_LEVEL_VERBOSE);
27+
// Turn debug_logging on if log level is trace (7) or above
28+
debug_logging = (log_level >= LOG_LEVEL_TRACE);
29+
}
30+
1731
// Callback method names
1832
constexpr const char* CALLBACK_GET_CONTRACT_INSTANCE = "getContractInstance";
1933
constexpr const char* CALLBACK_GET_CONTRACT_CLASS = "getContractClass";
@@ -100,20 +114,17 @@ struct ContractCallbacks {
100114

101115
Napi::Value AvmSimulateNapi::simulate(const Napi::CallbackInfo& cb_info)
102116
{
103-
// TODO(dbanks12): configurable verbosity (maybe based on TS log level)
104-
// verbose_logging = true;
105-
// debug_logging = true;
106-
107117
Napi::Env env = cb_info.Env();
108118

109-
// Validate arguments - expects 3 arguments
119+
// Validate arguments - expects 4 arguments
110120
// arg[0]: inputs Buffer (required)
111121
// arg[1]: contractProvider object (required)
112122
// arg[2]: worldStateHandle external (required)
113-
if (cb_info.Length() < 3) {
123+
// arg[3]: logLevel number (required) - index into TS LogLevels array
124+
if (cb_info.Length() < 4) {
114125
throw Napi::TypeError::New(env,
115-
"Wrong number of arguments. Expected 3 arguments: inputs Buffer, contractProvider "
116-
"object, and worldStateHandle.");
126+
"Wrong number of arguments. Expected 4 arguments: inputs Buffer, contractProvider "
127+
"object, worldStateHandle, and logLevel.");
117128
}
118129

119130
if (!cb_info[0].IsBuffer()) {
@@ -129,6 +140,14 @@ Napi::Value AvmSimulateNapi::simulate(const Napi::CallbackInfo& cb_info)
129140
throw Napi::TypeError::New(env, "Third argument must be a WorldState handle (External)");
130141
}
131142

143+
if (!cb_info[3].IsNumber()) {
144+
throw Napi::TypeError::New(env, "Fourth argument must be a log level number (0-7)");
145+
}
146+
147+
// Extract log level and set logging flags
148+
int log_level = cb_info[3].As<Napi::Number>().Int32Value();
149+
set_logging_from_level(log_level);
150+
132151
// Extract the inputs buffer
133152
auto inputs_buffer = cb_info[0].As<Napi::Buffer<uint8_t>>();
134153
size_t length = inputs_buffer.Length();
@@ -215,24 +234,30 @@ Napi::Value AvmSimulateNapi::simulate(const Napi::CallbackInfo& cb_info)
215234

216235
Napi::Value AvmSimulateNapi::simulateWithHintedDbs(const Napi::CallbackInfo& cb_info)
217236
{
218-
// TODO(dbanks12): configurable verbosity (maybe based on TS log level)
219-
verbose_logging = true;
220-
debug_logging = true;
221-
222237
Napi::Env env = cb_info.Env();
223238

224-
// Validate arguments - expects 1 argument
239+
// Validate arguments - expects 2 arguments
225240
// arg[0]: inputs Buffer (required) - AvmProvingInputs
226-
if (cb_info.Length() < 1) {
227-
throw Napi::TypeError::New(
228-
env, "Wrong number of arguments. Expected 1 argument: AvmProvingInputs/AvmCircuitInputs msgpack Buffer.");
241+
// arg[1]: logLevel number (required) - index into TS LogLevels array
242+
if (cb_info.Length() < 2) {
243+
throw Napi::TypeError::New(env,
244+
"Wrong number of arguments. Expected 2 arguments: AvmProvingInputs/AvmCircuitInputs "
245+
"msgpack Buffer and logLevel.");
229246
}
230247

231248
if (!cb_info[0].IsBuffer()) {
232249
throw Napi::TypeError::New(
233250
env, "First argument must be a Buffer containing serialized AvmProvingInputs/AvmCircuitInputs");
234251
}
235252

253+
if (!cb_info[1].IsNumber()) {
254+
throw Napi::TypeError::New(env, "Second argument must be a log level number (0-7)");
255+
}
256+
257+
// Extract log level and set logging flags
258+
int log_level = cb_info[1].As<Napi::Number>().Int32Value();
259+
set_logging_from_level(log_level);
260+
236261
// Extract the inputs buffer
237262
auto inputs_buffer = cb_info[0].As<Napi::Buffer<uint8_t>>();
238263
size_t length = inputs_buffer.Length();

yarn-project/native/src/native_module.ts

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
import { findNapiBinary } from '@aztec/bb.js';
2+
import type { LogLevel } from '@aztec/foundation/log';
23

34
import { createRequire } from 'module';
45

56
import type { MessageReceiver } from './msgpack_channel.js';
67

8+
// Map TS log level to numeric index for C++
9+
// Matches: ['silent', 'fatal', 'error', 'warn', 'info', 'verbose', 'debug', 'trace']
10+
const LOG_LEVEL_MAP: Record<LogLevel, number> = {
11+
silent: 0,
12+
fatal: 1,
13+
error: 2,
14+
warn: 3,
15+
info: 4,
16+
verbose: 5,
17+
debug: 6,
18+
trace: 7,
19+
};
20+
721
interface NativeClassCtor {
822
new (...args: unknown[]): MessageReceiver;
923
}
@@ -82,31 +96,45 @@ export interface ContractProvider {
8296
revertCheckpoint(): Promise<void>;
8397
}
8498

99+
// Internal native functions with numeric log level
100+
const nativeAvmSimulate = nativeModule.avmSimulate as (
101+
inputs: Buffer,
102+
contractProvider: ContractProvider,
103+
worldStateHandle: any,
104+
logLevel: number,
105+
) => Promise<Buffer>;
106+
107+
const nativeAvmSimulateWithHintedDbs = nativeModule.avmSimulateWithHintedDbs as (
108+
inputs: Buffer,
109+
logLevel: number,
110+
) => Promise<Buffer>;
111+
85112
/**
86113
* AVM simulation function that takes serialized inputs and a contract provider.
87114
* The contract provider enables C++ to callback to TypeScript for contract data during simulation.
88115
* @param inputs - Msgpack-serialized AvmFastSimulationInputs buffer
89116
* @param contractProvider - Object with callbacks for fetching contract instances and classes
90117
* @param worldStateHandle - Native handle to WorldState instance
91-
* TODO(MW): include generate_hints bool
118+
* @param logLevel - Log level to control C++ verbosity
92119
* @returns Promise resolving to msgpack-serialized AvmCircuitPublicInputs buffer
93120
*/
94-
export const avmSimulate: (
121+
export function avmSimulate(
95122
inputs: Buffer,
96123
contractProvider: ContractProvider,
97124
worldStateHandle: any,
98-
) => Promise<Buffer> = nativeModule.avmSimulate as (
99-
inputs: Buffer,
100-
contractProvider: ContractProvider,
101-
worldStateHandle: any,
102-
) => Promise<Buffer>;
125+
logLevel: LogLevel = 'info',
126+
): Promise<Buffer> {
127+
return nativeAvmSimulate(inputs, contractProvider, worldStateHandle, LOG_LEVEL_MAP[logLevel]);
128+
}
129+
103130
/**
104131
* AVM simulation function that uses pre-collected hints from TypeScript simulation.
105132
* All contract data and merkle tree hints are included in the AvmCircuitInputs, so no runtime
106133
* callbacks to TS or WS pointer are needed.
107134
* @param inputs - Msgpack-serialized AvmCircuitInputs (AvmProvingInputs in C++) buffer
135+
* @param logLevel - Log level to control C++ verbosity
108136
* @returns Promise resolving to msgpack-serialized simulation results buffer
109137
*/
110-
export const avmSimulateWithHintedDbs: (inputs: Buffer) => Promise<Buffer> = nativeModule.avmSimulateWithHintedDbs as (
111-
inputs: Buffer,
112-
) => Promise<Buffer>;
138+
export function avmSimulateWithHintedDbs(inputs: Buffer, logLevel: LogLevel = 'info'): Promise<Buffer> {
139+
return nativeAvmSimulateWithHintedDbs(inputs, LOG_LEVEL_MAP[logLevel]);
140+
}

yarn-project/simulator/src/public/public_tx_simulator/cpp_public_tx_simulator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type Logger, createLogger } from '@aztec/foundation/log';
1+
import { type Logger, createLogger, logLevel } from '@aztec/foundation/log';
22
import { avmSimulate } from '@aztec/native';
33
import { ProtocolContractsList } from '@aztec/protocol-contracts';
44
import {
@@ -91,7 +91,7 @@ export class CppPublicTxSimulator extends PublicTxSimulator implements PublicTxS
9191
let resultBuffer: Buffer;
9292
try {
9393
this.log.debug(`Calling C++ simulator for tx ${txHash}`);
94-
resultBuffer = await avmSimulate(inputBuffer, contractProvider, wsCppHandle);
94+
resultBuffer = await avmSimulate(inputBuffer, contractProvider, wsCppHandle, logLevel);
9595
} catch (error: any) {
9696
throw new SimulationError(`C++ simulation failed: ${error.message}`, []);
9797
}

yarn-project/simulator/src/public/public_tx_simulator/cpp_public_tx_simulator_with_hinted_dbs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type Logger, createLogger } from '@aztec/foundation/log';
1+
import { type Logger, createLogger, logLevel } from '@aztec/foundation/log';
22
import { avmSimulateWithHintedDbs } from '@aztec/native';
33
import {
44
AvmCircuitInputs,
@@ -74,7 +74,7 @@ export class CppPublicTxSimulatorHintedDbs extends PublicTxSimulator implements
7474

7575
let resultBuffer: Buffer;
7676
try {
77-
resultBuffer = await avmSimulateWithHintedDbs(inputBuffer);
77+
resultBuffer = await avmSimulateWithHintedDbs(inputBuffer, logLevel);
7878
} catch (error: any) {
7979
throw new SimulationError(`C++ hinted simulation failed: ${error.message}`, []);
8080
}

yarn-project/simulator/src/public/public_tx_simulator/cpp_vs_ts_public_tx_simulator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type Logger, createLogger } from '@aztec/foundation/log';
1+
import { type Logger, createLogger, logLevel } from '@aztec/foundation/log';
22
import { writeTestData } from '@aztec/foundation/testing/files';
33
import { avmSimulate } from '@aztec/native';
44
import { ProtocolContractsList } from '@aztec/protocol-contracts';
@@ -117,7 +117,7 @@ export class CppVsTsPublicTxSimulator extends PublicTxSimulator implements Publi
117117
let resultBuffer: Buffer;
118118
try {
119119
this.log.debug(`Calling C++ simulator for tx ${txHash}`);
120-
resultBuffer = await avmSimulate(inputBuffer, contractProvider, wsCppHandle);
120+
resultBuffer = await avmSimulate(inputBuffer, contractProvider, wsCppHandle, logLevel);
121121
} catch (error: any) {
122122
throw new SimulationError(`C++ simulation failed: ${error.message}`, []);
123123
}

0 commit comments

Comments
 (0)