Skip to content

Commit

Permalink
Small TVM improvments
Browse files Browse the repository at this point in the history
  • Loading branch information
tvorogme committed Oct 19, 2024
1 parent 9c2765d commit 2866e0b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
57 changes: 45 additions & 12 deletions tvm-python/PyTVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ void PyTVM::set_c7(PyStackEntry x) {
}

void PyTVM::log(const std::string &log_string, int level) {
if (this->log_level >= level && level == LOG_INFO) {
if (log_level >= level && level == LOG_INFO) {
py::print("INFO: " + log_string);
} else if (this->log_level >= level && level == LOG_DEBUG) {
} else if (log_level >= level && level == LOG_DEBUG) {
py::print("DEBUG: " + log_string);
}
}
Expand Down Expand Up @@ -103,7 +103,7 @@ td::Result<block::Config> decode_config(vm::Ref<vm::Cell> config_params_cell) {
block::Config::needWorkchainInfo | block::Config::needSpecialSmc |
block::Config::needCapabilities);
TRY_STATUS_PREFIX(global_config.unpack(), "Can't unpack config params: ");
return global_config;
return std::move(global_config);
}

// Vm logger
Expand Down Expand Up @@ -137,6 +137,11 @@ class PythonLogger : public td::LogInterface {
}
};

std::vector<vm::StackEntry> ref_tuple_to_vector(const td::Ref<vm::Tuple> &ref_tuple) {
return *ref_tuple;
}


PyStack PyTVM::run_vm() {
py::gil_scoped_release release;
if (code.is_null()) {
Expand All @@ -160,27 +165,45 @@ PyStack PyTVM::run_vm() {
vm_log.log_interface = pyLogger;
vm_log.log_options = td::LogOptions(VERBOSITY_NAME(DEBUG), true, false);

td::Ref<vm::Tuple> init_c7;
std::vector<vm::StackEntry> init_c7;
int supported_version = ton::SUPPORTED_VERSION;

td::uint16 max_vm_data_depth = 512;

if (!skip_c7 && c7) {
init_c7 = c7.value().entry.as_tuple();
init_c7 = ref_tuple_to_vector(c7.value().entry.as_tuple());

if (init_c7->size() >= 10) {
auto config = (*init_c7)[9];
if (init_c7.size() >= 10) {
auto config = init_c7[9];

if (config.is_cell()) {
auto cfg = decode_config(config.as_cell());

if (cfg.is_ok()) {
supported_version = cfg.move_as_ok().get_global_version();
auto config_parsed = cfg.move_as_ok();

supported_version = config_parsed.get_global_version();
auto r_limits = config_parsed.get_size_limits_config();
if (r_limits.is_ok()) {
max_vm_data_depth = r_limits.ok().max_vm_data_depth;
}
if (supported_version >= 6 && init_c7.size() > 17) {
if (init_c7[3].is_int()) {
auto now = static_cast<unsigned int>(init_c7[3].as_int()->to_long());
init_c7[15] = config_parsed.get_unpacked_config_tuple(now);
}

td::optional<block::PrecompiledContractsConfig::Contract> precompiled;
precompiled = config_parsed.get_precompiled_contracts_config().get_contract(
code.my_cell->get_hash().bits());
init_c7[17] = precompiled ? td::make_refint(precompiled.value().gas_usage) : vm::StackEntry();
}
}
}
}
}

auto tuple_ref = td::make_cnt_ref<std::vector<vm::StackEntry>>(std::move(init_c7));

}
log_debug("Use code: " + code.my_cell->get_hash().to_hex());

log_debug("Load cp0");
Expand All @@ -196,9 +219,19 @@ PyStack PyTVM::run_vm() {
}

vm::VmState vm_local{
code.my_cell, td::make_ref<vm::Stack>(stackVm), &vm_dumper, gas_limits, flags, data.my_cell, vm_log,
std::move(lib_set), vm::make_tuple_ref(std::move(init_c7))};
code.my_cell,
td::make_ref<vm::Stack>(stackVm),
&vm_dumper,
gas_limits,
flags,
data.my_cell,
vm_log,
std::move(lib_set)};

vm_local.set_c7(vm::make_tuple_ref(std::move(tuple_ref)));
vm_local.set_global_version(supported_version);
vm_local.set_chksig_always_succeed(ignore_chksig);
vm_local.set_max_data_depth(max_vm_data_depth);

vm_init_state_hash_out = vm_local.get_state_hash().to_hex();
exit_code_out = vm_local.run();
Expand Down
7 changes: 5 additions & 2 deletions tvm-python/PyTVM.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class PyTVM {
int log_level;
bool skip_c7 = false;
bool enable_vm_dumper = true;
bool ignore_chksig = false;

td::optional<PyStackEntry> c7;

Expand All @@ -68,8 +69,7 @@ class PyTVM {
sameC3 = sameC3_;
skip_c7 = skip_c7_;
enable_vm_dumper = enable_vm_dumper_;

this->log_level = log_level_;
log_level = log_level_;

if (code_) {
set_code(code_.value());
Expand All @@ -95,6 +95,9 @@ class PyTVM {
void set_data(PyCell data_);
void set_code(PyCell code_);
void set_state_init(PyCell state_init_);
void set_unsafe_ignore_chksig(bool ignore_chksig_){
ignore_chksig = ignore_chksig_;
}
void set_stack(PyStack pystack);
void set_libs(PyCell libs);
PyStack run_vm();
Expand Down
1 change: 1 addition & 0 deletions tvm-python/python_ton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ PYBIND11_MODULE(python_ton, m) {
.def("set_stack", &PyTVM::set_stack)
.def("set_libs", &PyTVM::set_libs)
.def("get_ops", &PyTVM::get_ops)
.def("set_unsafe_ignore_chksig", &PyTVM::set_unsafe_ignore_chksig)
.def("set_state_init", &PyTVM::set_state_init)
.def("clear_stack", &PyTVM::clear_stack)
.def("set_gasLimit", &PyTVM::set_gasLimit, py::arg("gas_limit") = "0", py::arg("gas_max") = "-1")
Expand Down

0 comments on commit 2866e0b

Please sign in to comment.