Added rpc and zmq modules for comminication with bitcoin node
This commit is contained in:
parent
d18e36d459
commit
24c6963ef3
12 changed files with 392 additions and 1 deletions
|
|
@ -963,6 +963,11 @@ void application::set_program_options(boost::program_options::options_descriptio
|
|||
("genesis-json", bpo::value<boost::filesystem::path>(), "File to read Genesis State from")
|
||||
("dbg-init-key", bpo::value<string>(), "Block signing key to use for init witnesses, overrides genesis file")
|
||||
("api-access", bpo::value<boost::filesystem::path>(), "JSON file specifying API permissions")
|
||||
("bitcoin-node-ip", bpo::value<string>()->implicit_value("127.0.0.1"), "IP address of Bitcoin node")
|
||||
("bitcoin-node-zmq-port", bpo::value<uint32_t>()->implicit_value(28332), "ZMQ port of Bitcoin node")
|
||||
("bitcoin-node-rpc-port", bpo::value<uint32_t>()->implicit_value(18332), "RPC port of Bitcoin node")
|
||||
("bitcoin-node-rpc-user", bpo::value<string>(), "Bitcoin RPC user")
|
||||
("bitcoin-node-rpc-password", bpo::value<string>(), "Bitcoin RPC password")
|
||||
;
|
||||
command_line_options.add(configuration_file_options);
|
||||
command_line_options.add_options()
|
||||
|
|
|
|||
|
|
@ -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" )
|
||||
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@
|
|||
|
||||
#include <graphene/app/plugin.hpp>
|
||||
#include <graphene/chain/database.hpp>
|
||||
#include <sidechain/network/sidechain_net_manager.hpp>
|
||||
|
||||
#include <fc/thread/future.hpp>
|
||||
|
||||
|
|
@ -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<chain::public_key_type, fc::ecc::private_key> _private_keys;
|
||||
std::set<chain::witness_id_type> _witnesses;
|
||||
|
|
|
|||
|
|
@ -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<std::string>();
|
||||
const auto zmq_port = options.at("bitcoin-node-zmq-port").as<uint32_t>();
|
||||
const auto rpc_port = options.at("bitcoin-node-rpc-port").as<uint32_t>();
|
||||
const auto rpc_user = options.at("bitcoin-node-rpc-user").as<std::string>();
|
||||
const auto rpc_password = options.at("bitcoin-node-rpc-password").as<std::string>();
|
||||
|
||||
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() }
|
||||
|
||||
|
|
|
|||
|
|
@ -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 )
|
||||
|
|
|
|||
7
libraries/sidechain/network/CMakeLists.txt
Normal file
7
libraries/sidechain/network/CMakeLists.txt
Normal file
|
|
@ -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" )
|
||||
124
libraries/sidechain/network/bitcoin_rpc_client.cpp
Normal file
124
libraries/sidechain/network/bitcoin_rpc_client.cpp
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
#include <sidechain/network/bitcoin_rpc_client.hpp>
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include <boost/property_tree/ptree.hpp>
|
||||
#include <boost/property_tree/json_parser.hpp>
|
||||
|
||||
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<int64_t>();
|
||||
}
|
||||
}
|
||||
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<std::string>();
|
||||
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} );
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <fc/network/http/connection.hpp>
|
||||
#include <fc/network/ip.hpp>
|
||||
|
||||
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;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
#pragma once
|
||||
|
||||
#include <sidechain/network/zmq_listener.hpp>
|
||||
#include <sidechain/network/bitcoin_rpc_client.hpp>
|
||||
#include <graphene/chain/database.hpp>
|
||||
|
||||
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<zmq_listener> listener;
|
||||
std::unique_ptr<bitcoin_rpc_client> bitcoin_client;
|
||||
std::unique_ptr<graphene::chain::database> db;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <fc/network/http/connection.hpp>
|
||||
#include <fc/network/ip.hpp>
|
||||
#include <fc/signals.hpp>
|
||||
|
||||
#include <zmq.hpp>
|
||||
|
||||
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<void( const std::string& )> block_received;
|
||||
|
||||
private:
|
||||
|
||||
void handle_zmq();
|
||||
std::vector<zmq::message_t> receive_multipart();
|
||||
|
||||
std::string ip;
|
||||
uint32_t zmq_port;
|
||||
uint32_t rpc_port;
|
||||
|
||||
zmq::context_t ctx;
|
||||
zmq::socket_t socket;
|
||||
};
|
||||
|
||||
}
|
||||
63
libraries/sidechain/network/sidechain_net_manager.cpp
Normal file
63
libraries/sidechain/network/sidechain_net_manager.cpp
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
#include <sidechain/network/sidechain_net_manager.hpp>
|
||||
#include <thread>
|
||||
|
||||
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<graphene::chain::database>( _db );
|
||||
listener = std::unique_ptr<zmq_listener>( new zmq_listener( _ip, _zmq, _rpc ) );
|
||||
bitcoin_client = std::unique_ptr<bitcoin_rpc_client>( 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 );
|
||||
}
|
||||
|
||||
}
|
||||
54
libraries/sidechain/network/zmq_listener.cpp
Normal file
54
libraries/sidechain/network/zmq_listener.cpp
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
#include <sidechain/network/zmq_listener.hpp>
|
||||
#include <boost/algorithm/hex.hpp>
|
||||
#include <thread>
|
||||
|
||||
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::message_t> zmq_listener::receive_multipart()
|
||||
{
|
||||
std::vector<zmq::message_t> 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<char*>( msg[0].data() ), msg[0].size() );
|
||||
const auto hash = boost::algorithm::hex( std::string( static_cast<char*>( msg[1].data() ), msg[1].size() ) );
|
||||
|
||||
block_received( hash );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in a new issue