mirror of
https://github.com/ton-blockchain/ton
synced 2025-03-09 15:40:10 +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>
This commit is contained in:
parent
adf67aa869
commit
3b3c25b654
32 changed files with 2095 additions and 158 deletions
254
emulator/transaction-emulator.cpp
Normal file
254
emulator/transaction-emulator.cpp
Normal file
|
@ -0,0 +1,254 @@
|
|||
#include <string>
|
||||
#include "transaction-emulator.h"
|
||||
#include "crypto/common/refcnt.hpp"
|
||||
#include "vm/cp0.h"
|
||||
|
||||
using td::Ref;
|
||||
using namespace std::string_literals;
|
||||
|
||||
namespace emulator {
|
||||
td::Result<std::unique_ptr<TransactionEmulator::EmulationResult>> TransactionEmulator::emulate_transaction(
|
||||
block::Account&& account, td::Ref<vm::Cell> msg_root, ton::UnixTime utime, ton::LogicalTime lt, int trans_type) {
|
||||
|
||||
td::Ref<vm::Cell> old_mparams;
|
||||
std::vector<block::StoragePrices> storage_prices;
|
||||
block::StoragePhaseConfig storage_phase_cfg{&storage_prices};
|
||||
block::ComputePhaseConfig compute_phase_cfg;
|
||||
block::ActionPhaseConfig action_phase_cfg;
|
||||
td::RefInt256 masterchain_create_fee, basechain_create_fee;
|
||||
|
||||
if (!utime) {
|
||||
utime = unixtime_;
|
||||
}
|
||||
if (!utime) {
|
||||
utime = (unsigned)std::time(nullptr);
|
||||
}
|
||||
|
||||
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, account.workchain, utime);
|
||||
if(fetch_res.is_error()) {
|
||||
return fetch_res.move_as_error_prefix("cannot fetch config params ");
|
||||
}
|
||||
|
||||
vm::init_op_cp0();
|
||||
|
||||
if (!lt) {
|
||||
lt = lt_;
|
||||
}
|
||||
if (!lt) {
|
||||
lt = (account.last_trans_lt_ / block::ConfigInfo::get_lt_align() + 1) * block::ConfigInfo::get_lt_align(); // next block after account_.last_trans_lt_
|
||||
}
|
||||
|
||||
compute_phase_cfg.libraries = std::make_unique<vm::Dictionary>(libraries_);
|
||||
compute_phase_cfg.ignore_chksig = ignore_chksig_;
|
||||
compute_phase_cfg.with_vm_log = true;
|
||||
compute_phase_cfg.vm_log_verbosity = vm_log_verbosity_;
|
||||
|
||||
auto res = create_transaction(msg_root, &account, utime, lt, trans_type,
|
||||
&storage_phase_cfg, &compute_phase_cfg,
|
||||
&action_phase_cfg);
|
||||
if(res.is_error()) {
|
||||
return res.move_as_error_prefix("cannot run message on account ");
|
||||
}
|
||||
std::unique_ptr<block::transaction::Transaction> trans = res.move_as_ok();
|
||||
|
||||
if (!trans->compute_phase->accepted && trans->in_msg_extern) {
|
||||
auto vm_log = trans->compute_phase->vm_log;
|
||||
auto vm_exit_code = trans->compute_phase->exit_code;
|
||||
return std::make_unique<TransactionEmulator::EmulationExternalNotAccepted>(std::move(vm_log), vm_exit_code);
|
||||
}
|
||||
|
||||
if (!trans->serialize()) {
|
||||
return td::Status::Error(-669,"cannot serialize new transaction for smart contract "s + trans->account.addr.to_hex());
|
||||
}
|
||||
|
||||
auto trans_root = trans->commit(account);
|
||||
if (trans_root.is_null()) {
|
||||
return td::Status::Error(PSLICE() << "cannot commit new transaction for smart contract");
|
||||
}
|
||||
|
||||
return std::make_unique<TransactionEmulator::EmulationSuccess>(std::move(trans_root), std::move(account), std::move(trans->compute_phase->vm_log), std::move(trans->compute_phase->actions));
|
||||
}
|
||||
|
||||
td::Result<TransactionEmulator::EmulationSuccess> TransactionEmulator::emulate_transaction(block::Account&& account, td::Ref<vm::Cell> original_trans) {
|
||||
|
||||
block::gen::Transaction::Record record_trans;
|
||||
if (!tlb::unpack_cell(original_trans, record_trans)) {
|
||||
return td::Status::Error("Failed to unpack Transaction");
|
||||
}
|
||||
|
||||
ton::LogicalTime lt = record_trans.lt;
|
||||
ton::UnixTime utime = record_trans.now;
|
||||
account.now_ = utime;
|
||||
td::Ref<vm::Cell> msg_root = record_trans.r1.in_msg->prefetch_ref();
|
||||
int tag = block::gen::t_TransactionDescr.get_tag(vm::load_cell_slice(record_trans.description));
|
||||
|
||||
int trans_type = block::transaction::Transaction::tr_none;
|
||||
switch (tag) {
|
||||
case block::gen::TransactionDescr::trans_ord: {
|
||||
trans_type = block::transaction::Transaction::tr_ord;
|
||||
break;
|
||||
}
|
||||
case block::gen::TransactionDescr::trans_storage: {
|
||||
trans_type = block::transaction::Transaction::tr_storage;
|
||||
break;
|
||||
}
|
||||
case block::gen::TransactionDescr::trans_tick_tock: {
|
||||
block::gen::TransactionDescr::Record_trans_tick_tock tick_tock;
|
||||
if (!tlb::unpack_cell(record_trans.description, tick_tock)) {
|
||||
return td::Status::Error("Failed to unpack tick tock transaction description");
|
||||
}
|
||||
trans_type = tick_tock.is_tock ? block::transaction::Transaction::tr_tock : block::transaction::Transaction::tr_tick;
|
||||
break;
|
||||
}
|
||||
case block::gen::TransactionDescr::trans_split_prepare: {
|
||||
trans_type = block::transaction::Transaction::tr_split_prepare;
|
||||
break;
|
||||
}
|
||||
case block::gen::TransactionDescr::trans_split_install: {
|
||||
trans_type = block::transaction::Transaction::tr_split_install;
|
||||
break;
|
||||
}
|
||||
case block::gen::TransactionDescr::trans_merge_prepare: {
|
||||
trans_type = block::transaction::Transaction::tr_merge_prepare;
|
||||
break;
|
||||
}
|
||||
case block::gen::TransactionDescr::trans_merge_install: {
|
||||
trans_type = block::transaction::Transaction::tr_merge_install;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
TRY_RESULT(emulation, emulate_transaction(std::move(account), msg_root, utime, lt, trans_type));
|
||||
|
||||
auto emulation_result = dynamic_cast<EmulationSuccess&>(*emulation);
|
||||
if (td::Bits256(emulation_result.transaction->get_hash().bits()) != td::Bits256(original_trans->get_hash().bits())) {
|
||||
return td::Status::Error("transaction hash mismatch");
|
||||
}
|
||||
|
||||
if (!check_state_update(emulation_result.account, record_trans)) {
|
||||
return td::Status::Error("account hash mismatch");
|
||||
}
|
||||
|
||||
return emulation_result;
|
||||
}
|
||||
|
||||
td::Result<TransactionEmulator::EmulationChain> TransactionEmulator::emulate_transactions_chain(block::Account&& account, std::vector<td::Ref<vm::Cell>>&& original_transactions) {
|
||||
|
||||
std::vector<td::Ref<vm::Cell>> emulated_transactions;
|
||||
for (const auto& original_trans : original_transactions) {
|
||||
if (original_trans.is_null()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
TRY_RESULT(emulation_result, emulate_transaction(std::move(account), original_trans));
|
||||
emulated_transactions.push_back(std::move(emulation_result.transaction));
|
||||
account = std::move(emulation_result.account);
|
||||
}
|
||||
|
||||
return TransactionEmulator::EmulationChain{ std::move(emulated_transactions), std::move(account) };
|
||||
}
|
||||
|
||||
bool TransactionEmulator::check_state_update(const block::Account& account, const block::gen::Transaction::Record& trans) {
|
||||
block::gen::HASH_UPDATE::Record hash_update;
|
||||
return tlb::type_unpack_cell(trans.state_update, block::gen::t_HASH_UPDATE_Account, hash_update) &&
|
||||
hash_update.new_hash == account.total_state->get_hash().bits();
|
||||
}
|
||||
|
||||
td::Result<std::unique_ptr<block::transaction::Transaction>> TransactionEmulator::create_transaction(
|
||||
td::Ref<vm::Cell> msg_root, block::Account* acc,
|
||||
ton::UnixTime utime, ton::LogicalTime lt, int trans_type,
|
||||
block::StoragePhaseConfig* storage_phase_cfg,
|
||||
block::ComputePhaseConfig* compute_phase_cfg,
|
||||
block::ActionPhaseConfig* action_phase_cfg) {
|
||||
bool external{false}, ihr_delivered{false}, need_credit_phase{false};
|
||||
|
||||
if (msg_root.not_null()) {
|
||||
auto cs = vm::load_cell_slice(msg_root);
|
||||
external = block::gen::t_CommonMsgInfo.get_tag(cs);
|
||||
}
|
||||
|
||||
if (trans_type == block::transaction::Transaction::tr_ord) {
|
||||
need_credit_phase = !external;
|
||||
} else if (trans_type == block::transaction::Transaction::tr_merge_install) {
|
||||
need_credit_phase = true;
|
||||
}
|
||||
|
||||
std::unique_ptr<block::transaction::Transaction> trans =
|
||||
std::make_unique<block::transaction::Transaction>(*acc, trans_type, lt, utime, msg_root);
|
||||
|
||||
if (msg_root.not_null() && !trans->unpack_input_msg(ihr_delivered, action_phase_cfg)) {
|
||||
if (external) {
|
||||
// inbound external message was not accepted
|
||||
return td::Status::Error(-701,"inbound external message rejected by account "s + acc->addr.to_hex() +
|
||||
" before smart-contract execution");
|
||||
}
|
||||
return td::Status::Error(-669,"cannot unpack input message for a new transaction");
|
||||
}
|
||||
|
||||
if (trans->bounce_enabled) {
|
||||
if (!trans->prepare_storage_phase(*storage_phase_cfg, true)) {
|
||||
return td::Status::Error(-669,"cannot create storage phase of a new transaction for smart contract "s + acc->addr.to_hex());
|
||||
}
|
||||
if (need_credit_phase && !trans->prepare_credit_phase()) {
|
||||
return td::Status::Error(-669,"cannot create credit phase of a new transaction for smart contract "s + acc->addr.to_hex());
|
||||
}
|
||||
} else {
|
||||
if (need_credit_phase && !trans->prepare_credit_phase()) {
|
||||
return td::Status::Error(-669,"cannot create credit phase of a new transaction for smart contract "s + acc->addr.to_hex());
|
||||
}
|
||||
if (!trans->prepare_storage_phase(*storage_phase_cfg, true, need_credit_phase)) {
|
||||
return td::Status::Error(-669,"cannot create storage phase of a new transaction for smart contract "s + acc->addr.to_hex());
|
||||
}
|
||||
}
|
||||
|
||||
if (!trans->prepare_compute_phase(*compute_phase_cfg)) {
|
||||
return td::Status::Error(-669,"cannot create compute phase of a new transaction for smart contract "s + acc->addr.to_hex());
|
||||
}
|
||||
|
||||
if (!trans->compute_phase->accepted) {
|
||||
if (!external && trans->compute_phase->skip_reason == block::ComputePhase::sk_none) {
|
||||
return td::Status::Error(-669,"new ordinary transaction for smart contract "s + acc->addr.to_hex() +
|
||||
" has not been accepted by the smart contract (?)");
|
||||
}
|
||||
}
|
||||
|
||||
if (trans->compute_phase->success && !trans->prepare_action_phase(*action_phase_cfg)) {
|
||||
return td::Status::Error(-669,"cannot create action phase of a new transaction for smart contract "s + acc->addr.to_hex());
|
||||
}
|
||||
|
||||
if (trans->bounce_enabled && !trans->compute_phase->success && !trans->prepare_bounce_phase(*action_phase_cfg)) {
|
||||
return td::Status::Error(-669,"cannot create bounce phase of a new transaction for smart contract "s + acc->addr.to_hex());
|
||||
}
|
||||
|
||||
return trans;
|
||||
}
|
||||
|
||||
void TransactionEmulator::set_unixtime(ton::UnixTime unixtime) {
|
||||
unixtime_ = unixtime;
|
||||
}
|
||||
|
||||
void TransactionEmulator::set_lt(ton::LogicalTime lt) {
|
||||
lt_ = lt;
|
||||
}
|
||||
|
||||
void TransactionEmulator::set_rand_seed(td::BitArray<256>& rand_seed) {
|
||||
rand_seed_ = rand_seed;
|
||||
}
|
||||
|
||||
void TransactionEmulator::set_ignore_chksig(bool ignore_chksig) {
|
||||
ignore_chksig_ = ignore_chksig;
|
||||
}
|
||||
|
||||
void TransactionEmulator::set_config(block::Config &&config) {
|
||||
config_ = std::forward<block::Config>(config);
|
||||
}
|
||||
|
||||
void TransactionEmulator::set_libs(vm::Dictionary &&libs) {
|
||||
libraries_ = std::forward<vm::Dictionary>(libs);
|
||||
}
|
||||
|
||||
} // namespace emulator
|
Loading…
Add table
Add a link
Reference in a new issue