From 0bb271a455db2a0abbdef656c908b3d181c67174 Mon Sep 17 00:00:00 2001 From: Alexander Suslikov Date: Tue, 22 Jan 2019 15:43:48 +0300 Subject: [PATCH] Added bitcoin_transaction_create_operation, evaluator; filled sidechain_net_manager::send_btc_tx --- libraries/app/application.cpp | 4 +- libraries/app/impacted.cpp | 4 ++ libraries/chain/CMakeLists.txt | 1 + .../chain/bitcoin_transaction_evaluator.cpp | 49 +++++++++++++++ libraries/chain/db_init.cpp | 4 ++ libraries/chain/db_management.cpp | 2 +- libraries/chain/db_notify.cpp | 4 ++ .../chain/bitcoin_transaction_evaluator.hpp | 19 ++++++ .../chain/bitcoin_transaction_object.hpp | 43 +++++++++++++ .../chain/include/graphene/chain/database.hpp | 8 ++- .../chain/protocol/bitcoin_transaction.hpp | 37 +++++++++++ .../graphene/chain/protocol/operations.hpp | 4 +- .../include/graphene/chain/protocol/types.hpp | 63 ++++++++++--------- libraries/net/CMakeLists.txt | 2 +- .../sidechain/primary_wallet_vout_manager.hpp | 17 ++--- .../sidechain/include/sidechain/types.hpp | 2 +- .../sidechain/network/bitcoin_rpc_client.cpp | 2 +- .../network/sidechain_net_manager.hpp | 2 +- .../network/sidechain_net_manager.cpp | 30 ++++++++- .../sidechain/primary_wallet_vout_manager.cpp | 20 ++++-- programs/js_operation_serializer/main.cpp | 4 ++ .../primary_wallet_vout_manager_tests.cpp | 1 + 22 files changed, 272 insertions(+), 50 deletions(-) create mode 100644 libraries/chain/bitcoin_transaction_evaluator.cpp create mode 100644 libraries/chain/include/graphene/chain/bitcoin_transaction_evaluator.hpp create mode 100644 libraries/chain/include/graphene/chain/bitcoin_transaction_object.hpp create mode 100644 libraries/chain/include/graphene/chain/protocol/bitcoin_transaction.hpp diff --git a/libraries/app/application.cpp b/libraries/app/application.cpp index 5993020a..1029d6fd 100644 --- a/libraries/app/application.cpp +++ b/libraries/app/application.cpp @@ -538,7 +538,9 @@ namespace detail { // you can help the network code out by throwing a block_older_than_undo_history exception. // when the net code sees that, it will stop trying to push blocks from that chain, but // leave that peer connected so that they can get sync blocks from us - bool result = _chain_db->push_block(blk_msg.block, (_is_block_producer | _force_validate) ? database::skip_nothing : database::skip_transaction_signatures); + auto skip_nothing_mode = sync_mode ? database::skip_nothing | database::skip_btc_tx_sending : database::skip_nothing; + auto skip_transaction_signatures_mode = sync_mode ? database::skip_transaction_signatures | database::skip_btc_tx_sending : database::skip_transaction_signatures; + bool result = _chain_db->push_block(blk_msg.block, (_is_block_producer | _force_validate) ? skip_nothing_mode : skip_transaction_signatures_mode); // the block was accepted, so we now know all of the transactions contained in the block if (!sync_mode) diff --git a/libraries/app/impacted.cpp b/libraries/app/impacted.cpp index ac758eaa..65d73906 100644 --- a/libraries/app/impacted.cpp +++ b/libraries/app/impacted.cpp @@ -290,6 +290,10 @@ struct get_impacted_account_visitor { _impacted.insert( op.payer ); } + void operator()( const bitcoin_transaction_send_operation& op ) + { + _impacted.insert( op.payer ); + } }; void operation_get_impacted_accounts( const operation& op, flat_set& result ) diff --git a/libraries/chain/CMakeLists.txt b/libraries/chain/CMakeLists.txt index 0a82e51f..aa87d669 100644 --- a/libraries/chain/CMakeLists.txt +++ b/libraries/chain/CMakeLists.txt @@ -86,6 +86,7 @@ add_library( graphene_chain special_authority.cpp buyback.cpp bitcoin_address_evaluator.cpp + bitcoin_transaction_evaluator.cpp account_object.cpp asset_object.cpp diff --git a/libraries/chain/bitcoin_transaction_evaluator.cpp b/libraries/chain/bitcoin_transaction_evaluator.cpp new file mode 100644 index 00000000..eb0da7ae --- /dev/null +++ b/libraries/chain/bitcoin_transaction_evaluator.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include + +namespace graphene { namespace chain { + +void_result bitcoin_transaction_send_evaluator::do_evaluate( const bitcoin_transaction_send_operation& op ) +{ + // FC_ASSERT( db().get_sidechain_account_id() == op.payer ); + return void_result(); +} + +object_id_type bitcoin_transaction_send_evaluator::do_apply( const bitcoin_transaction_send_operation& op ) +{ + database& d = db(); + sidechain::prev_out new_pw_vout = { "", 0, 0 }; + sidechain::bytes wit_script; + + const bitcoin_transaction_object& btc_tx = d.create< bitcoin_transaction_object >( [&]( bitcoin_transaction_object& obj ) + { + obj.pw_vin = op.pw_vin; + obj.vins = op.vins; + obj.vouts = op.vouts; + obj.transaction = op.transaction; + obj.transaction_id = op.transaction.get_txid(); + obj.fee_for_size = op.fee_for_size; + obj.confirm = false; + }); + + if( btc_tx.transaction.vout[0].is_p2wsh() && btc_tx.transaction.vout[0].scriptPubKey == wit_script ) + new_pw_vout = { btc_tx.transaction.get_txid(), 0, btc_tx.transaction.vout[0].value }; + + d.pw_vout_manager.create_new_vout( new_pw_vout ); + send_bitcoin_transaction( btc_tx ); + + return btc_tx.id; +} + +void bitcoin_transaction_send_evaluator::send_bitcoin_transaction( const bitcoin_transaction_object& btc_tx ) +{ + database& d = db(); + uint32_t skip = d.get_node_properties().skip_flags; + if( !(skip & graphene::chain::database::skip_btc_tx_sending) ){ + d.send_btc_tx( btc_tx.transaction ); + } +} + +} } diff --git a/libraries/chain/db_init.cpp b/libraries/chain/db_init.cpp index 40844ee2..4554fe83 100644 --- a/libraries/chain/db_init.cpp +++ b/libraries/chain/db_init.cpp @@ -53,6 +53,7 @@ #include #include #include +#include #include @@ -82,6 +83,7 @@ #include #include #include +#include #include @@ -246,6 +248,7 @@ void database::initialize_evaluators() register_evaluator(); register_evaluator(); register_evaluator(); + register_evaluator(); } void database::initialize_indexes() @@ -315,6 +318,7 @@ void database::initialize_indexes() add_index< primary_index >(); add_index< primary_index >(); add_index< primary_index >(); + add_index< primary_index >(); } void database::init_genesis(const genesis_state_type& genesis_state) diff --git a/libraries/chain/db_management.cpp b/libraries/chain/db_management.cpp index b63ba6c2..ce8041a9 100644 --- a/libraries/chain/db_management.cpp +++ b/libraries/chain/db_management.cpp @@ -36,7 +36,7 @@ namespace graphene { namespace chain { database::database() : - i_w_info(*this), _random_number_generator(fc::ripemd160().data()) + i_w_info(*this), pw_vout_manager(*this), _random_number_generator(fc::ripemd160().data()) { initialize_indexes(); initialize_evaluators(); diff --git a/libraries/chain/db_notify.cpp b/libraries/chain/db_notify.cpp index a8175c0d..fd02ba23 100644 --- a/libraries/chain/db_notify.cpp +++ b/libraries/chain/db_notify.cpp @@ -277,6 +277,10 @@ struct get_impacted_account_visitor { _impacted.insert( op.payer ); } + void operator()( const bitcoin_transaction_send_operation& op ) + { + _impacted.insert( op.payer ); + } }; void operation_get_impacted_accounts( const operation& op, flat_set& result ) diff --git a/libraries/chain/include/graphene/chain/bitcoin_transaction_evaluator.hpp b/libraries/chain/include/graphene/chain/bitcoin_transaction_evaluator.hpp new file mode 100644 index 00000000..6013294c --- /dev/null +++ b/libraries/chain/include/graphene/chain/bitcoin_transaction_evaluator.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include + +namespace graphene { namespace chain { + +class bitcoin_transaction_send_evaluator : public evaluator +{ +public: + typedef bitcoin_transaction_send_operation operation_type; + + void_result do_evaluate( const bitcoin_transaction_send_operation& op ); + + object_id_type do_apply( const bitcoin_transaction_send_operation& op ); + + void send_bitcoin_transaction( const bitcoin_transaction_object& btc_tx ); +}; + +} } // graphene::chain diff --git a/libraries/chain/include/graphene/chain/bitcoin_transaction_object.hpp b/libraries/chain/include/graphene/chain/bitcoin_transaction_object.hpp new file mode 100644 index 00000000..3f70fdf5 --- /dev/null +++ b/libraries/chain/include/graphene/chain/bitcoin_transaction_object.hpp @@ -0,0 +1,43 @@ +#pragma once + +#include +#include +#include + +namespace graphene { namespace chain { + +class bitcoin_transaction_object : public abstract_object +{ + public: + static const uint8_t space_id = protocol_ids; + static const uint8_t type_id = bitcoin_transaction_object_type; + + bitcoin_transaction_id_type get_id()const { return id; } + + fc::sha256 pw_vin; + + std::vector< fc::sha256 > vins; + std::vector< info_for_vout_id_type > vouts; + + sidechain::bitcoin_transaction transaction; + std::string transaction_id; + + uint64_t fee_for_size; + + bool confirm = false; +}; + +struct by_transaction_id; + +typedef boost::multi_index_container< + bitcoin_transaction_object, + indexed_by< + ordered_unique< tag< by_id >, member< object, object_id_type, &object::id > >, + ordered_unique< tag< by_transaction_id >, member< bitcoin_transaction_object, string, &bitcoin_transaction_object::transaction_id > > + > +> bitcoin_transaction_multi_index_container; +typedef generic_index bitcoin_transaction_index; + +} } // graphene::chain + +FC_REFLECT_DERIVED( graphene::chain::bitcoin_transaction_object, (graphene::chain::object), (pw_vin)(vins)(vouts)(transaction)(transaction_id)(fee_for_size)(confirm) ) diff --git a/libraries/chain/include/graphene/chain/database.hpp b/libraries/chain/include/graphene/chain/database.hpp index 2d31421d..511dfefd 100644 --- a/libraries/chain/include/graphene/chain/database.hpp +++ b/libraries/chain/include/graphene/chain/database.hpp @@ -39,7 +39,9 @@ #include #include + #include +#include #include @@ -79,7 +81,8 @@ namespace graphene { namespace chain { skip_assert_evaluation = 1 << 8, ///< used while reindexing skip_undo_history_check = 1 << 9, ///< used while reindexing skip_witness_schedule_check = 1 << 10, ///< used while reindexing - skip_validate = 1 << 11 ///< used prior to checkpoint, skips validate() call on transaction + skip_validate = 1 << 11, ///< used prior to checkpoint, skips validate() call on transaction + skip_btc_tx_sending = 1 << 12 }; /** @@ -509,6 +512,9 @@ namespace graphene { namespace chain { public: sidechain::input_withdrawal_info i_w_info; + sidechain::primary_wallet_vout_manager pw_vout_manager; + + fc::signal send_btc_tx; private: diff --git a/libraries/chain/include/graphene/chain/protocol/bitcoin_transaction.hpp b/libraries/chain/include/graphene/chain/protocol/bitcoin_transaction.hpp new file mode 100644 index 00000000..f216a7be --- /dev/null +++ b/libraries/chain/include/graphene/chain/protocol/bitcoin_transaction.hpp @@ -0,0 +1,37 @@ +#pragma once +#include +#include + +namespace graphene { namespace chain { + + struct bitcoin_transaction_send_operation : public base_operation + { + struct fee_parameters_type { + uint64_t fee = 0; + uint32_t price_per_kbyte = 0; + }; + + asset fee; + account_id_type payer; + + fc::sha256 pw_vin; + + std::vector< fc::sha256 > vins; + std::vector< info_for_vout_id_type > vouts; + + sidechain::bitcoin_transaction transaction; + uint64_t fee_for_size; + + account_id_type fee_payer()const { return payer; } + void validate()const {} + share_type calculate_fee( const fee_parameters_type& k )const { + share_type fee_required = k.fee; + fee_required += calculate_data_fee( fc::raw::pack_size(*this), k.price_per_kbyte ); + return fee_required; + } + }; + +} } // graphene::chain + +FC_REFLECT( graphene::chain::bitcoin_transaction_send_operation::fee_parameters_type, (fee)(price_per_kbyte) ) +FC_REFLECT( graphene::chain::bitcoin_transaction_send_operation, (fee)(payer)(vins)(vouts)(transaction)(fee_for_size) ) diff --git a/libraries/chain/include/graphene/chain/protocol/operations.hpp b/libraries/chain/include/graphene/chain/protocol/operations.hpp index d256258c..e88e8ee8 100644 --- a/libraries/chain/include/graphene/chain/protocol/operations.hpp +++ b/libraries/chain/include/graphene/chain/protocol/operations.hpp @@ -46,6 +46,7 @@ #include #include #include +#include namespace graphene { namespace chain { @@ -133,7 +134,8 @@ namespace graphene { namespace chain { affiliate_payout_operation, // VIRTUAL affiliate_referral_payout_operation, // VIRTUAL withdraw_pbtc_operation, - bitcoin_address_create_operation + bitcoin_address_create_operation, + bitcoin_transaction_send_operation > operation; /// @} // operations group diff --git a/libraries/chain/include/graphene/chain/protocol/types.hpp b/libraries/chain/include/graphene/chain/protocol/types.hpp index b01e9eae..69c17669 100644 --- a/libraries/chain/include/graphene/chain/protocol/types.hpp +++ b/libraries/chain/include/graphene/chain/protocol/types.hpp @@ -149,6 +149,7 @@ namespace graphene { namespace chain { bitcoin_address_object_type, primary_wallet_vout_object_type, sidechain_proposal_object_type, + bitcoin_transaction_object_type, OBJECT_TYPE_COUNT ///< Sentry value which contains the number of different object types }; @@ -210,36 +211,38 @@ namespace graphene { namespace chain { class bitcoin_address_object; class primary_wallet_vout_object; class sidechain_proposal_object; + class bitcoin_transaction_object; - typedef object_id< protocol_ids, account_object_type, account_object> account_id_type; - typedef object_id< protocol_ids, asset_object_type, asset_object> asset_id_type; - typedef object_id< protocol_ids, force_settlement_object_type, force_settlement_object> force_settlement_id_type; - typedef object_id< protocol_ids, committee_member_object_type, committee_member_object> committee_member_id_type; - typedef object_id< protocol_ids, witness_object_type, witness_object> witness_id_type; - typedef object_id< protocol_ids, limit_order_object_type, limit_order_object> limit_order_id_type; - typedef object_id< protocol_ids, call_order_object_type, call_order_object> call_order_id_type; - typedef object_id< protocol_ids, custom_object_type, custom_object> custom_id_type; - typedef object_id< protocol_ids, proposal_object_type, proposal_object> proposal_id_type; - typedef object_id< protocol_ids, operation_history_object_type, operation_history_object> operation_history_id_type; - typedef object_id< protocol_ids, withdraw_permission_object_type,withdraw_permission_object> withdraw_permission_id_type; - typedef object_id< protocol_ids, vesting_balance_object_type, vesting_balance_object> vesting_balance_id_type; - typedef object_id< protocol_ids, worker_object_type, worker_object> worker_id_type; - typedef object_id< protocol_ids, balance_object_type, balance_object> balance_id_type; - typedef object_id< protocol_ids, tournament_object_type, tournament_object> tournament_id_type; - typedef object_id< protocol_ids, tournament_details_object_type, tournament_details_object> tournament_details_id_type; - typedef object_id< protocol_ids, match_object_type, match_object> match_id_type; - typedef object_id< protocol_ids, game_object_type, game_object> game_id_type; - typedef object_id< protocol_ids, sport_object_type, sport_object> sport_id_type; - typedef object_id< protocol_ids, event_group_object_type, event_group_object> event_group_id_type; - typedef object_id< protocol_ids, event_object_type, event_object> event_id_type; - typedef object_id< protocol_ids, betting_market_rules_object_type, betting_market_rules_object> betting_market_rules_id_type; - typedef object_id< protocol_ids, betting_market_group_object_type, betting_market_group_object> betting_market_group_id_type; - typedef object_id< protocol_ids, betting_market_object_type, betting_market_object> betting_market_id_type; - typedef object_id< protocol_ids, bet_object_type, bet_object> bet_id_type; - typedef object_id< protocol_ids, info_for_vout_object_type, info_for_vout_object> info_for_vout_id_type; - typedef object_id< protocol_ids, bitcoin_address_object_type, bitcoin_address_object> bitcoin_address_id_type; - typedef object_id< protocol_ids, primary_wallet_vout_object_type,primary_wallet_vout_object> primary_wallet_vout_id_type; - typedef object_id< protocol_ids, sidechain_proposal_object_type, sidechain_proposal_object> sidechain_proposal_id_type; + typedef object_id< protocol_ids, account_object_type, account_object> account_id_type; + typedef object_id< protocol_ids, asset_object_type, asset_object> asset_id_type; + typedef object_id< protocol_ids, force_settlement_object_type, force_settlement_object> force_settlement_id_type; + typedef object_id< protocol_ids, committee_member_object_type, committee_member_object> committee_member_id_type; + typedef object_id< protocol_ids, witness_object_type, witness_object> witness_id_type; + typedef object_id< protocol_ids, limit_order_object_type, limit_order_object> limit_order_id_type; + typedef object_id< protocol_ids, call_order_object_type, call_order_object> call_order_id_type; + typedef object_id< protocol_ids, custom_object_type, custom_object> custom_id_type; + typedef object_id< protocol_ids, proposal_object_type, proposal_object> proposal_id_type; + typedef object_id< protocol_ids, operation_history_object_type, operation_history_object> operation_history_id_type; + typedef object_id< protocol_ids, withdraw_permission_object_type, withdraw_permission_object> withdraw_permission_id_type; + typedef object_id< protocol_ids, vesting_balance_object_type, vesting_balance_object> vesting_balance_id_type; + typedef object_id< protocol_ids, worker_object_type, worker_object> worker_id_type; + typedef object_id< protocol_ids, balance_object_type, balance_object> balance_id_type; + typedef object_id< protocol_ids, tournament_object_type, tournament_object> tournament_id_type; + typedef object_id< protocol_ids, tournament_details_object_type, tournament_details_object> tournament_details_id_type; + typedef object_id< protocol_ids, match_object_type, match_object> match_id_type; + typedef object_id< protocol_ids, game_object_type, game_object> game_id_type; + typedef object_id< protocol_ids, sport_object_type, sport_object> sport_id_type; + typedef object_id< protocol_ids, event_group_object_type, event_group_object> event_group_id_type; + typedef object_id< protocol_ids, event_object_type, event_object> event_id_type; + typedef object_id< protocol_ids, betting_market_rules_object_type, betting_market_rules_object> betting_market_rules_id_type; + typedef object_id< protocol_ids, betting_market_group_object_type, betting_market_group_object> betting_market_group_id_type; + typedef object_id< protocol_ids, betting_market_object_type, betting_market_object> betting_market_id_type; + typedef object_id< protocol_ids, bet_object_type, bet_object> bet_id_type; + typedef object_id< protocol_ids, info_for_vout_object_type, info_for_vout_object> info_for_vout_id_type; + typedef object_id< protocol_ids, bitcoin_address_object_type, bitcoin_address_object> bitcoin_address_id_type; + typedef object_id< protocol_ids, primary_wallet_vout_object_type, primary_wallet_vout_object> primary_wallet_vout_id_type; + typedef object_id< protocol_ids, sidechain_proposal_object_type, sidechain_proposal_object> sidechain_proposal_id_type; + typedef object_id< protocol_ids, bitcoin_transaction_object_type, bitcoin_transaction_object> bitcoin_transaction_id_type; // implementation types class global_property_object; @@ -418,6 +421,7 @@ FC_REFLECT_ENUM( graphene::chain::object_type, (bitcoin_address_object_type) (primary_wallet_vout_object_type) (sidechain_proposal_object_type) + (bitcoin_transaction_object_type) (OBJECT_TYPE_COUNT) ) FC_REFLECT_ENUM( graphene::chain::impl_object_type, @@ -473,6 +477,7 @@ FC_REFLECT_TYPENAME( graphene::chain::info_for_vout_id_type ) FC_REFLECT_TYPENAME( graphene::chain::bitcoin_address_id_type ) FC_REFLECT_TYPENAME( graphene::chain::primary_wallet_vout_id_type ) FC_REFLECT_TYPENAME( graphene::chain::sidechain_proposal_id_type ) +FC_REFLECT_TYPENAME( graphene::chain::bitcoin_transaction_id_type ) FC_REFLECT_TYPENAME( graphene::chain::global_property_id_type ) FC_REFLECT_TYPENAME( graphene::chain::dynamic_global_property_id_type ) FC_REFLECT_TYPENAME( graphene::chain::asset_dynamic_data_id_type ) diff --git a/libraries/net/CMakeLists.txt b/libraries/net/CMakeLists.txt index 39f9cd05..35a12eae 100644 --- a/libraries/net/CMakeLists.txt +++ b/libraries/net/CMakeLists.txt @@ -10,7 +10,7 @@ set(SOURCES node.cpp add_library( graphene_net ${SOURCES} ${HEADERS} ) target_link_libraries( graphene_net - PUBLIC fc graphene_db ) + PUBLIC fc graphene_db sidechain ) target_include_directories( graphene_net PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../chain/include" diff --git a/libraries/sidechain/include/sidechain/primary_wallet_vout_manager.hpp b/libraries/sidechain/include/sidechain/primary_wallet_vout_manager.hpp index d569669d..abe8c9c8 100644 --- a/libraries/sidechain/include/sidechain/primary_wallet_vout_manager.hpp +++ b/libraries/sidechain/include/sidechain/primary_wallet_vout_manager.hpp @@ -1,10 +1,11 @@ #pragma once -#include -#include - +#include +#include #include +namespace graphene { namespace chain { class database; } } + namespace sidechain { class primary_wallet_vout_manager @@ -16,18 +17,20 @@ public: bool is_reach_max_unconfirmaed_vout() const; fc::optional< graphene::chain::primary_wallet_vout_object > get_latest_unused_vout() const; + + fc::optional< graphene::chain::primary_wallet_vout_object > get_vout( fc::sha256 hash_id ) const; void create_new_vout( const sidechain::prev_out& out ); - void delete_vout_with_newer( fc::uint256 hash_id ); + void delete_vout_with_newer( fc::sha256 hash_id ); - void confirm_vout( fc::uint256 hash_id ); + void confirm_vout( fc::sha256 hash_id ); - void use_latest_vout( fc::uint256 hash_id ); + void use_latest_vout( fc::sha256 hash_id ); private: - fc::optional< graphene::chain::primary_wallet_vout_id_type > get_vout_id( fc::uint256 hash_id ) const; + fc::optional< graphene::chain::primary_wallet_vout_id_type > get_vout_id( fc::sha256 hash_id ) const; graphene::chain::database& db; diff --git a/libraries/sidechain/include/sidechain/types.hpp b/libraries/sidechain/include/sidechain/types.hpp index 501c2bff..78a28fc6 100644 --- a/libraries/sidechain/include/sidechain/types.hpp +++ b/libraries/sidechain/include/sidechain/types.hpp @@ -5,7 +5,7 @@ #include #include -#include +#include namespace sidechain { diff --git a/libraries/sidechain/network/bitcoin_rpc_client.cpp b/libraries/sidechain/network/bitcoin_rpc_client.cpp index b9f25d2b..850b0178 100644 --- a/libraries/sidechain/network/bitcoin_rpc_client.cpp +++ b/libraries/sidechain/network/bitcoin_rpc_client.cpp @@ -100,7 +100,7 @@ std::string bitcoin_rpc_client::send_btc_tx( const std::string& tx_hex ) const auto reply = send_post_request( body ); - if( reply.body.empty() ) + if( reply.body.empty() || reply.status != 200 ) return ""; std::string reply_str( reply.body.begin(), reply.body.end() ); diff --git a/libraries/sidechain/network/include/sidechain/network/sidechain_net_manager.hpp b/libraries/sidechain/network/include/sidechain/network/sidechain_net_manager.hpp index 7be2cdd0..f010877a 100644 --- a/libraries/sidechain/network/include/sidechain/network/sidechain_net_manager.hpp +++ b/libraries/sidechain/network/include/sidechain/network/sidechain_net_manager.hpp @@ -25,7 +25,7 @@ public: void update_estimated_fee(); - void send_btc_tx(); + void send_btc_tx( const sidechain::bitcoin_transaction& trx ); bool connection_is_not_defined() const; diff --git a/libraries/sidechain/network/sidechain_net_manager.cpp b/libraries/sidechain/network/sidechain_net_manager.cpp index 085ea540..60bf84c2 100644 --- a/libraries/sidechain/network/sidechain_net_manager.cpp +++ b/libraries/sidechain/network/sidechain_net_manager.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -32,7 +33,11 @@ void sidechain_net_manager::initialize_manager( graphene::chain::database* _db, } listener->block_received.connect([this]( const std::string& block_hash ) { - std::thread( &sidechain_net_manager::handle_block, this, block_hash).detach(); + std::thread( &sidechain_net_manager::handle_block, this, block_hash ).detach(); + } ); + + db->send_btc_tx.connect([this]( const sidechain::bitcoin_transaction& trx ) { + std::thread( &sidechain_net_manager::send_btc_tx, this, trx ).detach(); } ); } @@ -98,9 +103,30 @@ void sidechain_net_manager::update_estimated_fee() auto estimated_fee = bitcoin_client->receive_estimated_fee(); } -void sidechain_net_manager::send_btc_tx() +void sidechain_net_manager::send_btc_tx( const sidechain::bitcoin_transaction& trx ) { FC_ASSERT( !bitcoin_client->connection_is_not_defined() ); + const auto tx_hex = fc::to_hex( pack( trx ) ); + idump((tx_hex)); + + auto reply = bitcoin_client->send_btc_tx( tx_hex ); + + std::stringstream ss(reply); + boost::property_tree::ptree json; + boost::property_tree::read_json(ss, json); + + if( !json.get_child( "error" ).empty() ) { + wlog( "BTC tx is not sent! Reply: ${msg}", ("msg", reply) ); + const auto error_code = json.get_child( "error" ).get_child( "code" ).get_value(); + + if( error_code != -27 ) // transaction already in block chain + return; + } + + if( reply == "" ){ + wlog( "Don't receive successful reply status" ); + return; + } } bool sidechain_net_manager::connection_is_not_defined() const diff --git a/libraries/sidechain/primary_wallet_vout_manager.cpp b/libraries/sidechain/primary_wallet_vout_manager.cpp index b7ae9aef..6c98e7c1 100644 --- a/libraries/sidechain/primary_wallet_vout_manager.cpp +++ b/libraries/sidechain/primary_wallet_vout_manager.cpp @@ -1,6 +1,9 @@ #include +#include +#include #include + namespace sidechain { bool primary_wallet_vout_manager::is_reach_max_unconfirmaed_vout() const @@ -21,6 +24,15 @@ fc::optional< graphene::chain::primary_wallet_vout_object > primary_wallet_vout_ return fc::optional< graphene::chain::primary_wallet_vout_object > (*itr); } +fc::optional< graphene::chain::primary_wallet_vout_object > primary_wallet_vout_manager::get_vout( fc::sha256 hash_id ) const +{ + const auto& PW_vout_by_hash_id = db.get_index_type().indices().get< graphene::chain::by_hash_id >(); + const auto& itr_hash_id = PW_vout_by_hash_id.find( hash_id ); + if( itr_hash_id == PW_vout_by_hash_id.end() ) + return fc::optional< graphene::chain::primary_wallet_vout_object >(); + return fc::optional< graphene::chain::primary_wallet_vout_object >( *itr_hash_id ); +} + void primary_wallet_vout_manager::create_new_vout( const sidechain::prev_out& out ) { db.create([&]( graphene::chain::primary_wallet_vout_object& obj ) { @@ -31,7 +43,7 @@ void primary_wallet_vout_manager::create_new_vout( const sidechain::prev_out& ou }); } -void primary_wallet_vout_manager::delete_vout_with_newer( fc::uint256 hash_id ) +void primary_wallet_vout_manager::delete_vout_with_newer( fc::sha256 hash_id ) { const auto& PW_vout_by_id = db.get_index_type().indices().get< graphene::chain::by_id >(); auto vout_id = get_vout_id( hash_id ); @@ -48,7 +60,7 @@ void primary_wallet_vout_manager::delete_vout_with_newer( fc::uint256 hash_id ) } } -void primary_wallet_vout_manager::confirm_vout( fc::uint256 hash_id ) +void primary_wallet_vout_manager::confirm_vout( fc::sha256 hash_id ) { const auto& PW_vout_by_id = db.get_index_type().indices().get< graphene::chain::by_id >(); auto vout_id = get_vout_id( hash_id ); @@ -67,7 +79,7 @@ void primary_wallet_vout_manager::confirm_vout( fc::uint256 hash_id ) } } -void primary_wallet_vout_manager::use_latest_vout( fc::uint256 hash_id ) +void primary_wallet_vout_manager::use_latest_vout( fc::sha256 hash_id ) { const auto& PW_vout_by_id = db.get_index_type().indices().get< graphene::chain::by_id >(); auto vout_id = get_vout_id( hash_id ); @@ -85,7 +97,7 @@ void primary_wallet_vout_manager::use_latest_vout( fc::uint256 hash_id ) } } -fc::optional< graphene::chain::primary_wallet_vout_id_type > primary_wallet_vout_manager::get_vout_id( fc::uint256 hash_id ) const +fc::optional< graphene::chain::primary_wallet_vout_id_type > primary_wallet_vout_manager::get_vout_id( fc::sha256 hash_id ) const { const auto& PW_vout_by_hash_id = db.get_index_type().indices().get< graphene::chain::by_hash_id >(); const auto& itr_hash_id = PW_vout_by_hash_id.find( hash_id ); diff --git a/programs/js_operation_serializer/main.cpp b/programs/js_operation_serializer/main.cpp index 8994b36b..baa048d6 100644 --- a/programs/js_operation_serializer/main.cpp +++ b/programs/js_operation_serializer/main.cpp @@ -43,6 +43,10 @@ #include #include #include +#include +#include +#include +#include #include #include diff --git a/tests/tests/primary_wallet_vout_manager_tests.cpp b/tests/tests/primary_wallet_vout_manager_tests.cpp index 2159b0ab..30dcca58 100644 --- a/tests/tests/primary_wallet_vout_manager_tests.cpp +++ b/tests/tests/primary_wallet_vout_manager_tests.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "../common/database_fixture.hpp" #include