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
13 changes: 10 additions & 3 deletions benchmark/bls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,17 @@ struct interface_in_benchmark {
// build transaction context from the packed transaction
timer = std::make_unique<platform_timer>();
trx_timer = std::make_unique<transaction_checktime_timer>(*timer);
trx_ctx = std::make_unique<transaction_context>(*chain->control.get(), *ptrx, ptrx->id(), std::move(*trx_timer),
trx_ctx = std::make_unique<transaction_context>(*chain->control.get(),
*ptrx,
std::move(*trx_timer),
fc::time_point::now(),
transaction_metadata::trx_type::input);
trx_ctx->max_transaction_time_subjective = fc::microseconds::maximum();
transaction_metadata::trx_type::input,
std::nullopt,
fc::time_point::maximum(),
fc::microseconds::maximum(),
false,
accounts_billing_t{},
cpu_usage_t{});
trx_ctx->init_for_input_trx();
trx_ctx->exec(); // this is required to generate action traces to be used by apply_context constructor

Expand Down
22 changes: 12 additions & 10 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1951,16 +1951,18 @@ struct controller_impl {

const signed_transaction& trn = trx->packed_trx()->get_signed_transaction();
transaction_checktime_timer trx_timer(timer);
transaction_context trx_context(self, *trx->packed_trx(), trx->id(), std::move(trx_timer),
start, trx->get_trx_type());
if ((bool)subjective_cpu_leeway && is_speculative_block()) {
trx_context.leeway = *subjective_cpu_leeway;
}
trx_context.block_deadline = block_deadline;
trx_context.max_transaction_time_subjective = max_transaction_time;
trx_context.explicit_billed_cpu_time = explicit_billed_cpu_time;
trx_context.billed_cpu_us = billed_cpu_us;
trx_context.prev_accounts_billing = trx->prev_accounts_billing;
transaction_context trx_context(self,
*trx->packed_trx(),
std::move(trx_timer),
start,
trx->get_trx_type(),
subjective_cpu_leeway,
block_deadline,
max_transaction_time,
explicit_billed_cpu_time,
trx->prev_accounts_billing,
billed_cpu_us
);
trace = trx_context.trace;

auto handle_exception =[&](const auto& e)
Expand Down
27 changes: 15 additions & 12 deletions libraries/chain/include/sysio/chain/transaction_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,15 @@ namespace sysio::chain {

transaction_context( controller& c,
const packed_transaction& t,
const transaction_id_type& trx_id, // trx_id diff than t.id() before replace_deferred
transaction_checktime_timer&& timer,
fc::time_point start,
transaction_metadata::trx_type type);
transaction_metadata::trx_type type,
const std::optional<fc::microseconds>& subjective_cpu_leeway,
const fc::time_point& block_deadline,
const fc::microseconds& max_transaction_time_subjective,
bool explicit_billed_cpu_time,
const accounts_billing_t& prev_accounts_billing,
const cpu_usage_t& billed_cpu_us );
~transaction_context();

void init_for_implicit_trx();
Expand Down Expand Up @@ -184,7 +189,6 @@ namespace sysio::chain {

controller& control;
const packed_transaction& packed_trx;
const transaction_id_type& id;
std::optional<chainbase::database::session> undo_session;
transaction_trace_ptr trace;
fc::time_point start;
Expand All @@ -201,19 +205,19 @@ namespace sysio::chain {
bool is_input = false;
bool enforce_whiteblacklist = true;

fc::time_point block_deadline = fc::time_point::maximum();
fc::microseconds leeway = fc::microseconds( config::default_subjective_cpu_leeway_us );
cpu_usage_t billed_cpu_us;
accounts_billing_t prev_accounts_billing;
bool explicit_billed_cpu_time = false;

transaction_checktime_timer transaction_timer;

private:
bool enforce_deadline = true;
bool is_initialized = false;
bool is_cpu_updated = false;
transaction_metadata::trx_type trx_type;
const transaction_metadata::trx_type trx_type;
const fc::microseconds leeway;
const bool enforce_deadline;
const fc::time_point block_deadline;
const fc::microseconds max_transaction_time_subjective;
const bool explicit_billed_cpu_time;
const accounts_billing_t& prev_accounts_billing;
cpu_usage_t billed_cpu_us;

uint64_t trx_net_limit = 0;
bool net_limit_due_to_block = true;
Expand All @@ -222,7 +226,6 @@ namespace sysio::chain {
bool cpu_limit_due_to_greylist = false;
fc::microseconds subjective_cpu_bill;

fc::microseconds max_transaction_time_subjective;
fc::time_point paused_time;
fc::microseconds objective_duration_limit;
fc::time_point trx_deadline = fc::time_point::maximum(); // calculated deadline
Expand Down
61 changes: 34 additions & 27 deletions libraries/chain/transaction_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,39 @@ namespace sysio::chain {

transaction_context::transaction_context( controller& c,
const packed_transaction& t,
const transaction_id_type& trx_id,
transaction_checktime_timer&& tmr,
fc::time_point s,
transaction_metadata::trx_type type)
transaction_metadata::trx_type type,
const std::optional<fc::microseconds>& subjective_cpu_leeway,
const fc::time_point& block_deadline,
const fc::microseconds& max_transaction_time_subjective,
bool explicit_billed_cpu_time,
const accounts_billing_t& prev_accounts_billing,
const cpu_usage_t& billed_cpu_us
)
:control(c)
,packed_trx(t)
,id(trx_id)
,undo_session()
,trace(std::make_shared<transaction_trace>())
,start(s)
,executed_action_receipts()
,transaction_timer(std::move(tmr))
,trx_type(type)
,leeway(subjective_cpu_leeway.value_or(fc::microseconds(config::default_subjective_cpu_leeway_us)))
// set maximum to a semi-valid deadline (six months) to allow for pause math and conversion to dates for logging
,enforce_deadline(block_deadline != fc::time_point::maximum())
,block_deadline(block_deadline == fc::time_point::maximum() ? s + fc::hours(24*7*26) : block_deadline)
,max_transaction_time_subjective(max_transaction_time_subjective)
,explicit_billed_cpu_time(explicit_billed_cpu_time)
,prev_accounts_billing(prev_accounts_billing)
,billed_cpu_us(billed_cpu_us)
,pseudo_start(s)
{
initialize();

if(auto dm_logger = control.get_deep_mind_logger(is_transient())) {
dm_logger->on_start_transaction();
}
}

void transaction_context::reset() {
Expand All @@ -62,7 +79,8 @@ namespace sysio::chain {
*trace = transaction_trace{}; // reset trace
trace->net_usage = net_usage;
initialize();
billed_cpu_us.clear();
if (!explicit_billed_cpu_time)
billed_cpu_us.clear();
trx_blk_context = trx_block_context{};
transaction_timer.stop();
if (paused_timer) {
Expand All @@ -81,13 +99,17 @@ namespace sysio::chain {
undo_session.emplace(control.mutable_db().start_undo_session(true));
}

trace->id = id;
trace->id = packed_trx.id();
trace->block_num = control.head().block_num() + 1;
trace->block_time = control.pending_block_time();
trace->producer_block_id = control.pending_producer_block_id();

if(auto dm_logger = control.get_deep_mind_logger(is_transient())) {
dm_logger->on_start_transaction();
const transaction& trx = packed_trx.get_transaction();
if (explicit_billed_cpu_time) {
SYS_ASSERT(billed_cpu_us.size() == trx.total_actions(), transaction_exception, "No transaction receipt cpu usage");
trace->total_cpu_usage_us = std::ranges::fold_left(billed_cpu_us, 0l, std::plus());
} else {
billed_cpu_us.reserve(trx.total_actions());
}
}

Expand Down Expand Up @@ -119,11 +141,6 @@ namespace sysio::chain {

published = control.pending_block_time();

// set maximum to a semi-valid deadline to allow for pause math and conversion to dates for logging
const fc::time_point six_months = start + fc::hours(24*7*52);
if( block_deadline == fc::time_point::maximum() )
block_deadline = six_months; // half-year

const auto& cfg = control.get_global_properties().configuration;
auto& rl = control.get_mutable_resource_limits_manager();

Expand Down Expand Up @@ -175,14 +192,8 @@ namespace sysio::chain {

leeway_trx_net_limit = trx_net_limit; // no leeway for block, cfg.max_transaction_net_usage, or trx.max_net_usage_words

if ( !is_read_only() ) {
if (explicit_billed_cpu_time) {
SYS_ASSERT(billed_cpu_us.size() == trx.total_actions(), transaction_exception, "No transaction receipt cpu usage");
trace->total_cpu_usage_us = std::ranges::fold_left(billed_cpu_us, 0l, std::plus());
validate_trx_billed_cpu();
} else {
billed_cpu_us.reserve(trx.total_actions());
}
if ( !is_read_only() && explicit_billed_cpu_time ) {
validate_trx_billed_cpu();
}

std::array all_actions = {std::views::all(trx.context_free_actions), std::views::all(trx.actions)};
Expand Down Expand Up @@ -255,12 +266,8 @@ namespace sysio::chain {
if(control.skip_trx_checks()) {
trx_deadline = block_deadline;
}
if (trx_deadline >= six_months) {
enforce_deadline = false;
active_deadline = fc::time_point::maximum();
} else {
active_deadline = trx_deadline;
}

active_deadline = enforce_deadline ? trx_deadline : fc::time_point::maximum();
transaction_timer.start( active_deadline );
checktime(); // Fail early if deadline as already been exceeded

Expand Down Expand Up @@ -296,7 +303,7 @@ namespace sysio::chain {

init();
if ( !is_read_only() ) {
record_transaction( id, trx.expiration );
record_transaction( packed_trx.id(), trx.expiration );
}
}

Expand Down
Loading