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_);
|
|
|
|
}
|
|
|
|
|
2022-10-10 14:13:21 +00:00
|
|
|
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");
|
|
|
|
}
|
2022-10-10 14:13:21 +00:00
|
|
|
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()};
|
2020-02-28 10:28:47 +00:00
|
|
|
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");
|
|
|
|
}
|
2020-02-28 10:28:47 +00:00
|
|
|
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};
|
|
|
|
}
|
|
|
|
|
2024-05-28 10:31:13 +00:00
|
|
|
void ExtMessageQ::run_message(td::Ref<ExtMessage> message, td::actor::ActorId<ton::validator::ValidatorManager> manager,
|
2022-10-10 14:13:21 +00:00
|
|
|
td::Promise<td::Ref<ExtMessage>> promise) {
|
2024-05-28 10:31:13 +00:00
|
|
|
auto root = message->root_cell();
|
2021-11-13 20:28:06 +00:00
|
|
|
block::gen::CommonMsgInfo::Record_ext_in_msg_info info;
|
2022-10-10 14:13:21 +00:00
|
|
|
tlb::unpack_cell_inexact(root, info); // checked in create message
|
2024-05-28 10:31:13 +00:00
|
|
|
ton::StdSmcAddress addr = message->addr();
|
|
|
|
ton::WorkchainId wc = message->wc();
|
2021-11-13 20:28:06 +00:00
|
|
|
|
2022-10-10 14:13:21 +00:00
|
|
|
run_fetch_account_state(
|
|
|
|
wc, addr, manager,
|
2024-05-28 10:31:13 +00:00
|
|
|
[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>>>
|
2022-10-10 14:13:21 +00:00
|
|
|
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));
|
2024-01-17 09:01:34 +00:00
|
|
|
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 {
|
2022-05-24 18:21:58 +00:00
|
|
|
auto status = run_message_on_account(wc, &acc, utime, lt + 1, msg_root, std::move(config));
|
|
|
|
if (status.is_ok()) {
|
2024-05-28 10:31:13 +00:00
|
|
|
promise.set_value(std::move(message));
|
2021-11-13 20:28:06 +00:00
|
|
|
} else {
|
2022-10-10 14:13:21 +00:00
|
|
|
promise.set_error(td::Status::Error(PSLICE() << "External message was not accepted\n"
|
|
|
|
<< status.message()));
|
2021-11-13 20:28:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-10 14:13:21 +00:00
|
|
|
});
|
2021-11-13 20:28:06 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 18:21:58 +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;
|
|
|
|
|
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()) {
|
2022-05-24 18:21:58 +00:00
|
|
|
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
|
|
|
}
|
2023-02-02 07:03:45 +00:00
|
|
|
compute_phase_cfg_.libraries = std::make_unique<vm::Dictionary>(config->get_libraries_root(), 256);
|
2022-05-24 18:21:58 +00:00
|
|
|
compute_phase_cfg_.with_vm_log = true;
|
2024-01-22 09:34:49 +00:00
|
|
|
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()) {
|
2022-05-24 18:21:58 +00:00
|
|
|
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
|
|
|
}
|
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()) {
|
2022-05-24 18:21:58 +00:00
|
|
|
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
|
|
|
}
|
2022-05-24 18:21:58 +00:00
|
|
|
return td::Status::OK();
|
2019-09-07 10:03:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace validator
|
|
|
|
} // namespace ton
|