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 #7150 from EOSIO/ship-partial-transaction
Browse files Browse the repository at this point in the history
 ship: add partial transaction to trace
  • Loading branch information
tbfleming authored Apr 17, 2019
2 parents a0f40f5 + 2bad775 commit d891a76
Show file tree
Hide file tree
Showing 12 changed files with 166 additions and 66 deletions.
14 changes: 7 additions & 7 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,7 @@ struct controller_impl {
trace->receipt = push_receipt( gtrx.trx_id, transaction_receipt::expired, billed_cpu_time_us, 0 ); // expire the transaction
trace->account_ram_delta = account_delta( gtrx.payer, trx_removal_ram_delta );
emit( self.accepted_transaction, trx );
emit( self.applied_transaction, trace );
emit( self.applied_transaction, std::tie(trace, dtrx) );
undo_session.squash();
return trace;
}
Expand Down Expand Up @@ -1125,7 +1125,7 @@ struct controller_impl {
trace->account_ram_delta = account_delta( gtrx.payer, trx_removal_ram_delta );

emit( self.accepted_transaction, trx );
emit( self.applied_transaction, trace );
emit( self.applied_transaction, std::tie(trace, dtrx) );

trx_context.squash();
undo_session.squash();
Expand Down Expand Up @@ -1159,7 +1159,7 @@ struct controller_impl {
if( !trace->except_ptr ) {
trace->account_ram_delta = account_delta( gtrx.payer, trx_removal_ram_delta );
emit( self.accepted_transaction, trx );
emit( self.applied_transaction, trace );
emit( self.applied_transaction, std::tie(trace, dtrx) );
undo_session.squash();
return trace;
}
Expand Down Expand Up @@ -1197,12 +1197,12 @@ struct controller_impl {
trace->account_ram_delta = account_delta( gtrx.payer, trx_removal_ram_delta );

emit( self.accepted_transaction, trx );
emit( self.applied_transaction, trace );
emit( self.applied_transaction, std::tie(trace, dtrx) );

undo_session.squash();
} else {
emit( self.accepted_transaction, trx );
emit( self.applied_transaction, trace );
emit( self.applied_transaction, std::tie(trace, dtrx) );
}

return trace;
Expand Down Expand Up @@ -1314,7 +1314,7 @@ struct controller_impl {
emit( self.accepted_transaction, trx);
}

emit(self.applied_transaction, trace);
emit(self.applied_transaction, std::tie(trace, trn));


if ( read_mode != db_read_mode::SPECULATIVE && pending->_block_status == controller::block_status::incomplete ) {
Expand Down Expand Up @@ -1344,7 +1344,7 @@ struct controller_impl {
}

emit( self.accepted_transaction, trx );
emit( self.applied_transaction, trace );
emit( self.applied_transaction, std::tie(trace, trn) );

return trace;
} FC_CAPTURE_AND_RETHROW((trace))
Expand Down
2 changes: 1 addition & 1 deletion libraries/chain/include/eosio/chain/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ namespace eosio { namespace chain {
signal<void(const block_state_ptr&)> accepted_block;
signal<void(const block_state_ptr&)> irreversible_block;
signal<void(const transaction_metadata_ptr&)> accepted_transaction;
signal<void(const transaction_trace_ptr&)> applied_transaction;
signal<void(std::tuple<const transaction_trace_ptr&, const signed_transaction&>)> applied_transaction;
signal<void(const int&)> bad_alloc;

/*
Expand Down
4 changes: 2 additions & 2 deletions plugins/chain_plugin/chain_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -943,8 +943,8 @@ void chain_plugin::plugin_initialize(const variables_map& options) {
} );

my->applied_transaction_connection = my->chain->applied_transaction.connect(
[this]( const transaction_trace_ptr& trace ) {
my->applied_transaction_channel.publish( priority::low, trace );
[this]( std::tuple<const transaction_trace_ptr&, const signed_transaction&> t ) {
my->applied_transaction_channel.publish( priority::low, std::get<0>(t) );
} );

my->chain->add_indices();
Expand Down
4 changes: 2 additions & 2 deletions plugins/history_plugin/history_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,8 @@ namespace eosio {
db.add_index<public_key_history_multi_index>();

my->applied_transaction_connection.emplace(
chain.applied_transaction.connect( [&]( const transaction_trace_ptr& p ) {
my->on_applied_transaction( p );
chain.applied_transaction.connect( [&]( std::tuple<const transaction_trace_ptr&, const signed_transaction&> t ) {
my->on_applied_transaction( std::get<0>(t) );
} ));
} FC_LOG_AND_RETHROW()
}
Expand Down
4 changes: 2 additions & 2 deletions plugins/mongo_db_plugin/mongo_db_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1695,8 +1695,8 @@ void mongo_db_plugin::plugin_initialize(const variables_map& options)
my->accepted_transaction( t );
} ));
my->applied_transaction_connection.emplace(
chain.applied_transaction.connect( [&]( const chain::transaction_trace_ptr& t ) {
my->applied_transaction( t );
chain.applied_transaction.connect( [&]( std::tuple<const chain::transaction_trace_ptr&, const chain::signed_transaction&> t ) {
my->applied_transaction( std::get<0>(t) );
} ));

if( my->wipe_database_on_startup ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,53 @@ using std::shared_ptr;

typedef shared_ptr<struct state_history_plugin_impl> state_history_ptr;

struct partial_transaction {
chain::time_point_sec expiration = {};
uint16_t ref_block_num = {};
uint32_t ref_block_prefix = {};
fc::unsigned_int max_net_usage_words = {};
uint8_t max_cpu_usage_ms = {};
fc::unsigned_int delay_sec = {};
chain::extensions_type transaction_extensions = {};
vector<chain::signature_type> signatures = {};
vector<bytes> context_free_data = {};

partial_transaction(const chain::signed_transaction& t)
: expiration(t.expiration)
, ref_block_num(t.ref_block_num)
, ref_block_prefix(t.ref_block_prefix)
, max_net_usage_words(t.max_net_usage_words)
, max_cpu_usage_ms(t.max_cpu_usage_ms)
, delay_sec(t.delay_sec)
, transaction_extensions(t.transaction_extensions)
, signatures(t.signatures)
, context_free_data(t.context_free_data) {}
};

struct augmented_transaction_trace {
chain::transaction_trace_ptr trace;
std::shared_ptr<partial_transaction> partial;

augmented_transaction_trace() = default;
augmented_transaction_trace(const augmented_transaction_trace&) = default;
augmented_transaction_trace(augmented_transaction_trace&&) = default;

augmented_transaction_trace(const chain::transaction_trace_ptr& trace)
: trace{trace} {}

augmented_transaction_trace(const chain::transaction_trace_ptr& trace,
const std::shared_ptr<partial_transaction>& partial)
: trace{trace}
, partial{partial} {}

augmented_transaction_trace(const chain::transaction_trace_ptr& trace, const chain::signed_transaction& t)
: trace{trace}
, partial{std::make_shared<partial_transaction>(t)} {}

augmented_transaction_trace& operator=(const augmented_transaction_trace&) = default;
augmented_transaction_trace& operator=(augmented_transaction_trace&&) = default;
};

struct table_delta {
fc::unsigned_int struct_version = 0;
std::string name{};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,53 +524,71 @@ datastream<ST>& operator<<(datastream<ST>& ds, const history_serial_wrapper<eosi
}

template <typename ST>
datastream<ST>& operator<<(datastream<ST>& ds,
const history_context_wrapper<uint8_t, eosio::chain::transaction_trace>& obj) {
datastream<ST>& operator<<(datastream<ST>& ds,
const history_context_wrapper<uint8_t, eosio::augmented_transaction_trace>& obj) {
auto& trace = *obj.obj.trace;
fc::raw::pack(ds, fc::unsigned_int(0));
fc::raw::pack(ds, as_type<eosio::chain::transaction_id_type>(obj.obj.id));
if (obj.obj.receipt) {
if (obj.obj.failed_dtrx_trace &&
obj.obj.receipt->status.value == eosio::chain::transaction_receipt_header::soft_fail)
fc::raw::pack(ds, as_type<eosio::chain::transaction_id_type>(trace.id));
if (trace.receipt) {
if (trace.failed_dtrx_trace && trace.receipt->status.value == eosio::chain::transaction_receipt_header::soft_fail)
fc::raw::pack(ds, uint8_t(eosio::chain::transaction_receipt_header::executed));
else
fc::raw::pack(ds, as_type<uint8_t>(obj.obj.receipt->status.value));
fc::raw::pack(ds, as_type<uint32_t>(obj.obj.receipt->cpu_usage_us));
fc::raw::pack(ds, as_type<fc::unsigned_int>(obj.obj.receipt->net_usage_words));
fc::raw::pack(ds, as_type<uint8_t>(trace.receipt->status.value));
fc::raw::pack(ds, as_type<uint32_t>(trace.receipt->cpu_usage_us));
fc::raw::pack(ds, as_type<fc::unsigned_int>(trace.receipt->net_usage_words));
} else {
fc::raw::pack(ds, uint8_t(obj.context));
fc::raw::pack(ds, uint32_t(0));
fc::raw::pack(ds, fc::unsigned_int(0));
}
fc::raw::pack(ds, as_type<int64_t>(obj.obj.elapsed.count()));
fc::raw::pack(ds, as_type<uint64_t>(obj.obj.net_usage));
fc::raw::pack(ds, as_type<bool>(obj.obj.scheduled));
history_serialize_container(ds, obj.db, as_type<std::vector<eosio::chain::action_trace>>(obj.obj.action_traces));
fc::raw::pack(ds, as_type<int64_t>(trace.elapsed.count()));
fc::raw::pack(ds, as_type<uint64_t>(trace.net_usage));
fc::raw::pack(ds, as_type<bool>(trace.scheduled));
history_serialize_container(ds, obj.db, as_type<std::vector<eosio::chain::action_trace>>(trace.action_traces));

fc::raw::pack(ds, bool(obj.obj.account_ram_delta));
if (obj.obj.account_ram_delta) {
fc::raw::pack(ds, bool(trace.account_ram_delta));
if (trace.account_ram_delta) {
fc::raw::pack(
ds, make_history_serial_wrapper(obj.db, as_type<eosio::chain::account_delta>(*obj.obj.account_ram_delta)));
ds, make_history_serial_wrapper(obj.db, as_type<eosio::chain::account_delta>(*trace.account_ram_delta)));
}

fc::optional<std::string> e;
if (obj.obj.except)
e = obj.obj.except->to_string();
if (trace.except)
e = trace.except->to_string();
fc::raw::pack(ds, as_type<fc::optional<std::string>>(e));
fc::raw::pack(ds, as_type<fc::optional<uint64_t>>(obj.obj.error_code));
fc::raw::pack(ds, as_type<fc::optional<uint64_t>>(trace.error_code));

fc::raw::pack(ds, bool(obj.obj.failed_dtrx_trace));
if (obj.obj.failed_dtrx_trace) {
fc::raw::pack(ds, bool(trace.failed_dtrx_trace));
if (trace.failed_dtrx_trace) {
uint8_t stat = eosio::chain::transaction_receipt_header::hard_fail;
if (obj.obj.receipt && obj.obj.receipt->status.value == eosio::chain::transaction_receipt_header::soft_fail)
if (trace.receipt && trace.receipt->status.value == eosio::chain::transaction_receipt_header::soft_fail)
stat = eosio::chain::transaction_receipt_header::soft_fail;
fc::raw::pack(ds, make_history_context_wrapper(obj.db, stat, *obj.obj.failed_dtrx_trace));
fc::raw::pack( //
ds, make_history_context_wrapper(
obj.db, stat, eosio::augmented_transaction_trace{trace.failed_dtrx_trace, obj.obj.partial}));
}

bool include_partial = obj.obj.partial && !trace.failed_dtrx_trace;
fc::raw::pack(ds, include_partial);
if (include_partial) {
auto& partial = *obj.obj.partial;
fc::raw::pack(ds, fc::unsigned_int(0));
fc::raw::pack(ds, as_type<eosio::chain::time_point_sec>(partial.expiration));
fc::raw::pack(ds, as_type<uint16_t>(partial.ref_block_num));
fc::raw::pack(ds, as_type<uint32_t>(partial.ref_block_prefix));
fc::raw::pack(ds, as_type<fc::unsigned_int>(partial.max_net_usage_words));
fc::raw::pack(ds, as_type<uint8_t>(partial.max_cpu_usage_ms));
fc::raw::pack(ds, as_type<fc::unsigned_int>(partial.delay_sec));
fc::raw::pack(ds, as_type<eosio::chain::extensions_type>(partial.transaction_extensions));
fc::raw::pack(ds, as_type<std::vector<eosio::chain::signature_type>>(partial.signatures));
fc::raw::pack(ds, as_type<std::vector<eosio::bytes>>(partial.context_free_data));
}

return ds;
}

template <typename ST>
datastream<ST>& operator<<(datastream<ST>& ds, const history_serial_wrapper<eosio::chain::transaction_trace>& obj) {
datastream<ST>& operator<<(datastream<ST>& ds, const history_serial_wrapper<eosio::augmented_transaction_trace>& obj) {
uint8_t stat = eosio::chain::transaction_receipt_header::hard_fail;
ds << make_history_context_wrapper(obj.db, stat, obj.obj);
return ds;
Expand Down
40 changes: 21 additions & 19 deletions plugins/state_history_plugin/state_history_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,17 @@ bool include_delta(const eosio::chain::code_object& old, const eosio::chain::cod
}

struct state_history_plugin_impl : std::enable_shared_from_this<state_history_plugin_impl> {
chain_plugin* chain_plug = nullptr;
fc::optional<state_history_log> trace_log;
fc::optional<state_history_log> chain_state_log;
bool stopping = false;
fc::optional<scoped_connection> applied_transaction_connection;
fc::optional<scoped_connection> accepted_block_connection;
string endpoint_address = "0.0.0.0";
uint16_t endpoint_port = 8080;
std::unique_ptr<tcp::acceptor> acceptor;
std::map<transaction_id_type, transaction_trace_ptr> cached_traces;
transaction_trace_ptr onblock_trace;
chain_plugin* chain_plug = nullptr;
fc::optional<state_history_log> trace_log;
fc::optional<state_history_log> chain_state_log;
bool stopping = false;
fc::optional<scoped_connection> applied_transaction_connection;
fc::optional<scoped_connection> accepted_block_connection;
string endpoint_address = "0.0.0.0";
uint16_t endpoint_port = 8080;
std::unique_ptr<tcp::acceptor> acceptor;
std::map<transaction_id_type, augmented_transaction_trace> cached_traces;
fc::optional<augmented_transaction_trace> onblock_trace;

void get_log_entry(state_history_log& log, uint32_t block_num, fc::optional<bytes>& result) {
if (block_num < log.begin_block() || block_num >= log.end_block())
Expand Down Expand Up @@ -392,14 +392,14 @@ struct state_history_plugin_impl : std::enable_shared_from_this<state_history_pl
auth.permission == eosio::chain::config::active_name;
}

void on_applied_transaction(const transaction_trace_ptr& p) {
void on_applied_transaction(const transaction_trace_ptr& p, const signed_transaction& t) {
if (p->receipt && trace_log) {
if (is_onblock(p))
onblock_trace = p;
onblock_trace.emplace(p, t);
else if (p->failed_dtrx_trace)
cached_traces[p->failed_dtrx_trace->id] = p;
cached_traces[p->failed_dtrx_trace->id] = augmented_transaction_trace{p, t};
else
cached_traces[p->id] = p;
cached_traces[p->id] = augmented_transaction_trace{p, t};
}
}

Expand All @@ -419,17 +419,17 @@ struct state_history_plugin_impl : std::enable_shared_from_this<state_history_pl
void store_traces(const block_state_ptr& block_state) {
if (!trace_log)
return;
std::vector<transaction_trace_ptr> traces;
std::vector<augmented_transaction_trace> traces;
if (onblock_trace)
traces.push_back(onblock_trace);
traces.push_back(*onblock_trace);
for (auto& r : block_state->block->transactions) {
transaction_id_type id;
if (r.trx.contains<transaction_id_type>())
id = r.trx.get<transaction_id_type>();
else
id = r.trx.get<packed_transaction>().id();
auto it = cached_traces.find(id);
EOS_ASSERT(it != cached_traces.end() && it->second->receipt, plugin_exception,
EOS_ASSERT(it != cached_traces.end() && it->second.trace->receipt, plugin_exception,
"missing trace for transaction ${id}", ("id", id));
traces.push_back(it->second);
}
Expand Down Expand Up @@ -576,7 +576,9 @@ void state_history_plugin::plugin_initialize(const variables_map& options) {
EOS_ASSERT(my->chain_plug, chain::missing_chain_plugin_exception, "");
auto& chain = my->chain_plug->chain();
my->applied_transaction_connection.emplace(
chain.applied_transaction.connect([&](const transaction_trace_ptr& p) { my->on_applied_transaction(p); }));
chain.applied_transaction.connect([&](std::tuple<const transaction_trace_ptr&, const signed_transaction&> t) {
my->on_applied_transaction(std::get<0>(t), std::get<1>(t));
}));
my->accepted_block_connection.emplace(
chain.accepted_block.connect([&](const block_state_ptr& p) { my->on_accepted_block(p); }));

Expand Down
17 changes: 16 additions & 1 deletion plugins/state_history_plugin/state_history_plugin_abi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,19 @@ extern const char* const state_history_plugin_abi = R"({
{ "name": "error_code", "type": "uint64?" }
]
},
{
"name": "partial_transaction_v0", "fields": [
{ "name": "expiration", "type": "time_point_sec" },
{ "name": "ref_block_num", "type": "uint16" },
{ "name": "ref_block_prefix", "type": "uint32" },
{ "name": "max_net_usage_words", "type": "varuint32" },
{ "name": "max_cpu_usage_ms", "type": "uint8" },
{ "name": "delay_sec", "type": "varuint32" },
{ "name": "transaction_extensions", "type": "extension[]" },
{ "name": "signatures", "type": "signature[]" },
{ "name": "context_free_data", "type": "bytes[]" }
]
},
{
"name": "transaction_trace_v0", "fields": [
{ "name": "id", "type": "checksum256" },
Expand All @@ -119,7 +132,8 @@ extern const char* const state_history_plugin_abi = R"({
{ "name": "account_ram_delta", "type": "account_delta?" },
{ "name": "except", "type": "string?" },
{ "name": "error_code", "type": "uint64?" },
{ "name": "failed_dtrx_trace", "type": "transaction_trace?" }
{ "name": "failed_dtrx_trace", "type": "transaction_trace?" },
{ "name": "partial", "type": "partial_transaction?" }
]
},
{
Expand Down Expand Up @@ -451,6 +465,7 @@ extern const char* const state_history_plugin_abi = R"({
{ "name": "action_receipt", "types": ["action_receipt_v0"] },
{ "name": "action_trace", "types": ["action_trace_v0"] },
{ "name": "partial_transaction", "types": ["partial_transaction_v0"] },
{ "name": "transaction_trace", "types": ["transaction_trace_v0"] },
{ "name": "transaction_variant", "types": ["transaction_id", "packed_transaction"] },
Expand Down
Loading

0 comments on commit d891a76

Please sign in to comment.