Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion solver/include/solver/smartcontracts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <optional>
#include <vector>
#include <unordered_map>
#include <atomic>
#include <unordered_set>

//#define DEBUG_SMARTS
Expand Down Expand Up @@ -261,6 +262,12 @@ struct SmartExecutionData {
std::string error;
std::string explicit_last_state;
csdb::Address abs_addr;
// resolved under the lock by execute_async, so the worker thread does not read the queue and the
// known contracts while the main thread changes them; synchronous callers leave it unresolved
bool resolved = false;
csdb::Transaction transaction;
csdb::Transaction deploy;
cs::Executor::MethodNameConvention convention = cs::Executor::MethodNameConvention::Default;

void setError(int8_t code, const char* message) {
if (!result.smartsRes.empty()) {
Expand Down Expand Up @@ -574,7 +581,8 @@ public slots:
csconnector::connector::ApiExecHandlerPtr exec_handler_ptr;

// flag to allow execution, currently depends on executor presence
bool executor_ready;
// written by the worker thread of execute_async too
std::atomic<bool> executor_ready;

// max block sequence read from DB
cs::Sequence max_read_sequence;
Expand Down Expand Up @@ -826,6 +834,8 @@ public slots:

csdb::Transaction get_deploy_transaction(const csdb::Address& abs_addr) const;

cs::Executor::MethodNameConvention method_convention(const csdb::Transaction& transaction) const;

void enqueue(const csdb::Pool& block, size_t trx_idx, bool skip_log);

// perform async execution via API to remote executor
Expand Down
42 changes: 26 additions & 16 deletions solver/src/smartcontracts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,23 @@ csdb::Transaction SmartContracts::get_transaction(const SmartContractRef& contra
return SmartContracts::get_transaction(bc, contract);
}

cs::Executor::MethodNameConvention SmartContracts::method_convention(const csdb::Transaction& transaction) const {
if (!is_smart(transaction)) {
// the most frequent fast test
auto item = known_contracts.find(absolute_address(transaction.target()));
if (item != known_contracts.end()) {
const StateItem& state = item->second;
if (state.payable == PayableStatus::Implemented) {
return cs::Executor::MethodNameConvention::PayableLegacy;
}
if (state.payable == PayableStatus::ImplementedVer1) {
return cs::Executor::MethodNameConvention::Payable;
}
}
}
return cs::Executor::MethodNameConvention::Default;
}

csdb::Transaction SmartContracts::get_deploy_transaction(const csdb::Address& abs_addr) const {
//csdebug() << kLogPrefix << __func__;
auto it_state = known_contracts.find(abs_addr);
Expand Down Expand Up @@ -1891,7 +1908,7 @@ bool SmartContracts::execute(SmartExecutionData& data, bool validationMode) {
data.setError(error::ExecuteTransaction, "contract executor is unavailable");
return false;
}
csdb::Transaction transaction = get_transaction(data.contract_ref, data.abs_addr);
csdb::Transaction transaction = data.resolved ? data.transaction : get_transaction(data.contract_ref, data.abs_addr);
if (!transaction.is_valid()) {
data.setError(error::InternalBug, "load starter transaction failed");
return false;
Expand All @@ -1906,25 +1923,12 @@ bool SmartContracts::execute(SmartExecutionData& data, bool validationMode) {
std::vector<cs::Executor::ExecuteTransactionInfo> smarts;
auto& info = smarts.emplace_back(cs::Executor::ExecuteTransactionInfo{});
info.transaction = transaction;
info.deploy = get_deploy_transaction(data.abs_addr);
info.deploy = data.resolved ? data.deploy : get_deploy_transaction(data.abs_addr);
info.sequence = data.contract_ref.sequence;
// data.executor_fee bring all available fee for future execution:
info.feeLimit = data.executor_fee;
data.executor_fee = csdb::Amount(0);
info.convention = cs::Executor::MethodNameConvention::Default;
if (!is_smart(transaction)) {
// the most frequent fast test
auto item = known_contracts.find(absolute_address(transaction.target()));
if (item != known_contracts.end()) {
StateItem& state = item->second;
if (state.payable == PayableStatus::Implemented) {
info.convention = cs::Executor::MethodNameConvention::PayableLegacy;
}
else if (state.payable == PayableStatus::ImplementedVer1) {
info.convention = cs::Executor::MethodNameConvention::Payable;
}
}
}
info.convention = data.resolved ? data.convention : method_convention(transaction);
std::optional<cs::Executor::ExecuteResult> maybe_result;
if (validationMode) {
// for now smarts always contains a one item:
Expand Down Expand Up @@ -2025,6 +2029,12 @@ bool SmartContracts::execute_async(const std::vector<ExecutionItem>& executions)
execution_data.contract_ref = execution.ref_start;
execution_data.abs_addr = absolute_address(start_tr.target());
execution_data.executor_fee = execution.avail_fee;
// execute_async runs under public_access_lock (checkExeQueue): resolve here what execute() would
// otherwise read from exe_queue and known_contracts on the worker thread
execution_data.transaction = get_transaction(execution_data.contract_ref, execution_data.abs_addr);
execution_data.deploy = get_deploy_transaction(execution_data.abs_addr);
execution_data.convention = method_convention(execution_data.transaction);
execution_data.resolved = true;
bool replenish_only = false; // means indirect call to payable()
if (!is_executable(start_tr)) {
replenish_only = is_payable_target(start_tr);
Expand Down
Loading