Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #5339 from EOSIO/gh#5149-mongo
Browse files Browse the repository at this point in the history
Add block info & ram delta to action_trace
  • Loading branch information
heifner authored Sep 13, 2018
2 parents b9a0b93 + 35f2c83 commit 92b3183
Show file tree
Hide file tree
Showing 12 changed files with 293 additions and 154 deletions.
23 changes: 19 additions & 4 deletions libraries/chain/apply_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,13 @@ action_trace apply_context::exec_one()

action_trace t(r);
t.trx_id = trx_context.id;
t.block_num = control.pending_block_state()->block_num;
t.block_time = control.pending_block_time();
t.producer_block_id = control.pending_producer_block_id();
t.account_ram_deltas = std::move( _account_ram_deltas );
_account_ram_deltas.clear();
t.act = act;
t.context_free = context_free;
t.console = _pending_console_output.str();

trx_context.executed.emplace_back( move(r) );
Expand Down Expand Up @@ -275,7 +281,7 @@ void apply_context::schedule_deferred_transaction( const uint128_t& sender_id, a
"Replacing a deferred transaction is temporarily disabled." );

// TODO: The logic of the next line needs to be incorporated into the next hard fork.
// trx_context.add_ram_usage( ptr->payer, -(config::billable_size_v<generated_transaction_object> + ptr->packed_trx.size()) );
// add_ram_usage( ptr->payer, -(config::billable_size_v<generated_transaction_object> + ptr->packed_trx.size()) );

d.modify<generated_transaction_object>( *ptr, [&]( auto& gtx ) {
gtx.sender = receiver;
Expand Down Expand Up @@ -303,14 +309,14 @@ void apply_context::schedule_deferred_transaction( const uint128_t& sender_id, a

EOS_ASSERT( control.is_ram_billing_in_notify_allowed() || (receiver == act.account) || (receiver == payer) || privileged,
subjective_block_production_exception, "Cannot charge RAM to other accounts during notify." );
trx_context.add_ram_usage( payer, (config::billable_size_v<generated_transaction_object> + trx_size) );
add_ram_usage( payer, (config::billable_size_v<generated_transaction_object> + trx_size) );
}

bool apply_context::cancel_deferred_transaction( const uint128_t& sender_id, account_name sender ) {
auto& generated_transaction_idx = db.get_mutable_index<generated_transaction_multi_index>();
const auto* gto = db.find<generated_transaction_object,by_sender_id>(boost::make_tuple(sender, sender_id));
if ( gto ) {
trx_context.add_ram_usage( gto->payer, -(config::billable_size_v<generated_transaction_object> + gto->packed_trx.size()) );
add_ram_usage( gto->payer, -(config::billable_size_v<generated_transaction_object> + gto->packed_trx.size()) );
generated_transaction_idx.remove(*gto);
}
return gto;
Expand Down Expand Up @@ -369,7 +375,7 @@ void apply_context::update_db_usage( const account_name& payer, int64_t delta )
require_authorization( payer );
}
}
trx_context.add_ram_usage(payer, delta);
add_ram_usage(payer, delta);
}


Expand Down Expand Up @@ -634,5 +640,14 @@ uint64_t apply_context::next_auth_sequence( account_name actor ) {
return rs.auth_sequence;
}

