Code formatting

This commit is contained in:
serkixenos 2022-06-28 20:39:00 +02:00
parent 7689d5adc0
commit cfaf31e705
5 changed files with 887 additions and 890 deletions

View file

@ -57,7 +57,7 @@ typedef fc::static_variant<
delegate_vesting_shares_operation>
ethereum_operation;
}}} // namespace graphene::peerplays_sidechain::hive
}}} // namespace graphene::peerplays_sidechain::ethereum
namespace fc {

View file

@ -11,7 +11,7 @@
#include <graphene/peerplays_sidechain/ethereum/operations.hpp>
namespace graphene { namespace peerplays_sidechain { namespace ethereum {
typedef fc::ecc::private_key private_key_type;
typedef fc::ecc::private_key private_key_type;
typedef fc::sha256 chain_id_type;
class transaction {
@ -29,7 +29,7 @@ public:
void set_expiration(fc::time_point_sec expiration_time);
void set_reference_block(const block_id_type &reference_block);
/*
/*
/// Serialises this transaction to an RLPStream.
/// @throws TransactionIsUnsigned if including signature was requested but it was not initialized
void streamRLP(RLPStream& _s, IncludeSignature _sig = WithSignature, bool _forEip155hash = false) const;
@ -76,7 +76,7 @@ public:
void clear();
};
}}} // namespace graphene::peerplays_sidechain::ethereum
}}} // namespace graphene::peerplays_sidechain::ethereum
FC_REFLECT(graphene::peerplays_sidechain::ethereum::transaction,
(ref_block_num)(ref_block_prefix)(expiration)(operations)(extensions))

View file

@ -14,257 +14,256 @@
#include <fc/api.hpp>
#include <fc/log/logger.hpp>
#include <websocketpp/config/asio_client.hpp>
#include <websocketpp/client.hpp>
#include <websocketpp/config/asio_client.hpp>
#include <fc/thread/thread.hpp>
#include <boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision::literals;
using u256 = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>>;
using s256 = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<256, 256, boost::multiprecision::signed_magnitude, boost::multiprecision::unchecked, void>>;
using u256 = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>>;
using s256 = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<256, 256, boost::multiprecision::signed_magnitude, boost::multiprecision::unchecked, void>>;
u256 constexpr Invalid256 = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_cppui256;
using byte = uint8_t;
using bytes = std::vector<byte>;
namespace graphene { namespace peerplays_sidechain {
typedef websocketpp::client<websocketpp::config::asio_client> client;
typedef websocketpp::client<websocketpp::config::asio_client> client;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
using websocketpp::lib::bind;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
// pull out the type of messages sent by our config
typedef websocketpp::config::asio_tls_client::message_type::ptr message_ptr;
typedef websocketpp::lib::shared_ptr<boost::asio::ssl::context> context_ptr;
typedef client::connection_ptr connection_ptr;
// pull out the type of messages sent by our config
typedef websocketpp::config::asio_tls_client::message_type::ptr message_ptr;
typedef websocketpp::lib::shared_ptr<boost::asio::ssl::context> context_ptr;
typedef client::connection_ptr connection_ptr;
class eth_rpc_client {
public:
typedef eth_rpc_client type;
class eth_rpc_client {
public:
typedef eth_rpc_client type;
enum req_t {
ETH_CHAIN_ID,
ETH_GET_TRANSACTION_RECEIPT,
ETH_CALL,
ETH_SEND_TRANSACTION,
ETH_SEND_RAW_TRANSACTION,
ETH_GET_CODE,
ETH_GET_BALANCE,
ETH_SIGN,
ETH_COINBASE,
GET_LIST_OWNERS,
ADD_OWNER,
REMOVE_OWNER
};
enum req_t {
ETH_CHAIN_ID,
ETH_GET_TRANSACTION_RECEIPT,
ETH_CALL,
ETH_SEND_TRANSACTION,
ETH_SEND_RAW_TRANSACTION,
ETH_GET_CODE,
ETH_GET_BALANCE,
ETH_SIGN,
ETH_COINBASE,
GET_LIST_OWNERS,
ADD_OWNER,
REMOVE_OWNER
};
enum class multi_type {
script,
address
};
struct multi_params {
multi_params(multi_type _type, const std::string &_address_or_script, const std::string &_label = "") :
type{_type},
address_or_script{_address_or_script},
label{_label} {
}
enum class multi_type {
script,
address
};
struct multi_params {
multi_params(multi_type _type, const std::string &_address_or_script, const std::string &_label = "") :
type{_type},
address_or_script{_address_or_script},
label{_label} {
}
multi_type type;
std::string address_or_script;
std::string label;
};
multi_type type;
std::string address_or_script;
std::string label;
};
eth_rpc_client(const std::string &url, const std::string &user_name, const std::string &password, bool debug_rpc_calls);
eth_rpc_client(const std::string &url, const std::string &user_name, const std::string &password, bool debug_rpc_calls);
void start();
void stop();
void on_socket_init(websocketpp::connection_hdl);
void on_fail(websocketpp::connection_hdl hdl);
void on_open(websocketpp::connection_hdl hdl);
void on_message(websocketpp::connection_hdl hdl, message_ptr msg);
void on_close(websocketpp::connection_hdl);
void start();
void stop();
void on_socket_init(websocketpp::connection_hdl);
void on_fail(websocketpp::connection_hdl hdl);
void on_open(websocketpp::connection_hdl hdl);
void on_message(websocketpp::connection_hdl hdl, message_ptr msg);
void on_close(websocketpp::connection_hdl);
uint64_t get_chain_id();
uint64_t eth_getTransactionReceipt(const std::string& tx_id);
uint64_t eth_call(const std::string& to, const std::string& data);
uint64_t eth_sendTransaction(const std::string& from, const std::string& to, const std::string& data);
uint64_t eth_sendRawTransaction(const std::string& params);
uint64_t eth_getCode(const std::string& addr);
uint64_t eth_getBalance(const std::string& addr);
uint64_t eth_sign(const string& addr, const string& message);
uint64_t eth_coinbase();
uint64_t get_chain_id();
uint64_t eth_getTransactionReceipt(const std::string &tx_id);
uint64_t eth_call(const std::string &to, const std::string &data);
uint64_t eth_sendTransaction(const std::string &from, const std::string &to, const std::string &data);
uint64_t eth_sendRawTransaction(const std::string &params);
uint64_t eth_getCode(const std::string &addr);
uint64_t eth_getBalance(const std::string &addr);
uint64_t eth_sign(const string &addr, const string &message);
uint64_t eth_coinbase();
uint64_t get_list_owners(const std::string& safe_account);
uint64_t add_owner(const std::string& addr );
uint64_t remove_owner(const std::string& addr, uint32_t threshold);
uint64_t get_list_owners(const std::string &safe_account);
uint64_t add_owner(const std::string &addr);
uint64_t remove_owner(const std::string &addr, uint32_t threshold);
std::string addmultisigaddress(const uint32_t nrequired, const std::vector<std::string> public_keys);
std::string combinepsbt(const vector<std::string> &psbts);
std::string addmultisigaddress(const uint32_t nrequired, const std::vector<std::string> public_keys);
std::string combinepsbt(const vector<std::string> &psbts);
uint64_t createmultisig(const uint32_t nrequired, const std::vector<std::string> owner_addresses, const std::string &private_key);
uint64_t createmultisig(const uint32_t nrequired, const std::vector<std::string> owner_addresses, const std::string &private_key);
std::string createpsbt();
std::string createrawtransaction();
std::string createwallet(const std::string &wallet_name);
std::string decodepsbt(std::string const &tx_psbt);
std::string decoderawtransaction(std::string const &tx_hex);
std::string encryptwallet(const std::string &passphrase);
uint64_t estimatesmartfee(uint16_t conf_target = 128);
std::string finalizepsbt(std::string const &tx_psbt);
std::string getaddressinfo(const std::string &address);
std::string getblock(const std::string &block_hash, int32_t verbosity = 2);
std::string getrawtransaction(const std::string &txid, const bool verbose = false);
std::string getnetworkinfo();
std::string gettransaction(const std::string &txid, const bool include_watch_only = false);
std::string getblockchaininfo();
void importaddress(const std::string &address_or_script, const std::string &label = "", const bool rescan = true, const bool p2sh = false);
void importmulti(const std::vector<multi_params> &address_or_script_array, const bool rescan = true);
std::string loadwallet(const std::string &filename);
std::string sendrawtransaction(const std::string &tx_hex);
std::string signrawtransactionwithwallet(const std::string &tx_hash);
std::string unloadwallet(const std::string &filename);
std::string walletlock();
std::string walletprocesspsbt(std::string const &tx_psbt);
bool walletpassphrase(const std::string &passphrase, uint32_t timeout = 60);
std::string createpsbt();
std::string createrawtransaction();
std::string createwallet(const std::string &wallet_name);
std::string decodepsbt(std::string const &tx_psbt);
std::string decoderawtransaction(std::string const &tx_hex);
std::string encryptwallet(const std::string &passphrase);
uint64_t estimatesmartfee(uint16_t conf_target = 128);
std::string finalizepsbt(std::string const &tx_psbt);
std::string getaddressinfo(const std::string &address);
std::string getblock(const std::string &block_hash, int32_t verbosity = 2);
std::string getrawtransaction(const std::string &txid, const bool verbose = false);
std::string getnetworkinfo();
std::string gettransaction(const std::string &txid, const bool include_watch_only = false);
std::string getblockchaininfo();
void importaddress(const std::string &address_or_script, const std::string &label = "", const bool rescan = true, const bool p2sh = false);
void importmulti(const std::vector<multi_params> &address_or_script_array, const bool rescan = true);
std::string loadwallet(const std::string &filename);
std::string sendrawtransaction(const std::string &tx_hex);
std::string signrawtransactionwithwallet(const std::string &tx_hash);
std::string unloadwallet(const std::string &filename);
std::string walletlock();
std::string walletprocesspsbt(std::string const &tx_psbt);
bool walletpassphrase(const std::string &passphrase, uint32_t timeout = 60);
std::string chain_id;//256 bit value
std::vector<std::string> owners;
std::string safe_account_addr;
private:
/// Encodes an Ethereum transaction for exporting in JSON
class Transaction
{
public:
/// JSON serialization
std::string serialize(const std::string& from, const std::string& to, const std::string& tx_id, u256 const& _value, u256 const& _gasPrice, u256 const& _gas, bytes const& _data, u256 const& _nonce = Invalid256);
protected:
/// Type of transaction.
enum Type
{
NullTransaction, ///< Null transaction.
ContractCreation, ///< Transaction to create contracts - receiveAddress() is ignored.
MessageCall ///< Transaction to invoke a message call - receiveAddress() is used.
};
std::string chain_id; //256 bit value
std::vector<std::string> owners;
std::string safe_account_addr;
Type m_type = NullTransaction; ///< Is this a contract-creation transaction or a message-call transaction?
u256 m_nonce; ///< The transaction-count of the sender.
u256 m_value; ///< The amount of ETH to be transferred by this transaction. Called 'endowment' for contract-creation transactions.
u256 m_gasPrice; ///< The base fee and thus the implied exchange rate of ETH to GAS.
u256 m_gas; ///< The total gas to convert, paid for from sender's account. Any unused gas gets refunded once the contract is ended.
bytes m_data; ///< The data associated with the transaction, or the initialiser if it's a creation transaction.
};
private:
/// Encodes an Ethereum transaction for exporting in JSON
class Transaction {
public:
/// JSON serialization
std::string serialize(const std::string &from, const std::string &to, const std::string &tx_id, u256 const &_value, u256 const &_gasPrice, u256 const &_gas, bytes const &_data, u256 const &_nonce = Invalid256);
class ethereum_function_call_encoder {
public:
enum operation_t {
OPERATION_CALL,
OPERATION_DELEGATE_CALL
};
protected:
/// Type of transaction.
enum Type {
NullTransaction, ///< Null transaction.
ContractCreation, ///< Transaction to create contracts - receiveAddress() is ignored.
MessageCall ///< Transaction to invoke a message call - receiveAddress() is used.
};
static constexpr const char*const default_prev_addr = "0000000000000000000000000000000000000001";
Type m_type = NullTransaction; ///< Is this a contract-creation transaction or a message-call transaction?
u256 m_nonce; ///< The transaction-count of the sender.
u256 m_value; ///< The amount of ETH to be transferred by this transaction. Called 'endowment' for contract-creation transactions.
u256 m_gasPrice; ///< The base fee and thus the implied exchange rate of ETH to GAS.
u256 m_gas; ///< The total gas to convert, paid for from sender's account. Any unused gas gets refunded once the contract is ended.
bytes m_data; ///< The data associated with the transaction, or the initialiser if it's a creation transaction.
};
std::string encode_function_signature(const std::string& function_signature);
std::string encode_address(const std::string& addr);
std::string encode_uint256(const std::string& value);
std::string encode_uint8(uint8_t value);
std::string encode_bytes(const std::string& values);
};
class ethereum_function_call_encoder {
public:
enum operation_t {
OPERATION_CALL,
OPERATION_DELEGATE_CALL
};
class safe_transaction_encoder {
public:
static constexpr const char*const default_safe_tx_gas = "0";
static constexpr const char*const default_data_gas = "0";
static constexpr const char*const default_gas_price = "0";
static constexpr const char*const default_gas_token = "0000000000000000000000000000000000000000";
static constexpr const char*const default_refund_receiver = "0000000000000000000000000000000000000000";
static constexpr const char *const default_prev_addr = "0000000000000000000000000000000000000001";
std::string create_safe_address(const std::vector<std::string>& owner_addresses, uint32_t threshold);
std::string build_transaction(const std::string& safe_account_addr, const std::string& value, const std::string& data, uint8_t operation, const std::string& safeTxGas, const std::string& dataGas, const std::string& gasPrice, const std::string& gasToken, const std::string& refundReceiver);
std::string encode_function_signature(const std::string &function_signature);
std::string encode_address(const std::string &addr);
std::string encode_uint256(const std::string &value);
std::string encode_uint8(uint8_t value);
std::string encode_bytes(const std::string &values);
};
private:
ethereum_function_call_encoder m_ethereum_function_call_encoder;
};
class safe_transaction_encoder {
public:
static constexpr const char *const default_safe_tx_gas = "0";
static constexpr const char *const default_data_gas = "0";
static constexpr const char *const default_gas_price = "0";
static constexpr const char *const default_gas_token = "0000000000000000000000000000000000000000";
static constexpr const char *const default_refund_receiver = "0000000000000000000000000000000000000000";
ethereum_function_call_encoder m_ethereum_function_call_encoder;
safe_transaction_encoder m_safe_transaction_encoder;
std::string create_safe_address(const std::vector<std::string> &owner_addresses, uint32_t threshold);
std::string build_transaction(const std::string &safe_account_addr, const std::string &value, const std::string &data, uint8_t operation, const std::string &safeTxGas, const std::string &dataGas, const std::string &gasPrice, const std::string &gasToken, const std::string &refundReceiver);
std::shared_ptr<fc::thread> _thread;
std::string signature;
std::string geth_url;
uint64_t t_id;
std::string account_address;
std::string transaction_id;
uint32_t threshold;
std::unordered_map<uint64_t, req_t> m_requests;
std::string balance;
std::string code;
private:
ethereum_function_call_encoder m_ethereum_function_call_encoder;
};
std::string ip;
uint32_t rpc_port;
std::string user;
std::string password;
std::string wallet;
std::string wallet_password;
bool debug_rpc_calls;
ethereum_function_call_encoder m_ethereum_function_call_encoder;
safe_transaction_encoder m_safe_transaction_encoder;
fc::http::header authorization;
std::shared_ptr<fc::thread> _thread;
std::string signature;
std::string geth_url;
uint64_t t_id;
std::string account_address;
std::string transaction_id;
uint32_t threshold;
std::unordered_map<uint64_t, req_t> m_requests;
std::string balance;
std::string code;
client m_endpoint;
websocketpp::connection_hdl m_hdl;
};
std::string ip;
uint32_t rpc_port;
std::string user;
std::string password;
std::string wallet;
std::string wallet_password;
bool debug_rpc_calls;
// =============================================================================
fc::http::header authorization;
class sidechain_net_handler_eth : public sidechain_net_handler {
public:
sidechain_net_handler_eth(peerplays_sidechain_plugin &_plugin, const boost::program_options::variables_map &options);
virtual ~sidechain_net_handler_eth();
client m_endpoint;
websocketpp::connection_hdl m_hdl;
};
bool process_proposal(const proposal_object &po);
void process_primary_wallet();
void process_sidechain_addresses();
bool process_deposit(const son_wallet_deposit_object &swdo);
bool process_withdrawal(const son_wallet_withdraw_object &swwo);
std::string process_sidechain_transaction(const sidechain_transaction_object &sto);
std::string send_sidechain_transaction(const sidechain_transaction_object &sto);
bool settle_sidechain_transaction(const sidechain_transaction_object &sto, asset &settle_amount);
// =============================================================================
private:
std::string url;
uint32_t rpc_port;
class sidechain_net_handler_ethereum : public sidechain_net_handler {
public:
sidechain_net_handler_ethereum(peerplays_sidechain_plugin &_plugin, const boost::program_options::variables_map &options);
virtual ~sidechain_net_handler_ethereum();
std::string rpc_user;
std::string rpc_password;
std::string wallet;
std::string wallet_password;
bool process_proposal(const proposal_object &po);
void process_primary_wallet();
void process_sidechain_addresses();
bool process_deposit(const son_wallet_deposit_object &swdo);
bool process_withdrawal(const son_wallet_withdraw_object &swwo);
std::string process_sidechain_transaction(const sidechain_transaction_object &sto);
std::string send_sidechain_transaction(const sidechain_transaction_object &sto);
bool settle_sidechain_transaction(const sidechain_transaction_object &sto, asset &settle_amount);
std::unique_ptr<eth_rpc_client> eth_client;
private:
std::string url;
uint32_t rpc_port;
fc::future<void> on_changed_objects_task;
std::string rpc_user;
std::string rpc_password;
std::string wallet;
std::string wallet_password;
std::mutex event_handler_mutex;
typedef std::lock_guard<decltype(event_handler_mutex)> scoped_lock;
std::unique_ptr<eth_rpc_client> eth_client;
std::string create_primary_wallet_address(const std::vector<son_info> &son_pubkeys);
fc::future<void> on_changed_objects_task;
std::string create_primary_wallet_transaction(const son_wallet_object &prev_swo, std::string new_sw_address);
std::string create_deposit_transaction(const son_wallet_deposit_object &swdo);
std::string create_withdrawal_transaction(const son_wallet_withdraw_object &swwo);
std::mutex event_handler_mutex;
typedef std::lock_guard<decltype(event_handler_mutex)> scoped_lock;
std::string create_transaction();
std::string sign_transaction(const sidechain_transaction_object &sto);
std::string send_transaction(const sidechain_transaction_object &sto);
std::string create_primary_wallet_address(const std::vector<son_info> &son_pubkeys);
void handle_event(const std::string &event_data);
std::vector<info_for_vin> extract_info_from_block(const std::string &_block);
void on_changed_objects(const vector<object_id_type> &ids, const flat_set<account_id_type> &accounts);
void on_changed_objects_cb(const vector<object_id_type> &ids, const flat_set<account_id_type> &accounts);
std::string create_primary_wallet_transaction(const son_wallet_object &prev_swo, std::string new_sw_address);
std::string create_deposit_transaction(const son_wallet_deposit_object &swdo);
std::string create_withdrawal_transaction(const son_wallet_withdraw_object &swwo);
std::vector<char> parse_hex(const std::string &str);
fc::ecc::public_key_data create_public_key_data(const std::vector<char> &public_key);
};
std::string create_transaction();
std::string sign_transaction(const sidechain_transaction_object &sto);
std::string send_transaction(const sidechain_transaction_object &sto);
void handle_event(const std::string &event_data);
std::vector<info_for_vin> extract_info_from_block(const std::string &_block);
void on_changed_objects(const vector<object_id_type> &ids, const flat_set<account_id_type> &accounts);
void on_changed_objects_cb(const vector<object_id_type> &ids, const flat_set<account_id_type> &accounts);
std::vector<char> parse_hex(const std::string &str);
fc::ecc::public_key_data create_public_key_data(const std::vector<char> &public_key);
};
}} // namespace graphene::peerplays_sidechain

View file

@ -222,8 +222,8 @@ void peerplays_sidechain_plugin_impl::plugin_initialize(const boost::program_opt
sidechain_enabled_ethereum = options.at("ethereum-sidechain-enabled").as<bool>();
config_ready_ethereum = options.count("ethereum-node-rpc-url");
//options.count("ethereum-address") &&
//options.count("ethereum-public-key") && options.count("ethereum-private-key");
//options.count("ethereum-address") &&
//options.count("ethereum-public-key") && options.count("ethereum-private-key");
if (!config_ready_ethereum) {
wlog("Haven't set up Ethereum sidechain parameters");
}