From cff3bec50765965b92d8c604a7aaaa6b28cc7097 Mon Sep 17 00:00:00 2001 From: Pavel Baykov Date: Thu, 26 May 2022 09:17:26 -0300 Subject: [PATCH] parse output of rpc commands --- .../sidechain_net_handler_eth.hpp | 18 +++++- .../sidechain_net_handler_eth.cpp | 57 +++++++++++++++++-- 2 files changed, 70 insertions(+), 5 deletions(-) diff --git a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_eth.hpp b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_eth.hpp index 3faf4a4a..24478105 100644 --- a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_eth.hpp +++ b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_eth.hpp @@ -35,6 +35,20 @@ class eth_rpc_client { public: typedef eth_rpc_client type; + enum req_t { + ETH_CHAIN_ID, + ETH_GET_TRANSACTION_RECEIPT, + ETH_CALL, + ETH_SEND_TRANSACTION, + ETH_SEND_RAW_TRANSACTION, + ETH_GET_CODE, + ETH_GET_BALANCE, + ETH_SIGN, + GET_LIST_OWNERS, + ADD_OWNER, + REMOVE_OWNER + }; + enum class multi_type { script, address @@ -107,10 +121,12 @@ public: private: std::string geth_url; uint64_t t_id; + std::string transaction_id; std::string safe_account_addr; std::vector owners; uint32_t threshold; - std::unordered_map m_messages; + std::unordered_map m_requests; + std::string chain_id;//256 bit value std::string ip; uint32_t rpc_port; diff --git a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_eth.cpp b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_eth.cpp index 00c88db9..06f58b71 100644 --- a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_eth.cpp +++ b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_eth.cpp @@ -94,6 +94,46 @@ void eth_rpc_client::on_message(websocketpp::connection_hdl hdl, message_ptr msg fc::variants list = fc::json::variants_from_string( msg->get_payload() ); ilog("json reposnse: ${list}", ("list", list)); + + const auto& b_obj = list[0].get_object().find( "id" ); + std::string result_str = list[0].get_object().find( "result" )->value().as(1); + uint32_t num_owners = 0; + uint32_t i = 0; + fc::variant v; + switch(b_obj->value().as(1)){ + case ETH_CHAIN_ID: + chain_id = result_str; + break; + case ETH_GET_TRANSACTION_RECEIPT: + list = fc::json::variants_from_string( result_str ); + v = list[0].get_object().find( "logs" )->value(); + safe_account_addr = v.get_object().find( "address" )->value().as(1); + break; + case ETH_CALL: + break; + case ETH_SEND_TRANSACTION: + transaction_id = result_str; + break; + case ETH_SEND_RAW_TRANSACTION: + break; + case ETH_GET_CODE: + break; + case ETH_GET_BALANCE: + break; + case ETH_SIGN: + break; + case GET_LIST_OWNERS: + num_owners = (uint32_t)strtol(result_str.substr(2 + 32 + 32 - 4, 4).c_str(), NULL, 16); + owners.clear(); + for (i = 0; i < num_owners; ++i){ + owners.push_back("0x" + result_str.substr(2 + 32 + 32 + 12 + 32 * i,20)); + } + break; + case ADD_OWNER: + break; + case REMOVE_OWNER: + break; + } } void eth_rpc_client::on_close(websocketpp::connection_hdl) { @@ -103,26 +143,29 @@ void eth_rpc_client::on_close(websocketpp::connection_hdl) { uint64_t eth_rpc_client::get_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); - + m_requests[t_id] = req_t::ETH_CHAIN_ID; return t_id++; } uint64_t eth_rpc_client::eth_getTransactionReceipt(const std::string& tx_id) { std::string req = str(boost::format("{\"jsonrpc\":\"2.0\",\"method\":\"eth_getTransactionReceipt\",\"params\":[\"%1%\"],\"id\":%2%}") % tx_id.c_str() % t_id); m_endpoint.send(m_hdl, req.c_str(), websocketpp::frame::opcode::text); + m_requests[t_id] = req_t::ETH_GET_TRANSACTION_RECEIPT; return t_id++; } uint64_t eth_rpc_client::eth_call(const std::string& to, const std::string& data) { std::string req = str(boost::format("{\"jsonrpc\": \"2.0\", \"method\": \"eth_call\", \"params\": [{\"to\": \"%1%\", \"data\": \"%2%\"}, \"latest\"], \"id\": %3%}") % to.c_str() % data.c_str() % t_id); m_endpoint.send(m_hdl, req.c_str(), websocketpp::frame::opcode::text); - + m_requests[t_id] = req_t::ETH_CALL; + return t_id++; } uint64_t eth_rpc_client::eth_sendTransaction(const std::string& from, const std::string& to, const std::string& data) { std::string req = str(boost::format("{\"jsonrpc\":\"2.0\",\"method\": \"eth_sendTransaction\", \"params\": [{\"from\": \"%1%\", \"to\": \"%2%\", \"data\": \"%3%\"}], \"id\": %4%}") % from.c_str() % to.c_str() % data.c_str() % t_id); m_endpoint.send(m_hdl, req.c_str(), websocketpp::frame::opcode::text); + m_requests[t_id] = req_t::ETH_SEND_TRANSACTION; return t_id++; } @@ -130,26 +173,29 @@ uint64_t eth_rpc_client::eth_sendTransaction(const std::string& from, const std: uint64_t eth_rpc_client::eth_sendRawTransaction(const std::string& params) { std::string req = str(boost::format("{\"jsonrpc\":\"2.0\",\"method\":\"eth_sendRawTransaction\",\"params\":[\"%1%\"],\"id\":%2%}") % params.c_str() % t_id); m_endpoint.send(m_hdl, req.c_str(), websocketpp::frame::opcode::text); + m_requests[t_id] = req_t::ETH_SEND_RAW_TRANSACTION; + return t_id++; } uint64_t eth_rpc_client::eth_getCode(const std::string& addr) { std::string req = str(boost::format("{\"jsonrpc\":\"2.0\",\"method\": \"eth_getCode\", \"params\": [\"%1%\",\"latest\"], \"id\": %2%}") % addr.c_str() % t_id); m_endpoint.send(m_hdl, req.c_str(), websocketpp::frame::opcode::text); - + m_requests[t_id] = req_t::ETH_GET_CODE; return t_id++; } uint64_t eth_rpc_client::eth_getBalance(const std::string& addr) { std::string req = str(boost::format("{\"jsonrpc\":\"2.0\",\"method\": \"eth_getBalance\", \"params\": [\"%1%\",\"latest\"], \"id\": %2%}") % addr.c_str() % t_id); m_endpoint.send(m_hdl, req.c_str(), websocketpp::frame::opcode::text); - + m_requests[t_id] = req_t::ETH_GET_BALANCE; return t_id++; } uint64_t eth_rpc_client::eth_sign(const string& addr, const string& message) { std::string req = str(boost::format("{\"jsonrpc\":\"2.0\",\"method\": \"eth_sign\", \"params\": [\"%1%\",\"%2%\"], \"id\": %2%}") % addr.c_str() % message.c_str() % t_id); m_endpoint.send(m_hdl, req.c_str(), websocketpp::frame::opcode::text); + m_requests[t_id] = req_t::ETH_SIGN; return t_id++; } @@ -176,6 +222,8 @@ uint64_t eth_rpc_client::add_owner(const std::string& addr ) { std::string gasPrice = str(boost::format("%032u") % 0); std::string gasToken = "0000000000000000000000000000000000000000"; std::string refundReceiver = "0000000000000000000000000000000000000000"; + + m_requests[t_id] = req_t::ADD_OWNER; } uint64_t eth_rpc_client::remove_owner(const std::string& addr, uint32_t threshold) { @@ -201,6 +249,7 @@ uint64_t eth_rpc_client::remove_owner(const std::string& addr, uint32_t threshol std::string gasToken = "0000000000000000000000000000000000000000"; std::string refundReceiver = "0000000000000000000000000000000000000000"; + m_requests[t_id] = req_t::REMOVE_OWNER; } std::string eth_rpc_client::addmultisigaddress(const uint32_t nrequired, const std::vector public_keys) {