Fix crashing errors on bitcoin event received
This commit is contained in:
parent
4be5743d68
commit
5d67efa964
7 changed files with 71 additions and 178 deletions
|
|
@ -11,17 +11,17 @@ namespace graphene { namespace peerplays_sidechain {
|
|||
|
||||
class sidechain_net_handler {
|
||||
public:
|
||||
sidechain_net_handler(peerplays_sidechain_plugin &_plugin, const boost::program_options::variables_map& options);
|
||||
sidechain_net_handler(std::shared_ptr<graphene::chain::database> db, const boost::program_options::variables_map& options);
|
||||
virtual ~sidechain_net_handler();
|
||||
|
||||
graphene::chain::database& get_database();
|
||||
|
||||
std::vector<std::string> get_sidechain_addresses();
|
||||
|
||||
protected:
|
||||
peerplays_sidechain_plugin &plugin;
|
||||
std::shared_ptr<graphene::chain::database> database;
|
||||
graphene::peerplays_sidechain::sidechain_type sidechain;
|
||||
|
||||
void sidechain_event_data_received(const sidechain_event_data& sed);
|
||||
|
||||
virtual std::string create_multisignature_wallet( const std::vector<std::string> public_keys ) = 0;
|
||||
virtual std::string transfer( const std::string& from, const std::string& to, const uint64_t amount ) = 0;
|
||||
virtual std::string sign_transaction( const std::string& transaction ) = 0;
|
||||
|
|
|
|||
|
|
@ -56,17 +56,9 @@ private:
|
|||
|
||||
class sidechain_net_handler_bitcoin : public sidechain_net_handler {
|
||||
public:
|
||||
sidechain_net_handler_bitcoin(peerplays_sidechain_plugin &_plugin, const boost::program_options::variables_map& options);
|
||||
sidechain_net_handler_bitcoin(std::shared_ptr<graphene::chain::database> db, const boost::program_options::variables_map& options);
|
||||
virtual ~sidechain_net_handler_bitcoin();
|
||||
|
||||
void update_tx_infos( const std::string& block_hash );
|
||||
|
||||
//void update_tx_approvals();
|
||||
|
||||
//void update_estimated_fee();
|
||||
|
||||
//void send_btc_tx( const sidechain::bitcoin_transaction& trx );
|
||||
|
||||
bool connection_is_not_defined() const;
|
||||
|
||||
std::string create_multisignature_wallet( const std::vector<std::string> public_keys );
|
||||
|
|
@ -85,15 +77,7 @@ private:
|
|||
std::unique_ptr<bitcoin_rpc_client> bitcoin_client;
|
||||
|
||||
void handle_event( const std::string& event_data);
|
||||
|
||||
std::vector<info_for_vin> extract_info_from_block( const std::string& _block );
|
||||
|
||||
void update_transaction_status( std::vector<fc::sha256> trx_for_check );
|
||||
|
||||
std::set<fc::sha256> get_valid_vins( const std::string tx_hash );
|
||||
|
||||
inline uint64_t parse_amount(std::string raw);
|
||||
|
||||
};
|
||||
|
||||
} } // graphene::peerplays_sidechain
|
||||
|
|
|
|||
|
|
@ -12,12 +12,12 @@ namespace graphene { namespace peerplays_sidechain {
|
|||
|
||||
class sidechain_net_manager {
|
||||
public:
|
||||
sidechain_net_manager(peerplays_sidechain_plugin &_plugin);
|
||||
sidechain_net_manager(std::shared_ptr<graphene::chain::database> db);
|
||||
virtual ~sidechain_net_manager();
|
||||
|
||||
bool create_handler(peerplays_sidechain::sidechain_type sidechain, const boost::program_options::variables_map& options);
|
||||
private:
|
||||
peerplays_sidechain_plugin &plugin;
|
||||
std::shared_ptr<graphene::chain::database> database;
|
||||
std::vector<std::unique_ptr<sidechain_net_handler>> net_handlers;
|
||||
|
||||
};
|
||||
|
|
|
|||
|
|
@ -18,8 +18,6 @@ class peerplays_sidechain_plugin_impl
|
|||
peerplays_sidechain_plugin_impl(peerplays_sidechain_plugin& _plugin);
|
||||
virtual ~peerplays_sidechain_plugin_impl();
|
||||
|
||||
graphene::chain::database& get_database();
|
||||
|
||||
void plugin_set_program_options(
|
||||
boost::program_options::options_description& cli,
|
||||
boost::program_options::options_description& cfg);
|
||||
|
|
@ -27,18 +25,19 @@ class peerplays_sidechain_plugin_impl
|
|||
void plugin_startup();
|
||||
|
||||
private:
|
||||
peerplays_sidechain_plugin& _self;
|
||||
peerplays_sidechain::sidechain_net_manager _net_manager;
|
||||
peerplays_sidechain_plugin& plugin;
|
||||
|
||||
bool config_ready_son;
|
||||
bool config_ready_bitcoin;
|
||||
|
||||
std::unique_ptr<peerplays_sidechain::sidechain_net_manager> net_manager;
|
||||
};
|
||||
|
||||
peerplays_sidechain_plugin_impl::peerplays_sidechain_plugin_impl(peerplays_sidechain_plugin& _plugin) :
|
||||
_self( _plugin ),
|
||||
_net_manager( _plugin ),
|
||||
plugin( _plugin ),
|
||||
config_ready_son(false),
|
||||
config_ready_bitcoin(false)
|
||||
config_ready_bitcoin(false),
|
||||
net_manager(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
@ -46,11 +45,6 @@ peerplays_sidechain_plugin_impl::~peerplays_sidechain_plugin_impl()
|
|||
{
|
||||
}
|
||||
|
||||
graphene::chain::database& peerplays_sidechain_plugin_impl::get_database()
|
||||
{
|
||||
return _self.database();
|
||||
}
|
||||
|
||||
void peerplays_sidechain_plugin_impl::plugin_set_program_options(
|
||||
boost::program_options::options_description& cli,
|
||||
boost::program_options::options_description& cfg)
|
||||
|
|
@ -84,16 +78,27 @@ void peerplays_sidechain_plugin_impl::plugin_initialize(const boost::program_opt
|
|||
wlog("Haven't set up SON parameters");
|
||||
}
|
||||
|
||||
net_manager = std::unique_ptr<sidechain_net_manager>(new sidechain_net_manager(plugin.app().chain_database()));
|
||||
|
||||
config_ready_bitcoin = 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" ) &&
|
||||
options.count( "bitcoin-address" ) && options.count( "bitcoin-public-key" ) && options.count( "bitcoin-private-key" );
|
||||
if (config_ready_bitcoin) {
|
||||
_net_manager.create_handler(sidechain_type::bitcoin, options);
|
||||
net_manager->create_handler(sidechain_type::bitcoin, options);
|
||||
ilog("Bitcoin sidechain handler created");
|
||||
} else {
|
||||
wlog("Haven't set up bitcoin sidechain parameters");
|
||||
wlog("Haven't set up Bitcoin sidechain parameters");
|
||||
}
|
||||
|
||||
//config_ready_ethereum = options.count( "ethereum-node-ip" ) &&
|
||||
// options.count( "ethereum-address" ) && options.count( "ethereum-public-key" ) && options.count( "ethereum-private-key" );
|
||||
//if (config_ready_ethereum) {
|
||||
// net_manager->create_handler(sidechain_type::ethereum, options);
|
||||
// ilog("Ethereum sidechain handler created");
|
||||
//} else {
|
||||
// wlog("Haven't set up Ethereum sidechain parameters");
|
||||
//}
|
||||
}
|
||||
|
||||
void peerplays_sidechain_plugin_impl::plugin_startup()
|
||||
|
|
@ -105,6 +110,10 @@ void peerplays_sidechain_plugin_impl::plugin_startup()
|
|||
if (config_ready_bitcoin) {
|
||||
ilog("Bitcoin sidechain handler running");
|
||||
}
|
||||
|
||||
//if (config_ready_ethereum) {
|
||||
// ilog("Ethereum sidechain handler running");
|
||||
//}
|
||||
}
|
||||
|
||||
} // end namespace detail
|
||||
|
|
|
|||
|
|
@ -6,26 +6,21 @@
|
|||
|
||||
namespace graphene { namespace peerplays_sidechain {
|
||||
|
||||
sidechain_net_handler::sidechain_net_handler(peerplays_sidechain_plugin &_plugin, const boost::program_options::variables_map& options) :
|
||||
plugin( _plugin )
|
||||
sidechain_net_handler::sidechain_net_handler(std::shared_ptr<graphene::chain::database> db, const boost::program_options::variables_map& options) :
|
||||
database(db)
|
||||
{
|
||||
}
|
||||
|
||||
sidechain_net_handler::~sidechain_net_handler() {
|
||||
}
|
||||
|
||||
graphene::chain::database& sidechain_net_handler::get_database()
|
||||
{
|
||||
return plugin.database();
|
||||
}
|
||||
|
||||
std::vector<std::string> sidechain_net_handler::get_sidechain_addresses() {
|
||||
std::vector<std::string> result;
|
||||
|
||||
switch (sidechain) {
|
||||
case sidechain_type::bitcoin:
|
||||
{
|
||||
const auto& sidechain_addresses_idx = get_database().get_index_type<sidechain_address_index>();
|
||||
const auto& sidechain_addresses_idx = database->get_index_type<sidechain_address_index>();
|
||||
const auto& sidechain_addresses_by_sidechain_idx = sidechain_addresses_idx.indices().get<by_sidechain>();
|
||||
const auto& sidechain_addresses_by_sidechain_range = sidechain_addresses_by_sidechain_idx.equal_range(sidechain);
|
||||
std::for_each(sidechain_addresses_by_sidechain_range.first, sidechain_addresses_by_sidechain_range.second,
|
||||
|
|
@ -41,5 +36,15 @@ std::vector<std::string> sidechain_net_handler::get_sidechain_addresses() {
|
|||
return result;
|
||||
}
|
||||
|
||||
void sidechain_net_handler::sidechain_event_data_received(const sidechain_event_data& sed) {
|
||||
ilog( __FUNCTION__ );
|
||||
ilog( "sidechain_event_data:" );
|
||||
ilog( " sidechain: ${sidechain}", ( "sidechain", sed.sidechain ) );
|
||||
ilog( " transaction_id: ${transaction_id}", ( "transaction_id", sed.transaction_id ) );
|
||||
ilog( " from: ${from}", ( "from", sed.from ) );
|
||||
ilog( " to: ${to}", ( "to", sed.to ) );
|
||||
ilog( " amount: ${amount}", ( "amount", sed.amount ) );
|
||||
}
|
||||
|
||||
} } // graphene::peerplays_sidechain
|
||||
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@
|
|||
#include <fc/log/logger.hpp>
|
||||
#include <fc/network/ip.hpp>
|
||||
|
||||
#include "graphene/peerplays_sidechain/sidechain_net_manager.hpp"
|
||||
#include <graphene/chain/sidechain_address_object.hpp>
|
||||
|
||||
namespace graphene { namespace peerplays_sidechain {
|
||||
|
||||
|
|
@ -186,8 +186,8 @@ void zmq_listener::handle_zmq() {
|
|||
|
||||
// =============================================================================
|
||||
|
||||
sidechain_net_handler_bitcoin::sidechain_net_handler_bitcoin(peerplays_sidechain_plugin &_plugin, const boost::program_options::variables_map& options) :
|
||||
sidechain_net_handler(plugin, options) {
|
||||
sidechain_net_handler_bitcoin::sidechain_net_handler_bitcoin(std::shared_ptr<graphene::chain::database> db, const boost::program_options::variables_map& options) :
|
||||
sidechain_net_handler(db, options) {
|
||||
sidechain = sidechain_type::bitcoin;
|
||||
|
||||
ip = options.at("bitcoin-node-ip").as<std::string>();
|
||||
|
|
@ -206,74 +206,15 @@ sidechain_net_handler_bitcoin::sidechain_net_handler_bitcoin(peerplays_sidechain
|
|||
|
||||
listener = std::unique_ptr<zmq_listener>( new zmq_listener( ip, zmq_port ) );
|
||||
bitcoin_client = std::unique_ptr<bitcoin_rpc_client>( new bitcoin_rpc_client( ip, rpc_port, rpc_user, rpc_password ) );
|
||||
//db = _db;
|
||||
|
||||
listener->event_received.connect([this]( const std::string& event_data ) {
|
||||
std::thread( &sidechain_net_handler_bitcoin::handle_event, this, event_data ).detach();
|
||||
} );
|
||||
|
||||
//db->send_btc_tx.connect([this]( const sidechain::bitcoin_transaction& trx ) {
|
||||
// std::thread( &sidechain_net_handler_bitcoin::send_btc_tx, this, trx ).detach();
|
||||
//} );
|
||||
}
|
||||
|
||||
sidechain_net_handler_bitcoin::~sidechain_net_handler_bitcoin() {
|
||||
}
|
||||
|
||||
void sidechain_net_handler_bitcoin::update_tx_infos( const std::string& block_hash )
|
||||
{
|
||||
std::string block = bitcoin_client->receive_full_block( block_hash );
|
||||
if( block != "" ) {
|
||||
const auto& vins = extract_info_from_block( block );
|
||||
// const auto& addr_idx = db->get_index_type<bitcoin_address_index>().indices().get<by_address>();
|
||||
// for( const auto& v : vins ) {
|
||||
// const auto& addr_itr = addr_idx.find( v.address );
|
||||
// FC_ASSERT( addr_itr != addr_idx.end() );
|
||||
// db->i_w_info.insert_info_for_vin( prev_out{ v.out.hash_tx, v.out.n_vout, v.out.amount }, v.address, addr_itr->address.get_witness_script() );
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
//void sidechain_net_handler_bitcoin::update_tx_approvals()
|
||||
//{
|
||||
// std::vector<fc::sha256> trx_for_check;
|
||||
// const auto& confirmations_num = db->get_sidechain_params().confirmations_num;
|
||||
//
|
||||
// db->bitcoin_confirmations.safe_for<by_hash>([&]( btc_tx_confirmations_index::iterator itr_b, btc_tx_confirmations_index::iterator itr_e ){
|
||||
// for(auto iter = itr_b; iter != itr_e; iter++) {
|
||||
// db->bitcoin_confirmations.modify<by_hash>( iter->transaction_id, [&]( bitcoin_transaction_confirmations& obj ) {
|
||||
// obj.count_block++;
|
||||
// });
|
||||
//
|
||||
// if( iter->count_block == confirmations_num ) {
|
||||
// trx_for_check.push_back( iter->transaction_id );
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
//
|
||||
// update_transaction_status( trx_for_check );
|
||||
//
|
||||
//}
|
||||
|
||||
//void sidechain_net_handler_bitcoin::update_estimated_fee()
|
||||
//{
|
||||
// db->estimated_feerate = bitcoin_client->receive_estimated_fee();
|
||||
//}
|
||||
|
||||
//void sidechain_net_handler_bitcoin::send_btc_tx( const sidechain::bitcoin_transaction& trx )
|
||||
//{
|
||||
// std::set<fc::sha256> valid_vins;
|
||||
// for( const auto& v : trx.vin ) {
|
||||
// valid_vins.insert( v.prevout.hash );
|
||||
// }
|
||||
// db->bitcoin_confirmations.insert( bitcoin_transaction_confirmations( trx.get_txid(), valid_vins ) );
|
||||
//
|
||||
// FC_ASSERT( !bitcoin_client->connection_is_not_defined() );
|
||||
// const auto tx_hex = fc::to_hex( pack( trx ) );
|
||||
//
|
||||
// bitcoin_client->send_btc_tx( tx_hex );
|
||||
//}
|
||||
|
||||
bool sidechain_net_handler_bitcoin::connection_is_not_defined() const
|
||||
{
|
||||
return listener->connection_is_not_defined() && bitcoin_client->connection_is_not_defined();
|
||||
|
|
@ -303,11 +244,26 @@ void sidechain_net_handler_bitcoin::handle_event( const std::string& event_data
|
|||
ilog("peerplays sidechain plugin: sidechain_net_handler_bitcoin::handle_event");
|
||||
ilog(" event_data: ${event_data}", ("event_data", event_data));
|
||||
|
||||
update_tx_infos( event_data );
|
||||
std::string block = bitcoin_client->receive_full_block( event_data );
|
||||
if( block != "" ) {
|
||||
const auto& vins = extract_info_from_block( block );
|
||||
|
||||
//update_tx_approvals();
|
||||
//update_estimated_fee();
|
||||
//update_tx_infos( block_hash );
|
||||
const auto& sidechain_addresses_idx = database->get_index_type<sidechain_address_index>().indices().get<by_sidechain_and_address>();
|
||||
|
||||
for( const auto& v : vins ) {
|
||||
const auto& addr_itr = sidechain_addresses_idx.find(std::make_tuple(sidechain_type::bitcoin, v.address));
|
||||
if ( addr_itr == sidechain_addresses_idx.end() )
|
||||
continue;
|
||||
|
||||
sidechain_event_data sed;
|
||||
sed.sidechain = addr_itr->sidechain;
|
||||
sed.transaction_id = v.out.hash_tx;
|
||||
sed.from = "";
|
||||
sed.to = v.address;
|
||||
sed.amount = v.out.amount;
|
||||
sidechain_event_data_received(sed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<info_for_vin> sidechain_net_handler_bitcoin::extract_info_from_block( const std::string& _block )
|
||||
|
|
@ -318,8 +274,6 @@ std::vector<info_for_vin> sidechain_net_handler_bitcoin::extract_info_from_block
|
|||
|
||||
std::vector<info_for_vin> result;
|
||||
|
||||
const auto& addr_idx = get_sidechain_addresses();// db->get_index_type<bitcoin_address_index>().indices().get<by_address>();
|
||||
|
||||
for (const auto& tx_child : block.get_child("tx")) {
|
||||
const auto& tx = tx_child.second;
|
||||
|
||||
|
|
@ -330,18 +284,12 @@ std::vector<info_for_vin> sidechain_net_handler_bitcoin::extract_info_from_block
|
|||
|
||||
for (const auto& addr : script.get_child("addresses")) { // in which cases there can be more addresses?
|
||||
const auto address_base58 = addr.second.get_value<std::string>();
|
||||
ilog(" address_base58: ${address_base58}", ("address_base58", address_base58));
|
||||
|
||||
auto it = find(addr_idx.begin(), addr_idx.end(), address_base58);
|
||||
if (it == addr_idx.end()) continue;
|
||||
ilog(" address_base58 found: ${address_base58}", ("address_base58", address_base58));
|
||||
|
||||
info_for_vin vin;
|
||||
vin.out.hash_tx = tx.get_child("txid").get_value<std::string>();
|
||||
vin.out.amount = parse_amount( o.second.get_child( "value" ).get_value<std::string>() );
|
||||
ilog(" amount: ${amount}", ("amount", vin.out.amount));
|
||||
string amount = o.second.get_child( "value" ).get_value<std::string>();
|
||||
amount.erase(std::remove(amount.begin(), amount.end(), '.'), amount.end());
|
||||
vin.out.amount = std::stoll(amount);
|
||||
vin.out.n_vout = o.second.get_child( "n" ).get_value<uint32_t>();
|
||||
ilog(" n_vout: ${n_vout}", ("n_vout", vin.out.n_vout));
|
||||
vin.address = address_base58;
|
||||
result.push_back( vin );
|
||||
}
|
||||
|
|
@ -351,59 +299,6 @@ std::vector<info_for_vin> sidechain_net_handler_bitcoin::extract_info_from_block
|
|||
return result;
|
||||
}
|
||||
|
||||
//void sidechain_net_handler_bitcoin::update_transaction_status( std::vector<fc::sha256> trx_for_check )
|
||||
//{
|
||||
// const auto& confirmations_num = db->get_sidechain_params().confirmations_num;
|
||||
//
|
||||
// for( const auto& trx : trx_for_check ) {
|
||||
// auto confirmations = bitcoin_client->receive_confirmations_tx( trx.str() );
|
||||
// db->bitcoin_confirmations.modify<by_hash>( trx, [&]( bitcoin_transaction_confirmations& obj ) {
|
||||
// obj.count_block = confirmations;
|
||||
// });
|
||||
//
|
||||
// if( confirmations >= confirmations_num ) {
|
||||
// db->bitcoin_confirmations.modify<by_hash>( trx, [&]( bitcoin_transaction_confirmations& obj ) {
|
||||
// obj.confirmed = true;
|
||||
// });
|
||||
//
|
||||
// } else if( confirmations == 0 ) {
|
||||
// auto is_in_mempool = bitcoin_client->receive_mempool_entry_tx( trx.str() );
|
||||
//
|
||||
// std::set<fc::sha256> valid_vins;
|
||||
// if( !is_in_mempool ) {
|
||||
// valid_vins = get_valid_vins( trx.str() );
|
||||
// }
|
||||
//
|
||||
// db->bitcoin_confirmations.modify<by_hash>( trx, [&]( bitcoin_transaction_confirmations& obj ) {
|
||||
// obj.missing = !is_in_mempool;
|
||||
// obj.valid_vins = valid_vins;
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
//std::set<fc::sha256> sidechain_net_handler_bitcoin::get_valid_vins( const std::string tx_hash )
|
||||
//{
|
||||
// const auto& confirmations_obj = db->bitcoin_confirmations.find<sidechain::by_hash>( fc::sha256( tx_hash ) );
|
||||
// FC_ASSERT( confirmations_obj.valid() );
|
||||
//
|
||||
// std::set<fc::sha256> valid_vins;
|
||||
// for( const auto& v : confirmations_obj->valid_vins ) {
|
||||
// auto confirmations = bitcoin_client->receive_confirmations_tx( v.str() );
|
||||
// if( confirmations == 0 ) {
|
||||
// continue;
|
||||
// }
|
||||
// valid_vins.insert( v );
|
||||
// }
|
||||
// return valid_vins;
|
||||
//}
|
||||
|
||||
// Removes dot from amount output: "50.00000000"
|
||||
inline uint64_t sidechain_net_handler_bitcoin::parse_amount(std::string raw) {
|
||||
raw.erase(std::remove(raw.begin(), raw.end(), '.'), raw.end());
|
||||
return std::stoll(raw);
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
|
||||
} } // graphene::peerplays_sidechain
|
||||
|
|
|
|||
|
|
@ -5,8 +5,8 @@
|
|||
|
||||
namespace graphene { namespace peerplays_sidechain {
|
||||
|
||||
sidechain_net_manager::sidechain_net_manager(peerplays_sidechain_plugin &_plugin) :
|
||||
plugin(_plugin)
|
||||
sidechain_net_manager::sidechain_net_manager(std::shared_ptr<graphene::chain::database> db) :
|
||||
database(db)
|
||||
{
|
||||
ilog(__FUNCTION__);
|
||||
}
|
||||
|
|
@ -22,7 +22,7 @@ bool sidechain_net_manager::create_handler(peerplays_sidechain::sidechain_type s
|
|||
|
||||
switch (sidechain) {
|
||||
case sidechain_type::bitcoin: {
|
||||
std::unique_ptr<sidechain_net_handler> h = std::unique_ptr<sidechain_net_handler>(new sidechain_net_handler_bitcoin(plugin, options));
|
||||
std::unique_ptr<sidechain_net_handler> h = std::unique_ptr<sidechain_net_handler>(new sidechain_net_handler_bitcoin(database, options));
|
||||
net_handlers.push_back(std::move(h));
|
||||
ret_val = true;
|
||||
break;
|
||||
|
|
|
|||
Loading…
Reference in a new issue