Replace Hive cli_wallet RPC calls with Hive node RPC calls

This commit is contained in:
serkixenos 2021-07-13 13:47:50 +02:00
parent 9344ed2778
commit 143b244ccd
5 changed files with 61 additions and 65 deletions

View file

@ -21,6 +21,35 @@ rpc_client::rpc_client(std::string _ip, uint32_t _port, std::string _user, std::
authorization.val = "Basic " + fc::base64_encode(user + ":" + password);
}
std::string rpc_client::retrieve_array_value_from_reply(std::string reply_str, std::string array_path, uint32_t idx) {
std::stringstream ss(reply_str);
boost::property_tree::ptree json;
boost::property_tree::read_json(ss, json);
if (json.find("result") == json.not_found()) {
return "";
}
auto json_result = json.get_child("result");
if (json_result.find(array_path) == json_result.not_found()) {
return "";
}
boost::property_tree::ptree array_ptree = json_result;
if (!array_path.empty()) {
array_ptree = json_result.get_child(array_path);
}
uint32_t array_el_idx = -1;
for (const auto &array_el : array_ptree) {
array_el_idx = array_el_idx + 1;
if (array_el_idx == idx) {
std::stringstream ss_res;
boost::property_tree::json_parser::write_json(ss_res, array_el.second);
return ss_res.str();
}
}
return "";
}
std::string rpc_client::retrieve_value_from_reply(std::string reply_str, std::string value_path) {
std::stringstream ss(reply_str);
boost::property_tree::ptree json;

View file

@ -12,6 +12,7 @@ public:
rpc_client(std::string _ip, uint32_t _port, std::string _user, std::string _password);
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);
std::string send_post_request(std::string method, std::string params, bool show_log);

View file

