170 Napi::Env env = cb_info.Env();
179 if (cb_info.Length() < 3) {
180 throw Napi::TypeError::New(
182 "Wrong number of arguments. Expected 3-6 arguments: inputs Buffer, contractProvider "
183 "object, worldStateHandle, optional logLevel, optional loggerFunction, and optional cancellationToken.");
189 if (!cb_info[0].IsBuffer()) {
190 throw Napi::TypeError::New(env,
191 "First argument must be a Buffer containing serialized AvmFastSimulationInputs");
194 auto inputs_buffer = cb_info[0].As<Napi::Buffer<uint8_t>>();
195 size_t length = inputs_buffer.Length();
202 if (!cb_info[1].IsObject()) {
203 throw Napi::TypeError::New(env,
"Second argument must be a contractProvider object");
206 auto contract_provider = cb_info[1].As<Napi::Object>();
207 ContractCallbacks::validate(env, contract_provider);
211 .instance = make_tsfn(env,
212 ContractCallbacks::get(contract_provider, CALLBACK_GET_CONTRACT_INSTANCE),
213 CALLBACK_GET_CONTRACT_INSTANCE),
215 env, ContractCallbacks::get(contract_provider, CALLBACK_GET_CONTRACT_CLASS), CALLBACK_GET_CONTRACT_CLASS),
217 make_tsfn(env, ContractCallbacks::get(contract_provider, CALLBACK_ADD_CONTRACTS), CALLBACK_ADD_CONTRACTS),
219 make_tsfn(env, ContractCallbacks::get(contract_provider, CALLBACK_GET_BYTECODE), CALLBACK_GET_BYTECODE),
221 make_tsfn(env, ContractCallbacks::get(contract_provider, CALLBACK_GET_DEBUG_NAME), CALLBACK_GET_DEBUG_NAME),
222 .create_checkpoint = make_tsfn(
223 env, ContractCallbacks::get(contract_provider, CALLBACK_CREATE_CHECKPOINT), CALLBACK_CREATE_CHECKPOINT),
224 .commit_checkpoint = make_tsfn(
225 env, ContractCallbacks::get(contract_provider, CALLBACK_COMMIT_CHECKPOINT), CALLBACK_COMMIT_CHECKPOINT),
226 .revert_checkpoint = make_tsfn(
227 env, ContractCallbacks::get(contract_provider, CALLBACK_REVERT_CHECKPOINT), CALLBACK_REVERT_CHECKPOINT),
233 if (!cb_info[2].IsExternal()) {
234 throw Napi::TypeError::New(env,
"Third argument must be a WorldState handle (External)");
237 auto external = cb_info[2].As<Napi::External<world_state::WorldState>>();
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>>();
286 env, deferred, [
data, tsfns, logger_tsfn, ws_ptr, cancellation_token](msgpack::sbuffer& result_buffer) {
288 auto all_tsfns = tsfns.to_vector();
289 all_tsfns.push_back(logger_tsfn);
291 TsfnReleaser releaser = TsfnReleaser(
std::move(all_tsfns));
296 msgpack::object_handle obj_handle =
297 msgpack::unpack(
reinterpret_cast<const char*
>(
data->data()),
data->size());
298 msgpack::object obj = obj_handle.get();
304 *tsfns.add_contracts,
307 *tsfns.create_checkpoint,
308 *tsfns.commit_checkpoint,
309 *tsfns.revert_checkpoint);
317 msgpack::pack(result_buffer, result);
320 throw std::runtime_error(
"Simulation cancelled");
321 }
catch (
const std::exception& e) {
323 throw std::runtime_error(std::string(
"AVM simulation failed: ") + e.what());
325 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();
372 auto* op =
new AsyncOperation(env, deferred, [
data](msgpack::sbuffer& result_buffer) {
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");
399 return deferred->Promise();