Remove WS client
This commit is contained in:
parent
867f38796a
commit
35c2d4c63f
5 changed files with 3 additions and 239 deletions
|
|
@ -18,7 +18,6 @@ add_library( peerplays_sidechain
|
||||||
bitcoin/sign_bitcoin_transaction.cpp
|
bitcoin/sign_bitcoin_transaction.cpp
|
||||||
common/rpc_client.cpp
|
common/rpc_client.cpp
|
||||||
common/utils.cpp
|
common/utils.cpp
|
||||||
common/ws_client.cpp
|
|
||||||
ethereum/encoders.cpp
|
ethereum/encoders.cpp
|
||||||
ethereum/decoders.cpp
|
ethereum/decoders.cpp
|
||||||
ethereum/transaction.cpp
|
ethereum/transaction.cpp
|
||||||
|
|
|
||||||
|
|
@ -13,8 +13,6 @@
|
||||||
#include <boost/asio/ssl/stream.hpp>
|
#include <boost/asio/ssl/stream.hpp>
|
||||||
#include <boost/beast/http.hpp>
|
#include <boost/beast/http.hpp>
|
||||||
|
|
||||||
#include <curl/curl.h>
|
|
||||||
|
|
||||||
#include <fc/log/logger.hpp>
|
#include <fc/log/logger.hpp>
|
||||||
|
|
||||||
namespace graphene { namespace peerplays_sidechain {
|
namespace graphene { namespace peerplays_sidechain {
|
||||||
|
|
|
||||||
|
|
@ -1,195 +0,0 @@
|
||||||
#include <graphene/peerplays_sidechain/common/ws_client.hpp>
|
|
||||||
|
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
#include <boost/asio.hpp>
|
|
||||||
#include <boost/asio/buffers_iterator.hpp>
|
|
||||||
#include <boost/asio/ssl.hpp>
|
|
||||||
#include <boost/beast.hpp>
|
|
||||||
#include <boost/beast/core.hpp>
|
|
||||||
#include <boost/beast/http.hpp>
|
|
||||||
#include <boost/beast/ssl.hpp>
|
|
||||||
#include <boost/beast/websocket.hpp>
|
|
||||||
#include <boost/property_tree/json_parser.hpp>
|
|
||||||
#include <boost/property_tree/ptree.hpp>
|
|
||||||
#include <boost/xpressive/xpressive.hpp>
|
|
||||||
|
|
||||||
#include <fc/log/logger.hpp>
|
|
||||||
|
|
||||||
namespace graphene { namespace peerplays_sidechain {
|
|
||||||
|
|
||||||
ws_client::ws_client(std::string _url, std::string _user, std::string _password, bool _debug_ws_calls) :
|
|
||||||
url(_url),
|
|
||||||
user(_user),
|
|
||||||
password(_password),
|
|
||||||
debug_ws_calls(_debug_ws_calls),
|
|
||||||
request_id(0) {
|
|
||||||
|
|
||||||
std::string reg_expr = "^((?P<Protocol>wss|ws):\\/\\/)?(?P<Host>[a-zA-Z0-9\\-\\.]+)(:(?P<Port>\\d{1,5}))?(?P<Target>\\/.+)?";
|
|
||||||
boost::xpressive::sregex sr = boost::xpressive::sregex::compile(reg_expr);
|
|
||||||
|
|
||||||
boost::xpressive::smatch sm;
|
|
||||||
|
|
||||||
if (boost::xpressive::regex_search(url, sm, sr)) {
|
|
||||||
protocol = sm["Protocol"];
|
|
||||||
if (protocol.empty()) {
|
|
||||||
protocol = "ws";
|
|
||||||
}
|
|
||||||
|
|
||||||
host = sm["Host"];
|
|
||||||
if (host.empty()) {
|
|
||||||
host + "localhost";
|
|
||||||
}
|
|
||||||
|
|
||||||
port = sm["Port"];
|
|
||||||
if (port.empty()) {
|
|
||||||
port = "80";
|
|
||||||
}
|
|
||||||
|
|
||||||
target = sm["Target"];
|
|
||||||
if (target.empty()) {
|
|
||||||
target = "/";
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
elog("Invalid URL: ${url}", ("url", url));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string ws_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_optional("result");
|
|
||||||
if (json_result) {
|
|
||||||
boost::property_tree::ptree array_ptree = json_result.get();
|
|
||||||
if (!array_path.empty()) {
|
|
||||||
array_ptree = json_result.get().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 ws_client::retrieve_value_from_reply(std::string reply_str, std::string value_path) {
|
|
||||||
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_optional("result");
|
|
||||||
if (json_result) {
|
|
||||||
return json_result.get().get<std::string>(value_path);
|
|
||||||
}
|
|
||||||
|
|
||||||
return json.get<std::string>("result");
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string ws_client::send_post_request(std::string method, std::string params, bool show_log) {
|
|
||||||
std::stringstream body;
|
|
||||||
|
|
||||||
request_id = request_id + 1;
|
|
||||||
|
|
||||||
body << "{ \"jsonrpc\": \"2.0\", \"id\": " << request_id << ", \"method\": \"" << method << "\"";
|
|
||||||
|
|
||||||
if (!params.empty()) {
|
|
||||||
body << ", \"params\": " << params;
|
|
||||||
}
|
|
||||||
|
|
||||||
body << " }";
|
|
||||||
|
|
||||||
const auto reply = send_post_request(body.str(), show_log);
|
|
||||||
|
|
||||||
if (reply.body.empty()) {
|
|
||||||
wlog("RPC call ${function} failed", ("function", __FUNCTION__));
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
std::stringstream ss(std::string(reply.body.begin(), reply.body.end()));
|
|
||||||
boost::property_tree::ptree json;
|
|
||||||
boost::property_tree::read_json(ss, json);
|
|
||||||
|
|
||||||
if (reply.status == 200) {
|
|
||||||
return ss.str();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (json.count("error") && !json.get_child("error").empty()) {
|
|
||||||
wlog("RPC call ${function} with body ${body} failed with reply '${msg}'", ("function", __FUNCTION__)("body", body.str())("msg", ss.str()));
|
|
||||||
}
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
ws_reply ws_client::send_post_request(std::string body, bool show_log) {
|
|
||||||
|
|
||||||
// The io_context is required for all I/O
|
|
||||||
boost::beast::net::io_context ioc;
|
|
||||||
|
|
||||||
// These objects perform our I/O
|
|
||||||
boost::beast::net::ip::tcp::resolver resolver(ioc);
|
|
||||||
boost::beast::websocket::stream<boost::asio::ip::tcp::socket> ws{ioc};
|
|
||||||
|
|
||||||
// Look up the domain name
|
|
||||||
auto const results = resolver.resolve(host, port);
|
|
||||||
|
|
||||||
// Make the connection on the IP address we get from a lookup
|
|
||||||
boost::beast::net::connect(ws.next_layer(), results.begin(), results.end());
|
|
||||||
|
|
||||||
// Set a decorator to change the User-Agent of the handshake
|
|
||||||
ws.set_option(boost::beast::websocket::stream_base::decorator(
|
|
||||||
[](boost::beast::websocket::request_type &req) {
|
|
||||||
//// Set up an HTTP GET request message
|
|
||||||
//boost::beast::http::request<boost::beast::http::string_body> req{boost::beast::http::verb::post, target, 11};
|
|
||||||
//req.set(boost::beast::http::field::host, host + ":" + port);
|
|
||||||
//req.set(boost::beast::http::field::accept, "application/json");
|
|
||||||
//req.set(boost::beast::http::field::content_type, "application/json");
|
|
||||||
//req.set(boost::beast::http::field::content_encoding, "utf-8");
|
|
||||||
//req.set(boost::beast::http::field::content_length, body.length());
|
|
||||||
//req.body() = body;
|
|
||||||
}));
|
|
||||||
|
|
||||||
// Perform the websocket handshake
|
|
||||||
ws.handshake(host, "/");
|
|
||||||
|
|
||||||
// Send the message
|
|
||||||
ws.write(boost::asio::buffer(body));
|
|
||||||
|
|
||||||
// This buffer is used for reading and must be persisted
|
|
||||||
boost::beast::flat_buffer buffer;
|
|
||||||
|
|
||||||
// Read a message into our buffer
|
|
||||||
ws.read(buffer);
|
|
||||||
|
|
||||||
// Close the WebSocket connection
|
|
||||||
ws.close(boost::beast::websocket::close_code::normal);
|
|
||||||
|
|
||||||
//std::string rbody{boost::asio::buffers_begin(buffer),
|
|
||||||
// boost::asio::buffers_end(reading)};
|
|
||||||
|
|
||||||
ws_reply reply;
|
|
||||||
reply.status = 200;
|
|
||||||
//reply.body = rbody;
|
|
||||||
|
|
||||||
//if (show_log) {
|
|
||||||
// ilog("### Request URL: ${url}", ("url", url));
|
|
||||||
// ilog("### Request: ${body}", ("body", body));
|
|
||||||
// ilog("### Response: ${rbody}", ("rbody", rbody));
|
|
||||||
//}
|
|
||||||
|
|
||||||
return reply;
|
|
||||||
}
|
|
||||||
|
|
||||||
}} // namespace graphene::peerplays_sidechain
|
|
||||||
|
|
@ -1,38 +0,0 @@
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <cstdint>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
namespace graphene { namespace peerplays_sidechain {
|
|
||||||
|
|
||||||
struct ws_reply {
|
|
||||||
uint16_t status;
|
|
||||||
std::string body;
|
|
||||||
};
|
|
||||||
|
|
||||||
class ws_client {
|
|
||||||
public:
|
|
||||||
ws_client(std::string _url, std::string _user, std::string _password, bool _debug_ws_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);
|
|
||||||
std::string send_post_request(std::string method, std::string params, bool show_log);
|
|
||||||
|
|
||||||
std::string url;
|
|
||||||
std::string protocol;
|
|
||||||
std::string host;
|
|
||||||
std::string port;
|
|
||||||
std::string target;
|
|
||||||
|
|
||||||
std::string user;
|
|
||||||
std::string password;
|
|
||||||
bool debug_ws_calls;
|
|
||||||
|
|
||||||
uint32_t request_id;
|
|
||||||
|
|
||||||
private:
|
|
||||||
ws_reply send_post_request(std::string body, bool show_log);
|
|
||||||
};
|
|
||||||
|
|
||||||
}} // namespace graphene::peerplays_sidechain
|
|
||||||
|
|
@ -161,9 +161,9 @@ void peerplays_sidechain_plugin_impl::plugin_set_program_options(
|
||||||
cli.add_options()("ethereum-node-rpc-url", bpo::value<string>()->default_value("127.0.0.1:8545"), "Ethereum node RPC URL [http[s]://]host[:port]");
|
cli.add_options()("ethereum-node-rpc-url", bpo::value<string>()->default_value("127.0.0.1:8545"), "Ethereum node RPC URL [http[s]://]host[:port]");
|
||||||
cli.add_options()("ethereum-node-rpc-user", bpo::value<string>(), "Ethereum RPC user");
|
cli.add_options()("ethereum-node-rpc-user", bpo::value<string>(), "Ethereum RPC user");
|
||||||
cli.add_options()("ethereum-node-rpc-password", bpo::value<string>(), "Ethereum RPC password");
|
cli.add_options()("ethereum-node-rpc-password", bpo::value<string>(), "Ethereum RPC password");
|
||||||
cli.add_options()("ethereum-wallet-contract-address", bpo::value<string>()->default_value("0000000000000000000000000000000000000000"), "Ethereum wallet contract address"),
|
cli.add_options()("ethereum-wallet-contract-address", bpo::value<string>()->default_value("0000000000000000000000000000000000000000"), "Ethereum wallet contract address");
|
||||||
cli.add_options()("ethereum-private-key", bpo::value<vector<string>>()->composing()->multitoken()->DEFAULT_VALUE_VECTOR(std::make_pair("5fbbb31be52608d2f52247e8400b7fcaa9e0bc12", "9bedac2bd8fe2a6f6528e066c67fc8ac0622e96828d40c0e820d83c5bd2b0589")),
|
cli.add_options()("ethereum-private-key", bpo::value<vector<string>>()->composing()->multitoken()->DEFAULT_VALUE_VECTOR(std::make_pair("5fbbb31be52608d2f52247e8400b7fcaa9e0bc12", "9bedac2bd8fe2a6f6528e066c67fc8ac0622e96828d40c0e820d83c5bd2b0589")),
|
||||||
"Tuple of [Ethereum public key, Ethereum private key] (may specify multiple times)");
|
"Tuple of [Ethereum public key, Ethereum private key] (may specify multiple times)");
|
||||||
|
|
||||||
cli.add_options()("hive-sidechain-enabled", bpo::value<bool>()->default_value(false), "Hive sidechain handler enabled");
|
cli.add_options()("hive-sidechain-enabled", bpo::value<bool>()->default_value(false), "Hive sidechain handler enabled");
|
||||||
cli.add_options()("hive-node-rpc-url", bpo::value<string>()->default_value("127.0.0.1:28090"), "Hive node RPC URL [http[s]://]host[:port]");
|
cli.add_options()("hive-node-rpc-url", bpo::value<string>()->default_value("127.0.0.1:28090"), "Hive node RPC URL [http[s]://]host[:port]");
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue