Comments applied
show_log move net_utl to other files single IP resolve removed custom exception handler
This commit is contained in:
parent
876c0fcb71
commit
68b33c816c
5 changed files with 71 additions and 105 deletions
|
|
@ -15,7 +15,6 @@ add_library( peerplays_sidechain
|
||||||
bitcoin/utils.cpp
|
bitcoin/utils.cpp
|
||||||
bitcoin/sign_bitcoin_transaction.cpp
|
bitcoin/sign_bitcoin_transaction.cpp
|
||||||
common/rpc_client.cpp
|
common/rpc_client.cpp
|
||||||
common/net_utl.cpp
|
|
||||||
common/utils.cpp
|
common/utils.cpp
|
||||||
hive/asset.cpp
|
hive/asset.cpp
|
||||||
hive/operations.cpp
|
hive/operations.cpp
|
||||||
|
|
|
||||||
|
|
@ -1,25 +0,0 @@
|
||||||
#include "net_utl.hpp"
|
|
||||||
|
|
||||||
#include <boost/asio.hpp>
|
|
||||||
|
|
||||||
namespace graphene { namespace peerplays_sidechain {
|
|
||||||
|
|
||||||
std::string resolve_host_addr(const std::string &host_name) {
|
|
||||||
using namespace boost::asio;
|
|
||||||
io_service service;
|
|
||||||
ip::tcp::resolver resolver(service);
|
|
||||||
auto query = ip::tcp::resolver::query(host_name, std::string());
|
|
||||||
auto iter = resolver.resolve(query);
|
|
||||||
auto endpoint = *iter;
|
|
||||||
auto addr = ((ip::tcp::endpoint)endpoint).address();
|
|
||||||
return addr.to_string();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string strip_proto_name(const std::string &url) {
|
|
||||||
auto index = url.find("://");
|
|
||||||
if (index == std::string::npos)
|
|
||||||
return url;
|
|
||||||
return url.substr(index + 3);
|
|
||||||
}
|
|
||||||
|
|
||||||
}} // namespace graphene::peerplays_sidechain
|
|
||||||
|
|
@ -1,15 +0,0 @@
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
namespace graphene { namespace peerplays_sidechain {
|
|
||||||
|
|
||||||
// resolve IP address by host name
|
|
||||||
// ex: api.hive.blog -> 52.79.10.214
|
|
||||||
std::string resolve_host_addr(const std::string &host_name);
|
|
||||||
|
|
||||||
// remove schema part from URL
|
|
||||||
// ex: http://api.hive.blog -> api.hive.blog
|
|
||||||
std::string strip_proto_name(const std::string &url);
|
|
||||||
|
|
||||||
}} // namespace graphene::peerplays_sidechain
|
|
||||||
|
|
@ -19,8 +19,6 @@
|
||||||
#include <boost/algorithm/string/case_conv.hpp>
|
#include <boost/algorithm/string/case_conv.hpp>
|
||||||
#include <boost/algorithm/string/trim.hpp>
|
#include <boost/algorithm/string/trim.hpp>
|
||||||
|
|
||||||
#include "net_utl.hpp"
|
|
||||||
|
|
||||||
namespace graphene { namespace peerplays_sidechain {
|
namespace graphene { namespace peerplays_sidechain {
|
||||||
|
|
||||||
struct http_request {
|
struct http_request {
|
||||||
|
|
@ -105,10 +103,6 @@ static const char *crlfcrlf = "\x0D\x0A\x0D\x0A";
|
||||||
|
|
||||||
using namespace boost::asio;
|
using namespace boost::asio;
|
||||||
|
|
||||||
[[noreturn]] static void throw_error(const std::string &msg) {
|
|
||||||
throw std::runtime_error(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
class https_call_impl {
|
class https_call_impl {
|
||||||
public:
|
public:
|
||||||
https_call_impl(const https_call &call, const http_request &request, http_response &response) :
|
https_call_impl(const https_call &call, const http_request &request, http_response &response) :
|
||||||
|
|
@ -200,8 +194,9 @@ private:
|
||||||
const auto &h = m_request.headers;
|
const auto &h = m_request.headers;
|
||||||
|
|
||||||
if (!h.empty()) {
|
if (!h.empty()) {
|
||||||
if (h.size() < 2)
|
if (h.size() < 2) {
|
||||||
throw_error("invalid headers data");
|
FC_THROW("invalid headers data");
|
||||||
|
}
|
||||||
stream << h;
|
stream << h;
|
||||||
// ensure headers finished correctly
|
// ensure headers finished correctly
|
||||||
if ((h.substr(h.size() - 2) != crlf))
|
if ((h.substr(h.size() - 2) != crlf))
|
||||||
|
|
@ -236,7 +231,7 @@ private:
|
||||||
stream >> m_response.status_code;
|
stream >> m_response.status_code;
|
||||||
|
|
||||||
if (!stream || http_version.substr(0, 5) != "HTTP/") {
|
if (!stream || http_version.substr(0, 5) != "HTTP/") {
|
||||||
throw_error("invalid response");
|
FC_THROW("invalid response data");
|
||||||
}
|
}
|
||||||
|
|
||||||
// read/skip headers
|
// read/skip headers
|
||||||
|
|
@ -274,25 +269,28 @@ private:
|
||||||
|
|
||||||
// check content length
|
// check content length
|
||||||
|
|
||||||
if (content_length < 2) // minimum content is "{}"
|
if (content_length < 2) { // minimum content is "{}"
|
||||||
throw_error("invalid response body (too short)");
|
FC_THROW("invalid response body (too short)");
|
||||||
|
}
|
||||||
|
|
||||||
if (content_length > m_call.response_size_limit_bytes())
|
if (content_length > m_call.response_size_limit_bytes()) {
|
||||||
throw_error("response body size limit exceeded");
|
FC_THROW("response body size limit exceeded");
|
||||||
|
}
|
||||||
|
|
||||||
// read body
|
// read body
|
||||||
|
|
||||||
auto avail = buf.size(); // size of body data already stored in the buffer
|
auto avail = buf.size(); // size of body data already stored in the buffer
|
||||||
|
|
||||||
if (avail > content_length)
|
if (avail > content_length) {
|
||||||
throw_error("invalid response body (content length mismatch)");
|
FC_THROW("invalid response body (content length mismatch)");
|
||||||
|
}
|
||||||
|
|
||||||
body.resize(content_length);
|
body.resize(content_length);
|
||||||
|
|
||||||
if (avail) {
|
if (avail) {
|
||||||
// copy already existing data
|
// copy already existing data
|
||||||
if (avail != buf.sgetn(&body[0], avail)) {
|
if (avail != buf.sgetn(&body[0], avail)) {
|
||||||
throw_error("stream read failed");
|
FC_THROW("stream read failed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -438,59 +436,39 @@ rpc_reply rpc_client::send_post_request(std::string body, bool show_log) {
|
||||||
memcpy(&reply.body[0], &response.body[0], response.body.size());
|
memcpy(&reply.body[0], &response.body[0], response.body.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (show_log) {
|
} else {
|
||||||
std::string url = ip + ":" + std::to_string(port);
|
|
||||||
ilog("### Request URL: ${url}", ("url", url));
|
|
||||||
ilog("### Request: ${body}", ("body", body));
|
|
||||||
ilog("### Response code: ${code}", ("code", response.status_code));
|
|
||||||
ilog("### Response len: ${len}", ("len", response.body.size()));
|
|
||||||
std::stringstream ss(std::string(reply.body.begin(), reply.body.end()));
|
|
||||||
ilog("### Response body: ${ss}", ("ss", ss.str()));
|
|
||||||
}
|
|
||||||
|
|
||||||
return reply;
|
std::string host;
|
||||||
}
|
|
||||||
|
|
||||||
std::string host;
|
if (start == "http:/")
|
||||||
|
host = ip.substr(7); // skip "http://"
|
||||||
|
else
|
||||||
|
host = ip;
|
||||||
|
|
||||||
if (start == "http:/")
|
std::string url = "http://" + host + ":" + std::to_string(port);
|
||||||
host = ip.substr(7); // skip "http://"
|
fc::ip::address addr;
|
||||||
else
|
|
||||||
host = ip;
|
|
||||||
|
|
||||||
std::string url = "http://" + host + ":" + std::to_string(port);
|
|
||||||
fc::ip::address addr;
|
|
||||||
|
|
||||||
try {
|
|
||||||
addr = fc::ip::address(host);
|
|
||||||
} catch (...) {
|
|
||||||
try {
|
try {
|
||||||
addr = fc::ip::address(resolve_host_addr(host));
|
addr = fc::ip::address(host);
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
if (show_log) {
|
|
||||||
std::string url = ip + ":" + std::to_string(port);
|
|
||||||
ilog("### Request URL: ${url}", ("url", url));
|
|
||||||
ilog("### Request: ${body}", ("body", body));
|
|
||||||
ilog("### Request: error: host address resolve failed");
|
|
||||||
}
|
|
||||||
return reply;
|
return reply;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
fc::http::connection conn;
|
fc::http::connection conn;
|
||||||
conn.connect_to(fc::ip::endpoint(addr, port));
|
conn.connect_to(fc::ip::endpoint(addr, port));
|
||||||
|
|
||||||
//if (wallet.length() > 0) {
|
//if (wallet.length() > 0) {
|
||||||
// url = url + "/wallet/" + wallet;
|
// url = url + "/wallet/" + wallet;
|
||||||
//}
|
//}
|
||||||
|
|
||||||
auto r = conn.request("POST", url, body, fc::http::headers{authorization});
|
auto r = conn.request("POST", url, body, fc::http::headers{authorization});
|
||||||
reply.status = r.status;
|
reply.status = r.status;
|
||||||
reply.body.assign(r.body.begin(), r.body.end());
|
reply.body.assign(r.body.begin(), r.body.end());
|
||||||
|
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (show_log) {
|
if (show_log) {
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,34 @@
|
||||||
#include <graphene/peerplays_sidechain/hive/transaction.hpp>
|
#include <graphene/peerplays_sidechain/hive/transaction.hpp>
|
||||||
#include <graphene/utilities/key_conversion.hpp>
|
#include <graphene/utilities/key_conversion.hpp>
|
||||||
|
|
||||||
#include "common/net_utl.hpp"
|
#include <boost/asio.hpp>
|
||||||
|
|
||||||
|
namespace graphene { namespace peerplays_sidechain {
|
||||||
|
|
||||||
|
std::string resolve_host_addr(const std::string &host_name) {
|
||||||
|
using namespace boost::asio;
|
||||||
|
io_service service;
|
||||||
|
ip::tcp::resolver resolver(service);
|
||||||
|
auto query = ip::tcp::resolver::query(host_name, std::string());
|
||||||
|
auto iter = resolver.resolve(query);
|
||||||
|
auto endpoint = *iter;
|
||||||
|
auto addr = ((ip::tcp::endpoint)endpoint).address();
|
||||||
|
return addr.to_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string strip_proto_name(const std::string &url, std::string *schema) {
|
||||||
|
auto index = url.find("://");
|
||||||
|
if (index == std::string::npos) {
|
||||||
|
if (schema)
|
||||||
|
schema->clear();
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
if (schema)
|
||||||
|
schema->assign(&url[0], &url[index + 3]);
|
||||||
|
return url.substr(index + 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
}} // namespace graphene::peerplays_sidechain
|
||||||
|
|
||||||
namespace graphene { namespace peerplays_sidechain {
|
namespace graphene { namespace peerplays_sidechain {
|
||||||
|
|
||||||
|
|
@ -147,31 +174,33 @@ sidechain_net_handler_hive::sidechain_net_handler_hive(peerplays_sidechain_plugi
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fc::http::connection conn;
|
std::string schema;
|
||||||
|
auto host = strip_proto_name(node_ip, &schema);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
auto host = strip_proto_name(node_ip);
|
fc::ip::address ip_addr;
|
||||||
fc::ip::address addr;
|
|
||||||
try {
|
try {
|
||||||
// IP address assumed
|
// IP address assumed
|
||||||
addr = fc::ip::address(host);
|
ip_addr = fc::ip::address(host);
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
try {
|
try {
|
||||||
// host name assumed
|
// host name assumed
|
||||||
addr = fc::ip::address(resolve_host_addr(host));
|
host = resolve_host_addr(host);
|
||||||
|
ip_addr = fc::ip::address(host);
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
elog("Failed to resolve Hive node address ${ip}", ("ip", node_ip));
|
elog("Failed to resolve Hive node address ${ip}", ("ip", node_ip));
|
||||||
FC_ASSERT(false);
|
FC_ASSERT(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// try to connect to TCP endpoint
|
// try to connect to TCP endpoint
|
||||||
conn.connect_to(fc::ip::endpoint(addr, node_rpc_port));
|
fc::http::connection conn;
|
||||||
|
conn.connect_to(fc::ip::endpoint(ip_addr, node_rpc_port));
|
||||||
} catch (fc::exception &e) {
|
} catch (fc::exception &e) {
|
||||||
elog("No Hive node running at ${ip} or wrong rpc port: ${port}", ("ip", node_ip)("port", node_rpc_port));
|
elog("No Hive node running at ${ip} or wrong rpc port: ${port}", ("ip", node_ip)("port", node_rpc_port));
|
||||||
FC_ASSERT(false);
|
FC_ASSERT(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
node_rpc_client = new hive_node_rpc_client(node_ip, node_rpc_port, node_rpc_user, node_rpc_password, debug_rpc_calls);
|
node_rpc_client = new hive_node_rpc_client(schema + host, node_rpc_port, node_rpc_user, node_rpc_password, debug_rpc_calls);
|
||||||
|
|
||||||
std::string chain_id_str = node_rpc_client->get_chain_id();
|
std::string chain_id_str = node_rpc_client->get_chain_id();
|
||||||
chain_id = chain_id_type(chain_id_str);
|
chain_id = chain_id_type(chain_id_str);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue