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

Commit

Permalink
store uint64_t error_code in eosio_assert_code_exception rather than …
Browse files Browse the repository at this point in the history
…extracting it from the string message #6898
  • Loading branch information
arhag committed Apr 11, 2019
1 parent c3817b3 commit 7eebf69
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 45 deletions.
7 changes: 1 addition & 6 deletions libraries/chain/apply_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,9 @@ void apply_context::exec_one()
} catch( const wasm_exit& ) {}
}
} FC_RETHROW_EXCEPTIONS( warn, "pending console output: ${console}", ("console", _pending_console_output) )
} 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.error_code = controller::convert_exception_to_error_code( e );
trace.except = e;
finalize_trace( trace, start );
throw;
Expand Down
43 changes: 8 additions & 35 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -981,13 +981,9 @@ 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->error_code = controller::convert_exception_to_error_code( e );
trace->except = e;
trace->except_ptr = std::current_exception();
}
Expand Down Expand Up @@ -1128,13 +1124,9 @@ 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->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;
Expand Down Expand Up @@ -1324,11 +1316,8 @@ struct controller_impl {
unapplied_transactions.erase( trx->signed_id );
}
return trace;
} 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->error_code = controller::convert_exception_to_error_code( e );
trace->except = e;
trace->except_ptr = std::current_exception();
}
Expand Down Expand Up @@ -2997,28 +2986,12 @@ 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 {};
}
fc::optional<uint64_t> controller::convert_exception_to_error_code( const fc::exception& e ) {
const eosio_assert_code_exception* e_ptr = dynamic_cast<const eosio_assert_code_exception*>( &e );

if( e_ptr == nullptr ) return {};

return error_code;
return e_ptr->error_code;
}

/// Protocol feature activation handlers:
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 @@ -275,7 +275,7 @@ 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 );
static fc::optional<uint64_t> convert_exception_to_error_code( const fc::exception& e );

signal<void(const signed_block_ptr&)> pre_accepted_block;
signal<void(const block_state_ptr&)> accepted_block_header;
Expand Down
38 changes: 37 additions & 1 deletion libraries/chain/include/eosio/chain/exceptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,42 @@
{ throw( effect_type( e.what(), e.get_log() ) ); }


#define FC_DECLARE_DERIVED_EXCEPTION_WITH_ERROR_CODE( TYPE, BASE, CODE, WHAT ) \
class TYPE : public BASE \
{ \
public: \
enum code_enum { \
code_value = CODE, \
}; \
explicit TYPE( int64_t code, const std::string& name_value, const std::string& what_value ) \
:BASE( code, name_value, what_value ){} \
explicit TYPE( fc::log_message&& m, int64_t code, const std::string& name_value, const std::string& what_value ) \
:BASE( std::move(m), code, name_value, what_value ){} \
explicit TYPE( fc::log_messages&& m, int64_t code, const std::string& name_value, const std::string& what_value )\
:BASE( std::move(m), code, name_value, what_value ){}\
explicit TYPE( const fc::log_messages& m, int64_t code, const std::string& name_value, const std::string& what_value )\
:BASE( m, code, name_value, what_value ){}\
TYPE( const std::string& what_value, const fc::log_messages& m ) \
:BASE( m, CODE, BOOST_PP_STRINGIZE(TYPE), what_value ){} \
TYPE( fc::log_message&& m ) \
:BASE( fc::move(m), CODE, BOOST_PP_STRINGIZE(TYPE), WHAT ){}\
TYPE( fc::log_messages msgs ) \
:BASE( fc::move( msgs ), CODE, BOOST_PP_STRINGIZE(TYPE), WHAT ) {} \
TYPE( const TYPE& c ) \
:BASE(c),error_code(c.error_code) {} \
TYPE( const BASE& c ) \
:BASE(c){} \
TYPE():BASE(CODE, BOOST_PP_STRINGIZE(TYPE), WHAT){}\
\
virtual std::shared_ptr<fc::exception> dynamic_copy_exception()const\
{ return std::make_shared<TYPE>( *this ); } \
virtual NO_RETURN void dynamic_rethrow_exception()const \
{ if( code() == CODE ) throw *this;\
else fc::exception::dynamic_rethrow_exception(); \
} \
fc::optional<uint64_t> error_code; \
};

namespace eosio { namespace chain {

FC_DECLARE_EXCEPTION( chain_exception,
Expand Down Expand Up @@ -207,7 +243,7 @@ namespace eosio { namespace chain {
3050002, "Invalid Action Arguments" )
FC_DECLARE_DERIVED_EXCEPTION( eosio_assert_message_exception, action_validate_exception,
3050003, "eosio_assert_message assertion failure" )
FC_DECLARE_DERIVED_EXCEPTION( eosio_assert_code_exception, action_validate_exception,
FC_DECLARE_DERIVED_EXCEPTION_WITH_ERROR_CODE( eosio_assert_code_exception, action_validate_exception,
3050004, "eosio_assert_code assertion failure" )
FC_DECLARE_DERIVED_EXCEPTION( action_not_found_exception, action_validate_exception,
3050005, "Action can not be found" )
Expand Down
8 changes: 6 additions & 2 deletions libraries/chain/wasm_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -960,8 +960,12 @@ class context_free_system_api : public context_aware_api {

void eosio_assert_code( bool condition, uint64_t error_code ) {
if( BOOST_UNLIKELY( !condition ) ) {
EOS_THROW( eosio_assert_code_exception,
"assertion failure with error code: ${error_code}", ("error_code", error_code) );
eosio_assert_code_exception e( FC_LOG_MESSAGE( error,
"assertion failure with error code: ${error_code}",
("error_code", error_code)
) );
e.error_code = error_code;
throw e;
}
}

Expand Down

0 comments on commit 7eebf69

Please sign in to comment.