From 2fbe2ffe68b544beb825b1e6f05a870694a7f459 Mon Sep 17 00:00:00 2001 From: serkixenos Date: Fri, 29 Jul 2022 18:48:57 +0200 Subject: [PATCH 1/5] Improve SONs config parameter checks --- CMakeLists.txt | 1 - README.md | 6 +++--- libraries/CMakeLists.txt | 2 +- .../peerplays_sidechain_plugin.cpp | 12 ++---------- 4 files changed, 6 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1d4c2f1d..695881be 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,7 +51,6 @@ if(CMAKE_SYSTEM_NAME MATCHES "Linux") list(GET arg_list 0 output) message("Ubuntu version is: ${output}") add_definitions(-DPEERPLAYS_UBUNTU_VERSION=${output}) - endif() # function to help with cUrl diff --git a/README.md b/README.md index a8d98021..f3d8c5b9 100644 --- a/README.md +++ b/README.md @@ -84,9 +84,9 @@ sudo apt-get install \ Install Boost libraries from source ``` -wget -c 'http://sourceforge.net/projects/boost/files/boost/1.67.0/boost_1_67_0.tar.bz2/download' -O boost_1_67_0.tar.bz2 -tar xjf boost_1_67_0.tar.bz2 -cd boost_1_67_0/ +wget -c 'https://boostorg.jfrog.io/artifactory/main/release/1.71.0/source/boost_1_71_0.tar.bz2' -O boost_1_71_0.tar.bz2 +tar xjf boost_1_71_0.tar.bz2 +cd boost_1_71_0/ ./bootstrap.sh sudo ./b2 install ``` diff --git a/libraries/CMakeLists.txt b/libraries/CMakeLists.txt index dfe3d397..df872464 100644 --- a/libraries/CMakeLists.txt +++ b/libraries/CMakeLists.txt @@ -1,4 +1,3 @@ -add_subdirectory( vendor ) add_subdirectory( app ) add_subdirectory( chain ) add_subdirectory( db ) @@ -8,4 +7,5 @@ add_subdirectory( net ) add_subdirectory( plugins ) add_subdirectory( time ) add_subdirectory( utilities ) +add_subdirectory( vendor ) add_subdirectory( wallet ) diff --git a/libraries/plugins/peerplays_sidechain/peerplays_sidechain_plugin.cpp b/libraries/plugins/peerplays_sidechain/peerplays_sidechain_plugin.cpp index 6300c8e4..2e5742a6 100644 --- a/libraries/plugins/peerplays_sidechain/peerplays_sidechain_plugin.cpp +++ b/libraries/plugins/peerplays_sidechain/peerplays_sidechain_plugin.cpp @@ -245,22 +245,14 @@ void peerplays_sidechain_plugin_impl::plugin_initialize(const boost::program_opt } #ifdef ENABLE_PEERPLAYS_ASSET_DEPOSITS - sidechain_enabled_peerplays = true; //options.at("peerplays-sidechain-enabled").as(); + sidechain_enabled_peerplays = true; #else sidechain_enabled_peerplays = false; #endif config_ready_peerplays = true; - if (!config_ready_peerplays) { + if (sidechain_enabled_peerplays && !config_ready_peerplays) { wlog("Haven't set up Peerplays sidechain parameters"); } - - if (!(config_ready_bitcoin && - config_ready_ethereum && - config_ready_hive && - config_ready_peerplays)) { - wlog("Haven't set up any sidechain parameters"); - throw; - } } void peerplays_sidechain_plugin_impl::plugin_startup() { From f780fa794f9231404a36f55347e168adb761d583 Mon Sep 17 00:00:00 2001 From: Vlad Dobromyslov Date: Fri, 26 Aug 2022 10:57:18 +0300 Subject: [PATCH 2/5] Fix get_nonce --- .../peerplays_sidechain/sidechain_net_handler_ethereum.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp index 86a162f4..734a5fcd 100644 --- a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp +++ b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp @@ -20,7 +20,7 @@ #include #include -//#define SEND_RAW_TRANSACTION 1 +#define SEND_RAW_TRANSACTION 1 namespace graphene { namespace peerplays_sidechain { @@ -642,7 +642,7 @@ std::string sidechain_net_handler_ethereum::sign_transaction(const sidechain_tra #ifdef SEND_RAW_TRANSACTION ethereum::raw_transaction raw_tr; - raw_tr.nonce = ethereum::to_hex( ethereum::from_hex( rpc_client->get_nonce( ethereum::add_0x(public_key) ) ) + 1 ); + raw_tr.nonce = ethereum::add_0x( ethereum::to_hex( ethereum::from_hex( rpc_client->get_nonce( ethereum::add_0x(public_key) ) ) ) ); raw_tr.gas_price = rpc_client->get_gas_price(); raw_tr.gas_limit = rpc_client->get_gas_limit(); raw_tr.to = wallet_contract_address; From ab7bb9e63305bba74d5c9ce5ffc1b3e878fd86e8 Mon Sep 17 00:00:00 2001 From: Vlad Dobromyslov Date: Fri, 26 Aug 2022 11:09:55 +0300 Subject: [PATCH 3/5] clang-format --- .../peerplays_sidechain/ethereum/decoders.cpp | 97 ++++++++----------- .../peerplays_sidechain/ethereum/encoders.cpp | 74 ++++++-------- .../ethereum/transaction.cpp | 49 ++++------ .../peerplays_sidechain/ethereum/utils.cpp | 21 ++-- .../peerplays_sidechain/ethereum/decoders.hpp | 17 ++-- .../peerplays_sidechain/ethereum/encoders.hpp | 18 ++-- .../ethereum/transaction.hpp | 24 ++--- .../peerplays_sidechain/ethereum/utils.hpp | 22 ++--- .../sidechain_net_handler_ethereum.hpp | 12 +-- .../sidechain_net_handler_ethereum.cpp | 91 +++++++++-------- 10 files changed, 183 insertions(+), 242 deletions(-) diff --git a/libraries/plugins/peerplays_sidechain/ethereum/decoders.cpp b/libraries/plugins/peerplays_sidechain/ethereum/decoders.cpp index cadb3f65..02f334f5 100644 --- a/libraries/plugins/peerplays_sidechain/ethereum/decoders.cpp +++ b/libraries/plugins/peerplays_sidechain/ethereum/decoders.cpp @@ -7,71 +7,64 @@ namespace graphene { namespace peerplays_sidechain { namespace ethereum { //! rlp_decoder -namespace -{ +namespace { const signed char p_util_hexdigit[256] = - { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - 0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1, - -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 }; + {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, + -1, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; } -std::vector rlp_decoder::decode(const std::string& str) -{ +std::vector rlp_decoder::decode(const std::string &str) { size_t consumed = 0; const auto raw_vec = parse_hex(str); const std::vector rlp_array = decode_rlp(raw_vec.data(), raw_vec.size(), consumed); std::vector result_array; - for(const auto& rlp : decode_rlp(raw_vec.data(), raw_vec.size(), consumed)) - { - result_array.emplace_back( bytes2hex( rlp ) ); + for (const auto &rlp : decode_rlp(raw_vec.data(), raw_vec.size(), consumed)) { + result_array.emplace_back(bytes2hex(rlp)); } return result_array; } -std::vector rlp_decoder::decode_rlp(const unsigned char *raw, size_t len, size_t& consumed) -{ +std::vector rlp_decoder::decode_rlp(const unsigned char *raw, size_t len, size_t &consumed) { std::vector rlp_result; consumed = 0; - const unsigned char* end = raw + len; + const unsigned char *end = raw + len; const size_t prefixlen = 1; unsigned char ch = *raw; - if (len < 1) - { + if (len < 1) { return rlp_result; } // Case 1: [prefix is 1-byte data buffer] - if (ch <= 0x7f) - { + if (ch <= 0x7f) { const unsigned char *tok_start = raw; const unsigned char *tok_end = tok_start + prefixlen; FC_ASSERT(tok_end <= end); // parsing done; assign data buffer value. const std::vector buf{tok_start, tok_end}; - rlp_result.emplace_back( buf.cbegin(), buf.cend() ); + rlp_result.emplace_back(buf.cbegin(), buf.cend()); consumed = buf.size(); } // Case 2: [prefix, including buffer length][data] - else if ((ch >= 0x80) && (ch <= 0xb7)) - { + else if ((ch >= 0x80) && (ch <= 0xb7)) { const size_t blen = ch - 0x80; const size_t expected = prefixlen + blen; @@ -88,13 +81,12 @@ std::vector rlp_decoder::decode_rlp(const unsigned char *raw, size_ // parsing done; assign data buffer value. const std::vector buf{tok_start, tok_end}; - rlp_result.emplace_back( buf.cbegin(), buf.cend() ); + rlp_result.emplace_back(buf.cbegin(), buf.cend()); consumed = expected; } // Case 3: [prefix][buffer length][data] - else if ((ch >= 0xb8) && (ch <= 0xbf)) - { + else if ((ch >= 0xb8) && (ch <= 0xbf)) { const size_t uintlen = ch - 0xb7; size_t expected = prefixlen + uintlen; @@ -104,7 +96,7 @@ std::vector rlp_decoder::decode_rlp(const unsigned char *raw, size_ FC_ASSERT(uintlen > 0 && uintlen <= RLP_maxUintLen); const unsigned char *tok_start = raw + prefixlen; - if ((uintlen > 1) && (tok_start[0] == 0)) // no leading zeroes + if ((uintlen > 1) && (tok_start[0] == 0)) // no leading zeroes return std::vector{}; // read buffer length @@ -119,13 +111,12 @@ std::vector rlp_decoder::decode_rlp(const unsigned char *raw, size_ tok_start = raw + prefixlen + uintlen; const unsigned char *tok_end = tok_start + slen; const std::vector buf{tok_start, tok_end}; - rlp_result.emplace_back( buf.cbegin(), buf.cend() ); + rlp_result.emplace_back(buf.cbegin(), buf.cend()); consumed = expected; } // Case 4: [prefix][list] - else if ((ch >= 0xc0) && (ch <= 0xf7)) - { + else if ((ch >= 0xc0) && (ch <= 0xf7)) { const size_t payloadlen = ch - 0xc0; const size_t expected = prefixlen + payloadlen; @@ -136,8 +127,7 @@ std::vector rlp_decoder::decode_rlp(const unsigned char *raw, size_ consumed = expected; } // Case 5: [prefix][list length][list] - else - { + else { FC_ASSERT((ch >= 0xf8) && (ch <= 0xff)); const size_t uintlen = ch - 0xf7; @@ -149,7 +139,7 @@ std::vector rlp_decoder::decode_rlp(const unsigned char *raw, size_ FC_ASSERT(uintlen > 0 && uintlen <= RLP_maxUintLen); const unsigned char *tok_start = raw + prefixlen; - if ((uintlen > 1) && (tok_start[0] == 0)) // no leading zeroes + if ((uintlen > 1) && (tok_start[0] == 0)) // no leading zeroes return std::vector{}; // read list length @@ -169,8 +159,7 @@ std::vector rlp_decoder::decode_rlp(const unsigned char *raw, size_ return rlp_result; } -std::vector rlp_decoder::decode_array(const unsigned char *raw, size_t len, size_t uintlen, size_t payloadlen) -{ +std::vector rlp_decoder::decode_array(const unsigned char *raw, size_t len, size_t uintlen, size_t payloadlen) { std::vector rlp_result; const size_t prefixlen = 1; @@ -183,8 +172,7 @@ std::vector rlp_decoder::decode_array(const unsigned char *raw, siz const unsigned char *list_ent = raw + prefixlen + uintlen; // recursively read until payloadlen bytes parsed, or error - while (child_len > 0) - { + while (child_len > 0) { size_t child_consumed = 0; const auto val = decode_rlp(list_ent, child_len, child_consumed); @@ -197,8 +185,7 @@ std::vector rlp_decoder::decode_array(const unsigned char *raw, siz return rlp_result; } -uint64_t rlp_decoder::to_int(const unsigned char *raw, size_t len) -{ +uint64_t rlp_decoder::to_int(const unsigned char *raw, size_t len) { if (len == 0) return 0; else if (len == 1) @@ -207,17 +194,14 @@ uint64_t rlp_decoder::to_int(const unsigned char *raw, size_t len) return (raw[len - 1]) + (to_int(raw, len - 1) * 256); } -std::vector rlp_decoder::parse_hex(const std::string& str) -{ +std::vector rlp_decoder::parse_hex(const std::string &str) { return parse_hex(str.c_str()); } -std::vector rlp_decoder::parse_hex(const char* psz) -{ +std::vector rlp_decoder::parse_hex(const char *psz) { // convert hex dump to vector std::vector vch; - while (true) - { + while (true) { while (isspace(*psz)) psz++; signed char c = hex_digit(*psz++); @@ -233,8 +217,7 @@ std::vector rlp_decoder::parse_hex(const char* psz) return vch; } -signed char rlp_decoder::hex_digit(char c) -{ +signed char rlp_decoder::hex_digit(char c) { return p_util_hexdigit[(unsigned char)c]; } diff --git a/libraries/plugins/peerplays_sidechain/ethereum/encoders.cpp b/libraries/plugins/peerplays_sidechain/ethereum/encoders.cpp index 90b05fd2..ad42088b 100644 --- a/libraries/plugins/peerplays_sidechain/ethereum/encoders.cpp +++ b/libraries/plugins/peerplays_sidechain/ethereum/encoders.cpp @@ -1,41 +1,35 @@ #include -#include #include #include +#include #include namespace graphene { namespace peerplays_sidechain { namespace ethereum { //! base_encoder -std::string base_encoder::encode_uint256(boost::multiprecision::uint256_t value) -{ +std::string base_encoder::encode_uint256(boost::multiprecision::uint256_t value) { return (boost::format("%x") % boost::io::group(std::setw(64), std::setfill('0'), value)).str(); } -std::string base_encoder::encode_address(const std::string& value) -{ +std::string base_encoder::encode_address(const std::string &value) { return (boost::format("%x") % boost::io::group(std::setw(64), std::setfill('0'), value)).str(); } -std::string base_encoder::encode_string(const std::string& value) -{ +std::string base_encoder::encode_string(const std::string &value) { std::string data = (boost::format("%x") % boost::io::group(std::setw(64), std::setfill('0'), value.size())).str(); - data += boost::algorithm::hex(value) + std::string( (64 - value.size() * 2 % 64), '0' ); + data += boost::algorithm::hex(value) + std::string((64 - value.size() * 2 % 64), '0'); return data; } - //! update_owners_encoder -std::string update_owners_encoder::encode(const std::vector>& owners_weights, const std::string& object_id) const -{ +std::string update_owners_encoder::encode(const std::vector> &owners_weights, const std::string &object_id) const { std::string data = "0x" + function_signature; data += base_encoder::encode_uint256(64); data += base_encoder::encode_uint256((owners_weights.size() * 2 + 3) * 32); data += base_encoder::encode_uint256(owners_weights.size()); - for(const auto& owner : owners_weights) - { + for (const auto &owner : owners_weights) { data += base_encoder::encode_address(owner.first); data += base_encoder::encode_uint256(owner.second); } @@ -45,74 +39,62 @@ std::string update_owners_encoder::encode(const std::vector= '0' && input <= '9') +int rlp_encoder::char2int(char input) { + if (input >= '0' && input <= '9') return input - '0'; - if(input >= 'A' && input <= 'F') + if (input >= 'A' && input <= 'F') return input - 'A' + 10; - if(input >= 'a' && input <= 'f') + if (input >= 'a' && input <= 'f') return input - 'a' + 10; return -1; } -void rlp_encoder::hex2bin(const char* src, char* target) -{ - while(*src && src[1]) - { - *(target++) = char2int(*src)*16 + char2int(src[1]); +void rlp_encoder::hex2bin(const char *src, char *target) { + while (*src && src[1]) { + *(target++) = char2int(*src) * 16 + char2int(src[1]); src += 2; } } diff --git a/libraries/plugins/peerplays_sidechain/ethereum/transaction.cpp b/libraries/plugins/peerplays_sidechain/ethereum/transaction.cpp index 8a5f7fdf..668e9639 100644 --- a/libraries/plugins/peerplays_sidechain/ethereum/transaction.cpp +++ b/libraries/plugins/peerplays_sidechain/ethereum/transaction.cpp @@ -10,14 +10,13 @@ #include #include -#include #include +#include #include #include namespace graphene { namespace peerplays_sidechain { namespace ethereum { - const secp256k1_context *eth_context() { static secp256k1_context *ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN); return ctx; @@ -25,13 +24,11 @@ const secp256k1_context *eth_context() { //! transaction -const transaction& transaction::sign(const std::string& private_key) const -{ +const transaction &transaction::sign(const std::string &private_key) const { return *this; } -std::string transaction::serialize() const -{ +std::string transaction::serialize() const { boost::property_tree::ptree pt; pt.put("from", from); pt.put("to", to); @@ -42,24 +39,22 @@ std::string transaction::serialize() const return ss.str(); } -void transaction::deserialize(const std::string& raw_tx) -{ +void transaction::deserialize(const std::string &raw_tx) { std::stringstream ss_tx(raw_tx); boost::property_tree::ptree tx_json; boost::property_tree::read_json(ss_tx, tx_json); - if(tx_json.count("from")) + if (tx_json.count("from")) from = tx_json.get("from"); - if(tx_json.count("to")) + if (tx_json.count("to")) to = tx_json.get("to"); - if(tx_json.count("data")) + if (tx_json.count("data")) data = tx_json.get("data"); } //! raw_transaction -signed_transaction raw_transaction::sign(const std::string& private_key) const -{ +signed_transaction raw_transaction::sign(const std::string &private_key) const { //! Prepare signed transaction signed_transaction tr; tr.nonce = nonce; @@ -72,8 +67,8 @@ signed_transaction raw_transaction::sign(const std::string& private_key) const //! Calculate keccak hash of transaction bytes hash; hash.resize(32); - const auto transaction_string = boost::algorithm::unhex( remove_0x( serialize() ) ); - keccak_256((const unsigned char *) transaction_string.data(), transaction_string.size(), (unsigned char *) hash.data()); + const auto transaction_string = boost::algorithm::unhex(remove_0x(serialize())); + keccak_256((const unsigned char *)transaction_string.data(), transaction_string.size(), (unsigned char *)hash.data()); const bytes priv_key = parse_hex(private_key); @@ -85,11 +80,11 @@ signed_transaction raw_transaction::sign(const std::string& private_key) const bytes v = bytes{char(recid + from_hex(chain_id) * 2 + 35)}; bytes r; - for(int i = 1; i < 33; i++) - r.emplace_back((char) result.at(i)); + for (int i = 1; i < 33; i++) + r.emplace_back((char)result.at(i)); bytes s; - for(int i = 33; i < 65; i++) - s.emplace_back((char) result.at(i)); + for (int i = 33; i < 65; i++) + s.emplace_back((char)result.at(i)); tr.v = fc::to_hex((char *)&v[0], v.size()); tr.r = fc::to_hex((char *)&r[0], r.size()); @@ -98,8 +93,7 @@ signed_transaction raw_transaction::sign(const std::string& private_key) const return tr; } -std::string raw_transaction::serialize() const -{ +std::string raw_transaction::serialize() const { rlp_encoder encoder; const std::string serialized = encoder.encode(remove_0x(nonce)) + encoder.encode(remove_0x(gas_price)) + @@ -111,11 +105,10 @@ std::string raw_transaction::serialize() const encoder.encode("") + encoder.encode(""); - return add_0x( bytes2hex( encoder.encode_length(serialized.size(), 192) + serialized ) ); + return add_0x(bytes2hex(encoder.encode_length(serialized.size(), 192) + serialized)); } -void raw_transaction::deserialize(const std::string& raw_tx) -{ +void raw_transaction::deserialize(const std::string &raw_tx) { rlp_decoder decoder; const auto rlp_array = decoder.decode(remove_0x(raw_tx)); FC_ASSERT(rlp_array.size() >= 7, "Wrong rlp format"); @@ -131,8 +124,7 @@ void raw_transaction::deserialize(const std::string& raw_tx) //! signed_transaction -std::string signed_transaction::serialize() const -{ +std::string signed_transaction::serialize() const { rlp_encoder encoder; const std::string serialized = encoder.encode(remove_0x(nonce)) + encoder.encode(remove_0x(gas_price)) + @@ -144,11 +136,10 @@ std::string signed_transaction::serialize() const encoder.encode(remove_0x(r)) + encoder.encode(remove_0x(s)); - return add_0x( bytes2hex( encoder.encode_length(serialized.size(), 192) + serialized ) ); + return add_0x(bytes2hex(encoder.encode_length(serialized.size(), 192) + serialized)); } -void signed_transaction::deserialize(const std::string& raw_tx) -{ +void signed_transaction::deserialize(const std::string &raw_tx) { rlp_decoder decoder; const auto rlp_array = decoder.decode(remove_0x(raw_tx)); FC_ASSERT(rlp_array.size() >= 9, "Wrong rlp format"); diff --git a/libraries/plugins/peerplays_sidechain/ethereum/utils.cpp b/libraries/plugins/peerplays_sidechain/ethereum/utils.cpp index 04861b88..ce64e1ae 100644 --- a/libraries/plugins/peerplays_sidechain/ethereum/utils.cpp +++ b/libraries/plugins/peerplays_sidechain/ethereum/utils.cpp @@ -10,23 +10,20 @@ bytes parse_hex(const std::string &str) { return vec; } -std::string bytes2hex(const std::string& s) -{ +std::string bytes2hex(const std::string &s) { std::string dest; - for( const auto& i : s ) + for (const auto &i : s) dest += uchar2Hex((unsigned char)i); return dest; } -std::string uchar2Hex(unsigned char n) -{ +std::string uchar2Hex(unsigned char n) { std::string dest; dest.resize(2); sprintf(&dest[0], "%X", n); - if(n < (unsigned char)16) - { + if (n < (unsigned char)16) { dest[1] = dest[0]; dest[0] = '0'; } @@ -34,9 +31,8 @@ std::string uchar2Hex(unsigned char n) return dest; } -std::string add_0x(const std::string& s) -{ - if(s.size() > 1) { +std::string add_0x(const std::string &s) { + if (s.size() > 1) { if (s.substr(0, 2) == "0x") return s; } @@ -44,9 +40,8 @@ std::string add_0x(const std::string& s) return "0x" + s; } -std::string remove_0x(const std::string& s) -{ - if(s.size() > 1) { +std::string remove_0x(const std::string &s) { + if (s.size() > 1) { if (s.substr(0, 2) == "0x") return s.substr(2); } diff --git a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/ethereum/decoders.hpp b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/ethereum/decoders.hpp index 0f30038f..9edb6ae3 100644 --- a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/ethereum/decoders.hpp +++ b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/ethereum/decoders.hpp @@ -7,22 +7,21 @@ namespace graphene { namespace peerplays_sidechain { namespace ethereum { class rlp_decoder { private: - enum RLP_constants - { - RLP_maxUintLen = 8, - RLP_bufferLenStart = 0x80, - RLP_listStart = 0xc0, + enum RLP_constants { + RLP_maxUintLen = 8, + RLP_bufferLenStart = 0x80, + RLP_listStart = 0xc0, }; public: - static std::vector decode(const std::string& str); + static std::vector decode(const std::string &str); private: - static std::vector decode_rlp(const unsigned char *raw, size_t len, size_t& consumed); + static std::vector decode_rlp(const unsigned char *raw, size_t len, size_t &consumed); static std::vector decode_array(const unsigned char *raw, size_t len, size_t uintlen, size_t payloadlen); static uint64_t to_int(const unsigned char *raw, size_t len); - static std::vector parse_hex(const std::string& str); - static std::vector parse_hex(const char* psz); + static std::vector parse_hex(const std::string &str); + static std::vector parse_hex(const char *psz); static signed char hex_digit(char c); }; diff --git a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/ethereum/encoders.hpp b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/ethereum/encoders.hpp index d3652199..1ff97978 100644 --- a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/ethereum/encoders.hpp +++ b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/ethereum/encoders.hpp @@ -1,42 +1,42 @@ #pragma once +#include #include #include -#include namespace graphene { namespace peerplays_sidechain { namespace ethereum { class base_encoder { public: static std::string encode_uint256(boost::multiprecision::uint256_t value); - static std::string encode_address(const std::string& value); - static std::string encode_string(const std::string& value); + static std::string encode_address(const std::string &value); + static std::string encode_string(const std::string &value); }; class update_owners_encoder { public: const std::string function_signature = "23ab6adf"; //! updateOwners((address,uint256)[],string) - std::string encode(const std::vector>& owners_weights, const std::string& object_id) const; + std::string encode(const std::vector> &owners_weights, const std::string &object_id) const; }; class withdrawal_encoder { public: const std::string function_signature = "e088747b"; //! withdraw(address,uint256,string) - std::string encode(const std::string& to, boost::multiprecision::uint256_t amount, const std::string& object_id) const; + std::string encode(const std::string &to, boost::multiprecision::uint256_t amount, const std::string &object_id) const; }; class rlp_encoder { public: - static std::string encode(const std::string& s); + static std::string encode(const std::string &s); static std::string encode_length(int len, int offset); - static std::string hex2bytes(const std::string& s); + static std::string hex2bytes(const std::string &s); private: - static std::string encode_rlp(const std::string& s); + static std::string encode_rlp(const std::string &s); static int char2int(char input); - static void hex2bin(const char* src, char* target); + static void hex2bin(const char *src, char *target); }; /*class ethereum_function_call_encoder { diff --git a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/ethereum/transaction.hpp b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/ethereum/transaction.hpp index 50c3e2f7..c112206b 100644 --- a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/ethereum/transaction.hpp +++ b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/ethereum/transaction.hpp @@ -6,28 +6,25 @@ namespace graphene { namespace peerplays_sidechain { namespace ethereum { -class base_transaction -{ +class base_transaction { virtual std::string serialize() const = 0; - virtual void deserialize(const std::string& raw_tx) = 0; + virtual void deserialize(const std::string &raw_tx) = 0; }; -class transaction : base_transaction -{ +class transaction : base_transaction { public: std::string from; std::string to; std::string data; - const transaction& sign(const std::string& private_key) const; + const transaction &sign(const std::string &private_key) const; virtual std::string serialize() const override; - virtual void deserialize(const std::string& raw_tx) override; + virtual void deserialize(const std::string &raw_tx) override; }; class signed_transaction; -class raw_transaction : base_transaction -{ +class raw_transaction : base_transaction { public: std::string nonce; std::string gas_price; @@ -37,14 +34,13 @@ public: std::string data; std::string chain_id; - signed_transaction sign(const std::string& private_key) const; + signed_transaction sign(const std::string &private_key) const; virtual std::string serialize() const override; - virtual void deserialize(const std::string& raw_tx) override; + virtual void deserialize(const std::string &raw_tx) override; }; -class signed_transaction : base_transaction -{ +class signed_transaction : base_transaction { public: std::string nonce; std::string gas_price; @@ -57,7 +53,7 @@ public: std::string s; virtual std::string serialize() const override; - virtual void deserialize(const std::string& raw_tx) override; + virtual void deserialize(const std::string &raw_tx) override; }; }}} // namespace graphene::peerplays_sidechain::ethereum diff --git a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/ethereum/utils.hpp b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/ethereum/utils.hpp index 519ed4a1..b4461713 100644 --- a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/ethereum/utils.hpp +++ b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/ethereum/utils.hpp @@ -6,28 +6,26 @@ namespace graphene { namespace peerplays_sidechain { namespace ethereum { bytes parse_hex(const std::string &str); -std::string bytes2hex(const std::string& s); +std::string bytes2hex(const std::string &s); std::string uchar2Hex(unsigned char n); -std::string add_0x(const std::string& s); +std::string add_0x(const std::string &s); -std::string remove_0x(const std::string& s); +std::string remove_0x(const std::string &s); -template -std::string to_hex( const T& val ) -{ +template +std::string to_hex(const T &val) { std::stringstream stream; stream << std::hex << val; - std::string result( stream.str() ); - if(result.size() % 2) - result = "0"+result; + std::string result(stream.str()); + if (result.size() % 2) + result = "0" + result; return result; } -template -T from_hex( const std::string& s ) -{ +template +T from_hex(const std::string &s) { T val; std::stringstream stream; stream << std::hex << s; diff --git a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_ethereum.hpp b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_ethereum.hpp index 68ba2c59..3944d0a2 100644 --- a/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_ethereum.hpp +++ b/libraries/plugins/peerplays_sidechain/include/graphene/peerplays_sidechain/sidechain_net_handler_ethereum.hpp @@ -19,18 +19,18 @@ public: std::string eth_get_block_by_number(std::string block_number, bool full_block); std::string eth_get_logs(std::string wallet_contract_address); std::string net_version(); - std::string eth_get_transaction_count(const std::string& params); + std::string eth_get_transaction_count(const std::string ¶ms); std::string eth_gas_price(); std::string get_chain_id(); std::string get_network_id(); - std::string get_nonce(const std::string& address); + std::string get_nonce(const std::string &address); std::string get_gas_price(); std::string get_gas_limit(); - std::string eth_send_transaction(const std::string& params); - std::string eth_send_raw_transaction(const std::string& params); - std::string eth_get_transaction_receipt(const std::string& params); + std::string eth_send_transaction(const std::string ¶ms); + std::string eth_send_raw_transaction(const std::string ¶ms); + std::string eth_get_transaction_receipt(const std::string ¶ms); }; class sidechain_net_handler_ethereum : public sidechain_net_handler { @@ -58,7 +58,7 @@ private: ethereum::chain_id_type chain_id; ethereum::network_id_type network_id; - std::string create_primary_wallet_transaction(const std::vector &son_pubkeys, const std::string& object_id); + std::string create_primary_wallet_transaction(const std::vector &son_pubkeys, const std::string &object_id); std::string create_deposit_transaction(const son_wallet_deposit_object &swdo); std::string create_withdrawal_transaction(const son_wallet_withdraw_object &swwo); diff --git a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp index 734a5fcd..163f2599 100644 --- a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp +++ b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp @@ -47,7 +47,7 @@ std::string ethereum_rpc_client::net_version() { return send_post_request("net_version", "", debug_rpc_calls); } -std::string ethereum_rpc_client::eth_get_transaction_count(const std::string& params) { +std::string ethereum_rpc_client::eth_get_transaction_count(const std::string ¶ms) { return send_post_request("eth_getTransactionCount", params, debug_rpc_calls); } @@ -65,7 +65,7 @@ std::string ethereum_rpc_client::get_network_id() { return retrieve_value_from_reply(reply_str, "protocols.eth.network"); } -std::string ethereum_rpc_client::get_nonce(const std::string& address) { +std::string ethereum_rpc_client::get_nonce(const std::string &address) { std::string reply_str = eth_get_transaction_count("[\"" + address + "\", \"latest\"]"); return retrieve_value_from_reply(reply_str, ""); } @@ -89,15 +89,15 @@ std::string ethereum_rpc_client::get_gas_limit() { return std::string{}; } -std::string ethereum_rpc_client::eth_send_transaction(const std::string& params) { +std::string ethereum_rpc_client::eth_send_transaction(const std::string ¶ms) { return send_post_request("eth_sendTransaction", "[" + params + "]", debug_rpc_calls); } -std::string ethereum_rpc_client::eth_send_raw_transaction(const std::string& params) { +std::string ethereum_rpc_client::eth_send_raw_transaction(const std::string ¶ms) { return send_post_request("eth_sendRawTransaction", "[ \"" + params + "\" ]", debug_rpc_calls); } -std::string ethereum_rpc_client::eth_get_transaction_receipt(const std::string& params) { +std::string ethereum_rpc_client::eth_get_transaction_receipt(const std::string ¶ms) { return send_post_request("eth_getTransactionReceipt", "[\"" + params + "\"]", debug_rpc_calls); } @@ -541,27 +541,26 @@ std::string sidechain_net_handler_ethereum::process_sidechain_transaction(const std::string sidechain_net_handler_ethereum::send_sidechain_transaction(const sidechain_transaction_object &sto) { boost::property_tree::ptree pt; boost::property_tree::ptree pt_array; - for(const auto& signature : sto.signatures) { - const auto& transaction = signature.second; - #ifdef SEND_RAW_TRANSACTION - const std::string sidechain_transaction = rpc_client->eth_send_raw_transaction(transaction); - #else - const std::string sidechain_transaction = rpc_client->eth_send_transaction(transaction); - #endif + for (const auto &signature : sto.signatures) { + const auto &transaction = signature.second; +#ifdef SEND_RAW_TRANSACTION + const std::string sidechain_transaction = rpc_client->eth_send_raw_transaction(transaction); +#else + const std::string sidechain_transaction = rpc_client->eth_send_transaction(transaction); +#endif std::stringstream ss_tx(sidechain_transaction); boost::property_tree::ptree tx_json; boost::property_tree::read_json(ss_tx, tx_json); - if( tx_json.count("result") && !tx_json.count("error") ) { + if (tx_json.count("result") && !tx_json.count("error")) { boost::property_tree::ptree node; node.put("transaction", transaction); node.put("transaction_receipt", tx_json.get("result")); pt_array.push_back(std::make_pair("", node)); - } - else { + } else { //! Fixme //! How should we proceed with error in eth_send_transaction - elog("Error in eth_send_transaction for transaction ${id}, transaction ${transaction}", ("id", sto.id) ("transaction", transaction)); + elog("Error in eth_send_transaction for transaction ${id}, transaction ${transaction}", ("id", sto.id)("transaction", transaction)); } } pt.add_child("result_array", pt_array); @@ -576,25 +575,24 @@ bool sidechain_net_handler_ethereum::settle_sidechain_transaction(const sidechai boost::property_tree::ptree json; boost::property_tree::read_json(ss, json); - if( !json.count("result_array") ) { + if (!json.count("result_array")) { return false; } size_t count = 0; - for(const auto &entry : json.get_child("result_array")) { - const std::string receipt = rpc_client->eth_get_transaction_receipt( entry.second.get("transaction_receipt") ); + for (const auto &entry : json.get_child("result_array")) { + const std::string receipt = rpc_client->eth_get_transaction_receipt(entry.second.get("transaction_receipt")); std::stringstream ss_receipt(receipt); boost::property_tree::ptree json_receipt; boost::property_tree::read_json(ss_receipt, json_receipt); - if( json_receipt.get("result") == "null" ) { + if (json_receipt.get("result") == "null") { wlog("Block is not minted yet for transaction ${id}", ("id", sto.id)); return false; } - if( "0x1" == json_receipt.get("result.status") ) - { + if ("0x1" == json_receipt.get("result.status")) { count += 1; //! Fixme - compare data somehow? //if( sto.transaction == entry_receipt.second.get("data") ) { @@ -603,17 +601,16 @@ bool sidechain_net_handler_ethereum::settle_sidechain_transaction(const sidechai } //! Check that we have all transactions - if(count != json.get_child("result_array").size()) { + if (count != json.get_child("result_array").size()) { wlog("Not all receipts received for transaction ${id}", ("id", sto.id)); return false; - } - else + } else return true; return false; } -std::string sidechain_net_handler_ethereum::create_primary_wallet_transaction(const std::vector &son_pubkeys, const std::string& object_id) { +std::string sidechain_net_handler_ethereum::create_primary_wallet_transaction(const std::vector &son_pubkeys, const std::string &object_id) { std::vector> owners_weights; for (auto &son : son_pubkeys) { FC_ASSERT(son.sidechain_public_keys.contains(sidechain_type::ethereum), "No public keys for son: ${son_id}", ("son_id", son.son_id)); @@ -631,34 +628,34 @@ std::string sidechain_net_handler_ethereum::create_deposit_transaction(const son std::string sidechain_net_handler_ethereum::create_withdrawal_transaction(const son_wallet_withdraw_object &swwo) { ethereum::withdrawal_encoder encoder; - return encoder.encode(swwo.withdraw_address.substr(2), swwo.withdraw_amount.value*10000000000, swwo.id.operator std::string()); + return encoder.encode(swwo.withdraw_address.substr(2), swwo.withdraw_amount.value * 10000000000, swwo.id.operator std::string()); } std::string sidechain_net_handler_ethereum::sign_transaction(const sidechain_transaction_object &sto) { - const auto& current_son = plugin.get_current_son_object(); + const auto ¤t_son = plugin.get_current_son_object(); FC_ASSERT(current_son.sidechain_public_keys.contains(sidechain_type::ethereum), "No public keys for current son: ${account_id}", ("account_id", current_son.son_account)); - const auto& public_key = current_son.sidechain_public_keys.at(sidechain); + const auto &public_key = current_son.sidechain_public_keys.at(sidechain); - #ifdef SEND_RAW_TRANSACTION - ethereum::raw_transaction raw_tr; - raw_tr.nonce = ethereum::add_0x( ethereum::to_hex( ethereum::from_hex( rpc_client->get_nonce( ethereum::add_0x(public_key) ) ) ) ); - raw_tr.gas_price = rpc_client->get_gas_price(); - raw_tr.gas_limit = rpc_client->get_gas_limit(); - raw_tr.to = wallet_contract_address; - raw_tr.value = ""; - raw_tr.data = sto.transaction; - raw_tr.chain_id = ethereum::add_0x(ethereum::to_hex(chain_id)); +#ifdef SEND_RAW_TRANSACTION + ethereum::raw_transaction raw_tr; + raw_tr.nonce = ethereum::add_0x(ethereum::to_hex(ethereum::from_hex(rpc_client->get_nonce(ethereum::add_0x(public_key))))); + raw_tr.gas_price = rpc_client->get_gas_price(); + raw_tr.gas_limit = rpc_client->get_gas_limit(); + raw_tr.to = wallet_contract_address; + raw_tr.value = ""; + raw_tr.data = sto.transaction; + raw_tr.chain_id = ethereum::add_0x(ethereum::to_hex(chain_id)); - const auto sign_tr = raw_tr.sign(get_private_key(public_key)); - return sign_tr.serialize(); - #else - ethereum::transaction sign_transaction; - sign_transaction.data = sto.transaction; - sign_transaction.to = wallet_contract_address; - sign_transaction.from = "0x" + public_key; - return sign_transaction.sign(get_private_key(public_key)).serialize(); - #endif + const auto sign_tr = raw_tr.sign(get_private_key(public_key)); + return sign_tr.serialize(); +#else + ethereum::transaction sign_transaction; + sign_transaction.data = sto.transaction; + sign_transaction.to = wallet_contract_address; + sign_transaction.from = "0x" + public_key; + return sign_transaction.sign(get_private_key(public_key)).serialize(); +#endif } void sidechain_net_handler_ethereum::schedule_ethereum_listener() { From fbe263e2cb294816773d7a4fa155e0484e80c2b1 Mon Sep 17 00:00:00 2001 From: Vlad Dobromyslov Date: Mon, 29 Aug 2022 13:52:37 +0300 Subject: [PATCH 4/5] Fix for nonce = 0 --- .../peerplays_sidechain/sidechain_net_handler_ethereum.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp index 163f2599..8ceb582b 100644 --- a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp +++ b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp @@ -67,7 +67,8 @@ std::string ethereum_rpc_client::get_network_id() { std::string ethereum_rpc_client::get_nonce(const std::string &address) { std::string reply_str = eth_get_transaction_count("[\"" + address + "\", \"latest\"]"); - return retrieve_value_from_reply(reply_str, ""); + const auto nonce_val = ethereum::from_hex(retrieve_value_from_reply(reply_str, "")); + return nonce_val == 0 ? ethereum::add_0x("0") : ethereum::add_0x(ethereum::to_hex(nonce_val)); } std::string ethereum_rpc_client::get_gas_price() { @@ -639,7 +640,7 @@ std::string sidechain_net_handler_ethereum::sign_transaction(const sidechain_tra #ifdef SEND_RAW_TRANSACTION ethereum::raw_transaction raw_tr; - raw_tr.nonce = ethereum::add_0x(ethereum::to_hex(ethereum::from_hex(rpc_client->get_nonce(ethereum::add_0x(public_key))))); + raw_tr.nonce = rpc_client->get_nonce(ethereum::add_0x(public_key)); raw_tr.gas_price = rpc_client->get_gas_price(); raw_tr.gas_limit = rpc_client->get_gas_limit(); raw_tr.to = wallet_contract_address; From 1f3beba32acda451e79bd0eae9f2404988d3c833 Mon Sep 17 00:00:00 2001 From: Vlad Dobromyslov Date: Mon, 29 Aug 2022 15:31:42 +0300 Subject: [PATCH 5/5] Don't send transaction that is not signed --- .../peerplays_sidechain/sidechain_net_handler_ethereum.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp index 8ceb582b..52d401cb 100644 --- a/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp +++ b/libraries/plugins/peerplays_sidechain/sidechain_net_handler_ethereum.cpp @@ -544,6 +544,11 @@ std::string sidechain_net_handler_ethereum::send_sidechain_transaction(const sid boost::property_tree::ptree pt_array; for (const auto &signature : sto.signatures) { const auto &transaction = signature.second; + + //! Check if we have this signed transaction, if not, don't send it + if(transaction.empty()) + continue; + #ifdef SEND_RAW_TRANSACTION const std::string sidechain_transaction = rpc_client->eth_send_raw_transaction(transaction); #else @@ -562,6 +567,7 @@ std::string sidechain_net_handler_ethereum::send_sidechain_transaction(const sid //! Fixme //! How should we proceed with error in eth_send_transaction elog("Error in eth_send_transaction for transaction ${id}, transaction ${transaction}", ("id", sto.id)("transaction", transaction)); + return std::string{}; //! Return empty string, as we have error in sending } } pt.add_child("result_array", pt_array);