From 4ac9898f76564965512d13354d1edb14c6ea4966 Mon Sep 17 00:00:00 2001 From: Anton Perkov Date: Wed, 23 May 2018 16:05:32 -0400 Subject: [PATCH] chain_plugin parameter --contracts-console added #3339 --- libraries/chain/apply_context.cpp | 26 ++--- libraries/chain/controller.cpp | 4 + .../chain/include/eosio/chain/controller.hpp | 3 + libraries/chain/wasm_interface.cpp | 104 +++++++++++------- libraries/testing/tester.cpp | 1 + plugins/chain_plugin/chain_plugin.cpp | 5 +- 6 files changed, 90 insertions(+), 53 deletions(-) diff --git a/libraries/chain/apply_context.cpp b/libraries/chain/apply_context.cpp index e93dca548..79dc9b0a0 100644 --- a/libraries/chain/apply_context.cpp +++ b/libraries/chain/apply_context.cpp @@ -16,18 +16,16 @@ using boost::container::flat_set; namespace eosio { namespace chain { static inline void print_debug(account_name receiver, const action_trace& ar) { - if(fc::logger::get(DEFAULT_LOGGER).is_enabled(fc::log_level::debug)) { - if (!ar.console.empty()) { - auto prefix = fc::format_string( - "\n[(${a},${n})->${r}]", - fc::mutable_variant_object() - ("a", ar.act.account) - ("n", ar.act.name) - ("r", receiver)); - dlog(prefix + ": CONSOLE OUTPUT BEGIN =====================\n" - + ar.console - + prefix + ": CONSOLE OUTPUT END =====================" ); - } + if (!ar.console.empty()) { + auto prefix = fc::format_string( + "\n[(${a},${n})->${r}]", + fc::mutable_variant_object() + ("a", ar.act.account) + ("n", ar.act.name) + ("r", receiver)); + dlog(prefix + ": CONSOLE OUTPUT BEGIN =====================\n" + + ar.console + + prefix + ": CONSOLE OUTPUT END =====================" ); } } @@ -75,7 +73,9 @@ action_trace apply_context::exec_one() trx_context.executed.emplace_back( move(r) ); - print_debug(receiver, t); + if ( control.contracts_console() ) { + print_debug(receiver, t); + } reset_console(); diff --git a/libraries/chain/controller.cpp b/libraries/chain/controller.cpp index ce58e218c..bc3808c14 100644 --- a/libraries/chain/controller.cpp +++ b/libraries/chain/controller.cpp @@ -1283,6 +1283,10 @@ bool controller::skip_auth_check()const { return my->replaying_irreversible && !my->conf.force_all_checks; } +bool controller::contracts_console()const { + return my->conf.contracts_console; +} + const apply_handler* controller::find_apply_handler( account_name receiver, account_name scope, action_name act ) const { auto native_handler_scope = my->apply_handlers.find( receiver ); diff --git a/libraries/chain/include/eosio/chain/controller.hpp b/libraries/chain/include/eosio/chain/controller.hpp index 9edf87299..25b8a0a72 100644 --- a/libraries/chain/include/eosio/chain/controller.hpp +++ b/libraries/chain/include/eosio/chain/controller.hpp @@ -42,6 +42,7 @@ namespace eosio { namespace chain { uint64_t shared_memory_size = chain::config::default_shared_memory_size; bool read_only = false; bool force_all_checks = false; + bool contracts_console = false; genesis_state genesis; wasm_interface::vm_type wasm_runtime = chain::config::default_wasm_runtime; @@ -151,6 +152,7 @@ namespace eosio { namespace chain { bool skip_auth_check()const; + bool contracts_console()const; signal accepted_block_header; signal accepted_block; @@ -206,6 +208,7 @@ FC_REFLECT( eosio::chain::controller::config, (reversible_cache_size) (shared_memory_dir)(shared_memory_size)(read_only) (force_all_checks) + (contracts_console) (genesis) (wasm_runtime) ) diff --git a/libraries/chain/wasm_interface.cpp b/libraries/chain/wasm_interface.cpp index d5710d96d..24736cc9f 100644 --- a/libraries/chain/wasm_interface.cpp +++ b/libraries/chain/wasm_interface.cpp @@ -977,68 +977,85 @@ class action_api : public context_aware_api { class console_api : public context_aware_api { public: console_api( apply_context& ctx ) - :context_aware_api(ctx,true){} + : context_aware_api(ctx,true) + , ignore(!ctx.control.contracts_console()) {} // Kept as intrinsic rather than implementing on WASM side (using prints_l and strlen) because strlen is faster on native side. void prints(null_terminated_ptr str) { - context.console_append(str); + if ( !ignore ) { + context.console_append(str); + } } void prints_l(array_ptr str, size_t str_len ) { - context.console_append(string(str, str_len)); + if ( !ignore ) { + context.console_append(string(str, str_len)); + } } void printi(int64_t val) { - context.console_append(val); + if ( !ignore ) { + context.console_append(val); + } } void printui(uint64_t val) { - context.console_append(val); + if ( !ignore ) { + context.console_append(val); + } } void printi128(const __int128& val) { - bool is_negative = (val < 0); - unsigned __int128 val_magnitude; + if ( !ignore ) { + bool is_negative = (val < 0); + unsigned __int128 val_magnitude; - if( is_negative ) - val_magnitude = static_cast(-val); // Works even if val is at the lowest possible value of a int128_t - else - val_magnitude = static_cast(val); + if( is_negative ) + val_magnitude = static_cast(-val); // Works even if val is at the lowest possible value of a int128_t + else + val_magnitude = static_cast(val); - fc::uint128_t v(val_magnitude>>64, static_cast(val_magnitude) ); + fc::uint128_t v(val_magnitude>>64, static_cast(val_magnitude) ); - if( is_negative ) { - context.console_append("-"); - } + if( is_negative ) { + context.console_append("-"); + } - context.console_append(fc::variant(v).get_string()); + context.console_append(fc::variant(v).get_string()); + } } void printui128(const unsigned __int128& val) { - fc::uint128_t v(val>>64, static_cast(val) ); - context.console_append(fc::variant(v).get_string()); + if ( !ignore ) { + fc::uint128_t v(val>>64, static_cast(val) ); + context.console_append(fc::variant(v).get_string()); + } } void printsf( float val ) { - // Assumes float representation on native side is the same as on the WASM side - auto& console = context.get_console_stream(); - auto orig_prec = console.precision(); + if ( !ignore ) { + // Assumes float representation on native side is the same as on the WASM side + auto& console = context.get_console_stream(); + auto orig_prec = console.precision(); - console.precision( std::numeric_limits::digits10 ); - context.console_append(val); + console.precision( std::numeric_limits::digits10 ); + context.console_append(val); - console.precision( orig_prec ); + console.precision( orig_prec ); + } } void printdf( double val ) { - // Assumes double representation on native side is the same as on the WASM side - auto& console = context.get_console_stream(); - auto orig_prec = console.precision(); + if ( !ignore ) { + // Assumes double representation on native side is the same as on the WASM side + auto& console = context.get_console_stream(); + auto orig_prec = console.precision(); - console.precision( std::numeric_limits::digits10 ); - context.console_append(val); + console.precision( std::numeric_limits::digits10 ); + context.console_append(val); - console.precision( orig_prec ); + console.precision( orig_prec ); + } } void printqf( const float128_t& val ) { @@ -1054,25 +1071,34 @@ class console_api : public context_aware_api { * having to deal with long doubles at all. */ - auto& console = context.get_console_stream(); - auto orig_prec = console.precision(); + if ( !ignore ) { + auto& console = context.get_console_stream(); + auto orig_prec = console.precision(); - console.precision( std::numeric_limits::digits10 ); + console.precision( std::numeric_limits::digits10 ); - extFloat80_t val_approx; - f128M_to_extF80M(&val, &val_approx); - context.console_append( *(long double*)(&val_approx) ); + extFloat80_t val_approx; + f128M_to_extF80M(&val, &val_approx); + context.console_append( *(long double*)(&val_approx) ); - console.precision( orig_prec ); + console.precision( orig_prec ); + } } void printn(const name& value) { - context.console_append(value.to_string()); + if ( !ignore ) { + context.console_append(value.to_string()); + } } void printhex(array_ptr data, size_t data_len ) { - context.console_append(fc::to_hex(data, data_len)); + if ( !ignore ) { + context.console_append(fc::to_hex(data, data_len)); + } } + + private: + bool ignore; }; #define DB_API_METHOD_WRAPPERS_SIMPLE_SECONDARY(IDX, TYPE)\ diff --git a/libraries/testing/tester.cpp b/libraries/testing/tester.cpp index 7558efd18..74b98d569 100644 --- a/libraries/testing/tester.cpp +++ b/libraries/testing/tester.cpp @@ -41,6 +41,7 @@ namespace eosio { namespace testing { cfg.shared_memory_dir = tempdir.path() / "shared"; cfg.shared_memory_size = 1024*1024*8; cfg.reversible_cache_size = 1024*1024*8; + cfg.contracts_console = true; cfg.genesis.initial_timestamp = fc::time_point::from_iso_string("2020-01-01T00:00:00.000"); cfg.genesis.initial_key = get_public_key( config::system_account_name, "active" ); diff --git a/plugins/chain_plugin/chain_plugin.cpp b/plugins/chain_plugin/chain_plugin.cpp index 57c28775b..4c27c7707 100644 --- a/plugins/chain_plugin/chain_plugin.cpp +++ b/plugins/chain_plugin/chain_plugin.cpp @@ -139,6 +139,8 @@ void chain_plugin::set_program_options(options_description& cli, options_descrip "clear chain database, recover as many blocks as possible from the block log, and then replay those blocks") ("resync-blockchain", bpo::bool_switch()->default_value(false), "clear chain database and block log") + ("contracts-console", bpo::bool_switch()->default_value(false), + "print contract's output to console") ; } @@ -201,7 +203,8 @@ void chain_plugin::plugin_initialize(const variables_map& options) { if( my->wasm_runtime ) my->chain_config->wasm_runtime = *my->wasm_runtime; - my->chain_config->force_all_checks = options.at("force-all-checks").as(); + my->chain_config->force_all_checks = options.at("force-all-checks").as(); + my->chain_config->contracts_console = options.at("contracts-console").as(); if( options.at("resync-blockchain").as() ) { ilog("Resync requested: wiping database and blocks"); -- GitLab