@ -18,11 +18,14 @@ public:
std::string account_history_api_get_transaction(std::string transaction_id);
std::string block_api_get_block(uint32_t block_number);
std::string condenser_api_get_accounts(std::vector<std::string> accounts);
std::string condenser_api_get_config();
std::string database_api_get_dynamic_global_properties();
std::string database_api_get_version();
std::string network_broadcast_api_broadcast_transaction(std::string htrx);
std::string get_account(std::string account);
std::string get_account_memo_key(std::string account);
std::string get_chain_id();
std::string get_head_block_id();
std::string get_head_block_time();
@ -30,15 +33,6 @@ public:
std::string get_last_irreversible_block_num();
};
class hive_wallet_rpc_client : public rpc_client {
public:
hive_wallet_rpc_client(std::string _ip, uint32_t _port, std::string _user, std::string _password);
std::string get_account(std::string account);
std::string get_account_memo_key(std::string account);
};
class sidechain_net_handler_hive : public sidechain_net_handler {
public:
sidechain_net_handler_hive(peerplays_sidechain_plugin &_plugin, const boost::program_options::variables_map &options);
@ -60,12 +54,6 @@ private:
std::string node_rpc_password;
hive_node_rpc_client *node_rpc_client;
std::string wallet_ip;
uint32_t wallet_rpc_port;
std::string wallet_rpc_user;
std::string wallet_rpc_password;
hive_wallet_rpc_client *wallet_rpc_client;
hive::chain_id_type chain_id;
hive::network network_type;

View file

@ -158,10 +158,6 @@ void peerplays_sidechain_plugin_impl::plugin_set_program_options(
cli.add_options()("hive-node-rpc-port", bpo::value<uint32_t>()->default_value(28090), "Hive node RPC port");
cli.add_options()("hive-node-rpc-user", bpo::value<string>(), "Hive node RPC user");
cli.add_options()("hive-node-rpc-password", bpo::value<string>(), "Hive node RPC password");
cli.add_options()("hive-wallet-ip", bpo::value<string>()->default_value("127.0.0.1"), "Hive wallet IP address");
cli.add_options()("hive-wallet-rpc-port", bpo::value<uint32_t>()->default_value(28091), "Hive wallet RPC port ");
cli.add_options()("hive-wallet-rpc-user", bpo::value<string>(), "Hive wallet RPC user");
cli.add_options()("hive-wallet-rpc-password", bpo::value<string>(), "Hive wallet RPC password");
cli.add_options()("hive-private-key", bpo::value<vector<string>>()->composing()->multitoken()->DEFAULT_VALUE_VECTOR(std::make_pair("TST6LLegbAgLAy28EHrffBVuANFWcFgmqRMW13wBmTExqFE9SCkg4", "5JNHfZYKGaomSFvd4NUdQ9qMcEAC43kujbfjueTHpVapX1Kzq2n")),
"Tuple of [Hive public key, Hive private key] (may specify multiple times)");
@ -234,9 +230,6 @@ void peerplays_sidechain_plugin_impl::plugin_initialize(const boost::program_opt
config_ready_hive = options.count("hive-node-ip") &&
options.count("hive-node-rpc-port") &&
/*options.count("hive-node-rpc-user") && options.count("hive-node-rpc-password") &&*/
options.count("hive-wallet-ip") &&
options.count("hive-wallet-rpc-port") &&
/*options.count("hive-wallet-rpc-user") && options.count("hive-wallet-rpc-password") &&*/
options.count("hive-private-key");
if (!config_ready_hive) {
wlog("Haven't set up Hive sidechain parameters");

View file

@ -42,6 +42,18 @@ std::string hive_node_rpc_client::block_api_get_block(uint32_t block_number) {
return send_post_request("block_api.get_block", params, false);
}
std::string hive_node_rpc_client::condenser_api_get_accounts(std::vector<std::string> accounts) {
std::string params = "";
for (auto account : accounts) {
if (!params.empty()) {
params = params + ",";
}
params = "\"" + account + "\"";
}
params = "[[" + params + "]]";
return send_post_request("condenser_api.get_accounts", params, false);
}
std::string hive_node_rpc_client::condenser_api_get_config() {
std::string params = "[]";
return send_post_request("condenser_api.get_config", params, false);
@ -60,6 +72,19 @@ std::string hive_node_rpc_client::network_broadcast_api_broadcast_transaction(st
return send_post_request("network_broadcast_api.broadcast_transaction", params, false);
}
std::string hive_node_rpc_client::get_account(std::string account) {
std::vector<std::string> accounts;
accounts.push_back(account);
std::string reply_str = condenser_api_get_accounts(accounts);
return retrieve_array_value_from_reply(reply_str, "", 0);
}
std::string hive_node_rpc_client::get_account_memo_key(std::string account) {
std::string reply_str = get_account(account);
reply_str = "{\"result\":" + reply_str + "}";
return retrieve_value_from_reply(reply_str, "memo_key");
}
std::string hive_node_rpc_client::get_chain_id() {
std::string reply_str = database_api_get_version();
return retrieve_value_from_reply(reply_str, "chain_id");
@ -85,20 +110,6 @@ std::string hive_node_rpc_client::get_last_irreversible_block_num() {
return retrieve_value_from_reply(reply_str, "last_irreversible_block_num");
}
hive_wallet_rpc_client::hive_wallet_rpc_client(std::string _ip, uint32_t _port, std::string _user, std::string _password) :
rpc_client(_ip, _port, _user, _password) {
}
std::string hive_wallet_rpc_client::get_account(std::string account) {
std::string params = "[\"" + account + "\"]";
return send_post_request("get_account", params, false);
}
std::string hive_wallet_rpc_client::get_account_memo_key(std::string account) {
std::string reply_str = get_account(account);
return retrieve_value_from_reply(reply_str, "memo_key");
}
sidechain_net_handler_hive::sidechain_net_handler_hive(peerplays_sidechain_plugin &_plugin, const boost::program_options::variables_map &options) :
sidechain_net_handler(_plugin, options) {
sidechain = sidechain_type::hive;
@ -116,19 +127,6 @@ sidechain_net_handler_hive::sidechain_net_handler_hive(peerplays_sidechain_plugi
node_rpc_password = "";
}
wallet_ip = options.at("hive-wallet-ip").as<std::string>();
wallet_rpc_port = options.at("hive-wallet-rpc-port").as<uint32_t>();
if (options.count("hive-wallet-rpc-user")) {
wallet_rpc_user = options.at("hive-wallet-rpc-user").as<std::string>();
} else {
wallet_rpc_user = "";
}
if (options.count("hive-wallet-rpc-password")) {
wallet_rpc_password = options.at("hive-wallet-rpc-password").as<std::string>();
} else {
wallet_rpc_password = "";
}
if (options.count("hive-private-key")) {
const std::vector<std::string> pub_priv_keys = options["hive-private-key"].as<std::vector<std::string>>();
for (const std::string &itr_key_pair : pub_priv_keys) {
@ -148,17 +146,9 @@ sidechain_net_handler_hive::sidechain_net_handler_hive(peerplays_sidechain_plugi
elog("No Hive node running at ${ip} or wrong rpc port: ${port}", ("ip", node_ip)("port", node_rpc_port));
FC_ASSERT(false);
}
try {
conn.connect_to(fc::ip::endpoint(fc::ip::address(wallet_ip), wallet_rpc_port));
} catch (fc::exception &e) {
elog("No Hive wallet running at ${ip} or wrong rpc port: ${port}", ("ip", wallet_ip)("port", wallet_rpc_port));
FC_ASSERT(false);
}
node_rpc_client = new hive_node_rpc_client(node_ip, node_rpc_port, node_rpc_user, node_rpc_password);
wallet_rpc_client = new hive_wallet_rpc_client(wallet_ip, wallet_rpc_port, wallet_rpc_user, wallet_rpc_password);
std::string chain_id_str = node_rpc_client->get_chain_id();
chain_id = chain_id_type(chain_id_str);
@ -183,7 +173,6 @@ sidechain_net_handler_hive::~sidechain_net_handler_hive() {
}
bool sidechain_net_handler_hive::process_proposal(const proposal_object &po) {
//ilog("Proposal to process: ${po}, SON id ${son_id}", ("po", po.id)("son_id", plugin.get_current_son_id()));
bool should_approve = false;
@ -258,7 +247,7 @@ bool sidechain_net_handler_hive::process_proposal(const proposal_object &po) {
account_auths[wallet_son.sidechain_public_keys.at(sidechain)] = wallet_son.weight;
}
std::string memo_key = wallet_rpc_client->get_account_memo_key("son-account");
std::string memo_key = node_rpc_client->get_account_memo_key("son-account");
hive::authority active;
active.weight_threshold = total_weight * 2 / 3 + 1;
@ -493,7 +482,8 @@ void sidechain_net_handler_hive::process_primary_wallet() {
account_auths[active_son.sidechain_public_keys.at(sidechain)] = active_son.weight;
}
std::string memo_key = wallet_rpc_client->get_account_memo_key("son-account");
std::string memo_key = node_rpc_client->get_account_memo_key("son-account");
if (memo_key.empty()) {
return;
}
@ -600,7 +590,6 @@ void sidechain_net_handler_hive::process_sidechain_addresses() {
}
bool sidechain_net_handler_hive::process_deposit(const son_wallet_deposit_object &swdo) {
const chain::global_property_object &gpo = database.get_global_properties();
price asset_price;
@ -647,7 +636,6 @@ bool sidechain_net_handler_hive::process_deposit(const son_wallet_deposit_object
}
bool sidechain_net_handler_hive::process_withdrawal(const son_wallet_withdraw_object &swwo) {
const chain::global_property_object &gpo = database.get_global_properties();
//=====
@ -722,7 +710,6 @@ bool sidechain_net_handler_hive::process_withdrawal(const son_wallet_withdraw_ob
}
std::string sidechain_net_handler_hive::process_sidechain_transaction(const sidechain_transaction_object &sto) {
std::stringstream ss_trx(boost::algorithm::unhex(sto.transaction));
hive::signed_transaction htrx;
fc::raw::unpack(ss_trx, htrx, 1000);
@ -741,7 +728,6 @@ std::string sidechain_net_handler_hive::process_sidechain_transaction(const side
}
std::string sidechain_net_handler_hive::send_sidechain_transaction(const sidechain_transaction_object &sto) {
std::stringstream ss_trx(boost::algorithm::unhex(sto.transaction));
hive::signed_transaction htrx;
fc::raw::unpack(ss_trx, htrx, 1000);
@ -762,7 +748,6 @@ std::string sidechain_net_handler_hive::send_sidechain_transaction(const sidecha
}
bool sidechain_net_handler_hive::settle_sidechain_transaction(const sidechain_transaction_object &sto, asset &settle_amount) {
if (sto.object_id.is<son_wallet_id_type>()) {
settle_amount.amount = 0;
return true;