void apply_context::add_ram_usage( account_name account, int64_t ram_delta ) {
trx_context.add_ram_usage( account, ram_delta );

auto p = _account_ram_deltas.emplace( account, ram_delta );
if( !p.second ) {
p.first->delta += ram_delta;
}
}


} } /// eosio::chain
24 changes: 19 additions & 5 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ struct pending_state {

controller::block_status _block_status = controller::block_status::incomplete;

optional<block_id_type> _producer_block_id;

void push() {
_db_session.push();
}
Expand Down Expand Up @@ -629,6 +631,9 @@ struct controller_impl {
if( gtrx.expiration < self.pending_block_time() ) {
trace = std::make_shared<transaction_trace>();
trace->id = gtrx.trx_id;
trace->block_num = self.pending_block_state()->block_num;
trace->block_time = self.pending_block_time();
trace->producer_block_id = self.pending_producer_block_id();
trace->scheduled = true;
trace->receipt = push_receipt( gtrx.trx_id, transaction_receipt::expired, billed_cpu_time_us, 0 ); // expire the transaction
emit( self.accepted_transaction, trx );
Expand Down Expand Up @@ -868,7 +873,9 @@ struct controller_impl {
} /// push_transaction


void start_block( block_timestamp_type when, uint16_t confirm_block_count, controller::block_status s ) {
void start_block( block_timestamp_type when, uint16_t confirm_block_count, controller::block_status s,
const optional<block_id_type>& producer_block_id )
{
EOS_ASSERT( !pending, block_validate_exception, "pending block already exists" );

auto guard_pending = fc::make_scoped_exit([this](){
Expand All @@ -885,6 +892,7 @@ struct controller_impl {
}

pending->_block_status = s;
pending->_producer_block_id = producer_block_id;
pending->_pending_block_state = std::make_shared<block_state>( *head, when ); // promotes pending schedule (if any) to active
pending->_pending_block_state->in_current_chain = true;

Expand Down Expand Up @@ -953,7 +961,8 @@ struct controller_impl {
void apply_block( const signed_block_ptr& b, controller::block_status s ) { try {
try {
EOS_ASSERT( b->block_extensions.size() == 0, block_validate_exception, "no supported extensions" );
start_block( b->timestamp, b->confirmed, s );
auto producer_block_id = b->id();
start_block( b->timestamp, b->confirmed, s , producer_block_id);

transaction_trace_ptr trace;

Expand Down Expand Up @@ -993,9 +1002,9 @@ struct controller_impl {
finalize_block();

// this implicitly asserts that all header fields (less the signature) are identical
EOS_ASSERT(b->id() == pending->_pending_block_state->header.id(),
EOS_ASSERT(producer_block_id == pending->_pending_block_state->header.id(),
block_validate_exception, "Block ID does not match",
("producer_block_id",b->id())("validator_block_id",pending->_pending_block_state->header.id()));
("producer_block_id",producer_block_id)("validator_block_id",pending->_pending_block_state->header.id()));

// We need to fill out the pending block state's block because that gets serialized in the reversible block log
// in the future we can optimize this by serializing the original and not the copy
Expand Down Expand Up @@ -1389,7 +1398,7 @@ fork_database& controller::fork_db()const { return my->fork_db; }

void controller::start_block( block_timestamp_type when, uint16_t confirm_block_count) {
validate_db_available_size();
my->start_block(when, confirm_block_count, block_status::incomplete );
my->start_block(when, confirm_block_count, block_status::incomplete, optional<block_id_type>() );
}

void controller::finalize_block() {
Expand Down Expand Up @@ -1521,6 +1530,11 @@ time_point controller::pending_block_time()const {
return my->pending->_pending_block_state->header.timestamp;
}

optional<block_id_type> controller::pending_producer_block_id()const {
EOS_ASSERT( my->pending, block_validate_exception, "no pending block" );
return my->pending->_producer_block_id;
}

uint32_t controller::last_irreversible_block_num() const {
return std::max(my->head->bft_irreversible_blocknum, my->head->dpos_irreversible_blocknum);
}
Expand Down
16 changes: 8 additions & 8 deletions libraries/chain/eosio_contract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ void apply_eosio_newaccount(apply_context& context) {
ram_delta += owner_permission.auth.get_billable_size();
ram_delta += active_permission.auth.get_billable_size();

context.trx_context.add_ram_usage(create.name, ram_delta);
context.add_ram_usage(create.name, ram_delta);

} FC_CAPTURE_AND_RETHROW( (create) ) }

Expand Down Expand Up @@ -167,7 +167,7 @@ void apply_eosio_setcode(apply_context& context) {
});

if (new_size != old_size) {
context.trx_context.add_ram_usage( act.account, new_size - old_size );
context.add_ram_usage( act.account, new_size - old_size );
}
}

Expand Down Expand Up @@ -196,7 +196,7 @@ void apply_eosio_setabi(apply_context& context) {
});

if (new_size != old_size) {
context.trx_context.add_ram_usage( act.account, new_size - old_size );
context.add_ram_usage( act.account, new_size - old_size );
}
}

Expand Down Expand Up @@ -254,13 +254,13 @@ void apply_eosio_updateauth(apply_context& context) {

int64_t new_size = (int64_t)(config::billable_size_v<permission_object> + permission->auth.get_billable_size());

context.trx_context.add_ram_usage( permission->owner, new_size - old_size );
context.add_ram_usage( permission->owner, new_size - old_size );
} else {
const auto& p = authorization.create_permission( update.account, update.permission, parent_id, update.auth );

int64_t new_size = (int64_t)(config::billable_size_v<permission_object> + p.auth.get_billable_size());

context.trx_context.add_ram_usage( update.account, new_size );
context.add_ram_usage( update.account, new_size );
}
}

Expand Down Expand Up @@ -291,7 +291,7 @@ void apply_eosio_deleteauth(apply_context& context) {

authorization.remove_permission( permission );

context.trx_context.add_ram_usage( remove.account, -old_size );
context.add_ram_usage( remove.account, -old_size );

}

Expand Down Expand Up @@ -334,7 +334,7 @@ void apply_eosio_linkauth(apply_context& context) {
link.required_permission = requirement.requirement;
});

context.trx_context.add_ram_usage(
context.add_ram_usage(
l.account,
(int64_t)(config::billable_size_v<permission_link_object>)
);
Expand All @@ -354,7 +354,7 @@ void apply_eosio_unlinkauth(apply_context& context) {
auto link_key = boost::make_tuple(unlink.account, unlink.code, unlink.type);
auto link = db.find<permission_link_object, by_action_name>(link_key);
EOS_ASSERT(link != nullptr, action_validate_exception, "Attempting to unlink authority, but no link found");
context.trx_context.add_ram_usage(
context.add_ram_usage(
link->account,
-(int64_t)(config::billable_size_v<permission_link_object>)
);
Expand Down
3 changes: 3 additions & 0 deletions libraries/chain/include/eosio/chain/apply_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,8 @@ class apply_context {
uint64_t next_recv_sequence( account_name receiver );
uint64_t next_auth_sequence( account_name actor );

void add_ram_usage( account_name account, int64_t ram_delta );

private:

void validate_referenced_accounts( const transaction& t )const;
Expand Down Expand Up @@ -607,6 +609,7 @@ class apply_context {
vector<action> _inline_actions; ///< queued inline messages
vector<action> _cfa_inline_actions; ///< queued inline messages
std::ostringstream _pending_console_output;
flat_set<account_delta> _account_ram_deltas; ///< flat_set of account_delta so json is an array of objects

//bytes _cached_trx;
};
Expand Down
5 changes: 3 additions & 2 deletions libraries/chain/include/eosio/chain/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,9 @@ namespace eosio { namespace chain {
time_point fork_db_head_block_time()const;
account_name fork_db_head_block_producer()const;

time_point pending_block_time()const;
block_state_ptr pending_block_state()const;
time_point pending_block_time()const;
block_state_ptr pending_block_state()const;
optional<block_id_type> pending_producer_block_id()const;

const producer_schedule_type& active_producers()const;
const producer_schedule_type& pending_producers()const;
Expand Down
27 changes: 25 additions & 2 deletions libraries/chain/include/eosio/chain/trace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,33 @@

namespace eosio { namespace chain {

struct account_delta {
account_delta( const account_name& n, int64_t d):account(n),delta(d){}
account_delta(){}

account_name account;
int64_t delta = 0;

friend bool operator<( const account_delta& lhs, const account_delta& rhs ) { return lhs.account < rhs.account; }
};

struct base_action_trace {
base_action_trace( const action_receipt& r ):receipt(r){}
base_action_trace(){}

action_receipt receipt;
action act;
bool context_free = false;
fc::microseconds elapsed;
uint64_t cpu_usage = 0;
string console;

uint64_t total_cpu_usage = 0; /// total of inline_traces[x].cpu_usage + cpu_usage
transaction_id_type trx_id; ///< the transaction that generated this action
uint32_t block_num = 0;
block_timestamp_type block_time;
fc::optional<block_id_type> producer_block_id;
flat_set<account_delta> account_ram_deltas;
};

struct action_trace : public base_action_trace {
Expand All @@ -35,6 +50,9 @@ namespace eosio { namespace chain {

struct transaction_trace {
transaction_id_type id;
uint32_t block_num = 0;
block_timestamp_type block_time;
fc::optional<block_id_type> producer_block_id;
fc::optional<transaction_receipt_header> receipt;
fc::microseconds elapsed;
uint64_t net_usage = 0;
Expand All @@ -48,11 +66,16 @@ namespace eosio { namespace chain {

} } /// namespace eosio::chain

FC_REFLECT( eosio::chain::account_delta,
(account)(delta) )

FC_REFLECT( eosio::chain::base_action_trace,
(receipt)(act)(elapsed)(cpu_usage)(console)(total_cpu_usage)(trx_id) )
(receipt)(act)(context_free)(elapsed)(cpu_usage)(console)(total_cpu_usage)(trx_id)
(block_num)(block_time)(producer_block_id)(account_ram_deltas) )

FC_REFLECT_DERIVED( eosio::chain::action_trace,
(eosio::chain::base_action_trace), (inline_traces) )

FC_REFLECT( eosio::chain::transaction_trace, (id)(receipt)(elapsed)(net_usage)(scheduled)
FC_REFLECT( eosio::chain::transaction_trace, (id)(block_num)(block_time)(producer_block_id)
(receipt)(elapsed)(net_usage)(scheduled)
(action_traces)(failed_dtrx_trace)(except) )
4 changes: 2 additions & 2 deletions libraries/chain/include/eosio/chain/transaction_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ namespace eosio { namespace chain {
void pause_billing_timer();
void resume_billing_timer();

void add_ram_usage( account_name account, int64_t ram_delta );

uint32_t update_billed_cpu_time( fc::time_point now );

std::tuple<int64_t, int64_t, bool, bool> max_bandwidth_billed_accounts_can_pay( bool force_elastic_limits = false )const;
Expand All @@ -49,6 +47,8 @@ namespace eosio { namespace chain {
friend struct controller_impl;
friend class apply_context;

void add_ram_usage( account_name account, int64_t ram_delta );

void dispatch_action( action_trace& trace, const action& a, account_name receiver, bool context_free = false, uint32_t recurse_depth = 0 );
inline void dispatch_action( action_trace& trace, const action& a, bool context_free = false ) {
dispatch_action(trace, a, a.account, context_free);
Expand Down
3 changes: 3 additions & 0 deletions libraries/chain/transaction_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ namespace eosio { namespace chain {
undo_session = c.db().start_undo_session(true);
}
trace->id = id;
trace->block_num = c.pending_block_state()->block_num;
trace->block_time = c.pending_block_time();
trace->producer_block_id = c.pending_producer_block_id();
executed.reserve( trx.total_actions() );
EOS_ASSERT( trx.transaction_extensions.size() == 0, unsupported_feature, "we don't support any extensions yet" );
}
Expand Down
Loading

0 comments on commit 92b3183

Please sign in to comment.