1
0
Fork 0
mirror of https://github.com/ton-blockchain/ton synced 2025-02-12 19:22:37 +00:00
ton/validator/impl/external-message.cpp

176 lines
7.8 KiB
C++
Raw Permalink Normal View History

2019-09-07 10:03:22 +00:00
/*
This file is part of TON Blockchain Library.
TON Blockchain Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
TON Blockchain Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
2020-02-20 15:56:18 +00:00
Copyright 2017-2020 Telegram Systems LLP
2019-09-07 10:03:22 +00:00
*/
2021-11-13 20:28:06 +00:00
2019-09-07 10:03:22 +00:00
#include "external-message.hpp"
2021-11-13 20:28:06 +00:00
#include "collator-impl.h"
2019-09-07 10:03:22 +00:00
#include "vm/boc.h"
#include "block/block-parse.h"
#include "block/block-auto.h"
#include "block/block-db.h"
2021-11-13 20:28:06 +00:00
#include "fabric.h"
#include "td/actor/actor.h"
#include "td/utils/Random.h"
#include "crypto/openssl/rand.hpp"
2019-09-07 10:03:22 +00:00
namespace ton {
namespace validator {
using td::Ref;
2021-11-13 20:28:06 +00:00
ExtMessageQ::ExtMessageQ(td::BufferSlice data, td::Ref<vm::Cell> root, AccountIdPrefixFull addr_prefix, ton::WorkchainId wc, ton::StdSmcAddress addr)
: root_(std::move(root)), addr_prefix_(addr_prefix), data_(std::move(data)), wc_(wc), addr_(addr) {
2019-09-07 10:03:22 +00:00
hash_ = block::compute_file_hash(data_);
}
td::Result<Ref<ExtMessageQ>> ExtMessageQ::create_ext_message(td::BufferSlice data,
block::SizeLimitsConfig::ExtMsgLimits limits) {
if (data.size() > limits.max_size) {
2019-09-07 10:03:22 +00:00
return td::Status::Error("external message too large, rejecting");
}
vm::BagOfCells boc;
auto res = boc.deserialize(data.as_slice());
if (res.is_error()) {
return res.move_as_error();
}
if (boc.get_root_count() != 1) {
return td::Status::Error("external message is not a valid bag of cells"); // not a valid bag-of-Cells
}
auto ext_msg = boc.get_root_cell();
if (ext_msg->get_level() != 0) {
return td::Status::Error("external message must have zero level");
}
if (ext_msg->get_depth() >= limits.max_depth) {
2020-02-20 15:56:18 +00:00
return td::Status::Error("external message is too deep");
}
2019-09-07 10:03:22 +00:00
vm::CellSlice cs{vm::NoVmOrd{}, ext_msg};
if (cs.prefetch_ulong(2) != 2) { // ext_in_msg_info$10
return td::Status::Error("external message must begin with ext_in_msg_info$10");
}
ton::Bits256 hash{ext_msg->get_hash().bits()};
if (!block::gen::t_Message_Any.validate_ref(128, ext_msg)) {
2019-09-07 10:03:22 +00:00
return td::Status::Error("external message is not a (Message Any) according to automated checks");
}
if (!block::tlb::t_Message.validate_ref(128, ext_msg)) {
2019-09-07 10:03:22 +00:00
return td::Status::Error("external message is not a (Message Any) according to hand-written checks");
}
block::gen::CommonMsgInfo::Record_ext_in_msg_info info;
if (!tlb::unpack_cell_inexact(ext_msg, info)) {
return td::Status::Error("cannot unpack external message header");
}
auto dest_prefix = block::tlb::t_MsgAddressInt.get_prefix(info.dest);
if (!dest_prefix.is_valid()) {
return td::Status::Error("destination of an inbound external message is an invalid blockchain address");
}
2021-11-13 20:28:06 +00:00
ton::StdSmcAddress addr;
ton::WorkchainId wc;
if(!block::tlb::t_MsgAddressInt.extract_std_address(info.dest, wc, addr)) {
return td::Status::Error(PSLICE() << "Can't parse destination address");
}
return Ref<ExtMessageQ>{true, std::move(data), std::move(ext_msg), dest_prefix, wc, addr};
}
void ExtMessageQ::run_message(td::Ref<ExtMessage> message, td::actor::ActorId<ton::validator::ValidatorManager> manager,
td::Promise<td::Ref<ExtMessage>> promise) {
auto root = message->root_cell();
2021-11-13 20:28:06 +00:00
block::gen::CommonMsgInfo::Record_ext_in_msg_info info;
tlb::unpack_cell_inexact(root, info); // checked in create message
ton::StdSmcAddress addr = message->addr();
ton::WorkchainId wc = message->wc();
2021-11-13 20:28:06 +00:00
run_fetch_account_state(
wc, addr, manager,
[promise = std::move(promise), msg_root = root, wc, addr, message](
td::Result<std::tuple<td::Ref<vm::CellSlice>, UnixTime, LogicalTime, std::unique_ptr<block::ConfigInfo>>>
res) mutable {
2021-11-13 20:28:06 +00:00
if (res.is_error()) {
promise.set_error(td::Status::Error(PSLICE() << "Failed to get account state"));
} else {
auto tuple = res.move_as_ok();
block::Account acc;
auto shard_acc = std::move(std::get<0>(tuple));
auto utime = std::get<1>(tuple);
auto lt = std::get<2>(tuple);
auto config = std::move(std::get<3>(tuple));
bool special = wc == masterchainId && config->is_special_smartcontract(addr);
if (!acc.unpack(shard_acc, utime, special)) {
2021-11-13 20:28:06 +00:00
promise.set_error(td::Status::Error(PSLICE() << "Failed to unpack account state"));
} else {
auto status = run_message_on_account(wc, &acc, utime, lt + 1, msg_root, std::move(config));
if (status.is_ok()) {
promise.set_value(std::move(message));
2021-11-13 20:28:06 +00:00
} else {
promise.set_error(td::Status::Error(PSLICE() << "External message was not accepted\n"
<< status.message()));
2021-11-13 20:28:06 +00:00
}
}
}
});
2021-11-13 20:28:06 +00:00
}
td::Status ExtMessageQ::run_message_on_account(ton::WorkchainId wc,
block::Account* acc,
UnixTime utime, LogicalTime lt,
td::Ref<vm::Cell> msg_root,
std::unique_ptr<block::ConfigInfo> config) {
2021-11-13 20:28:06 +00:00
Ref<vm::Cell> old_mparams;
std::vector<block::StoragePrices> storage_prices_;
block::StoragePhaseConfig storage_phase_cfg_{&storage_prices_};
td::BitArray<256> rand_seed_;
block::ComputePhaseConfig compute_phase_cfg_;
block::ActionPhaseConfig action_phase_cfg_;
td::RefInt256 masterchain_create_fee, basechain_create_fee;
Add account state by transaction and emulator (extended) (#592) * account_state_by_transaction * Correct time calculation * Bug fixes * Refactor * namespace block::transaction * smc.forget * RunEmulator: remove wallet_id * Refactor & fixes * AccountStateByTransaction: use shardchain block instead of masterchain block * transaction emulator core * refactor * tx emulator major functionality * small format changes * readme * clean * return json, add support for init messages * tx emulator readme * refactor getConfigParam and getConfigAll * add shardchain_libs_boc parameter * option to change verbosity level of transaction emulator * fix deserializing ShardAccount with account_none * add mode needSpecialSmc when unpack config * emulator: block::Transaction -> block::transaction::Transaction * Refactor * emulator: Fix bug * emulator: Support for emulator-extern * emulator: Refactor * Return vm log and vm exit code. * fix build on macos, emulator_static added * adjust documentation * ignore_chksig for external messages * tvm emulator, run get method * Added more params for transaction emulator * Added setters for optional transaction emulator params, moved libs to a new setter * Added actions cell output to transaction emulator * fix tonlib build * refactoring, rand seed as hex size 64, tvm emulator send message * tvm send message, small refactoring * fix config decoding, rename * improve documentation * macos export symbols * Added run_get_method to transaction emulator emscipten wrapper * Fixed empty action list serialization * Changed actions list cell to serialize as json null instead of empty string in transaction emulator * stack as boc * log gas remaining * Fix prev_block_id * fix build errors * Refactor fetch_config_params * fix failing unwrap of optional rand_seed * lookup correct shard, choose prev_block based on account shard * fix tonlib android jni build --------- Co-authored-by: legaii <jgates.ardux@gmail.com> Co-authored-by: ms <dungeon666master@protonmail.com> Co-authored-by: krigga <krigga7@gmail.com>
2023-02-02 07:03:45 +00:00
auto fetch_res = block::FetchConfigParams::fetch_config_params(*config, &old_mparams,
&storage_prices_, &storage_phase_cfg_,
&rand_seed_, &compute_phase_cfg_,
&action_phase_cfg_, &masterchain_create_fee,
&basechain_create_fee, wc, utime);
2021-11-13 20:28:06 +00:00
if(fetch_res.is_error()) {
auto error = fetch_res.move_as_error();
LOG(DEBUG) << "Cannot fetch config params: " << error.message();
return error.move_as_error_prefix("Cannot fetch config params: ");
2021-11-13 20:28:06 +00:00
}
Add account state by transaction and emulator (extended) (#592) * account_state_by_transaction * Correct time calculation * Bug fixes * Refactor * namespace block::transaction * smc.forget * RunEmulator: remove wallet_id * Refactor & fixes * AccountStateByTransaction: use shardchain block instead of masterchain block * transaction emulator core * refactor * tx emulator major functionality * small format changes * readme * clean * return json, add support for init messages * tx emulator readme * refactor getConfigParam and getConfigAll * add shardchain_libs_boc parameter * option to change verbosity level of transaction emulator * fix deserializing ShardAccount with account_none * add mode needSpecialSmc when unpack config * emulator: block::Transaction -> block::transaction::Transaction * Refactor * emulator: Fix bug * emulator: Support for emulator-extern * emulator: Refactor * Return vm log and vm exit code. * fix build on macos, emulator_static added * adjust documentation * ignore_chksig for external messages * tvm emulator, run get method * Added more params for transaction emulator * Added setters for optional transaction emulator params, moved libs to a new setter * Added actions cell output to transaction emulator * fix tonlib build * refactoring, rand seed as hex size 64, tvm emulator send message * tvm send message, small refactoring * fix config decoding, rename * improve documentation * macos export symbols * Added run_get_method to transaction emulator emscipten wrapper * Fixed empty action list serialization * Changed actions list cell to serialize as json null instead of empty string in transaction emulator * stack as boc * log gas remaining * Fix prev_block_id * fix build errors * Refactor fetch_config_params * fix failing unwrap of optional rand_seed * lookup correct shard, choose prev_block based on account shard * fix tonlib android jni build --------- Co-authored-by: legaii <jgates.ardux@gmail.com> Co-authored-by: ms <dungeon666master@protonmail.com> Co-authored-by: krigga <krigga7@gmail.com>
2023-02-02 07:03:45 +00:00
compute_phase_cfg_.libraries = std::make_unique<vm::Dictionary>(config->get_libraries_root(), 256);
compute_phase_cfg_.with_vm_log = true;
compute_phase_cfg_.stop_on_accept_message = true;
2021-11-13 20:28:06 +00:00
auto res = Collator::impl_create_ordinary_transaction(msg_root, acc, utime, lt,
&storage_phase_cfg_, &compute_phase_cfg_,
&action_phase_cfg_,
true, lt);
if(res.is_error()) {
auto error = res.move_as_error();
LOG(DEBUG) << "Cannot run message on account: " << error.message();
return error.move_as_error_prefix("Cannot run message on account: ");
2021-11-13 20:28:06 +00:00
}
Add account state by transaction and emulator (extended) (#592) * account_state_by_transaction * Correct time calculation * Bug fixes * Refactor * namespace block::transaction * smc.forget * RunEmulator: remove wallet_id * Refactor & fixes * AccountStateByTransaction: use shardchain block instead of masterchain block * transaction emulator core * refactor * tx emulator major functionality * small format changes * readme * clean * return json, add support for init messages * tx emulator readme * refactor getConfigParam and getConfigAll * add shardchain_libs_boc parameter * option to change verbosity level of transaction emulator * fix deserializing ShardAccount with account_none * add mode needSpecialSmc when unpack config * emulator: block::Transaction -> block::transaction::Transaction * Refactor * emulator: Fix bug * emulator: Support for emulator-extern * emulator: Refactor * Return vm log and vm exit code. * fix build on macos, emulator_static added * adjust documentation * ignore_chksig for external messages * tvm emulator, run get method * Added more params for transaction emulator * Added setters for optional transaction emulator params, moved libs to a new setter * Added actions cell output to transaction emulator * fix tonlib build * refactoring, rand seed as hex size 64, tvm emulator send message * tvm send message, small refactoring * fix config decoding, rename * improve documentation * macos export symbols * Added run_get_method to transaction emulator emscipten wrapper * Fixed empty action list serialization * Changed actions list cell to serialize as json null instead of empty string in transaction emulator * stack as boc * log gas remaining * Fix prev_block_id * fix build errors * Refactor fetch_config_params * fix failing unwrap of optional rand_seed * lookup correct shard, choose prev_block based on account shard * fix tonlib android jni build --------- Co-authored-by: legaii <jgates.ardux@gmail.com> Co-authored-by: ms <dungeon666master@protonmail.com> Co-authored-by: krigga <krigga7@gmail.com>
2023-02-02 07:03:45 +00:00
std::unique_ptr<block::transaction::Transaction> trans = res.move_as_ok();
2021-11-13 20:28:06 +00:00
auto trans_root = trans->commit(*acc);
if (trans_root.is_null()) {
LOG(DEBUG) << "Cannot commit new transaction for smart contract";
return td::Status::Error("Cannot commit new transaction for smart contract");
2021-11-13 20:28:06 +00:00
}
return td::Status::OK();
2019-09-07 10:03:22 +00:00
}
} // namespace validator
} // namespace ton