From 24c6963ef3cb4b8eaf1d3cf58ffbfccbab00c639 Mon Sep 17 00:00:00 2001 From: Alexander Suslikov Date: Thu, 10 Jan 2019 16:38:39 +0300 Subject: [PATCH] Added rpc and zmq modules for comminication with bitcoin node --- libraries/app/application.cpp | 5 + libraries/plugins/witness/CMakeLists.txt | 2 +- .../include/graphene/witness/witness.hpp | 2 + libraries/plugins/witness/witness.cpp | 13 ++ libraries/sidechain/CMakeLists.txt | 2 + libraries/sidechain/network/CMakeLists.txt | 7 + .../sidechain/network/bitcoin_rpc_client.cpp | 124 ++++++++++++++++++ .../sidechain/network/bitcoin_rpc_client.hpp | 41 ++++++ .../network/sidechain_net_manager.hpp | 42 ++++++ .../sidechain/network/zmq_listener.hpp | 38 ++++++ .../network/sidechain_net_manager.cpp | 63 +++++++++ libraries/sidechain/network/zmq_listener.cpp | 54 ++++++++ 12 files changed, 392 insertions(+), 1 deletion(-) create mode 100644 libraries/sidechain/network/CMakeLists.txt create mode 100644 libraries/sidechain/network/bitcoin_rpc_client.cpp create mode 100644 libraries/sidechain/network/include/sidechain/network/bitcoin_rpc_client.hpp create mode 100644 libraries/sidechain/network/include/sidechain/network/sidechain_net_manager.hpp create mode 100644 libraries/sidechain/network/include/sidechain/network/zmq_listener.hpp create mode 100644 libraries/sidechain/network/sidechain_net_manager.cpp create mode 100644 libraries/sidechain/network/zmq_listener.cpp diff --git a/libraries/app/application.cpp b/libraries/app/application.cpp index 5e4f9c7e..5993020a 100644 --- a/libraries/app/application.cpp +++ b/libraries/app/application.cpp @@ -963,6 +963,11 @@ void application::set_program_options(boost::program_options::options_descriptio ("genesis-json", bpo::value(), "File to read Genesis State from") ("dbg-init-key", bpo::value(), "Block signing key to use for init witnesses, overrides genesis file") ("api-access", bpo::value(), "JSON file specifying API permissions") + ("bitcoin-node-ip", bpo::value()->implicit_value("127.0.0.1"), "IP address of Bitcoin node") + ("bitcoin-node-zmq-port", bpo::value()->implicit_value(28332), "ZMQ port of Bitcoin node") + ("bitcoin-node-rpc-port", bpo::value()->implicit_value(18332), "RPC port of Bitcoin node") + ("bitcoin-node-rpc-user", bpo::value(), "Bitcoin RPC user") + ("bitcoin-node-rpc-password", bpo::value(), "Bitcoin RPC password") ; command_line_options.add(configuration_file_options); command_line_options.add_options() diff --git a/libraries/plugins/witness/CMakeLists.txt b/libraries/plugins/witness/CMakeLists.txt index 95759bbf..51d03ba7 100644 --- a/libraries/plugins/witness/CMakeLists.txt +++ b/libraries/plugins/witness/CMakeLists.txt @@ -4,7 +4,7 @@ add_library( graphene_witness witness.cpp ) -target_link_libraries( graphene_witness graphene_chain graphene_app ) +target_link_libraries( graphene_witness graphene_chain graphene_app graphene_time sidechain_network ) target_include_directories( graphene_witness PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" ) diff --git a/libraries/plugins/witness/include/graphene/witness/witness.hpp b/libraries/plugins/witness/include/graphene/witness/witness.hpp index e2f60bf8..d8152793 100644 --- a/libraries/plugins/witness/include/graphene/witness/witness.hpp +++ b/libraries/plugins/witness/include/graphene/witness/witness.hpp @@ -25,6 +25,7 @@ #include #include +#include #include @@ -82,6 +83,7 @@ private: bool _consecutive_production_enabled = false; uint32_t _required_witness_participation = 33 * GRAPHENE_1_PERCENT; uint32_t _production_skip_flags = graphene::chain::database::skip_nothing; + sidechain::sidechain_net_manager bitcoin_manager; std::map _private_keys; std::set _witnesses; diff --git a/libraries/plugins/witness/witness.cpp b/libraries/plugins/witness/witness.cpp index dce1234a..b4f08898 100644 --- a/libraries/plugins/witness/witness.cpp +++ b/libraries/plugins/witness/witness.cpp @@ -121,6 +121,19 @@ void witness_plugin::plugin_initialize(const boost::program_options::variables_m _private_keys[key_id_to_wif_pair.first] = *private_key; } } + if( options.count( "bitcoin-node-ip" ) && options.count( "bitcoin-node-zmq-port" ) && options.count( "bitcoin-node-rpc-port" ) + && options.count( "bitcoin-node-rpc-user" ) && options.count( "bitcoin-node-rpc-password" ) ) + { + const auto ip = options.at("bitcoin-node-ip").as(); + const auto zmq_port = options.at("bitcoin-node-zmq-port").as(); + const auto rpc_port = options.at("bitcoin-node-rpc-port").as(); + const auto rpc_user = options.at("bitcoin-node-rpc-user").as(); + const auto rpc_password = options.at("bitcoin-node-rpc-password").as(); + + bitcoin_manager.initialize_manager(&database(), ip, zmq_port, rpc_port, rpc_user, rpc_password); + } else { + wlog("Haven't set up sidechain parameters"); + } ilog("witness plugin: plugin_initialize() end"); } FC_LOG_AND_RETHROW() } diff --git a/libraries/sidechain/CMakeLists.txt b/libraries/sidechain/CMakeLists.txt index e12f598c..82de11e3 100644 --- a/libraries/sidechain/CMakeLists.txt +++ b/libraries/sidechain/CMakeLists.txt @@ -1,6 +1,8 @@ file( GLOB SOURCES "*.cpp" ) file( GLOB HEADERS "include/*.hpp" ) +add_subdirectory( network ) + add_library( sidechain STATIC ${SOURCES} ${HEADERS} ) target_link_libraries( sidechain ${Boost_LIBRARIES} fc graphene_chain ) diff --git a/libraries/sidechain/network/CMakeLists.txt b/libraries/sidechain/network/CMakeLists.txt new file mode 100644 index 00000000..c7e3ca91 --- /dev/null +++ b/libraries/sidechain/network/CMakeLists.txt @@ -0,0 +1,7 @@ +file( GLOB HEADERS "include/sidechain/network/*.hpp" ) +file( GLOB SOURCES "*.cpp" ) + +add_library( sidechain_network STATIC ${SOURCES} ${HEADERS} ) + +target_link_libraries( sidechain_network PUBLIC graphene_chain fc zmq ) +target_include_directories( sidechain_network PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" ) diff --git a/libraries/sidechain/network/bitcoin_rpc_client.cpp b/libraries/sidechain/network/bitcoin_rpc_client.cpp new file mode 100644 index 00000000..be050646 --- /dev/null +++ b/libraries/sidechain/network/bitcoin_rpc_client.cpp @@ -0,0 +1,124 @@ +#include + +#include + +#include +#include + +namespace sidechain { + +bitcoin_rpc_client::bitcoin_rpc_client( std::string _ip, uint32_t _rpc, std::string _user, std::string _password ): + ip( _ip ), rpc_port( _rpc ), user( _user ), password( _password ) +{ + authorization.key = "Authorization"; + authorization.val = "Basic " + fc::base64_encode( user + ":" + password ); +} + +std::string bitcoin_rpc_client::receive_full_block( const std::string& block_hash ) +{ + fc::http::connection conn; + conn.connect_to( fc::ip::endpoint( fc::ip::address( ip ), rpc_port ) ); + + const auto url = "http://" + ip + ":" + std::to_string( rpc_port ) + "/rest/block/" + block_hash + ".json"; + + const auto reply = conn.request( "GET", url ); + if ( reply.status != 200 ) + return ""; + + ilog( "Receive Bitcoin block: ${hash}", ( "hash", block_hash ) ); + return std::string( reply.body.begin(), reply.body.end() ); +} + +int32_t bitcoin_rpc_client::receive_confirmations_tx( const std::string& tx_hash ) +{ + const auto body = std::string("{\"jsonrpc\": \"1.0\", \"id\":\"curltest\", \"method\": \"getrawtransaction\", \"params\": [") + + std::string("\"") + tx_hash + std::string("\"") + ", " + "true" + std::string("] }"); + + const auto reply = send_post_request( body ); + + if ( reply.status != 200 ) + return -1; + + const auto result = std::string( reply.body.begin(), reply.body.end() ); + + std::stringstream ss( result ); + boost::property_tree::ptree tx; + boost::property_tree::read_json( ss, tx ); + if( tx.count( "result" ) ) { + if( tx.get_child( "result" ).count( "confirmations" ) ) { + return tx.get_child( "result" ).get_child( "confirmations" ).get_value(); + } + } + return 0; + +} + +bool bitcoin_rpc_client::receive_mempool_entry_tx( const std::string& tx_hash ) +{ + const auto body = std::string("{\"jsonrpc\": \"1.0\", \"id\":\"curltest\", \"method\": \"getmempoolentry\", \"params\": [") + + std::string("\"") + tx_hash + std::string("\"") + std::string("] }"); + + const auto reply = send_post_request( body ); + + if ( reply.status != 200 ) + return false; + + return true; +} + +uint64_t bitcoin_rpc_client::receive_estimated_fee() +{ + static const auto confirmation_target_blocks = 6; + + const auto body = std::string("{\"jsonrpc\": \"1.0\", \"id\":\"estimated_feerate\", \"method\": \"estimatesmartfee\", \"params\": [") + + std::to_string(confirmation_target_blocks) + std::string("] }"); + + const auto reply = send_post_request( body ); + + if( reply.status != 200 ) + return 0; + + std::stringstream ss( std::string( reply.body.begin(), reply.body.end() ) ); + boost::property_tree::ptree json; + boost::property_tree::read_json( ss, json ); + + if( json.count( "result" ) ) + if ( json.get_child( "result" ).count( "feerate" ) ) { + auto feerate_str = json.get_child( "result" ).get_child( "feerate" ).get_value(); + feerate_str.erase( std::remove( feerate_str.begin(), feerate_str.end(), '.' ), feerate_str.end() ); + return std::stoll( feerate_str ); + } + return 0; +} + +std::string bitcoin_rpc_client::send_btc_tx( const std::string& tx_hex ) +{ + const auto body = std::string("{\"jsonrpc\": \"1.0\", \"id\":\"send_tx\", \"method\": \"sendrawtransaction\", \"params\": [") + + std::string("\"") + tx_hex + std::string("\"") + std::string("] }"); + + const auto reply = send_post_request( body ); + + if( reply.body.empty() ) + return ""; + + std::string reply_str( reply.body.begin(), reply.body.end() ); + + return reply_str; +} + +bool bitcoin_rpc_client::connection_is_not_defined() const +{ + return ip.empty() || rpc_port == 0 || user.empty() || password.empty(); +} + +fc::http::reply bitcoin_rpc_client::send_post_request( std::string body ) +{ + fc::http::connection conn; + conn.connect_to( fc::ip::endpoint( fc::ip::address( ip ), rpc_port ) ); + + const auto url = "http://" + ip + ":" + std::to_string( rpc_port ); + + return conn.request( "POST", url, body, fc::http::headers{authorization} ); +} + +} \ No newline at end of file diff --git a/libraries/sidechain/network/include/sidechain/network/bitcoin_rpc_client.hpp b/libraries/sidechain/network/include/sidechain/network/bitcoin_rpc_client.hpp new file mode 100644 index 00000000..0fa18352 --- /dev/null +++ b/libraries/sidechain/network/include/sidechain/network/bitcoin_rpc_client.hpp @@ -0,0 +1,41 @@ +#pragma once + +#include +#include +#include + +namespace sidechain { + +class bitcoin_rpc_client +{ + +public: + + bitcoin_rpc_client( std::string _ip, uint32_t _rpc, std::string _user, std::string _password) ; + + std::string receive_full_block( const std::string& block_hash ); + + int32_t receive_confirmations_tx( const std::string& tx_hash ); + + bool receive_mempool_entry_tx( const std::string& tx_hash ); + + uint64_t receive_estimated_fee(); + + std::string send_btc_tx( const std::string& tx_hex ); + + bool connection_is_not_defined() const; + +private: + + fc::http::reply send_post_request( std::string body ); + + std::string ip; + uint32_t rpc_port; + std::string user; + std::string password; + + fc::http::header authorization; + +}; + +} diff --git a/libraries/sidechain/network/include/sidechain/network/sidechain_net_manager.hpp b/libraries/sidechain/network/include/sidechain/network/sidechain_net_manager.hpp new file mode 100644 index 00000000..befff470 --- /dev/null +++ b/libraries/sidechain/network/include/sidechain/network/sidechain_net_manager.hpp @@ -0,0 +1,42 @@ +#pragma once + +#include +#include +#include + +namespace sidechain { + +class sidechain_net_manager +{ + +public: + + sidechain_net_manager() {}; + + sidechain_net_manager( graphene::chain::database* _db, std::string _ip, + uint32_t _zmq, uint32_t _rpc, std::string _user, std::string _password ); + + void initialize_manager( graphene::chain::database* _db, std::string _ip, + uint32_t _zmq, uint32_t _rpc, std::string _user, std::string _password ); + + void update_tx_infos( const std::string& block_hash ); + + void update_tx_approvals(); + + void update_estimated_fee(); + + void send_btc_tx(); + + bool connection_is_not_defined() const; + +private: + + void handle_block( const std::string& block_hash ); + + std::unique_ptr listener; + std::unique_ptr bitcoin_client; + std::unique_ptr db; + +}; + +} diff --git a/libraries/sidechain/network/include/sidechain/network/zmq_listener.hpp b/libraries/sidechain/network/include/sidechain/network/zmq_listener.hpp new file mode 100644 index 00000000..33150b9d --- /dev/null +++ b/libraries/sidechain/network/include/sidechain/network/zmq_listener.hpp @@ -0,0 +1,38 @@ +#pragma once + +#include +#include + +#include +#include +#include + +#include + +namespace sidechain { + +class zmq_listener +{ + +public: + + zmq_listener( std::string _ip, uint32_t _zmq, uint32_t _rpc ); + + bool connection_is_not_defined() const { return zmq_port == 0; } + + fc::signal block_received; + +private: + + void handle_zmq(); + std::vector receive_multipart(); + + std::string ip; + uint32_t zmq_port; + uint32_t rpc_port; + + zmq::context_t ctx; + zmq::socket_t socket; +}; + +} diff --git a/libraries/sidechain/network/sidechain_net_manager.cpp b/libraries/sidechain/network/sidechain_net_manager.cpp new file mode 100644 index 00000000..1ecd20f6 --- /dev/null +++ b/libraries/sidechain/network/sidechain_net_manager.cpp @@ -0,0 +1,63 @@ +#include +#include + +namespace sidechain { + +sidechain_net_manager::sidechain_net_manager( graphene::chain::database* _db, std::string _ip, + uint32_t _zmq, uint32_t _rpc, std::string _user, std::string _password ): + listener( new zmq_listener( _ip, _zmq, _rpc ) ), bitcoin_client( new bitcoin_rpc_client( _ip, _rpc, _user, _password ) ), db( _db ) +{ + listener->block_received.connect( [this]( const std::string& block_hash ) { + std::thread( &sidechain_net_manager::handle_block, this, block_hash ).detach(); + }); +} + +void sidechain_net_manager::initialize_manager( graphene::chain::database* _db, std::string _ip, + uint32_t _zmq, uint32_t _rpc, std::string _user, std::string _password ) +{ + db = std::unique_ptr( _db ); + listener = std::unique_ptr( new zmq_listener( _ip, _zmq, _rpc ) ); + bitcoin_client = std::unique_ptr( new bitcoin_rpc_client( _ip, _rpc, _user, _password ) ); + + listener->block_received.connect([this]( const std::string& block_hash ) { + std::thread( &sidechain_net_manager::handle_block, this, block_hash).detach(); + } ); +} + + +void sidechain_net_manager::update_tx_infos( const std::string& block_hash ) +{ + std::string block = bitcoin_client->receive_full_block( block_hash ); + if( block != "" ) { + + } +} + +void sidechain_net_manager::update_tx_approvals() +{ + +} + +void sidechain_net_manager::update_estimated_fee() +{ + auto estimated_fee = bitcoin_client->receive_estimated_fee(); +} + +void sidechain_net_manager::send_btc_tx() +{ + FC_ASSERT( !bitcoin_client->connection_is_not_defined() ); +} + +bool sidechain_net_manager::connection_is_not_defined() const +{ + return listener->connection_is_not_defined() && bitcoin_client->connection_is_not_defined(); +} + +void sidechain_net_manager::handle_block( const std::string& block_hash ) +{ + update_tx_approvals(); + update_estimated_fee(); + update_tx_infos( block_hash ); +} + +} \ No newline at end of file diff --git a/libraries/sidechain/network/zmq_listener.cpp b/libraries/sidechain/network/zmq_listener.cpp new file mode 100644 index 00000000..91644cac --- /dev/null +++ b/libraries/sidechain/network/zmq_listener.cpp @@ -0,0 +1,54 @@ +#include +#include +#include + +namespace sidechain { + +zmq_listener::zmq_listener( std::string _ip, uint32_t _zmq, uint32_t _rpc ): + ip( _ip ), zmq_port( _zmq ), rpc_port( _rpc ), ctx( 1 ), socket( ctx, ZMQ_SUB ) +{ + std::thread( &zmq_listener::handle_zmq, this ).detach(); +} + +std::vector zmq_listener::receive_multipart() +{ + std::vector msgs; + + int32_t more; + size_t more_size = sizeof( more ); + while ( true ) { + zmq::message_t msg; + socket.recv( &msg, 0 ); + socket.getsockopt( ZMQ_RCVMORE, &more, &more_size ); + + if ( !more ) + break; + msgs.push_back( std::move(msg) ); + } + + return msgs; +} + +void zmq_listener::handle_zmq() +{ + socket.setsockopt( ZMQ_SUBSCRIBE, "hashblock", 0 ); + socket.connect( "tcp://" + ip + ":" + std::to_string( zmq_port ) ); + + fc::http::connection conn; + try { + conn.connect_to( fc::ip::endpoint( fc::ip::address( ip ), rpc_port ) ); + } catch ( fc::exception e ) { + elog( "No BTC node running at ${ip} or wrong rpc port: ${port}", ("ip", ip) ("port", rpc_port) ); + FC_ASSERT( false ); + } + + while ( true ) { + auto msg = receive_multipart(); + const auto header = std::string( static_cast( msg[0].data() ), msg[0].size() ); + const auto hash = boost::algorithm::hex( std::string( static_cast( msg[1].data() ), msg[1].size() ) ); + + block_received( hash ); + } +} + +} \ No newline at end of file