From 9da846cbcc9d8e43018d192e55374181a18e0871 Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Sun, 3 Nov 2019 11:41:26 +0100 Subject: [PATCH 01/31] Disable p2p network when delayed_node plugin is enabled --- libraries/app/api.cpp | 14 +++++++++++--- libraries/app/application.cpp | 3 ++- libraries/app/include/graphene/app/plugin.hpp | 2 +- .../witness/include/graphene/witness/witness.hpp | 3 ++- libraries/plugins/witness/witness.cpp | 8 +++++++- 5 files changed, 23 insertions(+), 7 deletions(-) diff --git a/libraries/app/api.cpp b/libraries/app/api.cpp index 99564106a2..d10ed283df 100644 --- a/libraries/app/api.cpp +++ b/libraries/app/api.cpp @@ -218,6 +218,7 @@ namespace graphene { namespace app { fc::variant_object network_node_api::get_info() const { + FC_ASSERT( _app.p2p_node() != nullptr, "No P2P network!" ); fc::mutable_variant_object result = _app.p2p_node()->network_get_info(); result["connection_count"] = _app.p2p_node()->get_connection_count(); return result; @@ -225,26 +226,33 @@ namespace graphene { namespace app { void network_node_api::add_node(const fc::ip::endpoint& ep) { - _app.p2p_node()->add_node(ep); + if( _app.p2p_node() != nullptr ) + _app.p2p_node()->add_node(ep); } std::vector network_node_api::get_connected_peers() const { - return _app.p2p_node()->get_connected_peers(); + if( _app.p2p_node() != nullptr ) + return _app.p2p_node()->get_connected_peers(); + return {}; } std::vector network_node_api::get_potential_peers() const { - return _app.p2p_node()->get_potential_peers(); + if( _app.p2p_node() != nullptr ) + return _app.p2p_node()->get_potential_peers(); + return {}; } fc::variant_object network_node_api::get_advanced_node_parameters() const { + FC_ASSERT( _app.p2p_node() != nullptr, "No P2P network!" ); return _app.p2p_node()->get_advanced_node_parameters(); } void network_node_api::set_advanced_node_parameters(const fc::variant_object& params) { + FC_ASSERT( _app.p2p_node() != nullptr, "No P2P network!" ); return _app.p2p_node()->set_advanced_node_parameters(params); } diff --git a/libraries/app/application.cpp b/libraries/app/application.cpp index de5bf8a334..42b8778e20 100644 --- a/libraries/app/application.cpp +++ b/libraries/app/application.cpp @@ -537,7 +537,8 @@ void application_impl::startup() _apiaccess.permission_map["*"] = wild_access; } - reset_p2p_node(_data_dir); + if( _active_plugins.find( "delayed_node" ) == _active_plugins.end() ) + reset_p2p_node(_data_dir); reset_websocket_server(); reset_websocket_tls_server(); } FC_LOG_AND_RETHROW() } diff --git a/libraries/app/include/graphene/app/plugin.hpp b/libraries/app/include/graphene/app/plugin.hpp index 45336f677c..a4286d9c75 100644 --- a/libraries/app/include/graphene/app/plugin.hpp +++ b/libraries/app/include/graphene/app/plugin.hpp @@ -114,7 +114,7 @@ class plugin : public abstract_plugin chain::database& database() { return *app().chain_database(); } application& app()const { assert(_app); return *_app; } protected: - net::node& p2p_node() { return *app().p2p_node(); } + net::node_ptr p2p_node() { return app().p2p_node(); } private: application* _app = nullptr; diff --git a/libraries/plugins/witness/include/graphene/witness/witness.hpp b/libraries/plugins/witness/include/graphene/witness/witness.hpp index 8ca09a5b27..8f7a8b61de 100644 --- a/libraries/plugins/witness/include/graphene/witness/witness.hpp +++ b/libraries/plugins/witness/include/graphene/witness/witness.hpp @@ -42,7 +42,8 @@ namespace block_production_condition low_participation = 5, lag = 6, exception_producing_block = 7, - shutdown = 8 + shutdown = 8, + no_network = 9 }; } diff --git a/libraries/plugins/witness/witness.cpp b/libraries/plugins/witness/witness.cpp index d2609625ab..26b8b232f9 100644 --- a/libraries/plugins/witness/witness.cpp +++ b/libraries/plugins/witness/witness.cpp @@ -300,6 +300,9 @@ block_production_condition::block_production_condition_enum witness_plugin::bloc case block_production_condition::shutdown: ilog( "shutdown producing block" ); return result; + case block_production_condition::no_network: + ilog( "not connected to P2P network" ); + return result; default: elog( "unknown condition ${result} while producing block", ("result", (unsigned char)result) ); break; @@ -374,6 +377,9 @@ block_production_condition::block_production_condition_enum witness_plugin::mayb return block_production_condition::lag; } + if( p2p_node() == nullptr ) + return block_production_condition::no_network; + auto block = db.generate_block( scheduled_time, scheduled_witness, @@ -381,7 +387,7 @@ block_production_condition::block_production_condition_enum witness_plugin::mayb _production_skip_flags ); capture("n", block.block_num())("t", block.timestamp)("c", now)("x", block.transactions.size()); - fc::async( [this,block](){ p2p_node().broadcast(net::block_message(block)); } ); + fc::async( [this,block](){ p2p_node()->broadcast(net::block_message(block)); } ); return block_production_condition::produced; } From 6eb5b2242fa3ea9a43f41b63c94e294e637f2d35 Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Mon, 4 Nov 2019 18:49:32 +0100 Subject: [PATCH 02/31] Fail in network_broadcast_api when not connected to P2P network --- libraries/app/api.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/libraries/app/api.cpp b/libraries/app/api.cpp index d10ed283df..e02229881f 100644 --- a/libraries/app/api.cpp +++ b/libraries/app/api.cpp @@ -180,9 +180,9 @@ namespace graphene { namespace app { void network_broadcast_api::broadcast_transaction(const precomputable_transaction& trx) { _app.chain_database()->precompute_parallel( trx ).wait(); + FC_ASSERT( _app.p2p_node() != nullptr, "Not connected to P2P network, can't broadcast!" ); _app.chain_database()->push_transaction(trx); - if( _app.p2p_node() != nullptr ) - _app.p2p_node()->broadcast_transaction(trx); + _app.p2p_node()->broadcast_transaction(trx); } fc::variant network_broadcast_api::broadcast_transaction_synchronous(const precomputable_transaction& trx) @@ -198,18 +198,18 @@ namespace graphene { namespace app { void network_broadcast_api::broadcast_block( const signed_block& b ) { _app.chain_database()->precompute_parallel( b ).wait(); + FC_ASSERT( _app.p2p_node() != nullptr, "Not connected to P2P network, can't broadcast!" ); _app.chain_database()->push_block(b); - if( _app.p2p_node() != nullptr ) - _app.p2p_node()->broadcast( net::block_message( b )); + _app.p2p_node()->broadcast( net::block_message( b )); } void network_broadcast_api::broadcast_transaction_with_callback(confirmation_callback cb, const precomputable_transaction& trx) { _app.chain_database()->precompute_parallel( trx ).wait(); + FC_ASSERT( _app.p2p_node() != nullptr, "Not connected to P2P network, can't broadcast!" ); _callbacks[trx.id()] = cb; _app.chain_database()->push_transaction(trx); - if( _app.p2p_node() != nullptr ) - _app.p2p_node()->broadcast_transaction(trx); + _app.p2p_node()->broadcast_transaction(trx); } network_node_api::network_node_api( application& a ) : _app( a ) From 359af02b2d6a73570a582a55d44d6eb2343a51b2 Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Wed, 6 Nov 2019 08:34:26 +0100 Subject: [PATCH 03/31] Refactored database_fixture for better customization --- tests/common/database_fixture.cpp | 364 ++++++++++++++---------------- tests/common/database_fixture.hpp | 67 +++++- tests/elasticsearch/main.cpp | 6 +- tests/tests/block_tests.cpp | 9 +- 4 files changed, 237 insertions(+), 209 deletions(-) diff --git a/tests/common/database_fixture.cpp b/tests/common/database_fixture.cpp index 211957ce2a..52344bad54 100644 --- a/tests/common/database_fixture.cpp +++ b/tests/common/database_fixture.cpp @@ -22,7 +22,6 @@ * THE SOFTWARE. */ #include -#include #include #include @@ -43,8 +42,6 @@ #include #include -#include - #include #include @@ -69,10 +66,13 @@ void clearable_block::clear() _block_id = block_id_type(); } -database_fixture::database_fixture(const fc::time_point_sec &initial_timestamp) - : app(), db( *app.chain_database() ) -{ - try { +database_fixture_base::database_fixture_base() + : app(), db( *app.chain_database() ), + init_account_pub_key( init_account_priv_key.get_public_key() ), + current_test_name( buf::current_test_case().p_name.value ), + current_suite_name( buf::get(buf::current_test_case().p_parent_id).p_name + .value ) +{ try { int argc = buf::master_test_suite().argc; char** argv = buf::master_test_suite().argv; for( int i=1; i(current_test_suite_id).p_name.value; - auto mhplugin = app.register_plugin(); - auto goplugin = app.register_plugin(); - init_account_pub_key = init_account_priv_key.get_public_key(); - - boost::program_options::variables_map options; - - genesis_state.initial_timestamp = initial_timestamp; +database_fixture_base::~database_fixture_base() +{ + try { + // If we're unwinding due to an exception, don't do any more checks. + // This way, boost test's last checkpoint tells us approximately where the error was. + if( !std::uncaught_exception() ) + { + verify_asset_supplies(db); + BOOST_CHECK( db.get_node_properties().skip_flags == database::skip_nothing ); + } + return; + } catch (fc::exception& ex) { + BOOST_FAIL( std::string("fc::exception in ~database_fixture: ") + ex.to_detail_string() ); + } catch (std::exception& e) { + BOOST_FAIL( std::string("std::exception in ~database_fixture:") + e.what() ); + } catch (...) { + BOOST_FAIL( "Uncaught exception in ~database_fixture" ); + } +} - if(current_test_name == "hf_1270_test") +void database_fixture_base::init_genesis( database_fixture_base& fixture ) +{ + fixture.genesis_state.initial_timestamp = fc::time_point_sec(GRAPHENE_TESTING_GENESIS_TIMESTAMP); + if( fixture.current_test_name == "hf_1270_test" ) { - genesis_state.initial_active_witnesses = 20; + fixture.genesis_state.initial_active_witnesses = 20; } else { - genesis_state.initial_active_witnesses = 10; - genesis_state.immutable_parameters.min_committee_member_count = INITIAL_COMMITTEE_MEMBER_COUNT; - genesis_state.immutable_parameters.min_witness_count = INITIAL_WITNESS_COUNT; + fixture.genesis_state.initial_active_witnesses = 10; + fixture.genesis_state.immutable_parameters.min_committee_member_count = INITIAL_COMMITTEE_MEMBER_COUNT; + fixture.genesis_state.immutable_parameters.min_witness_count = INITIAL_WITNESS_COUNT; } - for( unsigned int i = 0; i < genesis_state.initial_active_witnesses; ++i ) + for( unsigned int i = 0; i < fixture.genesis_state.initial_active_witnesses; ++i ) { auto name = "init"+fc::to_string(i); - genesis_state.initial_accounts.emplace_back(name, - init_account_priv_key.get_public_key(), - init_account_priv_key.get_public_key(), - true); - genesis_state.initial_committee_candidates.push_back({name}); - genesis_state.initial_witness_candidates.push_back({name, init_account_priv_key.get_public_key()}); + fixture.genesis_state.initial_accounts.emplace_back( name, fixture.init_account_pub_key, + fixture.init_account_pub_key, true); + fixture.genesis_state.initial_committee_candidates.push_back({name}); + fixture.genesis_state.initial_witness_candidates.push_back({ name, fixture.init_account_pub_key }); } - genesis_state.initial_parameters.get_mutable_fees().zero_all_fees(); + fixture.genesis_state.initial_parameters.get_mutable_fees().zero_all_fees(); genesis_state_type::initial_asset_type init_mpa1; init_mpa1.symbol = "INITMPA"; @@ -125,142 +136,144 @@ database_fixture::database_fixture(const fc::time_point_sec &initial_timestamp) init_mpa1.max_supply = GRAPHENE_MAX_SHARE_SUPPLY; init_mpa1.accumulated_fees = 0; init_mpa1.is_bitasset = true; - // TODO add initial UIA's; add initial short positions; test non-zero accumulated_fees - genesis_state.initial_assets.push_back( init_mpa1 ); - - open_database(); + fixture.genesis_state.initial_assets.push_back( init_mpa1 ); +} +boost::program_options::variables_map database_fixture_base::init_options( database_fixture_base& fixture ) +{ + boost::program_options::variables_map options; + set_option( options, "seed-nodes", std::string("[]") ); /** * Test specific settings */ - if (current_test_name == "get_account_history_operations") + if (fixture.current_test_name == "get_account_history_operations") { options.insert(std::make_pair("max-ops-per-account", boost::program_options::variable_value((uint64_t)75, false))); } - if (current_test_name == "api_limit_get_account_history_operations") + if (fixture.current_test_name == "api_limit_get_account_history_operations") { options.insert(std::make_pair("max-ops-per-account", boost::program_options::variable_value((uint64_t)125, false))); options.insert(std::make_pair("api-limit-get-account-history-operations", boost::program_options::variable_value((uint64_t)300, false))); } - if(current_test_name =="api_limit_get_account_history") + if(fixture.current_test_name =="api_limit_get_account_history") { options.insert(std::make_pair("max-ops-per-account", boost::program_options::variable_value((uint64_t)125, false))); options.insert(std::make_pair("api-limit-get-account-history", boost::program_options::variable_value((uint64_t)250, false))); } - if(current_test_name =="api_limit_get_grouped_limit_orders") + if(fixture.current_test_name =="api_limit_get_grouped_limit_orders") { options.insert(std::make_pair("api-limit-get-grouped-limit-orders", boost::program_options::variable_value((uint64_t)250, false))); } - if(current_test_name =="api_limit_get_relative_account_history") + if(fixture.current_test_name =="api_limit_get_relative_account_history") { options.insert(std::make_pair("max-ops-per-account", boost::program_options::variable_value((uint64_t)125, false))); options.insert(std::make_pair("api-limit-get-relative-account-history", boost::program_options::variable_value((uint64_t)250, false))); } - if(current_test_name =="api_limit_get_account_history_by_operations") + if(fixture.current_test_name =="api_limit_get_account_history_by_operations") { options.insert(std::make_pair("api-limit-get-account-history-by-operations", boost::program_options::variable_value((uint64_t)250, false))); options.insert(std::make_pair("api-limit-get-relative-account-history", boost::program_options::variable_value((uint64_t)250, false))); } - if(current_test_name =="api_limit_get_asset_holders") + if(fixture.current_test_name =="api_limit_get_asset_holders") { options.insert(std::make_pair("api-limit-get-asset-holders", boost::program_options::variable_value((uint64_t)250, false))); } - if(current_test_name =="api_limit_get_key_references") + if(fixture.current_test_name =="api_limit_get_key_references") { options.insert(std::make_pair("api-limit-get-key-references", boost::program_options::variable_value((uint64_t)200, false))); } - if(current_test_name =="api_limit_get_limit_orders") + if(fixture.current_test_name =="api_limit_get_limit_orders") { options.insert(std::make_pair("api-limit-get-limit-orders", boost::program_options::variable_value( (uint64_t)350, false))); } - if(current_test_name =="api_limit_get_call_orders") + if(fixture.current_test_name =="api_limit_get_call_orders") { options.insert(std::make_pair("api-limit-get-call-orders", boost::program_options::variable_value( (uint64_t)350, false))); } - if(current_test_name =="api_limit_get_settle_orders") + if(fixture.current_test_name =="api_limit_get_settle_orders") { options.insert(std::make_pair("api-limit-get-settle-orders", boost::program_options::variable_value( (uint64_t)350, false))); } - if(current_test_name =="api_limit_get_order_book") + if(fixture.current_test_name =="api_limit_get_order_book") { options.insert(std::make_pair("api-limit-get-order-book", boost::program_options::variable_value( (uint64_t)80, false))); } - if( current_test_name == "asset_in_collateral" ) + if( fixture.current_test_name == "asset_in_collateral" ) { options.insert( std::make_pair( "plugins", boost::program_options::variable_value( string("api_helper_indexes"), false ) ) ); } - if(current_test_name =="api_limit_lookup_accounts") + if(fixture.current_test_name =="api_limit_lookup_accounts") { options.insert(std::make_pair("api-limit-lookup-accounts", boost::program_options::variable_value ((uint64_t)200, false))); } - if(current_test_name =="api_limit_lookup_witness_accounts") + if(fixture.current_test_name =="api_limit_lookup_witness_accounts") { options.insert(std::make_pair("api-limit-lookup-witness-accounts", boost::program_options::variable_value ((uint64_t)200, false))); } - if(current_test_name =="api_limit_lookup_committee_member_accounts") + if(fixture.current_test_name =="api_limit_lookup_committee_member_accounts") { options.insert(std::make_pair("api-limit-lookup-committee-member-accounts", boost::program_options::variable_value ((uint64_t)200, false))); } - if(current_test_name =="api_limit_lookup_committee_member_accounts") + if(fixture.current_test_name =="api_limit_lookup_committee_member_accounts") { options.insert(std::make_pair("api-limit-lookup-committee-member-accounts", boost::program_options::variable_value ((uint64_t)200, false))); } - if(current_test_name =="api_limit_lookup_vote_ids") + if(fixture.current_test_name =="api_limit_lookup_vote_ids") { options.insert(std::make_pair("api-limit-lookup-vote-ids", boost::program_options::variable_value ((uint64_t)3, false))); } - if(current_test_name =="api_limit_get_account_limit_orders") + if(fixture.current_test_name =="api_limit_get_account_limit_orders") { options.insert(std::make_pair("api-limit-get-account-limit-orders", boost::program_options::variable_value ((uint64_t)250, false))); } - if(current_test_name =="api_limit_get_collateral_bids") + if(fixture.current_test_name =="api_limit_get_collateral_bids") { options.insert(std::make_pair("api-limit-get-collateral-bids", boost::program_options::variable_value ((uint64_t)250, false))); } - if(current_test_name =="api_limit_get_top_markets") + if(fixture.current_test_name =="api_limit_get_top_markets") { options.insert(std::make_pair("api-limit-get-top-markets", boost::program_options::variable_value ((uint64_t)250, false))); } - if(current_test_name =="api_limit_get_trade_history") + if(fixture.current_test_name =="api_limit_get_trade_history") { options.insert(std::make_pair("api-limit-get-trade-history", boost::program_options::variable_value ((uint64_t)250, false))); } - if(current_test_name =="api_limit_get_trade_history_by_sequence") + if(fixture.current_test_name =="api_limit_get_trade_history_by_sequence") { options.insert(std::make_pair("api-limit-get-trade-history-by-sequence", boost::program_options::variable_value ((uint64_t)250, false))); } - if(current_test_name =="api_limit_get_withdraw_permissions_by_giver") + if(fixture.current_test_name =="api_limit_get_withdraw_permissions_by_giver") { options.insert(std::make_pair("api-limit-get-withdraw-permissions-by-giver", boost::program_options::variable_value ((uint64_t)250, false))); } - if(current_test_name =="api_limit_get_withdraw_permissions_by_recipient") + if(fixture.current_test_name =="api_limit_get_withdraw_permissions_by_recipient") { options.insert(std::make_pair("api-limit-get-withdraw-permissions-by-recipient", boost::program_options::variable_value ((uint64_t)250, false))); } - if(current_test_name =="api_limit_get_full_accounts2") + if(fixture.current_test_name =="api_limit_get_full_accounts2") { options.insert(std::make_pair("api-limit-get-full-accounts", boost::program_options::variable_value ((uint64_t)200, false))); } // add account tracking for ahplugin for special test case with track-account enabled - if( !options.count("track-account") && current_test_name == "track_account") { + if( !options.count("track-account") && fixture.current_test_name == "track_account") { std::vector track_account; std::string track = "\"1.2.17\""; track_account.push_back(track); @@ -268,7 +281,7 @@ database_fixture::database_fixture(const fc::time_point_sec &initial_timestamp) options.insert(std::make_pair("partial-operations", boost::program_options::variable_value(true, false))); } // account tracking 2 accounts - if( !options.count("track-account") && current_test_name == "track_account2") { + if( !options.count("track-account") && fixture.current_test_name == "track_account2") { std::vector track_account; std::string track = "\"1.2.0\""; track_account.push_back(track); @@ -277,14 +290,14 @@ database_fixture::database_fixture(const fc::time_point_sec &initial_timestamp) options.insert(std::make_pair("track-account", boost::program_options::variable_value(track_account, false))); } // standby votes tracking - if( current_test_name == "track_votes_witnesses_disabled" - || current_test_name == "track_votes_committee_disabled") { - app.chain_database()->enable_standby_votes_tracking( false ); + if( fixture.current_test_name == "track_votes_witnesses_disabled" + || fixture.current_test_name == "track_votes_committee_disabled") { + fixture.app.chain_database()->enable_standby_votes_tracking( false ); } - if(current_test_name == "elasticsearch_account_history" || current_test_name == "elasticsearch_suite" || - current_test_name == "elasticsearch_history_api") { - auto esplugin = app.register_plugin(); - esplugin->plugin_set_app(&app); + if(fixture.current_test_name == "elasticsearch_account_history" || fixture.current_test_name == "elasticsearch_suite" || + fixture.current_test_name == "elasticsearch_history_api") { + auto esplugin = fixture.app.register_plugin(); + esplugin->plugin_set_app(&fixture.app); options.insert(std::make_pair("elasticsearch-node-url", boost::program_options::variable_value(string("http://localhost:9200/"), false))); options.insert(std::make_pair("elasticsearch-bulk-replay", boost::program_options::variable_value(uint32_t(2), false))); @@ -298,23 +311,23 @@ database_fixture::database_fixture(const fc::time_point_sec &initial_timestamp) esplugin->plugin_initialize(options); esplugin->plugin_startup(); } - else if( current_suite_name != "performance_tests" ) + else if( fixture.current_suite_name != "performance_tests" ) { - auto ahplugin = app.register_plugin(); - ahplugin->plugin_set_app(&app); + auto ahplugin = fixture.app.register_plugin(); + ahplugin->plugin_set_app(&fixture.app); ahplugin->plugin_initialize(options); ahplugin->plugin_startup(); - if(validation_current_test_name_for_setting_api_limit(current_test_name)) + if(validation_current_test_name_for_setting_api_limit(fixture.current_test_name)) { - app.initialize(graphene::utilities::temp_directory_path(), options); - app.set_api_limit(); + fixture.app.initialize(graphene::utilities::temp_directory_path(), options); + fixture.app.set_api_limit(); } } - if(current_test_name == "elasticsearch_objects" || current_test_name == "elasticsearch_suite") { - auto esobjects_plugin = app.register_plugin(); - esobjects_plugin->plugin_set_app(&app); + if(fixture.current_test_name == "elasticsearch_objects" || fixture.current_test_name == "elasticsearch_suite") { + auto esobjects_plugin = fixture.app.register_plugin(); + esobjects_plugin->plugin_set_app(&fixture.app); options.insert(std::make_pair("es-objects-elasticsearch-url", boost::program_options::variable_value(string("http://localhost:9200/"), false))); options.insert(std::make_pair("es-objects-bulk-replay", boost::program_options::variable_value(uint32_t(2), false))); @@ -329,71 +342,41 @@ database_fixture::database_fixture(const fc::time_point_sec &initial_timestamp) esobjects_plugin->plugin_initialize(options); esobjects_plugin->plugin_startup(); } - else if( current_test_name == "asset_in_collateral" - || current_test_name == "htlc_database_api" - || current_suite_name == "database_api_tests" ) + else if( fixture.current_test_name == "asset_in_collateral" + || fixture.current_test_name == "htlc_database_api" + || fixture.current_suite_name == "database_api_tests" ) { - auto ahiplugin = app.register_plugin(); - ahiplugin->plugin_set_app(&app); + auto ahiplugin = fixture.app.register_plugin(); + ahiplugin->plugin_set_app(&fixture.app); ahiplugin->plugin_initialize(options); ahiplugin->plugin_startup(); } - if(current_test_name == "custom_operations_account_storage_map_test" || - current_test_name == "custom_operations_account_storage_list_test") { - auto custom_operations_plugin = app.register_plugin(); - custom_operations_plugin->plugin_set_app(&app); + if(fixture.current_test_name == "custom_operations_account_storage_map_test" || + fixture.current_test_name == "custom_operations_account_storage_list_test") { + auto custom_operations_plugin = fixture.app.register_plugin(); + custom_operations_plugin->plugin_set_app(&fixture.app); custom_operations_plugin->plugin_initialize(options); custom_operations_plugin->plugin_startup(); } options.insert(std::make_pair("bucket-size", boost::program_options::variable_value(string("[15]"),false))); - mhplugin->plugin_set_app(&app); + + auto mhplugin = fixture.app.register_plugin(); + auto goplugin = fixture.app.register_plugin(); + mhplugin->plugin_set_app(&fixture.app); mhplugin->plugin_initialize(options); - goplugin->plugin_set_app(&app); + goplugin->plugin_set_app(&fixture.app); goplugin->plugin_initialize(options); mhplugin->plugin_startup(); goplugin->plugin_startup(); - generate_block(); - - asset_id_type mpa1_id(1); - BOOST_REQUIRE( mpa1_id(db).is_market_issued() ); - BOOST_CHECK( mpa1_id(db).bitasset_data(db).asset_id == mpa1_id ); - - set_expiration( db, trx ); - } catch ( const fc::exception& e ) - { - edump( (e.to_detail_string()) ); - throw; - } - - return; + return options; } -database_fixture::~database_fixture() -{ - try { - // If we're unwinding due to an exception, don't do any more checks. - // This way, boost test's last checkpoint tells us approximately where the error was. - if( !std::uncaught_exception() ) - { - verify_asset_supplies(db); - BOOST_CHECK( db.get_node_properties().skip_flags == database::skip_nothing ); - } - return; - } catch (fc::exception& ex) { - BOOST_FAIL( std::string("fc::exception in ~database_fixture: ") + ex.to_detail_string() ); - } catch (std::exception& e) { - BOOST_FAIL( std::string("std::exception in ~database_fixture:") + e.what() ); - } catch (...) { - BOOST_FAIL( "Uncaught exception in ~database_fixture" ); - } -} - -void database_fixture::vote_for_committee_and_witnesses(uint16_t num_committee, uint16_t num_witness) +void database_fixture_base::vote_for_committee_and_witnesses(uint16_t num_committee, uint16_t num_witness) { try { auto &init0 = get_account("init0"); @@ -433,7 +416,7 @@ void database_fixture::vote_for_committee_and_witnesses(uint16_t num_committee, } FC_CAPTURE_AND_RETHROW() } -fc::ecc::private_key database_fixture::generate_private_key(string seed) +fc::ecc::private_key database_fixture_base::generate_private_key(string seed) { static const fc::ecc::private_key committee = fc::ecc::private_key::regenerate(fc::sha256::hash(string("null_key"))); if( seed == "null_key" ) @@ -441,13 +424,13 @@ fc::ecc::private_key database_fixture::generate_private_key(string seed) return fc::ecc::private_key::regenerate(fc::sha256::hash(seed)); } -string database_fixture::generate_anon_acct_name() +string database_fixture_base::generate_anon_acct_name() { // names of the form "anon-acct-x123" ; the "x" is necessary // to workaround issue #46 return "anon-acct-x" + std::to_string( anon_acct_count++ ); } -bool database_fixture::validation_current_test_name_for_setting_api_limit( const string& current_test_name ) const +bool database_fixture_base::validation_current_test_name_for_setting_api_limit( const string& current_test_name ) { vector valid_testcase {"api_limit_get_account_history_operations","api_limit_get_account_history" ,"api_limit_get_grouped_limit_orders","api_limit_get_relative_account_history" @@ -471,7 +454,7 @@ bool database_fixture::validation_current_test_name_for_setting_api_limit( const return false; } -void database_fixture::verify_asset_supplies( const database& db ) +void database_fixture_base::verify_asset_supplies( const database& db ) { //wlog("*** Begin asset supply verification ***"); const asset_dynamic_data_object& core_asset_data = db.get_core_asset().dynamic_asset_data_id(db); @@ -554,15 +537,7 @@ void database_fixture::verify_asset_supplies( const database& db ) // wlog("*** End asset supply verification ***"); } -void database_fixture::open_database() -{ - if( !data_dir ) { - data_dir = fc::temp_directory( graphene::utilities::temp_directory_path() ); - db.open(data_dir->path(), [this]{return genesis_state;}, "test"); - } -} - -signed_block database_fixture::generate_block(uint32_t skip, const fc::ecc::private_key& key, int miss_blocks) +signed_block database_fixture_base::generate_block(uint32_t skip, const fc::ecc::private_key& key, int miss_blocks) { skip |= database::skip_undo_history_check; // skip == ~0 will skip checks specified in database::validation_steps @@ -573,13 +548,13 @@ signed_block database_fixture::generate_block(uint32_t skip, const fc::ecc::priv return block; } -void database_fixture::generate_blocks( uint32_t block_count ) +void database_fixture_base::generate_blocks( uint32_t block_count ) { for( uint32_t i = 0; i < block_count; ++i ) generate_block(); } -uint32_t database_fixture::generate_blocks(fc::time_point_sec timestamp, bool miss_intermediate_blocks, uint32_t skip) +uint32_t database_fixture_base::generate_blocks(fc::time_point_sec timestamp, bool miss_intermediate_blocks, uint32_t skip) { if( miss_intermediate_blocks ) { @@ -600,7 +575,7 @@ uint32_t database_fixture::generate_blocks(fc::time_point_sec timestamp, bool mi return blocks; } -account_create_operation database_fixture::make_account( +account_create_operation database_fixture_base::make_account( const std::string& name /* = "nathan" */, public_key_type key /* = key_id_type() */ ) @@ -631,7 +606,7 @@ account_create_operation database_fixture::make_account( return create_account; } FC_CAPTURE_AND_RETHROW() } -account_create_operation database_fixture::make_account( +account_create_operation database_fixture_base::make_account( const std::string& name, const account_object& registrar, const account_object& referrer, @@ -672,7 +647,7 @@ account_create_operation database_fixture::make_account( FC_CAPTURE_AND_RETHROW((name)(referrer_percent)) } -const asset_object& database_fixture::get_asset( const string& symbol )const +const asset_object& database_fixture_base::get_asset( const string& symbol )const { const auto& idx = db.get_index_type().indices().get(); const auto itr = idx.find(symbol); @@ -680,7 +655,7 @@ const asset_object& database_fixture::get_asset( const string& symbol )const return *itr; } -const account_object& database_fixture::get_account( const string& name )const +const account_object& database_fixture_base::get_account( const string& name )const { const auto& idx = db.get_index_type().indices().get(); const auto itr = idx.find(name); @@ -688,7 +663,7 @@ const account_object& database_fixture::get_account( const string& name )const return *itr; } -const asset_object& database_fixture::create_bitasset( +const asset_object& database_fixture_base::create_bitasset( const string& name, account_id_type issuer /* = GRAPHENE_WITNESS_ACCOUNT */, uint16_t market_fee_percent /* = 100 */ /* 1% */, @@ -719,7 +694,7 @@ const asset_object& database_fixture::create_bitasset( return db.get(ptx.operation_results[0].get()); } FC_CAPTURE_AND_RETHROW( (name)(flags) ) } -const asset_object& database_fixture::create_prediction_market( +const asset_object& database_fixture_base::create_prediction_market( const string& name, account_id_type issuer /* = GRAPHENE_WITNESS_ACCOUNT */, uint16_t market_fee_percent /* = 100 */ /* 1% */, @@ -751,7 +726,7 @@ const asset_object& database_fixture::create_prediction_market( } FC_CAPTURE_AND_RETHROW( (name)(flags) ) } -const asset_object& database_fixture::create_user_issued_asset( const string& name ) +const asset_object& database_fixture_base::create_user_issued_asset( const string& name ) { asset_create_operation creator; creator.issuer = account_id_type(); @@ -770,7 +745,7 @@ const asset_object& database_fixture::create_user_issued_asset( const string& na return db.get(ptx.operation_results[0].get()); } -const asset_object& database_fixture::create_user_issued_asset( const string& name, const account_object& issuer, +const asset_object& database_fixture_base::create_user_issued_asset( const string& name, const account_object& issuer, uint16_t flags, const price& core_exchange_rate, uint8_t precision, uint16_t market_fee_percent, additional_asset_options_t additional_options) @@ -796,7 +771,7 @@ const asset_object& database_fixture::create_user_issued_asset( const string& na return db.get(ptx.operation_results[0].get()); } -void database_fixture::issue_uia( const account_object& recipient, asset amount ) +void database_fixture_base::issue_uia( const account_object& recipient, asset amount ) { BOOST_TEST_MESSAGE( "Issuing UIA" ); asset_issue_operation op; @@ -808,12 +783,12 @@ void database_fixture::issue_uia( const account_object& recipient, asset amount trx.operations.clear(); } -void database_fixture::issue_uia( account_id_type recipient_id, asset amount ) +void database_fixture_base::issue_uia( account_id_type recipient_id, asset amount ) { issue_uia( recipient_id(db), amount ); } -void database_fixture::change_fees( +void database_fixture_base::change_fees( const fee_parameters::flat_set_type& new_params, uint32_t new_scale /* = 0 */ ) @@ -843,7 +818,7 @@ void database_fixture::change_fees( }); } -const account_object& database_fixture::create_account( +const account_object& database_fixture_base::create_account( const string& name, const public_key_type& key /* = public_key_type() */ ) @@ -856,7 +831,7 @@ const account_object& database_fixture::create_account( return result; } -const account_object& database_fixture::create_account( +const account_object& database_fixture_base::create_account( const string& name, const account_object& registrar, const account_object& referrer, @@ -877,7 +852,7 @@ const account_object& database_fixture::create_account( FC_CAPTURE_AND_RETHROW( (name)(registrar)(referrer) ) } -const account_object& database_fixture::create_account( +const account_object& database_fixture_base::create_account( const string& name, const private_key_type& key, const account_id_type& registrar_id /* = account_id_type() */, @@ -911,7 +886,7 @@ const account_object& database_fixture::create_account( FC_CAPTURE_AND_RETHROW( (name)(registrar_id)(referrer_id) ) } -const committee_member_object& database_fixture::create_committee_member( const account_object& owner ) +const committee_member_object& database_fixture_base::create_committee_member( const account_object& owner ) { committee_member_create_operation op; op.committee_member_account = owner.id; @@ -922,14 +897,14 @@ const committee_member_object& database_fixture::create_committee_member( const return db.get(ptx.operation_results[0].get()); } -const witness_object&database_fixture::create_witness(account_id_type owner, +const witness_object& database_fixture_base::create_witness(account_id_type owner, const fc::ecc::private_key& signing_private_key, uint32_t skip_flags ) { return create_witness(owner(db), signing_private_key, skip_flags ); } -const witness_object& database_fixture::create_witness( const account_object& owner, +const witness_object& database_fixture_base::create_witness( const account_object& owner, const fc::ecc::private_key& signing_private_key, uint32_t skip_flags ) { try { @@ -943,7 +918,7 @@ const witness_object& database_fixture::create_witness( const account_object& ow return db.get(ptx.operation_results[0].get()); } FC_CAPTURE_AND_RETHROW() } -const worker_object& database_fixture::create_worker( const account_id_type owner, const share_type daily_pay, const fc::microseconds& duration ) +const worker_object& database_fixture_base::create_worker( const account_id_type owner, const share_type daily_pay, const fc::microseconds& duration ) { try { worker_create_operation op; op.owner = owner; @@ -958,7 +933,7 @@ const worker_object& database_fixture::create_worker( const account_id_type owne return db.get(ptx.operation_results[0].get()); } FC_CAPTURE_AND_RETHROW() } -uint64_t database_fixture::fund( +uint64_t database_fixture_base::fund( const account_object& account, const asset& amount /* = asset(500000) */ ) @@ -967,17 +942,17 @@ uint64_t database_fixture::fund( return get_balance(account, amount.asset_id(db)); } -void database_fixture::sign(signed_transaction& trx, const fc::ecc::private_key& key) +void database_fixture_base::sign(signed_transaction& trx, const fc::ecc::private_key& key) { trx.sign( key, db.get_chain_id() ); } -digest_type database_fixture::digest( const transaction& tx ) +digest_type database_fixture_base::digest( const transaction& tx ) { return tx.digest(); } -const limit_order_object*database_fixture::create_sell_order(account_id_type user, const asset& amount, const asset& recv, +const limit_order_object* database_fixture_base::create_sell_order(account_id_type user, const asset& amount, const asset& recv, const time_point_sec order_expiration, const price& fee_core_exchange_rate ) { @@ -986,7 +961,7 @@ const limit_order_object*database_fixture::create_sell_order(account_id_type use return r; } -const limit_order_object* database_fixture::create_sell_order( const account_object& user, const asset& amount, const asset& recv, +const limit_order_object* database_fixture_base::create_sell_order( const account_object& user, const asset& amount, const asset& recv, const time_point_sec order_expiration, const price& fee_core_exchange_rate ) { @@ -1007,7 +982,7 @@ const limit_order_object* database_fixture::create_sell_order( const account_obj return db.find( processed.operation_results[0].get() ); } -asset database_fixture::cancel_limit_order( const limit_order_object& order ) +asset database_fixture_base::cancel_limit_order( const limit_order_object& order ) { limit_order_cancel_operation cancel_order; cancel_order.fee_paying_account = order.seller; @@ -1021,7 +996,7 @@ asset database_fixture::cancel_limit_order( const limit_order_object& order ) return processed.operation_results[0].get(); } -void database_fixture::transfer( +void database_fixture_base::transfer( account_id_type from, account_id_type to, const asset& amount, @@ -1031,7 +1006,7 @@ void database_fixture::transfer( transfer(from(db), to(db), amount, fee); } -void database_fixture::transfer( +void database_fixture_base::transfer( const account_object& from, const account_object& to, const asset& amount, @@ -1057,7 +1032,7 @@ void database_fixture::transfer( } FC_CAPTURE_AND_RETHROW( (from.id)(to.id)(amount)(fee) ) } -void database_fixture::update_feed_producers( const asset_object& mia, flat_set producers ) +void database_fixture_base::update_feed_producers( const asset_object& mia, flat_set producers ) { try { set_expiration( db, trx ); trx.operations.clear(); @@ -1074,7 +1049,7 @@ void database_fixture::update_feed_producers( const asset_object& mia, flat_set< verify_asset_supplies(db); } FC_CAPTURE_AND_RETHROW( (mia)(producers) ) } -void database_fixture::publish_feed( const asset_object& mia, const account_object& by, const price_feed& f ) +void database_fixture_base::publish_feed( const asset_object& mia, const account_object& by, const price_feed& f ) { set_expiration( db, trx ); trx.operations.clear(); @@ -1099,7 +1074,6 @@ void database_fixture::publish_feed( const asset_object& mia, const account_obje * * Adds a price feed for asset2, pushes the transaction, and generates the block * - * @param fixture the database_fixture * @param publisher who is publishing the feed * @param asset1 the base asset * @param amount1 the amount of the base asset @@ -1107,7 +1081,7 @@ void database_fixture::publish_feed( const asset_object& mia, const account_obje * @param amount2 the amount of the quote asset * @param core_id id of core (helps with core_exchange_rate) */ -void database_fixture::publish_feed(const account_id_type& publisher, +void database_fixture_base::publish_feed(const account_id_type& publisher, const asset_id_type& asset1, int64_t amount1, const asset_id_type& asset2, int64_t amount2, const asset_id_type& core_id) @@ -1126,7 +1100,7 @@ void database_fixture::publish_feed(const account_id_type& publisher, trx.clear(); } -void database_fixture::force_global_settle( const asset_object& what, const price& p ) +void database_fixture_base::force_global_settle( const asset_object& what, const price& p ) { try { set_expiration( db, trx ); trx.operations.clear(); @@ -1142,7 +1116,7 @@ void database_fixture::force_global_settle( const asset_object& what, const pric verify_asset_supplies(db); } FC_CAPTURE_AND_RETHROW( (what)(p) ) } -operation_result database_fixture::force_settle( const account_object& who, asset what ) +operation_result database_fixture_base::force_settle( const account_object& who, asset what ) { try { set_expiration( db, trx ); trx.operations.clear(); @@ -1159,7 +1133,7 @@ operation_result database_fixture::force_settle( const account_object& who, asse return op_result; } FC_CAPTURE_AND_RETHROW( (who)(what) ) } -const call_order_object* database_fixture::borrow( const account_object& who, asset what, asset collateral, +const call_order_object* database_fixture_base::borrow( const account_object& who, asset what, asset collateral, optional target_cr ) { try { set_expiration( db, trx ); @@ -1185,7 +1159,7 @@ const call_order_object* database_fixture::borrow( const account_object& who, as return call_obj; } FC_CAPTURE_AND_RETHROW( (who.name)(what)(collateral)(target_cr) ) } -void database_fixture::cover(const account_object& who, asset what, asset collateral, optional target_cr) +void database_fixture_base::cover(const account_object& who, asset what, asset collateral, optional target_cr) { try { set_expiration( db, trx ); trx.operations.clear(); @@ -1202,7 +1176,7 @@ void database_fixture::cover(const account_object& who, asset what, asset collat verify_asset_supplies(db); } FC_CAPTURE_AND_RETHROW( (who.name)(what)(collateral)(target_cr) ) } -void database_fixture::bid_collateral(const account_object& who, const asset& to_bid, const asset& to_cover) +void database_fixture_base::bid_collateral(const account_object& who, const asset& to_bid, const asset& to_cover) { try { set_expiration( db, trx ); trx.operations.clear(); @@ -1218,7 +1192,7 @@ void database_fixture::bid_collateral(const account_object& who, const asset& to verify_asset_supplies(db); } FC_CAPTURE_AND_RETHROW( (who.name)(to_bid)(to_cover) ) } -void database_fixture::fund_fee_pool( const account_object& from, const asset_object& asset_to_fund, const share_type amount ) +void database_fixture_base::fund_fee_pool( const account_object& from, const asset_object& asset_to_fund, const share_type amount ) { asset_fund_fee_pool_operation fund; fund.from_account = from.id; @@ -1234,7 +1208,7 @@ void database_fixture::fund_fee_pool( const account_object& from, const asset_ob verify_asset_supplies(db); } -void database_fixture::enable_fees() +void database_fixture_base::enable_fees() { db.modify(global_property_id_type()(db), [](global_property_object& gpo) { @@ -1242,12 +1216,12 @@ void database_fixture::enable_fees() }); } -void database_fixture::upgrade_to_lifetime_member(account_id_type account) +void database_fixture_base::upgrade_to_lifetime_member(account_id_type account) { upgrade_to_lifetime_member(account(db)); } -void database_fixture::upgrade_to_lifetime_member( const account_object& account ) +void database_fixture_base::upgrade_to_lifetime_member( const account_object& account ) { try { @@ -1264,12 +1238,12 @@ void database_fixture::upgrade_to_lifetime_member( const account_object& account FC_CAPTURE_AND_RETHROW((account)) } -void database_fixture::upgrade_to_annual_member(account_id_type account) +void database_fixture_base::upgrade_to_annual_member(account_id_type account) { upgrade_to_annual_member(account(db)); } -void database_fixture::upgrade_to_annual_member(const account_object& account) +void database_fixture_base::upgrade_to_annual_member(const account_object& account) { try { account_upgrade_operation op; @@ -1283,7 +1257,7 @@ void database_fixture::upgrade_to_annual_member(const account_object& account) } FC_CAPTURE_AND_RETHROW((account)) } -void database_fixture::print_market( const string& syma, const string& symb )const +void database_fixture_base::print_market( const string& syma, const string& symb )const { const auto& limit_idx = db.get_index_type(); const auto& price_idx = limit_idx.indices().get(); @@ -1311,7 +1285,7 @@ void database_fixture::print_market( const string& syma, const string& symb )con } } -string database_fixture::pretty( const asset& a )const +string database_fixture_base::pretty( const asset& a )const { std::stringstream ss; ss << a.amount.value << " "; @@ -1319,7 +1293,7 @@ string database_fixture::pretty( const asset& a )const return ss.str(); } -void database_fixture::print_limit_order( const limit_order_object& cur )const +void database_fixture_base::print_limit_order( const limit_order_object& cur )const { std::cout << std::setw(10) << cur.seller(db).name << " "; std::cout << std::setw(10) << "LIMIT" << " "; @@ -1328,7 +1302,7 @@ void database_fixture::print_limit_order( const limit_order_object& cur )const std::cout << std::setw(16) << cur.sell_price.to_real() << " "; } -void database_fixture::print_call_orders()const +void database_fixture_base::print_call_orders()const { cout << std::fixed; cout.precision(5); @@ -1356,7 +1330,7 @@ void database_fixture::print_call_orders()const std::cout << "\n"; } -void database_fixture::print_joint_market( const string& syma, const string& symb )const +void database_fixture_base::print_joint_market( const string& syma, const string& symb )const { cout << std::fixed; cout.precision(5); @@ -1380,27 +1354,27 @@ void database_fixture::print_joint_market( const string& syma, const string& sym } } -int64_t database_fixture::get_balance( account_id_type account, asset_id_type a )const +int64_t database_fixture_base::get_balance( account_id_type account, asset_id_type a )const { return db.get_balance(account, a).amount.value; } -int64_t database_fixture::get_balance( const account_object& account, const asset_object& a )const +int64_t database_fixture_base::get_balance( const account_object& account, const asset_object& a )const { return db.get_balance(account.get_id(), a.get_id()).amount.value; } -int64_t database_fixture::get_market_fee_reward( account_id_type account_id, asset_id_type asset_id)const +int64_t database_fixture_base::get_market_fee_reward( account_id_type account_id, asset_id_type asset_id)const { return db.get_market_fee_vesting_balance(account_id, asset_id).amount.value; } -int64_t database_fixture::get_market_fee_reward( const account_object& account, const asset_object& asset )const +int64_t database_fixture_base::get_market_fee_reward( const account_object& account, const asset_object& asset )const { return get_market_fee_reward(account.get_id(), asset.get_id()); } -vector< operation_history_object > database_fixture::get_operation_history( account_id_type account_id )const +vector< operation_history_object > database_fixture_base::get_operation_history( account_id_type account_id )const { vector< operation_history_object > result; const auto& stats = account_id(db).statistics(db); @@ -1418,7 +1392,7 @@ vector< operation_history_object > database_fixture::get_operation_history( acco return result; } -vector< graphene::market_history::order_history_object > database_fixture::get_market_order_history( asset_id_type a, asset_id_type b )const +vector< graphene::market_history::order_history_object > database_fixture_base::get_market_order_history( asset_id_type a, asset_id_type b )const { const auto& history_idx = db.get_index_type().indices().get(); graphene::market_history::history_key hkey; @@ -1436,7 +1410,7 @@ vector< graphene::market_history::order_history_object > database_fixture::get_m return result; } -flat_map< uint64_t, graphene::chain::fee_parameters > database_fixture::get_htlc_fee_parameters() +flat_map< uint64_t, graphene::chain::fee_parameters > database_fixture_base::get_htlc_fee_parameters() { flat_map ret_val; @@ -1458,7 +1432,7 @@ flat_map< uint64_t, graphene::chain::fee_parameters > database_fixture::get_htlc return ret_val; } -void database_fixture::set_htlc_committee_parameters() +void database_fixture_base::set_htlc_committee_parameters() { // htlc fees // get existing fee_schedule @@ -1530,7 +1504,7 @@ bool _push_block( database& db, const signed_block& b, uint32_t skip_flags /* = processed_transaction _push_transaction( database& db, const signed_transaction& tx, uint32_t skip_flags /* = 0 */ ) { try { auto pt = db.push_transaction( precomputable_transaction(tx), skip_flags ); - database_fixture::verify_asset_supplies(db); + database_fixture_base::verify_asset_supplies(db); return pt; } FC_CAPTURE_AND_RETHROW((tx)) } diff --git a/tests/common/database_fixture.hpp b/tests/common/database_fixture.hpp index a3cc5624df..4a2e848d53 100644 --- a/tests/common/database_fixture.hpp +++ b/tests/common/database_fixture.hpp @@ -25,6 +25,9 @@ #include +#include +#include + #include #include @@ -34,6 +37,7 @@ #include #include #include +#include #include @@ -182,7 +186,7 @@ class clearable_block : public signed_block { void clear(); }; -struct database_fixture { +struct database_fixture_base { // the reason we use an app is to exercise the indexes of built-in // plugins graphene::app::application app; @@ -191,23 +195,33 @@ struct database_fixture { signed_transaction trx; public_key_type committee_key; account_id_type committee_account; - fc::ecc::private_key private_key = fc::ecc::private_key::generate(); - fc::ecc::private_key init_account_priv_key = fc::ecc::private_key::regenerate(fc::sha256::hash(string("null_key")) ); - public_key_type init_account_pub_key; + const fc::ecc::private_key private_key = fc::ecc::private_key::generate(); + const fc::ecc::private_key init_account_priv_key = fc::ecc::private_key::regenerate(fc::sha256::hash(string("null_key")) ); + const public_key_type init_account_pub_key; - optional data_dir; + fc::temp_directory data_dir; bool skip_key_index_test = false; uint32_t anon_acct_count; bool hf1270 = false; - database_fixture(const fc::time_point_sec &initial_timestamp = - fc::time_point_sec(GRAPHENE_TESTING_GENESIS_TIMESTAMP)); - ~database_fixture(); + const std::string current_test_name; + const std::string current_suite_name; + + database_fixture_base(); + ~database_fixture_base(); + + template + static void set_option( boost::program_options::variables_map& options, const std::string& name, const T& value ) + { + options.insert( std::make_pair( name, boost::program_options::variable_value( value, false ) ) ); + } + + static void init_genesis( database_fixture_base& fixture ); + static boost::program_options::variables_map init_options( database_fixture_base& fixture ); static fc::ecc::private_key generate_private_key(string seed); string generate_anon_acct_name(); static void verify_asset_supplies( const database& db ); - void open_database(); void vote_for_committee_and_witnesses(uint16_t num_committee, uint16_t num_witness); signed_block generate_block(uint32_t skip = ~0, const fc::ecc::private_key& key = generate_private_key("null_key"), @@ -371,7 +385,7 @@ struct database_fixture { vector< operation_history_object > get_operation_history( account_id_type account_id )const; vector< graphene::market_history::order_history_object > get_market_order_history( asset_id_type a, asset_id_type b )const; - bool validation_current_test_name_for_setting_api_limit( const string& current_test_name )const; + static bool validation_current_test_name_for_setting_api_limit( const string& current_test_name ); /**** * @brief return htlc fee parameters @@ -400,6 +414,39 @@ void set_expiration( const database& db, transaction& tx ); bool _push_block( database& db, const signed_block& b, uint32_t skip_flags = 0 ); processed_transaction _push_transaction( database& db, const signed_transaction& tx, uint32_t skip_flags = 0 ); + } +template +struct database_fixture_init : database_fixture_base { + database_fixture_init() + { + F::init( *this ); + + asset_id_type mpa1_id(1); + BOOST_REQUIRE( mpa1_id(db).is_market_issued() ); + BOOST_CHECK( mpa1_id(db).bitasset_data(db).asset_id == mpa1_id ); + } + + static void init( database_fixture_init& fixture ) + { try { + fixture.data_dir = fc::temp_directory( graphene::utilities::temp_directory_path() ); + fc::create_directories( fixture.data_dir.path() ); + F::init_genesis( fixture ); + fc::json::save_to_file( fixture.genesis_state, fixture.data_dir.path() / "genesis.json" ); + boost::program_options::variables_map options = F::init_options( fixture ); + set_option( options, "genesis-json", boost::filesystem::path(fixture.data_dir.path() / "genesis.json") ); + fixture.app.initialize( fixture.data_dir.path(), options ); + fixture.app.startup(); + + fixture.generate_block(); + + test::set_expiration( fixture.db, fixture.trx ); + } FC_LOG_AND_RETHROW() } +}; + +struct database_fixture : database_fixture_init +{ +}; + } } diff --git a/tests/elasticsearch/main.cpp b/tests/elasticsearch/main.cpp index 24eb9382e8..bcd11cae96 100644 --- a/tests/elasticsearch/main.cpp +++ b/tests/elasticsearch/main.cpp @@ -22,6 +22,9 @@ * THE SOFTWARE. */ +#define BOOST_TEST_MODULE Elastic Search Database Tests +#include + #include #include #include @@ -31,9 +34,6 @@ #include "../common/database_fixture.hpp" -#define BOOST_TEST_MODULE Elastic Search Database Tests -#include - using namespace graphene::chain; using namespace graphene::chain::test; using namespace graphene::app; diff --git a/tests/tests/block_tests.cpp b/tests/tests/block_tests.cpp index c36455361f..c83ef29e7b 100644 --- a/tests/tests/block_tests.cpp +++ b/tests/tests/block_tests.cpp @@ -39,6 +39,7 @@ #include #include +#include #include "../common/database_fixture.hpp" @@ -1246,7 +1247,13 @@ BOOST_FIXTURE_TEST_CASE( transaction_invalidated_in_cache, database_fixture ) fc::temp_directory data_dir2( graphene::utilities::temp_directory_path() ); database db2; - db2.open(data_dir2.path(), make_genesis, "TEST"); + { + std::string genesis_json; + fc::read_file_contents( data_dir.path() / "genesis.json", genesis_json ); + genesis_state_type genesis = fc::json::from_string( genesis_json ).as( 50 ); + genesis.initial_chain_id = fc::sha256::hash( genesis_json ); + db2.open(data_dir2.path(), [&genesis] () { return genesis; }, "TEST"); + } BOOST_CHECK( db.get_chain_id() == db2.get_chain_id() ); while( db2.head_block_num() < db.head_block_num() ) From afd64de9834e8ef2bdec2a07e00bc31940730f0f Mon Sep 17 00:00:00 2001 From: abitmore Date: Fri, 26 Mar 2021 14:54:23 +0000 Subject: [PATCH 04/31] Update database_fixture_base destructor To cleanup properly. --- tests/common/database_fixture.cpp | 33 +++++++++++++++---------------- tests/common/database_fixture.hpp | 2 +- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/tests/common/database_fixture.cpp b/tests/common/database_fixture.cpp index fbc6d7e3b6..92bdfabc36 100644 --- a/tests/common/database_fixture.cpp +++ b/tests/common/database_fixture.cpp @@ -89,23 +89,6 @@ database_fixture_base::database_fixture_base() database_fixture_base::~database_fixture_base() { - try { - // If we're unwinding due to an exception, don't do any more checks. - // This way, boost test's last checkpoint tells us approximately where the error was. - if( !std::uncaught_exception() ) - { - verify_asset_supplies(db); - BOOST_CHECK( db.get_node_properties().skip_flags == database::skip_nothing ); - } - return; - } catch (fc::exception& ex) { - BOOST_FAIL( std::string("fc::exception in ~database_fixture: ") + ex.to_detail_string() ); - } catch (std::exception& e) { - BOOST_FAIL( std::string("std::exception in ~database_fixture:") + e.what() ); - } catch (...) { - BOOST_FAIL( "Uncaught exception in ~database_fixture" ); - } - // cleanup data in ES if( !es_index_prefix.empty() || !es_obj_index_prefix.empty() ) { @@ -140,6 +123,22 @@ database_fixture_base::~database_fixture_base() } } + try { + // If we're unwinding due to an exception, don't do any more checks. + // This way, boost test's last checkpoint tells us approximately where the error was. + if( !std::uncaught_exception() ) + { + verify_asset_supplies(db); + BOOST_CHECK( db.get_node_properties().skip_flags == database::skip_nothing ); + } + } catch (fc::exception& ex) { + BOOST_FAIL( std::string("fc::exception in ~database_fixture: ") + ex.to_detail_string() ); + } catch (std::exception& e) { + BOOST_FAIL( std::string("std::exception in ~database_fixture:") + e.what() ); + } catch (...) { + BOOST_FAIL( "Uncaught exception in ~database_fixture" ); + } + } void database_fixture_base::init_genesis( database_fixture_base& fixture ) diff --git a/tests/common/database_fixture.hpp b/tests/common/database_fixture.hpp index 000016050e..a8338b27b5 100644 --- a/tests/common/database_fixture.hpp +++ b/tests/common/database_fixture.hpp @@ -224,7 +224,7 @@ struct database_fixture_base { const std::string current_suite_name; database_fixture_base(); - ~database_fixture_base(); + virtual ~database_fixture_base(); template static void set_option( boost::program_options::variables_map& options, const std::string& name, const T& value ) From 00d8c7b6b055e546d8a32f42df0177c8d4706906 Mon Sep 17 00:00:00 2001 From: abitmore Date: Fri, 26 Mar 2021 16:32:45 +0000 Subject: [PATCH 05/31] Move db_fixture_base member init to constructor --- tests/common/database_fixture.cpp | 2 ++ tests/common/database_fixture.hpp | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/common/database_fixture.cpp b/tests/common/database_fixture.cpp index 92bdfabc36..cca8b293a9 100644 --- a/tests/common/database_fixture.cpp +++ b/tests/common/database_fixture.cpp @@ -70,6 +70,8 @@ void clearable_block::clear() database_fixture_base::database_fixture_base() : app(), db( *app.chain_database() ), + private_key( fc::ecc::private_key::generate() ), + init_account_priv_key( fc::ecc::private_key::regenerate(fc::sha256::hash(string("null_key")) ) ), init_account_pub_key( init_account_priv_key.get_public_key() ), current_test_name( buf::current_test_case().p_name.value ), current_suite_name( buf::get(buf::current_test_case().p_parent_id).p_name diff --git a/tests/common/database_fixture.hpp b/tests/common/database_fixture.hpp index a8338b27b5..339388d123 100644 --- a/tests/common/database_fixture.hpp +++ b/tests/common/database_fixture.hpp @@ -207,8 +207,8 @@ struct database_fixture_base { signed_transaction trx; public_key_type committee_key; account_id_type committee_account; - const fc::ecc::private_key private_key = fc::ecc::private_key::generate(); - const fc::ecc::private_key init_account_priv_key = fc::ecc::private_key::regenerate(fc::sha256::hash(string("null_key")) ); + const fc::ecc::private_key private_key; + const fc::ecc::private_key init_account_priv_key; const public_key_type init_account_pub_key; fc::temp_directory data_dir; From 1db990562716f0ab96607be44ad8380a8c4049ac Mon Sep 17 00:00:00 2001 From: abitmore Date: Fri, 26 Mar 2021 16:48:41 +0000 Subject: [PATCH 06/31] Remove Extra initialization of db_fixture.app --- tests/common/database_fixture.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/common/database_fixture.cpp b/tests/common/database_fixture.cpp index cca8b293a9..19a7b9bc5f 100644 --- a/tests/common/database_fixture.cpp +++ b/tests/common/database_fixture.cpp @@ -427,9 +427,6 @@ boost::program_options::variables_map database_fixture_base::init_options( datab options.insert(std::make_pair("bucket-size", boost::program_options::variable_value(string("[15]"),false))); - // apply api limits, initialize the "has_market_history_plugin" variable and etc in app_options - fixture.app.initialize(graphene::utilities::temp_directory_path(), options); - auto mhplugin = fixture.app.register_plugin(true); auto goplugin = fixture.app.register_plugin(true); From 10b64a23f017b77dd4cb16c71463483ddc16364d Mon Sep 17 00:00:00 2001 From: abitmore Date: Sat, 27 Mar 2021 01:04:57 +0000 Subject: [PATCH 07/31] Fix typo in logging --- libraries/db/object_database.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/db/object_database.cpp b/libraries/db/object_database.cpp index 75b7090be4..6767051264 100644 --- a/libraries/db/object_database.cpp +++ b/libraries/db/object_database.cpp @@ -98,7 +98,7 @@ void object_database::wipe(const fc::path& data_dir) close(); ilog("Wiping object database..."); fc::remove_all(data_dir / "object_database"); - ilog("Done wiping object databse."); + ilog("Done wiping object database."); } void object_database::open(const fc::path& data_dir) From 8ed1ce29dc4b2bea0841806fe13a1eff92640005 Mon Sep 17 00:00:00 2001 From: abitmore Date: Sun, 28 Mar 2021 03:04:13 +0000 Subject: [PATCH 08/31] Refactor application and plugin framework --- libraries/app/application.cpp | 171 +++++++++++------- libraries/app/application_impl.hxx | 25 ++- .../app/include/graphene/app/application.hpp | 15 +- libraries/app/include/graphene/app/plugin.hpp | 42 ++--- libraries/app/plugin.cpp | 25 +-- .../account_history_plugin.cpp | 10 +- .../account_history_plugin.hpp | 10 +- .../api_helper_indexes/api_helper_indexes.cpp | 3 +- .../api_helper_indexes/api_helper_indexes.hpp | 4 +- .../custom_operations_plugin.cpp | 5 +- .../custom_operations_plugin.hpp | 10 +- libraries/plugins/debug_witness/debug_api.cpp | 4 +- .../plugins/debug_witness/debug_witness.cpp | 12 +- .../graphene/debug_witness/debug_witness.hpp | 12 +- .../delayed_node/delayed_node_plugin.cpp | 5 +- .../delayed_node/delayed_node_plugin.hpp | 10 +- .../elasticsearch/elasticsearch_plugin.cpp | 3 +- .../elasticsearch/elasticsearch_plugin.hpp | 10 +- libraries/plugins/es_objects/es_objects.cpp | 3 +- .../graphene/es_objects/es_objects.hpp | 10 +- .../grouped_orders/grouped_orders_plugin.cpp | 3 +- .../grouped_orders/grouped_orders_plugin.hpp | 10 +- .../market_history/market_history_plugin.hpp | 10 +- .../market_history/market_history_plugin.cpp | 3 +- .../include/graphene/snapshot/snapshot.hpp | 8 +- libraries/plugins/snapshot/snapshot.cpp | 4 - .../template_plugin/template_plugin.hpp | 13 +- .../template_plugin/template_plugin.cpp | 26 ++- .../include/graphene/witness/witness.hpp | 13 +- libraries/plugins/witness/witness.cpp | 5 - programs/witness_node/main.cpp | 88 +++++---- 31 files changed, 313 insertions(+), 259 deletions(-) diff --git a/libraries/app/application.cpp b/libraries/app/application.cpp index e1ff23f2ce..827c5c4144 100644 --- a/libraries/app/application.cpp +++ b/libraries/app/application.cpp @@ -114,6 +114,11 @@ namespace detail { namespace graphene { namespace app { namespace detail { +application_impl::~application_impl() +{ + this->shutdown(); +} + void application_impl::reset_p2p_node(const fc::path& data_dir) { try { _p2p_network = std::make_shared("BitShares Reference Implementation"); @@ -158,7 +163,7 @@ void application_impl::reset_p2p_node(const fc::path& data_dir) void application_impl::new_connection( const fc::http::websocket_connection_ptr& c ) { auto wsc = std::make_shared(c, GRAPHENE_NET_MAX_NESTED_OBJECTS); - auto login = std::make_shared( std::ref(*_self) ); + auto login = std::make_shared( _self ); login->enable_api("database_api"); wsc->register_api(login->database()); @@ -236,8 +241,17 @@ void application_impl::set_dbg_init_key( graphene::chain::genesis_state_type& ge genesis.initial_witness_candidates[i].block_signing_key = init_pubkey; } -void application_impl::initialize() +void application_impl::initialize(const fc::path& data_dir, shared_ptr options) { + _data_dir = data_dir; + _options = options; + + if ( _options->count("io-threads") > 0 ) + { + const uint16_t num_threads = _options->at("io-threads").as(); + fc::asio::default_io_service_scope::set_num_threads(num_threads); + } + if( _options->count("force-validate") > 0 ) { ilog( "All transaction signatures will be validated" ); @@ -287,6 +301,7 @@ void application_impl::initialize() _apiaccess.permission_map["*"] = wild_access; } + initialize_plugins(); } void application_impl::set_api_limit() { @@ -501,8 +516,11 @@ void application_impl::startup() throw; } + startup_plugins(); + if( _active_plugins.find( "delayed_node" ) == _active_plugins.end() ) reset_p2p_node(_data_dir); + reset_websocket_server(); reset_websocket_tls_server(); } FC_LOG_AND_RETHROW() } @@ -621,7 +639,7 @@ bool application_impl::handle_block(const graphene::net::block_message& blk_msg, if( !_is_finished_syncing && !sync_mode ) { _is_finished_syncing = true; - _self->syncing_finished(); + _self.syncing_finished(); } } FC_CAPTURE_AND_RETHROW( (blk_msg)(sync_mode) ) return false; } @@ -969,29 +987,92 @@ uint8_t application_impl::get_current_block_interval_in_seconds() const return _chain_db->get_global_properties().parameters.block_interval; } +void application_impl::shutdown() +{ + if( _websocket_tls_server ) + _websocket_tls_server.reset(); + if( _websocket_server ) + _websocket_server.reset(); + // TODO wait until all connections are closed and messages handled? + // plugins E.G. witness_plugin may send data to p2p network, so shutdown them first + shutdown_plugins(); -} } } // namespace graphene namespace app namespace detail + if( _p2p_network ) + { + _p2p_network->close(); + _p2p_network.reset(); + // TODO wait until all connections are closed and messages handled? + } -namespace graphene { namespace app { + if( _chain_db ) + { + _chain_db->close(); + _chain_db.reset(); + } +} -application::application() - : my(std::make_unique(this)) -{} +void application_impl::enable_plugin( const string& name ) +{ + FC_ASSERT(_available_plugins[name], "Unknown plugin '" + name + "'"); + _active_plugins[name] = _available_plugins[name]; +} -application::~application() +void application_impl::initialize_plugins() { - if( my->_p2p_network ) + for( auto& entry : _active_plugins ) { - my->_p2p_network->close(); - my->_p2p_network.reset(); + ilog( "Initializing plugin ${name}", ( "name", entry.second->plugin_name() ) ); + entry.second->plugin_initialize( *_options ); + ilog( "Initialized plugin ${name}", ( "name", entry.second->plugin_name() ) ); } - if( my->_chain_db ) +} + +void application_impl::startup_plugins() +{ + for( auto& entry : _active_plugins ) { - my->_chain_db->close(); + ilog( "Starting plugin ${name}", ( "name", entry.second->plugin_name() ) ); + entry.second->plugin_startup(); + ilog( "Started plugin ${name}", ( "name", entry.second->plugin_name() ) ); } } +void application_impl::shutdown_plugins() +{ + for( auto& entry : _active_plugins ) + { + ilog( "Stopping plugin ${name}", ( "name", entry.second->plugin_name() ) ); + entry.second->plugin_shutdown(); + ilog( "Stopped plugin ${name}", ( "name", entry.second->plugin_name() ) ); + } +} + +void application_impl::add_available_plugin(std::shared_ptr p) +{ + _available_plugins[p->plugin_name()] = p; +} + +void application_impl::set_block_production(bool producing_blocks) +{ + _is_block_producer = producing_blocks; +} + +} } } // namespace graphene namespace app namespace detail + +namespace graphene { namespace app { + +application::application() + : my(std::make_unique(*this)) +{ + //nothing else to do +} + +application::~application() +{ + //nothing to do +} + void application::set_program_options(boost::program_options::options_description& command_line_options, boost::program_options::options_description& configuration_file_options) const { @@ -1097,18 +1178,9 @@ void application::set_program_options(boost::program_options::options_descriptio configuration_file_options.add(_cfg_options); } -void application::initialize(const fc::path& data_dir, const boost::program_options::variables_map& options) +void application::initialize(const fc::path& data_dir, std::shared_ptr options) { - my->_data_dir = data_dir; - my->_options = &options; - - if ( options.count("io-threads") > 0 ) - { - const uint16_t num_threads = options["io-threads"].as(); - fc::asio::default_io_service_scope::set_num_threads(num_threads); - } - - my->initialize(); + my->initialize( data_dir, options ); } void application::startup() @@ -1158,7 +1230,7 @@ std::shared_ptr application::chain_database() const void application::set_block_production(bool producing_blocks) { - my->_is_block_producer = producing_blocks; + my->set_block_production(producing_blocks); } optional< api_access_info > application::get_api_access_info( const string& username )const @@ -1176,56 +1248,19 @@ bool application::is_finished_syncing() const return my->_is_finished_syncing; } -void graphene::app::application::enable_plugin(const string& name) +void application::enable_plugin(const string& name) { - FC_ASSERT(my->_available_plugins[name], "Unknown plugin '" + name + "'"); - my->_active_plugins[name] = my->_available_plugins[name]; - my->_active_plugins[name]->plugin_set_app(this); + my->enable_plugin(name); } -void graphene::app::application::add_available_plugin(std::shared_ptr p) +void application::add_available_plugin(std::shared_ptr p) { - my->_available_plugins[p->plugin_name()] = p; + my->add_available_plugin(p); } -void application::shutdown_plugins() -{ - for( auto& entry : my->_active_plugins ) - { - ilog( "Stopping plugin ${name}", ( "name", entry.second->plugin_name() ) ); - entry.second->plugin_shutdown(); - ilog( "Stopped plugin ${name}", ( "name", entry.second->plugin_name() ) ); - } -} void application::shutdown() { - if( my->_p2p_network ) - my->_p2p_network->close(); - if( my->_chain_db ) - { - my->_chain_db->close(); - my->_chain_db = nullptr; - } -} - -void application::initialize_plugins( const boost::program_options::variables_map& options ) -{ - for( auto& entry : my->_active_plugins ) - { - ilog( "Initializing plugin ${name}", ( "name", entry.second->plugin_name() ) ); - entry.second->plugin_initialize( options ); - ilog( "Initialized plugin ${name}", ( "name", entry.second->plugin_name() ) ); - } -} - -void application::startup_plugins() -{ - for( auto& entry : my->_active_plugins ) - { - ilog( "Starting plugin ${name}", ( "name", entry.second->plugin_name() ) ); - entry.second->plugin_startup(); - ilog( "Started plugin ${name}", ( "name", entry.second->plugin_name() ) ); - } + my->shutdown(); } const application_options& application::get_options() diff --git a/libraries/app/application_impl.hxx b/libraries/app/application_impl.hxx index 9218770c7c..e95e7b6ed1 100644 --- a/libraries/app/application_impl.hxx +++ b/libraries/app/application_impl.hxx @@ -28,21 +28,22 @@ class application_impl : public net::node_delegate void reset_websocket_tls_server(); - explicit application_impl(application* self) + explicit application_impl(application& self) : _self(self), _chain_db(std::make_shared()) { } - virtual ~application_impl() - { - } + virtual ~application_impl(); + + void set_block_production(bool producing_blocks); void set_dbg_init_key( graphene::chain::genesis_state_type& genesis, const std::string& init_key ); void set_api_limit(); - void initialize(); + void initialize(const fc::path& data_dir, std::shared_ptr options); void startup(); + void shutdown(); fc::optional< api_access_info > get_api_access_info(const string& username)const; @@ -183,13 +184,19 @@ class application_impl : public net::node_delegate uint8_t get_current_block_interval_in_seconds() const override; + /// Add an available plugin + void add_available_plugin( std::shared_ptr p ); + + /// Enables a plugin + void enable_plugin(const string& name); + /// Returns whether a plugin is enabled bool is_plugin_enabled(const string& name) const; - application* _self; + application& _self; fc::path _data_dir; - const boost::program_options::variables_map* _options = nullptr; + std::shared_ptr _options; api_access _apiaccess; std::shared_ptr _chain_db; @@ -202,6 +209,10 @@ class application_impl : public net::node_delegate bool _is_finished_syncing = false; private: + void initialize_plugins(); + void startup_plugins(); + void shutdown_plugins(); + fc::serial_valve valve; }; diff --git a/libraries/app/include/graphene/app/application.hpp b/libraries/app/include/graphene/app/application.hpp index f3f54ae54e..4ccd8b031d 100644 --- a/libraries/app/include/graphene/app/application.hpp +++ b/libraries/app/include/graphene/app/application.hpp @@ -85,20 +85,17 @@ namespace graphene { namespace app { void set_program_options(boost::program_options::options_description& command_line_options, boost::program_options::options_description& configuration_file_options)const; - void initialize(const fc::path& data_dir, const boost::program_options::variables_map& options); - void initialize_plugins(const boost::program_options::variables_map& options); + void initialize(const fc::path& data_dir, std::shared_ptr options); void startup(); void shutdown(); - void startup_plugins(); - void shutdown_plugins(); template std::shared_ptr register_plugin(bool auto_load = false) { - auto plug = std::make_shared(); - plug->plugin_set_app(this); + auto plug = std::make_shared(*this); string cli_plugin_desc = plug->plugin_name() + " plugin. " + plug->plugin_description() + "\nOptions"; - boost::program_options::options_description plugin_cli_options( cli_plugin_desc ), plugin_cfg_options; + boost::program_options::options_description plugin_cli_options( cli_plugin_desc ); + boost::program_options::options_description plugin_cfg_options; plug->plugin_set_program_options(plugin_cli_options, plugin_cfg_options); if( !plugin_cli_options.options().empty() ) @@ -150,8 +147,10 @@ namespace graphene { namespace app { std::shared_ptr elasticsearch_thread; private: + /// Add an available plugin void add_available_plugin( std::shared_ptr p ); - std::shared_ptr my; + + std::unique_ptr my; boost::program_options::options_description _cli_options; boost::program_options::options_description _cfg_options; diff --git a/libraries/app/include/graphene/app/plugin.hpp b/libraries/app/include/graphene/app/plugin.hpp index 12e0fce798..b2809723ad 100644 --- a/libraries/app/include/graphene/app/plugin.hpp +++ b/libraries/app/include/graphene/app/plugin.hpp @@ -33,10 +33,18 @@ namespace graphene { namespace app { class abstract_plugin { public: - virtual ~abstract_plugin(){} + explicit abstract_plugin(application& a) : _app(a) {} + virtual ~abstract_plugin() = default; + + /// Get the name of the plugin virtual std::string plugin_name()const = 0; + + /// Get the description of the plugin virtual std::string plugin_description()const = 0; + /// Get a reference of the application bound to the plugin + application& app()const { return _app; } + /** * @brief Perform early startup routines and register plugin indexes, callbacks, etc. * @@ -66,13 +74,6 @@ class abstract_plugin */ virtual void plugin_shutdown() = 0; - /** - * @brief Register the application instance with the plugin. - * - * This is called by the framework to set the application. - */ - virtual void plugin_set_app( application* a ) = 0; - /** * @brief Fill in command line parameters used by the plugin. * @@ -88,6 +89,8 @@ class abstract_plugin boost::program_options::options_description& command_line_options, boost::program_options::options_description& config_file_options ) = 0; + protected: + application& _app; }; /** @@ -97,27 +100,22 @@ class abstract_plugin class plugin : public abstract_plugin { public: - plugin(); - virtual ~plugin() override; - - virtual std::string plugin_name()const override; - virtual std::string plugin_description()const override; - virtual void plugin_initialize( const boost::program_options::variables_map& options ) override; - virtual void plugin_startup() override; - virtual void plugin_shutdown() override; - virtual void plugin_set_app( application* app ) override; - virtual void plugin_set_program_options( + explicit plugin(application& a) : abstract_plugin(a) {} + ~plugin() override = default; + + std::string plugin_name()const override; + std::string plugin_description()const override; + void plugin_initialize( const boost::program_options::variables_map& options ) override; + void plugin_startup() override; + void plugin_shutdown() override; + void plugin_set_program_options( boost::program_options::options_description& command_line_options, boost::program_options::options_description& config_file_options ) override; chain::database& database() { return *app().chain_database(); } - application& app()const { assert(_app); return *_app; } protected: net::node_ptr p2p_node() { return app().p2p_node(); } - - private: - application* _app = nullptr; }; /// @ingroup Some useful tools for boost::program_options arguments using vectors of JSON strings diff --git a/libraries/app/plugin.cpp b/libraries/app/plugin.cpp index 02d1fdb828..58e781f3dd 100644 --- a/libraries/app/plugin.cpp +++ b/libraries/app/plugin.cpp @@ -27,17 +27,6 @@ namespace graphene { namespace app { -plugin::plugin() -{ - _app = nullptr; - return; -} - -plugin::~plugin() -{ - return; -} - std::string plugin::plugin_name()const { return ""; @@ -50,23 +39,17 @@ std::string plugin::plugin_description()const void plugin::plugin_initialize( const boost::program_options::variables_map& options ) { - return; + // nothing to do } void plugin::plugin_startup() { - return; + // nothing to do } void plugin::plugin_shutdown() { - return; -} - -void plugin::plugin_set_app( application* app ) -{ - _app = app; - return; + // nothing to do } void plugin::plugin_set_program_options( @@ -74,7 +57,7 @@ void plugin::plugin_set_program_options( boost::program_options::options_description& config_file_options ) { - return; + // nothing to do } } } // graphene::app diff --git a/libraries/plugins/account_history/account_history_plugin.cpp b/libraries/plugins/account_history/account_history_plugin.cpp index c0bbcebc2e..19e384a171 100644 --- a/libraries/plugins/account_history/account_history_plugin.cpp +++ b/libraries/plugins/account_history/account_history_plugin.cpp @@ -49,8 +49,6 @@ class account_history_plugin_impl account_history_plugin_impl(account_history_plugin& _plugin) : _self( _plugin ) { } - virtual ~account_history_plugin_impl(); - /** this method is called as a callback after a block is applied * and will process/index all operations that were applied in the block. @@ -76,11 +74,6 @@ class account_history_plugin_impl }; -account_history_plugin_impl::~account_history_plugin_impl() -{ - return; -} - void account_history_plugin_impl::update_account_histories( const signed_block& b ) { graphene::chain::database& db = database(); @@ -282,7 +275,8 @@ void account_history_plugin_impl::add_account_history( const account_id_type acc -account_history_plugin::account_history_plugin() : +account_history_plugin::account_history_plugin(graphene::app::application& app) : + plugin(app), my( std::make_unique(*this) ) { } diff --git a/libraries/plugins/account_history/include/graphene/account_history/account_history_plugin.hpp b/libraries/plugins/account_history/include/graphene/account_history/account_history_plugin.hpp index 99492768ce..9676869fbf 100644 --- a/libraries/plugins/account_history/include/graphene/account_history/account_history_plugin.hpp +++ b/libraries/plugins/account_history/include/graphene/account_history/account_history_plugin.hpp @@ -64,15 +64,15 @@ namespace detail class account_history_plugin : public graphene::app::plugin { public: - account_history_plugin(); - virtual ~account_history_plugin(); + account_history_plugin(graphene::app::application& app); + ~account_history_plugin() override; std::string plugin_name()const override; - virtual void plugin_set_program_options( + void plugin_set_program_options( boost::program_options::options_description& cli, boost::program_options::options_description& cfg) override; - virtual void plugin_initialize(const boost::program_options::variables_map& options) override; - virtual void plugin_startup() override; + void plugin_initialize(const boost::program_options::variables_map& options) override; + void plugin_startup() override; flat_set tracked_accounts()const; diff --git a/libraries/plugins/api_helper_indexes/api_helper_indexes.cpp b/libraries/plugins/api_helper_indexes/api_helper_indexes.cpp index 548c5fc689..00fbbf3e16 100644 --- a/libraries/plugins/api_helper_indexes/api_helper_indexes.cpp +++ b/libraries/plugins/api_helper_indexes/api_helper_indexes.cpp @@ -150,7 +150,8 @@ class api_helper_indexes_impl } // end namespace detail -api_helper_indexes::api_helper_indexes() : +api_helper_indexes::api_helper_indexes(graphene::app::application& app) : + plugin(app), my( std::make_unique(*this) ) { } diff --git a/libraries/plugins/api_helper_indexes/include/graphene/api_helper_indexes/api_helper_indexes.hpp b/libraries/plugins/api_helper_indexes/include/graphene/api_helper_indexes/api_helper_indexes.hpp index 49b79bee90..a6bb59b307 100644 --- a/libraries/plugins/api_helper_indexes/include/graphene/api_helper_indexes/api_helper_indexes.hpp +++ b/libraries/plugins/api_helper_indexes/include/graphene/api_helper_indexes/api_helper_indexes.hpp @@ -79,8 +79,8 @@ namespace detail class api_helper_indexes : public graphene::app::plugin { public: - api_helper_indexes(); - virtual ~api_helper_indexes(); + api_helper_indexes(graphene::app::application& app); + ~api_helper_indexes() override; std::string plugin_name()const override; std::string plugin_description()const override; diff --git a/libraries/plugins/custom_operations/custom_operations_plugin.cpp b/libraries/plugins/custom_operations/custom_operations_plugin.cpp index c9c6b21f18..1696b98f50 100644 --- a/libraries/plugins/custom_operations/custom_operations_plugin.cpp +++ b/libraries/plugins/custom_operations/custom_operations_plugin.cpp @@ -100,12 +100,13 @@ void custom_operations_plugin_impl::onBlock() custom_operations_plugin_impl::~custom_operations_plugin_impl() { - return; + // nothing to do } } // end namespace detail -custom_operations_plugin::custom_operations_plugin() : +custom_operations_plugin::custom_operations_plugin(graphene::app::application& app) : + plugin(app), my( std::make_unique(*this) ) { } diff --git a/libraries/plugins/custom_operations/include/graphene/custom_operations/custom_operations_plugin.hpp b/libraries/plugins/custom_operations/include/graphene/custom_operations/custom_operations_plugin.hpp index f4baacd573..e2a5fd9633 100644 --- a/libraries/plugins/custom_operations/include/graphene/custom_operations/custom_operations_plugin.hpp +++ b/libraries/plugins/custom_operations/include/graphene/custom_operations/custom_operations_plugin.hpp @@ -41,16 +41,16 @@ namespace detail class custom_operations_plugin : public graphene::app::plugin { public: - custom_operations_plugin(); - virtual ~custom_operations_plugin(); + custom_operations_plugin(graphene::app::application& app); + ~custom_operations_plugin() override; std::string plugin_name()const override; std::string plugin_description()const override; - virtual void plugin_set_program_options( + void plugin_set_program_options( boost::program_options::options_description& cli, boost::program_options::options_description& cfg) override; - virtual void plugin_initialize(const boost::program_options::variables_map& options) override; - virtual void plugin_startup() override; + void plugin_initialize(const boost::program_options::variables_map& options) override; + void plugin_startup() override; friend class detail::custom_operations_plugin_impl; std::unique_ptr my; diff --git a/libraries/plugins/debug_witness/debug_api.cpp b/libraries/plugins/debug_witness/debug_api.cpp index 43ffd6cd38..7fd8792af5 100644 --- a/libraries/plugins/debug_witness/debug_api.cpp +++ b/libraries/plugins/debug_witness/debug_api.cpp @@ -34,7 +34,9 @@ class debug_api_impl }; debug_api_impl::debug_api_impl( graphene::app::application& _app ) : app( _app ) -{} +{ + // Nothing else to do +} void debug_api_impl::debug_push_blocks( const std::string& src_filename, uint32_t count ) diff --git a/libraries/plugins/debug_witness/debug_witness.cpp b/libraries/plugins/debug_witness/debug_witness.cpp index ea7f209fa6..1a9533166a 100644 --- a/libraries/plugins/debug_witness/debug_witness.cpp +++ b/libraries/plugins/debug_witness/debug_witness.cpp @@ -38,7 +38,10 @@ using std::vector; namespace bpo = boost::program_options; -debug_witness_plugin::~debug_witness_plugin() {} +debug_witness_plugin::~debug_witness_plugin() +{ + cleanup(); +} void debug_witness_plugin::plugin_set_program_options( boost::program_options::options_description& command_line_options, @@ -100,7 +103,6 @@ void debug_witness_plugin::plugin_startup() _changed_objects_conn = db.changed_objects.connect([this](const std::vector& ids, const fc::flat_set& impacted_accounts){ on_changed_objects(ids, impacted_accounts); }); _removed_objects_conn = db.removed_objects.connect([this](const std::vector& ids, const std::vector& objs, const fc::flat_set& impacted_accounts){ on_removed_objects(ids, objs, impacted_accounts); }); - return; } void debug_witness_plugin::on_changed_objects( const std::vector& ids, const fc::flat_set& impacted_accounts ) @@ -155,11 +157,15 @@ void debug_witness_plugin::flush_json_object_stream() } void debug_witness_plugin::plugin_shutdown() +{ + cleanup(); +} + +void debug_witness_plugin::cleanup() { if( _json_object_stream ) { _json_object_stream->close(); _json_object_stream.reset(); } - return; } diff --git a/libraries/plugins/debug_witness/include/graphene/debug_witness/debug_witness.hpp b/libraries/plugins/debug_witness/include/graphene/debug_witness/debug_witness.hpp index 4b5369211a..e7d4f734d1 100644 --- a/libraries/plugins/debug_witness/include/graphene/debug_witness/debug_witness.hpp +++ b/libraries/plugins/debug_witness/include/graphene/debug_witness/debug_witness.hpp @@ -34,23 +34,25 @@ namespace graphene { namespace debug_witness_plugin { class debug_witness_plugin : public graphene::app::plugin { public: - ~debug_witness_plugin(); + debug_witness_plugin(graphene::app::application& app) : graphene::app::plugin(app) {} + ~debug_witness_plugin() override; std::string plugin_name()const override; - virtual void plugin_set_program_options( + void plugin_set_program_options( boost::program_options::options_description &command_line_options, boost::program_options::options_description &config_file_options ) override; - virtual void plugin_initialize( const boost::program_options::variables_map& options ) override; - virtual void plugin_startup() override; - virtual void plugin_shutdown() override; + void plugin_initialize( const boost::program_options::variables_map& options ) override; + void plugin_startup() override; + void plugin_shutdown() override; void set_json_object_stream( const std::string& filename ); void flush_json_object_stream(); private: + void cleanup(); void on_changed_objects( const std::vector& ids, const fc::flat_set& impacted_accounts ); void on_removed_objects( const std::vector& ids, const std::vector objs, const fc::flat_set& impacted_accounts ); diff --git a/libraries/plugins/delayed_node/delayed_node_plugin.cpp b/libraries/plugins/delayed_node/delayed_node_plugin.cpp index d85b6291e7..18ef6b26b1 100644 --- a/libraries/plugins/delayed_node/delayed_node_plugin.cpp +++ b/libraries/plugins/delayed_node/delayed_node_plugin.cpp @@ -46,8 +46,9 @@ struct delayed_node_plugin_impl { }; } -delayed_node_plugin::delayed_node_plugin() - : my(nullptr) +delayed_node_plugin::delayed_node_plugin(graphene::app::application& app) : + plugin(app), + my(nullptr) {} delayed_node_plugin::~delayed_node_plugin() diff --git a/libraries/plugins/delayed_node/include/graphene/delayed_node/delayed_node_plugin.hpp b/libraries/plugins/delayed_node/include/graphene/delayed_node/delayed_node_plugin.hpp index 509ef4c717..4e06c7fcfc 100644 --- a/libraries/plugins/delayed_node/include/graphene/delayed_node/delayed_node_plugin.hpp +++ b/libraries/plugins/delayed_node/include/graphene/delayed_node/delayed_node_plugin.hpp @@ -32,14 +32,14 @@ class delayed_node_plugin : public graphene::app::plugin { std::unique_ptr my; public: - delayed_node_plugin(); - virtual ~delayed_node_plugin(); + delayed_node_plugin(graphene::app::application& app); + ~delayed_node_plugin() override; std::string plugin_name()const override { return "delayed_node"; } - virtual void plugin_set_program_options(boost::program_options::options_description&, + void plugin_set_program_options(boost::program_options::options_description&, boost::program_options::options_description& cfg) override; - virtual void plugin_initialize(const boost::program_options::variables_map& options) override; - virtual void plugin_startup() override; + void plugin_initialize(const boost::program_options::variables_map& options) override; + void plugin_startup() override; void mainloop(); protected: diff --git a/libraries/plugins/elasticsearch/elasticsearch_plugin.cpp b/libraries/plugins/elasticsearch/elasticsearch_plugin.cpp index 3d80c64f1c..4eaa6643e9 100644 --- a/libraries/plugins/elasticsearch/elasticsearch_plugin.cpp +++ b/libraries/plugins/elasticsearch/elasticsearch_plugin.cpp @@ -433,7 +433,8 @@ void elasticsearch_plugin_impl::populateESstruct() } // end namespace detail -elasticsearch_plugin::elasticsearch_plugin() : +elasticsearch_plugin::elasticsearch_plugin(graphene::app::application& app) : + plugin(app), my( std::make_unique(*this) ) { } diff --git a/libraries/plugins/elasticsearch/include/graphene/elasticsearch/elasticsearch_plugin.hpp b/libraries/plugins/elasticsearch/include/graphene/elasticsearch/elasticsearch_plugin.hpp index 7b08e1d73f..61f385efa0 100644 --- a/libraries/plugins/elasticsearch/include/graphene/elasticsearch/elasticsearch_plugin.hpp +++ b/libraries/plugins/elasticsearch/include/graphene/elasticsearch/elasticsearch_plugin.hpp @@ -55,16 +55,16 @@ enum mode { only_save = 0 , only_query = 1, all = 2 }; class elasticsearch_plugin : public graphene::app::plugin { public: - elasticsearch_plugin(); - virtual ~elasticsearch_plugin(); + elasticsearch_plugin(graphene::app::application& app); + ~elasticsearch_plugin() override; std::string plugin_name()const override; std::string plugin_description()const override; - virtual void plugin_set_program_options( + void plugin_set_program_options( boost::program_options::options_description& cli, boost::program_options::options_description& cfg) override; - virtual void plugin_initialize(const boost::program_options::variables_map& options) override; - virtual void plugin_startup() override; + void plugin_initialize(const boost::program_options::variables_map& options) override; + void plugin_startup() override; operation_history_object get_operation_by_id(operation_history_id_type id); vector get_account_history(const account_id_type account_id, diff --git a/libraries/plugins/es_objects/es_objects.cpp b/libraries/plugins/es_objects/es_objects.cpp index 7be78a2630..e538edd370 100644 --- a/libraries/plugins/es_objects/es_objects.cpp +++ b/libraries/plugins/es_objects/es_objects.cpp @@ -274,7 +274,8 @@ es_objects_plugin_impl::~es_objects_plugin_impl() } // end namespace detail -es_objects_plugin::es_objects_plugin() : +es_objects_plugin::es_objects_plugin(graphene::app::application& app) : + plugin(app), my( std::make_unique(*this) ) { } diff --git a/libraries/plugins/es_objects/include/graphene/es_objects/es_objects.hpp b/libraries/plugins/es_objects/include/graphene/es_objects/es_objects.hpp index fa91e3bde4..b9bc339681 100644 --- a/libraries/plugins/es_objects/include/graphene/es_objects/es_objects.hpp +++ b/libraries/plugins/es_objects/include/graphene/es_objects/es_objects.hpp @@ -38,16 +38,16 @@ namespace detail class es_objects_plugin : public graphene::app::plugin { public: - es_objects_plugin(); - virtual ~es_objects_plugin(); + es_objects_plugin(graphene::app::application& app); + ~es_objects_plugin(); std::string plugin_name()const override; std::string plugin_description()const override; - virtual void plugin_set_program_options( + void plugin_set_program_options( boost::program_options::options_description& cli, boost::program_options::options_description& cfg) override; - virtual void plugin_initialize(const boost::program_options::variables_map& options) override; - virtual void plugin_startup() override; + void plugin_initialize(const boost::program_options::variables_map& options) override; + void plugin_startup() override; friend class detail::es_objects_plugin_impl; std::unique_ptr my; diff --git a/libraries/plugins/grouped_orders/grouped_orders_plugin.cpp b/libraries/plugins/grouped_orders/grouped_orders_plugin.cpp index b83ee7d8f7..da655026fd 100644 --- a/libraries/plugins/grouped_orders/grouped_orders_plugin.cpp +++ b/libraries/plugins/grouped_orders/grouped_orders_plugin.cpp @@ -241,7 +241,8 @@ grouped_orders_plugin_impl::~grouped_orders_plugin_impl() } // end namespace detail -grouped_orders_plugin::grouped_orders_plugin() : +grouped_orders_plugin::grouped_orders_plugin(graphene::app::application& app) : + plugin(app), my( std::make_unique(*this) ) { } diff --git a/libraries/plugins/grouped_orders/include/graphene/grouped_orders/grouped_orders_plugin.hpp b/libraries/plugins/grouped_orders/include/graphene/grouped_orders/grouped_orders_plugin.hpp index 8f91ccbd9c..637e6e7925 100644 --- a/libraries/plugins/grouped_orders/include/graphene/grouped_orders/grouped_orders_plugin.hpp +++ b/libraries/plugins/grouped_orders/include/graphene/grouped_orders/grouped_orders_plugin.hpp @@ -69,16 +69,16 @@ namespace detail class grouped_orders_plugin : public graphene::app::plugin { public: - grouped_orders_plugin(); - virtual ~grouped_orders_plugin(); + grouped_orders_plugin(graphene::app::application& app); + ~grouped_orders_plugin() override; std::string plugin_name()const override; - virtual void plugin_set_program_options( + void plugin_set_program_options( boost::program_options::options_description& cli, boost::program_options::options_description& cfg) override; - virtual void plugin_initialize( + void plugin_initialize( const boost::program_options::variables_map& options) override; - virtual void plugin_startup() override; + void plugin_startup() override; const flat_set& tracked_groups()const; diff --git a/libraries/plugins/market_history/include/graphene/market_history/market_history_plugin.hpp b/libraries/plugins/market_history/include/graphene/market_history/market_history_plugin.hpp index 4007b11d15..4021dad75e 100644 --- a/libraries/plugins/market_history/include/graphene/market_history/market_history_plugin.hpp +++ b/libraries/plugins/market_history/include/graphene/market_history/market_history_plugin.hpp @@ -380,16 +380,16 @@ namespace detail class market_history_plugin : public graphene::app::plugin { public: - market_history_plugin(); - virtual ~market_history_plugin(); + market_history_plugin(graphene::app::application& app); + ~market_history_plugin() override; std::string plugin_name()const override; - virtual void plugin_set_program_options( + void plugin_set_program_options( boost::program_options::options_description& cli, boost::program_options::options_description& cfg) override; - virtual void plugin_initialize( + void plugin_initialize( const boost::program_options::variables_map& options) override; - virtual void plugin_startup() override; + void plugin_startup() override; uint32_t max_history()const; const flat_set& tracked_buckets()const; diff --git a/libraries/plugins/market_history/market_history_plugin.cpp b/libraries/plugins/market_history/market_history_plugin.cpp index b6d3800d4a..f9211d1d36 100644 --- a/libraries/plugins/market_history/market_history_plugin.cpp +++ b/libraries/plugins/market_history/market_history_plugin.cpp @@ -735,7 +735,8 @@ void market_history_plugin_impl::update_liquidity_pool_histories( -market_history_plugin::market_history_plugin() : +market_history_plugin::market_history_plugin(graphene::app::application& app) : + plugin(app), my( std::make_unique(*this) ) { } diff --git a/libraries/plugins/snapshot/include/graphene/snapshot/snapshot.hpp b/libraries/plugins/snapshot/include/graphene/snapshot/snapshot.hpp index eb8d3a16cb..5f62a0a678 100644 --- a/libraries/plugins/snapshot/include/graphene/snapshot/snapshot.hpp +++ b/libraries/plugins/snapshot/include/graphene/snapshot/snapshot.hpp @@ -32,19 +32,17 @@ namespace graphene { namespace snapshot_plugin { class snapshot_plugin : public graphene::app::plugin { public: - ~snapshot_plugin() {} + snapshot_plugin(graphene::app::application& app) : graphene::app::plugin(app) {} std::string plugin_name()const override; std::string plugin_description()const override; - virtual void plugin_set_program_options( + void plugin_set_program_options( boost::program_options::options_description &command_line_options, boost::program_options::options_description &config_file_options ) override; - virtual void plugin_initialize( const boost::program_options::variables_map& options ) override; - virtual void plugin_startup() override; - virtual void plugin_shutdown() override; + void plugin_initialize( const boost::program_options::variables_map& options ) override; private: void check_snapshot( const graphene::chain::signed_block& b); diff --git a/libraries/plugins/snapshot/snapshot.cpp b/libraries/plugins/snapshot/snapshot.cpp index a7e9d5138c..12a0553417 100644 --- a/libraries/plugins/snapshot/snapshot.cpp +++ b/libraries/plugins/snapshot/snapshot.cpp @@ -82,10 +82,6 @@ void snapshot_plugin::plugin_initialize(const boost::program_options::variables_ ilog("snapshot plugin: plugin_initialize() end"); } FC_LOG_AND_RETHROW() } -void snapshot_plugin::plugin_startup() {} - -void snapshot_plugin::plugin_shutdown() {} - static void create_snapshot( const graphene::chain::database& db, const fc::path& dest ) { ilog("snapshot plugin: creating snapshot"); diff --git a/libraries/plugins/template_plugin/include/graphene/template_plugin/template_plugin.hpp b/libraries/plugins/template_plugin/include/graphene/template_plugin/template_plugin.hpp index 168dac42bf..12d4ae4d1e 100644 --- a/libraries/plugins/template_plugin/include/graphene/template_plugin/template_plugin.hpp +++ b/libraries/plugins/template_plugin/include/graphene/template_plugin/template_plugin.hpp @@ -52,18 +52,21 @@ namespace detail class template_plugin : public graphene::app::plugin { public: - template_plugin(); - virtual ~template_plugin(); + template_plugin(graphene::app::application& app); + ~template_plugin() override; std::string plugin_name()const override; std::string plugin_description()const override; - virtual void plugin_set_program_options( + void plugin_set_program_options( boost::program_options::options_description& cli, boost::program_options::options_description& cfg) override; - virtual void plugin_initialize(const boost::program_options::variables_map& options) override; - virtual void plugin_startup() override; + void plugin_initialize(const boost::program_options::variables_map& options) override; + void plugin_startup() override; + void plugin_shutdown() override; friend class detail::template_plugin_impl; + private: + void cleanup(); std::unique_ptr my; }; diff --git a/libraries/plugins/template_plugin/template_plugin.cpp b/libraries/plugins/template_plugin/template_plugin.cpp index 7c2cfbc831..3f96a79081 100644 --- a/libraries/plugins/template_plugin/template_plugin.cpp +++ b/libraries/plugins/template_plugin/template_plugin.cpp @@ -37,7 +37,7 @@ class template_plugin_impl { } virtual ~template_plugin_impl(); - void onBlock( const signed_block& b ); + void on_block( const signed_block& b ); graphene::chain::database& database() { @@ -52,25 +52,28 @@ class template_plugin_impl }; -void template_plugin_impl::onBlock( const signed_block& b ) +void template_plugin_impl::on_block( const signed_block& b ) { wdump((b.block_num())); } template_plugin_impl::~template_plugin_impl() { - return; + // Put the real code here } } // end namespace detail -template_plugin::template_plugin() : - my( new detail::template_plugin_impl(*this) ) +template_plugin::template_plugin(graphene::app::application& app) : + plugin(app), + my( std::make_unique(*this) ) { + // Add needed code here } template_plugin::~template_plugin() { + cleanup(); } std::string template_plugin::plugin_name()const @@ -96,7 +99,7 @@ void template_plugin::plugin_set_program_options( void template_plugin::plugin_initialize(const boost::program_options::variables_map& options) { database().applied_block.connect( [&]( const signed_block& b) { - my->onBlock(b); + my->on_block(b); } ); if (options.count("template_plugin") > 0) { @@ -109,4 +112,15 @@ void template_plugin::plugin_startup() ilog("template_plugin: plugin_startup() begin"); } +void template_plugin::plugin_shutdown() +{ + ilog("template_plugin: plugin_shutdown() begin"); + cleanup(); +} + +void template_plugin::cleanup() +{ + // Add cleanup code here +} + } } diff --git a/libraries/plugins/witness/include/graphene/witness/witness.hpp b/libraries/plugins/witness/include/graphene/witness/witness.hpp index 8f7a8b61de..40c4fe87c8 100644 --- a/libraries/plugins/witness/include/graphene/witness/witness.hpp +++ b/libraries/plugins/witness/include/graphene/witness/witness.hpp @@ -49,11 +49,12 @@ namespace block_production_condition class witness_plugin : public graphene::app::plugin { public: - ~witness_plugin() { stop_block_production(); } + witness_plugin(graphene::app::application& app) : graphene::app::plugin(app) {} + ~witness_plugin() override { cleanup(); } std::string plugin_name()const override; - virtual void plugin_set_program_options( + void plugin_set_program_options( boost::program_options::options_description &command_line_options, boost::program_options::options_description &config_file_options ) override; @@ -61,14 +62,16 @@ class witness_plugin : public graphene::app::plugin { void set_block_production(bool allow) { _production_enabled = allow; } void stop_block_production(); - virtual void plugin_initialize( const boost::program_options::variables_map& options ) override; - virtual void plugin_startup() override; - virtual void plugin_shutdown() override; + void plugin_initialize( const boost::program_options::variables_map& options ) override; + void plugin_startup() override; + void plugin_shutdown() override { cleanup(); } inline const fc::flat_map< chain::witness_id_type, fc::optional >& get_witness_key_cache() { return _witness_key_cache; } private: + void cleanup() { stop_block_production(); } + void schedule_production_loop(); block_production_condition::block_production_condition_enum block_production_loop(); block_production_condition::block_production_condition_enum maybe_produce_block( fc::limited_mutable_variant_object& capture ); diff --git a/libraries/plugins/witness/witness.cpp b/libraries/plugins/witness/witness.cpp index 9cf7df428b..1d9ad36c9e 100644 --- a/libraries/plugins/witness/witness.cpp +++ b/libraries/plugins/witness/witness.cpp @@ -195,11 +195,6 @@ void witness_plugin::plugin_startup() ilog("witness plugin: plugin_startup() end"); } FC_CAPTURE_AND_RETHROW() } -void witness_plugin::plugin_shutdown() -{ - stop_block_production(); -} - void witness_plugin::stop_block_production() { _shutting_down = true; diff --git a/programs/witness_node/main.cpp b/programs/witness_node/main.cpp index b2dd6d77d6..b0ca998e20 100644 --- a/programs/witness_node/main.cpp +++ b/programs/witness_node/main.cpp @@ -52,17 +52,16 @@ #include #ifdef WIN32 -# include +# include #else # include #endif -using namespace graphene; namespace bpo = boost::program_options; int main(int argc, char** argv) { fc::print_stacktrace_on_segfault(); - auto node = std::make_unique(); + auto node = std::make_unique(); fc::oexception unhandled_exception; try { bpo::options_description app_options("BitShares Witness Node"); @@ -78,9 +77,11 @@ int main(int argc, char** argv) { "Space-separated list of plugins to activate") ("ignore-api-helper-indexes-warning", "Do not exit if api_helper_indexes plugin is not enabled."); - bpo::variables_map options; + auto sharable_options = std::make_shared(); + auto& options = *sharable_options; - bpo::options_description cli, cfg; + bpo::options_description cli; + bpo::options_description cfg; node->set_program_options(cli, cfg); cfg_options.add(cfg); @@ -89,47 +90,50 @@ int main(int argc, char** argv) { "Space-separated list of plugins to activate") ("ignore-api-helper-indexes-warning", "Do not exit if api_helper_indexes plugin is not enabled."); - auto witness_plug = node->register_plugin(); - auto debug_witness_plug = node->register_plugin(); - auto history_plug = node->register_plugin(); - auto elasticsearch_plug = node->register_plugin(); - auto market_history_plug = node->register_plugin(); - auto delayed_plug = node->register_plugin(); - auto snapshot_plug = node->register_plugin(); - auto es_objects_plug = node->register_plugin(); - auto grouped_orders_plug = node->register_plugin(); - auto api_helper_indexes_plug = node->register_plugin(); - auto custom_operations_plug = node->register_plugin(); + node->register_plugin(); + node->register_plugin(); + node->register_plugin(); + node->register_plugin(); + node->register_plugin(); + node->register_plugin(); + node->register_plugin(); + node->register_plugin(); + node->register_plugin(); + node->register_plugin(); + node->register_plugin(); // add plugin options to config try { - bpo::options_description cli, cfg; - node->set_program_options(cli, cfg); - app_options.add(cli); - cfg_options.add(cfg); + bpo::options_description tmp_cli; + bpo::options_description tmp_cfg; + node->set_program_options(tmp_cli, tmp_cfg); + app_options.add(tmp_cli); + cfg_options.add(tmp_cfg); bpo::store(bpo::parse_command_line(argc, argv, app_options), options); } catch (const boost::program_options::error& e) { std::cerr << "Error parsing command line: " << e.what() << "\n"; - return 1; + return EXIT_FAILURE; } if( options.count("version") > 0 ) { std::cout << "Version: " << graphene::utilities::git_revision_description << "\n"; std::cout << "SHA: " << graphene::utilities::git_revision_sha << "\n"; - std::cout << "Timestamp: " << fc::get_approximate_relative_time_string(fc::time_point_sec(graphene::utilities::git_revision_unix_timestamp)) << "\n"; + std::cout << "Timestamp: " << fc::get_approximate_relative_time_string(fc::time_point_sec( + graphene::utilities::git_revision_unix_timestamp)) << "\n"; std::cout << "SSL: " << OPENSSL_VERSION_TEXT << "\n"; std::cout << "Boost: " << boost::replace_all_copy(std::string(BOOST_LIB_VERSION), "_", ".") << "\n"; - std::cout << "Websocket++: " << websocketpp::major_version << "." << websocketpp::minor_version << "." << websocketpp::patch_version << "\n"; - return 0; + std::cout << "Websocket++: " << websocketpp::major_version << "." << websocketpp::minor_version + << "." << websocketpp::patch_version << "\n"; + return EXIT_SUCCESS; } if( options.count("help") > 0 ) { std::cout << app_options << "\n"; - return 0; + return EXIT_SUCCESS; } fc::path data_dir; @@ -139,14 +143,14 @@ int main(int argc, char** argv) { if( data_dir.is_relative() ) data_dir = fc::current_path() / data_dir; } - app::load_configuration_options(data_dir, cfg_options, options); + graphene::app::load_configuration_options(data_dir, cfg_options, options); std::set plugins; boost::split(plugins, options.at("plugins").as(), [](char c){return c == ' ';}); if( plugins.count("account_history") > 0 && plugins.count("elasticsearch") > 0 ) { std::cerr << "Plugin conflict: Cannot load both account_history plugin and elasticsearch plugin\n"; - return 1; + return EXIT_FAILURE; } if( plugins.count("api_helper_indexes") == 0 && options.count("ignore-api-helper-indexes-warning") == 0 @@ -155,7 +159,7 @@ int main(int argc, char** argv) { std::cerr << "\nIf this is an API node, please enable api_helper_indexes plugin." "\nIf this is not an API node, please start with \"--ignore-api-helper-indexes-warning\"" " or enable it in config.ini file.\n\n"; - return 1; + return EXIT_FAILURE; } std::for_each(plugins.begin(), plugins.end(), [&node](const std::string& plug) mutable { @@ -166,30 +170,34 @@ int main(int argc, char** argv) { bpo::notify(options); - node->initialize(data_dir, options); - node->initialize_plugins( options ); + node->initialize(data_dir, sharable_options); node->startup(); - node->startup_plugins(); fc::promise::ptr exit_promise = fc::promise::create("UNIX Signal Handler"); - fc::set_signal_handler([&exit_promise](int signal) { - elog( "Caught SIGINT attempting to exit cleanly" ); - exit_promise->set_value(signal); + fc::set_signal_handler([&exit_promise](int the_signal) { + elog( "Caught SIGINT, attempting to exit cleanly" ); + exit_promise->set_value(the_signal); }, SIGINT); - fc::set_signal_handler([&exit_promise](int signal) { - elog( "Caught SIGTERM attempting to exit cleanly" ); - exit_promise->set_value(signal); + fc::set_signal_handler([&exit_promise](int the_signal) { + elog( "Caught SIGTERM, attempting to exit cleanly" ); + exit_promise->set_value(the_signal); }, SIGTERM); +#ifdef SIGQUIT + fc::set_signal_handler( [&exit_promise](int the_signal) { + elog( "Caught SIGQUIT, attempting to exit cleanly" ); + exit_promise->set_value(the_signal); + }, SIGQUIT ); +#endif + ilog("Started BitShares node on a chain with ${h} blocks.", ("h", node->chain_database()->head_block_num())); ilog("Chain ID is ${id}", ("id", node->chain_database()->get_chain_id()) ); - int signal = exit_promise->wait(); - ilog("Exiting from signal ${n}", ("n", signal)); - node->shutdown_plugins(); + auto caught_signal = exit_promise->wait(); + ilog("Exiting from signal ${n}", ("n", caught_signal)); node->shutdown(); node.reset(); return EXIT_SUCCESS; From 81208392137e0eb877ed35440c22d609d149776f Mon Sep 17 00:00:00 2001 From: abitmore Date: Sun, 28 Mar 2021 10:34:27 +0000 Subject: [PATCH 09/31] Update tests to adapt the plugin framework changes --- tests/app/main.cpp | 16 ++++++----- tests/cli/main.cpp | 11 +++----- tests/common/database_fixture.cpp | 44 ++++++++++--------------------- tests/common/database_fixture.hpp | 6 ++--- tests/tests/operation_tests2.cpp | 1 - 5 files changed, 30 insertions(+), 48 deletions(-) diff --git a/tests/app/main.cpp b/tests/app/main.cpp index 27291dee98..9a91139b67 100644 --- a/tests/app/main.cpp +++ b/tests/app/main.cpp @@ -246,12 +246,12 @@ BOOST_AUTO_TEST_CASE( two_node_network ) app1.register_plugin< graphene::market_history::market_history_plugin >(); app1.register_plugin< graphene::witness_plugin::witness_plugin >(); app1.register_plugin< graphene::grouped_orders::grouped_orders_plugin>(); - app1.startup_plugins(); - boost::program_options::variables_map cfg; + auto sharable_cfg = std::make_shared(); + auto& cfg = *sharable_cfg; cfg.emplace("p2p-endpoint", boost::program_options::variable_value(app1_p2p_endpoint_str, false)); cfg.emplace("genesis-json", boost::program_options::variable_value(genesis_file, false)); cfg.emplace("seed-nodes", boost::program_options::variable_value(string("[]"), false)); - app1.initialize(app_dir.path(), cfg); + app1.initialize(app_dir.path(), sharable_cfg); BOOST_TEST_MESSAGE( "Starting app1 and waiting" ); app1.startup(); @@ -271,11 +271,11 @@ BOOST_AUTO_TEST_CASE( two_node_network ) app2.register_plugin< graphene::market_history::market_history_plugin >(); app2.register_plugin< graphene::witness_plugin::witness_plugin >(); app2.register_plugin< graphene::grouped_orders::grouped_orders_plugin>(); - app2.startup_plugins(); - boost::program_options::variables_map cfg2; + auto sharable_cfg2 = std::make_shared(); + auto& cfg2 = *sharable_cfg2; cfg2.emplace("genesis-json", boost::program_options::variable_value(genesis_file, false)); cfg2.emplace("seed-nodes", boost::program_options::variable_value(app2_seed_nodes_str, false)); - app2.initialize(app2_dir.path(), cfg2); + app2.initialize(app2_dir.path(), sharable_cfg2); BOOST_TEST_MESSAGE( "Starting app2 and waiting for connection" ); app2.startup(); @@ -396,10 +396,12 @@ BOOST_AUTO_TEST_CASE(application_impl_breakout) { class test_impl : public graphene::app::detail::application_impl { // override the constructor, just to test that we can public: - test_impl() : application_impl(nullptr) {} + test_impl() : my_app(),application_impl(my_app) {} bool has_item(const net::item_id& id) override { return true; } + private: + graphene::app::application my_app; }; test_impl impl; diff --git a/tests/cli/main.cpp b/tests/cli/main.cpp index 9626c5ecd2..6032d26449 100644 --- a/tests/cli/main.cpp +++ b/tests/cli/main.cpp @@ -96,7 +96,7 @@ using std::cerr; /// @returns the application object ////////// std::shared_ptr start_application(fc::temp_directory& app_dir, int& server_port_number) { - std::shared_ptr app1(new graphene::app::application{}); + auto app1 = std::make_shared(); app1->register_plugin(true); app1->register_plugin< graphene::market_history::market_history_plugin >(true); @@ -104,8 +104,8 @@ std::shared_ptr start_application(fc::temp_directory app1->register_plugin< graphene::api_helper_indexes::api_helper_indexes>(true); app1->register_plugin(true); - app1->startup_plugins(); - boost::program_options::variables_map cfg; + auto sharable_cfg = std::make_shared(); + auto& cfg = *sharable_cfg; #ifdef _WIN32 sockInit(); #endif @@ -117,10 +117,7 @@ std::shared_ptr start_application(fc::temp_directory cfg.emplace("genesis-json", boost::program_options::variable_value(create_genesis_file(app_dir), false)); cfg.emplace("seed-nodes", boost::program_options::variable_value(string("[]"), false)); cfg.emplace("custom-operations-start-block", boost::program_options::variable_value(uint32_t(1), false)); - app1->initialize(app_dir.path(), cfg); - - app1->initialize_plugins(cfg); - app1->startup_plugins(); + app1->initialize(app_dir.path(), sharable_cfg); app1->startup(); diff --git a/tests/common/database_fixture.cpp b/tests/common/database_fixture.cpp index 19a7b9bc5f..ef30b1d302 100644 --- a/tests/common/database_fixture.cpp +++ b/tests/common/database_fixture.cpp @@ -178,9 +178,11 @@ void database_fixture_base::init_genesis( database_fixture_base& fixture ) // TODO add initial UIA's; add initial short positions; test non-zero accumulated_fees } -boost::program_options::variables_map database_fixture_base::init_options( database_fixture_base& fixture ) +std::shared_ptr database_fixture_base::init_options( + database_fixture_base& fixture ) { - boost::program_options::variables_map options; + auto sharable_options = std::make_shared(); + auto& options = *sharable_options; set_option( options, "seed-nodes", std::string("[]") ); /** * Test specific settings @@ -340,7 +342,7 @@ boost::program_options::variables_map database_fixture_base::init_options( datab if(fixture.current_test_name == "elasticsearch_account_history" || fixture.current_test_name == "elasticsearch_suite" || fixture.current_test_name == "elasticsearch_history_api") { - auto esplugin = fixture.app.register_plugin(true); + fixture.app.register_plugin(true); options.insert(std::make_pair("elasticsearch-node-url", boost::program_options::variable_value(GRAPHENE_TESTING_ES_URL, false))); @@ -363,19 +365,14 @@ boost::program_options::variables_map database_fixture_base::init_options( datab BOOST_TEST_MESSAGE( string("ES index prefix is ") + fixture.es_index_prefix ); options.insert(std::make_pair("elasticsearch-index-prefix", boost::program_options::variable_value(fixture.es_index_prefix, false))); - - esplugin->plugin_initialize(options); - esplugin->plugin_startup(); } else if( fixture.current_suite_name != "performance_tests" ) { - auto ahplugin = fixture.app.register_plugin(true); - ahplugin->plugin_initialize(options); - ahplugin->plugin_startup(); + fixture.app.register_plugin(true); } if(fixture.current_test_name == "elasticsearch_objects" || fixture.current_test_name == "elasticsearch_suite") { - auto esobjects_plugin = fixture.app.register_plugin(true); + fixture.app.register_plugin(true); options.insert(std::make_pair("es-objects-elasticsearch-url", boost::program_options::variable_value(GRAPHENE_TESTING_ES_URL, false))); @@ -400,9 +397,6 @@ boost::program_options::variables_map database_fixture_base::init_options( datab BOOST_TEST_MESSAGE( string("ES_OBJ index prefix is ") + fixture.es_obj_index_prefix ); options.insert(std::make_pair("es-objects-index-prefix", boost::program_options::variable_value(fixture.es_obj_index_prefix, false))); - - esobjects_plugin->plugin_initialize(options); - esobjects_plugin->plugin_startup(); } if( fixture.current_test_name == "asset_in_collateral" @@ -411,32 +405,22 @@ boost::program_options::variables_map database_fixture_base::init_options( datab || fixture.current_suite_name == "database_api_tests" || fixture.current_suite_name == "api_limit_tests" ) { - auto ahiplugin = fixture.app.register_plugin(true); - ahiplugin->plugin_initialize(options); - ahiplugin->plugin_startup(); + fixture.app.register_plugin(true); } if(fixture.current_test_name == "custom_operations_account_storage_map_test" || fixture.current_test_name == "custom_operations_account_storage_list_test") { - auto custom_operations_plugin = - fixture.app.register_plugin(true); - options.insert(std::make_pair("custom-operations-start-block", boost::program_options::variable_value(uint32_t(1), false))); - custom_operations_plugin->plugin_initialize(options); - custom_operations_plugin->plugin_startup(); + fixture.app.register_plugin(true); + options.insert(std::make_pair("custom-operations-start-block", + boost::program_options::variable_value(uint32_t(1), false))); } options.insert(std::make_pair("bucket-size", boost::program_options::variable_value(string("[15]"),false))); - auto mhplugin = fixture.app.register_plugin(true); - auto goplugin = fixture.app.register_plugin(true); - - mhplugin->plugin_initialize(options); - goplugin->plugin_initialize(options); - - mhplugin->plugin_startup(); - goplugin->plugin_startup(); + fixture.app.register_plugin(true); + fixture.app.register_plugin(true); - return options; + return sharable_options; } void database_fixture_base::vote_for_committee_and_witnesses(uint16_t num_committee, uint16_t num_witness) diff --git a/tests/common/database_fixture.hpp b/tests/common/database_fixture.hpp index 339388d123..5c9f746af4 100644 --- a/tests/common/database_fixture.hpp +++ b/tests/common/database_fixture.hpp @@ -233,7 +233,7 @@ struct database_fixture_base { } static void init_genesis( database_fixture_base& fixture ); - static boost::program_options::variables_map init_options( database_fixture_base& fixture ); + static std::shared_ptr init_options( database_fixture_base& fixture ); static fc::ecc::private_key generate_private_key(string seed); string generate_anon_acct_name(); @@ -516,8 +516,8 @@ struct database_fixture_init : database_fixture_base { fc::create_directories( fixture.data_dir.path() ); F::init_genesis( fixture ); fc::json::save_to_file( fixture.genesis_state, fixture.data_dir.path() / "genesis.json" ); - boost::program_options::variables_map options = F::init_options( fixture ); - set_option( options, "genesis-json", boost::filesystem::path(fixture.data_dir.path() / "genesis.json") ); + auto options = F::init_options( fixture ); + set_option( *options, "genesis-json", boost::filesystem::path(fixture.data_dir.path() / "genesis.json") ); fixture.app.initialize( fixture.data_dir.path(), options ); fixture.app.startup(); diff --git a/tests/tests/operation_tests2.cpp b/tests/tests/operation_tests2.cpp index e99182f02e..1e97b41207 100644 --- a/tests/tests/operation_tests2.cpp +++ b/tests/tests/operation_tests2.cpp @@ -829,7 +829,6 @@ BOOST_AUTO_TEST_CASE( witness_create ) generate_block(skip); auto wtplugin = app.register_plugin(); - wtplugin->plugin_set_app(&app); boost::program_options::variables_map options; // init witness key cahce From 5db63f5fe998b3019074b6e0ab924a75b8a5ec94 Mon Sep 17 00:00:00 2001 From: abitmore Date: Mon, 29 Mar 2021 08:28:08 +0000 Subject: [PATCH 10/31] Remove application::shutdown() Since it's always called before destruction, it's good to just cleanup everything in the destructor. --- libraries/app/application.cpp | 5 ----- libraries/app/application_impl.hxx | 2 +- libraries/app/include/graphene/app/application.hpp | 1 - programs/witness_node/main.cpp | 4 ---- tests/cli/main.cpp | 7 ------- 5 files changed, 1 insertion(+), 18 deletions(-) diff --git a/libraries/app/application.cpp b/libraries/app/application.cpp index 827c5c4144..dfeaa9dcdb 100644 --- a/libraries/app/application.cpp +++ b/libraries/app/application.cpp @@ -1258,11 +1258,6 @@ void application::add_available_plugin(std::shared_ptradd_available_plugin(p); } -void application::shutdown() -{ - my->shutdown(); -} - const application_options& application::get_options() { return my->_app_options; diff --git a/libraries/app/application_impl.hxx b/libraries/app/application_impl.hxx index e95e7b6ed1..b344fdb8ee 100644 --- a/libraries/app/application_impl.hxx +++ b/libraries/app/application_impl.hxx @@ -43,7 +43,6 @@ class application_impl : public net::node_delegate void initialize(const fc::path& data_dir, std::shared_ptr options); void startup(); - void shutdown(); fc::optional< api_access_info > get_api_access_info(const string& username)const; @@ -209,6 +208,7 @@ class application_impl : public net::node_delegate bool _is_finished_syncing = false; private: + void shutdown(); void initialize_plugins(); void startup_plugins(); void shutdown_plugins(); diff --git a/libraries/app/include/graphene/app/application.hpp b/libraries/app/include/graphene/app/application.hpp index 4ccd8b031d..3c8c63bd10 100644 --- a/libraries/app/include/graphene/app/application.hpp +++ b/libraries/app/include/graphene/app/application.hpp @@ -87,7 +87,6 @@ namespace graphene { namespace app { boost::program_options::options_description& configuration_file_options)const; void initialize(const fc::path& data_dir, std::shared_ptr options); void startup(); - void shutdown(); template std::shared_ptr register_plugin(bool auto_load = false) { diff --git a/programs/witness_node/main.cpp b/programs/witness_node/main.cpp index b0ca998e20..846d8cd71e 100644 --- a/programs/witness_node/main.cpp +++ b/programs/witness_node/main.cpp @@ -198,8 +198,6 @@ int main(int argc, char** argv) { auto caught_signal = exit_promise->wait(); ilog("Exiting from signal ${n}", ("n", caught_signal)); - node->shutdown(); - node.reset(); return EXIT_SUCCESS; } catch( const fc::exception& e ) { // deleting the node can yield, so do this outside the exception handler @@ -209,8 +207,6 @@ int main(int argc, char** argv) { if (unhandled_exception) { elog("Exiting with error:\n${e}", ("e", unhandled_exception->to_detail_string())); - node->shutdown(); - node.reset(); return EXIT_FAILURE; } } diff --git a/tests/cli/main.cpp b/tests/cli/main.cpp index 6032d26449..11e30391fd 100644 --- a/tests/cli/main.cpp +++ b/tests/cli/main.cpp @@ -314,7 +314,6 @@ struct cli_fixture ~cli_fixture() { BOOST_TEST_MESSAGE("Cleanup cli_wallet::boost_fixture_test_case"); - app1->shutdown(); #ifdef _WIN32 sockQuit(); #endif @@ -1497,8 +1496,6 @@ BOOST_AUTO_TEST_CASE( cli_multisig_transaction ) edump((e.to_detail_string())); throw; } - app1->shutdown(); - app1.reset(); } graphene::wallet::plain_keys decrypt_keys( const std::string& password, const vector& cipher_keys ) @@ -1731,8 +1728,6 @@ BOOST_AUTO_TEST_CASE( cli_create_htlc ) edump((e.to_detail_string())); throw; } - app1->shutdown(); - app1.reset(); } static string encapsulate( const graphene::wallet::signed_message& msg ) @@ -2308,6 +2303,4 @@ BOOST_AUTO_TEST_CASE( cli_create_htlc_bsip64 ) edump((e.to_detail_string())); throw; } - app1->shutdown(); - app1.reset(); } From d4441b05176ed69ba94a0de63de60e654dde543b Mon Sep 17 00:00:00 2001 From: abitmore Date: Mon, 29 Mar 2021 08:49:25 +0000 Subject: [PATCH 11/31] Add option in app to enable or disable P2P network --- libraries/app/application.cpp | 38 ++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/libraries/app/application.cpp b/libraries/app/application.cpp index dfeaa9dcdb..aee0716864 100644 --- a/libraries/app/application.cpp +++ b/libraries/app/application.cpp @@ -410,13 +410,13 @@ void application_impl::startup() auto initial_state = [this] { ilog("Initializing database..."); - if( _options->count("genesis-json") ) + if( _options->count("genesis-json") > 0 ) { std::string genesis_str; fc::read_file_contents( _options->at("genesis-json").as(), genesis_str ); graphene::chain::genesis_state_type genesis = fc::json::from_string( genesis_str ).as( 20 ); bool modified_genesis = false; - if( _options->count("genesis-timestamp") ) + if( _options->count("genesis-timestamp") > 0 ) { genesis.initial_timestamp = fc::time_point_sec( fc::time_point::now() ) + genesis.initial_parameters.block_interval @@ -430,7 +430,7 @@ void application_impl::startup() ("timestamp", genesis.initial_timestamp.to_iso_string()) ); } - if( _options->count("dbg-init-key") ) + if( _options->count("dbg-init-key") > 0 ) { std::string init_key = _options->at( "dbg-init-key" ).as(); FC_ASSERT( genesis.initial_witness_candidates.size() >= genesis.initial_active_witnesses ); @@ -460,11 +460,11 @@ void application_impl::startup() } }; - if( _options->count("resync-blockchain") ) + if( _options->count("resync-blockchain") > 0 ) _chain_db->wipe(_data_dir / "blockchain", true); flat_map loaded_checkpoints; - if( _options->count("checkpoint") ) + if( _options->count("checkpoint") > 0 ) { auto cps = _options->at("checkpoint").as>(); loaded_checkpoints.reserve( cps.size() ); @@ -476,23 +476,23 @@ void application_impl::startup() } _chain_db->add_checkpoints( loaded_checkpoints ); - if( _options->count("enable-standby-votes-tracking") ) + if( _options->count("enable-standby-votes-tracking") > 0 ) { _chain_db->enable_standby_votes_tracking( _options->at("enable-standby-votes-tracking").as() ); } - if( _options->count("replay-blockchain") || _options->count("revalidate-blockchain") ) + if( _options->count("replay-blockchain") > 0 || _options->count("revalidate-blockchain") > 0 ) _chain_db->wipe( _data_dir / "blockchain", false ); try { // these flags are used in open() only, i. e. during replay uint32_t skip; - if( _options->count("revalidate-blockchain") ) // see also handle_block() + if( _options->count("revalidate-blockchain") > 0 ) // see also handle_block() { if( !loaded_checkpoints.empty() ) wlog( "Warning - revalidate will not validate before last checkpoint" ); - if( _options->count("force-validate") ) + if( _options->count("force-validate") > 0 ) skip = graphene::chain::database::skip_nothing; else skip = graphene::chain::database::skip_transaction_signatures; @@ -518,7 +518,10 @@ void application_impl::startup() startup_plugins(); - if( _active_plugins.find( "delayed_node" ) == _active_plugins.end() ) + bool enable_p2p_network = true; + if( _options->count("enable-p2p-network") > 0 ) + enable_p2p_network = _options->at("enable-p2p-network").as(); + if( enable_p2p_network && _active_plugins.find( "delayed_node" ) == _active_plugins.end() ) reset_p2p_node(_data_dir); reset_websocket_server(); @@ -1077,6 +1080,9 @@ void application::set_program_options(boost::program_options::options_descriptio boost::program_options::options_description& configuration_file_options) const { configuration_file_options.add_options() + ("enable-p2p-network", bpo::value()->implicit_value(true), + "Whether to enable P2P network. Note: if delayed_node plugin is enabled, " + "this option will be ignored and P2P network will always be disabled.") ("p2p-endpoint", bpo::value(), "Endpoint for P2P node to listen on") ("seed-node,s", bpo::value>()->composing(), "P2P nodes to connect to on startup (may specify multiple times)") @@ -1088,15 +1094,18 @@ void application::set_program_options(boost::program_options::options_descriptio "Endpoint for websocket RPC to listen on") ("rpc-tls-endpoint", bpo::value()->implicit_value("127.0.0.1:8089"), "Endpoint for TLS websocket RPC to listen on") - ("server-pem,p", bpo::value()->implicit_value("server.pem"), "The TLS certificate file for this server") + ("server-pem,p", bpo::value()->implicit_value("server.pem"), + "The TLS certificate file for this server") ("server-pem-password,P", bpo::value()->implicit_value(""), "Password for this certificate") ("proxy-forwarded-for-header", bpo::value()->implicit_value("X-Forwarded-For-Client"), "A HTTP header similar to X-Forwarded-For (XFF), used by the RPC server to extract clients' address info, " "usually added by a trusted reverse proxy") ("genesis-json", bpo::value(), "File to read Genesis State from") - ("dbg-init-key", bpo::value(), "Block signing key to use for init witnesses, overrides genesis file") + ("dbg-init-key", bpo::value(), + "Block signing key to use for init witnesses, overrides genesis file, for debug") ("api-access", bpo::value(), "JSON file specifying API permissions") - ("io-threads", bpo::value()->implicit_value(0), "Number of IO threads, default to 0 for auto-configuration") + ("io-threads", bpo::value()->implicit_value(0), + "Number of IO threads, default to 0 for auto-configuration") ("enable-subscribe-to-all", bpo::value()->implicit_value(true), "Whether allow API clients to subscribe to universal object creation and removal events") ("enable-standby-votes-tracking", bpo::value()->implicit_value(true), @@ -1156,7 +1165,8 @@ void application::set_program_options(boost::program_options::options_descriptio "For database_api_impl::get_trade_history_by_sequence to set max limit value") ("api-limit-get-withdraw-permissions-by-giver",boost::program_options::value()->default_value(101), "For database_api_impl::get_withdraw_permissions_by_giver to set max limit value") - ("api-limit-get-withdraw-permissions-by-recipient",boost::program_options::value()->default_value(101), + ("api-limit-get-withdraw-permissions-by-recipient", + boost::program_options::value()->default_value(101), "For database_api_impl::get_withdraw_permissions_by_recipient to set max limit value") ("api-limit-get-tickets", boost::program_options::value()->default_value(101), "Set maximum limit value for database APIs which query for tickets") From 290b3d09afb5f7db07c887e0cea1327c0a4f691f Mon Sep 17 00:00:00 2001 From: abitmore Date: Mon, 29 Mar 2021 09:21:57 +0000 Subject: [PATCH 12/31] Refactor application_impl::startup() Split it into several functions. --- libraries/app/application.cpp | 49 ++++++++++--------- libraries/app/application_impl.hxx | 20 +++++--- libraries/chain/genesis_state.cpp | 9 ++++ .../include/graphene/chain/genesis_state.hpp | 4 ++ 4 files changed, 53 insertions(+), 29 deletions(-) diff --git a/libraries/app/application.cpp b/libraries/app/application.cpp index aee0716864..6bac4fe328 100644 --- a/libraries/app/application.cpp +++ b/libraries/app/application.cpp @@ -233,14 +233,6 @@ void application_impl::reset_websocket_tls_server() _websocket_tls_server->start_accept(); } FC_CAPTURE_AND_RETHROW() } -void application_impl::set_dbg_init_key( graphene::chain::genesis_state_type& genesis, const std::string& init_key ) -{ - flat_set< std::string > initial_witness_names; - public_key_type init_pubkey( init_key ); - for( uint64_t i=0; i options) { _data_dir = data_dir; @@ -404,17 +396,15 @@ void application_impl::set_api_limit() { } } -void application_impl::startup() -{ try { - fc::create_directories(_data_dir / "blockchain"); - - auto initial_state = [this] { +graphene::chain::genesis_state_type application_impl::initialize_genesis_state() const +{ + try { ilog("Initializing database..."); if( _options->count("genesis-json") > 0 ) { std::string genesis_str; fc::read_file_contents( _options->at("genesis-json").as(), genesis_str ); - graphene::chain::genesis_state_type genesis = fc::json::from_string( genesis_str ).as( 20 ); + auto genesis = fc::json::from_string( genesis_str ).as( 20 ); bool modified_genesis = false; if( _options->count("genesis-timestamp") > 0 ) { @@ -434,7 +424,7 @@ void application_impl::startup() { std::string init_key = _options->at( "dbg-init-key" ).as(); FC_ASSERT( genesis.initial_witness_candidates.size() >= genesis.initial_active_witnesses ); - set_dbg_init_key( genesis, init_key ); + genesis.override_witness_signing_keys( init_key ); modified_genesis = true; ilog("Set init witness key to ${init_key}", ("init_key", init_key)); } @@ -442,10 +432,8 @@ void application_impl::startup() { wlog("WARNING: GENESIS WAS MODIFIED, YOUR CHAIN ID MAY BE DIFFERENT"); genesis_str += "BOGUS"; - genesis.initial_chain_id = fc::sha256::hash( genesis_str ); } - else - genesis.initial_chain_id = fc::sha256::hash( genesis_str ); + genesis.initial_chain_id = fc::sha256::hash( genesis_str ); return genesis; } else @@ -458,7 +446,12 @@ void application_impl::startup() genesis.initial_chain_id = fc::sha256::hash( egenesis_json ); return genesis; } - }; + } FC_CAPTURE_AND_RETHROW() +} + +void application_impl::open_chain_database() +{ try { + fc::create_directories(_data_dir / "blockchain"); if( _options->count("resync-blockchain") > 0 ) _chain_db->wipe(_data_dir / "blockchain", true); @@ -506,8 +499,12 @@ void application_impl::startup() graphene::chain::database::skip_tapos_check | graphene::chain::database::skip_witness_schedule_check; - graphene::chain::detail::with_skip_flags( *_chain_db, skip, [this,&initial_state] () { - _chain_db->open( _data_dir / "blockchain", initial_state, GRAPHENE_CURRENT_DB_VERSION ); + auto genesis_loader = [this](){ + return initialize_genesis_state(); + }; + + graphene::chain::detail::with_skip_flags( *_chain_db, skip, [this, &genesis_loader] () { + _chain_db->open( _data_dir / "blockchain", genesis_loader, GRAPHENE_CURRENT_DB_VERSION ); }); } catch( const fc::exception& e ) @@ -515,12 +512,18 @@ void application_impl::startup() elog( "Caught exception ${e} in open(), you might want to force a replay", ("e", e.to_detail_string()) ); throw; } +} FC_LOG_AND_RETHROW() } - startup_plugins(); - +void application_impl::startup() +{ try { bool enable_p2p_network = true; if( _options->count("enable-p2p-network") > 0 ) enable_p2p_network = _options->at("enable-p2p-network").as(); + + open_chain_database(); + + startup_plugins(); + if( enable_p2p_network && _active_plugins.find( "delayed_node" ) == _active_plugins.end() ) reset_p2p_node(_data_dir); diff --git a/libraries/app/application_impl.hxx b/libraries/app/application_impl.hxx index b344fdb8ee..7084f69f31 100644 --- a/libraries/app/application_impl.hxx +++ b/libraries/app/application_impl.hxx @@ -38,7 +38,6 @@ class application_impl : public net::node_delegate void set_block_production(bool producing_blocks); - void set_dbg_init_key( graphene::chain::genesis_state_type& genesis, const std::string& init_key ); void set_api_limit(); void initialize(const fc::path& data_dir, std::shared_ptr options); @@ -192,6 +191,20 @@ class application_impl : public net::node_delegate /// Returns whether a plugin is enabled bool is_plugin_enabled(const string& name) const; + private: + void shutdown(); + + void initialize_plugins(); + void startup_plugins(); + void shutdown_plugins(); + + /// Initialize genesis state. Called by @ref open_chain_database. + graphene::chain::genesis_state_type initialize_genesis_state() const; + /// Open the chain database. Called by @ref startup. + void open_chain_database(); + + friend graphene::app::application; + application& _self; fc::path _data_dir; @@ -207,11 +220,6 @@ class application_impl : public net::node_delegate std::map> _available_plugins; bool _is_finished_syncing = false; - private: - void shutdown(); - void initialize_plugins(); - void startup_plugins(); - void shutdown_plugins(); fc::serial_valve valve; }; diff --git a/libraries/chain/genesis_state.cpp b/libraries/chain/genesis_state.cpp index ae6b015990..78771a8a93 100644 --- a/libraries/chain/genesis_state.cpp +++ b/libraries/chain/genesis_state.cpp @@ -34,6 +34,15 @@ chain_id_type genesis_state_type::compute_chain_id() const return initial_chain_id; } +void genesis_state_type::override_witness_signing_keys( const std::string& new_key ) +{ + public_key_type new_pubkey( new_key ); + for( auto& wit : initial_witness_candidates ) + { + wit.block_signing_key = new_pubkey; + } +} + } } // graphene::chain FC_REFLECT_DERIVED_NO_TYPENAME(graphene::chain::genesis_state_type::initial_account_type, BOOST_PP_SEQ_NIL, diff --git a/libraries/chain/include/graphene/chain/genesis_state.hpp b/libraries/chain/include/graphene/chain/genesis_state.hpp index 98538e824e..391e3e58d4 100644 --- a/libraries/chain/include/graphene/chain/genesis_state.hpp +++ b/libraries/chain/include/graphene/chain/genesis_state.hpp @@ -124,6 +124,10 @@ struct genesis_state_type { * This is the SHA256 serialization of the genesis_state. */ chain_id_type compute_chain_id() const; + + /// Method to override initial witness signing keys for debug + void override_witness_signing_keys( const std::string& new_key ); + }; } } // namespace graphene::chain From c19a676f97a094bd7c975eaa37b4a7faa0dcfbad Mon Sep 17 00:00:00 2001 From: abitmore Date: Mon, 29 Mar 2021 10:37:34 +0000 Subject: [PATCH 13/31] Fix a compiler warning --- tests/app/main.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/app/main.cpp b/tests/app/main.cpp index 9a91139b67..b07eda1736 100644 --- a/tests/app/main.cpp +++ b/tests/app/main.cpp @@ -390,18 +390,18 @@ BOOST_AUTO_TEST_CASE( two_node_network ) } } -// a contrived example to test the breaking out of application_impl to a header file - +/// a contrived example to test the breaking out of application_impl to a header file BOOST_AUTO_TEST_CASE(application_impl_breakout) { + + static graphene::app::application my_app; + class test_impl : public graphene::app::detail::application_impl { // override the constructor, just to test that we can public: - test_impl() : my_app(),application_impl(my_app) {} + test_impl() : application_impl(my_app) {} bool has_item(const net::item_id& id) override { return true; } - private: - graphene::app::application my_app; }; test_impl impl; From 3d6803095eabec643cc0f45e029f754b6b662998 Mon Sep 17 00:00:00 2001 From: abitmore Date: Mon, 29 Mar 2021 10:44:49 +0000 Subject: [PATCH 14/31] Remove redundant 'virtual' declarations --- libraries/app/application_impl.hxx | 39 ++++++++++++++++-------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/libraries/app/application_impl.hxx b/libraries/app/application_impl.hxx index 7084f69f31..7d23ac326a 100644 --- a/libraries/app/application_impl.hxx +++ b/libraries/app/application_impl.hxx @@ -50,7 +50,7 @@ class application_impl : public net::node_delegate /** * If delegate has the item, the network has no need to fetch it. */ - virtual bool has_item(const net::item_id& id) override; + bool has_item(const net::item_id& id) override; /** * @brief allows the application to validate an item prior to broadcasting to peers. @@ -62,10 +62,10 @@ class application_impl : public net::node_delegate * * @throws exception if error validating the item, otherwise the item is safe to broadcast on. */ - virtual bool handle_block(const graphene::net::block_message& blk_msg, bool sync_mode, + bool handle_block(const graphene::net::block_message& blk_msg, bool sync_mode, std::vector& contained_transaction_message_ids) override; - virtual void handle_transaction(const graphene::net::trx_message& transaction_message) override; + void handle_transaction(const graphene::net::trx_message& transaction_message) override; void handle_message(const graphene::net::message& message_to_process) override; @@ -80,16 +80,17 @@ class application_impl : public net::node_delegate * in our blockchain after the last item returned in the result, * or 0 if the result contains the last item in the blockchain */ - virtual std::vector get_block_ids(const std::vector& blockchain_synopsis, - uint32_t& remaining_item_count, - uint32_t limit) override; + std::vector get_block_ids( + const std::vector& blockchain_synopsis, + uint32_t& remaining_item_count, + uint32_t limit) override; /** * Given the hash of the requested data, fetch the body. */ - virtual graphene::net::message get_item(const graphene::net::item_id& id) override; + graphene::net::message get_item(const graphene::net::item_id& id) override; - virtual graphene::chain::chain_id_type get_chain_id()const override; + graphene::chain::chain_id_type get_chain_id()const override; /** * Returns a synopsis of the blockchain used for syncing. This consists of a list of @@ -149,36 +150,38 @@ class application_impl : public net::node_delegate * successfully pushed to the blockchain, so that tells us whether the peer is on a fork or on * the main chain. */ - virtual std::vector get_blockchain_synopsis(const graphene::net::item_hash_t& reference_point, - uint32_t number_of_blocks_after_reference_point) override; + std::vector get_blockchain_synopsis( + const graphene::net::item_hash_t& reference_point, + uint32_t number_of_blocks_after_reference_point) override; /** * Call this after the call to handle_message succeeds. * - * @param item_type the type of the item we're synchronizing, will be the same as item passed to the sync_from() call + * @param item_type the type of the item we're synchronizing, + * will be the same as item passed to the sync_from() call * @param item_count the number of items known to the node that haven't been sent to handle_item() yet. * After `item_count` more calls to handle_item(), the node will be in sync */ - virtual void sync_status(uint32_t item_type, uint32_t item_count) override; + void sync_status(uint32_t item_type, uint32_t item_count) override; /** * Call any time the number of connected peers changes. */ - virtual void connection_count_changed(uint32_t c) override; + void connection_count_changed(uint32_t c) override; - virtual uint32_t get_block_number(const graphene::net::item_hash_t& block_id) override; + uint32_t get_block_number(const graphene::net::item_hash_t& block_id) override; /** * Returns the time a block was produced (if block_id = 0, returns genesis time). * If we don't know about the block, returns time_point_sec::min() */ - virtual fc::time_point_sec get_block_time(const graphene::net::item_hash_t& block_id) override; + fc::time_point_sec get_block_time(const graphene::net::item_hash_t& block_id) override; - virtual graphene::net::item_hash_t get_head_block_id() const override; + graphene::net::item_hash_t get_head_block_id() const override; - virtual uint32_t estimate_last_known_fork_from_git_revision_timestamp(uint32_t unix_timestamp) const override; + uint32_t estimate_last_known_fork_from_git_revision_timestamp(uint32_t unix_timestamp) const override; - virtual void error_encountered(const std::string& message, const fc::oexception& error) override; + void error_encountered(const std::string& message, const fc::oexception& error) override; uint8_t get_current_block_interval_in_seconds() const override; From d135a9c7ae7d02c0aec420e53543a40ac73808f2 Mon Sep 17 00:00:00 2001 From: abitmore Date: Mon, 29 Mar 2021 13:39:31 +0000 Subject: [PATCH 15/31] Pass a shared_ptr of node_delegate to node instead of a raw pointer. By the way, - move simulated_network from node.hpp and node.cpp to tests, - avoid referencing uint160_t, use message_hash_type or ripemd160. --- libraries/app/application.cpp | 9 +- libraries/app/application_impl.hxx | 4 +- .../app/include/graphene/app/application.hpp | 2 +- .../net/include/graphene/net/message.hpp | 4 +- libraries/net/include/graphene/net/node.hpp | 34 +---- libraries/net/node.cpp | 138 ++++++------------ libraries/net/node_impl.hxx | 55 ++++--- tests/common/simulated_network.cpp | 93 ++++++++++++ tests/common/simulated_network.hpp | 56 +++++++ 9 files changed, 244 insertions(+), 151 deletions(-) create mode 100644 tests/common/simulated_network.cpp create mode 100644 tests/common/simulated_network.hpp diff --git a/libraries/app/application.cpp b/libraries/app/application.cpp index 6bac4fe328..5f3df7f67c 100644 --- a/libraries/app/application.cpp +++ b/libraries/app/application.cpp @@ -124,7 +124,7 @@ void application_impl::reset_p2p_node(const fc::path& data_dir) _p2p_network = std::make_shared("BitShares Reference Implementation"); _p2p_network->load_configuration(data_dir / "p2p"); - _p2p_network->set_node_delegate(this); + _p2p_network->set_node_delegate(shared_from_this()); if( _options->count("seed-node") ) { @@ -578,7 +578,7 @@ bool application_impl::has_item(const net::item_id& id) * @throws exception if error validating the item, otherwise the item is safe to broadcast on. */ bool application_impl::handle_block(const graphene::net::block_message& blk_msg, bool sync_mode, - std::vector& contained_transaction_message_ids) + std::vector& contained_transaction_message_ids) { try { auto latency = fc::time_point::now() - blk_msg.block.timestamp; @@ -587,7 +587,8 @@ bool application_impl::handle_block(const graphene::net::block_message& blk_msg, const auto& witness = blk_msg.block.witness(*_chain_db); const auto& witness_account = witness.witness_account(*_chain_db); auto last_irr = _chain_db->get_dynamic_global_properties().last_irreversible_block_num; - ilog("Got block: #${n} ${bid} time: ${t} transaction(s): ${x} latency: ${l} ms from: ${w} irreversible: ${i} (-${d})", + ilog("Got block: #${n} ${bid} time: ${t} transaction(s): ${x} " + "latency: ${l} ms from: ${w} irreversible: ${i} (-${d})", ("t",blk_msg.block.timestamp) ("n", blk_msg.block.block_num()) ("bid", blk_msg.block.id()) @@ -1069,7 +1070,7 @@ void application_impl::set_block_production(bool producing_blocks) namespace graphene { namespace app { application::application() - : my(std::make_unique(*this)) + : my(std::make_shared(*this)) { //nothing else to do } diff --git a/libraries/app/application_impl.hxx b/libraries/app/application_impl.hxx index 7d23ac326a..829a7fdd6f 100644 --- a/libraries/app/application_impl.hxx +++ b/libraries/app/application_impl.hxx @@ -12,7 +12,7 @@ namespace graphene { namespace app { namespace detail { -class application_impl : public net::node_delegate +class application_impl : public net::node_delegate, public std::enable_shared_from_this { public: fc::optional _lock_file; @@ -63,7 +63,7 @@ class application_impl : public net::node_delegate * @throws exception if error validating the item, otherwise the item is safe to broadcast on. */ bool handle_block(const graphene::net::block_message& blk_msg, bool sync_mode, - std::vector& contained_transaction_message_ids) override; + std::vector& contained_transaction_message_ids) override; void handle_transaction(const graphene::net::trx_message& transaction_message) override; diff --git a/libraries/app/include/graphene/app/application.hpp b/libraries/app/include/graphene/app/application.hpp index 3c8c63bd10..6478a97a31 100644 --- a/libraries/app/include/graphene/app/application.hpp +++ b/libraries/app/include/graphene/app/application.hpp @@ -149,7 +149,7 @@ namespace graphene { namespace app { /// Add an available plugin void add_available_plugin( std::shared_ptr p ); - std::unique_ptr my; + std::shared_ptr my; boost::program_options::options_description _cli_options; boost::program_options::options_description _cfg_options; diff --git a/libraries/net/include/graphene/net/message.hpp b/libraries/net/include/graphene/net/message.hpp index 6d254b6120..b6463aaa4a 100644 --- a/libraries/net/include/graphene/net/message.hpp +++ b/libraries/net/include/graphene/net/message.hpp @@ -51,7 +51,7 @@ namespace graphene { namespace net { } }; - typedef fc::uint160_t message_hash_type; + using message_hash_type = fc::ripemd160; /** * Abstracts the process of packing/unpacking a message for a @@ -80,7 +80,7 @@ namespace graphene { namespace net { size = (uint32_t)data.size(); } - fc::uint160_t id()const + message_hash_type id()const { return fc::ripemd160::hash( data.data(), (uint32_t)data.size() ); } diff --git a/libraries/net/include/graphene/net/node.hpp b/libraries/net/include/graphene/net/node.hpp index 0d4d5159d9..7c80d09b1f 100644 --- a/libraries/net/include/graphene/net/node.hpp +++ b/libraries/net/include/graphene/net/node.hpp @@ -29,8 +29,6 @@ #include -#include - namespace graphene { namespace net { using fc::variant_object; @@ -61,7 +59,7 @@ namespace graphene { namespace net { class node_delegate { public: - virtual ~node_delegate(){} + virtual ~node_delegate() = default; /** * If delegate has the item, the network has no need to fetch it. @@ -80,7 +78,7 @@ namespace graphene { namespace net { * safe to broadcast on. */ virtual bool handle_block( const graphene::net::block_message& blk_msg, bool sync_mode, - std::vector& contained_transaction_message_ids ) = 0; + std::vector& contained_transaction_message_ids ) = 0; /** * @brief Called when a new transaction comes in from the network @@ -199,7 +197,7 @@ namespace graphene { namespace net { void close(); - void set_node_delegate( node_delegate* del ); + void set_node_delegate( std::shared_ptr del ); void load_configuration( const fc::path& configuration_directory ); @@ -321,31 +319,7 @@ namespace graphene { namespace net { std::unique_ptr my; }; - class simulated_network : public node - { - public: - ~simulated_network(); - simulated_network(const std::string& user_agent) : node(user_agent) {} - void listen_to_p2p_network() override {} - void connect_to_p2p_network() override {} - void connect_to_endpoint(const fc::ip::endpoint& ep) override {} - - fc::ip::endpoint get_actual_listening_endpoint() const override { return fc::ip::endpoint(); } - - void sync_from(const item_id& current_head_block, const std::vector& hard_fork_block_numbers) override {} - void broadcast(const message& item_to_broadcast) override; - void add_node_delegate(node_delegate* node_delegate_to_add); - - virtual uint32_t get_connection_count() const override { return 8; } - private: - struct node_info; - void message_sender(node_info* destination_node); - std::list network_nodes; - }; - - - typedef std::shared_ptr node_ptr; - typedef std::shared_ptr simulated_network_ptr; + using node_ptr = std::shared_ptr; } } // graphene::net diff --git a/libraries/net/node.cpp b/libraries/net/node.cpp index cdabcf6ba2..812ba66e69 100644 --- a/libraries/net/node.cpp +++ b/libraries/net/node.cpp @@ -146,13 +146,15 @@ namespace graphene { namespace net { // for network performance stats message_propagation_data propagation_data; - fc::uint160_t message_contents_hash; // hash of whatever the message contains (if it's a transaction, this is the transaction id, if it's a block, it's the block_id) + /// hash of whatever the message contains + /// (if it's a transaction, this is the transaction id, if it's a block, it's the block_id) + message_hash_type message_contents_hash; message_info( const message_hash_type& message_hash, const message& message_body, uint32_t block_clock_when_received, const message_propagation_data& propagation_data, - fc::uint160_t message_contents_hash ) : + message_hash_type message_contents_hash ) : message_hash( message_hash ), message_body( message_body ), block_clock_when_received( block_clock_when_received ), @@ -160,15 +162,14 @@ namespace graphene { namespace net { message_contents_hash( message_contents_hash ) {} }; - typedef boost::multi_index_container - < message_info, - bmi::indexed_by< bmi::ordered_unique< bmi::tag, - bmi::member >, - bmi::ordered_non_unique< bmi::tag, - bmi::member >, - bmi::ordered_non_unique< bmi::tag, - bmi::member > > - > message_cache_container; + using message_cache_container = boost::multi_index_container < message_info, + bmi::indexed_by< + bmi::ordered_unique< bmi::tag, + bmi::member >, + bmi::ordered_non_unique< bmi::tag, + bmi::member >, + bmi::ordered_non_unique< bmi::tag, + bmi::member > > >; message_cache_container _message_cache; @@ -179,10 +180,13 @@ namespace graphene { namespace net { block_clock( 0 ) {} void block_accepted(); - void cache_message( const message& message_to_cache, const message_hash_type& hash_of_message_to_cache, - const message_propagation_data& propagation_data, const fc::uint160_t& message_content_hash ); + void cache_message( const message& message_to_cache, + const message_hash_type& hash_of_message_to_cache, + const message_propagation_data& propagation_data, + const message_hash_type& message_content_hash ); message get_message( const message_hash_type& hash_of_message_to_lookup ); - message_propagation_data get_message_propagation_data( const fc::uint160_t& hash_of_message_contents_to_lookup ) const; + message_propagation_data get_message_propagation_data( + const message_hash_type& hash_of_message_contents_to_lookup ) const; size_t size() const { return _message_cache.size(); } }; @@ -190,14 +194,14 @@ namespace graphene { namespace net { { ++block_clock; if( block_clock > cache_duration_in_blocks ) - _message_cache.get().erase(_message_cache.get().begin(), - _message_cache.get().lower_bound(block_clock - cache_duration_in_blocks ) ); + _message_cache.get().erase(_message_cache.get().begin(), + _message_cache.get().lower_bound(block_clock - cache_duration_in_blocks ) ); } void blockchain_tied_message_cache::cache_message( const message& message_to_cache, - const message_hash_type& hash_of_message_to_cache, - const message_propagation_data& propagation_data, - const fc::uint160_t& message_content_hash ) + const message_hash_type& hash_of_message_to_cache, + const message_propagation_data& propagation_data, + const message_hash_type& message_content_hash ) { _message_cache.insert( message_info(hash_of_message_to_cache, message_to_cache, @@ -215,9 +219,10 @@ namespace graphene { namespace net { FC_THROW_EXCEPTION( fc::key_not_found_exception, "Requested message not in cache" ); } - message_propagation_data blockchain_tied_message_cache::get_message_propagation_data( const fc::uint160_t& hash_of_message_contents_to_lookup ) const + message_propagation_data blockchain_tied_message_cache::get_message_propagation_data( + const message_hash_type& hash_of_message_contents_to_lookup ) const { - if( hash_of_message_contents_to_lookup != fc::uint160_t() ) + if( hash_of_message_contents_to_lookup != message_hash_type() ) { message_cache_container::index::type::const_iterator iter = _message_cache.get().find(hash_of_message_contents_to_lookup ); @@ -2658,7 +2663,7 @@ namespace graphene { namespace net { namespace detail { try { - std::vector contained_transaction_message_ids; + std::vector contained_transaction_message_ids; _delegate->handle_block(block_message_to_send, true, contained_transaction_message_ids); ilog("Successfully pushed sync block ${num} (id:${id})", ("num", block_message_to_send.block.block_num()) @@ -2981,11 +2986,13 @@ namespace graphene { namespace net { namespace detail { { if (!_node_is_shutting_down && (!_process_backlog_of_sync_blocks_done.valid() || _process_backlog_of_sync_blocks_done.ready())) - _process_backlog_of_sync_blocks_done = fc::async([=](){ process_backlog_of_sync_blocks(); }, "process_backlog_of_sync_blocks"); + _process_backlog_of_sync_blocks_done = fc::async( [=](){ process_backlog_of_sync_blocks(); }, + "process_backlog_of_sync_blocks" ); } void node_impl::process_block_during_sync( peer_connection* originating_peer, - const graphene::net::block_message& block_message_to_process, const message_hash_type& message_hash ) + const graphene::net::block_message& block_message_to_process, + const message_hash_type& message_hash ) { VERIFY_CORRECT_THREAD(); dlog( "received a sync block from peer ${endpoint}", ("endpoint", originating_peer->get_remote_endpoint() ) ); @@ -2997,12 +3004,13 @@ namespace graphene { namespace net { namespace detail { } void node_impl::process_block_during_normal_operation( peer_connection* originating_peer, - const graphene::net::block_message& block_message_to_process, - const message_hash_type& message_hash ) + const graphene::net::block_message& block_message_to_process, + const message_hash_type& message_hash ) { fc::time_point message_receive_time = fc::time_point::now(); - dlog( "received a block from peer ${endpoint}, passing it to client", ("endpoint", originating_peer->get_remote_endpoint() ) ); + dlog( "received a block from peer ${endpoint}, passing it to client", + ("endpoint", originating_peer->get_remote_endpoint() ) ); std::set peers_to_disconnect; std::string disconnect_reason; fc::oexception disconnect_exception; @@ -3019,7 +3027,7 @@ namespace graphene { namespace net { namespace detail { if (std::find(_most_recent_blocks_accepted.begin(), _most_recent_blocks_accepted.end(), block_message_to_process.block_id) == _most_recent_blocks_accepted.end()) { - std::vector contained_transaction_message_ids; + std::vector contained_transaction_message_ids; _delegate->handle_block(block_message_to_process, false, contained_transaction_message_ids); message_validated_time = fc::time_point::now(); ilog("Successfully pushed block ${num} (id:${id})", @@ -3150,7 +3158,8 @@ namespace graphene { namespace net { namespace detail { for (const peer_connection_ptr& peer : peers_to_disconnect) { - wlog("disconnecting client ${endpoint} because it offered us the rejected block", ("endpoint", peer->get_remote_endpoint())); + wlog("disconnecting client ${endpoint} because it offered us the rejected block", + ("endpoint", peer->get_remote_endpoint())); disconnect_from_peer(peer.get(), disconnect_reason, true, *disconnect_exception); } } @@ -4120,12 +4129,12 @@ namespace graphene { namespace net { namespace detail { } // methods implementing node's public interface - void node_impl::set_node_delegate(node_delegate* del, fc::thread* thread_for_delegate_calls) + void node_impl::set_node_delegate(std::shared_ptr del, fc::thread* thread_for_delegate_calls) { VERIFY_CORRECT_THREAD(); _delegate.reset(); if (del) - _delegate.reset(new statistics_gathering_node_delegate_wrapper(del, thread_for_delegate_calls)); + _delegate = std::make_unique(del, thread_for_delegate_calls); if( _delegate ) _chain_id = del->get_chain_id(); } @@ -4671,7 +4680,7 @@ namespace graphene { namespace net { namespace detail { void node_impl::broadcast( const message& item_to_broadcast, const message_propagation_data& propagation_data ) { VERIFY_CORRECT_THREAD(); - fc::uint160_t hash_of_message_contents; + message_hash_type hash_of_message_contents; if( item_to_broadcast.msg_type.value() == graphene::net::block_message_type ) { graphene::net::block_message block_message_to_broadcast = item_to_broadcast.as(); @@ -4897,7 +4906,7 @@ namespace graphene { namespace net { namespace detail { { } - void node::set_node_delegate( node_delegate* del ) + void node::set_node_delegate( std::shared_ptr del ) { fc::thread* delegate_thread = &fc::thread::current(); INVOKE_IN_IMPL(set_node_delegate, del, delegate_thread); @@ -5044,63 +5053,6 @@ namespace graphene { namespace net { namespace detail { INVOKE_IN_IMPL(close); } - struct simulated_network::node_info - { - node_delegate* delegate; - fc::future message_sender_task_done; - std::queue messages_to_deliver; - node_info(node_delegate* delegate) : delegate(delegate) {} - }; - - simulated_network::~simulated_network() - { - for( node_info* network_node_info : network_nodes ) - { - network_node_info->message_sender_task_done.cancel_and_wait("~simulated_network()"); - delete network_node_info; - } - } - - void simulated_network::message_sender(node_info* destination_node) - { - while (!destination_node->messages_to_deliver.empty()) - { - try - { - const message& message_to_deliver = destination_node->messages_to_deliver.front(); - if (message_to_deliver.msg_type.value() == trx_message_type) - destination_node->delegate->handle_transaction(message_to_deliver.as()); - else if (message_to_deliver.msg_type.value() == block_message_type) - { - std::vector contained_transaction_message_ids; - destination_node->delegate->handle_block(message_to_deliver.as(), false, contained_transaction_message_ids); - } - else - destination_node->delegate->handle_message(message_to_deliver); - } - catch ( const fc::exception& e ) - { - elog( "${r}", ("r",e) ); - } - destination_node->messages_to_deliver.pop(); - } - } - - void simulated_network::broadcast( const message& item_to_broadcast ) - { - for (node_info* network_node_info : network_nodes) - { - network_node_info->messages_to_deliver.emplace(item_to_broadcast); - if (!network_node_info->message_sender_task_done.valid() || network_node_info->message_sender_task_done.ready()) - network_node_info->message_sender_task_done = fc::async([=](){ message_sender(network_node_info); }, "simulated_network_sender"); - } - } - - void simulated_network::add_node_delegate( node_delegate* node_delegate_to_add ) - { - network_nodes.push_back(new node_info(node_delegate_to_add)); - } - namespace detail { #define ROLLING_WINDOW_SIZE 1000 @@ -5110,7 +5062,8 @@ namespace graphene { namespace net { namespace detail { , BOOST_PP_CAT(_, BOOST_PP_CAT(method_name, _delay_after_accumulator))(boost::accumulators::tag::rolling_window::window_size = ROLLING_WINDOW_SIZE) - statistics_gathering_node_delegate_wrapper::statistics_gathering_node_delegate_wrapper(node_delegate* delegate, fc::thread* thread_for_delegate_calls) : + statistics_gathering_node_delegate_wrapper::statistics_gathering_node_delegate_wrapper( + std::shared_ptr delegate, fc::thread* thread_for_delegate_calls) : _node_delegate(delegate), _thread(thread_for_delegate_calls) BOOST_PP_SEQ_FOR_EACH(INITIALIZE_ACCUMULATOR, unused, NODE_DELEGATE_METHOD_NAMES) @@ -5213,7 +5166,8 @@ namespace graphene { namespace net { namespace detail { INVOKE_AND_COLLECT_STATISTICS(handle_message, message_to_handle); } - bool statistics_gathering_node_delegate_wrapper::handle_block( const graphene::net::block_message& block_message, bool sync_mode, std::vector& contained_transaction_message_ids) + bool statistics_gathering_node_delegate_wrapper::handle_block( const graphene::net::block_message& block_message, + bool sync_mode, std::vector& contained_transaction_message_ids) { INVOKE_AND_COLLECT_STATISTICS(handle_block, block_message, sync_mode, contained_transaction_message_ids); } diff --git a/libraries/net/node_impl.hxx b/libraries/net/node_impl.hxx index f1f44b6046..9590f8c149 100644 --- a/libraries/net/node_impl.hxx +++ b/libraries/net/node_impl.hxx @@ -144,15 +144,16 @@ struct prioritized_item_id class statistics_gathering_node_delegate_wrapper : public node_delegate { -private: - node_delegate *_node_delegate; - fc::thread *_thread; - - typedef boost::accumulators::accumulator_set > call_stats_accumulator; + private: + std::shared_ptr _node_delegate; + fc::thread *_thread; + + using call_stats_accumulator = boost::accumulators::accumulator_set< int64_t, + boost::accumulators::stats< boost::accumulators::tag::min, + boost::accumulators::tag::rolling_mean, + boost::accumulators::tag::max, + boost::accumulators::tag::sum, + boost::accumulators::tag::count> >; #define NODE_DELEGATE_METHOD_NAMES (has_item) \ (handle_message) \ (handle_block) \ @@ -194,7 +195,7 @@ private: { std::shared_ptr _collector; public: - actual_execution_measurement_helper(std::shared_ptr collector) : + explicit actual_execution_measurement_helper(std::shared_ptr collector) : _collector(collector) { _collector->starting_execution(); @@ -245,14 +246,16 @@ private: _execution_completed_time = fc::time_point::now(); } }; - public: - statistics_gathering_node_delegate_wrapper(node_delegate* delegate, fc::thread* thread_for_delegate_calls); + public: + statistics_gathering_node_delegate_wrapper(std::shared_ptr delegate, + fc::thread* thread_for_delegate_calls); fc::variant_object get_call_statistics(); bool has_item( const graphene::net::item_id& id ) override; void handle_message( const message& ) override; - bool handle_block( const graphene::net::block_message& block_message, bool sync_mode, std::vector& contained_transaction_message_ids ) override; + bool handle_block( const graphene::net::block_message& block_message, bool sync_mode, + std::vector& contained_transaction_message_ids ) override; void handle_transaction( const graphene::net::trx_message& transaction_message ) override; std::vector get_block_ids(const std::vector& blockchain_synopsis, uint32_t& remaining_item_count, @@ -269,7 +272,7 @@ private: uint32_t estimate_last_known_fork_from_git_revision_timestamp(uint32_t unix_timestamp) const override; void error_encountered(const std::string& message, const fc::oexception& error) override; uint8_t get_current_block_interval_in_seconds() const override; - }; +}; class node_impl : public peer_connection_delegate { @@ -558,11 +561,23 @@ class node_impl : public peer_connection_delegate void send_sync_block_to_node_delegate(const graphene::net::block_message& block_message_to_send); void process_backlog_of_sync_blocks(); void trigger_process_backlog_of_sync_blocks(); - void process_block_during_sync(peer_connection* originating_peer, const graphene::net::block_message& block_message, const message_hash_type& message_hash); - void process_block_during_normal_operation(peer_connection* originating_peer, const graphene::net::block_message& block_message, const message_hash_type& message_hash); - void process_block_message(peer_connection* originating_peer, const message& message_to_process, const message_hash_type& message_hash); - - void process_ordinary_message(peer_connection* originating_peer, const message& message_to_process, const message_hash_type& message_hash); + void process_block_during_sync( + peer_connection* originating_peer, + const graphene::net::block_message& block_message, + const message_hash_type& message_hash); + void process_block_during_normal_operation( + peer_connection* originating_peer, + const graphene::net::block_message& block_message, + const message_hash_type& message_hash); + void process_block_message( + peer_connection* originating_peer, + const message& message_to_process, + const message_hash_type& message_hash); + + void process_ordinary_message( + peer_connection* originating_peer, + const message& message_to_process, + const message_hash_type& message_hash); void start_synchronizing(); void start_synchronizing_with_peer(const peer_connection_ptr& peer); @@ -594,7 +609,7 @@ class node_impl : public peer_connection_delegate const fc::oexception& additional_data = fc::oexception() ); // methods implementing node's public interface - void set_node_delegate(node_delegate* del, fc::thread* thread_for_delegate_calls); + void set_node_delegate(std::shared_ptr del, fc::thread* thread_for_delegate_calls); void load_configuration( const fc::path& configuration_directory ); void listen_to_p2p_network(); void connect_to_p2p_network(); diff --git a/tests/common/simulated_network.cpp b/tests/common/simulated_network.cpp new file mode 100644 index 0000000000..1ea1f357d5 --- /dev/null +++ b/tests/common/simulated_network.cpp @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2015 Cryptonomex, Inc., and contributors. + * + * The MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include +#include + +#include "simulated_network.hpp" + +#include + +namespace graphene { namespace net { + + struct simulated_network::node_info + { + std::shared_ptr delegate; + fc::future message_sender_task_done; + std::queue messages_to_deliver; + explicit node_info(std::shared_ptr del) : delegate(del) {} + }; + + simulated_network::~simulated_network() + { + for( auto network_node_info : network_nodes ) + { + network_node_info->message_sender_task_done.cancel_and_wait("~simulated_network()"); + } + } + + void simulated_network::message_sender(std::shared_ptr destination_node) + { + while (!destination_node->messages_to_deliver.empty()) + { + try + { + const message& message_to_deliver = destination_node->messages_to_deliver.front(); + if (message_to_deliver.msg_type.value() == trx_message_type) + destination_node->delegate->handle_transaction(message_to_deliver.as()); + else if (message_to_deliver.msg_type.value() == block_message_type) + { + std::vector contained_transaction_message_ids; + destination_node->delegate->handle_block(message_to_deliver.as(), false, + contained_transaction_message_ids); + } + else + destination_node->delegate->handle_message(message_to_deliver); + } + catch ( const fc::exception& e ) + { + elog( "${r}", ("r",e) ); + } + destination_node->messages_to_deliver.pop(); + } + } + + void simulated_network::broadcast( const message& item_to_broadcast ) + { + for (auto network_node_info : network_nodes) + { + network_node_info->messages_to_deliver.emplace(item_to_broadcast); + if (!network_node_info->message_sender_task_done.valid() || network_node_info->message_sender_task_done.ready()) + network_node_info->message_sender_task_done = fc::async([=](){ message_sender(network_node_info); }, + "simulated_network_sender"); + } + } + + void simulated_network::add_node_delegate( std::shared_ptr node_delegate_to_add ) + { + network_nodes.push_back(std::make_shared(node_delegate_to_add)); + } + +} } // graphene::net diff --git a/tests/common/simulated_network.hpp b/tests/common/simulated_network.hpp new file mode 100644 index 0000000000..85f59e213a --- /dev/null +++ b/tests/common/simulated_network.hpp @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2015 Cryptonomex, Inc., and contributors. + * + * The MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#pragma once + +#include + +#include + +namespace graphene { namespace net { + +class simulated_network : public node +{ +public: + ~simulated_network() override; + explicit simulated_network(const std::string& user_agent) : node(user_agent) {} + void listen_to_p2p_network() override {} + void connect_to_p2p_network() override {} + void connect_to_endpoint(const fc::ip::endpoint& ep) override {} + + fc::ip::endpoint get_actual_listening_endpoint() const override { return fc::ip::endpoint(); } + + void sync_from(const item_id& current_head_block, const std::vector& hard_fork_block_numbers) override {} + void broadcast(const message& item_to_broadcast) override; + void add_node_delegate(std::shared_ptr node_delegate_to_add); + + uint32_t get_connection_count() const override { return 8; } +private: + struct node_info; + void message_sender(std::shared_ptr destination_node); + std::list> network_nodes; +}; + +using simulated_network_ptr = std::shared_ptr; + +} } // graphene::net From c147536a82d225bb3fdaacfb82f80fca4afb8259 Mon Sep 17 00:00:00 2001 From: abitmore Date: Tue, 6 Apr 2021 18:43:07 +0000 Subject: [PATCH 16/31] Manage node_impl object with shared_ptr and explicitly close chain database in destructor of application class --- libraries/app/application.cpp | 17 ++++++++++++-- libraries/net/include/graphene/net/node.hpp | 7 ++---- libraries/net/node.cpp | 26 ++++++++++++++------- libraries/net/node_impl.hxx | 11 ++++++--- 4 files changed, 42 insertions(+), 19 deletions(-) diff --git a/libraries/app/application.cpp b/libraries/app/application.cpp index 5f3df7f67c..e6b34b26e2 100644 --- a/libraries/app/application.cpp +++ b/libraries/app/application.cpp @@ -996,6 +996,7 @@ uint8_t application_impl::get_current_block_interval_in_seconds() const void application_impl::shutdown() { + ilog( "Shutting down application" ); if( _websocket_tls_server ) _websocket_tls_server.reset(); if( _websocket_server ) @@ -1003,20 +1004,27 @@ void application_impl::shutdown() // TODO wait until all connections are closed and messages handled? // plugins E.G. witness_plugin may send data to p2p network, so shutdown them first + ilog( "Shutting down plugins" ); shutdown_plugins(); if( _p2p_network ) { + ilog( "Disconnecting from P2P network" ); + // FIXME wait() is called in close() but it doesn't block this thread _p2p_network->close(); _p2p_network.reset(); - // TODO wait until all connections are closed and messages handled? } + else + ilog( "P2P network is disabled" ); if( _chain_db ) { + ilog( "Closing chain database" ); _chain_db->close(); _chain_db.reset(); } + else + ilog( "Chain database is not open" ); } void application_impl::enable_plugin( const string& name ) @@ -1077,7 +1085,8 @@ application::application() application::~application() { - //nothing to do + ilog("Application quitting"); + my->shutdown(); } void application::set_program_options(boost::program_options::options_description& command_line_options, @@ -1194,13 +1203,17 @@ void application::set_program_options(boost::program_options::options_descriptio void application::initialize(const fc::path& data_dir, std::shared_ptr options) { + ilog( "Initializing application" ); my->initialize( data_dir, options ); + ilog( "Done initializing application" ); } void application::startup() { try { + ilog( "Starting up application" ); my->startup(); + ilog( "Done starting up application" ); } catch ( const fc::exception& e ) { elog( "${e}", ("e",e.to_detail_string()) ); throw; diff --git a/libraries/net/include/graphene/net/node.hpp b/libraries/net/include/graphene/net/node.hpp index 7c80d09b1f..d22b5c800e 100644 --- a/libraries/net/include/graphene/net/node.hpp +++ b/libraries/net/include/graphene/net/node.hpp @@ -37,11 +37,8 @@ namespace graphene { namespace net { namespace detail { class node_impl; - struct node_impl_deleter - { - void operator()(node_impl*); - }; } + using node_impl_ptr = std::shared_ptr; // during network development, we need to track message propagation across the network // using a structure like this: @@ -316,7 +313,7 @@ namespace graphene { namespace net { void disable_peer_advertising(); fc::variant_object get_call_statistics() const; private: - std::unique_ptr my; + node_impl_ptr my; }; using node_ptr = std::shared_ptr; diff --git a/libraries/net/node.cpp b/libraries/net/node.cpp index 812ba66e69..db14b34386 100644 --- a/libraries/net/node.cpp +++ b/libraries/net/node.cpp @@ -859,7 +859,7 @@ namespace graphene { namespace net { namespace detail { _retrigger_advertise_inventory_loop_promise->set_value(); } - void node_impl::terminate_inactive_connections_loop() + void node_impl::terminate_inactive_connections_loop(node_impl_ptr self) { VERIFY_CORRECT_THREAD(); std::list peers_to_disconnect_gently; @@ -1077,9 +1077,12 @@ namespace graphene { namespace net { namespace detail { peers_to_send_keep_alive.clear(); if (!_node_is_shutting_down && !_terminate_inactive_connections_loop_done.canceled()) - _terminate_inactive_connections_loop_done = fc::schedule( [this](){ terminate_inactive_connections_loop(); }, - fc::time_point::now() + fc::seconds(GRAPHENE_NET_PEER_HANDSHAKE_INACTIVITY_TIMEOUT / 2), - "terminate_inactive_connections_loop" ); + { + _terminate_inactive_connections_loop_done = fc::schedule( + [this,self](){ terminate_inactive_connections_loop(self); }, + fc::time_point::now() + fc::seconds(GRAPHENE_NET_PEER_HANDSHAKE_INACTIVITY_TIMEOUT / 2), + "terminate_inactive_connections_loop" ); + } } void node_impl::fetch_updated_peer_lists_loop() @@ -4307,7 +4310,7 @@ namespace graphene { namespace net { namespace detail { } } - void node_impl::connect_to_p2p_network() + void node_impl::connect_to_p2p_network(node_impl_ptr self) { VERIFY_CORRECT_THREAD(); assert(_node_public_key != fc::ecc::public_key_data()); @@ -4324,12 +4327,15 @@ namespace graphene { namespace net { namespace detail { !_dump_node_status_task_done.valid()); if (_node_configuration.accept_incoming_connections) _accept_loop_complete = fc::async( [=](){ accept_loop(); }, "accept_loop"); + _p2p_network_connect_loop_done = fc::async( [=]() { p2p_network_connect_loop(); }, "p2p_network_connect_loop" ); _fetch_sync_items_loop_done = fc::async( [=]() { fetch_sync_items_loop(); }, "fetch_sync_items_loop" ); _fetch_item_loop_done = fc::async( [=]() { fetch_items_loop(); }, "fetch_items_loop" ); _advertise_inventory_loop_done = fc::async( [=]() { advertise_inventory_loop(); }, "advertise_inventory_loop" ); - _terminate_inactive_connections_loop_done = fc::async( [=]() { terminate_inactive_connections_loop(); }, "terminate_inactive_connections_loop" ); - _fetch_updated_peer_lists_loop_done = fc::async([=](){ fetch_updated_peer_lists_loop(); }, "fetch_updated_peer_lists_loop"); + _terminate_inactive_connections_loop_done = fc::async( [=]() { terminate_inactive_connections_loop(self); }, + "terminate_inactive_connections_loop" ); + _fetch_updated_peer_lists_loop_done = fc::async([=](){ fetch_updated_peer_lists_loop(); }, + "fetch_updated_peer_lists_loop"); _bandwidth_monitor_loop_done = fc::async([=](){ bandwidth_monitor_loop(); }, "bandwidth_monitor_loop"); _dump_node_status_task_done = fc::async([=](){ dump_node_status_task(); }, "dump_node_status_task"); schedule_next_update_seed_nodes_task(); @@ -4898,12 +4904,14 @@ namespace graphene { namespace net { namespace detail { #endif // P2P_IN_DEDICATED_THREAD node::node(const std::string& user_agent) : - my(new detail::node_impl(user_agent)) + my(new detail::node_impl(user_agent), detail::node_impl_deleter()) { + // nothing else to do } node::~node() { + // nothing to do } void node::set_node_delegate( std::shared_ptr del ) @@ -4924,7 +4932,7 @@ namespace graphene { namespace net { namespace detail { void node::connect_to_p2p_network() { - INVOKE_IN_IMPL(connect_to_p2p_network); + INVOKE_IN_IMPL(connect_to_p2p_network, my); } void node::add_node( const fc::ip::endpoint& ep ) diff --git a/libraries/net/node_impl.hxx b/libraries/net/node_impl.hxx index 9590f8c149..ad4b937ada 100644 --- a/libraries/net/node_impl.hxx +++ b/libraries/net/node_impl.hxx @@ -274,7 +274,7 @@ class statistics_gathering_node_delegate_wrapper : public node_delegate uint8_t get_current_block_interval_in_seconds() const override; }; -class node_impl : public peer_connection_delegate +class node_impl : public peer_connection_delegate, public std::enable_shared_from_this { public: #ifdef P2P_IN_DEDICATED_THREAD @@ -478,7 +478,7 @@ class node_impl : public peer_connection_delegate void advertise_inventory_loop(); void trigger_advertise_inventory_loop(); - void terminate_inactive_connections_loop(); + void terminate_inactive_connections_loop(node_impl_ptr self); void fetch_updated_peer_lists_loop(); void update_bandwidth_data(uint32_t bytes_read_this_second, uint32_t bytes_written_this_second); @@ -612,7 +612,7 @@ class node_impl : public peer_connection_delegate void set_node_delegate(std::shared_ptr del, fc::thread* thread_for_delegate_calls); void load_configuration( const fc::path& configuration_directory ); void listen_to_p2p_network(); - void connect_to_p2p_network(); + void connect_to_p2p_network(node_impl_ptr self); void add_node( const fc::ip::endpoint& ep ); void add_seed_node( const std::string& seed_string ); void resolve_seed_node_and_add( const std::string& seed_string ); @@ -652,4 +652,9 @@ class node_impl : public peer_connection_delegate uint32_t get_next_known_hard_fork_block_number(uint32_t block_number) const; }; // end class node_impl + struct node_impl_deleter + { + void operator()(node_impl*); + }; + }}} // end of namespace graphene::net::detail From 24f424d037f155020ae3c5f550b74f0ba9630e3f Mon Sep 17 00:00:00 2001 From: abitmore Date: Tue, 6 Apr 2021 20:59:10 +0000 Subject: [PATCH 17/31] Add try catch in _terminate_inactive_conn_loop to try to avoid accessing freed memory when node is shutting down --- libraries/app/application.cpp | 1 + libraries/net/node.cpp | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/libraries/app/application.cpp b/libraries/app/application.cpp index e6b34b26e2..879b02b7f6 100644 --- a/libraries/app/application.cpp +++ b/libraries/app/application.cpp @@ -991,6 +991,7 @@ void application_impl::error_encountered(const std::string& message, const fc::o uint8_t application_impl::get_current_block_interval_in_seconds() const { + FC_ASSERT( _chain_db, "Chain database is not operational" ); return _chain_db->get_global_properties().parameters.block_interval; } diff --git a/libraries/net/node.cpp b/libraries/net/node.cpp index db14b34386..a4fddaaed3 100644 --- a/libraries/net/node.cpp +++ b/libraries/net/node.cpp @@ -867,6 +867,9 @@ namespace graphene { namespace net { namespace detail { std::list peers_to_send_keep_alive; std::list peers_to_terminate; + try { + // Note: if the node is shutting down, it's possible that _delegate is already unusable, + // in this case, we'll get an exception _recent_block_interval_in_seconds = _delegate->get_current_block_interval_in_seconds(); // Disconnect peers that haven't sent us any data recently @@ -1076,6 +1079,14 @@ namespace graphene { namespace net { namespace detail { offsetof(current_time_request_message, request_sent_time)); peers_to_send_keep_alive.clear(); + } catch( const fc::exception& e ) { + wlog( "Exception caught in terminate_inactive_connections_loop: ${e}", ("e",e.to_detail_string()) ); + // If the node is shutting down, we just quit, no need to throw. + // If the node is not shutting down, the old code will throw, which means we won't schedule a new loop, + // likely it's unexpected behavior. + // Thus we don't throw here. + } + if (!_node_is_shutting_down && !_terminate_inactive_connections_loop_done.canceled()) { _terminate_inactive_connections_loop_done = fc::schedule( From 197adf7695f91460bd4c8085d730669aedf73cc2 Mon Sep 17 00:00:00 2001 From: abitmore Date: Tue, 6 Apr 2021 21:02:09 +0000 Subject: [PATCH 18/31] Explicitly shutdown witness plugin in unit test to avoid accessing freed memory --- tests/tests/operation_tests2.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/tests/operation_tests2.cpp b/tests/tests/operation_tests2.cpp index 1e97b41207..26d2215b2d 100644 --- a/tests/tests/operation_tests2.cpp +++ b/tests/tests/operation_tests2.cpp @@ -994,6 +994,8 @@ BOOST_AUTO_TEST_CASE( witness_create ) produced++; } BOOST_CHECK_GE( produced, 1 ); + + wtplugin->plugin_shutdown(); } FC_LOG_AND_RETHROW() } /** From 2384ecb2bf1ece9df8e83181a05d2fa9394bbeec Mon Sep 17 00:00:00 2001 From: abitmore Date: Tue, 6 Apr 2021 22:09:50 +0000 Subject: [PATCH 19/31] Cleanup socket after app in CLI tests for windows --- tests/cli/main.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/tests/cli/main.cpp b/tests/cli/main.cpp index 11e30391fd..d5ed125e09 100644 --- a/tests/cli/main.cpp +++ b/tests/cli/main.cpp @@ -106,9 +106,6 @@ std::shared_ptr start_application(fc::temp_directory auto sharable_cfg = std::make_shared(); auto& cfg = *sharable_cfg; -#ifdef _WIN32 - sockInit(); -#endif server_port_number = fc::network::get_available_port(); cfg.emplace( "rpc-endpoint", @@ -277,6 +274,16 @@ class client_connection struct cli_fixture { +#ifdef _WIN32 + struct socket_maintainer { + socket_maintainer() { + sockInit(); + } + ~socket_maintainer() { + sockQuit(); + } + } sock_maintainer; +#endif int server_port_number; fc::temp_directory app_dir; std::shared_ptr app1; @@ -314,9 +321,6 @@ struct cli_fixture ~cli_fixture() { BOOST_TEST_MESSAGE("Cleanup cli_wallet::boost_fixture_test_case"); -#ifdef _WIN32 - sockQuit(); -#endif } }; From 1476bc6e628c8a213ff37d42b418182560ec76d6 Mon Sep 17 00:00:00 2001 From: abitmore Date: Tue, 6 Apr 2021 23:04:59 +0000 Subject: [PATCH 20/31] Add tests for enable-p2p-network option --- tests/common/database_fixture.cpp | 11 +++++++- tests/tests/network_broadcast_api_tests.cpp | 30 +++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/tests/common/database_fixture.cpp b/tests/common/database_fixture.cpp index ef30b1d302..847181c5cf 100644 --- a/tests/common/database_fixture.cpp +++ b/tests/common/database_fixture.cpp @@ -183,10 +183,19 @@ std::shared_ptr database_fixture_base::in { auto sharable_options = std::make_shared(); auto& options = *sharable_options; - set_option( options, "seed-nodes", std::string("[]") ); + set_option( options, "seed-nodes", std::string("[]") ); // Do not connect to default seed nodes /** * Test specific settings */ + if (fixture.current_test_name == "broadcast_transaction_with_callback_test") + set_option( options, "enable-p2p-network", true ); + else if (fixture.current_test_name == "broadcast_transaction_disabled_p2p_test") + set_option( options, "enable-p2p-network", false ); + else if( rand() % 100 >= 50 ) // Disable P2P network randomly for test cases + set_option( options, "enable-p2p-network", false ); + else if( rand() % 100 >= 50 ) // this should lead to no change + set_option( options, "enable-p2p-network", true ); + if (fixture.current_test_name == "get_account_history_operations") { options.insert(std::make_pair("max-ops-per-account", boost::program_options::variable_value((uint64_t)75, false))); diff --git a/tests/tests/network_broadcast_api_tests.cpp b/tests/tests/network_broadcast_api_tests.cpp index a40c112662..9a4a264492 100644 --- a/tests/tests/network_broadcast_api_tests.cpp +++ b/tests/tests/network_broadcast_api_tests.cpp @@ -72,6 +72,36 @@ BOOST_AUTO_TEST_CASE( broadcast_transaction_with_callback_test ) { } FC_LOG_AND_RETHROW() } +BOOST_AUTO_TEST_CASE( broadcast_transaction_disabled_p2p_test ) { + try { + + uint32_t called = 0; + auto callback = [&]( const variant& v ) + { + ++called; + }; + + fc::ecc::private_key cid_key = fc::ecc::private_key::regenerate( fc::digest("key") ); + const account_id_type cid_id = create_account( "cid", cid_key.get_public_key() ).id; + fund( cid_id(db) ); + + auto nb_api = std::make_shared< graphene::app::network_broadcast_api >( app ); + + set_expiration( db, trx ); + transfer_operation trans; + trans.from = cid_id; + trans.to = account_id_type(); + trans.amount = asset(1); + trx.operations.push_back( trans ); + sign( trx, cid_key ); + + BOOST_CHECK_THROW( nb_api->broadcast_transaction( trx ), fc::exception ); + BOOST_CHECK_THROW( nb_api->broadcast_transaction_with_callback( callback, trx ), fc::exception ); + BOOST_CHECK_EQUAL( called, 0u ); + + } FC_LOG_AND_RETHROW() +} + BOOST_AUTO_TEST_CASE( broadcast_transaction_too_large ) { try { From e659a631d5f1da5cda90bbb7465758dff58256c6 Mon Sep 17 00:00:00 2001 From: abitmore Date: Tue, 6 Apr 2021 23:30:36 +0000 Subject: [PATCH 21/31] Update order of includes in es_test --- tests/elasticsearch/main.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/elasticsearch/main.cpp b/tests/elasticsearch/main.cpp index 7bbb090ea5..315c9f3dc2 100644 --- a/tests/elasticsearch/main.cpp +++ b/tests/elasticsearch/main.cpp @@ -29,9 +29,10 @@ #include #include +#include "../common/init_unit_test_suite.hpp" + #include "../common/database_fixture.hpp" -#include "../common/init_unit_test_suite.hpp" #include "../common/utils.hpp" #define ES_WAIT_TIME (fc::milliseconds(10000)) From b013559fad6ad904ee38fe8e3e0ea29bfbf8758b Mon Sep 17 00:00:00 2001 From: abitmore Date: Wed, 7 Apr 2021 00:31:56 +0000 Subject: [PATCH 22/31] Refuse to broadcast sooner if no P2P network --- libraries/app/api.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/app/api.cpp b/libraries/app/api.cpp index 3fd59676f9..5906ea63c2 100644 --- a/libraries/app/api.cpp +++ b/libraries/app/api.cpp @@ -179,8 +179,8 @@ namespace graphene { namespace app { void network_broadcast_api::broadcast_transaction(const precomputable_transaction& trx) { - _app.chain_database()->precompute_parallel( trx ).wait(); FC_ASSERT( _app.p2p_node() != nullptr, "Not connected to P2P network, can't broadcast!" ); + _app.chain_database()->precompute_parallel( trx ).wait(); _app.chain_database()->push_transaction(trx); _app.p2p_node()->broadcast_transaction(trx); } @@ -197,16 +197,16 @@ namespace graphene { namespace app { void network_broadcast_api::broadcast_block( const signed_block& b ) { - _app.chain_database()->precompute_parallel( b ).wait(); FC_ASSERT( _app.p2p_node() != nullptr, "Not connected to P2P network, can't broadcast!" ); + _app.chain_database()->precompute_parallel( b ).wait(); _app.chain_database()->push_block(b); _app.p2p_node()->broadcast( net::block_message( b )); } void network_broadcast_api::broadcast_transaction_with_callback(confirmation_callback cb, const precomputable_transaction& trx) { - _app.chain_database()->precompute_parallel( trx ).wait(); FC_ASSERT( _app.p2p_node() != nullptr, "Not connected to P2P network, can't broadcast!" ); + _app.chain_database()->precompute_parallel( trx ).wait(); _callbacks[trx.id()] = cb; _app.chain_database()->push_transaction(trx); _app.p2p_node()->broadcast_transaction(trx); From 67026f6dc5b2dfa69718d02a92ba08624a9de6ce Mon Sep 17 00:00:00 2001 From: abitmore Date: Wed, 7 Apr 2021 10:39:43 +0000 Subject: [PATCH 23/31] Rename long variable names --- libraries/app/application.cpp | 6 +++--- libraries/app/application_impl.hxx | 4 ++-- libraries/net/include/graphene/net/node.hpp | 4 ++-- libraries/net/node.cpp | 14 +++++++------- libraries/net/node_impl.hxx | 2 +- tests/common/simulated_network.cpp | 4 ++-- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/libraries/app/application.cpp b/libraries/app/application.cpp index 879b02b7f6..73e3174e82 100644 --- a/libraries/app/application.cpp +++ b/libraries/app/application.cpp @@ -578,7 +578,7 @@ bool application_impl::has_item(const net::item_id& id) * @throws exception if error validating the item, otherwise the item is safe to broadcast on. */ bool application_impl::handle_block(const graphene::net::block_message& blk_msg, bool sync_mode, - std::vector& contained_transaction_message_ids) + std::vector& contained_transaction_msg_ids) { try { auto latency = fc::time_point::now() - blk_msg.block.timestamp; @@ -622,12 +622,12 @@ bool application_impl::handle_block(const graphene::net::block_message& blk_msg, // happens, there's no reason to fetch the transactions, so construct a list of the // transaction message ids we no longer need. // during sync, it is unlikely that we'll see any old - contained_transaction_message_ids.reserve( contained_transaction_message_ids.size() + contained_transaction_msg_ids.reserve( contained_transaction_msg_ids.size() + blk_msg.block.transactions.size() ); for (const processed_transaction& ptrx : blk_msg.block.transactions) { graphene::net::trx_message transaction_message(ptrx); - contained_transaction_message_ids.emplace_back(graphene::net::message(transaction_message).id()); + contained_transaction_msg_ids.emplace_back(graphene::net::message(transaction_message).id()); } } diff --git a/libraries/app/application_impl.hxx b/libraries/app/application_impl.hxx index 829a7fdd6f..9f42800494 100644 --- a/libraries/app/application_impl.hxx +++ b/libraries/app/application_impl.hxx @@ -57,13 +57,13 @@ class application_impl : public net::node_delegate, public std::enable_shared_fr * * @param blk_msg the message which contains the block * @param sync_mode true if the message was fetched through the sync process, false during normal operation - * @param contained_transaction_message_ids container for the transactions to write back into + * @param contained_transaction_msg_ids container for the transactions to write back into * @returns true if this message caused the blockchain to switch forks, false if it did not * * @throws exception if error validating the item, otherwise the item is safe to broadcast on. */ bool handle_block(const graphene::net::block_message& blk_msg, bool sync_mode, - std::vector& contained_transaction_message_ids) override; + std::vector& contained_transaction_msg_ids) override; void handle_transaction(const graphene::net::trx_message& transaction_message) override; diff --git a/libraries/net/include/graphene/net/node.hpp b/libraries/net/include/graphene/net/node.hpp index d22b5c800e..55d2df782f 100644 --- a/libraries/net/include/graphene/net/node.hpp +++ b/libraries/net/include/graphene/net/node.hpp @@ -68,14 +68,14 @@ namespace graphene { namespace net { * * @param blk_msg the message which contains the block * @param sync_mode true if the message was fetched through the sync process, false during normal operation - * @param contained_transaction_message_ids container for the transactions to write back into + * @param contained_transaction_msg_ids container for the transactions to write back into * @returns true if this message caused the blockchain to switch forks, false if it did not * * @throws exception if error validating the item, otherwise the item is * safe to broadcast on. */ virtual bool handle_block( const graphene::net::block_message& blk_msg, bool sync_mode, - std::vector& contained_transaction_message_ids ) = 0; + std::vector& contained_transaction_msg_ids ) = 0; /** * @brief Called when a new transaction comes in from the network diff --git a/libraries/net/node.cpp b/libraries/net/node.cpp index a4fddaaed3..adb4f6c4be 100644 --- a/libraries/net/node.cpp +++ b/libraries/net/node.cpp @@ -2677,8 +2677,8 @@ namespace graphene { namespace net { namespace detail { try { - std::vector contained_transaction_message_ids; - _delegate->handle_block(block_message_to_send, true, contained_transaction_message_ids); + std::vector contained_transaction_msg_ids; + _delegate->handle_block(block_message_to_send, true, contained_transaction_msg_ids); ilog("Successfully pushed sync block ${num} (id:${id})", ("num", block_message_to_send.block.block_num()) ("id", block_message_to_send.block_id)); @@ -3041,8 +3041,8 @@ namespace graphene { namespace net { namespace detail { if (std::find(_most_recent_blocks_accepted.begin(), _most_recent_blocks_accepted.end(), block_message_to_process.block_id) == _most_recent_blocks_accepted.end()) { - std::vector contained_transaction_message_ids; - _delegate->handle_block(block_message_to_process, false, contained_transaction_message_ids); + std::vector contained_transaction_msg_ids; + _delegate->handle_block(block_message_to_process, false, contained_transaction_msg_ids); message_validated_time = fc::time_point::now(); ilog("Successfully pushed block ${num} (id:${id})", ("num", block_message_to_process.block.block_num()) @@ -3050,7 +3050,7 @@ namespace graphene { namespace net { namespace detail { _most_recent_blocks_accepted.push_back(block_message_to_process.block_id); bool new_transaction_discovered = false; - for (const item_hash_t& transaction_message_hash : contained_transaction_message_ids) + for (const item_hash_t& transaction_message_hash : contained_transaction_msg_ids) { /*size_t items_erased =*/ _items_to_fetch.get().erase(item_id(trx_message_type, transaction_message_hash)); // there are two ways we could behave here: we could either act as if we received @@ -5186,9 +5186,9 @@ namespace graphene { namespace net { namespace detail { } bool statistics_gathering_node_delegate_wrapper::handle_block( const graphene::net::block_message& block_message, - bool sync_mode, std::vector& contained_transaction_message_ids) + bool sync_mode, std::vector& contained_transaction_msg_ids) { - INVOKE_AND_COLLECT_STATISTICS(handle_block, block_message, sync_mode, contained_transaction_message_ids); + INVOKE_AND_COLLECT_STATISTICS(handle_block, block_message, sync_mode, contained_transaction_msg_ids); } void statistics_gathering_node_delegate_wrapper::handle_transaction( const graphene::net::trx_message& transaction_message ) diff --git a/libraries/net/node_impl.hxx b/libraries/net/node_impl.hxx index ad4b937ada..f217ec72f8 100644 --- a/libraries/net/node_impl.hxx +++ b/libraries/net/node_impl.hxx @@ -255,7 +255,7 @@ class statistics_gathering_node_delegate_wrapper : public node_delegate bool has_item( const graphene::net::item_id& id ) override; void handle_message( const message& ) override; bool handle_block( const graphene::net::block_message& block_message, bool sync_mode, - std::vector& contained_transaction_message_ids ) override; + std::vector& contained_transaction_msg_ids ) override; void handle_transaction( const graphene::net::trx_message& transaction_message ) override; std::vector get_block_ids(const std::vector& blockchain_synopsis, uint32_t& remaining_item_count, diff --git a/tests/common/simulated_network.cpp b/tests/common/simulated_network.cpp index 1ea1f357d5..91557b1f25 100644 --- a/tests/common/simulated_network.cpp +++ b/tests/common/simulated_network.cpp @@ -59,9 +59,9 @@ namespace graphene { namespace net { destination_node->delegate->handle_transaction(message_to_deliver.as()); else if (message_to_deliver.msg_type.value() == block_message_type) { - std::vector contained_transaction_message_ids; + std::vector contained_transaction_msg_ids; destination_node->delegate->handle_block(message_to_deliver.as(), false, - contained_transaction_message_ids); + contained_transaction_msg_ids); } else destination_node->delegate->handle_message(message_to_deliver); From 628f93ba7199998a3e25a0c3fc1e515a0877d930 Mon Sep 17 00:00:00 2001 From: abitmore Date: Wed, 7 Apr 2021 10:58:12 +0000 Subject: [PATCH 24/31] Replace duplicate constructors with using clauses and remove the unnecessary destructor from the plugin class --- libraries/app/include/graphene/app/plugin.hpp | 3 +-- .../include/graphene/debug_witness/debug_witness.hpp | 2 +- .../plugins/snapshot/include/graphene/snapshot/snapshot.hpp | 2 +- libraries/plugins/witness/include/graphene/witness/witness.hpp | 2 +- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/libraries/app/include/graphene/app/plugin.hpp b/libraries/app/include/graphene/app/plugin.hpp index b2809723ad..10a750e28a 100644 --- a/libraries/app/include/graphene/app/plugin.hpp +++ b/libraries/app/include/graphene/app/plugin.hpp @@ -100,8 +100,7 @@ class abstract_plugin class plugin : public abstract_plugin { public: - explicit plugin(application& a) : abstract_plugin(a) {} - ~plugin() override = default; + using abstract_plugin::abstract_plugin; std::string plugin_name()const override; std::string plugin_description()const override; diff --git a/libraries/plugins/debug_witness/include/graphene/debug_witness/debug_witness.hpp b/libraries/plugins/debug_witness/include/graphene/debug_witness/debug_witness.hpp index e7d4f734d1..92f1b21d74 100644 --- a/libraries/plugins/debug_witness/include/graphene/debug_witness/debug_witness.hpp +++ b/libraries/plugins/debug_witness/include/graphene/debug_witness/debug_witness.hpp @@ -34,7 +34,7 @@ namespace graphene { namespace debug_witness_plugin { class debug_witness_plugin : public graphene::app::plugin { public: - debug_witness_plugin(graphene::app::application& app) : graphene::app::plugin(app) {} + using graphene::app::plugin::plugin; ~debug_witness_plugin() override; std::string plugin_name()const override; diff --git a/libraries/plugins/snapshot/include/graphene/snapshot/snapshot.hpp b/libraries/plugins/snapshot/include/graphene/snapshot/snapshot.hpp index 5f62a0a678..c4de7b7624 100644 --- a/libraries/plugins/snapshot/include/graphene/snapshot/snapshot.hpp +++ b/libraries/plugins/snapshot/include/graphene/snapshot/snapshot.hpp @@ -32,7 +32,7 @@ namespace graphene { namespace snapshot_plugin { class snapshot_plugin : public graphene::app::plugin { public: - snapshot_plugin(graphene::app::application& app) : graphene::app::plugin(app) {} + using graphene::app::plugin::plugin; std::string plugin_name()const override; std::string plugin_description()const override; diff --git a/libraries/plugins/witness/include/graphene/witness/witness.hpp b/libraries/plugins/witness/include/graphene/witness/witness.hpp index 40c4fe87c8..e3e1497af2 100644 --- a/libraries/plugins/witness/include/graphene/witness/witness.hpp +++ b/libraries/plugins/witness/include/graphene/witness/witness.hpp @@ -49,7 +49,7 @@ namespace block_production_condition class witness_plugin : public graphene::app::plugin { public: - witness_plugin(graphene::app::application& app) : graphene::app::plugin(app) {} + using graphene::app::plugin::plugin; ~witness_plugin() override { cleanup(); } std::string plugin_name()const override; From aac5f43b127603b0e63ee4f0c1d99cb190ac1135 Mon Sep 17 00:00:00 2001 From: abitmore Date: Wed, 7 Apr 2021 11:12:23 +0000 Subject: [PATCH 25/31] Rename long variable names and function names --- libraries/net/node.cpp | 22 +++++++++++----------- libraries/net/node_impl.hxx | 4 ++-- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/libraries/net/node.cpp b/libraries/net/node.cpp index adb4f6c4be..317a9212c9 100644 --- a/libraries/net/node.cpp +++ b/libraries/net/node.cpp @@ -859,7 +859,7 @@ namespace graphene { namespace net { namespace detail { _retrigger_advertise_inventory_loop_promise->set_value(); } - void node_impl::terminate_inactive_connections_loop(node_impl_ptr self) + void node_impl::kill_inactive_conns_loop(node_impl_ptr self) { VERIFY_CORRECT_THREAD(); std::list peers_to_disconnect_gently; @@ -1080,19 +1080,19 @@ namespace graphene { namespace net { namespace detail { peers_to_send_keep_alive.clear(); } catch( const fc::exception& e ) { - wlog( "Exception caught in terminate_inactive_connections_loop: ${e}", ("e",e.to_detail_string()) ); + wlog( "Exception caught in kill_inactive_conns_loop: ${e}", ("e",e.to_detail_string()) ); // If the node is shutting down, we just quit, no need to throw. // If the node is not shutting down, the old code will throw, which means we won't schedule a new loop, // likely it's unexpected behavior. // Thus we don't throw here. } - if (!_node_is_shutting_down && !_terminate_inactive_connections_loop_done.canceled()) + if (!_node_is_shutting_down && !_kill_inactive_conns_loop_done.canceled()) { - _terminate_inactive_connections_loop_done = fc::schedule( - [this,self](){ terminate_inactive_connections_loop(self); }, + _kill_inactive_conns_loop_done = fc::schedule( + [this,self](){ kill_inactive_conns_loop(self); }, fc::time_point::now() + fc::seconds(GRAPHENE_NET_PEER_HANDSHAKE_INACTIVITY_TIMEOUT / 2), - "terminate_inactive_connections_loop" ); + "kill_inactive_conns_loop" ); } } @@ -3884,8 +3884,8 @@ namespace graphene { namespace net { namespace detail { // our loops now try { - _terminate_inactive_connections_loop_done.cancel_and_wait("node_impl::close()"); - dlog("Terminate inactive connections loop terminated"); + _kill_inactive_conns_loop_done.cancel_and_wait("node_impl::close()"); + dlog("Kill inactive connections loop terminated"); } catch ( const fc::exception& e ) { @@ -4332,7 +4332,7 @@ namespace graphene { namespace net { namespace detail { !_fetch_sync_items_loop_done.valid() && !_fetch_item_loop_done.valid() && !_advertise_inventory_loop_done.valid() && - !_terminate_inactive_connections_loop_done.valid() && + !_kill_inactive_conns_loop_done.valid() && !_fetch_updated_peer_lists_loop_done.valid() && !_bandwidth_monitor_loop_done.valid() && !_dump_node_status_task_done.valid()); @@ -4343,8 +4343,8 @@ namespace graphene { namespace net { namespace detail { _fetch_sync_items_loop_done = fc::async( [=]() { fetch_sync_items_loop(); }, "fetch_sync_items_loop" ); _fetch_item_loop_done = fc::async( [=]() { fetch_items_loop(); }, "fetch_items_loop" ); _advertise_inventory_loop_done = fc::async( [=]() { advertise_inventory_loop(); }, "advertise_inventory_loop" ); - _terminate_inactive_connections_loop_done = fc::async( [=]() { terminate_inactive_connections_loop(self); }, - "terminate_inactive_connections_loop" ); + _kill_inactive_conns_loop_done = fc::async( [=]() { kill_inactive_conns_loop(self); }, + "kill_inactive_conns_loop" ); _fetch_updated_peer_lists_loop_done = fc::async([=](){ fetch_updated_peer_lists_loop(); }, "fetch_updated_peer_lists_loop"); _bandwidth_monitor_loop_done = fc::async([=](){ bandwidth_monitor_loop(); }, "bandwidth_monitor_loop"); diff --git a/libraries/net/node_impl.hxx b/libraries/net/node_impl.hxx index f217ec72f8..8a4ad5fa67 100644 --- a/libraries/net/node_impl.hxx +++ b/libraries/net/node_impl.hxx @@ -351,7 +351,7 @@ class node_impl : public peer_connection_delegate, public std::enable_shared_fro concurrent_unordered_set _new_inventory; /// @} - fc::future _terminate_inactive_connections_loop_done; + fc::future _kill_inactive_conns_loop_done; uint8_t _recent_block_interval_in_seconds; // a cached copy of the block interval, to avoid a thread hop to the blockchain to get the current value std::string _user_agent_string; @@ -478,7 +478,7 @@ class node_impl : public peer_connection_delegate, public std::enable_shared_fro void advertise_inventory_loop(); void trigger_advertise_inventory_loop(); - void terminate_inactive_connections_loop(node_impl_ptr self); + void kill_inactive_conns_loop(node_impl_ptr self); void fetch_updated_peer_lists_loop(); void update_bandwidth_data(uint32_t bytes_read_this_second, uint32_t bytes_written_this_second); From 52ebad42a3010385ad8827aaab544e3cdc791724 Mon Sep 17 00:00:00 2001 From: abitmore Date: Wed, 7 Apr 2021 12:28:37 +0000 Subject: [PATCH 26/31] Fix code smells --- .../account_history_plugin.cpp | 12 +++++++----- .../account_history_plugin.hpp | 4 ++-- .../api_helper_indexes/api_helper_indexes.cpp | 11 ++++------- .../api_helper_indexes/api_helper_indexes.hpp | 2 +- .../custom_operations_plugin.cpp | 19 ++++++------------- .../custom_operations_plugin.hpp | 4 ++-- .../delayed_node/delayed_node_plugin.cpp | 13 +++++++------ .../delayed_node/delayed_node_plugin.hpp | 4 ++-- .../elasticsearch/elasticsearch_plugin.cpp | 8 +++----- .../elasticsearch/elasticsearch_plugin.hpp | 2 +- libraries/plugins/es_objects/es_objects.cpp | 8 +++----- .../graphene/es_objects/es_objects.hpp | 6 +++--- .../grouped_orders/grouped_orders_plugin.cpp | 11 +++-------- .../grouped_orders/grouped_orders_plugin.hpp | 3 +-- .../market_history/market_history_plugin.hpp | 3 +-- .../market_history/market_history_plugin.cpp | 15 +++------------ .../template_plugin/template_plugin.hpp | 3 +-- .../template_plugin/template_plugin.cpp | 17 +++++++++++------ 18 files changed, 61 insertions(+), 84 deletions(-) diff --git a/libraries/plugins/account_history/account_history_plugin.cpp b/libraries/plugins/account_history/account_history_plugin.cpp index 19e384a171..dd8a5c6215 100644 --- a/libraries/plugins/account_history/account_history_plugin.cpp +++ b/libraries/plugins/account_history/account_history_plugin.cpp @@ -46,7 +46,7 @@ namespace detail class account_history_plugin_impl { public: - account_history_plugin_impl(account_history_plugin& _plugin) + explicit account_history_plugin_impl(account_history_plugin& _plugin) : _self( _plugin ) { } @@ -60,6 +60,9 @@ class account_history_plugin_impl return _self.database(); } + friend class graphene::account_history::account_history_plugin; + + private: account_history_plugin& _self; flat_set _tracked_accounts; flat_set _extended_history_accounts; @@ -68,7 +71,7 @@ class account_history_plugin_impl primary_index< operation_history_index >* _oho_index; uint64_t _max_ops_per_account = -1; uint64_t _extended_max_ops_per_account = -1; - private: + /** add one history record, then check and remove the earliest history record */ void add_account_history( const account_id_type account_id, const operation_history_id_type op_id ); @@ -279,11 +282,10 @@ account_history_plugin::account_history_plugin(graphene::app::application& app) plugin(app), my( std::make_unique(*this) ) { + // Nothing else to do } -account_history_plugin::~account_history_plugin() -{ -} +account_history_plugin::~account_history_plugin() = default; std::string account_history_plugin::plugin_name()const { diff --git a/libraries/plugins/account_history/include/graphene/account_history/account_history_plugin.hpp b/libraries/plugins/account_history/include/graphene/account_history/account_history_plugin.hpp index 9676869fbf..6ceda572a7 100644 --- a/libraries/plugins/account_history/include/graphene/account_history/account_history_plugin.hpp +++ b/libraries/plugins/account_history/include/graphene/account_history/account_history_plugin.hpp @@ -64,7 +64,7 @@ namespace detail class account_history_plugin : public graphene::app::plugin { public: - account_history_plugin(graphene::app::application& app); + explicit account_history_plugin(graphene::app::application& app); ~account_history_plugin() override; std::string plugin_name()const override; @@ -76,7 +76,7 @@ class account_history_plugin : public graphene::app::plugin flat_set tracked_accounts()const; - friend class detail::account_history_plugin_impl; + private: std::unique_ptr my; }; diff --git a/libraries/plugins/api_helper_indexes/api_helper_indexes.cpp b/libraries/plugins/api_helper_indexes/api_helper_indexes.cpp index 00fbbf3e16..79c996399c 100644 --- a/libraries/plugins/api_helper_indexes/api_helper_indexes.cpp +++ b/libraries/plugins/api_helper_indexes/api_helper_indexes.cpp @@ -133,7 +133,7 @@ namespace detail class api_helper_indexes_impl { public: - api_helper_indexes_impl(api_helper_indexes& _plugin) + explicit api_helper_indexes_impl(api_helper_indexes& _plugin) : _self( _plugin ) { } @@ -142,10 +142,8 @@ class api_helper_indexes_impl return _self.database(); } - api_helper_indexes& _self; - private: - + api_helper_indexes& _self; }; } // end namespace detail @@ -154,11 +152,10 @@ api_helper_indexes::api_helper_indexes(graphene::app::application& app) : plugin(app), my( std::make_unique(*this) ) { + // Nothing else to do } -api_helper_indexes::~api_helper_indexes() -{ -} +api_helper_indexes::~api_helper_indexes() = default; std::string api_helper_indexes::plugin_name()const { diff --git a/libraries/plugins/api_helper_indexes/include/graphene/api_helper_indexes/api_helper_indexes.hpp b/libraries/plugins/api_helper_indexes/include/graphene/api_helper_indexes/api_helper_indexes.hpp index a6bb59b307..bfd7502ab1 100644 --- a/libraries/plugins/api_helper_indexes/include/graphene/api_helper_indexes/api_helper_indexes.hpp +++ b/libraries/plugins/api_helper_indexes/include/graphene/api_helper_indexes/api_helper_indexes.hpp @@ -79,7 +79,7 @@ namespace detail class api_helper_indexes : public graphene::app::plugin { public: - api_helper_indexes(graphene::app::application& app); + explicit api_helper_indexes(graphene::app::application& app); ~api_helper_indexes() override; std::string plugin_name()const override; diff --git a/libraries/plugins/custom_operations/custom_operations_plugin.cpp b/libraries/plugins/custom_operations/custom_operations_plugin.cpp index 1696b98f50..e9fb5b971c 100644 --- a/libraries/plugins/custom_operations/custom_operations_plugin.cpp +++ b/libraries/plugins/custom_operations/custom_operations_plugin.cpp @@ -35,10 +35,9 @@ namespace detail class custom_operations_plugin_impl { public: - custom_operations_plugin_impl(custom_operations_plugin& _plugin) + explicit custom_operations_plugin_impl(custom_operations_plugin& _plugin) : _self( _plugin ) { } - virtual ~custom_operations_plugin_impl(); void onBlock(); @@ -47,12 +46,12 @@ class custom_operations_plugin_impl return _self.database(); } - custom_operations_plugin& _self; - - uint32_t _start_block = 45000000; + friend class graphene::custom_operations::custom_operations_plugin; private: + custom_operations_plugin& _self; + uint32_t _start_block = 45000000; }; struct custom_op_visitor @@ -98,22 +97,16 @@ void custom_operations_plugin_impl::onBlock() } } -custom_operations_plugin_impl::~custom_operations_plugin_impl() -{ - // nothing to do -} - } // end namespace detail custom_operations_plugin::custom_operations_plugin(graphene::app::application& app) : plugin(app), my( std::make_unique(*this) ) { + // Nothing to do } -custom_operations_plugin::~custom_operations_plugin() -{ -} +custom_operations_plugin::~custom_operations_plugin() = default; std::string custom_operations_plugin::plugin_name()const { diff --git a/libraries/plugins/custom_operations/include/graphene/custom_operations/custom_operations_plugin.hpp b/libraries/plugins/custom_operations/include/graphene/custom_operations/custom_operations_plugin.hpp index e2a5fd9633..add8ce8637 100644 --- a/libraries/plugins/custom_operations/include/graphene/custom_operations/custom_operations_plugin.hpp +++ b/libraries/plugins/custom_operations/include/graphene/custom_operations/custom_operations_plugin.hpp @@ -41,7 +41,7 @@ namespace detail class custom_operations_plugin : public graphene::app::plugin { public: - custom_operations_plugin(graphene::app::application& app); + explicit custom_operations_plugin(graphene::app::application& app); ~custom_operations_plugin() override; std::string plugin_name()const override; @@ -52,7 +52,7 @@ class custom_operations_plugin : public graphene::app::plugin void plugin_initialize(const boost::program_options::variables_map& options) override; void plugin_startup() override; - friend class detail::custom_operations_plugin_impl; + private: std::unique_ptr my; }; diff --git a/libraries/plugins/delayed_node/delayed_node_plugin.cpp b/libraries/plugins/delayed_node/delayed_node_plugin.cpp index 18ef6b26b1..b16aadd233 100644 --- a/libraries/plugins/delayed_node/delayed_node_plugin.cpp +++ b/libraries/plugins/delayed_node/delayed_node_plugin.cpp @@ -47,17 +47,18 @@ struct delayed_node_plugin_impl { } delayed_node_plugin::delayed_node_plugin(graphene::app::application& app) : - plugin(app), - my(nullptr) -{} + plugin(app) +{ + // Nothing else to do +} -delayed_node_plugin::~delayed_node_plugin() -{} +delayed_node_plugin::~delayed_node_plugin() = default; void delayed_node_plugin::plugin_set_program_options(bpo::options_description& cli, bpo::options_description& cfg) { cli.add_options() - ("trusted-node", boost::program_options::value(), "RPC endpoint of a trusted validating node (required for delayed_node)") + ("trusted-node", boost::program_options::value(), + "RPC endpoint of a trusted validating node (required for delayed_node)") ; cfg.add(cli); } diff --git a/libraries/plugins/delayed_node/include/graphene/delayed_node/delayed_node_plugin.hpp b/libraries/plugins/delayed_node/include/graphene/delayed_node/delayed_node_plugin.hpp index 4e06c7fcfc..448d979782 100644 --- a/libraries/plugins/delayed_node/include/graphene/delayed_node/delayed_node_plugin.hpp +++ b/libraries/plugins/delayed_node/include/graphene/delayed_node/delayed_node_plugin.hpp @@ -32,12 +32,12 @@ class delayed_node_plugin : public graphene::app::plugin { std::unique_ptr my; public: - delayed_node_plugin(graphene::app::application& app); + explicit delayed_node_plugin(graphene::app::application& app); ~delayed_node_plugin() override; std::string plugin_name()const override { return "delayed_node"; } void plugin_set_program_options(boost::program_options::options_description&, - boost::program_options::options_description& cfg) override; + boost::program_options::options_description& cfg) override; void plugin_initialize(const boost::program_options::variables_map& options) override; void plugin_startup() override; void mainloop(); diff --git a/libraries/plugins/elasticsearch/elasticsearch_plugin.cpp b/libraries/plugins/elasticsearch/elasticsearch_plugin.cpp index 4eaa6643e9..d583afccc0 100644 --- a/libraries/plugins/elasticsearch/elasticsearch_plugin.cpp +++ b/libraries/plugins/elasticsearch/elasticsearch_plugin.cpp @@ -36,7 +36,7 @@ namespace detail class elasticsearch_plugin_impl { public: - elasticsearch_plugin_impl(elasticsearch_plugin& _plugin) + explicit elasticsearch_plugin_impl(elasticsearch_plugin& _plugin) : _self( _plugin ) { curl = curl_easy_init(); @@ -102,7 +102,6 @@ elasticsearch_plugin_impl::~elasticsearch_plugin_impl() curl_easy_cleanup(curl); curl = nullptr; } - return; } bool elasticsearch_plugin_impl::update_account_histories( const signed_block& b ) @@ -437,11 +436,10 @@ elasticsearch_plugin::elasticsearch_plugin(graphene::app::application& app) : plugin(app), my( std::make_unique(*this) ) { + // Nothing else to do } -elasticsearch_plugin::~elasticsearch_plugin() -{ -} +elasticsearch_plugin::~elasticsearch_plugin() = default; std::string elasticsearch_plugin::plugin_name()const { diff --git a/libraries/plugins/elasticsearch/include/graphene/elasticsearch/elasticsearch_plugin.hpp b/libraries/plugins/elasticsearch/include/graphene/elasticsearch/elasticsearch_plugin.hpp index 61f385efa0..fbd8366b3f 100644 --- a/libraries/plugins/elasticsearch/include/graphene/elasticsearch/elasticsearch_plugin.hpp +++ b/libraries/plugins/elasticsearch/include/graphene/elasticsearch/elasticsearch_plugin.hpp @@ -55,7 +55,7 @@ enum mode { only_save = 0 , only_query = 1, all = 2 }; class elasticsearch_plugin : public graphene::app::plugin { public: - elasticsearch_plugin(graphene::app::application& app); + explicit elasticsearch_plugin(graphene::app::application& app); ~elasticsearch_plugin() override; std::string plugin_name()const override; diff --git a/libraries/plugins/es_objects/es_objects.cpp b/libraries/plugins/es_objects/es_objects.cpp index e538edd370..2e0fd4613e 100644 --- a/libraries/plugins/es_objects/es_objects.cpp +++ b/libraries/plugins/es_objects/es_objects.cpp @@ -41,7 +41,7 @@ namespace detail class es_objects_plugin_impl { public: - es_objects_plugin_impl(es_objects_plugin& _plugin) + explicit es_objects_plugin_impl(es_objects_plugin& _plugin) : _self( _plugin ) { curl = curl_easy_init(); @@ -269,7 +269,6 @@ es_objects_plugin_impl::~es_objects_plugin_impl() curl_easy_cleanup(curl); curl = nullptr; } - return; } } // end namespace detail @@ -278,11 +277,10 @@ es_objects_plugin::es_objects_plugin(graphene::app::application& app) : plugin(app), my( std::make_unique(*this) ) { + // Nothing else to do } -es_objects_plugin::~es_objects_plugin() -{ -} +es_objects_plugin::~es_objects_plugin() = default; std::string es_objects_plugin::plugin_name()const { diff --git a/libraries/plugins/es_objects/include/graphene/es_objects/es_objects.hpp b/libraries/plugins/es_objects/include/graphene/es_objects/es_objects.hpp index b9bc339681..dff4812498 100644 --- a/libraries/plugins/es_objects/include/graphene/es_objects/es_objects.hpp +++ b/libraries/plugins/es_objects/include/graphene/es_objects/es_objects.hpp @@ -38,8 +38,8 @@ namespace detail class es_objects_plugin : public graphene::app::plugin { public: - es_objects_plugin(graphene::app::application& app); - ~es_objects_plugin(); + explicit es_objects_plugin(graphene::app::application& app); + ~es_objects_plugin() override; std::string plugin_name()const override; std::string plugin_description()const override; @@ -49,7 +49,7 @@ class es_objects_plugin : public graphene::app::plugin void plugin_initialize(const boost::program_options::variables_map& options) override; void plugin_startup() override; - friend class detail::es_objects_plugin_impl; + private: std::unique_ptr my; }; diff --git a/libraries/plugins/grouped_orders/grouped_orders_plugin.cpp b/libraries/plugins/grouped_orders/grouped_orders_plugin.cpp index da655026fd..b745ea494c 100644 --- a/libraries/plugins/grouped_orders/grouped_orders_plugin.cpp +++ b/libraries/plugins/grouped_orders/grouped_orders_plugin.cpp @@ -34,9 +34,8 @@ namespace detail class grouped_orders_plugin_impl { public: - grouped_orders_plugin_impl(grouped_orders_plugin& _plugin) + explicit grouped_orders_plugin_impl(grouped_orders_plugin& _plugin) :_self( _plugin ) {} - virtual ~grouped_orders_plugin_impl(); graphene::chain::database& database() { @@ -235,9 +234,6 @@ void limit_order_group_index::remove_order( const limit_order_object& o, bool re } } -grouped_orders_plugin_impl::~grouped_orders_plugin_impl() -{} - } // end namespace detail @@ -245,11 +241,10 @@ grouped_orders_plugin::grouped_orders_plugin(graphene::app::application& app) : plugin(app), my( std::make_unique(*this) ) { + // Nothing else to do } -grouped_orders_plugin::~grouped_orders_plugin() -{ -} +grouped_orders_plugin::~grouped_orders_plugin() = default; std::string grouped_orders_plugin::plugin_name()const { diff --git a/libraries/plugins/grouped_orders/include/graphene/grouped_orders/grouped_orders_plugin.hpp b/libraries/plugins/grouped_orders/include/graphene/grouped_orders/grouped_orders_plugin.hpp index 637e6e7925..4a0e4452ea 100644 --- a/libraries/plugins/grouped_orders/include/graphene/grouped_orders/grouped_orders_plugin.hpp +++ b/libraries/plugins/grouped_orders/include/graphene/grouped_orders/grouped_orders_plugin.hpp @@ -69,7 +69,7 @@ namespace detail class grouped_orders_plugin : public graphene::app::plugin { public: - grouped_orders_plugin(graphene::app::application& app); + explicit grouped_orders_plugin(graphene::app::application& app); ~grouped_orders_plugin() override; std::string plugin_name()const override; @@ -85,7 +85,6 @@ class grouped_orders_plugin : public graphene::app::plugin const map< limit_order_group_key, limit_order_group_data >& limit_order_groups(); private: - friend class detail::grouped_orders_plugin_impl; std::unique_ptr my; }; diff --git a/libraries/plugins/market_history/include/graphene/market_history/market_history_plugin.hpp b/libraries/plugins/market_history/include/graphene/market_history/market_history_plugin.hpp index 4021dad75e..bc70ae8794 100644 --- a/libraries/plugins/market_history/include/graphene/market_history/market_history_plugin.hpp +++ b/libraries/plugins/market_history/include/graphene/market_history/market_history_plugin.hpp @@ -380,7 +380,7 @@ namespace detail class market_history_plugin : public graphene::app::plugin { public: - market_history_plugin(graphene::app::application& app); + explicit market_history_plugin(graphene::app::application& app); ~market_history_plugin() override; std::string plugin_name()const override; @@ -397,7 +397,6 @@ class market_history_plugin : public graphene::app::plugin uint32_t max_order_his_seconds_per_market()const; private: - friend class detail::market_history_plugin_impl; std::unique_ptr my; }; diff --git a/libraries/plugins/market_history/market_history_plugin.cpp b/libraries/plugins/market_history/market_history_plugin.cpp index f9211d1d36..adca4f5b77 100644 --- a/libraries/plugins/market_history/market_history_plugin.cpp +++ b/libraries/plugins/market_history/market_history_plugin.cpp @@ -43,9 +43,8 @@ namespace detail class market_history_plugin_impl { public: - market_history_plugin_impl(market_history_plugin& _plugin) + explicit market_history_plugin_impl(market_history_plugin& _plugin) :_self( _plugin ) {} - virtual ~market_history_plugin_impl(); /** this method is called as a callback after a block is applied * and will process/index all operations that were applied in the block. @@ -294,9 +293,6 @@ struct operation_process_fill_order } }; -market_history_plugin_impl::~market_history_plugin_impl() -{} - void market_history_plugin_impl::update_market_histories( const signed_block& b ) { graphene::chain::database& db = database(); @@ -731,19 +727,14 @@ void market_history_plugin_impl::update_liquidity_pool_histories( } // end namespace detail - - - - market_history_plugin::market_history_plugin(graphene::app::application& app) : plugin(app), my( std::make_unique(*this) ) { + // Nothing else to do } -market_history_plugin::~market_history_plugin() -{ -} +market_history_plugin::~market_history_plugin() = default; std::string market_history_plugin::plugin_name()const { diff --git a/libraries/plugins/template_plugin/include/graphene/template_plugin/template_plugin.hpp b/libraries/plugins/template_plugin/include/graphene/template_plugin/template_plugin.hpp index 12d4ae4d1e..b9ccee9b79 100644 --- a/libraries/plugins/template_plugin/include/graphene/template_plugin/template_plugin.hpp +++ b/libraries/plugins/template_plugin/include/graphene/template_plugin/template_plugin.hpp @@ -52,7 +52,7 @@ namespace detail class template_plugin : public graphene::app::plugin { public: - template_plugin(graphene::app::application& app); + explicit template_plugin(graphene::app::application& app); ~template_plugin() override; std::string plugin_name()const override; @@ -64,7 +64,6 @@ class template_plugin : public graphene::app::plugin void plugin_startup() override; void plugin_shutdown() override; - friend class detail::template_plugin_impl; private: void cleanup(); std::unique_ptr my; diff --git a/libraries/plugins/template_plugin/template_plugin.cpp b/libraries/plugins/template_plugin/template_plugin.cpp index 3f96a79081..72020a8588 100644 --- a/libraries/plugins/template_plugin/template_plugin.cpp +++ b/libraries/plugins/template_plugin/template_plugin.cpp @@ -32,9 +32,7 @@ namespace detail class template_plugin_impl { public: - template_plugin_impl(template_plugin& _plugin) - : _self( _plugin ) - { } + explicit template_plugin_impl( template_plugin& _plugin ); virtual ~template_plugin_impl(); void on_block( const signed_block& b ); @@ -44,12 +42,13 @@ class template_plugin_impl return _self.database(); } + friend class graphene::template_plugin::template_plugin; + + private: template_plugin& _self; std::string _plugin_option = ""; - private: - }; void template_plugin_impl::on_block( const signed_block& b ) @@ -57,9 +56,15 @@ void template_plugin_impl::on_block( const signed_block& b ) wdump((b.block_num())); } +template_plugin_impl::template_plugin_impl( template_plugin& _plugin ) : + _self( _plugin ) +{ + // Put other code here +} + template_plugin_impl::~template_plugin_impl() { - // Put the real code here + // Put the real code here. If none, remove the destructor. } } // end namespace detail From e2aa835302aae22504382684b6e9c8a6955b2765 Mon Sep 17 00:00:00 2001 From: abitmore Date: Wed, 7 Apr 2021 18:52:27 +0000 Subject: [PATCH 27/31] Rename long function names --- libraries/net/node.cpp | 8 ++++---- libraries/net/node_impl.hxx | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/libraries/net/node.cpp b/libraries/net/node.cpp index 317a9212c9..5e5559f23c 100644 --- a/libraries/net/node.cpp +++ b/libraries/net/node.cpp @@ -3004,7 +3004,7 @@ namespace graphene { namespace net { namespace detail { "process_backlog_of_sync_blocks" ); } - void node_impl::process_block_during_sync( peer_connection* originating_peer, + void node_impl::process_block_during_syncing( peer_connection* originating_peer, const graphene::net::block_message& block_message_to_process, const message_hash_type& message_hash ) { @@ -3017,7 +3017,7 @@ namespace graphene { namespace net { namespace detail { trigger_process_backlog_of_sync_blocks(); } - void node_impl::process_block_during_normal_operation( peer_connection* originating_peer, + void node_impl::process_block_when_in_sync( peer_connection* originating_peer, const graphene::net::block_message& block_message_to_process, const message_hash_type& message_hash ) { @@ -3191,7 +3191,7 @@ namespace graphene { namespace net { namespace detail { if (item_iter != originating_peer->items_requested_from_peer.end()) { originating_peer->items_requested_from_peer.erase(item_iter); - process_block_during_normal_operation(originating_peer, block_message_to_process, message_hash); + process_block_when_in_sync(originating_peer, block_message_to_process, message_hash); if (originating_peer->idle()) trigger_fetch_items_loop(); return; @@ -3210,7 +3210,7 @@ namespace graphene { namespace net { namespace detail { { originating_peer->last_sync_item_received_time = fc::time_point::now(); _active_sync_requests.erase(block_message_to_process.block_id); - process_block_during_sync(originating_peer, block_message_to_process, message_hash); + process_block_during_syncing(originating_peer, block_message_to_process, message_hash); if (originating_peer->idle()) { // we have finished fetching a batch of items, so we either need to grab another batch of items diff --git a/libraries/net/node_impl.hxx b/libraries/net/node_impl.hxx index 8a4ad5fa67..4cc89d8e0f 100644 --- a/libraries/net/node_impl.hxx +++ b/libraries/net/node_impl.hxx @@ -561,11 +561,11 @@ class node_impl : public peer_connection_delegate, public std::enable_shared_fro void send_sync_block_to_node_delegate(const graphene::net::block_message& block_message_to_send); void process_backlog_of_sync_blocks(); void trigger_process_backlog_of_sync_blocks(); - void process_block_during_sync( + void process_block_during_syncing( peer_connection* originating_peer, const graphene::net::block_message& block_message, const message_hash_type& message_hash); - void process_block_during_normal_operation( + void process_block_when_in_sync( peer_connection* originating_peer, const graphene::net::block_message& block_message, const message_hash_type& message_hash); From 6a5f57674499a617cbfc0c2405561f5b8efe346d Mon Sep 17 00:00:00 2001 From: abitmore Date: Wed, 7 Apr 2021 19:02:16 +0000 Subject: [PATCH 28/31] Fix code smells --- libraries/net/include/graphene/net/node.hpp | 2 +- libraries/net/node.cpp | 40 +++++++++++---------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/libraries/net/include/graphene/net/node.hpp b/libraries/net/include/graphene/net/node.hpp index 55d2df782f..98981a7813 100644 --- a/libraries/net/include/graphene/net/node.hpp +++ b/libraries/net/include/graphene/net/node.hpp @@ -194,7 +194,7 @@ namespace graphene { namespace net { void close(); - void set_node_delegate( std::shared_ptr del ); + void set_node_delegate( std::shared_ptr del ) const; void load_configuration( const fc::path& configuration_directory ); diff --git a/libraries/net/node.cpp b/libraries/net/node.cpp index 5e5559f23c..d3b1129c9e 100644 --- a/libraries/net/node.cpp +++ b/libraries/net/node.cpp @@ -186,7 +186,7 @@ namespace graphene { namespace net { const message_hash_type& message_content_hash ); message get_message( const message_hash_type& hash_of_message_to_lookup ); message_propagation_data get_message_propagation_data( - const message_hash_type& hash_of_message_contents_to_lookup ) const; + const message_hash_type& hash_of_msg_contents_to_lookup ) const; size_t size() const { return _message_cache.size(); } }; @@ -220,12 +220,12 @@ namespace graphene { namespace net { } message_propagation_data blockchain_tied_message_cache::get_message_propagation_data( - const message_hash_type& hash_of_message_contents_to_lookup ) const + const message_hash_type& hash_of_msg_contents_to_lookup ) const { - if( hash_of_message_contents_to_lookup != message_hash_type() ) + if( hash_of_msg_contents_to_lookup != message_hash_type() ) { message_cache_container::index::type::const_iterator iter = - _message_cache.get().find(hash_of_message_contents_to_lookup ); + _message_cache.get().find(hash_of_msg_contents_to_lookup ); if( iter != _message_cache.get().end() ) return iter->propagation_data; } @@ -3000,13 +3000,13 @@ namespace graphene { namespace net { namespace detail { { if (!_node_is_shutting_down && (!_process_backlog_of_sync_blocks_done.valid() || _process_backlog_of_sync_blocks_done.ready())) - _process_backlog_of_sync_blocks_done = fc::async( [=](){ process_backlog_of_sync_blocks(); }, + _process_backlog_of_sync_blocks_done = fc::async( [this](){ process_backlog_of_sync_blocks(); }, "process_backlog_of_sync_blocks" ); } void node_impl::process_block_during_syncing( peer_connection* originating_peer, const graphene::net::block_message& block_message_to_process, - const message_hash_type& message_hash ) + const message_hash_type& ) { VERIFY_CORRECT_THREAD(); dlog( "received a sync block from peer ${endpoint}", ("endpoint", originating_peer->get_remote_endpoint() ) ); @@ -4337,18 +4337,20 @@ namespace graphene { namespace net { namespace detail { !_bandwidth_monitor_loop_done.valid() && !_dump_node_status_task_done.valid()); if (_node_configuration.accept_incoming_connections) - _accept_loop_complete = fc::async( [=](){ accept_loop(); }, "accept_loop"); - - _p2p_network_connect_loop_done = fc::async( [=]() { p2p_network_connect_loop(); }, "p2p_network_connect_loop" ); - _fetch_sync_items_loop_done = fc::async( [=]() { fetch_sync_items_loop(); }, "fetch_sync_items_loop" ); - _fetch_item_loop_done = fc::async( [=]() { fetch_items_loop(); }, "fetch_items_loop" ); - _advertise_inventory_loop_done = fc::async( [=]() { advertise_inventory_loop(); }, "advertise_inventory_loop" ); - _kill_inactive_conns_loop_done = fc::async( [=]() { kill_inactive_conns_loop(self); }, - "kill_inactive_conns_loop" ); - _fetch_updated_peer_lists_loop_done = fc::async([=](){ fetch_updated_peer_lists_loop(); }, - "fetch_updated_peer_lists_loop"); - _bandwidth_monitor_loop_done = fc::async([=](){ bandwidth_monitor_loop(); }, "bandwidth_monitor_loop"); - _dump_node_status_task_done = fc::async([=](){ dump_node_status_task(); }, "dump_node_status_task"); + _accept_loop_complete = fc::async( [this](){ accept_loop(); }, "accept_loop"); + + _p2p_network_connect_loop_done = fc::async( [this]() { p2p_network_connect_loop(); }, + "p2p_network_connect_loop" ); + _fetch_sync_items_loop_done = fc::async( [this]() { fetch_sync_items_loop(); }, "fetch_sync_items_loop" ); + _fetch_item_loop_done = fc::async( [this]() { fetch_items_loop(); }, "fetch_items_loop" ); + _advertise_inventory_loop_done = fc::async( [this]() { advertise_inventory_loop(); }, + "advertise_inventory_loop" ); + _kill_inactive_conns_loop_done = fc::async( [this,self]() { kill_inactive_conns_loop(self); }, + "kill_inactive_conns_loop" ); + _fetch_updated_peer_lists_loop_done = fc::async([this](){ fetch_updated_peer_lists_loop(); }, + "fetch_updated_peer_lists_loop"); + _bandwidth_monitor_loop_done = fc::async([this](){ bandwidth_monitor_loop(); }, "bandwidth_monitor_loop"); + _dump_node_status_task_done = fc::async([this](){ dump_node_status_task(); }, "dump_node_status_task"); schedule_next_update_seed_nodes_task(); } @@ -4925,7 +4927,7 @@ namespace graphene { namespace net { namespace detail { // nothing to do } - void node::set_node_delegate( std::shared_ptr del ) + void node::set_node_delegate( std::shared_ptr del ) const { fc::thread* delegate_thread = &fc::thread::current(); INVOKE_IN_IMPL(set_node_delegate, del, delegate_thread); From 670f70d580c426649b82b1ddb14eff9519dec202 Mon Sep 17 00:00:00 2001 From: abitmore Date: Wed, 7 Apr 2021 22:14:57 +0000 Subject: [PATCH 29/31] Change functions to const, fix code smells --- libraries/app/application.cpp | 21 ++++++++++--------- libraries/app/application_impl.hxx | 8 +++---- .../app/include/graphene/app/application.hpp | 7 ++++--- libraries/app/include/graphene/app/plugin.hpp | 2 +- 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/libraries/app/application.cpp b/libraries/app/application.cpp index 73e3174e82..f8436ec844 100644 --- a/libraries/app/application.cpp +++ b/libraries/app/application.cpp @@ -449,7 +449,7 @@ graphene::chain::genesis_state_type application_impl::initialize_genesis_state() } FC_CAPTURE_AND_RETHROW() } -void application_impl::open_chain_database() +void application_impl::open_chain_database() const { try { fc::create_directories(_data_dir / "blockchain"); @@ -1034,9 +1034,9 @@ void application_impl::enable_plugin( const string& name ) _active_plugins[name] = _available_plugins[name]; } -void application_impl::initialize_plugins() +void application_impl::initialize_plugins() const { - for( auto& entry : _active_plugins ) + for( const auto& entry : _active_plugins ) { ilog( "Initializing plugin ${name}", ( "name", entry.second->plugin_name() ) ); entry.second->plugin_initialize( *_options ); @@ -1044,9 +1044,9 @@ void application_impl::initialize_plugins() } } -void application_impl::startup_plugins() +void application_impl::startup_plugins() const { - for( auto& entry : _active_plugins ) + for( const auto& entry : _active_plugins ) { ilog( "Starting plugin ${name}", ( "name", entry.second->plugin_name() ) ); entry.second->plugin_startup(); @@ -1054,9 +1054,9 @@ void application_impl::startup_plugins() } } -void application_impl::shutdown_plugins() +void application_impl::shutdown_plugins() const { - for( auto& entry : _active_plugins ) + for( const auto& entry : _active_plugins ) { ilog( "Stopping plugin ${name}", ( "name", entry.second->plugin_name() ) ); entry.second->plugin_shutdown(); @@ -1202,7 +1202,8 @@ void application::set_program_options(boost::program_options::options_descriptio configuration_file_options.add(_cfg_options); } -void application::initialize(const fc::path& data_dir, std::shared_ptr options) +void application::initialize(const fc::path& data_dir, + std::shared_ptr options) const { ilog( "Initializing application" ); my->initialize( data_dir, options ); @@ -1276,12 +1277,12 @@ bool application::is_finished_syncing() const return my->_is_finished_syncing; } -void application::enable_plugin(const string& name) +void application::enable_plugin(const string& name) const { my->enable_plugin(name); } -void application::add_available_plugin(std::shared_ptr p) +void application::add_available_plugin(std::shared_ptr p) const { my->add_available_plugin(p); } diff --git a/libraries/app/application_impl.hxx b/libraries/app/application_impl.hxx index 9f42800494..64775cfe0d 100644 --- a/libraries/app/application_impl.hxx +++ b/libraries/app/application_impl.hxx @@ -197,14 +197,14 @@ class application_impl : public net::node_delegate, public std::enable_shared_fr private: void shutdown(); - void initialize_plugins(); - void startup_plugins(); - void shutdown_plugins(); + void initialize_plugins() const; + void startup_plugins() const; + void shutdown_plugins() const; /// Initialize genesis state. Called by @ref open_chain_database. graphene::chain::genesis_state_type initialize_genesis_state() const; /// Open the chain database. Called by @ref startup. - void open_chain_database(); + void open_chain_database() const; friend graphene::app::application; diff --git a/libraries/app/include/graphene/app/application.hpp b/libraries/app/include/graphene/app/application.hpp index 6478a97a31..3b0cbb2808 100644 --- a/libraries/app/include/graphene/app/application.hpp +++ b/libraries/app/include/graphene/app/application.hpp @@ -85,7 +85,8 @@ namespace graphene { namespace app { void set_program_options(boost::program_options::options_description& command_line_options, boost::program_options::options_description& configuration_file_options)const; - void initialize(const fc::path& data_dir, std::shared_ptr options); + void initialize(const fc::path& data_dir, + std::shared_ptr options) const; void startup(); template @@ -139,7 +140,7 @@ namespace graphene { namespace app { const application_options& get_options(); - void enable_plugin( const string& name ); + void enable_plugin( const string& name ) const; bool is_plugin_enabled(const string& name) const; @@ -147,7 +148,7 @@ namespace graphene { namespace app { private: /// Add an available plugin - void add_available_plugin( std::shared_ptr p ); + void add_available_plugin( std::shared_ptr p ) const; std::shared_ptr my; diff --git a/libraries/app/include/graphene/app/plugin.hpp b/libraries/app/include/graphene/app/plugin.hpp index 10a750e28a..cc61aafc27 100644 --- a/libraries/app/include/graphene/app/plugin.hpp +++ b/libraries/app/include/graphene/app/plugin.hpp @@ -114,7 +114,7 @@ class plugin : public abstract_plugin chain::database& database() { return *app().chain_database(); } protected: - net::node_ptr p2p_node() { return app().p2p_node(); } + net::node_ptr p2p_node() const { return app().p2p_node(); } }; /// @ingroup Some useful tools for boost::program_options arguments using vectors of JSON strings From c3b084939a0655d41f9e9f3634b60a786df518e3 Mon Sep 17 00:00:00 2001 From: abitmore Date: Thu, 8 Apr 2021 16:49:47 +0000 Subject: [PATCH 30/31] Replace usage of std::cout and std::cerr by logger --- programs/witness_node/main.cpp | 68 +++++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 17 deletions(-) diff --git a/programs/witness_node/main.cpp b/programs/witness_node/main.cpp index 846d8cd71e..50f1562d63 100644 --- a/programs/witness_node/main.cpp +++ b/programs/witness_node/main.cpp @@ -39,6 +39,8 @@ #include #include #include +#include +#include #include #include @@ -49,7 +51,7 @@ #include #include -#include +#include #ifdef WIN32 # include @@ -59,6 +61,23 @@ namespace bpo = boost::program_options; +/// Disable default logging +void disable_default_logging() +{ + fc::configure_logging( fc::logging_config() ); +} + +/// Hack to log messages to console with default color and no format via fc::console_appender +// TODO fix console_appender and use ilog() or etc instead: 1) stream is always stderr, 2) format can not change +void my_log( const std::string& s ) +{ + static fc::console_appender::config my_console_config; + static fc::console_appender my_appender( my_console_config ); + my_appender.print(s); + my_appender.print("\n"); // This is needed, otherwise the next message will cover it +} + +/// The main program int main(int argc, char** argv) { fc::print_stacktrace_on_segfault(); auto node = std::make_unique(); @@ -114,25 +133,34 @@ int main(int argc, char** argv) { } catch (const boost::program_options::error& e) { - std::cerr << "Error parsing command line: " << e.what() << "\n"; + disable_default_logging(); + std::stringstream ss; + ss << "Error parsing command line: " << e.what(); + my_log( ss.str() ); return EXIT_FAILURE; } if( options.count("version") > 0 ) { - std::cout << "Version: " << graphene::utilities::git_revision_description << "\n"; - std::cout << "SHA: " << graphene::utilities::git_revision_sha << "\n"; - std::cout << "Timestamp: " << fc::get_approximate_relative_time_string(fc::time_point_sec( + disable_default_logging(); + std::stringstream ss; + ss << "Version: " << graphene::utilities::git_revision_description << "\n"; + ss << "SHA: " << graphene::utilities::git_revision_sha << "\n"; + ss << "Timestamp: " << fc::get_approximate_relative_time_string(fc::time_point_sec( graphene::utilities::git_revision_unix_timestamp)) << "\n"; - std::cout << "SSL: " << OPENSSL_VERSION_TEXT << "\n"; - std::cout << "Boost: " << boost::replace_all_copy(std::string(BOOST_LIB_VERSION), "_", ".") << "\n"; - std::cout << "Websocket++: " << websocketpp::major_version << "." << websocketpp::minor_version - << "." << websocketpp::patch_version << "\n"; + ss << "SSL: " << OPENSSL_VERSION_TEXT << "\n"; + ss << "Boost: " << boost::replace_all_copy(std::string(BOOST_LIB_VERSION), "_", ".") << "\n"; + ss << "Websocket++: " << websocketpp::major_version << "." << websocketpp::minor_version + << "." << websocketpp::patch_version; // No end of line in the end + my_log( ss.str() ); return EXIT_SUCCESS; } if( options.count("help") > 0 ) { - std::cout << app_options << "\n"; + disable_default_logging(); + std::stringstream ss; + ss << app_options << "\n"; + my_log( ss.str() ); return EXIT_SUCCESS; } @@ -149,16 +177,22 @@ int main(int argc, char** argv) { boost::split(plugins, options.at("plugins").as(), [](char c){return c == ' ';}); if( plugins.count("account_history") > 0 && plugins.count("elasticsearch") > 0 ) { - std::cerr << "Plugin conflict: Cannot load both account_history plugin and elasticsearch plugin\n"; + disable_default_logging(); + std::stringstream ss; + ss << "Plugin conflict: Cannot load both account_history plugin and elasticsearch plugin"; + my_log( ss.str() ); return EXIT_FAILURE; } if( plugins.count("api_helper_indexes") == 0 && options.count("ignore-api-helper-indexes-warning") == 0 && ( options.count("rpc-endpoint") > 0 || options.count("rpc-tls-endpoint") > 0 ) ) { - std::cerr << "\nIf this is an API node, please enable api_helper_indexes plugin." - "\nIf this is not an API node, please start with \"--ignore-api-helper-indexes-warning\"" - " or enable it in config.ini file.\n\n"; + disable_default_logging(); + std::stringstream ss; + ss << "\nIf this is an API node, please enable api_helper_indexes plugin." + "\nIf this is not an API node, please start with \"--ignore-api-helper-indexes-warning\"" + " or enable it in config.ini file.\n"; + my_log( ss.str() ); return EXIT_FAILURE; } @@ -177,18 +211,18 @@ int main(int argc, char** argv) { fc::promise::ptr exit_promise = fc::promise::create("UNIX Signal Handler"); fc::set_signal_handler([&exit_promise](int the_signal) { - elog( "Caught SIGINT, attempting to exit cleanly" ); + wlog( "Caught SIGINT, attempting to exit cleanly" ); exit_promise->set_value(the_signal); }, SIGINT); fc::set_signal_handler([&exit_promise](int the_signal) { - elog( "Caught SIGTERM, attempting to exit cleanly" ); + wlog( "Caught SIGTERM, attempting to exit cleanly" ); exit_promise->set_value(the_signal); }, SIGTERM); #ifdef SIGQUIT fc::set_signal_handler( [&exit_promise](int the_signal) { - elog( "Caught SIGQUIT, attempting to exit cleanly" ); + wlog( "Caught SIGQUIT, attempting to exit cleanly" ); exit_promise->set_value(the_signal); }, SIGQUIT ); #endif From 6efa4c41cd9d8c5d56422f6a27d658482e0c50bd Mon Sep 17 00:00:00 2001 From: abitmore Date: Thu, 8 Apr 2021 17:08:45 +0000 Subject: [PATCH 31/31] Add tests for program arguments in Github Actions --- .github/workflows/build-and-test.mac.yml | 27 +++++++++++++++++++ .../workflows/build-and-test.ubuntu-debug.yml | 27 +++++++++++++++++++ .../build-and-test.ubuntu-release.yml | 27 +++++++++++++++++++ .github/workflows/sonar-scan.yml | 27 +++++++++++++++++++ 4 files changed, 108 insertions(+) diff --git a/.github/workflows/build-and-test.mac.yml b/.github/workflows/build-and-test.mac.yml index 17c99cf982..ed3874107a 100644 --- a/.github/workflows/build-and-test.mac.yml +++ b/.github/workflows/build-and-test.mac.yml @@ -50,6 +50,33 @@ jobs: libraries/fc/tests/run-parallel-tests.sh _build/tests/chain_test -l test_suite _build/tests/cli_test -l test_suite df -h + - name: Quick test for program arguments + run: | + _build/programs/witness_node/witness_node --version + _build/programs/witness_node/witness_node --help + if _build/programs/witness_node/witness_node --bad-arg ; then \ + echo "Fail: did not get expected error."; false; \ + else \ + echo "Pass: got expected error."; \ + fi + if _build/programs/witness_node/witness_node --plugins "account_history elasticsearch" ; then \ + echo "Fail: did not get expected error."; false; \ + else \ + echo "Pass: got expected error."; \ + fi + if _build/programs/witness_node/witness_node --rpc-endpoint --plugins "witness"; then \ + echo "Fail: did not get expected error."; false; \ + else \ + echo "Pass: got expected error."; \ + fi + _build/programs/cli_wallet/cli_wallet --version + _build/programs/cli_wallet/cli_wallet --help + _build/programs/cli_wallet/cli_wallet --suggest-brain-key + if _build/programs/cli_wallet/cli_wallet --bad-arg ; then \ + echo "Fail: did not get expected error."; false; \ + else \ + echo "Pass: got expected error."; \ + fi - name: Node-Test run: | df -h diff --git a/.github/workflows/build-and-test.ubuntu-debug.yml b/.github/workflows/build-and-test.ubuntu-debug.yml index ddec5bdec3..3a2de40eaa 100644 --- a/.github/workflows/build-and-test.ubuntu-debug.yml +++ b/.github/workflows/build-and-test.ubuntu-debug.yml @@ -104,6 +104,33 @@ jobs: libraries/fc/tests/run-parallel-tests.sh _build/tests/chain_test -l test_suite _build/tests/cli_test -l test_suite df -h + - name: Quick test for program arguments + run: | + _build/programs/witness_node/witness_node --version + _build/programs/witness_node/witness_node --help + if _build/programs/witness_node/witness_node --bad-arg ; then \ + echo "Fail: did not get expected error."; false; \ + else \ + echo "Pass: got expected error."; \ + fi + if _build/programs/witness_node/witness_node --plugins "account_history elasticsearch" ; then \ + echo "Fail: did not get expected error."; false; \ + else \ + echo "Pass: got expected error."; \ + fi + if _build/programs/witness_node/witness_node --rpc-endpoint --plugins "witness"; then \ + echo "Fail: did not get expected error."; false; \ + else \ + echo "Pass: got expected error."; \ + fi + _build/programs/cli_wallet/cli_wallet --version + _build/programs/cli_wallet/cli_wallet --help + _build/programs/cli_wallet/cli_wallet --suggest-brain-key + if _build/programs/cli_wallet/cli_wallet --bad-arg ; then \ + echo "Fail: did not get expected error."; false; \ + else \ + echo "Pass: got expected error."; \ + fi - name: Node-Test run: | df -h diff --git a/.github/workflows/build-and-test.ubuntu-release.yml b/.github/workflows/build-and-test.ubuntu-release.yml index 5f5be7fd12..8f310d8d0a 100644 --- a/.github/workflows/build-and-test.ubuntu-release.yml +++ b/.github/workflows/build-and-test.ubuntu-release.yml @@ -80,6 +80,33 @@ jobs: libraries/fc/tests/run-parallel-tests.sh _build/tests/chain_test -l test_suite _build/tests/cli_test -l test_suite df -h + - name: Quick test for program arguments + run: | + _build/programs/witness_node/witness_node --version + _build/programs/witness_node/witness_node --help + if _build/programs/witness_node/witness_node --bad-arg ; then \ + echo "Fail: did not get expected error."; false; \ + else \ + echo "Pass: got expected error."; \ + fi + if _build/programs/witness_node/witness_node --plugins "account_history elasticsearch" ; then \ + echo "Fail: did not get expected error."; false; \ + else \ + echo "Pass: got expected error."; \ + fi + if _build/programs/witness_node/witness_node --rpc-endpoint --plugins "witness"; then \ + echo "Fail: did not get expected error."; false; \ + else \ + echo "Pass: got expected error."; \ + fi + _build/programs/cli_wallet/cli_wallet --version + _build/programs/cli_wallet/cli_wallet --help + _build/programs/cli_wallet/cli_wallet --suggest-brain-key + if _build/programs/cli_wallet/cli_wallet --bad-arg ; then \ + echo "Fail: did not get expected error."; false; \ + else \ + echo "Pass: got expected error."; \ + fi - name: Node-Test run: | df -h diff --git a/.github/workflows/sonar-scan.yml b/.github/workflows/sonar-scan.yml index e7af75ea45..cda85540b1 100644 --- a/.github/workflows/sonar-scan.yml +++ b/.github/workflows/sonar-scan.yml @@ -129,6 +129,33 @@ jobs: echo "Cleanup" rm -rf /tmp/graphene* df -h + - name: Quick test for program arguments + run: | + _build/programs/witness_node/witness_node --version + _build/programs/witness_node/witness_node --help + if _build/programs/witness_node/witness_node --bad-arg ; then \ + echo "Fail: did not get expected error."; false; \ + else \ + echo "Pass: got expected error."; \ + fi + if _build/programs/witness_node/witness_node --plugins "account_history elasticsearch" ; then \ + echo "Fail: did not get expected error."; false; \ + else \ + echo "Pass: got expected error."; \ + fi + if _build/programs/witness_node/witness_node --rpc-endpoint --plugins "witness"; then \ + echo "Fail: did not get expected error."; false; \ + else \ + echo "Pass: got expected error."; \ + fi + _build/programs/cli_wallet/cli_wallet --version + _build/programs/cli_wallet/cli_wallet --help + _build/programs/cli_wallet/cli_wallet --suggest-brain-key + if _build/programs/cli_wallet/cli_wallet --bad-arg ; then \ + echo "Fail: did not get expected error."; false; \ + else \ + echo "Pass: got expected error."; \ + fi - name: Prepare for scanning with SonarScanner run: | mkdir -p sonar_cache