Ton

Форк
0
/
emulator-extern.cpp 
674 строки · 26.0 Кб
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"
12

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);
16
}
17

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());
21
}
22

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) {
25
  td::JsonBuilder jb;
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));
31
  if (actions) {
32
    json_obj("actions", actions.unwrap());
33
  } else {
34
    json_obj("actions", td::JsonNull());
35
  }
36
  json_obj("elapsed_time", elapsed_time);
37
  json_obj.leave();
38
  return strdup(jb.string_builder().as_cslice().c_str());
39
}
40

41
const char *error_response(std::string&& error) {
42
  td::JsonBuilder jb;
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());
47
  json_obj.leave();
48
  return strdup(jb.string_builder().as_cslice().c_str());
49
}
50

51
const char *external_not_accepted_response(std::string&& vm_log, int vm_exit_code, double elapsed_time) {
52
  td::JsonBuilder jb;
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);
60
  json_obj.leave();
61
  return strdup(jb.string_builder().as_cslice().c_str());
62
}
63

64
#define ERROR_RESPONSE(error) return error_response(error)
65

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: ");
70
  return global_config;
71
}
72

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();
77
    return nullptr;
78
  }
79

80
  return new emulator::TransactionEmulator(global_config_res.move_as_ok(), vm_log_verbosity);
81
}
82

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);
85
  
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());
89
  }
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);
93

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());
97
  }
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");
102
  }
103

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");
112
      }
113
      addr_slice = std::move(info.dest);
114
    }
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");
119
      }
120
      addr_slice = std::move(info.dest);
121
    } else {
122
      ERROR_RESPONSE(PSTRING() << "Only ext in and int message are supported");
123
    }
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");
128
    }
129
    addr_slice = std::move(account_record.addr);
130
  } else {
131
    ERROR_RESPONSE(PSTRING() << "Can't parse account cell");
132
  }
133
  ton::WorkchainId wc;
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");
137
  }
138

139
  auto account = block::Account(wc, addr.bits());
140
  ton::UnixTime now = emulator->get_unixtime();
141
  if (!now) {
142
    now = (unsigned)std::time(nullptr);
143
  }
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");
148
    }
149
  } else {
150
    if (!account.init_new(now)) {
151
      ERROR_RESPONSE(PSTRING() << "Can't init new account");
152
    }
153
    account.last_trans_lt_ = shard_account.last_trans_lt;
154
    account.last_trans_hash_ = shard_account.last_trans_hash;
155
  }
156

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());
160
  }
161
  auto emulation_result = result.move_as_ok();
162

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);
167
  }
168

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());
173
  }
174

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());
181
  }
182

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());
188
    }
189
    actions_boc_b64 = actions_boc_b64_result.move_as_ok();
190
  }
191

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);
194
}
195

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);
198
  
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());
202
  }
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");
207
  }
208

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");
213
  }
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");
217
  }
218
  addr_slice = std::move(account_record.addr);
219
  ton::WorkchainId wc;
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");
223
  }
224

225
  auto account = block::Account(wc, addr.bits());
226
  ton::UnixTime now = emulator->get_unixtime();
227
  if (!now) {
228
    now = (unsigned)std::time(nullptr);
229
  }
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");
233
  }
234

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());
239
  }
240
  auto emulation_result = result.move_as_ok();
241

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());
246
  }
247

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());
254
  }
255

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());
261
    }
262
    actions_boc_b64 = actions_boc_b64_result.move_as_ok();
263
  }
264

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);
267
}
268

269
bool transaction_emulator_set_unixtime(void *transaction_emulator, uint32_t unixtime) {
270
  auto emulator = static_cast<emulator::TransactionEmulator *>(transaction_emulator);
271

272
  emulator->set_unixtime(unixtime);
273

274
  return true;
275
}
276

277
bool transaction_emulator_set_lt(void *transaction_emulator, uint64_t lt) {
278
  auto emulator = static_cast<emulator::TransactionEmulator *>(transaction_emulator);
279

280
  emulator->set_lt(lt);
281

282
  return true;
283
}
284

285
bool transaction_emulator_set_rand_seed(void *transaction_emulator, const char* rand_seed_hex) {
286
  auto emulator = static_cast<emulator::TransactionEmulator *>(transaction_emulator);
287

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";
291
    return false;
292
  }
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";
296
    return false;
297
  }
298
  td::BitArray<256> rand_seed;
299
  rand_seed.as_slice().copy_from(rand_seed_bytes.move_as_ok());
300

301
  emulator->set_rand_seed(rand_seed);
302
  return true;
303
}
304

305
bool transaction_emulator_set_ignore_chksig(void *transaction_emulator, bool ignore_chksig) {
306
  auto emulator = static_cast<emulator::TransactionEmulator *>(transaction_emulator);
307

308
  emulator->set_ignore_chksig(ignore_chksig);
309

310
  return true;
311
}
312

313
bool transaction_emulator_set_config(void *transaction_emulator, const char* config_boc) {
314
  auto emulator = static_cast<emulator::TransactionEmulator *>(transaction_emulator);
315

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();
319
    return false;
320
  }
321

322
  emulator->set_config(global_config_res.move_as_ok());
323

324
  return true;
325
}
326

327
bool transaction_emulator_set_libs(void *transaction_emulator, const char* shardchain_libs_boc) {
328
  auto emulator = static_cast<emulator::TransactionEmulator *>(transaction_emulator);
329

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();
334
      return false;
335
    }
336
    emulator->set_libs(vm::Dictionary(shardchain_libs_cell.move_as_ok(), 256));
337
  }
338

339
  return true;
340
}
341

342
bool transaction_emulator_set_debug_enabled(void *transaction_emulator, bool debug_enabled) {
343
  auto emulator = static_cast<emulator::TransactionEmulator *>(transaction_emulator);
344

345
  emulator->set_debug_enabled(debug_enabled);
346

347
  return true;
348
}
349

350
bool transaction_emulator_set_prev_blocks_info(void *transaction_emulator, const char* info_boc) {
351
  auto emulator = static_cast<emulator::TransactionEmulator *>(transaction_emulator);
352

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();
357
      return false;
358
    }
359
    vm::StackEntry info_value;
360
    if (!info_value.deserialize(info_cell.move_as_ok())) {
361
      LOG(ERROR) << "Can't deserialize previous blocks tuple";
362
      return false;
363
    }
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());
368
    } else {
369
      LOG(ERROR) << "Can't set previous blocks tuple: not a tuple";
370
      return false;
371
    }
372
  }
373

374
  return true;
375
}
376

377
void transaction_emulator_destroy(void *transaction_emulator) {
378
  delete static_cast<emulator::TransactionEmulator *>(transaction_emulator);
379
}
380

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);
384
    return true;
385
  }
386
  return false;
387
}
388

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();
393
    return nullptr;
394
  }
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();
398
    return nullptr;
399
  }
400

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);
403
  return emulator;
404
}
405

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();
411
    return false;
412
  }
413
  libs = vm::Dictionary(libs_cell.move_as_ok(), 256);
414

415
  auto emulator = static_cast<emulator::TvmEmulator *>(tvm_emulator);
416
  emulator->set_libraries(std::move(libs));
417

418
  return true;
419
}
420

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();
426
    return false;
427
  }
428
  
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();
434
      return false;
435
    }
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";
442
      return false;
443
    }
444
  }
445

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";
449
    return false;
450
  }
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";
454
    return false;
455
  }
456
  td::BitArray<256> rand_seed;
457
  rand_seed.as_slice().copy_from(rand_seed_bytes.move_as_ok());
458

459
  emulator->set_c7(std_address.move_as_ok(), unixtime, balance, rand_seed, std::const_pointer_cast<const block::Config>(global_config));
460
  
461
  return true;
462
}
463

464
bool tvm_emulator_set_prev_blocks_info(void *tvm_emulator, const char* info_boc) {
465
  auto emulator = static_cast<emulator::TvmEmulator *>(tvm_emulator);
466

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();
471
      return false;
472
    }
473
    vm::StackEntry info_value;
474
    if (!info_value.deserialize(info_cell.move_as_ok())) {
475
      LOG(ERROR) << "Can't deserialize previous blocks tuple";
476
      return false;
477
    }
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());
482
    } else {
483
      LOG(ERROR) << "Can't set previous blocks tuple: not a tuple";
484
      return false;
485
    }
