Skip to content

Commit dd5540d

Browse files
authored
Single call optimized runGetMethod emulator (#920)
* TVM Emulator optimized output method * TVM Emulator single call version * Fixed tvm_emulator export, changed to tvm_emulator_emulate * Removed imports * Set C7 from outside * Removed tvm_emulator_run_get_method_optimized * Renamed tvm_emulator_emulate to tvm_emulator_emulate_run_method
1 parent 200508c commit dd5540d

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

emulator/emulator-extern.cpp

+60
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,66 @@ const char *tvm_emulator_run_get_method(void *tvm_emulator, int method_id, const
543543
return strdup(jb.string_builder().as_cslice().c_str());
544544
}
545545

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+
546606
const char *tvm_emulator_send_external_message(void *tvm_emulator, const char *message_body_boc) {
547607
auto message_body_cell = boc_b64_to_cell(message_body_boc);
548608
if (message_body_cell.is_error()) {

emulator/emulator-extern.h

+10
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,16 @@ EMULATOR_EXPORT bool tvm_emulator_set_debug_enabled(void *tvm_emulator, bool deb
213213
*/
214214
EMULATOR_EXPORT const char *tvm_emulator_run_get_method(void *tvm_emulator, int method_id, const char *stack_boc);
215215

216+
/**
217+
* @brief Optimized version of "run get method" with all passed parameters in a single call
218+
* @param len Length of params_boc buffer
219+
* @param params_boc BoC serialized parameters, scheme: request$_ code:^Cell data:^Cell stack:^VmStack params:^[c7:^VmStack libs:^Cell] method_id:(## 32)
220+
* @param gas_limit Gas limit
221+
* @return Char* with first 4 bytes defining length, and the rest BoC serialized result
222+
* Scheme: result$_ exit_code:(## 32) gas_used:(## 32) stack:^VmStack
223+
*/
224+
EMULATOR_EXPORT const char *tvm_emulator_emulate_run_method(uint32_t len, const char *params_boc, int64_t gas_limit);
225+
216226
/**
217227
* @brief Send external message
218228
* @param tvm_emulator Pointer to TVM emulator

emulator/emulator_export_list

+1
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ _tvm_emulator_run_get_method
2121
_tvm_emulator_send_external_message
2222
_tvm_emulator_send_internal_message
2323
_tvm_emulator_destroy
24+
_tvm_emulator_emulate_run_method

emulator/tvm-emulator.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ class TvmEmulator {
3333
}
3434
}
3535

36+
void set_c7_raw(td::Ref<vm::Tuple> c7) {
37+
args_.set_c7(std::move(c7));
38+
}
39+
3640
void set_prev_blocks_info(td::Ref<vm::Tuple> tuple) {
3741
args_.set_prev_blocks_info(std::move(tuple));
3842
}

0 commit comments

Comments
 (0)