clang-format

This commit is contained in:
Vlad Dobromyslov 2022-08-26 11:09:55 +03:00
parent f780fa794f
commit ab7bb9e633
10 changed files with 183 additions and 242 deletions

View file

@ -7,71 +7,64 @@ namespace graphene { namespace peerplays_sidechain { namespace ethereum {
//! rlp_decoder //! rlp_decoder
namespace namespace {
{
const signed char p_util_hexdigit[256] = 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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1,-1,-1,-1,-1,-1,-1,-1,-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, 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, 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,0xa,0xb,0xc,0xd,0xe,0xf,-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,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1,-1,-1,-1,-1,-1,-1,-1,-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<std::string> rlp_decoder::decode(const std::string& str) std::vector<std::string> rlp_decoder::decode(const std::string &str) {
{
size_t consumed = 0; size_t consumed = 0;
const auto raw_vec = parse_hex(str); const auto raw_vec = parse_hex(str);
const std::vector<std::string> rlp_array = decode_rlp(raw_vec.data(), raw_vec.size(), consumed); const std::vector<std::string> rlp_array = decode_rlp(raw_vec.data(), raw_vec.size(), consumed);
std::vector<std::string> result_array; std::vector<std::string> result_array;
for(const auto& rlp : decode_rlp(raw_vec.data(), raw_vec.size(), consumed)) for (const auto &rlp : decode_rlp(raw_vec.data(), raw_vec.size(), consumed)) {
{ result_array.emplace_back(bytes2hex(rlp));
result_array.emplace_back( bytes2hex( rlp ) );
} }
return result_array; return result_array;
} }
std::vector<std::string> rlp_decoder::decode_rlp(const unsigned char *raw, size_t len, size_t& consumed) std::vector<std::string> rlp_decoder::decode_rlp(const unsigned char *raw, size_t len, size_t &consumed) {
{
std::vector<std::string> rlp_result; std::vector<std::string> rlp_result;
consumed = 0; consumed = 0;
const unsigned char* end = raw + len; const unsigned char *end = raw + len;
const size_t prefixlen = 1; const size_t prefixlen = 1;
unsigned char ch = *raw; unsigned char ch = *raw;
if (len < 1) if (len < 1) {
{
return rlp_result; return rlp_result;
} }
// Case 1: [prefix is 1-byte data buffer] // Case 1: [prefix is 1-byte data buffer]
if (ch <= 0x7f) if (ch <= 0x7f) {
{
const unsigned char *tok_start = raw; const unsigned char *tok_start = raw;
const unsigned char *tok_end = tok_start + prefixlen; const unsigned char *tok_end = tok_start + prefixlen;
FC_ASSERT(tok_end <= end); FC_ASSERT(tok_end <= end);
// parsing done; assign data buffer value. // parsing done; assign data buffer value.
const std::vector<unsigned char> buf{tok_start, tok_end}; const std::vector<unsigned char> buf{tok_start, tok_end};
rlp_result.emplace_back( buf.cbegin(), buf.cend() ); rlp_result.emplace_back(buf.cbegin(), buf.cend());
consumed = buf.size(); consumed = buf.size();
} }
// Case 2: [prefix, including buffer length][data] // 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 blen = ch - 0x80;
const size_t expected = prefixlen + blen; const size_t expected = prefixlen + blen;
@ -88,13 +81,12 @@ std::vector<std::string> rlp_decoder::decode_rlp(const unsigned char *raw, size_
// parsing done; assign data buffer value. // parsing done; assign data buffer value.
const std::vector<unsigned char> buf{tok_start, tok_end}; const std::vector<unsigned char> buf{tok_start, tok_end};
rlp_result.emplace_back( buf.cbegin(), buf.cend() ); rlp_result.emplace_back(buf.cbegin(), buf.cend());
consumed = expected; consumed = expected;
} }
// Case 3: [prefix][buffer length][data] // Case 3: [prefix][buffer length][data]
else if ((ch >= 0xb8) && (ch <= 0xbf)) else if ((ch >= 0xb8) && (ch <= 0xbf)) {
{
const size_t uintlen = ch - 0xb7; const size_t uintlen = ch - 0xb7;
size_t expected = prefixlen + uintlen; size_t expected = prefixlen + uintlen;
@ -104,7 +96,7 @@ std::vector<std::string> rlp_decoder::decode_rlp(const unsigned char *raw, size_
FC_ASSERT(uintlen > 0 && uintlen <= RLP_maxUintLen); FC_ASSERT(uintlen > 0 && uintlen <= RLP_maxUintLen);
const unsigned char *tok_start = raw + prefixlen; 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<std::string>{}; return std::vector<std::string>{};
// read buffer length // read buffer length
@ -119,13 +111,12 @@ std::vector<std::string> rlp_decoder::decode_rlp(const unsigned char *raw, size_
tok_start = raw + prefixlen + uintlen; tok_start = raw + prefixlen + uintlen;
const unsigned char *tok_end = tok_start + slen; const unsigned char *tok_end = tok_start + slen;
const std::vector<unsigned char> buf{tok_start, tok_end}; const std::vector<unsigned char> buf{tok_start, tok_end};
rlp_result.emplace_back( buf.cbegin(), buf.cend() ); rlp_result.emplace_back(buf.cbegin(), buf.cend());
consumed = expected; consumed = expected;
} }
// Case 4: [prefix][list] // 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 payloadlen = ch - 0xc0;
const size_t expected = prefixlen + payloadlen; const size_t expected = prefixlen + payloadlen;
@ -136,8 +127,7 @@ std::vector<std::string> rlp_decoder::decode_rlp(const unsigned char *raw, size_
consumed = expected; consumed = expected;
} }
// Case 5: [prefix][list length][list] // Case 5: [prefix][list length][list]
else else {
{
FC_ASSERT((ch >= 0xf8) && (ch <= 0xff)); FC_ASSERT((ch >= 0xf8) && (ch <= 0xff));
const size_t uintlen = ch - 0xf7; const size_t uintlen = ch - 0xf7;
@ -149,7 +139,7 @@ std::vector<std::string> rlp_decoder::decode_rlp(const unsigned char *raw, size_
FC_ASSERT(uintlen > 0 && uintlen <= RLP_maxUintLen); FC_ASSERT(uintlen > 0 && uintlen <= RLP_maxUintLen);
const unsigned char *tok_start = raw + prefixlen; 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<std::string>{}; return std::vector<std::string>{};
// read list length // read list length
@ -169,8 +159,7 @@ std::vector<std::string> rlp_decoder::decode_rlp(const unsigned char *raw, size_
return rlp_result; return rlp_result;
} }
std::vector<std::string> rlp_decoder::decode_array(const unsigned char *raw, size_t len, size_t uintlen, size_t payloadlen) std::vector<std::string> rlp_decoder::decode_array(const unsigned char *raw, size_t len, size_t uintlen, size_t payloadlen) {
{
std::vector<std::string> rlp_result; std::vector<std::string> rlp_result;
const size_t prefixlen = 1; const size_t prefixlen = 1;
@ -183,8 +172,7 @@ std::vector<std::string> rlp_decoder::decode_array(const unsigned char *raw, siz
const unsigned char *list_ent = raw + prefixlen + uintlen; const unsigned char *list_ent = raw + prefixlen + uintlen;
// recursively read until payloadlen bytes parsed, or error // recursively read until payloadlen bytes parsed, or error
while (child_len > 0) while (child_len > 0) {
{
size_t child_consumed = 0; size_t child_consumed = 0;
const auto val = decode_rlp(list_ent, child_len, child_consumed); const auto val = decode_rlp(list_ent, child_len, child_consumed);
@ -197,8 +185,7 @@ std::vector<std::string> rlp_decoder::decode_array(const unsigned char *raw, siz
return rlp_result; 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) if (len == 0)
return 0; return 0;
else if (len == 1) 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); return (raw[len - 1]) + (to_int(raw, len - 1) * 256);
} }
std::vector<unsigned char> rlp_decoder::parse_hex(const std::string& str) std::vector<unsigned char> rlp_decoder::parse_hex(const std::string &str) {
{
return parse_hex(str.c_str()); return parse_hex(str.c_str());
} }
std::vector<unsigned char> rlp_decoder::parse_hex(const char* psz) std::vector<unsigned char> rlp_decoder::parse_hex(const char *psz) {
{
// convert hex dump to vector // convert hex dump to vector
std::vector<unsigned char> vch; std::vector<unsigned char> vch;
while (true) while (true) {
{
while (isspace(*psz)) while (isspace(*psz))
psz++; psz++;
signed char c = hex_digit(*psz++); signed char c = hex_digit(*psz++);
@ -233,8 +217,7 @@ std::vector<unsigned char> rlp_decoder::parse_hex(const char* psz)
return vch; 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]; return p_util_hexdigit[(unsigned char)c];
} }

View file

@ -1,41 +1,35 @@
#include <graphene/peerplays_sidechain/ethereum/encoders.hpp> #include <graphene/peerplays_sidechain/ethereum/encoders.hpp>
#include <stdlib.h>
#include <boost/algorithm/hex.hpp> #include <boost/algorithm/hex.hpp>
#include <boost/format.hpp> #include <boost/format.hpp>
#include <stdlib.h>
#include <graphene/peerplays_sidechain/ethereum/utils.hpp> #include <graphene/peerplays_sidechain/ethereum/utils.hpp>
namespace graphene { namespace peerplays_sidechain { namespace ethereum { namespace graphene { namespace peerplays_sidechain { namespace ethereum {
//! base_encoder //! 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(); 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(); 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(); 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; return data;
} }
//! update_owners_encoder //! update_owners_encoder
std::string update_owners_encoder::encode(const std::vector<std::pair<std::string, uint16_t>>& owners_weights, const std::string& object_id) const std::string update_owners_encoder::encode(const std::vector<std::pair<std::string, uint16_t>> &owners_weights, const std::string &object_id) const {
{
std::string data = "0x" + function_signature; std::string data = "0x" + function_signature;
data += base_encoder::encode_uint256(64); 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() * 2 + 3) * 32);
data += base_encoder::encode_uint256(owners_weights.size()); 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_address(owner.first);
data += base_encoder::encode_uint256(owner.second); data += base_encoder::encode_uint256(owner.second);
} }
@ -45,74 +39,62 @@ std::string update_owners_encoder::encode(const std::vector<std::pair<std::strin
} }
//! withdrawal_encoder //! withdrawal_encoder
std::string withdrawal_encoder::encode(const std::string& to, boost::multiprecision::uint256_t amount, const std::string& object_id) const std::string withdrawal_encoder::encode(const std::string &to, boost::multiprecision::uint256_t amount, const std::string &object_id) const {
{
std::string data = "0x" + function_signature; std::string data = "0x" + function_signature;
data += base_encoder::encode_address(to); data += base_encoder::encode_address(to);
data += base_encoder::encode_uint256(amount); data += base_encoder::encode_uint256(amount);
data += base_encoder::encode_uint256(32*3); data += base_encoder::encode_uint256(32 * 3);
data += base_encoder::encode_string(object_id); data += base_encoder::encode_string(object_id);
return data; return data;
} }
//! rlp_encoder //! rlp_encoder
std::string rlp_encoder::encode(const std::string& s) std::string rlp_encoder::encode(const std::string &s) {
{
return encode_rlp(hex2bytes(s)); return encode_rlp(hex2bytes(s));
} }
std::string rlp_encoder::encode_length(int len, int offset) std::string rlp_encoder::encode_length(int len, int offset) {
{ if (len < 56) {
if(len<56)
{
std::string temp; std::string temp;
temp=(char)(len+offset); temp = (char)(len + offset);
return temp; return temp;
} } else {
else
{
const std::string hexLength = to_hex(len); const std::string hexLength = to_hex(len);
const int lLength = hexLength.size()/2; const int lLength = hexLength.size() / 2;
const std::string fByte = to_hex(offset+55+lLength); const std::string fByte = to_hex(offset + 55 + lLength);
return hex2bytes(fByte+hexLength); return hex2bytes(fByte + hexLength);
} }
} }
std::string rlp_encoder::hex2bytes(const std::string& s) std::string rlp_encoder::hex2bytes(const std::string &s) {
{
std::string dest; std::string dest;
dest.resize(s.size()/2); dest.resize(s.size() / 2);
hex2bin(s.c_str(), &dest[0]); hex2bin(s.c_str(), &dest[0]);
return dest; return dest;
} }
std::string rlp_encoder::encode_rlp(const std::string& s) std::string rlp_encoder::encode_rlp(const std::string &s) {
{ if (s.size() == 1 && (unsigned char)s[0] < 128)
if(s.size()==1 && (unsigned char)s[0]<128)
return s; return s;
else else
return encode_length(s.size(), 128) + s; return encode_length(s.size(), 128) + s;
} }
int rlp_encoder::char2int(char input) int rlp_encoder::char2int(char input) {
{ if (input >= '0' && input <= '9')
if(input >= '0' && input <= '9')
return input - '0'; return input - '0';
if(input >= 'A' && input <= 'F') if (input >= 'A' && input <= 'F')
return input - 'A' + 10; return input - 'A' + 10;
if(input >= 'a' && input <= 'f') if (input >= 'a' && input <= 'f')
return input - 'a' + 10; return input - 'a' + 10;
return -1; return -1;
} }
void rlp_encoder::hex2bin(const char* src, char* target) void rlp_encoder::hex2bin(const char *src, char *target) {
{ while (*src && src[1]) {
while(*src && src[1]) *(target++) = char2int(*src) * 16 + char2int(src[1]);
{
*(target++) = char2int(*src)*16 + char2int(src[1]);
src += 2; src += 2;
} }
} }

View file

@ -10,14 +10,13 @@
#include <fc/crypto/elliptic.hpp> #include <fc/crypto/elliptic.hpp>
#include <fc/crypto/hex.hpp> #include <fc/crypto/hex.hpp>
#include <graphene/peerplays_sidechain/ethereum/encoders.hpp>
#include <graphene/peerplays_sidechain/ethereum/decoders.hpp> #include <graphene/peerplays_sidechain/ethereum/decoders.hpp>
#include <graphene/peerplays_sidechain/ethereum/encoders.hpp>
#include <graphene/peerplays_sidechain/ethereum/types.hpp> #include <graphene/peerplays_sidechain/ethereum/types.hpp>
#include <graphene/peerplays_sidechain/ethereum/utils.hpp> #include <graphene/peerplays_sidechain/ethereum/utils.hpp>
namespace graphene { namespace peerplays_sidechain { namespace ethereum { namespace graphene { namespace peerplays_sidechain { namespace ethereum {
const secp256k1_context *eth_context() { const secp256k1_context *eth_context() {
static secp256k1_context *ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN); static secp256k1_context *ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN);
return ctx; return ctx;
@ -25,13 +24,11 @@ const secp256k1_context *eth_context() {
//! transaction //! transaction
const transaction& transaction::sign(const std::string& private_key) const const transaction &transaction::sign(const std::string &private_key) const {
{
return *this; return *this;
} }
std::string transaction::serialize() const std::string transaction::serialize() const {
{
boost::property_tree::ptree pt; boost::property_tree::ptree pt;
pt.put("from", from); pt.put("from", from);
pt.put("to", to); pt.put("to", to);
@ -42,24 +39,22 @@ std::string transaction::serialize() const
return ss.str(); 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); std::stringstream ss_tx(raw_tx);
boost::property_tree::ptree tx_json; boost::property_tree::ptree tx_json;
boost::property_tree::read_json(ss_tx, 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<std::string>("from"); from = tx_json.get<std::string>("from");
if(tx_json.count("to")) if (tx_json.count("to"))
to = tx_json.get<std::string>("to"); to = tx_json.get<std::string>("to");
if(tx_json.count("data")) if (tx_json.count("data"))
data = tx_json.get<std::string>("data"); data = tx_json.get<std::string>("data");
} }
//! raw_transaction //! 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 //! Prepare signed transaction
signed_transaction tr; signed_transaction tr;
tr.nonce = nonce; tr.nonce = nonce;
@ -72,8 +67,8 @@ signed_transaction raw_transaction::sign(const std::string& private_key) const
//! Calculate keccak hash of transaction //! Calculate keccak hash of transaction
bytes hash; bytes hash;
hash.resize(32); hash.resize(32);
const auto transaction_string = boost::algorithm::unhex( remove_0x( serialize() ) ); 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()); keccak_256((const unsigned char *)transaction_string.data(), transaction_string.size(), (unsigned char *)hash.data());
const bytes priv_key = parse_hex(private_key); 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<int>(chain_id) * 2 + 35)}; bytes v = bytes{char(recid + from_hex<int>(chain_id) * 2 + 35)};
bytes r; bytes r;
for(int i = 1; i < 33; i++) for (int i = 1; i < 33; i++)
r.emplace_back((char) result.at(i)); r.emplace_back((char)result.at(i));
bytes s; bytes s;
for(int i = 33; i < 65; i++) for (int i = 33; i < 65; i++)
s.emplace_back((char) result.at(i)); s.emplace_back((char)result.at(i));
tr.v = fc::to_hex((char *)&v[0], v.size()); tr.v = fc::to_hex((char *)&v[0], v.size());
tr.r = fc::to_hex((char *)&r[0], r.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; return tr;
} }
std::string raw_transaction::serialize() const std::string raw_transaction::serialize() const {
{
rlp_encoder encoder; rlp_encoder encoder;
const std::string serialized = encoder.encode(remove_0x(nonce)) + const std::string serialized = encoder.encode(remove_0x(nonce)) +
encoder.encode(remove_0x(gas_price)) + encoder.encode(remove_0x(gas_price)) +
@ -111,11 +105,10 @@ std::string raw_transaction::serialize() const
encoder.encode("") + encoder.encode("") +
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; rlp_decoder decoder;
const auto rlp_array = decoder.decode(remove_0x(raw_tx)); const auto rlp_array = decoder.decode(remove_0x(raw_tx));
FC_ASSERT(rlp_array.size() >= 7, "Wrong rlp format"); FC_ASSERT(rlp_array.size() >= 7, "Wrong rlp format");
@ -131,8 +124,7 @@ void raw_transaction::deserialize(const std::string& raw_tx)
//! signed_transaction //! signed_transaction
std::string signed_transaction::serialize() const std::string signed_transaction::serialize() const {
{
rlp_encoder encoder; rlp_encoder encoder;
const std::string serialized = encoder.encode(remove_0x(nonce)) + const std::string serialized = encoder.encode(remove_0x(nonce)) +
encoder.encode(remove_0x(gas_price)) + 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(r)) +
encoder.encode(remove_0x(s)); 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; rlp_decoder decoder;
const auto rlp_array = decoder.decode(remove_0x(raw_tx)); const auto rlp_array = decoder.decode(remove_0x(raw_tx));
FC_ASSERT(rlp_array.size() >= 9, "Wrong rlp format"); FC_ASSERT(rlp_array.size() >= 9, "Wrong rlp format");

View file

@ -10,23 +10,20 @@ bytes parse_hex(const std::string &str) {
return vec; return vec;
} }
std::string bytes2hex(const std::string& s) std::string bytes2hex(const std::string &s) {
{
std::string dest; std::string dest;
for( const auto& i : s ) for (const auto &i : s)
dest += uchar2Hex((unsigned char)i); dest += uchar2Hex((unsigned char)i);
return dest; return dest;
} }
std::string uchar2Hex(unsigned char n) std::string uchar2Hex(unsigned char n) {
{
std::string dest; std::string dest;
dest.resize(2); dest.resize(2);
sprintf(&dest[0], "%X", n); sprintf(&dest[0], "%X", n);
if(n < (unsigned char)16) if (n < (unsigned char)16) {
{
dest[1] = dest[0]; dest[1] = dest[0];
dest[0] = '0'; dest[0] = '0';
} }
@ -34,9 +31,8 @@ std::string uchar2Hex(unsigned char n)
return dest; return dest;
} }
std::string add_0x(const std::string& s) std::string add_0x(const std::string &s) {
{ if (s.size() > 1) {
if(s.size() > 1) {
if (s.substr(0, 2) == "0x") if (s.substr(0, 2) == "0x")
return s; return s;
} }
@ -44,9 +40,8 @@ std::string add_0x(const std::string& s)
return "0x" + s; return "0x" + s;
} }
std::string remove_0x(const std::string& s) std::string remove_0x(const std::string &s) {
{ if (s.size() > 1) {
if(s.size() > 1) {
if (s.substr(0, 2) == "0x") if (s.substr(0, 2) == "0x")
return s.substr(2); return s.substr(2);
} }

View file

@ -7,22 +7,21 @@ namespace graphene { namespace peerplays_sidechain { namespace ethereum {
class rlp_decoder { class rlp_decoder {
private: private:
enum RLP_constants enum RLP_constants {
{ RLP_maxUintLen = 8,
RLP_maxUintLen = 8, RLP_bufferLenStart = 0x80,
RLP_bufferLenStart = 0x80, RLP_listStart = 0xc0,
RLP_listStart = 0xc0,
}; };
public: public:
static std::vector<std::string> decode(const std::string& str); static std::vector<std::string> decode(const std::string &str);
private: private:
static std::vector<std::string> decode_rlp(const unsigned char *raw, size_t len, size_t& consumed); static std::vector<std::string> decode_rlp(const unsigned char *raw, size_t len, size_t &consumed);
static std::vector<std::string> decode_array(const unsigned char *raw, size_t len, size_t uintlen, size_t payloadlen); static std::vector<std::string> 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 uint64_t to_int(const unsigned char *raw, size_t len);
static std::vector<unsigned char> parse_hex(const std::string& str); static std::vector<unsigned char> parse_hex(const std::string &str);
static std::vector<unsigned char> parse_hex(const char* psz); static std::vector<unsigned char> parse_hex(const char *psz);
static signed char hex_digit(char c); static signed char hex_digit(char c);
}; };

View file

@ -1,42 +1,42 @@
#pragma once #pragma once
#include <boost/multiprecision/cpp_int.hpp>
#include <string> #include <string>
#include <vector> #include <vector>
#include <boost/multiprecision/cpp_int.hpp>
namespace graphene { namespace peerplays_sidechain { namespace ethereum { namespace graphene { namespace peerplays_sidechain { namespace ethereum {
class base_encoder { class base_encoder {
public: public:
static std::string encode_uint256(boost::multiprecision::uint256_t value); static std::string encode_uint256(boost::multiprecision::uint256_t value);
static std::string encode_address(const std::string& value); static std::string encode_address(const std::string &value);
static std::string encode_string(const std::string& value); static std::string encode_string(const std::string &value);
}; };
class update_owners_encoder { class update_owners_encoder {
public: public:
const std::string function_signature = "23ab6adf"; //! updateOwners((address,uint256)[],string) const std::string function_signature = "23ab6adf"; //! updateOwners((address,uint256)[],string)
std::string encode(const std::vector<std::pair<std::string, uint16_t>>& owners_weights, const std::string& object_id) const; std::string encode(const std::vector<std::pair<std::string, uint16_t>> &owners_weights, const std::string &object_id) const;
}; };
class withdrawal_encoder { class withdrawal_encoder {
public: public:
const std::string function_signature = "e088747b"; //! withdraw(address,uint256,string) 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 { class rlp_encoder {
public: 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 encode_length(int len, int offset);
static std::string hex2bytes(const std::string& s); static std::string hex2bytes(const std::string &s);
private: 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 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 { /*class ethereum_function_call_encoder {

View file

@ -6,28 +6,25 @@
namespace graphene { namespace peerplays_sidechain { namespace ethereum { namespace graphene { namespace peerplays_sidechain { namespace ethereum {
class base_transaction class base_transaction {
{
virtual std::string serialize() const = 0; 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: public:
std::string from; std::string from;
std::string to; std::string to;
std::string data; 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 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 signed_transaction;
class raw_transaction : base_transaction class raw_transaction : base_transaction {
{
public: public:
std::string nonce; std::string nonce;
std::string gas_price; std::string gas_price;
@ -37,14 +34,13 @@ public:
std::string data; std::string data;
std::string chain_id; 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 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: public:
std::string nonce; std::string nonce;
std::string gas_price; std::string gas_price;
@ -57,7 +53,7 @@ public:
std::string s; std::string s;
virtual std::string serialize() const override; 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 }}} // namespace graphene::peerplays_sidechain::ethereum

View file

@ -6,28 +6,26 @@ namespace graphene { namespace peerplays_sidechain { namespace ethereum {
bytes parse_hex(const std::string &str); 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 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<typename T> template <typename T>
std::string to_hex( const T& val ) std::string to_hex(const T &val) {
{
std::stringstream stream; std::stringstream stream;
stream << std::hex << val; stream << std::hex << val;
std::string result( stream.str() ); std::string result(stream.str());
if(result.size() % 2) if (result.size() % 2)
result = "0"+result; result = "0" + result;
return result; return result;
} }
template<typename T> template <typename T>
T from_hex( const std::string& s ) T from_hex(const std::string &s) {
{
T val; T val;
std::stringstream stream; std::stringstream stream;
stream << std::hex << s; stream << std::hex << s;

View file

@ -19,18 +19,18 @@ public:
std::string eth_get_block_by_number(std::string block_number, bool full_block); 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 eth_get_logs(std::string wallet_contract_address);
std::string net_version(); std::string net_version();
std::string eth_get_transaction_count(const std::string& params); std::string eth_get_transaction_count(const std::string &params);
std::string eth_gas_price(); std::string eth_gas_price();
std::string get_chain_id(); std::string get_chain_id();
std::string get_network_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_price();
std::string get_gas_limit(); std::string get_gas_limit();
std::string eth_send_transaction(const std::string& params); std::string eth_send_transaction(const std::string &params);
std::string eth_send_raw_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_get_transaction_receipt(const std::string &params);
}; };
class sidechain_net_handler_ethereum : public sidechain_net_handler { class sidechain_net_handler_ethereum : public sidechain_net_handler {
@ -58,7 +58,7 @@ private:
ethereum::chain_id_type chain_id; ethereum::chain_id_type chain_id;
ethereum::network_id_type network_id; ethereum::network_id_type network_id;
std::string create_primary_wallet_transaction(const std::vector<son_info> &son_pubkeys, const std::string& object_id); std::string create_primary_wallet_transaction(const std::vector<son_info> &son_pubkeys, const std::string &object_id);
std::string create_deposit_transaction(const son_wallet_deposit_object &swdo); std::string create_deposit_transaction(const son_wallet_deposit_object &swdo);
std::string create_withdrawal_transaction(const son_wallet_withdraw_object &swwo); std::string create_withdrawal_transaction(const son_wallet_withdraw_object &swwo);

View file

@ -47,7 +47,7 @@ std::string ethereum_rpc_client::net_version() {
return send_post_request("net_version", "", debug_rpc_calls); 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 &params) {
return send_post_request("eth_getTransactionCount", params, debug_rpc_calls); 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"); 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\"]"); std::string reply_str = eth_get_transaction_count("[\"" + address + "\", \"latest\"]");
return retrieve_value_from_reply(reply_str, ""); return retrieve_value_from_reply(reply_str, "");
} }
@ -89,15 +89,15 @@ std::string ethereum_rpc_client::get_gas_limit() {
return std::string{}; 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 &params) {
return send_post_request("eth_sendTransaction", "[" + params + "]", debug_rpc_calls); 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 &params) {
return send_post_request("eth_sendRawTransaction", "[ \"" + params + "\" ]", debug_rpc_calls); 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 &params) {
return send_post_request("eth_getTransactionReceipt", "[\"" + params + "\"]", debug_rpc_calls); 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) { std::string sidechain_net_handler_ethereum::send_sidechain_transaction(const sidechain_transaction_object &sto) {
boost::property_tree::ptree pt; boost::property_tree::ptree pt;
boost::property_tree::ptree pt_array; boost::property_tree::ptree pt_array;
for(const auto& signature : sto.signatures) { for (const auto &signature : sto.signatures) {
const auto& transaction = signature.second; const auto &transaction = signature.second;
#ifdef SEND_RAW_TRANSACTION #ifdef SEND_RAW_TRANSACTION
const std::string sidechain_transaction = rpc_client->eth_send_raw_transaction(transaction); const std::string sidechain_transaction = rpc_client->eth_send_raw_transaction(transaction);
#else #else
const std::string sidechain_transaction = rpc_client->eth_send_transaction(transaction); const std::string sidechain_transaction = rpc_client->eth_send_transaction(transaction);
#endif #endif
std::stringstream ss_tx(sidechain_transaction); std::stringstream ss_tx(sidechain_transaction);
boost::property_tree::ptree tx_json; boost::property_tree::ptree tx_json;
boost::property_tree::read_json(ss_tx, 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; boost::property_tree::ptree node;
node.put("transaction", transaction); node.put("transaction", transaction);
node.put("transaction_receipt", tx_json.get<std::string>("result")); node.put("transaction_receipt", tx_json.get<std::string>("result"));
pt_array.push_back(std::make_pair("", node)); pt_array.push_back(std::make_pair("", node));
} } else {
else {
//! Fixme //! Fixme
//! How should we proceed with error in eth_send_transaction //! 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); 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::ptree json;
boost::property_tree::read_json(ss, json); boost::property_tree::read_json(ss, json);
if( !json.count("result_array") ) { if (!json.count("result_array")) {
return false; return false;
} }
size_t count = 0; size_t count = 0;
for(const auto &entry : json.get_child("result_array")) { for (const auto &entry : json.get_child("result_array")) {
const std::string receipt = rpc_client->eth_get_transaction_receipt( entry.second.get<std::string>("transaction_receipt") ); const std::string receipt = rpc_client->eth_get_transaction_receipt(entry.second.get<std::string>("transaction_receipt"));
std::stringstream ss_receipt(receipt); std::stringstream ss_receipt(receipt);
boost::property_tree::ptree json_receipt; boost::property_tree::ptree json_receipt;
boost::property_tree::read_json(ss_receipt, json_receipt); boost::property_tree::read_json(ss_receipt, json_receipt);
if( json_receipt.get<std::string>("result") == "null" ) { if (json_receipt.get<std::string>("result") == "null") {
wlog("Block is not minted yet for transaction ${id}", ("id", sto.id)); wlog("Block is not minted yet for transaction ${id}", ("id", sto.id));
return false; return false;
} }
if( "0x1" == json_receipt.get<std::string>("result.status") ) if ("0x1" == json_receipt.get<std::string>("result.status")) {
{
count += 1; count += 1;
//! Fixme - compare data somehow? //! Fixme - compare data somehow?
//if( sto.transaction == entry_receipt.second.get<std::string>("data") ) { //if( sto.transaction == entry_receipt.second.get<std::string>("data") ) {
@ -603,17 +601,16 @@ bool sidechain_net_handler_ethereum::settle_sidechain_transaction(const sidechai
} }
//! Check that we have all transactions //! 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)); wlog("Not all receipts received for transaction ${id}", ("id", sto.id));
return false; return false;
} } else
else
return true; return true;
return false; return false;
} }
std::string sidechain_net_handler_ethereum::create_primary_wallet_transaction(const std::vector<son_info> &son_pubkeys, const std::string& object_id) { std::string sidechain_net_handler_ethereum::create_primary_wallet_transaction(const std::vector<son_info> &son_pubkeys, const std::string &object_id) {
std::vector<std::pair<std::string, uint16_t>> owners_weights; std::vector<std::pair<std::string, uint16_t>> owners_weights;
for (auto &son : son_pubkeys) { 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)); 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) { std::string sidechain_net_handler_ethereum::create_withdrawal_transaction(const son_wallet_withdraw_object &swwo) {
ethereum::withdrawal_encoder encoder; 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) { std::string sidechain_net_handler_ethereum::sign_transaction(const sidechain_transaction_object &sto) {
const auto& current_son = plugin.get_current_son_object(); const auto &current_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)); 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 #ifdef SEND_RAW_TRANSACTION
ethereum::raw_transaction raw_tr; ethereum::raw_transaction raw_tr;
raw_tr.nonce = ethereum::add_0x( ethereum::to_hex( ethereum::from_hex<boost::multiprecision::uint256_t>( rpc_client->get_nonce( ethereum::add_0x(public_key) ) ) ) ); raw_tr.nonce = ethereum::add_0x(ethereum::to_hex(ethereum::from_hex<boost::multiprecision::uint256_t>(rpc_client->get_nonce(ethereum::add_0x(public_key)))));
raw_tr.gas_price = rpc_client->get_gas_price(); raw_tr.gas_price = rpc_client->get_gas_price();
raw_tr.gas_limit = rpc_client->get_gas_limit(); raw_tr.gas_limit = rpc_client->get_gas_limit();
raw_tr.to = wallet_contract_address; raw_tr.to = wallet_contract_address;
raw_tr.value = ""; raw_tr.value = "";
raw_tr.data = sto.transaction; raw_tr.data = sto.transaction;
raw_tr.chain_id = ethereum::add_0x(ethereum::to_hex(chain_id)); raw_tr.chain_id = ethereum::add_0x(ethereum::to_hex(chain_id));
const auto sign_tr = raw_tr.sign(get_private_key(public_key)); const auto sign_tr = raw_tr.sign(get_private_key(public_key));
return sign_tr.serialize(); return sign_tr.serialize();
#else #else
ethereum::transaction sign_transaction; ethereum::transaction sign_transaction;
sign_transaction.data = sto.transaction; sign_transaction.data = sto.transaction;
sign_transaction.to = wallet_contract_address; sign_transaction.to = wallet_contract_address;
sign_transaction.from = "0x" + public_key; sign_transaction.from = "0x" + public_key;
return sign_transaction.sign(get_private_key(public_key)).serialize(); return sign_transaction.sign(get_private_key(public_key)).serialize();
#endif #endif
} }
void sidechain_net_handler_ethereum::schedule_ethereum_listener() { void sidechain_net_handler_ethereum::schedule_ethereum_listener() {