172 Napi::Env env = cb_info.Env();
181 if (cb_info.Length() < 3) {
182 throw Napi::TypeError::New(
184 "Wrong number of arguments. Expected 3-6 arguments: inputs Buffer, contractProvider "
185 "object, worldStateHandle, optional logLevel, optional loggerFunction, and optional cancellationToken.");
191 if (!cb_info[0].IsBuffer()) {
192 throw Napi::TypeError::New(env,
193 "First argument must be a Buffer containing serialized AvmFastSimulationInputs");
196 auto inputs_buffer = cb_info[0].As<Napi::Buffer<uint8_t>>();
197 size_t length = inputs_buffer.Length();
204 if (!cb_info[1].IsObject()) {
205 throw Napi::TypeError::New(env,
"Second argument must be a contractProvider object");
208 auto contract_provider = cb_info[1].As<Napi::Object>();
209 ContractCallbacks::validate(env, contract_provider);
213 .instance = make_tsfn(env,
214 ContractCallbacks::get(contract_provider, CALLBACK_GET_CONTRACT_INSTANCE),
215 CALLBACK_GET_CONTRACT_INSTANCE),
217 env, ContractCallbacks::get(contract_provider, CALLBACK_GET_CONTRACT_CLASS), CALLBACK_GET_CONTRACT_CLASS),
219 make_tsfn(env, ContractCallbacks::get(contract_provider, CALLBACK_ADD_CONTRACTS), CALLBACK_ADD_CONTRACTS),
221 make_tsfn(env, ContractCallbacks::get(contract_provider, CALLBACK_GET_BYTECODE), CALLBACK_GET_BYTECODE),
223 make_tsfn(env, ContractCallbacks::get(contract_provider, CALLBACK_GET_DEBUG_NAME), CALLBACK_GET_DEBUG_NAME),
224 .create_checkpoint = make_tsfn(
225 env, ContractCallbacks::get(contract_provider, CALLBACK_CREATE_CHECKPOINT), CALLBACK_CREATE_CHECKPOINT),
226 .commit_checkpoint = make_tsfn(
227 env, ContractCallbacks::get(contract_provider, CALLBACK_COMMIT_CHECKPOINT), CALLBACK_COMMIT_CHECKPOINT),
228 .revert_checkpoint = make_tsfn(
229 env, ContractCallbacks::get(contract_provider, CALLBACK_REVERT_CHECKPOINT), CALLBACK_REVERT_CHECKPOINT),
235 if (!cb_info[2].IsString()) {
236 throw Napi::TypeError::New(env,
"Third argument must be a WSDB IPC path (string)");
238 std::string wsdb_ipc_path = cb_info[2].As<Napi::String>().Utf8Value();
244 if (cb_info.Length() > 3 && cb_info[3].IsNumber()) {
245 log_level = cb_info[3].As<Napi::Number>().Int32Value();
246 set_logging_from_level(log_level);
253 if (cb_info.Length() > 4 && !cb_info[4].IsNull() && !cb_info[4].IsUndefined()) {
254 if (cb_info[4].IsFunction()) {
256 auto logger_function = cb_info[4].As<Napi::Function>();
257 logger_tsfn = make_tsfn(env, logger_function,
"LoggerCallback");
262 throw Napi::TypeError::New(env,
"Fifth argument must be a logger function, null, or undefined");
270 if (cb_info.Length() > 5 && cb_info[5].IsExternal()) {
271 auto token_external = cb_info[5].As<Napi::External<avm2::simulation::CancellationToken>>();
285 env, deferred, [
data, tsfns, logger_tsfn, wsdb_ipc_path, cancellation_token](msgpack::sbuffer& result_buffer) {
287 auto all_tsfns = tsfns.to_vector();
288 all_tsfns.push_back(logger_tsfn);
290 TsfnReleaser releaser = TsfnReleaser(
std::move(all_tsfns));
295 msgpack::object_handle obj_handle =
296 msgpack::unpack(
reinterpret_cast<const char*
>(
data->data()),
data->size());
297 msgpack::object obj = obj_handle.get();
303 *tsfns.add_contracts,
306 *tsfns.create_checkpoint,
307 *tsfns.commit_checkpoint,
308 *tsfns.revert_checkpoint);
313 bb::wsdb::WsdbIpcClient wsdb_client(wsdb_ipc_path);
320 msgpack::pack(result_buffer, result);
323 throw std::runtime_error(
"Simulation cancelled");
324 }
catch (
const std::exception& e) {
326 throw std::runtime_error(std::string(
"AVM simulation failed: ") + e.what());
328 throw std::runtime_error(
"AVM simulation failed with unknown exception");
332 return deferred->Promise();
337 Napi::Env env = cb_info.Env();
342 if (cb_info.Length() < 2) {
343 throw Napi::TypeError::New(env,
344 "Wrong number of arguments. Expected 2 arguments: AvmProvingInputs/AvmCircuitInputs "
345 "msgpack Buffer and logLevel.");
348 if (!cb_info[0].IsBuffer()) {
349 throw Napi::TypeError::New(
350 env,
"First argument must be a Buffer containing serialized AvmProvingInputs/AvmCircuitInputs");
353 if (!cb_info[1].IsNumber()) {
354 throw Napi::TypeError::New(env,
"Second argument must be a log level number (0-7)");
358 int log_level = cb_info[1].As<Napi::Number>().Int32Value();
359 set_logging_from_level(log_level);
362 auto inputs_buffer = cb_info[0].As<Napi::Buffer<uint8_t>>();
363 size_t length = inputs_buffer.Length();
376 msgpack::object_handle obj_handle =
377 msgpack::unpack(
reinterpret_cast<const char*
>(
data->data()),
data->size());
378 msgpack::object obj = obj_handle.get();
387 msgpack::pack(result_buffer, result);
388 }
catch (
const std::exception& e) {
390 throw std::runtime_error(std::string(
"AVM simulation with hinted DBs failed: ") + e.what());
392 throw std::runtime_error(
"AVM simulation with hinted DBs failed with unknown exception");
396 return deferred->Promise();