mirror of
https://github.com/ton-blockchain/ton
synced 2025-03-09 15:40:10 +00:00
Add transaction running elapsed time to emulator response (#616)
This commit is contained in:
parent
5a3e6ec559
commit
6000a2646c
3 changed files with 23 additions and 12 deletions
|
@ -19,7 +19,8 @@ td::Result<std::string> cell_to_boc_b64(td::Ref<vm::Cell> cell) {
|
||||||
return td::base64_encode(boc.as_slice());
|
return td::base64_encode(boc.as_slice());
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *success_response(std::string&& transaction, std::string&& new_shard_account, std::string&& vm_log, td::optional<std::string>&& actions) {
|
const char *success_response(std::string&& transaction, std::string&& new_shard_account, std::string&& vm_log,
|
||||||
|
td::optional<std::string>&& actions, double elapsed_time) {
|
||||||
td::JsonBuilder jb;
|
td::JsonBuilder jb;
|
||||||
auto json_obj = jb.enter_object();
|
auto json_obj = jb.enter_object();
|
||||||
json_obj("success", td::JsonTrue());
|
json_obj("success", td::JsonTrue());
|
||||||
|
@ -31,6 +32,7 @@ const char *success_response(std::string&& transaction, std::string&& new_shard_
|
||||||
} else {
|
} else {
|
||||||
json_obj("actions", td::JsonNull());
|
json_obj("actions", td::JsonNull());
|
||||||
}
|
}
|
||||||
|
json_obj("elapsed_time", elapsed_time);
|
||||||
json_obj.leave();
|
json_obj.leave();
|
||||||
return strdup(jb.string_builder().as_cslice().c_str());
|
return strdup(jb.string_builder().as_cslice().c_str());
|
||||||
}
|
}
|
||||||
|
@ -44,13 +46,14 @@ const char *error_response(std::string&& error) {
|
||||||
return strdup(jb.string_builder().as_cslice().c_str());
|
return strdup(jb.string_builder().as_cslice().c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *external_not_accepted_response(std::string&& vm_log, int vm_exit_code) {
|
const char *external_not_accepted_response(std::string&& vm_log, int vm_exit_code, double elapsed_time) {
|
||||||
td::JsonBuilder jb;
|
td::JsonBuilder jb;
|
||||||
auto json_obj = jb.enter_object();
|
auto json_obj = jb.enter_object();
|
||||||
json_obj("success", td::JsonFalse());
|
json_obj("success", td::JsonFalse());
|
||||||
json_obj("error", "External message not accepted by smart contract");
|
json_obj("error", "External message not accepted by smart contract");
|
||||||
json_obj("vm_log", std::move(vm_log));
|
json_obj("vm_log", std::move(vm_log));
|
||||||
json_obj("vm_exit_code", vm_exit_code);
|
json_obj("vm_exit_code", vm_exit_code);
|
||||||
|
json_obj("elapsed_time", elapsed_time);
|
||||||
json_obj.leave();
|
json_obj.leave();
|
||||||
return strdup(jb.string_builder().as_cslice().c_str());
|
return strdup(jb.string_builder().as_cslice().c_str());
|
||||||
}
|
}
|
||||||
|
@ -142,7 +145,8 @@ const char *transaction_emulator_emulate_transaction(void *transaction_emulator,
|
||||||
|
|
||||||
auto external_not_accepted = dynamic_cast<emulator::TransactionEmulator::EmulationExternalNotAccepted *>(emulation_result.get());
|
auto external_not_accepted = dynamic_cast<emulator::TransactionEmulator::EmulationExternalNotAccepted *>(emulation_result.get());
|
||||||
if (external_not_accepted) {
|
if (external_not_accepted) {
|
||||||
return external_not_accepted_response(std::move(external_not_accepted->vm_log), external_not_accepted->vm_exit_code);
|
return external_not_accepted_response(std::move(external_not_accepted->vm_log), external_not_accepted->vm_exit_code,
|
||||||
|
external_not_accepted->elapsed_time);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto emulation_success = dynamic_cast<emulator::TransactionEmulator::EmulationSuccess&>(*emulation_result);
|
auto emulation_success = dynamic_cast<emulator::TransactionEmulator::EmulationSuccess&>(*emulation_result);
|
||||||
|
@ -168,7 +172,8 @@ const char *transaction_emulator_emulate_transaction(void *transaction_emulator,
|
||||||
actions_boc_b64 = actions_boc_b64_result.move_as_ok();
|
actions_boc_b64 = actions_boc_b64_result.move_as_ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
return success_response(trans_boc_b64.move_as_ok(), new_shard_account_boc_b64.move_as_ok(), std::move(emulation_success.vm_log), std::move(actions_boc_b64));
|
return success_response(trans_boc_b64.move_as_ok(), new_shard_account_boc_b64.move_as_ok(), std::move(emulation_success.vm_log),
|
||||||
|
std::move(actions_boc_b64), emulation_success.elapsed_time);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool transaction_emulator_set_unixtime(void *transaction_emulator, uint32_t unixtime) {
|
bool transaction_emulator_set_unixtime(void *transaction_emulator, uint32_t unixtime) {
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#include "transaction-emulator.h"
|
#include "transaction-emulator.h"
|
||||||
#include "crypto/common/refcnt.hpp"
|
#include "crypto/common/refcnt.hpp"
|
||||||
#include "vm/cp0.h"
|
#include "vm/cp0.h"
|
||||||
|
#include "tdutils/td/utils/Time.h"
|
||||||
|
|
||||||
using td::Ref;
|
using td::Ref;
|
||||||
using namespace std::string_literals;
|
using namespace std::string_literals;
|
||||||
|
@ -47,9 +48,12 @@ td::Result<std::unique_ptr<TransactionEmulator::EmulationResult>> TransactionEmu
|
||||||
compute_phase_cfg.with_vm_log = true;
|
compute_phase_cfg.with_vm_log = true;
|
||||||
compute_phase_cfg.vm_log_verbosity = vm_log_verbosity_;
|
compute_phase_cfg.vm_log_verbosity = vm_log_verbosity_;
|
||||||
|
|
||||||
|
double start_time = td::Time::now();
|
||||||
auto res = create_transaction(msg_root, &account, utime, lt, trans_type,
|
auto res = create_transaction(msg_root, &account, utime, lt, trans_type,
|
||||||
&storage_phase_cfg, &compute_phase_cfg,
|
&storage_phase_cfg, &compute_phase_cfg,
|
||||||
&action_phase_cfg);
|
&action_phase_cfg);
|
||||||
|
double elapsed = td::Time::now() - start_time;
|
||||||
|
|
||||||
if(res.is_error()) {
|
if(res.is_error()) {
|
||||||
return res.move_as_error_prefix("cannot run message on account ");
|
return res.move_as_error_prefix("cannot run message on account ");
|
||||||
}
|
}
|
||||||
|
@ -58,7 +62,7 @@ td::Result<std::unique_ptr<TransactionEmulator::EmulationResult>> TransactionEmu
|
||||||
if (!trans->compute_phase->accepted && trans->in_msg_extern) {
|
if (!trans->compute_phase->accepted && trans->in_msg_extern) {
|
||||||
auto vm_log = trans->compute_phase->vm_log;
|
auto vm_log = trans->compute_phase->vm_log;
|
||||||
auto vm_exit_code = trans->compute_phase->exit_code;
|
auto vm_exit_code = trans->compute_phase->exit_code;
|
||||||
return std::make_unique<TransactionEmulator::EmulationExternalNotAccepted>(std::move(vm_log), vm_exit_code);
|
return std::make_unique<TransactionEmulator::EmulationExternalNotAccepted>(std::move(vm_log), vm_exit_code, elapsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!trans->serialize()) {
|
if (!trans->serialize()) {
|
||||||
|
@ -70,7 +74,8 @@ td::Result<std::unique_ptr<TransactionEmulator::EmulationResult>> TransactionEmu
|
||||||
return td::Status::Error(PSLICE() << "cannot commit new transaction for smart contract");
|
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));
|
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), elapsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
td::Result<TransactionEmulator::EmulationSuccess> TransactionEmulator::emulate_transaction(block::Account&& account, td::Ref<vm::Cell> original_trans) {
|
td::Result<TransactionEmulator::EmulationSuccess> TransactionEmulator::emulate_transaction(block::Account&& account, td::Ref<vm::Cell> original_trans) {
|
||||||
|
|
|
@ -25,8 +25,9 @@ public:
|
||||||
|
|
||||||
struct EmulationResult {
|
struct EmulationResult {
|
||||||
std::string vm_log;
|
std::string vm_log;
|
||||||
|
double elapsed_time;
|
||||||
|
|
||||||
EmulationResult(std::string vm_log_) : vm_log(vm_log_) {}
|
EmulationResult(std::string vm_log_, double elapsed_time_) : vm_log(vm_log_), elapsed_time(elapsed_time_) {}
|
||||||
virtual ~EmulationResult() = default;
|
virtual ~EmulationResult() = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -35,16 +36,16 @@ public:
|
||||||
block::Account account;
|
block::Account account;
|
||||||
td::Ref<vm::Cell> actions;
|
td::Ref<vm::Cell> actions;
|
||||||
|
|
||||||
EmulationSuccess(td::Ref<vm::Cell> transaction_, block::Account account_, std::string vm_log_, td::Ref<vm::Cell> actions_) :
|
EmulationSuccess(td::Ref<vm::Cell> transaction_, block::Account account_, std::string vm_log_, td::Ref<vm::Cell> actions_, double elapsed_time_) :
|
||||||
EmulationResult(vm_log_), transaction(transaction_), account(account_) , actions(actions_)
|
EmulationResult(vm_log_, elapsed_time_), transaction(transaction_), account(account_) , actions(actions_)
|
||||||
{}
|
{}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct EmulationExternalNotAccepted: EmulationResult {
|
struct EmulationExternalNotAccepted: EmulationResult {
|
||||||
int vm_exit_code;
|
int vm_exit_code;
|
||||||
|
|
||||||
EmulationExternalNotAccepted(std::string vm_log_, int vm_exit_code_) :
|
EmulationExternalNotAccepted(std::string vm_log_, int vm_exit_code_, double elapsed_time_) :
|
||||||
EmulationResult(vm_log_), vm_exit_code(vm_exit_code_)
|
EmulationResult(vm_log_, elapsed_time_), vm_exit_code(vm_exit_code_)
|
||||||
{}
|
{}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue