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
|
@ -104,19 +104,7 @@ class Collator final : public td::actor::Actor {
|
|||
return 2;
|
||||
}
|
||||
|
||||
static td::Result<std::unique_ptr<block::ConfigInfo>>
|
||||
impl_fetch_config_params(std::unique_ptr<block::ConfigInfo> config,
|
||||
Ref<vm::Cell>* old_mparams,
|
||||
std::vector<block::StoragePrices>* storage_prices,
|
||||
block::StoragePhaseConfig* storage_phase_cfg,
|
||||
td::BitArray<256>* rand_seed,
|
||||
block::ComputePhaseConfig* compute_phase_cfg,
|
||||
block::ActionPhaseConfig* action_phase_cfg,
|
||||
td::RefInt256* masterchain_create_fee,
|
||||
td::RefInt256* basechain_create_fee,
|
||||
WorkchainId wc, UnixTime now);
|
||||
|
||||
static td::Result<std::unique_ptr<block::Transaction>>
|
||||
static td::Result<std::unique_ptr<block::transaction::Transaction>>
|
||||
impl_create_ordinary_transaction(Ref<vm::Cell> msg_root,
|
||||
block::Account* acc,
|
||||
UnixTime utime, LogicalTime lt,
|
||||
|
@ -285,7 +273,7 @@ class Collator final : public td::actor::Actor {
|
|||
td::Result<bool> register_shard_signatures_cell(Ref<vm::Cell> shard_blk_signatures);
|
||||
td::Result<bool> register_shard_signatures(td::Slice shard_blk_signatures_boc);
|
||||
void register_new_msg(block::NewOutMsg msg);
|
||||
void register_new_msgs(block::Transaction& trans);
|
||||
void register_new_msgs(block::transaction::Transaction& trans);
|
||||
bool process_new_messages(bool enqueue_only = false);
|
||||
int process_one_new_message(block::NewOutMsg msg, bool enqueue_only = false, Ref<vm::Cell>* is_special = nullptr);
|
||||
bool process_inbound_internal_messages();
|
||||
|
|
|
@ -1561,90 +1561,19 @@ bool Collator::init_lt() {
|
|||
}
|
||||
|
||||
bool Collator::fetch_config_params() {
|
||||
auto res = impl_fetch_config_params(std::move(config_), &old_mparams_, &storage_prices_, &storage_phase_cfg_,
|
||||
&rand_seed_, &compute_phase_cfg_, &action_phase_cfg_, &masterchain_create_fee_,
|
||||
&basechain_create_fee_, workchain(), now_);
|
||||
auto 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_,
|
||||
workchain(), now_
|
||||
);
|
||||
if (res.is_error()) {
|
||||
return fatal_error(res.move_as_error());
|
||||
}
|
||||
config_ = res.move_as_ok();
|
||||
compute_phase_cfg_.libraries = std::make_unique<vm::Dictionary>(config_->get_libraries_root(), 256);
|
||||
return true;
|
||||
}
|
||||
|
||||
td::Result<std::unique_ptr<block::ConfigInfo>> Collator::impl_fetch_config_params(
|
||||
std::unique_ptr<block::ConfigInfo> config, Ref<vm::Cell>* old_mparams,
|
||||
std::vector<block::StoragePrices>* storage_prices, block::StoragePhaseConfig* storage_phase_cfg,
|
||||
td::BitArray<256>* rand_seed, block::ComputePhaseConfig* compute_phase_cfg,
|
||||
block::ActionPhaseConfig* action_phase_cfg, td::RefInt256* masterchain_create_fee,
|
||||
td::RefInt256* basechain_create_fee, WorkchainId wc, UnixTime now) {
|
||||
*old_mparams = config->get_config_param(9);
|
||||
{
|
||||
auto res = config->get_storage_prices();
|
||||
if (res.is_error()) {
|
||||
return res.move_as_error();
|
||||
}
|
||||
*storage_prices = res.move_as_ok();
|
||||
}
|
||||
{
|
||||
// generate rand seed
|
||||
prng::rand_gen().strong_rand_bytes(rand_seed->data(), 32);
|
||||
LOG(DEBUG) << "block random seed set to " << rand_seed->to_hex();
|
||||
}
|
||||
TRY_RESULT(size_limits, config->get_size_limits_config());
|
||||
{
|
||||
// compute compute_phase_cfg / storage_phase_cfg
|
||||
auto cell = config->get_config_param(wc == ton::masterchainId ? 20 : 21);
|
||||
if (cell.is_null()) {
|
||||
return td::Status::Error(-668, "cannot fetch current gas prices and limits from masterchain configuration");
|
||||
}
|
||||
if (!compute_phase_cfg->parse_GasLimitsPrices(std::move(cell), storage_phase_cfg->freeze_due_limit,
|
||||
storage_phase_cfg->delete_due_limit)) {
|
||||
return td::Status::Error(-668, "cannot unpack current gas prices and limits from masterchain configuration");
|
||||
}
|
||||
compute_phase_cfg->block_rand_seed = *rand_seed;
|
||||
compute_phase_cfg->libraries = std::make_unique<vm::Dictionary>(config->get_libraries_root(), 256);
|
||||
compute_phase_cfg->max_vm_data_depth = size_limits.max_vm_data_depth;
|
||||
compute_phase_cfg->global_config = config->get_root_cell();
|
||||
compute_phase_cfg->suspended_addresses = config->get_suspended_addresses(now);
|
||||
}
|
||||
{
|
||||
// compute action_phase_cfg
|
||||
block::gen::MsgForwardPrices::Record rec;
|
||||
auto cell = config->get_config_param(24);
|
||||
if (cell.is_null() || !tlb::unpack_cell(std::move(cell), rec)) {
|
||||
return td::Status::Error(-668, "cannot fetch masterchain message transfer prices from masterchain configuration");
|
||||
}
|
||||
action_phase_cfg->fwd_mc =
|
||||
block::MsgPrices{rec.lump_price, rec.bit_price, rec.cell_price, rec.ihr_price_factor,
|
||||
(unsigned)rec.first_frac, (unsigned)rec.next_frac};
|
||||
cell = config->get_config_param(25);
|
||||
if (cell.is_null() || !tlb::unpack_cell(std::move(cell), rec)) {
|
||||
return td::Status::Error(-668, "cannot fetch standard message transfer prices from masterchain configuration");
|
||||
}
|
||||
action_phase_cfg->fwd_std =
|
||||
block::MsgPrices{rec.lump_price, rec.bit_price, rec.cell_price, rec.ihr_price_factor,
|
||||
(unsigned)rec.first_frac, (unsigned)rec.next_frac};
|
||||
action_phase_cfg->workchains = &config->get_workchain_list();
|
||||
action_phase_cfg->bounce_msg_body = (config->has_capability(ton::capBounceMsgBody) ? 256 : 0);
|
||||
action_phase_cfg->size_limits = size_limits;
|
||||
}
|
||||
{
|
||||
// fetch block_grams_created
|
||||
auto cell = config->get_config_param(14);
|
||||
if (cell.is_null()) {
|
||||
*basechain_create_fee = *masterchain_create_fee = td::zero_refint();
|
||||
} else {
|
||||
block::gen::BlockCreateFees::Record create_fees;
|
||||
if (!(tlb::unpack_cell(cell, create_fees) &&
|
||||
block::tlb::t_Grams.as_integer_to(create_fees.masterchain_block_fee, *masterchain_create_fee) &&
|
||||
block::tlb::t_Grams.as_integer_to(create_fees.basechain_block_fee, *basechain_create_fee))) {
|
||||
return td::Status::Error(-668, "cannot unpack BlockCreateFees from configuration parameter #14");
|
||||
}
|
||||
}
|
||||
}
|
||||
return std::move(config);
|
||||
}
|
||||
|
||||
bool Collator::compute_minted_amount(block::CurrencyCollection& to_mint) {
|
||||
if (!is_masterchain()) {
|
||||
return to_mint.set_zero();
|
||||
|
@ -2167,8 +2096,8 @@ bool Collator::create_ticktock_transaction(const ton::StdSmcAddress& smc_addr, t
|
|||
<< "last transaction time in the state of account " << workchain()
|
||||
<< ":" << smc_addr.to_hex() << " is too large"));
|
||||
}
|
||||
std::unique_ptr<block::Transaction> trans = std::make_unique<block::Transaction>(
|
||||
*acc, mask == 2 ? block::Transaction::tr_tick : block::Transaction::tr_tock, req_start_lt, now_);
|
||||
std::unique_ptr<block::transaction::Transaction> trans = std::make_unique<block::transaction::Transaction>(
|
||||
*acc, mask == 2 ? block::transaction::Transaction::tr_tick : block::transaction::Transaction::tr_tock, req_start_lt, now_);
|
||||
if (!trans->prepare_storage_phase(storage_phase_cfg_, true)) {
|
||||
return fatal_error(td::Status::Error(
|
||||
-666, std::string{"cannot create storage phase of a new transaction for smart contract "} + smc_addr.to_hex()));
|
||||
|
@ -2258,7 +2187,7 @@ Ref<vm::Cell> Collator::create_ordinary_transaction(Ref<vm::Cell> msg_root) {
|
|||
fatal_error(std::move(error));
|
||||
return {};
|
||||
}
|
||||
std::unique_ptr<block::Transaction> trans = res.move_as_ok();
|
||||
std::unique_ptr<block::transaction::Transaction> trans = res.move_as_ok();
|
||||
|
||||
if (!trans->update_limits(*block_limit_status_)) {
|
||||
fatal_error("cannot update block limit status to include the new transaction");
|
||||
|
@ -2277,10 +2206,13 @@ Ref<vm::Cell> Collator::create_ordinary_transaction(Ref<vm::Cell> msg_root) {
|
|||
|
||||
// If td::status::error_code == 669 - Fatal Error block can not be produced
|
||||
// if td::status::error_code == 701 - Transaction can not be included into block, but it's ok (external or too early internal)
|
||||
td::Result<std::unique_ptr<block::Transaction>> Collator::impl_create_ordinary_transaction(
|
||||
Ref<vm::Cell> msg_root, block::Account* acc, UnixTime utime, LogicalTime lt,
|
||||
block::StoragePhaseConfig* storage_phase_cfg, block::ComputePhaseConfig* compute_phase_cfg,
|
||||
block::ActionPhaseConfig* action_phase_cfg, bool external, LogicalTime after_lt) {
|
||||
td::Result<std::unique_ptr<block::transaction::Transaction>> Collator::impl_create_ordinary_transaction(Ref<vm::Cell> msg_root,
|
||||
block::Account* acc,
|
||||
UnixTime utime, LogicalTime lt,
|
||||
block::StoragePhaseConfig* storage_phase_cfg,
|
||||
block::ComputePhaseConfig* compute_phase_cfg,
|
||||
block::ActionPhaseConfig* action_phase_cfg,
|
||||
bool external, LogicalTime after_lt) {
|
||||
if (acc->last_trans_end_lt_ >= lt && acc->transactions.empty()) {
|
||||
return td::Status::Error(-669, PSTRING() << "last transaction time in the state of account " << acc->workchain
|
||||
<< ":" << acc->addr.to_hex() << " is too large");
|
||||
|
@ -2291,8 +2223,8 @@ td::Result<std::unique_ptr<block::Transaction>> Collator::impl_create_ordinary_t
|
|||
trans_min_lt = std::max(trans_min_lt, after_lt);
|
||||
}
|
||||
|
||||
std::unique_ptr<block::Transaction> trans =
|
||||
std::make_unique<block::Transaction>(*acc, block::Transaction::tr_ord, trans_min_lt + 1, utime, msg_root);
|
||||
std::unique_ptr<block::transaction::Transaction> trans =
|
||||
std::make_unique<block::transaction::Transaction>(*acc, block::transaction::Transaction::tr_ord, trans_min_lt + 1, utime, msg_root);
|
||||
bool ihr_delivered = false; // FIXME
|
||||
if (!trans->unpack_input_msg(ihr_delivered, action_phase_cfg)) {
|
||||
if (external) {
|
||||
|
@ -3041,7 +2973,7 @@ void Collator::register_new_msg(block::NewOutMsg new_msg) {
|
|||
new_msgs.push(std::move(new_msg));
|
||||
}
|
||||
|
||||
void Collator::register_new_msgs(block::Transaction& trans) {
|
||||
void Collator::register_new_msgs(block::transaction::Transaction& trans) {
|
||||
CHECK(trans.root.not_null());
|
||||
for (unsigned i = 0; i < trans.out_msgs.size(); i++) {
|
||||
register_new_msg(trans.extract_out_msg_ext(i));
|
||||
|
|
|
@ -143,16 +143,17 @@ td::Status ExtMessageQ::run_message_on_account(ton::WorkchainId wc,
|
|||
block::ActionPhaseConfig action_phase_cfg_;
|
||||
td::RefInt256 masterchain_create_fee, basechain_create_fee;
|
||||
|
||||
auto fetch_res = Collator::impl_fetch_config_params(std::move(config), &old_mparams,
|
||||
&storage_prices_, &storage_phase_cfg_,
|
||||
&rand_seed_, &compute_phase_cfg_,
|
||||
&action_phase_cfg_, &masterchain_create_fee,
|
||||
&basechain_create_fee, wc, utime);
|
||||
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);
|
||||
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: ");
|
||||
}
|
||||
compute_phase_cfg_.libraries = std::make_unique<vm::Dictionary>(config->get_libraries_root(), 256);
|
||||
compute_phase_cfg_.with_vm_log = true;
|
||||
|
||||
auto res = Collator::impl_create_ordinary_transaction(msg_root, acc, utime, lt,
|
||||
|
@ -164,7 +165,7 @@ td::Status ExtMessageQ::run_message_on_account(ton::WorkchainId wc,
|
|||
LOG(DEBUG) << "Cannot run message on account: " << error.message();
|
||||
return error.move_as_error_prefix("Cannot run message on account: ");
|
||||
}
|
||||
std::unique_ptr<block::Transaction> trans = res.move_as_ok();
|
||||
std::unique_ptr<block::transaction::Transaction> trans = res.move_as_ok();
|
||||
|
||||
auto trans_root = trans->commit(*acc);
|
||||
if (trans_root.is_null()) {
|
||||
|
|
|
@ -4359,7 +4359,7 @@ bool ValidateQuery::check_one_transaction(block::Account& account, ton::LogicalT
|
|||
<< account.total_state->get_hash().to_hex());
|
||||
}
|
||||
// some type-specific checks
|
||||
int trans_type = block::Transaction::tr_none;
|
||||
int trans_type = block::transaction::Transaction::tr_none;
|
||||
switch (tag) {
|
||||
case block::gen::TransactionDescr::trans_ord: {
|
||||
if (!block_limit_status_->fits(block::ParamLimits::cl_medium)) {
|
||||
|
@ -4369,7 +4369,7 @@ bool ValidateQuery::check_one_transaction(block::Account& account, ton::LogicalT
|
|||
<< "lt_delta=" << block_limit_status_->cur_lt - block_limits_->start_lt
|
||||
<< "(limit=" << block_limits_->lt_delta.hard() << ")");
|
||||
}
|
||||
trans_type = block::Transaction::tr_ord;
|
||||
trans_type = block::transaction::Transaction::tr_ord;
|
||||
if (in_msg_root.is_null()) {
|
||||
return reject_query(PSTRING() << "ordinary transaction " << lt << " of account " << addr.to_hex()
|
||||
<< " has no inbound message");
|
||||
|
@ -4378,7 +4378,7 @@ bool ValidateQuery::check_one_transaction(block::Account& account, ton::LogicalT
|
|||
break;
|
||||
}
|
||||
case block::gen::TransactionDescr::trans_storage: {
|
||||
trans_type = block::Transaction::tr_storage;
|
||||
trans_type = block::transaction::Transaction::tr_storage;
|
||||
if (in_msg_root.not_null()) {
|
||||
return reject_query(PSTRING() << "storage transaction " << lt << " of account " << addr.to_hex()
|
||||
<< " has an inbound message");
|
||||
|
@ -4394,7 +4394,7 @@ bool ValidateQuery::check_one_transaction(block::Account& account, ton::LogicalT
|
|||
}
|
||||
case block::gen::TransactionDescr::trans_tick_tock: {
|
||||
bool is_tock = (td_cs.prefetch_ulong(4) & 1);
|
||||
trans_type = is_tock ? block::Transaction::tr_tock : block::Transaction::tr_tick;
|
||||
trans_type = is_tock ? block::transaction::Transaction::tr_tock : block::transaction::Transaction::tr_tick;
|
||||
if (in_msg_root.not_null()) {
|
||||
return reject_query(PSTRING() << (is_tock ? "tock" : "tick") << " transaction " << lt << " of account "
|
||||
<< addr.to_hex() << " has an inbound message");
|
||||
|
@ -4402,7 +4402,7 @@ bool ValidateQuery::check_one_transaction(block::Account& account, ton::LogicalT
|
|||
break;
|
||||
}
|
||||
case block::gen::TransactionDescr::trans_merge_prepare: {
|
||||
trans_type = block::Transaction::tr_merge_prepare;
|
||||
trans_type = block::transaction::Transaction::tr_merge_prepare;
|
||||
if (in_msg_root.not_null()) {
|
||||
return reject_query(PSTRING() << "merge prepare transaction " << lt << " of account " << addr.to_hex()
|
||||
<< " has an inbound message");
|
||||
|
@ -4417,7 +4417,7 @@ bool ValidateQuery::check_one_transaction(block::Account& account, ton::LogicalT
|
|||
break;
|
||||
}
|
||||
case block::gen::TransactionDescr::trans_merge_install: {
|
||||
trans_type = block::Transaction::tr_merge_install;
|
||||
trans_type = block::transaction::Transaction::tr_merge_install;
|
||||
if (in_msg_root.is_null()) {
|
||||
return reject_query(PSTRING() << "merge install transaction " << lt << " of account " << addr.to_hex()
|
||||
<< " has no inbound message");
|
||||
|
@ -4429,7 +4429,7 @@ bool ValidateQuery::check_one_transaction(block::Account& account, ton::LogicalT
|
|||
break;
|
||||
}
|
||||
case block::gen::TransactionDescr::trans_split_prepare: {
|
||||
trans_type = block::Transaction::tr_split_prepare;
|
||||
trans_type = block::transaction::Transaction::tr_split_prepare;
|
||||
if (in_msg_root.not_null()) {
|
||||
return reject_query(PSTRING() << "split prepare transaction " << lt << " of account " << addr.to_hex()
|
||||
<< " has an inbound message");
|
||||
|
@ -4444,7 +4444,7 @@ bool ValidateQuery::check_one_transaction(block::Account& account, ton::LogicalT
|
|||
break;
|
||||
}
|
||||
case block::gen::TransactionDescr::trans_split_install: {
|
||||
trans_type = block::Transaction::tr_split_install;
|
||||
trans_type = block::transaction::Transaction::tr_split_install;
|
||||
if (in_msg_root.is_null()) {
|
||||
return reject_query(PSTRING() << "split install transaction " << lt << " of account " << addr.to_hex()
|
||||
<< " has no inbound message");
|
||||
|
@ -4459,8 +4459,8 @@ bool ValidateQuery::check_one_transaction(block::Account& account, ton::LogicalT
|
|||
// check transaction computation by re-doing it
|
||||
// similar to Collator::create_ordinary_transaction() and Collator::create_ticktock_transaction()
|
||||
// ....
|
||||
std::unique_ptr<block::Transaction> trs =
|
||||
std::make_unique<block::Transaction>(account, trans_type, lt, now_, in_msg_root);
|
||||
std::unique_ptr<block::transaction::Transaction> trs =
|
||||
std::make_unique<block::transaction::Transaction>(account, trans_type, lt, now_, in_msg_root);
|
||||
if (in_msg_root.not_null()) {
|
||||
if (!trs->unpack_input_msg(ihr_delivered, &action_phase_cfg_)) {
|
||||
// inbound external message was not accepted
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue