159 Napi::Env env = cb_info.Env();
168 if (cb_info.Length() < 3) {
169 throw Napi::TypeError::New(
171 "Wrong number of arguments. Expected 3-6 arguments: inputs Buffer, contractProvider "
172 "object, worldStateHandle, optional logLevel, optional loggerFunction, and optional cancellationToken.");
178 if (!cb_info[0].IsBuffer()) {
179 throw Napi::TypeError::New(env,
180 "First argument must be a Buffer containing serialized AvmFastSimulationInputs");
183 auto inputs_buffer = cb_info[0].As<Napi::Buffer<uint8_t>>();
184 size_t length = inputs_buffer.Length();
191 if (!cb_info[1].IsObject()) {
192 throw Napi::TypeError::New(env,
"Second argument must be a contractProvider object");
195 auto contract_provider = cb_info[1].As<Napi::Object>();
196 ContractCallbacks::validate(env, contract_provider);
200 .instance = make_tsfn(env,
201 ContractCallbacks::get(contract_provider, CALLBACK_GET_CONTRACT_INSTANCE),
202 CALLBACK_GET_CONTRACT_INSTANCE),
204 env, ContractCallbacks::get(contract_provider, CALLBACK_GET_CONTRACT_CLASS), CALLBACK_GET_CONTRACT_CLASS),
206 make_tsfn(env, ContractCallbacks::get(contract_provider, CALLBACK_ADD_CONTRACTS), CALLBACK_ADD_CONTRACTS),
208 make_tsfn(env, ContractCallbacks::get(contract_provider, CALLBACK_GET_BYTECODE), CALLBACK_GET_BYTECODE),
210 make_tsfn(env, ContractCallbacks::get(contract_provider, CALLBACK_GET_DEBUG_NAME), CALLBACK_GET_DEBUG_NAME),
211 .create_checkpoint = make_tsfn(
212 env, ContractCallbacks::get(contract_provider, CALLBACK_CREATE_CHECKPOINT), CALLBACK_CREATE_CHECKPOINT),
213 .commit_checkpoint = make_tsfn(
214 env, ContractCallbacks::get(contract_provider, CALLBACK_COMMIT_CHECKPOINT), CALLBACK_COMMIT_CHECKPOINT),
215 .revert_checkpoint = make_tsfn(
216 env, ContractCallbacks::get(contract_provider, CALLBACK_REVERT_CHECKPOINT), CALLBACK_REVERT_CHECKPOINT),
222 if (!cb_info[2].IsExternal()) {
223 throw Napi::TypeError::New(env,
"Third argument must be a WorldState handle (External)");
226 auto external = cb_info[2].As<Napi::External<world_state::WorldState>>();
233 if (cb_info.Length() > 3 && cb_info[3].IsNumber()) {
234 log_level = cb_info[3].As<Napi::Number>().Int32Value();
235 set_logging_from_level(log_level);
242 if (cb_info.Length() > 4 && !cb_info[4].IsNull() && !cb_info[4].IsUndefined()) {
243 if (cb_info[4].IsFunction()) {
245 auto logger_function = cb_info[4].As<Napi::Function>();
246 logger_tsfn = make_tsfn(env, logger_function,
"LoggerCallback");
251 throw Napi::TypeError::New(env,
"Fifth argument must be a logger function, null, or undefined");
259 if (cb_info.Length() > 5 && cb_info[5].IsExternal()) {
260 auto token_external = cb_info[5].As<Napi::External<avm2::simulation::CancellationToken>>();
275 env, deferred, [
data, tsfns, logger_tsfn, ws_ptr, cancellation_token](msgpack::sbuffer& result_buffer) {
277 auto all_tsfns = tsfns.to_vector();
278 all_tsfns.push_back(logger_tsfn);
280 TsfnReleaser releaser = TsfnReleaser(
std::move(all_tsfns));
285 msgpack::object_handle obj_handle =
286 msgpack::unpack(
reinterpret_cast<const char*
>(
data->data()),
data->size());
287 msgpack::object obj = obj_handle.get();
293 *tsfns.add_contracts,
296 *tsfns.create_checkpoint,
297 *tsfns.commit_checkpoint,
298 *tsfns.revert_checkpoint);
306 msgpack::pack(result_buffer, result);
309 throw std::runtime_error(
"Simulation cancelled");
310 }
catch (
const std::exception& e) {
312 throw std::runtime_error(std::string(
"AVM simulation failed: ") + e.what());
314 throw std::runtime_error(
"AVM simulation failed with unknown exception");
321 return deferred->Promise();
326 Napi::Env env = cb_info.Env();
331 if (cb_info.Length() < 2) {
332 throw Napi::TypeError::New(env,
333 "Wrong number of arguments. Expected 2 arguments: AvmProvingInputs/AvmCircuitInputs "
334 "msgpack Buffer and logLevel.");
337 if (!cb_info[0].IsBuffer()) {
338 throw Napi::TypeError::New(
339 env,
"First argument must be a Buffer containing serialized AvmProvingInputs/AvmCircuitInputs");
342 if (!cb_info[1].IsNumber()) {
343 throw Napi::TypeError::New(env,
"Second argument must be a log level number (0-7)");
347 int log_level = cb_info[1].As<Napi::Number>().Int32Value();
348 set_logging_from_level(log_level);
351 auto inputs_buffer = cb_info[0].As<Napi::Buffer<uint8_t>>();
352 size_t length = inputs_buffer.Length();
361 auto* op =
new AsyncOperation(env, deferred, [
data](msgpack::sbuffer& result_buffer) {
365 msgpack::object_handle obj_handle =
366 msgpack::unpack(
reinterpret_cast<const char*
>(
data->data()),
data->size());
367 msgpack::object obj = obj_handle.get();
376 msgpack::pack(result_buffer, result);
377 }
catch (
const std::exception& e) {
379 throw std::runtime_error(std::string(
"AVM simulation with hinted DBs failed: ") + e.what());
381 throw std::runtime_error(
"AVM simulation with hinted DBs failed with unknown exception");
388 return deferred->Promise();