From 6b941dcceb287f12100215095e3f7cfcc3ddebb8 Mon Sep 17 00:00:00 2001 From: EmelyanenkoK Date: Sat, 27 May 2023 21:22:31 +0300 Subject: [PATCH] Fix returning config from LS, add extra c7 elements in getmethods (#713) Co-authored-by: SpyCheese --- crypto/smc-envelope/SmartContract.cpp | 49 ++++++++++++++++----------- crypto/smc-envelope/SmartContract.h | 11 +++++- emulator/emulator-extern.cpp | 27 +++++++++++++++ emulator/emulator-extern.h | 8 +++++ emulator/emulator_export_list | 1 + emulator/tvm-emulator.hpp | 4 +++ tonlib/tonlib/LastConfig.cpp | 6 ++-- tonlib/tonlib/LastConfig.h | 1 + tonlib/tonlib/TonlibClient.cpp | 11 ++++-- validator/impl/liteserver.cpp | 25 ++++++++++---- 10 files changed, 111 insertions(+), 32 deletions(-) diff --git a/crypto/smc-envelope/SmartContract.cpp b/crypto/smc-envelope/SmartContract.cpp index 4103096c..8e95670a 100644 --- a/crypto/smc-envelope/SmartContract.cpp +++ b/crypto/smc-envelope/SmartContract.cpp @@ -120,7 +120,7 @@ td::Ref prepare_vm_stack(td::RefInt256 amount, td::Ref return stack_ref; } -td::Ref prepare_vm_c7(SmartContract::Args args) { +td::Ref prepare_vm_c7(SmartContract::Args args, td::Ref code) { td::BitArray<256> rand_seed; if (args.rand_seed) { rand_seed = args.rand_seed.unwrap(); @@ -139,10 +139,7 @@ td::Ref prepare_vm_c7(SmartContract::Args args) { if (args.address) { td::BigInt256 dest_addr; dest_addr.import_bits((*args.address).addr.as_bitslice()); - cb.store_ones(1) - .store_zeroes(2) - .store_long((*args.address).workchain, 8) - .store_int256(dest_addr, 256); + cb.store_ones(1).store_zeroes(2).store_long((*args.address).workchain, 8).store_int256(dest_addr, 256); } auto address = cb.finalize(); auto config = td::Ref(); @@ -151,20 +148,32 @@ td::Ref prepare_vm_c7(SmartContract::Args args) { config = (*args.config)->get_root_cell(); } - auto tuple = vm::make_tuple_ref( - td::make_refint(0x076ef1ea), // [ magic:0x076ef1ea - td::make_refint(0), // actions:Integer - td::make_refint(0), // msgs_sent:Integer - td::make_refint(now), // unixtime:Integer - td::make_refint(0), //TODO: // block_lt:Integer - td::make_refint(0), //TODO: // trans_lt:Integer - std::move(rand_seed_int), // rand_seed:Integer - block::CurrencyCollection(args.balance).as_vm_tuple(), // balance_remaining:[Integer (Maybe Cell)] - vm::load_cell_slice_ref(address), // myself:MsgAddressInt - vm::StackEntry::maybe(config) //vm::StackEntry::maybe(td::Ref()) - ); // global_config:(Maybe Cell) ] = SmartContractInfo; + std::vector tuple = { + td::make_refint(0x076ef1ea), // [ magic:0x076ef1ea + td::make_refint(0), // actions:Integer + td::make_refint(0), // msgs_sent:Integer + td::make_refint(now), // unixtime:Integer + td::make_refint(0), //TODO: // block_lt:Integer + td::make_refint(0), //TODO: // trans_lt:Integer + std::move(rand_seed_int), // rand_seed:Integer + block::CurrencyCollection(args.balance).as_vm_tuple(), // balance_remaining:[Integer (Maybe Cell)] + vm::load_cell_slice_ref(address), // myself:MsgAddressInt + vm::StackEntry::maybe(config) //vm::StackEntry::maybe(td::Ref()) + }; + if (args.config && args.config.value()->get_global_version() >= 4) { + tuple.push_back(code.not_null() ? code : vm::StackEntry{}); // code:Cell + tuple.push_back(block::CurrencyCollection::zero().as_vm_tuple()); // in_msg_value:[Integer (Maybe Cell)] + tuple.push_back(td::zero_refint()); // storage_fees:Integer + + // See crypto/block/mc-config.cpp#2115 (get_prev_blocks_info) + // [ wc:Integer shard:Integer seqno:Integer root_hash:Integer file_hash:Integer] = BlockId; + // [ last_mc_blocks:[BlockId...] + // prev_key_block:BlockId ] : PrevBlocksInfo + tuple.push_back(args.prev_blocks_info ? args.prev_blocks_info.value() : vm::StackEntry{}); // prev_block_info + } + auto tuple_ref = td::make_cnt_ref>(std::move(tuple)); //LOG(DEBUG) << "SmartContractInfo initialized with " << vm::StackEntry(tuple).to_string(); - return vm::make_tuple_ref(std::move(tuple)); + return vm::make_tuple_ref(std::move(tuple_ref)); } SmartContract::Answer run_smartcont(SmartContract::State state, td::Ref stack, td::Ref c7, @@ -282,7 +291,7 @@ td::Ref SmartContract::get_init_state() const { SmartContract::Answer SmartContract::run_method(Args args) { if (!args.c7) { - args.c7 = prepare_vm_c7(args); + args.c7 = prepare_vm_c7(args, state_.code); } if (!args.limits) { bool is_internal = args.get_method_id().ok() == 0; @@ -302,7 +311,7 @@ SmartContract::Answer SmartContract::run_method(Args args) { SmartContract::Answer SmartContract::run_get_method(Args args) const { if (!args.c7) { - args.c7 = prepare_vm_c7(args); + args.c7 = prepare_vm_c7(args, state_.code); } if (!args.limits) { args.limits = vm::GasLimits{1000000, 1000000}; diff --git a/crypto/smc-envelope/SmartContract.h b/crypto/smc-envelope/SmartContract.h index ce349de3..dd299220 100644 --- a/crypto/smc-envelope/SmartContract.h +++ b/crypto/smc-envelope/SmartContract.h @@ -70,6 +70,7 @@ class SmartContract : public td::CntObject { td::optional address; td::optional> config; td::optional libraries; + td::optional> prev_blocks_info; Args() { } @@ -124,7 +125,7 @@ class SmartContract : public td::CntObject { this->address = address; return std::move(*this); } - Args&& set_config(std::shared_ptr& config) { + Args&& set_config(const std::shared_ptr& config) { this->config = config; return std::move(*this); } @@ -132,6 +133,14 @@ class SmartContract : public td::CntObject { this->libraries = libraries; return std::move(*this); } + Args&& set_prev_blocks_info(td::Ref tuple) { + if (tuple.is_null()) { + this->prev_blocks_info = {}; + } else { + this->prev_blocks_info = std::move(tuple); + } + return std::move(*this); + } Args&& set_vm_verbosity_level(int vm_log_verbosity_level) { this->vm_log_verbosity_level = vm_log_verbosity_level; return std::move(*this); diff --git a/emulator/emulator-extern.cpp b/emulator/emulator-extern.cpp index 0ffc316a..971cdcdb 100644 --- a/emulator/emulator-extern.cpp +++ b/emulator/emulator-extern.cpp @@ -448,6 +448,33 @@ bool tvm_emulator_set_c7(void *tvm_emulator, const char *address, uint32_t unixt return true; } +bool tvm_emulator_set_prev_blocks_info(void *tvm_emulator, const char* info_boc) { + auto emulator = static_cast(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(tvm_emulator); emulator->set_gas_limit(gas_limit); diff --git a/emulator/emulator-extern.h b/emulator/emulator-extern.h index 4409608f..ce920f98 100644 --- a/emulator/emulator-extern.h +++ b/emulator/emulator-extern.h @@ -167,6 +167,14 @@ EMULATOR_EXPORT bool tvm_emulator_set_libraries(void *tvm_emulator, const char * */ EMULATOR_EXPORT 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); +/** + * @brief Set tuple of previous blocks (13th element of c7) + * @param tvm_emulator Pointer to TVM emulator + * @param info_boc Base64 encoded BoC serialized TVM tuple (VmStackValue). + * @return true in case of success, false in case of error + */ +EMULATOR_EXPORT bool tvm_emulator_set_prev_blocks_info(void *tvm_emulator, const char* info_boc); + /** * @brief Set TVM gas limit * @param tvm_emulator Pointer to TVM emulator diff --git a/emulator/emulator_export_list b/emulator/emulator_export_list index acd79df8..e70166e7 100644 --- a/emulator/emulator_export_list +++ b/emulator/emulator_export_list @@ -14,6 +14,7 @@ _emulator_set_verbosity_level _tvm_emulator_create _tvm_emulator_set_libraries _tvm_emulator_set_c7 +_tvm_emulator_set_prev_blocks_info _tvm_emulator_set_gas_limit _tvm_emulator_set_debug_enabled _tvm_emulator_run_get_method diff --git a/emulator/tvm-emulator.hpp b/emulator/tvm-emulator.hpp index 46e2538d..dafa2a5f 100644 --- a/emulator/tvm-emulator.hpp +++ b/emulator/tvm-emulator.hpp @@ -33,6 +33,10 @@ public: } } + void set_prev_blocks_info(td::Ref tuple) { + args_.set_prev_blocks_info(std::move(tuple)); + } + void set_debug_enabled(bool debug_enabled) { args_.set_debug_enabled(debug_enabled); } diff --git a/tonlib/tonlib/LastConfig.cpp b/tonlib/tonlib/LastConfig.cpp index 960d5994..bcc3c081 100644 --- a/tonlib/tonlib/LastConfig.cpp +++ b/tonlib/tonlib/LastConfig.cpp @@ -62,7 +62,8 @@ void LastConfig::with_last_block(td::Result r_last_block) { } auto last_block = r_last_block.move_as_ok(); - client_.send_query(ton::lite_api::liteServer_getConfigAll(0, create_tl_lite_block_id(last_block.last_block_id)), + client_.send_query(ton::lite_api::liteServer_getConfigAll(block::ConfigInfo::needPrevBlocks, + create_tl_lite_block_id(last_block.last_block_id)), [this](auto r_config) { this->on_config(std::move(r_config)); }); } @@ -92,7 +93,7 @@ td::Status LastConfig::process_config_proof(ton::ton_api::object_ptrstate_proof_.as_slice(), raw_config->config_proof_.as_slice())); - TRY_RESULT(config, block::Config::extract_from_state(std::move(state), 0)); + TRY_RESULT(config, block::ConfigInfo::extract_config(std::move(state), block::ConfigInfo::needPrevBlocks)); for (auto i : params_) { VLOG(last_config) << "ConfigParam(" << i << ") = "; @@ -109,6 +110,7 @@ td::Status LastConfig::process_config_proof(ton::ton_api::object_ptrget_prev_blocks_info()); state_.config.reset(config.release()); return td::Status::OK(); } diff --git a/tonlib/tonlib/LastConfig.h b/tonlib/tonlib/LastConfig.h index 514b4a59..901733dc 100644 --- a/tonlib/tonlib/LastConfig.h +++ b/tonlib/tonlib/LastConfig.h @@ -30,6 +30,7 @@ namespace tonlib { struct LastConfigState { std::shared_ptr config; + td::Ref prev_blocks_info; }; td::StringBuilder& operator<<(td::StringBuilder& sb, const LastConfigState& state); diff --git a/tonlib/tonlib/TonlibClient.cpp b/tonlib/tonlib/TonlibClient.cpp index cd1ad938..b49587c5 100644 --- a/tonlib/tonlib/TonlibClient.cpp +++ b/tonlib/tonlib/TonlibClient.cpp @@ -890,8 +890,10 @@ class Query { } return res; } - td::Result>> estimate_fees(bool ignore_chksig, std::shared_ptr& cfg, vm::Dictionary& libraries) { + td::Result>> estimate_fees(bool ignore_chksig, const LastConfigState& state, + vm::Dictionary& libraries) { // gas fees + const auto& cfg = state.config; bool is_masterchain = raw_.source->get_address().workchain == ton::masterchainId; TRY_RESULT(gas_limits_prices, cfg->get_gas_limits_prices(is_masterchain)); TRY_RESULT(storage_prices, cfg->get_storage_prices()); @@ -919,7 +921,9 @@ class Query { .set_now(raw_.source->get_sync_time()) .set_ignore_chksig(ignore_chksig) .set_address(raw_.source->get_address()) - .set_config(cfg).set_libraries(libraries)); + .set_config(cfg) + .set_prev_blocks_info(state.prev_blocks_info) + .set_libraries(libraries)); td::int64 fwd_fee = 0; if (res.success) { LOG(DEBUG) << "output actions:\n" @@ -4109,7 +4113,7 @@ void TonlibClient::query_estimate_fees(td::int64 id, bool ignore_chksig, td::Res return; } TRY_RESULT_PROMISE(promise, state, std::move(r_state)); - TRY_RESULT_PROMISE_PREFIX(promise, fees, TRY_VM(it->second->estimate_fees(ignore_chksig, state.config, libraries)), + TRY_RESULT_PROMISE_PREFIX(promise, fees, TRY_VM(it->second->estimate_fees(ignore_chksig, state, libraries)), TonlibError::Internal()); promise.set_value(tonlib_api::make_object( fees.first.to_tonlib_api(), td::transform(fees.second, [](auto& x) { return x.to_tonlib_api(); }))); @@ -4474,6 +4478,7 @@ td::Status TonlibClient::do_request(const tonlib_api::smc_runGetMethod& request, ](td::Result r_state) mutable { TRY_RESULT_PROMISE(promise, state, std::move(r_state)); args.set_config(state.config); + args.set_prev_blocks_info(state.prev_blocks_info); auto code = smc->get_state().code; if (code.not_null()) { diff --git a/validator/impl/liteserver.cpp b/validator/impl/liteserver.cpp index 1a4f8085..9c7a0456 100644 --- a/validator/impl/liteserver.cpp +++ b/validator/impl/liteserver.cpp @@ -1688,13 +1688,23 @@ void LiteQuery::continue_getConfigParams(int mode, std::vector param_list) } } - auto res = keyblk ? block::Config::extract_from_key_block(mpb.root(), mode) - : block::Config::extract_from_state(mpb.root(), mode); - if (res.is_error()) { - fatal_error(res.move_as_error()); - return; + std::unique_ptr cfg; + if (keyblk || !(mode & block::ConfigInfo::needPrevBlocks)) { + auto res = keyblk ? block::Config::extract_from_key_block(mpb.root(), mode) + : block::Config::extract_from_state(mpb.root(), mode); + if (res.is_error()) { + fatal_error(res.move_as_error()); + return; + } + cfg = res.move_as_ok(); + } else { + auto res = block::ConfigInfo::extract_config(mpb.root(), mode); + if (res.is_error()) { + fatal_error(res.move_as_error()); + return; + } + cfg = res.move_as_ok(); } - auto cfg = res.move_as_ok(); if (!cfg) { fatal_error("cannot extract configuration from last mc state"); return; @@ -1707,6 +1717,9 @@ void LiteQuery::continue_getConfigParams(int mode, std::vector param_list) visit(cfg->get_config_param(i)); } } + if (!keyblk && mode & block::ConfigInfo::needPrevBlocks) { + ((block::ConfigInfo*)cfg.get())->get_prev_blocks_info(); + } } catch (vm::VmError& err) { fatal_error("error while traversing required configuration parameters: "s + err.get_msg()); return;