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

Commit

Permalink
add error_code to transaction and action traces #6898
Browse files Browse the repository at this point in the history
  • Loading branch information
arhag committed Apr 10, 2019
1 parent 0d5f3d7 commit c3817b3
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 5 deletions.
8 changes: 7 additions & 1 deletion libraries/chain/apply_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ void apply_context::exec_one()
} catch( const wasm_exit& ) {}
}
} FC_RETHROW_EXCEPTIONS( warn, "pending console output: ${console}", ("console", _pending_console_output) )
} catch( fc::exception& e ) {
} catch( const eosio_assert_code_exception& e ) {
action_trace& trace = trx_context.get_action_trace( action_ordinal );
trace.error_code = controller::convert_exception_to_error_code( e );
trace.except = e;
finalize_trace( trace, start );
throw;
} catch( const fc::exception& e ) {
action_trace& trace = trx_context.get_action_trace( action_ordinal );
trace.except = e;
finalize_trace( trace, start );
Expand Down
40 changes: 39 additions & 1 deletion libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,11 @@ struct controller_impl {
return trace;
} catch( const protocol_feature_bad_block_exception& ) {
throw;
} catch( const eosio_assert_code_exception& e ) {
cpu_time_to_bill_us = trx_context.update_billed_cpu_time( fc::time_point::now() );
trace->error_code = controller::convert_exception_to_error_code( e );
trace->except = e;
trace->except_ptr = std::current_exception();
} catch( const fc::exception& e ) {
cpu_time_to_bill_us = trx_context.update_billed_cpu_time( fc::time_point::now() );
trace->except = e;
Expand Down Expand Up @@ -1123,6 +1128,11 @@ struct controller_impl {
return trace;
} catch( const protocol_feature_bad_block_exception& ) {
throw;
} catch( const eosio_assert_code_exception& e ) {
trace->error_code = controller::convert_exception_to_error_code( e );
trace->except = e;
trace->except_ptr = std::current_exception();
trace->elapsed = fc::time_point::now() - trx_context.start;
} catch( const fc::exception& e ) {
cpu_time_to_bill_us = trx_context.update_billed_cpu_time( fc::time_point::now() );
trace->except = e;
Expand Down Expand Up @@ -1314,7 +1324,11 @@ struct controller_impl {
unapplied_transactions.erase( trx->signed_id );
}
return trace;
} catch (const fc::exception& e) {
} catch( const eosio_assert_code_exception& e ) {
trace->error_code = controller::convert_exception_to_error_code( e );
trace->except = e;
trace->except_ptr = std::current_exception();
} catch( const fc::exception& e ) {
trace->except = e;
trace->except_ptr = std::current_exception();
}
Expand Down Expand Up @@ -2983,6 +2997,30 @@ bool controller::all_subjective_mitigations_disabled()const {
return my->conf.disable_all_subjective_mitigations;
}

fc::optional<uint64_t> controller::convert_exception_to_error_code( const eosio_assert_code_exception& e ) {
const auto& logs = e.get_log();

if( logs.size() == 0 ) return {};

const auto msg = logs[0].get_message();

auto pos = msg.find( ": " );

if( pos == std::string::npos || (pos + 2) >= msg.size() ) return {};

pos += 2;

uint64_t error_code = 0;

try {
error_code = std::strtoull( msg.c_str() + pos, nullptr, 10 );
} catch( ... ) {
return {};
}

return error_code;
}

/// Protocol feature activation handlers:

template<>
Expand Down
2 changes: 2 additions & 0 deletions libraries/chain/include/eosio/chain/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ namespace eosio { namespace chain {
void add_to_ram_correction( account_name account, uint64_t ram_bytes );
bool all_subjective_mitigations_disabled()const;

static fc::optional<uint64_t> convert_exception_to_error_code( const eosio_assert_code_exception& e );

signal<void(const signed_block_ptr&)> pre_accepted_block;
signal<void(const block_state_ptr&)> accepted_block_header;
signal<void(const block_state_ptr&)> accepted_block;
Expand Down
6 changes: 4 additions & 2 deletions libraries/chain/include/eosio/chain/trace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ namespace eosio { namespace chain {
fc::optional<block_id_type> producer_block_id;
flat_set<account_delta> account_ram_deltas;
fc::optional<fc::exception> except;
fc::optional<uint64_t> error_code;
};

struct transaction_trace {
Expand All @@ -63,6 +64,7 @@ namespace eosio { namespace chain {

transaction_trace_ptr failed_dtrx_trace;
fc::optional<fc::exception> except;
fc::optional<uint64_t> error_code;
std::exception_ptr except_ptr;
};

Expand All @@ -74,8 +76,8 @@ FC_REFLECT( eosio::chain::account_delta,
FC_REFLECT( eosio::chain::action_trace,
(action_ordinal)(creator_action_ordinal)(closest_unnotified_ancestor_action_ordinal)(receipt)
(receiver)(act)(context_free)(elapsed)(console)(trx_id)(block_num)(block_time)
(producer_block_id)(account_ram_deltas)(except) )
(producer_block_id)(account_ram_deltas)(except)(error_code) )

FC_REFLECT( eosio::chain::transaction_trace, (id)(block_num)(block_time)(producer_block_id)
(receipt)(elapsed)(net_usage)(scheduled)
(action_traces)(account_ram_delta)(failed_dtrx_trace)(except) )
(action_traces)(account_ram_delta)(failed_dtrx_trace)(except)(error_code) )
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ datastream<ST>& operator<<(datastream<ST>& ds, const history_serial_wrapper<eosi
if (obj.obj.except)
e = obj.obj.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));

return ds;
}
Expand Down Expand Up @@ -533,6 +534,7 @@ datastream<ST>& operator<<(datastream<ST>&
if (obj.obj.except)
e = obj.obj.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, bool(obj.obj.failed_dtrx_trace));
if (obj.obj.failed_dtrx_trace) {
Expand Down
4 changes: 3 additions & 1 deletion plugins/state_history_plugin/state_history_plugin_abi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ extern const char* const state_history_plugin_abi = R"({
{ "name": "elapsed", "type": "int64" },
{ "name": "console", "type": "string" },
{ "name": "account_ram_deltas", "type": "account_delta[]" },
{ "name": "except", "type": "string?" }
{ "name": "except", "type": "string?" },
{ "name": "error_code", "type": "uint64?" }
]
},
{
Expand All @@ -117,6 +118,7 @@ extern const char* const state_history_plugin_abi = R"({
{ "name": "action_traces", "type": "action_trace[]" },
{ "name": "account_ram_delta", "type": "account_delta?" },
{ "name": "except", "type": "string?" },
{ "name": "error_code", "type": "uint64?" },
{ "name": "failed_dtrx_trace", "type": "transaction_trace?" }
]
},
Expand Down
11 changes: 11 additions & 0 deletions unittests/api_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2046,6 +2046,17 @@ BOOST_FIXTURE_TEST_CASE(eosio_assert_code_tests, TESTER) { try {
BOOST_CHECK_EXCEPTION( CALL_TEST_FUNCTION( *this, "test_action", "test_assert_code", fc::raw::pack((uint64_t)42) ),
eosio_assert_code_exception, eosio_assert_code_is(42) );


auto trace = CALL_TEST_FUNCTION_NO_THROW( *this, "test_action", "test_assert_code", fc::raw::pack((uint64_t)42) );
BOOST_REQUIRE( trace );
BOOST_REQUIRE( trace->except );
BOOST_REQUIRE( trace->error_code );
BOOST_REQUIRE_EQUAL( *trace->error_code, 42 );
BOOST_REQUIRE_EQUAL( trace->action_traces.size(), 1 );
BOOST_REQUIRE( trace->action_traces[0].except );
BOOST_REQUIRE( trace->action_traces[0].error_code );
BOOST_REQUIRE_EQUAL( *trace->action_traces[0].error_code, 42 );

produce_block();

auto omsg1 = abis.get_error_message(1);
Expand Down

0 comments on commit c3817b3

Please sign in to comment.