1
#include "emulator-extern.h"
2
#include "td/utils/base64.h"
3
#include "td/utils/Status.h"
4
#include "td/utils/JsonBuilder.h"
5
#include "td/utils/logging.h"
6
#include "td/utils/Variant.h"
7
#include "td/utils/overloaded.h"
8
#include "transaction-emulator.h"
9
#include "tvm-emulator.hpp"
10
#include "crypto/vm/stack.hpp"
11
#include "crypto/vm/memo.h"
13
td::Result<td::Ref<vm::Cell>> boc_b64_to_cell(const char *boc) {
14
TRY_RESULT_PREFIX(boc_decoded, td::base64_decode(td::Slice(boc)), "Can't decode base64 boc: ");
15
return vm::std_boc_deserialize(boc_decoded);
18
td::Result<std::string> cell_to_boc_b64(td::Ref<vm::Cell> cell) {
19
TRY_RESULT_PREFIX(boc, vm::std_boc_serialize(std::move(cell), vm::BagOfCells::Mode::WithCRC32C), "Can't serialize cell: ");
20
return td::base64_encode(boc.as_slice());
23
const char *success_response(std::string&& transaction, std::string&& new_shard_account, std::string&& vm_log,
24
td::optional<std::string>&& actions, double elapsed_time) {
26
auto json_obj = jb.enter_object();
27
json_obj("success", td::JsonTrue());
28
json_obj("transaction", std::move(transaction));
29
json_obj("shard_account", std::move(new_shard_account));
30
json_obj("vm_log", std::move(vm_log));
32
json_obj("actions", actions.unwrap());
34
json_obj("actions", td::JsonNull());
36
json_obj("elapsed_time", elapsed_time);
38
return strdup(jb.string_builder().as_cslice().c_str());
41
const char *error_response(std::string&& error) {
43
auto json_obj = jb.enter_object();
44
json_obj("success", td::JsonFalse());
45
json_obj("error", std::move(error));
46
json_obj("external_not_accepted", td::JsonFalse());
48
return strdup(jb.string_builder().as_cslice().c_str());
51
const char *external_not_accepted_response(std::string&& vm_log, int vm_exit_code, double elapsed_time) {
53
auto json_obj = jb.enter_object();
54
json_obj("success", td::JsonFalse());
55
json_obj("error", "External message not accepted by smart contract");
56
json_obj("external_not_accepted", td::JsonTrue());
57
json_obj("vm_log", std::move(vm_log));
58
json_obj("vm_exit_code", vm_exit_code);
59
json_obj("elapsed_time", elapsed_time);
61
return strdup(jb.string_builder().as_cslice().c_str());
64
#define ERROR_RESPONSE(error) return error_response(error)
66
td::Result<block::Config> decode_config(const char* config_boc) {
67
TRY_RESULT_PREFIX(config_params_cell, boc_b64_to_cell(config_boc), "Can't deserialize config params boc: ");
68
auto global_config = block::Config(config_params_cell, td::Bits256::zero(), block::Config::needWorkchainInfo | block::Config::needSpecialSmc | block::Config::needCapabilities);
69
TRY_STATUS_PREFIX(global_config.unpack(), "Can't unpack config params: ");
73
void *transaction_emulator_create(const char *config_params_boc, int vm_log_verbosity) {
74
auto global_config_res = decode_config(config_params_boc);
75
if (global_config_res.is_error()) {
76
LOG(ERROR) << global_config_res.move_as_error().message();
80
return new emulator::TransactionEmulator(global_config_res.move_as_ok(), vm_log_verbosity);
83
const char *transaction_emulator_emulate_transaction(void *transaction_emulator, const char *shard_account_boc, const char *message_boc) {
84
auto emulator = static_cast<emulator::TransactionEmulator *>(transaction_emulator);
86
auto message_cell_r = boc_b64_to_cell(message_boc);
87
if (message_cell_r.is_error()) {
88
ERROR_RESPONSE(PSTRING() << "Can't deserialize message boc: " << message_cell_r.move_as_error());
90
auto message_cell = message_cell_r.move_as_ok();
91
auto message_cs = vm::load_cell_slice(message_cell);
92
int msg_tag = block::gen::t_CommonMsgInfo.get_tag(message_cs);
94
auto shard_account_cell = boc_b64_to_cell(shard_account_boc);
95
if (shard_account_cell.is_error()) {
96
ERROR_RESPONSE(PSTRING() << "Can't deserialize shard account boc: " << shard_account_cell.move_as_error());
98
auto shard_account_slice = vm::load_cell_slice(shard_account_cell.ok_ref());
99
block::gen::ShardAccount::Record shard_account;
100
if (!tlb::unpack(shard_account_slice, shard_account)) {
101
ERROR_RESPONSE(PSTRING() << "Can't unpack shard account cell");
104
td::Ref<vm::CellSlice> addr_slice;
105
auto account_slice = vm::load_cell_slice(shard_account.account);
106
bool account_exists = block::gen::t_Account.get_tag(account_slice) == block::gen::Account::account;
107
if (block::gen::t_Account.get_tag(account_slice) == block::gen::Account::account_none) {
108
if (msg_tag == block::gen::CommonMsgInfo::ext_in_msg_info) {
109
block::gen::CommonMsgInfo::Record_ext_in_msg_info info;
110
if (!tlb::unpack(message_cs, info)) {
111
ERROR_RESPONSE(PSTRING() << "Can't unpack inbound external message");
113
addr_slice = std::move(info.dest);
115
else if (msg_tag == block::gen::CommonMsgInfo::int_msg_info) {
116
block::gen::CommonMsgInfo::Record_int_msg_info info;
117
if (!tlb::unpack(message_cs, info)) {
118
ERROR_RESPONSE(PSTRING() << "Can't unpack inbound internal message");
120
addr_slice = std::move(info.dest);
122
ERROR_RESPONSE(PSTRING() << "Only ext in and int message are supported");
124
} else if (block::gen::t_Account.get_tag(account_slice) == block::gen::Account::account) {
125
block::gen::Account::Record_account account_record;
126
if (!tlb::unpack(account_slice, account_record)) {
127
ERROR_RESPONSE(PSTRING() << "Can't unpack account cell");
129
addr_slice = std::move(account_record.addr);
131
ERROR_RESPONSE(PSTRING() << "Can't parse account cell");
134
ton::StdSmcAddress addr;
135
if (!block::tlb::t_MsgAddressInt.extract_std_address(addr_slice, wc, addr)) {
136
ERROR_RESPONSE(PSTRING() << "Can't extract account address");
139
auto account = block::Account(wc, addr.bits());
140
ton::UnixTime now = emulator->get_unixtime();
142
now = (unsigned)std::time(nullptr);
144
bool is_special = wc == ton::masterchainId && emulator->get_config().is_special_smartcontract(addr);
145
if (account_exists) {
146
if (!account.unpack(vm::load_cell_slice_ref(shard_account_cell.move_as_ok()), now, is_special)) {
147
ERROR_RESPONSE(PSTRING() << "Can't unpack shard account");
150
if (!account.init_new(now)) {
151
ERROR_RESPONSE(PSTRING() << "Can't init new account");
153
account.last_trans_lt_ = shard_account.last_trans_lt;
154
account.last_trans_hash_ = shard_account.last_trans_hash;
157
auto result = emulator->emulate_transaction(std::move(account), message_cell, now, 0, block::transaction::Transaction::tr_ord);
158
if (result.is_error()) {
159
ERROR_RESPONSE(PSTRING() << "Emulate transaction failed: " << result.move_as_error());
161
auto emulation_result = result.move_as_ok();
163
auto external_not_accepted = dynamic_cast<emulator::TransactionEmulator::EmulationExternalNotAccepted *>(emulation_result.get());
164
if (external_not_accepted) {
165
return external_not_accepted_response(std::move(external_not_accepted->vm_log), external_not_accepted->vm_exit_code,
166
external_not_accepted->elapsed_time);
169
auto emulation_success = dynamic_cast<emulator::TransactionEmulator::EmulationSuccess&>(*emulation_result);
170
auto trans_boc_b64 = cell_to_boc_b64(std::move(emulation_success.transaction));
171
if (trans_boc_b64.is_error()) {
172
ERROR_RESPONSE(PSTRING() << "Can't serialize Transaction to boc " << trans_boc_b64.move_as_error());
175
auto new_shard_account_cell = vm::CellBuilder().store_ref(emulation_success.account.total_state)
176
.store_bits(emulation_success.account.last_trans_hash_.as_bitslice())
177
.store_long(emulation_success.account.last_trans_lt_).finalize();
178
auto new_shard_account_boc_b64 = cell_to_boc_b64(std::move(new_shard_account_cell));
179
if (new_shard_account_boc_b64.is_error()) {
180
ERROR_RESPONSE(PSTRING() << "Can't serialize ShardAccount to boc " << new_shard_account_boc_b64.move_as_error());
183
td::optional<td::string> actions_boc_b64;
184
if (emulation_success.actions.not_null()) {
185
auto actions_boc_b64_result = cell_to_boc_b64(std::move(emulation_success.actions));
186
if (actions_boc_b64_result.is_error()) {
187
ERROR_RESPONSE(PSTRING() << "Can't serialize actions list cell to boc " << actions_boc_b64_result.move_as_error());
189
actions_boc_b64 = actions_boc_b64_result.move_as_ok();
192
return success_response(trans_boc_b64.move_as_ok(), new_shard_account_boc_b64.move_as_ok(), std::move(emulation_success.vm_log),
193
std::move(actions_boc_b64), emulation_success.elapsed_time);
196
const char *transaction_emulator_emulate_tick_tock_transaction(void *transaction_emulator, const char *shard_account_boc, bool is_tock) {
197
auto emulator = static_cast<emulator::TransactionEmulator *>(transaction_emulator);
199
auto shard_account_cell = boc_b64_to_cell(shard_account_boc);
200
if (shard_account_cell.is_error()) {
201
ERROR_RESPONSE(PSTRING() << "Can't deserialize shard account boc: " << shard_account_cell.move_as_error());
203
auto shard_account_slice = vm::load_cell_slice(shard_account_cell.ok_ref());
204
block::gen::ShardAccount::Record shard_account;
205
if (!tlb::unpack(shard_account_slice, shard_account)) {
206
ERROR_RESPONSE(PSTRING() << "Can't unpack shard account cell");
209
td::Ref<vm::CellSlice> addr_slice;
210
auto account_slice = vm::load_cell_slice(shard_account.account);
211
if (block::gen::t_Account.get_tag(account_slice) == block::gen::Account::account_none) {
212
ERROR_RESPONSE(PSTRING() << "Can't run tick/tock transaction on account_none");
214
block::gen::Account::Record_account account_record;
215
if (!tlb::unpack(account_slice, account_record)) {
216
ERROR_RESPONSE(PSTRING() << "Can't unpack account cell");
218
addr_slice = std::move(account_record.addr);
220
ton::StdSmcAddress addr;
221
if (!block::tlb::t_MsgAddressInt.extract_std_address(addr_slice, wc, addr)) {
222
ERROR_RESPONSE(PSTRING() << "Can't extract account address");
225
auto account = block::Account(wc, addr.bits());
226
ton::UnixTime now = emulator->get_unixtime();
228
now = (unsigned)std::time(nullptr);
230
bool is_special = wc == ton::masterchainId && emulator->get_config().is_special_smartcontract(addr);
231
if (!account.unpack(vm::load_cell_slice_ref(shard_account_cell.move_as_ok()), now, is_special)) {
232
ERROR_RESPONSE(PSTRING() << "Can't unpack shard account");
235
auto trans_type = is_tock ? block::transaction::Transaction::tr_tock : block::transaction::Transaction::tr_tick;
236
auto result = emulator->emulate_transaction(std::move(account), {}, now, 0, trans_type);
237
if (result.is_error()) {
238
ERROR_RESPONSE(PSTRING() << "Emulate transaction failed: " << result.move_as_error());
240
auto emulation_result = result.move_as_ok();
242
auto emulation_success = dynamic_cast<emulator::TransactionEmulator::EmulationSuccess&>(*emulation_result);
243
auto trans_boc_b64 = cell_to_boc_b64(std::move(emulation_success.transaction));
244
if (trans_boc_b64.is_error()) {
245
ERROR_RESPONSE(PSTRING() << "Can't serialize Transaction to boc " << trans_boc_b64.move_as_error());
248
auto new_shard_account_cell = vm::CellBuilder().store_ref(emulation_success.account.total_state)
249
.store_bits(emulation_success.account.last_trans_hash_.as_bitslice())
250
.store_long(emulation_success.account.last_trans_lt_).finalize();
251
auto new_shard_account_boc_b64 = cell_to_boc_b64(std::move(new_shard_account_cell));
252
if (new_shard_account_boc_b64.is_error()) {
253
ERROR_RESPONSE(PSTRING() << "Can't serialize ShardAccount to boc " << new_shard_account_boc_b64.move_as_error());
256
td::optional<td::string> actions_boc_b64;
257
if (emulation_success.actions.not_null()) {
258
auto actions_boc_b64_result = cell_to_boc_b64(std::move(emulation_success.actions));
259
if (actions_boc_b64_result.is_error()) {
260
ERROR_RESPONSE(PSTRING() << "Can't serialize actions list cell to boc " << actions_boc_b64_result.move_as_error());
262
actions_boc_b64 = actions_boc_b64_result.move_as_ok();
265
return success_response(trans_boc_b64.move_as_ok(), new_shard_account_boc_b64.move_as_ok(), std::move(emulation_success.vm_log),
266
std::move(actions_boc_b64), emulation_success.elapsed_time);
269
bool transaction_emulator_set_unixtime(void *transaction_emulator, uint32_t unixtime) {
270
auto emulator = static_cast<emulator::TransactionEmulator *>(transaction_emulator);
272
emulator->set_unixtime(unixtime);
277
bool transaction_emulator_set_lt(void *transaction_emulator, uint64_t lt) {
278
auto emulator = static_cast<emulator::TransactionEmulator *>(transaction_emulator);
280
emulator->set_lt(lt);
285
bool transaction_emulator_set_rand_seed(void *transaction_emulator, const char* rand_seed_hex) {
286
auto emulator = static_cast<emulator::TransactionEmulator *>(transaction_emulator);
288
auto rand_seed_hex_slice = td::Slice(rand_seed_hex);
289
if (rand_seed_hex_slice.size() != 64) {
290
LOG(ERROR) << "Rand seed expected as 64 characters hex string";
293
auto rand_seed_bytes = td::hex_decode(rand_seed_hex_slice);
294
if (rand_seed_bytes.is_error()) {
295
LOG(ERROR) << "Can't decode hex rand seed";
298
td::BitArray<256> rand_seed;
299
rand_seed.as_slice().copy_from(rand_seed_bytes.move_as_ok());
301
emulator->set_rand_seed(rand_seed);
305
bool transaction_emulator_set_ignore_chksig(void *transaction_emulator, bool ignore_chksig) {
306
auto emulator = static_cast<emulator::TransactionEmulator *>(transaction_emulator);
308
emulator->set_ignore_chksig(ignore_chksig);
313
bool transaction_emulator_set_config(void *transaction_emulator, const char* config_boc) {
314
auto emulator = static_cast<emulator::TransactionEmulator *>(transaction_emulator);
316
auto global_config_res = decode_config(config_boc);
317
if (global_config_res.is_error()) {
318
LOG(ERROR) << global_config_res.move_as_error().message();
322
emulator->set_config(global_config_res.move_as_ok());
327
bool transaction_emulator_set_libs(void *transaction_emulator, const char* shardchain_libs_boc) {
328
auto emulator = static_cast<emulator::TransactionEmulator *>(transaction_emulator);
330
if (shardchain_libs_boc != nullptr) {
331
auto shardchain_libs_cell = boc_b64_to_cell(shardchain_libs_boc);
332
if (shardchain_libs_cell.is_error()) {
333
LOG(ERROR) << "Can't deserialize shardchain libraries boc: " << shardchain_libs_cell.move_as_error();
336
emulator->set_libs(vm::Dictionary(shardchain_libs_cell.move_as_ok(), 256));
342
bool transaction_emulator_set_debug_enabled(void *transaction_emulator, bool debug_enabled) {
343
auto emulator = static_cast<emulator::TransactionEmulator *>(transaction_emulator);
345
emulator->set_debug_enabled(debug_enabled);
350
bool transaction_emulator_set_prev_blocks_info(void *transaction_emulator, const char* info_boc) {
351
auto emulator = static_cast<emulator::TransactionEmulator *>(transaction_emulator);
353
if (info_boc != nullptr) {
354
auto info_cell = boc_b64_to_cell(info_boc);
355
if (info_cell.is_error()) {
356
LOG(ERROR) << "Can't deserialize previous blocks boc: " << info_cell.move_as_error();
359
vm::StackEntry info_value;
360
if (!info_value.deserialize(info_cell.move_as_ok())) {
361
LOG(ERROR) << "Can't deserialize previous blocks tuple";
364
if (info_value.is_null()) {
365
emulator->set_prev_blocks_info({});
366
} else if (info_value.is_tuple()) {
367
emulator->set_prev_blocks_info(info_value.as_tuple());
369
LOG(ERROR) << "Can't set previous blocks tuple: not a tuple";
377
void transaction_emulator_destroy(void *transaction_emulator) {
378
delete static_cast<emulator::TransactionEmulator *>(transaction_emulator);
381
bool emulator_set_verbosity_level(int verbosity_level) {
382
if (0 <= verbosity_level && verbosity_level <= VERBOSITY_NAME(NEVER)) {
383
SET_VERBOSITY_LEVEL(VERBOSITY_NAME(FATAL) + verbosity_level);
389
void *tvm_emulator_create(const char *code, const char *data, int vm_log_verbosity) {
390
auto code_cell = boc_b64_to_cell(code);
391
if (code_cell.is_error()) {
392
LOG(ERROR) << "Can't deserialize code boc: " << code_cell.move_as_error();
395
auto data_cell = boc_b64_to_cell(data);
396
if (data_cell.is_error()) {
397
LOG(ERROR) << "Can't deserialize code boc: " << data_cell.move_as_error();
401
auto emulator = new emulator::TvmEmulator(code_cell.move_as_ok(), data_cell.move_as_ok());
402
emulator->set_vm_verbosity_level(vm_log_verbosity);
406
bool tvm_emulator_set_libraries(void *tvm_emulator, const char *libs_boc) {
407
vm::Dictionary libs{256};
408
auto libs_cell = boc_b64_to_cell(libs_boc);
409
if (libs_cell.is_error()) {
410
LOG(ERROR) << "Can't deserialize libraries boc: " << libs_cell.move_as_error();
413
libs = vm::Dictionary(libs_cell.move_as_ok(), 256);
415
auto emulator = static_cast<emulator::TvmEmulator *>(tvm_emulator);
416
emulator->set_libraries(std::move(libs));
421
bool tvm_emulator_set_c7(void *tvm_emulator, const char *address, uint32_t unixtime, uint64_t balance, const char *rand_seed_hex, const char *config_boc) {
422
auto emulator = static_cast<emulator::TvmEmulator *>(tvm_emulator);
423
auto std_address = block::StdAddress::parse(td::Slice(address));
424
if (std_address.is_error()) {
425
LOG(ERROR) << "Can't parse address: " << std_address.move_as_error();
429
std::shared_ptr<block::Config> global_config;
430
if (config_boc != nullptr) {
431
auto config_params_cell = boc_b64_to_cell(config_boc);
432
if (config_params_cell.is_error()) {
433
LOG(ERROR) << "Can't deserialize config params boc: " << config_params_cell.move_as_error();
436
global_config = std::make_shared<block::Config>(
437
config_params_cell.move_as_ok(), td::Bits256::zero(),
438
block::Config::needWorkchainInfo | block::Config::needSpecialSmc | block::Config::needCapabilities);
439
auto unpack_res = global_config->unpack();
440
if (unpack_res.is_error()) {
441
LOG(ERROR) << "Can't unpack config params";
446
auto rand_seed_hex_slice = td::Slice(rand_seed_hex);
447
if (rand_seed_hex_slice.size() != 64) {
448
LOG(ERROR) << "Rand seed expected as 64 characters hex string";
451
auto rand_seed_bytes = td::hex_decode(rand_seed_hex_slice);
452
if (rand_seed_bytes.is_error()) {
453
LOG(ERROR) << "Can't decode hex rand seed";
456
td::BitArray<256> rand_seed;
457
rand_seed.as_slice().copy_from(rand_seed_bytes.move_as_ok());
459
emulator->set_c7(std_address.move_as_ok(), unixtime, balance, rand_seed, std::const_pointer_cast<const block::Config>(global_config));
464
bool tvm_emulator_set_prev_blocks_info(void *tvm_emulator, const char* info_boc) {
465
auto emulator = static_cast<emulator::TvmEmulator *>(tvm_emulator);
467
if (info_boc != nullptr) {
468
auto info_cell = boc_b64_to_cell(info_boc);
469
if (info_cell.is_error()) {
470
LOG(ERROR) << "Can't deserialize previous blocks boc: " << info_cell.move_as_error();
473
vm::StackEntry info_value;
474
if (!info_value.deserialize(info_cell.move_as_ok())) {
475
LOG(ERROR) << "Can't deserialize previous blocks tuple";
478
if (info_value.is_null()) {
479
emulator->set_prev_blocks_info({});
480
} else if (info_value.is_tuple()) {
481
emulator->set_prev_blocks_info(info_value.as_tuple());
483
LOG(ERROR) << "Can't set previous blocks tuple: not a tuple";
491
bool tvm_emulator_set_gas_limit(void *tvm_emulator, int64_t gas_limit) {
492
auto emulator = static_cast<emulator::TvmEmulator *>(tvm_emulator);
493
emulator->set_gas_limit(gas_limit);
497
bool tvm_emulator_set_debug_enabled(void *tvm_emulator, bool debug_enabled) {
498
auto emulator = static_cast<emulator::TvmEmulator *>(tvm_emulator);
499
emulator->set_debug_enabled(debug_enabled);
503
const char *tvm_emulator_run_get_method(void *tvm_emulator, int method_id, const char *stack_boc) {
504
auto stack_cell = boc_b64_to_cell(stack_boc);
505
if (stack_cell.is_error()) {
506
ERROR_RESPONSE(PSTRING() << "Couldn't deserialize stack cell: " << stack_cell.move_as_error().to_string());
508
auto stack_cs = vm::load_cell_slice(stack_cell.move_as_ok());
509
td::Ref<vm::Stack> stack;
510
if (!vm::Stack::deserialize_to(stack_cs, stack)) {
511
ERROR_RESPONSE(PSTRING() << "Couldn't deserialize stack");
514
auto emulator = static_cast<emulator::TvmEmulator *>(tvm_emulator);
515
auto result = emulator->run_get_method(method_id, stack);
517
vm::FakeVmStateLimits fstate(3500); // limit recursive (de)serialization calls
518
vm::VmStateInterface::Guard guard(&fstate);
520
vm::CellBuilder stack_cb;
521
if (!result.stack->serialize(stack_cb)) {
522
ERROR_RESPONSE(PSTRING() << "Couldn't serialize stack");
524
auto result_stack_boc = cell_to_boc_b64(stack_cb.finalize());
525
if (result_stack_boc.is_error()) {
526
ERROR_RESPONSE(PSTRING() << "Couldn't serialize stack cell: " << result_stack_boc.move_as_error().to_string());
530
auto json_obj = jb.enter_object();
531
json_obj("success", td::JsonTrue());
532
json_obj("stack", result_stack_boc.move_as_ok());
533
json_obj("gas_used", std::to_string(result.gas_used));
534
json_obj("vm_exit_code", result.code);
535
json_obj("vm_log", result.vm_log);
536
if (!result.missing_library) {
537
json_obj("missing_library", td::JsonNull());
539
json_obj("missing_library", result.missing_library.value().to_hex());
543
return strdup(jb.string_builder().as_cslice().c_str());
546
const char *tvm_emulator_emulate_run_method(uint32_t len, const char *params_boc, int64_t gas_limit) {
547
auto params_cell = vm::std_boc_deserialize(td::Slice(params_boc, len));
548
if (params_cell.is_error()) {
551
auto params_cs = vm::load_cell_slice(params_cell.move_as_ok());
552
auto code = params_cs.fetch_ref();
553
auto data = params_cs.fetch_ref();
555
auto stack_cs = vm::load_cell_slice(params_cs.fetch_ref());
556
auto params = vm::load_cell_slice(params_cs.fetch_ref());
557
auto c7_cs = vm::load_cell_slice(params.fetch_ref());
558
auto libs = vm::Dictionary(params.fetch_ref(), 256);
560
auto method_id = params_cs.fetch_long(32);
562
td::Ref<vm::Stack> stack;
563
if (!vm::Stack::deserialize_to(stack_cs, stack)) {
567
td::Ref<vm::Stack> c7;
568
if (!vm::Stack::deserialize_to(c7_cs, c7)) {
572
auto emulator = new emulator::TvmEmulator(code, data);
573
emulator->set_vm_verbosity_level(0);
574
emulator->set_gas_limit(gas_limit);
575
emulator->set_c7_raw(c7->fetch(0).as_tuple());
576
if (libs.is_empty()) {
577
emulator->set_libraries(std::move(libs));
579
auto result = emulator->run_get_method(int(method_id), stack);
582
vm::CellBuilder stack_cb;
583
if (!result.stack->serialize(stack_cb)) {
588
cb.store_long(result.code, 32);
589
cb.store_long(result.gas_used, 64);
590
cb.store_ref(stack_cb.finalize());
592
auto ser = vm::std_boc_serialize(cb.finalize());
596
auto sok = ser.move_as_ok();
598
auto sz = uint32_t(sok.size());
599
char* rn = (char*)malloc(sz + 4);
601
memcpy(rn+4, sok.data(), sz);
606
const char *tvm_emulator_send_external_message(void *tvm_emulator, const char *message_body_boc) {
607
auto message_body_cell = boc_b64_to_cell(message_body_boc);
608
if (message_body_cell.is_error()) {
609
ERROR_RESPONSE(PSTRING() << "Can't deserialize message body boc: " << message_body_cell.move_as_error());
612
auto emulator = static_cast<emulator::TvmEmulator *>(tvm_emulator);
613
auto result = emulator->send_external_message(message_body_cell.move_as_ok());
616
auto json_obj = jb.enter_object();
617
json_obj("success", td::JsonTrue());
618
json_obj("gas_used", std::to_string(result.gas_used));
619
json_obj("vm_exit_code", result.code);
620
json_obj("accepted", td::JsonBool(result.accepted));
621
json_obj("vm_log", result.vm_log);
622
if (!result.missing_library) {
623
json_obj("missing_library", td::JsonNull());
625
json_obj("missing_library", result.missing_library.value().to_hex());
627
if (result.actions.is_null()) {
628
json_obj("actions", td::JsonNull());
630
json_obj("actions", cell_to_boc_b64(result.actions).move_as_ok());
632
json_obj("new_code", cell_to_boc_b64(result.new_state.code).move_as_ok());
633
json_obj("new_data", cell_to_boc_b64(result.new_state.data).move_as_ok());
636
return strdup(jb.string_builder().as_cslice().c_str());
639
const char *tvm_emulator_send_internal_message(void *tvm_emulator, const char *message_body_boc, uint64_t amount) {
640
auto message_body_cell = boc_b64_to_cell(message_body_boc);
641
if (message_body_cell.is_error()) {
642
ERROR_RESPONSE(PSTRING() << "Can't deserialize message body boc: " << message_body_cell.move_as_error());
645
auto emulator = static_cast<emulator::TvmEmulator *>(tvm_emulator);
646
auto result = emulator->send_internal_message(message_body_cell.move_as_ok(), amount);
649
auto json_obj = jb.enter_object();
650
json_obj("success", td::JsonTrue());
651
json_obj("gas_used", std::to_string(result.gas_used));
652
json_obj("vm_exit_code", result.code);
653
json_obj("accepted", td::JsonBool(result.accepted));
654
json_obj("vm_log", result.vm_log);
655
if (!result.missing_library) {
656
json_obj("missing_library", td::JsonNull());
658
json_obj("missing_library", result.missing_library.value().to_hex());
660
if (result.actions.is_null()) {
661
json_obj("actions", td::JsonNull());
663
json_obj("actions", cell_to_boc_b64(result.actions).move_as_ok());
665
json_obj("new_code", cell_to_boc_b64(result.new_state.code).move_as_ok());
666
json_obj("new_data", cell_to_boc_b64(result.new_state.data).move_as_ok());
669
return strdup(jb.string_builder().as_cslice().c_str());
672
void tvm_emulator_destroy(void *tvm_emulator) {
673
delete static_cast<emulator::TvmEmulator *>(tvm_emulator);