Skip to content

Commit 403003c

Browse files
committed
feat: Added tvm_emulator_emulate_run_method clone with logs available
1 parent 9d9b016 commit 403003c

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

emulator/emulator-extern.cpp

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -639,10 +639,16 @@ const char *tvm_emulator_run_get_method(void *tvm_emulator, int method_id, const
639639
return strdup(jb.string_builder().as_cslice().c_str());
640640
}
641641

642-
const char *tvm_emulator_emulate_run_method(uint32_t len, const char *params_boc, int64_t gas_limit) {
642+
struct TvmEulatorEmulateRunMethodResponse
643+
{
644+
const char *response;
645+
const char *log;
646+
};
647+
648+
TvmEulatorEmulateRunMethodResponse emulate_run_method(uint32_t len, const char *params_boc, int64_t gas_limit) {
643649
auto params_cell = vm::std_boc_deserialize(td::Slice(params_boc, len));
644650
if (params_cell.is_error()) {
645-
return nullptr;
651+
return { nullptr, nullptr };
646652
}
647653
auto params_cs = vm::load_cell_slice(params_cell.move_as_ok());
648654
auto code = params_cs.fetch_ref();
@@ -657,12 +663,12 @@ const char *tvm_emulator_emulate_run_method(uint32_t len, const char *params_boc
657663

658664
td::Ref<vm::Stack> stack;
659665
if (!vm::Stack::deserialize_to(stack_cs, stack)) {
660-
return nullptr;
666+
return { nullptr, nullptr };
661667
}
662668

663669
td::Ref<vm::Stack> c7;
664670
if (!vm::Stack::deserialize_to(c7_cs, c7)) {
665-
return nullptr;
671+
return { nullptr, nullptr };
666672
}
667673

668674
auto emulator = new emulator::TvmEmulator(code, data);
@@ -692,7 +698,7 @@ const char *tvm_emulator_emulate_run_method(uint32_t len, const char *params_boc
692698
td::Status unpack_res = config_ptr->unpack();
693699
if (unpack_res.is_error()) {
694700
LOG(ERROR) << "Can't unpack config params";
695-
return nullptr;
701+
return { nullptr, nullptr };
696702
}
697703
emulator->set_config(config_ptr);
698704
}
@@ -708,7 +714,7 @@ const char *tvm_emulator_emulate_run_method(uint32_t len, const char *params_boc
708714

709715
vm::CellBuilder stack_cb;
710716
if (!result.stack->serialize(stack_cb)) {
711-
return nullptr;
717+
return { nullptr, nullptr };
712718
}
713719

714720
vm::CellBuilder cb;
@@ -718,7 +724,7 @@ const char *tvm_emulator_emulate_run_method(uint32_t len, const char *params_boc
718724

719725
auto ser = vm::std_boc_serialize(cb.finalize());
720726
if (!ser.is_ok()) {
721-
return nullptr;
727+
return { nullptr, nullptr };
722728
}
723729
auto sok = ser.move_as_ok();
724730

@@ -727,7 +733,26 @@ const char *tvm_emulator_emulate_run_method(uint32_t len, const char *params_boc
727733
memcpy(rn, &sz, 4);
728734
memcpy(rn+4, sok.data(), sz);
729735

730-
return rn;
736+
auto log_sz = uint32_t(result.vm_log.size());
737+
char* log_buffer = (char*)malloc(log_sz + 4);
738+
memcpy(log_buffer, &log_sz, 4);
739+
memcpy(log_buffer+4, result.vm_log.data(), log_sz);
740+
741+
return { rn, log_buffer };
742+
}
743+
744+
const char *tvm_emulator_emulate_run_method(uint32_t len, const char *params_boc, int64_t gas_limit) {
745+
auto result = emulate_run_method(len, params_boc, gas_limit);
746+
return result.response;
747+
}
748+
749+
const char *tvm_emulator_emulate_run_method_detailed(uint32_t len, const char *params_boc, int64_t gas_limit) {
750+
auto result = emulate_run_method(len, params_boc, gas_limit);
751+
TvmEulatorEmulateRunMethodResponse* response_ptr =
752+
(TvmEulatorEmulateRunMethodResponse*)malloc(sizeof(TvmEulatorEmulateRunMethodResponse));
753+
response_ptr->response = result.response;
754+
response_ptr->log = result.log;
755+
return reinterpret_cast<const char*>(response_ptr);
731756
}
732757

733758
const char *tvm_emulator_send_external_message(void *tvm_emulator, const char *message_body_boc) {

emulator/emulator-extern.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,18 @@ EMULATOR_EXPORT const char *tvm_emulator_run_get_method(void *tvm_emulator, int
254254
*/
255255
EMULATOR_EXPORT const char *tvm_emulator_emulate_run_method(uint32_t len, const char *params_boc, int64_t gas_limit);
256256

257+
/**
258+
* @brief Optimized version of "run get method" with all passed parameters in a single call. Also returns log.
259+
* @param len Length of params_boc buffer
260+
* @param params_boc BoC serialized parameters, scheme: request$_ code:^Cell data:^Cell stack:^VmStack params:^[c7:^VmStack libs:^Cell] method_id:(## 32)
261+
* @param gas_limit Gas limit
262+
* @return Struct with two fields:
263+
* - response: Char* with first 4 bytes defining length, and the rest BoC serialized result
264+
* Scheme: result$_ exit_code:(## 32) gas_used:(## 32) stack:^VmStack
265+
* - log: Char* with first 4 bytes defining length, and the rest is log
266+
*/
267+
EMULATOR_EXPORT const char *tvm_emulator_emulate_run_method_detailed(uint32_t len, const char *params_boc, int64_t gas_limit);
268+
257269
/**
258270
* @brief Send external message
259271
* @param tvm_emulator Pointer to TVM emulator

emulator/emulator_export_list

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ _tvm_emulator_send_external_message
2727
_tvm_emulator_send_internal_message
2828
_tvm_emulator_destroy
2929
_tvm_emulator_emulate_run_method
30+
_tvm_emulator_emulate_run_method_detailed
3031
_emulator_version

0 commit comments

Comments
 (0)