diff --git a/.gitignore b/.gitignore index 39b23163..1a84b559 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ data CMakeDoxyfile.in build +build__* libraries/utilities/git_revision.cpp diff --git a/libraries/plugins/peerplays_sidechain/CMakeLists.txt b/libraries/plugins/peerplays_sidechain/CMakeLists.txt index bb74df29..218e25fe 100755 --- a/libraries/plugins/peerplays_sidechain/CMakeLists.txt +++ b/libraries/plugins/peerplays_sidechain/CMakeLists.txt @@ -15,6 +15,7 @@ add_library( peerplays_sidechain bitcoin/utils.cpp bitcoin/sign_bitcoin_transaction.cpp common/rpc_client.cpp + common/net_utl.cpp common/utils.cpp hive/asset.cpp hive/operations.cpp diff --git a/libraries/plugins/peerplays_sidechain/common/net_utl.cpp b/libraries/plugins/peerplays_sidechain/common/net_utl.cpp new file mode 100644 index 00000000..019c09b7 --- /dev/null +++ b/libraries/plugins/peerplays_sidechain/common/net_utl.cpp @@ -0,0 +1,25 @@ +#include "net_utl.hpp" + +#include + +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 diff --git a/libraries/plugins/peerplays_sidechain/common/net_utl.hpp b/libraries/plugins/peerplays_sidechain/common/net_utl.hpp new file mode 100644 index 00000000..86ad72ba --- /dev/null +++ b/libraries/plugins/peerplays_sidechain/common/net_utl.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include + +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 diff --git a/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp b/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp index cad283a7..df692185 100644 --- a/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp +++ b/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp @@ -3,6 +3,10 @@ #include #include +#include +#include +#include + #include #include @@ -10,7 +14,319 @@ #include #include -//#include +#include + +#include +#include + +#include "net_utl.hpp" + +namespace graphene { namespace peerplays_sidechain { + +struct http_request { + + std::string method; // ex: "POST" + std::string path; // ex: "/" + std::string headers; + std::string body; + std::string content_type; // ex: "application/json" + + http_request() { + } + + http_request(const std::string &method_, const std::string &path_, const std::string &headers_, const std::string &body_, const std::string &content_type_) : + method(method_), + path(path_), + headers(headers_), + body(body_), + content_type(content_type_) { + } + + http_request(const std::string &method_, const std::string &path_, const std::string &headers_, const std::string &body_ = std::string()) : + method(method_), + path(path_), + headers(headers_), + body(body_), + content_type("application/json") { + } + + void clear() { + method.clear(); + path.clear(); + headers.clear(); + body.clear(); + content_type.clear(); + } +}; + +struct http_response { + + uint16_t status_code; + std::string body; + + void clear() { + status_code = 0; + body = decltype(body)(); + } +}; + +class https_call { +public: + https_call(const std::string &host, uint16_t port = 0) : + m_host(host), + m_port(port) { + } + + const std::string &host() const { + return m_host; + } + + uint16_t port() const { + return m_port; + } + + uint32_t response_size_limit_bytes() const { + return 1024 * 1024; + } + + bool exec(const http_request &request, http_response *response); + +private: + std::string m_host; + uint16_t m_port; +}; + +namespace detail { + +static const char cr = 0x0D; +static const char lf = 0x0A; +static const char *crlf = "\x0D\x0A"; +static const char *crlfcrlf = "\x0D\x0A\x0D\x0A"; + +using namespace boost::asio; + +[[noreturn]] static void throw_error(const std::string &msg) { + throw std::runtime_error(msg); +} + +class https_call_impl { +public: + https_call_impl(const https_call &call, const http_request &request, http_response &response) : + m_call(call), + m_request(request), + m_response(response), + m_service(), + m_context(ssl::context::tlsv12_client), + m_socket(m_service, m_context), + m_endpoint(), + m_response_buf(call.response_size_limit_bytes()), + m_content_length(0) { + m_context.set_default_verify_paths(); + } + + void exec() { + resolve(); + connect(); + send_request(); + process_response(); + } + +private: + const https_call &m_call; + const http_request &m_request; + http_response &m_response; + + io_service m_service; + ssl::context m_context; + ssl::stream m_socket; + ip::tcp::endpoint m_endpoint; + streambuf m_response_buf; + uint32_t m_content_length; + + void resolve() { + + // resolve TCP endpoint for host name + + ip::tcp::resolver resolver(m_service); + auto query = ip::tcp::resolver::query(m_call.host(), "https"); + auto iter = resolver.resolve(query); + m_endpoint = *iter; + + if (m_call.port() != 0) // if port was specified + m_endpoint.port(m_call.port()); // force set port + } + + void connect() { + + // TCP connect + + m_socket.lowest_layer().connect(m_endpoint); + + // SSL connect + + m_socket.set_verify_mode(ssl::verify_peer); + m_socket.handshake(ssl::stream_base::client); + } + + void send_request() { + + streambuf request_buf; + std::ostream stream(&request_buf); + + // start string: HTTP/1.0 + + stream << m_request.method << " " << m_request.path << " HTTP/1.0" << crlf; + + // host + + stream << "Host: " << m_call.host(); + + if (m_call.port() != 0) { + //ASSERT(m_Endpoint.port() == m_Call.port()); + stream << ":" << m_call.port(); + } + + stream << crlf; + + // content + + if (!m_request.body.empty()) { + stream << "Content-Type: " << m_request.content_type << crlf; + stream << "Content-Length: " << m_request.body.size() << crlf; + } + + // additional headers + + const auto &h = m_request.headers; + + if (!h.empty()) { + if (h.size() < 2) + throw_error("invalid headers data"); + stream << h; + // ensure headers finished correctly + if ((h.substr(h.size() - 2) != crlf)) + stream << crlf; + } + + // other + + stream << "Accept: *\x2F*" << crlf; + stream << "Connection: close" << crlf; + + // end + + stream << crlf; + + // content + + if (!m_request.body.empty()) + stream << m_request.body; + + // send + + write(m_socket, request_buf); + } + + void process_headers() { + + std::istream stream(&m_response_buf); + + std::string http_version; + stream >> http_version; + stream >> m_response.status_code; + + if (!stream || http_version.substr(0, 5) != "HTTP/") { + throw_error("invalid response"); + } + + // read/skip headers + + for (;;) { + std::string header; + if (!std::getline(stream, header, lf) || (header.size() == 1 && header[0] == cr)) + break; + if (m_content_length) // if content length is already known + continue; // continue skipping headers + auto pos = header.find(':'); + if (pos == std::string::npos) + continue; + auto name = header.substr(0, pos); + boost::algorithm::trim(name); + boost::algorithm::to_lower(name); + if (name != "content-length") + continue; + auto value = header.substr(pos + 1); + boost::algorithm::trim(value); + m_content_length = std::stol(value); + } + } + + void process_response() { + + auto &socket = m_socket; + auto &buf = m_response_buf; + auto &content_length = m_content_length; + auto &body = m_response.body; + + read_until(socket, buf, crlfcrlf); + + process_headers(); + + // check content length + + if (content_length < 2) // minimum content is "{}" + throw_error("invalid response body (too short)"); + + if (content_length > m_call.response_size_limit_bytes()) + throw_error("response body size limit exceeded"); + + // read body + + auto avail = buf.size(); // size of body data already stored in the buffer + + if (avail > content_length) + throw_error("invalid response body (content length mismatch)"); + + body.resize(content_length); + + if (avail) { + // copy already existing data + if (avail != buf.sgetn(&body[0], avail)) { + throw_error("stream read failed"); + } + } + + auto rest = content_length - avail; // size of remaining part of response body + + boost::system::error_code error_code; + + read(socket, buffer(&body[avail], rest), error_code); // read remaining part + + socket.shutdown(error_code); + } +}; + +} // namespace detail + +bool https_call::exec(const http_request &request, http_response *response) { + + // ASSERT(response); + auto &resp = *response; + + detail::https_call_impl impl(*this, request, resp); + + try { + resp.clear(); + impl.exec(); + } catch (...) { + resp.clear(); + return false; + } + + return true; +} + +}} // namespace graphene::peerplays_sidechain namespace graphene { namespace peerplays_sidechain { @@ -102,75 +418,80 @@ std::string rpc_client::send_post_request(std::string method, std::string params return ""; } -//fc::http::reply rpc_client::send_post_request(std::string body, bool show_log) { -// fc::http::connection conn; -// conn.connect_to(fc::ip::endpoint(fc::ip::address(ip), port)); -// -// std::string url = "http://" + ip + ":" + std::to_string(port); -// -// //if (wallet.length() > 0) { -// // url = url + "/wallet/" + wallet; -// //} -// -// fc::http::reply reply = conn.request("POST", url, body, fc::http::headers{authorization}); -// -// if (show_log) { -// ilog("### Request URL: ${url}", ("url", url)); -// ilog("### Request: ${body}", ("body", body)); -// std::stringstream ss(std::string(reply.body.begin(), reply.body.end())); -// ilog("### Response: ${ss}", ("ss", ss.str())); -// } -// -// return reply; -//} - -static size_t write_callback(char *ptr, size_t size, size_t nmemb, rpc_reply *reply) { - size_t retval = 0; - if (reply != nullptr) { - reply->body.append(ptr, size * nmemb); - retval = size * nmemb; - } - return retval; -} - rpc_reply rpc_client::send_post_request(std::string body, bool show_log) { - struct curl_slist *headers = nullptr; - headers = curl_slist_append(headers, "Accept: application/json"); - headers = curl_slist_append(headers, "Content-Type: application/json"); - headers = curl_slist_append(headers, "charset: utf-8"); - - CURL *curl = curl_easy_init(); - if (ip.find("https://", 0) != 0) { - curl_easy_setopt(curl, CURLOPT_URL, ip.c_str()); - curl_easy_setopt(curl, CURLOPT_PORT, port); - } else { - std::string full_address = ip + ":" + std::to_string(port); - curl_easy_setopt(curl, CURLOPT_URL, full_address.c_str()); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false); - } - if (!user.empty()) { - curl_easy_setopt(curl, CURLOPT_USERNAME, user.c_str()); - curl_easy_setopt(curl, CURLOPT_PASSWORD, password.c_str()); - } - - curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body.c_str()); - - //curl_easy_setopt(curl, CURLOPT_VERBOSE, true); - rpc_reply reply; + auto start = ip.substr(0, 6); + boost::algorithm::to_lower(start); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &reply); + if (start == "https:") { - curl_easy_perform(curl); + auto host = ip.substr(8); // skip "https://" - curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &reply.status); + https_call call(host, port); + http_request request("POST", "/", authorization.key + ":" + authorization.val, body); + http_response response; - curl_easy_cleanup(curl); - curl_slist_free_all(headers); + if (call.exec(request, &response)) { + reply.status = response.status_code; + reply.body.resize(response.body.size()); + memcpy(&reply.body[0], &response.body[0], response.body.size()); + } + + if (show_log) { + 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; + + if (start == "http:/") + host = ip.substr(7); // skip "http://" + else + host = ip; + + std::string url = "http://" + host + ":" + std::to_string(port); + fc::ip::address addr; + + try { + addr = fc::ip::address(host); + } catch (...) { + try { + addr = fc::ip::address(resolve_host_addr(host)); + } 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; + } + } + + try { + + fc::http::connection conn; + conn.connect_to(fc::ip::endpoint(addr, port)); + + //if (wallet.length() > 0) { + // url = url + "/wallet/" + wallet; + //} + + auto r = conn.request("POST", url, body, fc::http::headers{authorization}); + reply.status = r.status; + reply.body.assign(r.body.begin(), r.body.end()); + + } catch (...) { + } if (show_log) { std::string url = ip + ":" + std::to_string(port); diff --git a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp index e1a5c329..a8e296ac 100644 --- a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp +++ b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_hive.cpp @@ -28,6 +28,8 @@ #include #include +#include "common/net_utl.hpp" + namespace graphene { namespace peerplays_sidechain { hive_node_rpc_client::hive_node_rpc_client(std::string _ip, uint32_t _port, std::string _user, std::string _password, bool _debug_rpc_calls) : @@ -35,7 +37,7 @@ hive_node_rpc_client::hive_node_rpc_client(std::string _ip, uint32_t _port, std: } std::string hive_node_rpc_client::account_history_api_get_transaction(std::string transaction_id) { - std::string params = "{ \"id\": \"" + transaction_id + "\", \"include_reversible\": \"true\" }"; + std::string params = "{ \"id\": \"" + transaction_id + "\" }"; return send_post_request("account_history_api.get_transaction", params, debug_rpc_calls); } @@ -145,13 +147,29 @@ sidechain_net_handler_hive::sidechain_net_handler_hive(peerplays_sidechain_plugi } } - //fc::http::connection conn; - //try { - // conn.connect_to(fc::ip::endpoint(fc::ip::address(node_ip), node_rpc_port)); - //} catch (fc::exception &e) { - // elog("No Hive node running at ${ip} or wrong rpc port: ${port}", ("ip", node_ip)("port", node_rpc_port)); - // FC_ASSERT(false); - //} + fc::http::connection conn; + + try { + auto host = strip_proto_name(node_ip); + fc::ip::address addr; + try { + // IP address assumed + addr = fc::ip::address(host); + } catch (...) { + try { + // host name assumed + addr = fc::ip::address(resolve_host_addr(host)); + } catch (...) { + elog("Failed to resolve Hive node address ${ip}", ("ip", node_ip)); + FC_ASSERT(false); + } + } + // try to connect to TCP endpoint + conn.connect_to(fc::ip::endpoint(addr, node_rpc_port)); + } catch (fc::exception &e) { + elog("No Hive node running at ${ip} or wrong rpc port: ${port}", ("ip", node_ip)("port", node_rpc_port)); + 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); @@ -161,9 +179,11 @@ sidechain_net_handler_hive::sidechain_net_handler_hive(peerplays_sidechain_plugi std::string is_test_net = node_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; } 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; } @@ -817,7 +837,6 @@ void sidechain_net_handler_hive::hive_listener_loop() { schedule_hive_listener(); std::string reply = node_rpc_client->database_api_get_dynamic_global_properties(); - if (!reply.empty()) { std::stringstream ss(reply); boost::property_tree::ptree json; @@ -831,6 +850,16 @@ void sidechain_net_handler_hive::hive_listener_loop() { } } } + + //std::string reply = node_rpc_client->get_last_irreversible_block_num(); + //if (!reply.empty()) { + // uint64_t last_irreversible_block = std::stoul(reply); + // if (last_irreversible_block != last_block_received) { + // std::string event_data = std::to_string(last_irreversible_block); + // handle_event(event_data); + // last_block_received = last_irreversible_block; + // } + //} } void sidechain_net_handler_hive::handle_event(const std::string &event_data) { @@ -840,28 +869,25 @@ void sidechain_net_handler_hive::handle_event(const std::string &event_data) { boost::property_tree::ptree block_json; boost::property_tree::read_json(ss, block_json); - for (const auto &tx_ids_child : block_json.get_child("result.block.transaction_ids")) { - const auto &transaction_id = tx_ids_child.second.get_value(); + size_t tx_idx = -1; + for (const auto &tx_ids_child : block_json.get_child("result.block.transactions")) { + boost::property_tree::ptree tx = tx_ids_child.second; + tx_idx = tx_idx + 1; - std::string tx_str = node_rpc_client->account_history_api_get_transaction(transaction_id); - if (tx_str != "") { + size_t op_idx = -1; + for (const auto &ops : tx.get_child("operations")) { + const auto &op = ops.second; + op_idx = op_idx + 1; - std::stringstream ss_tx(tx_str); - boost::property_tree::ptree tx; - boost::property_tree::read_json(ss_tx, tx); + std::string operation_type = op.get("type"); - size_t op_idx = -1; - for (const auto &ops : tx.get_child("result.operations")) { - const auto &op = ops.second; - op_idx = op_idx + 1; + if (operation_type == "transfer_operation") { + const auto &op_value = op.get_child("value"); - std::string operation_type = op.get("type"); + std::string from = op_value.get("from"); + std::string to = op_value.get("to"); - if (operation_type == "transfer_operation") { - const auto &op_value = op.get_child("value"); - - std::string from = op_value.get("from"); - std::string to = op_value.get("to"); + if (to == "son-account") { const auto &amount_child = op_value.get_child("amount"); @@ -885,42 +911,47 @@ void sidechain_net_handler_hive::handle_event(const std::string &event_data) { from = memo; } - if (to == "son-account") { - const auto &sidechain_addresses_idx = database.get_index_type().indices().get(); - const auto &addr_itr = sidechain_addresses_idx.find(std::make_tuple(sidechain, from, time_point_sec::maximum())); - account_id_type accn = account_id_type(); - if (addr_itr == sidechain_addresses_idx.end()) { - const auto &account_idx = database.get_index_type().indices().get(); - const auto &account_itr = account_idx.find(from); - if (account_itr == account_idx.end()) { - continue; - } else { - accn = account_itr->id; - } + const auto &sidechain_addresses_idx = database.get_index_type().indices().get(); + const auto &addr_itr = sidechain_addresses_idx.find(std::make_tuple(sidechain, from, time_point_sec::maximum())); + account_id_type accn = account_id_type(); + if (addr_itr == sidechain_addresses_idx.end()) { + const auto &account_idx = database.get_index_type().indices().get(); + const auto &account_itr = account_idx.find(from); + if (account_itr == account_idx.end()) { + continue; } else { - accn = addr_itr->sidechain_address_account; + accn = account_itr->id; } - - std::stringstream ss; - ss << "hive" - << "-" << transaction_id << "-" << op_idx; - std::string sidechain_uid = ss.str(); - - sidechain_event_data sed; - sed.timestamp = database.head_block_time(); - sed.block_num = database.head_block_num(); - sed.sidechain = sidechain; - sed.sidechain_uid = sidechain_uid; - sed.sidechain_transaction_id = transaction_id; - sed.sidechain_from = from; - sed.sidechain_to = to; - sed.sidechain_currency = sidechain_currency; - sed.sidechain_amount = amount; - sed.peerplays_from = accn; - sed.peerplays_to = database.get_global_properties().parameters.son_account(); - sed.peerplays_asset = asset(sed.sidechain_amount * sidechain_currency_price.base.amount / sidechain_currency_price.quote.amount); - sidechain_event_data_received(sed); + } else { + accn = addr_itr->sidechain_address_account; } + + std::vector transaction_ids; + for (const auto &tx_ids_child : block_json.get_child("result.block.transaction_ids")) { + const auto &transaction_id = tx_ids_child.second.get_value(); + transaction_ids.push_back(transaction_id); + } + std::string transaction_id = transaction_ids.at(tx_idx); + + std::stringstream ss; + ss << "hive" + << "-" << transaction_id << "-" << op_idx; + std::string sidechain_uid = ss.str(); + + sidechain_event_data sed; + sed.timestamp = database.head_block_time(); + sed.block_num = database.head_block_num(); + sed.sidechain = sidechain; + sed.sidechain_uid = sidechain_uid; + sed.sidechain_transaction_id = transaction_id; + sed.sidechain_from = from; + sed.sidechain_to = to; + sed.sidechain_currency = sidechain_currency; + sed.sidechain_amount = amount; + sed.peerplays_from = accn; + sed.peerplays_to = database.get_global_properties().parameters.son_account(); + sed.peerplays_asset = asset(sed.sidechain_amount * sidechain_currency_price.base.amount / sidechain_currency_price.quote.amount); + sidechain_event_data_received(sed); } } }