486
  }
487

488
  return true;
489
}
490

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);
494
  return true;
495
}
496

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);
500
  return true;
501
}
502

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());
507
  }
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");
512
  }
513

514
  auto emulator = static_cast<emulator::TvmEmulator *>(tvm_emulator);
515
  auto result = emulator->run_get_method(method_id, stack);
516
  
517
  vm::FakeVmStateLimits fstate(3500);  // limit recursive (de)serialization calls
518
  vm::VmStateInterface::Guard guard(&fstate);
519
  
520
  vm::CellBuilder stack_cb;
521
  if (!result.stack->serialize(stack_cb)) {
522
    ERROR_RESPONSE(PSTRING() << "Couldn't serialize stack");
523
  }
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());
527
  }
528

529
  td::JsonBuilder jb;
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());
538
  } else {
539
    json_obj("missing_library", result.missing_library.value().to_hex());
540
  }
541
  json_obj.leave();
542

543
  return strdup(jb.string_builder().as_cslice().c_str());
544
}
545

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()) {
549
    return nullptr;
550
  }
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();
554

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);
559

560
  auto method_id = params_cs.fetch_long(32);
561

562
  td::Ref<vm::Stack> stack;
563
  if (!vm::Stack::deserialize_to(stack_cs, stack)) {
564
    return nullptr;
565
  }
566

567
  td::Ref<vm::Stack> c7;
568
  if (!vm::Stack::deserialize_to(c7_cs, c7)) {
569
    return nullptr;
570
  }
571

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));
578
  }
579
  auto result = emulator->run_get_method(int(method_id), stack);
580
  delete emulator;
581

582
  vm::CellBuilder stack_cb;
583
  if (!result.stack->serialize(stack_cb)) {
584
    return nullptr;
585
  }
586

587
  vm::CellBuilder cb;
588
  cb.store_long(result.code, 32);
589
  cb.store_long(result.gas_used, 64);
590
  cb.store_ref(stack_cb.finalize());
591

592
  auto ser = vm::std_boc_serialize(cb.finalize());
593
  if (!ser.is_ok()) {
594
    return nullptr;
595
  }
596
  auto sok = ser.move_as_ok();
597

598
  auto sz = uint32_t(sok.size());
599
  char* rn = (char*)malloc(sz + 4);
600
  memcpy(rn, &sz, 4);
601
  memcpy(rn+4, sok.data(), sz);
602

603
  return rn;
604
}
605

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());
610
  }
611

612
  auto emulator = static_cast<emulator::TvmEmulator *>(tvm_emulator);
613
  auto result = emulator->send_external_message(message_body_cell.move_as_ok());
614

615
  td::JsonBuilder jb;
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());
624
  } else {
625
    json_obj("missing_library", result.missing_library.value().to_hex());
626
  }
627
  if (result.actions.is_null()) {
628
    json_obj("actions", td::JsonNull());
629
  } else {
630
    json_obj("actions", cell_to_boc_b64(result.actions).move_as_ok());
631
  }
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());
634
  json_obj.leave();
635

636
  return strdup(jb.string_builder().as_cslice().c_str());
637
}
638

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());
643
  }
644

645
  auto emulator = static_cast<emulator::TvmEmulator *>(tvm_emulator);
646
  auto result = emulator->send_internal_message(message_body_cell.move_as_ok(), amount);
647

648
  td::JsonBuilder jb;
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());
657
  } else {
658
    json_obj("missing_library", result.missing_library.value().to_hex());
659
  }
660
  if (result.actions.is_null()) {
661
    json_obj("actions", td::JsonNull());
662
  } else {
663
    json_obj("actions", cell_to_boc_b64(result.actions).move_as_ok());
664
  }
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());
667
  json_obj.leave();
668

669
  return strdup(jb.string_builder().as_cslice().c_str());
670
}
671

672
void tvm_emulator_destroy(void *tvm_emulator) {
673
  delete static_cast<emulator::TvmEmulator *>(tvm_emulator);
674
}
675

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.