Merge branch 'feature/son-for-ethereum' into bug/issue416_corrected

This commit is contained in:
serkixenos 2022-08-29 23:25:32 +02:00
commit 27c8126b3f
13 changed files with 197 additions and 258 deletions

View file

@ -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

View file

@ -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
```

View file

@ -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<std::string> rlp_decoder::decode(const std::string& str)
{
std::vector<std::string> rlp_decoder::decode(const std::string &str) {
size_t consumed = 0;
const auto raw_vec = parse_hex(str);
const std::vector<std::string> rlp_array = decode_rlp(raw_vec.data(), raw_vec.size(), consumed);
std::vector<std::string> 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<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;
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<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();
}
// 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<std::string> rlp_decoder::decode_rlp(const unsigned char *raw, size_
// parsing done; assign data buffer value.
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;
}
// 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<std::string> 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<std::string>{};
// 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;
const unsigned char *tok_end = tok_start + slen;
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;
}
// 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<std::string> 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<std::string> 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<std::string>{};
// read list length
@ -169,8 +159,7 @@ std::vector<std::string> rlp_decoder::decode_rlp(const unsigned char *raw, size_
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;
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;
// 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<std::string> 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<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());
}
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
std::vector<unsigned char> vch;
while (true)
{
while (true) {
while (isspace(*psz))
psz++;
signed char c = hex_digit(*psz++);
@ -233,8 +217,7 @@ std::vector<unsigned char> 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];
}

View file

@ -1,41 +1,35 @@
#include <graphene/peerplays_sidechain/ethereum/encoders.hpp>
#include <stdlib.h>
#include <boost/algorithm/hex.hpp>
#include <boost/format.hpp>
#include <stdlib.h>
#include <graphene/peerplays_sidechain/ethereum/utils.hpp>
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<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;
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<std::pair<std::strin
}
//! 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;
data += base_encoder::encode_address(to);
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);
return data;
}
//! 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));
}
std::string rlp_encoder::encode_length(int len, int offset)
{
if(len<56)
{
std::string rlp_encoder::encode_length(int len, int offset) {
if (len < 56) {
std::string temp;
temp=(char)(len+offset);
temp = (char)(len + offset);
return temp;
}
else
{
} else {
const std::string hexLength = to_hex(len);
const int lLength = hexLength.size()/2;
const std::string fByte = to_hex(offset+55+lLength);
return hex2bytes(fByte+hexLength);
const int lLength = hexLength.size() / 2;
const std::string fByte = to_hex(offset + 55 + lLength);
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;
dest.resize(s.size()/2);
dest.resize(s.size() / 2);
hex2bin(s.c_str(), &dest[0]);
return dest;
}
std::string rlp_encoder::encode_rlp(const std::string& s)
{
if(s.size()==1 && (unsigned char)s[0]<128)
std::string rlp_encoder::encode_rlp(const std::string &s) {
if (s.size() == 1 && (unsigned char)s[0] < 128)
return s;
else
return encode_length(s.size(), 128) + s;
}
int rlp_encoder::char2int(char input)
{
if(input >= '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;
}
}

View file

@ -10,14 +10,13 @@
#include <fc/crypto/elliptic.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/encoders.hpp>
#include <graphene/peerplays_sidechain/ethereum/types.hpp>
#include <graphene/peerplays_sidechain/ethereum/utils.hpp>
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<std::string>("from");
if(tx_json.count("to"))
if (tx_json.count("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");
}
//! 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<int>(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");

View file

@ -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);
}

View file

@ -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<std::string> decode(const std::string& str);
static std::vector<std::string> decode(const std::string &str);
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 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 char* psz);
static std::vector<unsigned char> parse_hex(const std::string &str);
static std::vector<unsigned char> parse_hex(const char *psz);
static signed char hex_digit(char c);
};

View file

@ -1,42 +1,42 @@
#pragma once
#include <boost/multiprecision/cpp_int.hpp>
#include <string>
#include <vector>
#include <boost/multiprecision/cpp_int.hpp>
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<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 {
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 {

View file

@ -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

View file

@ -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<typename T>
std::string to_hex( const T& val )
{
template <typename T>
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<typename T>
T from_hex( const std::string& s )
{
template <typename T>
T from_hex(const std::string &s) {
T val;
std::stringstream stream;
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_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 &params);
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 &params);
std::string eth_send_raw_transaction(const std::string &params);
std::string eth_get_transaction_receipt(const std::string &params);
};
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_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_withdrawal_transaction(const son_wallet_withdraw_object &swwo);

View file

@ -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<bool>();
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() {

View file

@ -20,7 +20,7 @@
#include <graphene/peerplays_sidechain/ethereum/transaction.hpp>
#include <graphene/peerplays_sidechain/ethereum/utils.hpp>
//#define SEND_RAW_TRANSACTION 1
#define SEND_RAW_TRANSACTION 1
namespace graphene { namespace peerplays_sidechain {
@ -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 &params) {
return send_post_request("eth_getTransactionCount", params, debug_rpc_calls);
}
@ -65,9 +65,10 @@ 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, "");
const auto nonce_val = ethereum::from_hex<boost::multiprecision::uint256_t>(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() {
@ -89,15 +90,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 &params) {
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);
}
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);
}
@ -541,27 +542,32 @@ 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;
//! 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
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<std::string>("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));
return std::string{}; //! Return empty string, as we have error in sending
}
}
pt.add_child("result_array", pt_array);
@ -576,25 +582,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<std::string>("transaction_receipt") );
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"));
std::stringstream ss_receipt(receipt);
boost::property_tree::ptree 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));
return false;
}
if( "0x1" == json_receipt.get<std::string>("result.status") )
{
if ("0x1" == json_receipt.get<std::string>("result.status")) {
count += 1;
//! Fixme - compare data somehow?
//if( sto.transaction == entry_receipt.second.get<std::string>("data") ) {
@ -603,17 +608,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_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;
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 +635,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 &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));
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::to_hex( ethereum::from_hex<boost::multiprecision::uint256_t>( rpc_client->get_nonce( ethereum::add_0x(public_key) ) ) + 1 );
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 = 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() {