websocket connection to geth

This commit is contained in:
Pavel Baykov 2022-05-20 12:11:59 -03:00
parent bb976816af
commit eac7f1ead6
2 changed files with 106 additions and 12 deletions

View file

@ -3,7 +3,6 @@
#include <graphene/peerplays_sidechain/sidechain_net_handler.hpp>
#include <string>
#include <zmq_addon.hpp>
#include <boost/signals2.hpp>
@ -14,13 +13,27 @@
#include <fc/api.hpp>
#include <fc/log/logger.hpp>
#include <fc/rpc/api_connection.hpp>
#include <fc/rpc/websocket_api.hpp>
#include <websocketpp/config/asio_client.hpp>
#include <websocketpp/client.hpp>
namespace graphene { namespace peerplays_sidechain {
typedef websocketpp::client<websocketpp::config::asio_client> client;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
// pull out the type of messages sent by our config
typedef websocketpp::config::asio_tls_client::message_type::ptr message_ptr;
typedef websocketpp::lib::shared_ptr<boost::asio::ssl::context> context_ptr;
typedef client::connection_ptr connection_ptr;
class eth_rpc_client {
public:
typedef eth_rpc_client type;
enum class multi_type {
script,
address
@ -40,8 +53,17 @@ public:
public:
eth_rpc_client(const std::string &url, const std::string &user_name, const std::string &password, bool debug_rpc_calls);
void connect();
void start(std::string uri);
void on_socket_init(websocketpp::connection_hdl);
void on_fail(websocketpp::connection_hdl hdl);
void on_open(websocketpp::connection_hdl hdl);
void on_message(websocketpp::connection_hdl hdl, message_ptr msg);
void on_close(websocketpp::connection_hdl);
std::string get_chain_id();
std::string eth_getTransactionReceipt(const std::string& tx_id);
std::string eth_call(const std::string& to, const std::string& data);
std::string eth_sendTransaction(const std::string& from, const std::string& to, const std::string& data);
std::string addmultisigaddress(const uint32_t nrequired, const std::vector<std::string> public_keys);
std::string combinepsbt(const vector<std::string> &psbts);
@ -72,6 +94,8 @@ public:
private:
std::string geth_url;
uint64_t t_id;
std::string ip;
uint32_t rpc_port;
std::string user;
@ -82,8 +106,8 @@ private:
fc::http::header authorization;
fc::http::websocket_client client;
std::shared_ptr<fc::rpc::websocket_api_connection> client_connection;
client m_endpoint;
websocketpp::connection_hdl m_hdl;
};
// =============================================================================

View file

@ -3,6 +3,7 @@
#include <algorithm>
#include <thread>
#include <boost/format.hpp>
#include <boost/algorithm/hex.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
@ -19,7 +20,6 @@
#include <graphene/utilities/key_conversion.hpp>
namespace graphene { namespace peerplays_sidechain {
// =============================================================================
eth_rpc_client::eth_rpc_client(const std::string &url, const std::string &user_name, const std::string &password, bool debug_rpc_calls) {
@ -27,18 +27,88 @@ eth_rpc_client::eth_rpc_client(const std::string &url, const std::string &user_n
user = user_name;
this->password = password;
this->debug_rpc_calls = debug_rpc_calls;
t_id = 1;
ilog("eth_rpc_client");
ilog("### Geth URL: ${url}", ("url", url));
m_endpoint.set_access_channels(websocketpp::log::alevel::none);
m_endpoint.set_error_channels(websocketpp::log::elevel::none);
// Initialize ASIO
m_endpoint.init_asio();
// Register our handlers
m_endpoint.set_socket_init_handler(bind(&type::on_socket_init,this,::_1));
m_endpoint.set_message_handler(bind(&type::on_message,this,::_1,::_2));
m_endpoint.set_open_handler(bind(&type::on_open,this,::_1));
m_endpoint.set_close_handler(bind(&type::on_close,this,::_1));
m_endpoint.set_fail_handler(bind(&type::on_fail,this,::_1));
}
void eth_rpc_client::connect() {
client_connection = std::make_shared<fc::rpc::websocket_api_connection>(client.connect(geth_url), GRAPHENE_MAX_NESTED_OBJECTS);
void eth_rpc_client::start(std::string uri) {
websocketpp::lib::error_code ec;
client::connection_ptr con = m_endpoint.get_connection(uri, ec);
m_hdl = con->get_handle();
if (ec) {
m_endpoint.get_alog().write(websocketpp::log::alevel::app,ec.message());
return;
}
m_endpoint.connect(con);
// Start the ASIO io_service run loop
m_endpoint.run();
}
void eth_rpc_client::on_socket_init(websocketpp::connection_hdl) {
}
void eth_rpc_client::on_fail(websocketpp::connection_hdl hdl) {
client::connection_ptr con = m_endpoint.get_con_from_hdl(hdl);
elog("Ethereum websocket fail");
//elog("get_state: ${state}", ("state", con->get_state() ) );
elog("get_local_close_code: ${code}", ("code", con->get_local_close_code()));
elog("get_local_close_reason: ${close}", ("close",con->get_local_close_reason()));
elog("get_remote_close_code: ${close}", ("close", con->get_remote_close_code()));
elog("get_remote_close_reason: ${close}", ("close", con->get_remote_close_reason()));
elog("get_ec().message(): ${ec}", ("ec", con->get_ec().message()));
}
void eth_rpc_client::on_open(websocketpp::connection_hdl hdl) {
m_endpoint.send(hdl, "{\"jsonrpc\":\"2.0\",\"method\":\"eth_chainId\",\"params\":[],\"id\":84}", websocketpp::frame::opcode::text);
}
void eth_rpc_client::on_message(websocketpp::connection_hdl hdl, message_ptr msg) {
ilog("on_message: ${msg}", ("msg", msg->get_payload()));
//m_endpoint.close(hdl,websocketpp::close::status::going_away,"");
}
void eth_rpc_client::on_close(websocketpp::connection_hdl) {
ilog("Ethereum websocket close");
}
std::string eth_rpc_client::get_chain_id() {
//std::string reply_str = database_api_get_version();
return "";//retrieve_value_from_reply(reply_str, "chain_id");
std::string req = str(boost::format("{\"jsonrpc\":\"2.0\",\"method\":\"eth_chainId\",\"params\":[],\"id\":%1%}") % t_id++);
m_endpoint.send(m_hdl, req.c_str(), websocketpp::frame::opcode::text);
}
std::string eth_rpc_client::eth_getTransactionReceipt(const std::string& tx_id) {
std::string req = str(boost::format("{\"jsonrpc\":\"2.0\",\"method\":\"eth_getTransactionReceipt\",\"params\":[\"0xbc262e1222ff48cd026a62fd925912ab5006650e311b7711a7216054461a64e0\"],\"id\":%1%}") % t_id++);
m_endpoint.send(m_hdl, req.c_str(), websocketpp::frame::opcode::text);
return "";
}
std::string eth_rpc_client::eth_call(const std::string& to, const std::string& data) {
return "";
}
std::string eth_rpc_client::eth_sendTransaction(const std::string& from, const std::string& to, const std::string& data) {
return "";
}
std::string eth_rpc_client::addmultisigaddress(const uint32_t nrequired, const std::vector<std::string> public_keys) {
@ -155,7 +225,7 @@ sidechain_net_handler_eth::sidechain_net_handler_eth(peerplays_sidechain_plugin
url = options.at("ethereum-node-rpc-url").as<std::string>();
eth_client = std::unique_ptr<eth_rpc_client>(new eth_rpc_client(url, rpc_user, rpc_password, debug_rpc_calls));
eth_client->connect();
eth_client->start(url);
/*
if (!wallet.empty()) {
eth_client->loadwallet(wallet);