From 9dc7189dd681e652a9f15cf70099a4f7ec33f533 Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Tue, 30 Jun 2015 17:28:16 -0400 Subject: [PATCH] adding ability to receive a callback when a broadcast transaction is actually included in a block --- libraries/app/api.cpp | 31 ++++++++++++++++++++++ libraries/app/include/graphene/app/api.hpp | 29 +++++++++++++++++--- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/libraries/app/api.cpp b/libraries/app/api.cpp index 43f656ff..adde1683 100644 --- a/libraries/app/api.cpp +++ b/libraries/app/api.cpp @@ -293,6 +293,29 @@ namespace graphene { namespace app { return true; } + network_api::network_api(application& a):_app(a) + { + _applied_block_connection = _app.chain_database()->applied_block.connect([this](const signed_block& b){ on_applied_block(b); }); + } + + void network_api::on_applied_block( const signed_block& b ) + { + if( _callbacks.size() ) + { + for( uint32_t trx_num = 0; trx_num < b.transactions.size(); ++trx_num ) + { + const auto& trx = b.transactions[trx_num]; + auto id = trx.id(); + auto itr = _callbacks.find(id); + auto block_num = b.block_num(); + if( itr != _callbacks.end() ) + { + fc::async( [=](){ itr->second( fc::variant(transaction_confirmation{ id, block_num, trx_num, trx}) ); } ); + } + } + } + } + void network_api::add_node(const fc::ip::endpoint& ep) { _app.p2p_node()->add_node(ep); @@ -304,6 +327,14 @@ namespace graphene { namespace app { _app.chain_database()->push_transaction(trx); _app.p2p_node()->broadcast_transaction(trx); } + void network_api::broadcast_transaction_with_callback( confirmation_callback cb, const signed_transaction& trx) + { + trx.validate(); + _callbacks[trx.id()] = cb; + _app.chain_database()->push_transaction(trx); + _app.p2p_node()->broadcast_transaction(trx); + } + std::vector network_api::get_connected_peers() const { diff --git a/libraries/app/include/graphene/app/api.hpp b/libraries/app/include/graphene/app/api.hpp index 37402dc5..ff488809 100644 --- a/libraries/app/include/graphene/app/api.hpp +++ b/libraries/app/include/graphene/app/api.hpp @@ -313,7 +313,17 @@ namespace graphene { namespace app { class network_api { public: - network_api(application& a):_app(a){} + network_api(application& a); + + struct transaction_confirmation + { + transaction_id_type id; + uint32_t block_num; + uint32_t trx_num; + processed_transaction trx; + }; + + typedef std::function confirmation_callback; /** * @brief Broadcast a transaction to the network @@ -323,6 +333,13 @@ namespace graphene { namespace app { * apply locally, an error will be thrown and the transaction will not be broadcast. */ void broadcast_transaction(const signed_transaction& trx); + + /** this version of broadcast transaction registers a callback method that will be called when the transaction is + * included into a block. The callback method includes the transaction id, block number, and transaction number in the + * block. + */ + void broadcast_transaction_with_callback( confirmation_callback cb, const signed_transaction& trx); + /** * @brief add_node Connect to a new peer * @param ep The IP/Port of the peer to connect to @@ -333,8 +350,11 @@ namespace graphene { namespace app { */ std::vector get_connected_peers() const; + void on_applied_block( const signed_block& b ); private: - application& _app; + boost::signals2::scoped_connection _applied_block_connection; + map _callbacks; + application& _app; }; /** @@ -374,6 +394,9 @@ namespace graphene { namespace app { }} // graphene::app +FC_REFLECT( graphene::app::network_api::transaction_confirmation, + (id)(block_num)(trx_num)(trx) ) + FC_API(graphene::app::database_api, (get_objects) (get_block_header) @@ -409,7 +432,7 @@ FC_API(graphene::app::database_api, (get_balance_objects) ) FC_API(graphene::app::history_api, (get_account_history)(get_market_history)(get_market_history_buckets)) -FC_API(graphene::app::network_api, (broadcast_transaction)(add_node)(get_connected_peers)) +FC_API(graphene::app::network_api, (broadcast_transaction)(broadcast_transaction_with_callback)(add_node)(get_connected_peers)) FC_API(graphene::app::login_api, (login) (network)