SON conn. pool

This commit is contained in:
timur 2022-11-28 06:38:50 -04:00
parent 6bc8a10c67
commit aab2dac1c2
9 changed files with 61 additions and 68 deletions

View file

@ -1,4 +1,4 @@
#include <graphene/peerplays_sidechain/common/rpc_connection.hpp>
#include <graphene/peerplays_sidechain/common/rpc_client.hpp>
#include <regex>
#include <sstream>
@ -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<std::string> &_urls, const std::vector<std::string> &_users, const std::vector<std::string> &_passwords, bool _debug_rpc_calls)
rpc_client::rpc_client(const std::vector<std::string> &_urls, const std::vector<std::string> &_users, const std::vector<std::string> &_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<uint64_t> 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()

View file

@ -3,6 +3,9 @@
#include <cstdint>
#include <string>
#include <fc/thread/future.hpp>
#include <fc/thread/thread.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/beast/core.hpp>
@ -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<rpc_connection*> connections;
int n_active_conn;

View file

@ -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<std::string> &_urls, const std::vector<std::string> &_users, const std::vector<std::string> &_passwords, bool _debug_rpc_calls);
std::string createwallet(const std::string &wallet_name);
uint64_t estimatesmartfee(uint16_t conf_target = 128);

View file

@ -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<std::string> &urls, const std::vector<std::string> &user_names, const std::vector<std::string> &passwords, bool debug_rpc_calls);
std::string eth_blockNumber();
std::string eth_get_block_by_number(std::string block_number, bool full_block);

View file

@ -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<std::string> &urls, const std::vector<std::string> &user_names, const std::vector<std::string> &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);

View file

@ -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() :

View file

@ -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<std::string> &_urls, const std::vector<std::string> &_users, const std::vector<std::string> &_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<bitcoin_rpc_client>(new bitcoin_rpc_client(url, rpc_user, rpc_password, debug_rpc_calls));
bitcoin_client = std::unique_ptr<bitcoin_rpc_client>(new bitcoin_rpc_client({url}, {rpc_user}, {rpc_password}, debug_rpc_calls));
if (!wallet_name.empty()) {
bitcoin_client->loadwallet(wallet_name);
}

View file

@ -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<std::string> &urls, const std::vector<std::string> &user_names, const std::vector<std::string> &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()) {

View file

@ -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<std::string> &urls, const std::vector<std::string> &user_names, const std::vector<std::string> &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<bool>();
}
rpc_urls = options.at("hive-node-rpc-url").as<std::vector<std::string>>();
rpc_url = options.at("hive-node-rpc-url").as<std::string>();
if (options.count("hive-rpc-user")) {
rpc_user = options.at("hive-rpc-user").as<std::string>();
} 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;