From aab2dac1c2dd77078f2b23434b24eeb3ce0bbe50 Mon Sep 17 00:00:00 2001 From: timur <12267899-timur.5@users.noreply.gitlab.com> Date: Mon, 28 Nov 2022 06:38:50 -0400 Subject: [PATCH] SON conn. pool --- .../peerplays_sidechain/common/rpc_client.cpp | 29 ++++++---- .../peerplays_sidechain/common/rpc_client.hpp | 9 ++++ .../sidechain_net_handler_bitcoin.hpp | 2 +- .../sidechain_net_handler_ethereum.hpp | 2 +- .../sidechain_net_handler_hive.hpp | 2 +- .../peerplays_sidechain_plugin.cpp | 20 ------- .../sidechain_net_handler_bitcoin.cpp | 6 +-- .../sidechain_net_handler_ethereum.cpp | 6 +-- .../sidechain_net_handler_hive.cpp | 53 +++++++++---------- 9 files changed, 61 insertions(+), 68 deletions(-) diff --git a/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp b/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp index 910dc846..aa8fbe87 100644 --- a/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp +++ b/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include @@ -28,9 +28,12 @@ public: rpc_connection(std::string _url, std::string _user, std::string _password, bool _debug_rpc_calls); protected: - std::string retrieve_array_value_from_reply(std::string reply_str, std::string array_path, uint32_t idx); - std::string retrieve_value_from_reply(std::string reply_str, std::string value_path); + //static std::string retrieve_array_value_from_reply(std::string reply_str, std::string array_path, uint32_t idx); + //static std::string retrieve_value_from_reply(std::string reply_str, std::string value_path); + +public: std::string send_post_request(std::string method, std::string params, bool show_log); +protected: std::string url; std::string user; @@ -96,7 +99,7 @@ rpc_connection::rpc_connection(std::string _url, std::string _user, std::string } } -std::string rpc_connection::retrieve_array_value_from_reply(std::string reply_str, std::string array_path, uint32_t idx) { +std::string rpc_client::retrieve_array_value_from_reply(std::string reply_str, std::string array_path, uint32_t idx) { if (reply_str.empty()) { wlog("RPC call ${function}, empty reply string", ("function", __FUNCTION__)); return ""; @@ -133,7 +136,7 @@ std::string rpc_connection::retrieve_array_value_from_reply(std::string reply_st return ""; } -std::string rpc_connection::retrieve_value_from_reply(std::string reply_str, std::string value_path) { +std::string rpc_client::retrieve_value_from_reply(std::string reply_str, std::string value_path) { if (reply_str.empty()) { wlog("RPC call ${function}, empty reply string", ("function", __FUNCTION__)); return ""; @@ -282,7 +285,8 @@ rpc_reply rpc_connection::send_post_request(std::string body, bool show_log) { return reply; } -rpc_client::rpc_client(const std::vector &_urls, const std::vector &_users, const std::vector &_passwords, bool _debug_rpc_calls) +rpc_client::rpc_client(const std::vector &_urls, const std::vector &_users, const std::vector &_passwords, bool _debug_rpc_calls): + debug_rpc_calls(_debug_rpc_calls) { FC_ASSERT(_urls.size()); FC_ASSERT(_users.size() == _urls.size() && _passwords.size() == _urls.size()); @@ -298,11 +302,11 @@ void rpc_client::schedule_connection_selection() static const int64_t time_to_next_conn_selection = 2000000; fc::time_point next_wakeup = now + fc::microseconds(time_to_next_conn_selection); connection_selection_task = fc::schedule([this] { - reselect_connection(); + select_connection(); }, next_wakeup, "SON RPC connection selection"); } -void rpc_client::reselect_connection() +void rpc_client::select_connection() { //ilog("n_active_rpc_client=${n}", ("n", n_active_rpc_client)); FC_ASSERT(connections.size()); @@ -311,7 +315,7 @@ void rpc_client::reselect_connection() int best_quality = -1; std::vector head_block_numbers; - head_block_numbers.resize(rpc_clients.size()); + head_block_numbers.resize(connections.size()); for (size_t n=0; n < connections.size(); n++) { rpc_connection *conn = connections[n]; @@ -359,7 +363,12 @@ rpc_connection &rpc_client::get_active_connection() const std::string rpc_client::send_post_request(std::string method, std::string params, bool show_log) { - return get_active_connection().send_post_request(method, params, show_log); + return send_post_request(get_active_connection(), method, params, show_log); +} + +std::string rpc_client::send_post_request(rpc_connection &conn, std::string method, std::string params, bool show_log) +{ + return conn.send_post_request(method, params, show_log); } rpc_client::~rpc_client() diff --git a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/common/rpc_client.hpp b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/common/rpc_client.hpp index 83cb8171..62270eeb 100644 --- a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/common/rpc_client.hpp +++ b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/common/rpc_client.hpp @@ -3,6 +3,9 @@ #include #include +#include +#include + #include #include @@ -16,8 +19,14 @@ public: ~rpc_client(); protected: + bool debug_rpc_calls; std::string send_post_request(std::string method, std::string params, bool show_log); + static std::string send_post_request(rpc_connection &conn, std::string method, std::string params, bool show_log); + + static std::string retrieve_array_value_from_reply(std::string reply_str, std::string array_path, uint32_t idx); + static std::string retrieve_value_from_reply(std::string reply_str, std::string value_path); + private: std::vector connections; int n_active_conn; diff --git a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_bitcoin.hpp b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_bitcoin.hpp index 4478b850..83f204b3 100644 --- a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_bitcoin.hpp +++ b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_bitcoin.hpp @@ -42,7 +42,7 @@ public: }; public: - bitcoin_rpc_client(std::string _url, std::string _user, std::string _password, bool _debug_rpc_calls); + bitcoin_rpc_client(const std::vector &_urls, const std::vector &_users, const std::vector &_passwords, bool _debug_rpc_calls); std::string createwallet(const std::string &wallet_name); uint64_t estimatesmartfee(uint16_t conf_target = 128); diff --git a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_ethereum.hpp b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_ethereum.hpp index e6e8f298..919e87aa 100644 --- a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_ethereum.hpp +++ b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_ethereum.hpp @@ -13,7 +13,7 @@ namespace graphene { namespace peerplays_sidechain { class ethereum_rpc_client : public rpc_client { public: - ethereum_rpc_client(const std::string &url, const std::string &user_name, const std::string &password, bool debug_rpc_calls); + ethereum_rpc_client(const std::vector &urls, const std::vector &user_names, const std::vector &passwords, bool debug_rpc_calls); std::string eth_blockNumber(); std::string eth_get_block_by_number(std::string block_number, bool full_block); diff --git a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_hive.hpp b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_hive.hpp index 6ff4794d..87b08644 100644 --- a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_hive.hpp +++ b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_hive.hpp @@ -13,7 +13,7 @@ namespace graphene { namespace peerplays_sidechain { class hive_rpc_client : public rpc_client { public: - hive_rpc_client(const std::string &url, const std::string &user_name, const std::string &password, bool debug_rpc_calls); + hive_rpc_client(const std::vector &urls, const std::vector &user_names, const std::vector &passwords, bool debug_rpc_calls); std::string account_history_api_get_transaction(std::string transaction_id); std::string block_api_get_block(uint32_t block_number); diff --git a/libraries/plugins/peerplays_sidechain/peerplays_sidechain_plugin.cpp b/libraries/plugins/peerplays_sidechain/peerplays_sidechain_plugin.cpp index db1a9994..5f676869 100644 --- a/libraries/plugins/peerplays_sidechain/peerplays_sidechain_plugin.cpp +++ b/libraries/plugins/peerplays_sidechain/peerplays_sidechain_plugin.cpp @@ -865,26 +865,6 @@ void peerplays_sidechain_plugin_impl::on_applied_block(const signed_block &b) { } } -void peerplays_sidechain_plugin_impl::schedule_rpc_client_selection() { - fc::time_point now = fc::time_point::now(); - static const int64_t time_to_next_rpc_selection = 100000;//5000000; - fc::time_point next_wakeup = now + fc::microseconds(time_to_next_rpc_selection); - _rpc_client_selection_task = fc::schedule([this] { - rpc_client_selection(); - }, - next_wakeup, "SON RPC client selection"); -} - -void peerplays_sidechain_plugin_impl::rpc_client_selection() { - for (const auto &active_sidechain_type : active_sidechain_types) { - if (net_handlers.at(active_sidechain_type)) { - net_handlers.at(active_sidechain_type)->select_active_rpc_client(); - } - } - schedule_rpc_client_selection(); -} - - } // namespace detail peerplays_sidechain_plugin::peerplays_sidechain_plugin() : diff --git a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_bitcoin.cpp b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_bitcoin.cpp index 0e7d05b6..78dd06b2 100644 --- a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_bitcoin.cpp +++ b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_bitcoin.cpp @@ -25,8 +25,8 @@ namespace graphene { namespace peerplays_sidechain { // ============================================================================= -bitcoin_rpc_client::bitcoin_rpc_client(std::string _url, std::string _user, std::string _password, bool _debug_rpc_calls) : - rpc_client(_url, _user, _password, _debug_rpc_calls) { +bitcoin_rpc_client::bitcoin_rpc_client(const std::vector &_urls, const std::vector &_users, const std::vector &_passwords, bool _debug_rpc_calls) : + rpc_client(_urls, _users, _passwords, _debug_rpc_calls) { } std::string bitcoin_rpc_client::createwallet(const std::string &wallet_name) { @@ -358,7 +358,7 @@ sidechain_net_handler_bitcoin::sidechain_net_handler_bitcoin(peerplays_sidechain url = url + "/wallet/" + wallet_name; } - bitcoin_client = std::unique_ptr(new bitcoin_rpc_client(url, rpc_user, rpc_password, debug_rpc_calls)); + bitcoin_client = std::unique_ptr(new bitcoin_rpc_client({url}, {rpc_user}, {rpc_password}, debug_rpc_calls)); if (!wallet_name.empty()) { bitcoin_client->loadwallet(wallet_name); } diff --git a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp index c827a721..5254412b 100644 --- a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp +++ b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp @@ -24,8 +24,8 @@ namespace graphene { namespace peerplays_sidechain { -ethereum_rpc_client::ethereum_rpc_client(const std::string &url, const std::string &user_name, const std::string &password, bool debug_rpc_calls) : - rpc_client(url, user_name, password, debug_rpc_calls) { +ethereum_rpc_client::ethereum_rpc_client(const std::vector &urls, const std::vector &user_names, const std::vector &passwords, bool debug_rpc_calls) : + rpc_client(urls, user_names, passwords, debug_rpc_calls) { } std::string ethereum_rpc_client::eth_blockNumber() { @@ -155,7 +155,7 @@ sidechain_net_handler_ethereum::sidechain_net_handler_ethereum(peerplays_sidecha } } - rpc_client = new ethereum_rpc_client(rpc_url, rpc_user, rpc_password, debug_rpc_calls); + rpc_client = new ethereum_rpc_client({rpc_url}, {rpc_user}, {rpc_password}, debug_rpc_calls); const std::string chain_id_str = rpc_client->get_chain_id(); if (chain_id_str.empty()) { diff --git a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp index 853faf16..c9729f07 100644 --- a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp +++ b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp @@ -30,8 +30,8 @@ namespace graphene { namespace peerplays_sidechain { -hive_rpc_client::hive_rpc_client(const std::string &url, const std::string &user_name, const std::string &password, bool debug_rpc_calls) : - rpc_client(url, user_name, password, debug_rpc_calls) { +hive_rpc_client::hive_rpc_client(const std::vector &urls, const std::vector &user_names, const std::vector &passwords, bool debug_rpc_calls) : + rpc_client(urls, user_names, passwords, debug_rpc_calls) { } std::string hive_rpc_client::account_history_api_get_transaction(std::string transaction_id) { @@ -112,9 +112,8 @@ std::string hive_rpc_client::get_last_irreversible_block_num() { return retrieve_value_from_reply(reply_str, "last_irreversible_block_num"); } -uint64_t hive_rpc_client::ping(rpc_connection &conn) const -{ - const std::string reply = conn.send_post_request("database_api.get_dynamic_global_properties", "", debug_rpc_calls); +uint64_t hive_rpc_client::ping(rpc_connection &conn) const { + const std::string reply = send_post_request(conn, "database_api.get_dynamic_global_properties", "", debug_rpc_calls); if (!reply.empty()) { std::stringstream ss(reply); boost::property_tree::ptree json; @@ -134,7 +133,7 @@ sidechain_net_handler_hive::sidechain_net_handler_hive(peerplays_sidechain_plugi debug_rpc_calls = options.at("debug-rpc-calls").as(); } - rpc_urls = options.at("hive-node-rpc-url").as>(); + rpc_url = options.at("hive-node-rpc-url").as(); if (options.count("hive-rpc-user")) { rpc_user = options.at("hive-rpc-user").as(); } else { @@ -160,31 +159,27 @@ sidechain_net_handler_hive::sidechain_net_handler_hive(peerplays_sidechain_plugi } } + rpc_client = new hive_rpc_client(rpc_url, rpc_user, rpc_password, debug_rpc_calls); - for (const std::string &rpc_url : rpc_urls) { - hive_rpc_client *rpc_client = new hive_rpc_client(rpc_url, rpc_user, rpc_password, debug_rpc_calls); - rpc_clients.push_back(rpc_client); + const std::string chain_id_str = rpc_client->get_chain_id(); + if (chain_id_str.empty()) { + elog("No Hive node running at ${url}", ("url", rpc_url)); + FC_ASSERT(false); + } + chain_id = chain_id_type(chain_id_str); - const std::string chain_id_str = rpc_client->get_chain_id(); - if (chain_id_str.empty()) { - elog("No Hive node running at ${url}", ("url", rpc_url)); - FC_ASSERT(false); - } - chain_id = chain_id_type(chain_id_str); - - const std::string is_test_net = rpc_client->get_is_test_net(); - network_type = is_test_net.compare("true") == 0 ? hive::network::testnet : hive::network::mainnet; - if (network_type == hive::network::mainnet) { - ilog("Running on Hive mainnet, chain id ${chain_id_str}", ("chain_id_str", chain_id_str)); - hive::asset::hbd_symbol_ser = HBD_SYMBOL_SER; - hive::asset::hive_symbol_ser = HIVE_SYMBOL_SER; - hive::public_key_type::prefix = KEY_PREFIX_STM; - } else { - ilog("Running on Hive testnet, chain id ${chain_id_str}", ("chain_id_str", chain_id_str)); - hive::asset::hbd_symbol_ser = TBD_SYMBOL_SER; - hive::asset::hive_symbol_ser = TESTS_SYMBOL_SER; - hive::public_key_type::prefix = KEY_PREFIX_TST; - } + const std::string is_test_net = rpc_client->get_is_test_net(); + network_type = is_test_net.compare("true") == 0 ? hive::network::testnet : hive::network::mainnet; + if (network_type == hive::network::mainnet) { + ilog("Running on Hive mainnet, chain id ${chain_id_str}", ("chain_id_str", chain_id_str)); + hive::asset::hbd_symbol_ser = HBD_SYMBOL_SER; + hive::asset::hive_symbol_ser = HIVE_SYMBOL_SER; + hive::public_key_type::prefix = KEY_PREFIX_STM; + } else { + ilog("Running on Hive testnet, chain id ${chain_id_str}", ("chain_id_str", chain_id_str)); + hive::asset::hbd_symbol_ser = TBD_SYMBOL_SER; + hive::asset::hive_symbol_ser = TESTS_SYMBOL_SER; + hive::public_key_type::prefix = KEY_PREFIX_TST; } last_block_received = 0;