From a2c0793c488cfb1c553d5a9cf612960936ab4052 Mon Sep 17 00:00:00 2001 From: Pavel Baykov Date: Mon, 20 Jun 2022 09:45:50 -0300 Subject: [PATCH] big multiprecision values and serialize an Ethereum transaction for RPC --- .../sidechain_net_handler_eth.hpp | 33 +++++++++++++++++++ .../sidechain_net_handler_eth.cpp | 6 ++++ 2 files changed, 39 insertions(+) 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 d95d81a2..7b9427cb 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 @@ -19,6 +19,16 @@ #include +#include + +using namespace boost::multiprecision::literals; +using u256 = boost::multiprecision::number>; +using s256 = boost::multiprecision::number>; +u256 constexpr Invalid256 = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_cppui256; +using byte = uint8_t; +using bytes = std::vector; + + namespace graphene { namespace peerplays_sidechain { typedef websocketpp::client client; @@ -124,6 +134,29 @@ namespace graphene { namespace peerplays_sidechain { std::vector owners; std::string safe_account_addr; private: + /// Encodes an Ethereum transaction for exporting in JSON + class Transaction + { + public: + /// JSON serialization + std::string serialize(const std::string& from, const std::string& to, const std::string& tx_id, u256 const& _value, u256 const& _gasPrice, u256 const& _gas, bytes const& _data, u256 const& _nonce = Invalid256); + protected: + /// Type of transaction. + enum Type + { + NullTransaction, ///< Null transaction. + ContractCreation, ///< Transaction to create contracts - receiveAddress() is ignored. + MessageCall ///< Transaction to invoke a message call - receiveAddress() is used. + }; + + Type m_type = NullTransaction; ///< Is this a contract-creation transaction or a message-call transaction? + u256 m_nonce; ///< The transaction-count of the sender. + u256 m_value; ///< The amount of ETH to be transferred by this transaction. Called 'endowment' for contract-creation transactions. + u256 m_gasPrice; ///< The base fee and thus the implied exchange rate of ETH to GAS. + u256 m_gas; ///< The total gas to convert, paid for from sender's account. Any unused gas gets refunded once the contract is ended. + bytes m_data; ///< The data associated with the transaction, or the initialiser if it's a creation transaction. + }; + class ethereum_function_call_encoder { public: enum operation_t { diff --git a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_eth.cpp b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_eth.cpp index dc0544f9..5afa4eda 100644 --- a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_eth.cpp +++ b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_eth.cpp @@ -28,6 +28,12 @@ extern "C" { } namespace graphene { namespace peerplays_sidechain { + std::string eth_rpc_client::Transaction::serialize(const std::string& from, const std::string& to, const std::string& tx_id,u256 const& _value, u256 const& _gasPrice, u256 const& _gas, bytes const& _data, u256 const& _nonce) { + std::string m_data_str(m_data.begin(), m_data.end()); + std::string req = str(boost::format("{\"jsonrpc\":\"2.0\",\"method\":\"account_signTransaction\",\"params\":[{\"from\": \"%1%\", \"gas\": \"%2%\", \"gasPrice\": \"%3%\", \"input\": \"%4%\", \"nonce\": \"%5%\", \"to\": \"%6%\", \"value\": \"%7%\" }],\"id\":%8%}") % from.c_str() % m_gas.str() % m_gasPrice.str() % m_data_str % m_nonce.str() % to.c_str() % m_value.str() % tx_id.c_str()); + return req; + } + // ============================================================================= std::string eth_rpc_client::ethereum_function_call_encoder::encode_function_signature(const std::string& function_signature) { sha3_context c;