forked from ston-fi/ton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemulator-extern.cpp
729 lines (624 loc) · 28.2 KB
/
emulator-extern.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
#include "emulator-extern.h"
#include "td/utils/base64.h"
#include "td/utils/Status.h"
#include "td/utils/JsonBuilder.h"
#include "td/utils/logging.h"
#include "td/utils/Variant.h"
#include "td/utils/overloaded.h"
#include "transaction-emulator.h"
#include "tvm-emulator.hpp"
#include "crypto/vm/stack.hpp"
#include "crypto/vm/memo.h"
#include "git.h"
td::Result<td::Ref<vm::Cell>> boc_b64_to_cell(const char *boc) {
TRY_RESULT_PREFIX(boc_decoded, td::base64_decode(td::Slice(boc)), "Can't decode base64 boc: ");
return vm::std_boc_deserialize(boc_decoded);
}
td::Result<std::string> cell_to_boc_b64(td::Ref<vm::Cell> cell) {
TRY_RESULT_PREFIX(boc, vm::std_boc_serialize(std::move(cell), vm::BagOfCells::Mode::WithCRC32C), "Can't serialize cell: ");
return td::base64_encode(boc.as_slice());
}
const char *success_response(std::string&& transaction, std::string&& new_shard_account, std::string&& vm_log,
td::optional<std::string>&& actions, double elapsed_time) {
td::JsonBuilder jb;
auto json_obj = jb.enter_object();
json_obj("success", td::JsonTrue());
json_obj("transaction", std::move(transaction));
json_obj("shard_account", std::move(new_shard_account));
json_obj("vm_log", std::move(vm_log));
if (actions) {
json_obj("actions", actions.unwrap());
} else {
json_obj("actions", td::JsonNull());
}
json_obj("elapsed_time", elapsed_time);
json_obj.leave();
return strdup(jb.string_builder().as_cslice().c_str());
}
const char *error_response(std::string&& error) {
td::JsonBuilder jb;
auto json_obj = jb.enter_object();
json_obj("success", td::JsonFalse());
json_obj("error", std::move(error));
json_obj("external_not_accepted", td::JsonFalse());
json_obj.leave();
return strdup(jb.string_builder().as_cslice().c_str());
}
const char *external_not_accepted_response(std::string&& vm_log, int vm_exit_code, double elapsed_time) {
td::JsonBuilder jb;
auto json_obj = jb.enter_object();
json_obj("success", td::JsonFalse());
json_obj("error", "External message not accepted by smart contract");
json_obj("external_not_accepted", td::JsonTrue());
json_obj("vm_log", std::move(vm_log));
json_obj("vm_exit_code", vm_exit_code);
json_obj("elapsed_time", elapsed_time);
json_obj.leave();
return strdup(jb.string_builder().as_cslice().c_str());
}
#define ERROR_RESPONSE(error) return error_response(error)
td::Result<block::Config> decode_config(const char* config_boc) {
TRY_RESULT_PREFIX(config_params_cell, boc_b64_to_cell(config_boc), "Can't deserialize config params boc: ");
auto config_dict = std::make_unique<vm::Dictionary>(config_params_cell, 32);
auto config_addr_cell = config_dict->lookup_ref(td::BitArray<32>::zero());
if (config_addr_cell.is_null()) {
return td::Status::Error("Can't find config address (param 0) is missing in config params");
}
auto config_addr_cs = vm::load_cell_slice(std::move(config_addr_cell));
if (config_addr_cs.size() != 0x100) {
return td::Status::Error(PSLICE() << "configuration parameter 0 with config address has wrong size");
}
ton::StdSmcAddress config_addr;
config_addr_cs.fetch_bits_to(config_addr);
auto global_config = block::Config(config_params_cell, std::move(config_addr), block::Config::needWorkchainInfo | block::Config::needSpecialSmc | block::Config::needCapabilities);
TRY_STATUS_PREFIX(global_config.unpack(), "Can't unpack config params: ");
return global_config;
}
void *transaction_emulator_create(const char *config_params_boc, int vm_log_verbosity) {
auto global_config_res = decode_config(config_params_boc);
if (global_config_res.is_error()) {
LOG(ERROR) << global_config_res.move_as_error().message();
return nullptr;
}
auto global_config = std::make_shared<block::Config>(global_config_res.move_as_ok());
return new emulator::TransactionEmulator(std::move(global_config), vm_log_verbosity);
}
void *emulator_config_create(const char *config_params_boc) {
auto config = decode_config(config_params_boc);
if (config.is_error()) {
LOG(ERROR) << "Error decoding config: " << config.move_as_error();
return nullptr;
}
return new block::Config(config.move_as_ok());
}
const char *transaction_emulator_emulate_transaction(void *transaction_emulator, const char *shard_account_boc, const char *message_boc) {
auto emulator = static_cast<emulator::TransactionEmulator *>(transaction_emulator);
auto message_cell_r = boc_b64_to_cell(message_boc);
if (message_cell_r.is_error()) {
ERROR_RESPONSE(PSTRING() << "Can't deserialize message boc: " << message_cell_r.move_as_error());
}
auto message_cell = message_cell_r.move_as_ok();
auto message_cs = vm::load_cell_slice(message_cell);
int msg_tag = block::gen::t_CommonMsgInfo.get_tag(message_cs);
auto shard_account_cell = boc_b64_to_cell(shard_account_boc);
if (shard_account_cell.is_error()) {
ERROR_RESPONSE(PSTRING() << "Can't deserialize shard account boc: " << shard_account_cell.move_as_error());
}
auto shard_account_slice = vm::load_cell_slice(shard_account_cell.ok_ref());
block::gen::ShardAccount::Record shard_account;
if (!tlb::unpack(shard_account_slice, shard_account)) {
ERROR_RESPONSE(PSTRING() << "Can't unpack shard account cell");
}
td::Ref<vm::CellSlice> addr_slice;
auto account_slice = vm::load_cell_slice(shard_account.account);
bool account_exists = block::gen::t_Account.get_tag(account_slice) == block::gen::Account::account;
if (block::gen::t_Account.get_tag(account_slice) == block::gen::Account::account_none) {
if (msg_tag == block::gen::CommonMsgInfo::ext_in_msg_info) {
block::gen::CommonMsgInfo::Record_ext_in_msg_info info;
if (!tlb::unpack(message_cs, info)) {
ERROR_RESPONSE(PSTRING() << "Can't unpack inbound external message");
}
addr_slice = std::move(info.dest);
}
else if (msg_tag == block::gen::CommonMsgInfo::int_msg_info) {
block::gen::CommonMsgInfo::Record_int_msg_info info;
if (!tlb::unpack(message_cs, info)) {
ERROR_RESPONSE(PSTRING() << "Can't unpack inbound internal message");
}
addr_slice = std::move(info.dest);
} else {
ERROR_RESPONSE(PSTRING() << "Only ext in and int message are supported");
}
} else if (block::gen::t_Account.get_tag(account_slice) == block::gen::Account::account) {
block::gen::Account::Record_account account_record;
if (!tlb::unpack(account_slice, account_record)) {
ERROR_RESPONSE(PSTRING() << "Can't unpack account cell");
}
addr_slice = std::move(account_record.addr);
} else {
ERROR_RESPONSE(PSTRING() << "Can't parse account cell");
}
ton::WorkchainId wc;
ton::StdSmcAddress addr;
if (!block::tlb::t_MsgAddressInt.extract_std_address(addr_slice, wc, addr)) {
ERROR_RESPONSE(PSTRING() << "Can't extract account address");
}
auto account = block::Account(wc, addr.bits());
ton::UnixTime now = emulator->get_unixtime();
if (!now) {
now = (unsigned)std::time(nullptr);
}
bool is_special = wc == ton::masterchainId && emulator->get_config().is_special_smartcontract(addr);
if (account_exists) {
if (!account.unpack(vm::load_cell_slice_ref(shard_account_cell.move_as_ok()), now, is_special)) {
ERROR_RESPONSE(PSTRING() << "Can't unpack shard account");
}
} else {
if (!account.init_new(now)) {
ERROR_RESPONSE(PSTRING() << "Can't init new account");
}
account.last_trans_lt_ = shard_account.last_trans_lt;
account.last_trans_hash_ = shard_account.last_trans_hash;
}
auto result = emulator->emulate_transaction(std::move(account), message_cell, now, 0, block::transaction::Transaction::tr_ord);
if (result.is_error()) {
ERROR_RESPONSE(PSTRING() << "Emulate transaction failed: " << result.move_as_error());
}
auto emulation_result = result.move_as_ok();
auto external_not_accepted = dynamic_cast<emulator::TransactionEmulator::EmulationExternalNotAccepted *>(emulation_result.get());
if (external_not_accepted) {
return external_not_accepted_response(std::move(external_not_accepted->vm_log), external_not_accepted->vm_exit_code,
external_not_accepted->elapsed_time);
}
auto emulation_success = dynamic_cast<emulator::TransactionEmulator::EmulationSuccess&>(*emulation_result);
auto trans_boc_b64 = cell_to_boc_b64(std::move(emulation_success.transaction));
if (trans_boc_b64.is_error()) {
ERROR_RESPONSE(PSTRING() << "Can't serialize Transaction to boc " << trans_boc_b64.move_as_error());
}
auto new_shard_account_cell = vm::CellBuilder().store_ref(emulation_success.account.total_state)
.store_bits(emulation_success.account.last_trans_hash_.as_bitslice())
.store_long(emulation_success.account.last_trans_lt_).finalize();
auto new_shard_account_boc_b64 = cell_to_boc_b64(std::move(new_shard_account_cell));
if (new_shard_account_boc_b64.is_error()) {
ERROR_RESPONSE(PSTRING() << "Can't serialize ShardAccount to boc " << new_shard_account_boc_b64.move_as_error());
}
td::optional<td::string> actions_boc_b64;
if (emulation_success.actions.not_null()) {
auto actions_boc_b64_result = cell_to_boc_b64(std::move(emulation_success.actions));
if (actions_boc_b64_result.is_error()) {
ERROR_RESPONSE(PSTRING() << "Can't serialize actions list cell to boc " << actions_boc_b64_result.move_as_error());
}
actions_boc_b64 = actions_boc_b64_result.move_as_ok();
}
return success_response(trans_boc_b64.move_as_ok(), new_shard_account_boc_b64.move_as_ok(), std::move(emulation_success.vm_log),
std::move(actions_boc_b64), emulation_success.elapsed_time);
}
const char *transaction_emulator_emulate_tick_tock_transaction(void *transaction_emulator, const char *shard_account_boc, bool is_tock) {
auto emulator = static_cast<emulator::TransactionEmulator *>(transaction_emulator);
auto shard_account_cell = boc_b64_to_cell(shard_account_boc);
if (shard_account_cell.is_error()) {
ERROR_RESPONSE(PSTRING() << "Can't deserialize shard account boc: " << shard_account_cell.move_as_error());
}
auto shard_account_slice = vm::load_cell_slice(shard_account_cell.ok_ref());
block::gen::ShardAccount::Record shard_account;
if (!tlb::unpack(shard_account_slice, shard_account)) {
ERROR_RESPONSE(PSTRING() << "Can't unpack shard account cell");
}
td::Ref<vm::CellSlice> addr_slice;
auto account_slice = vm::load_cell_slice(shard_account.account);
if (block::gen::t_Account.get_tag(account_slice) == block::gen::Account::account_none) {
ERROR_RESPONSE(PSTRING() << "Can't run tick/tock transaction on account_none");
}
block::gen::Account::Record_account account_record;
if (!tlb::unpack(account_slice, account_record)) {
ERROR_RESPONSE(PSTRING() << "Can't unpack account cell");
}
addr_slice = std::move(account_record.addr);
ton::WorkchainId wc;
ton::StdSmcAddress addr;
if (!block::tlb::t_MsgAddressInt.extract_std_address(addr_slice, wc, addr)) {
ERROR_RESPONSE(PSTRING() << "Can't extract account address");
}
auto account = block::Account(wc, addr.bits());
ton::UnixTime now = emulator->get_unixtime();
if (!now) {
now = (unsigned)std::time(nullptr);
}
bool is_special = wc == ton::masterchainId && emulator->get_config().is_special_smartcontract(addr);
if (!account.unpack(vm::load_cell_slice_ref(shard_account_cell.move_as_ok()), now, is_special)) {
ERROR_RESPONSE(PSTRING() << "Can't unpack shard account");
}
auto trans_type = is_tock ? block::transaction::Transaction::tr_tock : block::transaction::Transaction::tr_tick;
auto result = emulator->emulate_transaction(std::move(account), {}, now, 0, trans_type);
if (result.is_error()) {
ERROR_RESPONSE(PSTRING() << "Emulate transaction failed: " << result.move_as_error());
}
auto emulation_result = result.move_as_ok();
auto emulation_success = dynamic_cast<emulator::TransactionEmulator::EmulationSuccess&>(*emulation_result);
auto trans_boc_b64 = cell_to_boc_b64(std::move(emulation_success.transaction));
if (trans_boc_b64.is_error()) {
ERROR_RESPONSE(PSTRING() << "Can't serialize Transaction to boc " << trans_boc_b64.move_as_error());
}
auto new_shard_account_cell = vm::CellBuilder().store_ref(emulation_success.account.total_state)
.store_bits(emulation_success.account.last_trans_hash_.as_bitslice())
.store_long(emulation_success.account.last_trans_lt_).finalize();
auto new_shard_account_boc_b64 = cell_to_boc_b64(std::move(new_shard_account_cell));
if (new_shard_account_boc_b64.is_error()) {
ERROR_RESPONSE(PSTRING() << "Can't serialize ShardAccount to boc " << new_shard_account_boc_b64.move_as_error());
}
td::optional<td::string> actions_boc_b64;
if (emulation_success.actions.not_null()) {
auto actions_boc_b64_result = cell_to_boc_b64(std::move(emulation_success.actions));
if (actions_boc_b64_result.is_error()) {
ERROR_RESPONSE(PSTRING() << "Can't serialize actions list cell to boc " << actions_boc_b64_result.move_as_error());
}
actions_boc_b64 = actions_boc_b64_result.move_as_ok();
}
return success_response(trans_boc_b64.move_as_ok(), new_shard_account_boc_b64.move_as_ok(), std::move(emulation_success.vm_log),
std::move(actions_boc_b64), emulation_success.elapsed_time);
}
bool transaction_emulator_set_unixtime(void *transaction_emulator, uint32_t unixtime) {
auto emulator = static_cast<emulator::TransactionEmulator *>(transaction_emulator);
emulator->set_unixtime(unixtime);
return true;
}
bool transaction_emulator_set_lt(void *transaction_emulator, uint64_t lt) {
auto emulator = static_cast<emulator::TransactionEmulator *>(transaction_emulator);
emulator->set_lt(lt);
return true;
}
bool transaction_emulator_set_rand_seed(void *transaction_emulator, const char* rand_seed_hex) {
auto emulator = static_cast<emulator::TransactionEmulator *>(transaction_emulator);
auto rand_seed_hex_slice = td::Slice(rand_seed_hex);
if (rand_seed_hex_slice.size() != 64) {
LOG(ERROR) << "Rand seed expected as 64 characters hex string";
return false;
}
auto rand_seed_bytes = td::hex_decode(rand_seed_hex_slice);
if (rand_seed_bytes.is_error()) {
LOG(ERROR) << "Can't decode hex rand seed";
return false;
}
td::BitArray<256> rand_seed;
rand_seed.as_slice().copy_from(rand_seed_bytes.move_as_ok());
emulator->set_rand_seed(rand_seed);
return true;
}
bool transaction_emulator_set_ignore_chksig(void *transaction_emulator, bool ignore_chksig) {
auto emulator = static_cast<emulator::TransactionEmulator *>(transaction_emulator);
emulator->set_ignore_chksig(ignore_chksig);
return true;
}
bool transaction_emulator_set_config(void *transaction_emulator, const char* config_boc) {
auto emulator = static_cast<emulator::TransactionEmulator *>(transaction_emulator);
auto global_config_res = decode_config(config_boc);
if (global_config_res.is_error()) {
LOG(ERROR) << global_config_res.move_as_error().message();
return false;
}
emulator->set_config(std::make_shared<block::Config>(global_config_res.move_as_ok()));
return true;
}
void config_deleter(block::Config* ptr) {
// We do not delete the config object, since ownership management is delegated to the caller
}
bool transaction_emulator_set_config_object(void *transaction_emulator, void* config) {
auto emulator = static_cast<emulator::TransactionEmulator *>(transaction_emulator);
std::shared_ptr<block::Config> config_ptr(static_cast<block::Config *>(config), config_deleter);
emulator->set_config(config_ptr);
return true;
}
bool transaction_emulator_set_libs(void *transaction_emulator, const char* shardchain_libs_boc) {
auto emulator = static_cast<emulator::TransactionEmulator *>(transaction_emulator);
if (shardchain_libs_boc != nullptr) {
auto shardchain_libs_cell = boc_b64_to_cell(shardchain_libs_boc);
if (shardchain_libs_cell.is_error()) {
LOG(ERROR) << "Can't deserialize shardchain libraries boc: " << shardchain_libs_cell.move_as_error();
return false;
}
emulator->set_libs(vm::Dictionary(shardchain_libs_cell.move_as_ok(), 256));
}
return true;
}
bool transaction_emulator_set_debug_enabled(void *transaction_emulator, bool debug_enabled) {
auto emulator = static_cast<emulator::TransactionEmulator *>(transaction_emulator);
emulator->set_debug_enabled(debug_enabled);
return true;
}
bool transaction_emulator_set_prev_blocks_info(void *transaction_emulator, const char* info_boc) {
auto emulator = static_cast<emulator::TransactionEmulator *>(transaction_emulator);
if (info_boc != nullptr) {
auto info_cell = boc_b64_to_cell(info_boc);
if (info_cell.is_error()) {
LOG(ERROR) << "Can't deserialize previous blocks boc: " << info_cell.move_as_error();
return false;
}
vm::StackEntry info_value;
if (!info_value.deserialize(info_cell.move_as_ok())) {
LOG(ERROR) << "Can't deserialize previous blocks tuple";
return false;
}
if (info_value.is_null()) {
emulator->set_prev_blocks_info({});
} else if (info_value.is_tuple()) {
emulator->set_prev_blocks_info(info_value.as_tuple());
} else {
LOG(ERROR) << "Can't set previous blocks tuple: not a tuple";
return false;
}
}
return true;
}
void transaction_emulator_destroy(void *transaction_emulator) {
delete static_cast<emulator::TransactionEmulator *>(transaction_emulator);
}
bool emulator_set_verbosity_level(int verbosity_level) {
if (0 <= verbosity_level && verbosity_level <= VERBOSITY_NAME(NEVER)) {
SET_VERBOSITY_LEVEL(VERBOSITY_NAME(FATAL) + verbosity_level);
return true;
}
return false;
}
void *tvm_emulator_create(const char *code, const char *data, int vm_log_verbosity) {
auto code_cell = boc_b64_to_cell(code);
if (code_cell.is_error()) {
LOG(ERROR) << "Can't deserialize code boc: " << code_cell.move_as_error();
return nullptr;
}
auto data_cell = boc_b64_to_cell(data);
if (data_cell.is_error()) {
LOG(ERROR) << "Can't deserialize code boc: " << data_cell.move_as_error();
return nullptr;
}
auto emulator = new emulator::TvmEmulator(code_cell.move_as_ok(), data_cell.move_as_ok());
emulator->set_vm_verbosity_level(vm_log_verbosity);
return emulator;
}
bool tvm_emulator_set_libraries(void *tvm_emulator, const char *libs_boc) {
vm::Dictionary libs{256};
auto libs_cell = boc_b64_to_cell(libs_boc);
if (libs_cell.is_error()) {
LOG(ERROR) << "Can't deserialize libraries boc: " << libs_cell.move_as_error();
return false;
}
libs = vm::Dictionary(libs_cell.move_as_ok(), 256);
auto emulator = static_cast<emulator::TvmEmulator *>(tvm_emulator);
emulator->set_libraries(std::move(libs));
return true;
}
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) {
auto emulator = static_cast<emulator::TvmEmulator *>(tvm_emulator);
auto std_address = block::StdAddress::parse(td::Slice(address));
if (std_address.is_error()) {
LOG(ERROR) << "Can't parse address: " << std_address.move_as_error();
return false;
}
std::shared_ptr<block::Config> global_config;
if (config_boc != nullptr) {
auto config_params_cell = boc_b64_to_cell(config_boc);
if (config_params_cell.is_error()) {
LOG(ERROR) << "Can't deserialize config params boc: " << config_params_cell.move_as_error();
return false;
}
global_config = std::make_shared<block::Config>(
config_params_cell.move_as_ok(), td::Bits256::zero(),
block::Config::needWorkchainInfo | block::Config::needSpecialSmc | block::Config::needCapabilities);
auto unpack_res = global_config->unpack();
if (unpack_res.is_error()) {
LOG(ERROR) << "Can't unpack config params";
return false;
}
}
auto rand_seed_hex_slice = td::Slice(rand_seed_hex);
if (rand_seed_hex_slice.size() != 64) {
LOG(ERROR) << "Rand seed expected as 64 characters hex string";
return false;
}
auto rand_seed_bytes = td::hex_decode(rand_seed_hex_slice);
if (rand_seed_bytes.is_error()) {
LOG(ERROR) << "Can't decode hex rand seed";
return false;
}
td::BitArray<256> rand_seed;
rand_seed.as_slice().copy_from(rand_seed_bytes.move_as_ok());
emulator->set_c7(std_address.move_as_ok(), unixtime, balance, rand_seed, std::const_pointer_cast<const block::Config>(global_config));
return true;
}
bool tvm_emulator_set_config_object(void* tvm_emulator, void* config) {
auto emulator = static_cast<emulator::TvmEmulator *>(tvm_emulator);
auto global_config = std::shared_ptr<block::Config>(static_cast<block::Config *>(config), config_deleter);
emulator->set_config(global_config);
return true;
}
bool tvm_emulator_set_prev_blocks_info(void *tvm_emulator, const char* info_boc) {
auto emulator = static_cast<emulator::TvmEmulator *>(tvm_emulator);
if (info_boc != nullptr) {
auto info_cell = boc_b64_to_cell(info_boc);
if (info_cell.is_error()) {
LOG(ERROR) << "Can't deserialize previous blocks boc: " << info_cell.move_as_error();
return false;
}
vm::StackEntry info_value;
if (!info_value.deserialize(info_cell.move_as_ok())) {
LOG(ERROR) << "Can't deserialize previous blocks tuple";
return false;
}
if (info_value.is_null()) {
emulator->set_prev_blocks_info({});
} else if (info_value.is_tuple()) {
emulator->set_prev_blocks_info(info_value.as_tuple());
} else {
LOG(ERROR) << "Can't set previous blocks tuple: not a tuple";
return false;
}
}
return true;
}
bool tvm_emulator_set_gas_limit(void *tvm_emulator, int64_t gas_limit) {
auto emulator = static_cast<emulator::TvmEmulator *>(tvm_emulator);
emulator->set_gas_limit(gas_limit);
return true;
}
bool tvm_emulator_set_debug_enabled(void *tvm_emulator, bool debug_enabled) {
auto emulator = static_cast<emulator::TvmEmulator *>(tvm_emulator);
emulator->set_debug_enabled(debug_enabled);
return true;
}
const char *tvm_emulator_run_get_method(void *tvm_emulator, int method_id, const char *stack_boc) {
auto stack_cell = boc_b64_to_cell(stack_boc);
if (stack_cell.is_error()) {
ERROR_RESPONSE(PSTRING() << "Couldn't deserialize stack cell: " << stack_cell.move_as_error().to_string());
}
auto stack_cs = vm::load_cell_slice(stack_cell.move_as_ok());
td::Ref<vm::Stack> stack;
if (!vm::Stack::deserialize_to(stack_cs, stack)) {
ERROR_RESPONSE(PSTRING() << "Couldn't deserialize stack");
}
auto emulator = static_cast<emulator::TvmEmulator *>(tvm_emulator);
auto result = emulator->run_get_method(method_id, stack);
vm::FakeVmStateLimits fstate(3500); // limit recursive (de)serialization calls
vm::VmStateInterface::Guard guard(&fstate);
vm::CellBuilder stack_cb;
if (!result.stack->serialize(stack_cb)) {
ERROR_RESPONSE(PSTRING() << "Couldn't serialize stack");
}
auto result_stack_boc = cell_to_boc_b64(stack_cb.finalize());
if (result_stack_boc.is_error()) {
ERROR_RESPONSE(PSTRING() << "Couldn't serialize stack cell: " << result_stack_boc.move_as_error().to_string());
}
td::JsonBuilder jb;
auto json_obj = jb.enter_object();
json_obj("success", td::JsonTrue());
json_obj("stack", result_stack_boc.move_as_ok());
json_obj("gas_used", std::to_string(result.gas_used));
json_obj("vm_exit_code", result.code);
json_obj("vm_log", result.vm_log);
if (!result.missing_library) {
json_obj("missing_library", td::JsonNull());
} else {
json_obj("missing_library", result.missing_library.value().to_hex());
}
json_obj.leave();
return strdup(jb.string_builder().as_cslice().c_str());
}
const char *tvm_emulator_emulate_run_method(uint32_t len, const char *params_boc, int64_t gas_limit) {
auto params_cell = vm::std_boc_deserialize(td::Slice(params_boc, len));
if (params_cell.is_error()) {
return nullptr;
}
auto params_cs = vm::load_cell_slice(params_cell.move_as_ok());
auto code = params_cs.fetch_ref();
auto data = params_cs.fetch_ref();
auto stack_cs = vm::load_cell_slice(params_cs.fetch_ref());
auto params = vm::load_cell_slice(params_cs.fetch_ref());
auto c7_cs = vm::load_cell_slice(params.fetch_ref());
auto libs = vm::Dictionary(params.fetch_ref(), 256);
auto method_id = params_cs.fetch_long(32);
td::Ref<vm::Stack> stack;
if (!vm::Stack::deserialize_to(stack_cs, stack)) {
return nullptr;
}
td::Ref<vm::Stack> c7;
if (!vm::Stack::deserialize_to(c7_cs, c7)) {
return nullptr;
}
auto emulator = new emulator::TvmEmulator(code, data);
emulator->set_vm_verbosity_level(0);
emulator->set_gas_limit(gas_limit);
emulator->set_c7_raw(c7->fetch(0).as_tuple());
if (libs.is_empty()) {
emulator->set_libraries(std::move(libs));
}
auto result = emulator->run_get_method(int(method_id), stack);
delete emulator;
vm::CellBuilder stack_cb;
if (!result.stack->serialize(stack_cb)) {
return nullptr;
}
vm::CellBuilder cb;
cb.store_long(result.code, 32);
cb.store_long(result.gas_used, 64);
cb.store_ref(stack_cb.finalize());
auto ser = vm::std_boc_serialize(cb.finalize());
if (!ser.is_ok()) {
return nullptr;
}
auto sok = ser.move_as_ok();
auto sz = uint32_t(sok.size());
char* rn = (char*)malloc(sz + 4);
memcpy(rn, &sz, 4);
memcpy(rn+4, sok.data(), sz);
return rn;
}
const char *tvm_emulator_send_external_message(void *tvm_emulator, const char *message_body_boc) {
auto message_body_cell = boc_b64_to_cell(message_body_boc);
if (message_body_cell.is_error()) {
ERROR_RESPONSE(PSTRING() << "Can't deserialize message body boc: " << message_body_cell.move_as_error());
}
auto emulator = static_cast<emulator::TvmEmulator *>(tvm_emulator);
auto result = emulator->send_external_message(message_body_cell.move_as_ok());
td::JsonBuilder jb;
auto json_obj = jb.enter_object();
json_obj("success", td::JsonTrue());
json_obj("gas_used", std::to_string(result.gas_used));
json_obj("vm_exit_code", result.code);
json_obj("accepted", td::JsonBool(result.accepted));
json_obj("vm_log", result.vm_log);
if (!result.missing_library) {
json_obj("missing_library", td::JsonNull());
} else {
json_obj("missing_library", result.missing_library.value().to_hex());
}
if (result.actions.is_null()) {
json_obj("actions", td::JsonNull());
} else {
json_obj("actions", cell_to_boc_b64(result.actions).move_as_ok());
}
json_obj("new_code", cell_to_boc_b64(result.new_state.code).move_as_ok());
json_obj("new_data", cell_to_boc_b64(result.new_state.data).move_as_ok());
json_obj.leave();
return strdup(jb.string_builder().as_cslice().c_str());
}
const char *tvm_emulator_send_internal_message(void *tvm_emulator, const char *message_body_boc, uint64_t amount) {
auto message_body_cell = boc_b64_to_cell(message_body_boc);
if (message_body_cell.is_error()) {
ERROR_RESPONSE(PSTRING() << "Can't deserialize message body boc: " << message_body_cell.move_as_error());
}
auto emulator = static_cast<emulator::TvmEmulator *>(tvm_emulator);
auto result = emulator->send_internal_message(message_body_cell.move_as_ok(), amount);
td::JsonBuilder jb;
auto json_obj = jb.enter_object();
json_obj("success", td::JsonTrue());
json_obj("gas_used", std::to_string(result.gas_used));
json_obj("vm_exit_code", result.code);
json_obj("accepted", td::JsonBool(result.accepted));
json_obj("vm_log", result.vm_log);
if (!result.missing_library) {
json_obj("missing_library", td::JsonNull());
} else {
json_obj("missing_library", result.missing_library.value().to_hex());
}
if (result.actions.is_null()) {
json_obj("actions", td::JsonNull());
} else {
json_obj("actions", cell_to_boc_b64(result.actions).move_as_ok());
}
json_obj("new_code", cell_to_boc_b64(result.new_state.code).move_as_ok());
json_obj("new_data", cell_to_boc_b64(result.new_state.data).move_as_ok());
json_obj.leave();
return strdup(jb.string_builder().as_cslice().c_str());
}
void tvm_emulator_destroy(void *tvm_emulator) {
delete static_cast<emulator::TvmEmulator *>(tvm_emulator);
}
void emulator_config_destroy(void *config) {
delete static_cast<block::Config *>(config);
}
const char* emulator_version() {
auto version_json = td::JsonBuilder();
auto obj = version_json.enter_object();
obj("emulatorLibCommitHash", GitMetadata::CommitSHA1());
obj("emulatorLibCommitDate", GitMetadata::CommitDate());
obj.leave();
return strdup(version_json.string_builder().as_cslice().c_str());
}