Split network API into network_broadcast_api and network_node_api #118

This commit is contained in:
theoreticalbts 2015-07-06 14:36:06 -04:00
parent 9b5bd12c67
commit 1dd7316d44
3 changed files with 94 additions and 44 deletions

View file

@ -350,20 +350,22 @@ namespace graphene { namespace app {
bool login_api::login(const string& user, const string& password) bool login_api::login(const string& user, const string& password)
{ {
auto db_api = std::make_shared<database_api>(std::ref(*_app.chain_database())); auto db_api = std::make_shared<database_api>(std::ref(*_app.chain_database()));
auto net_api = std::make_shared<network_api>(std::ref(_app)); auto net_broadcast_api = std::make_shared<network_broadcast_api>(std::ref(_app));
auto hist_api = std::make_shared<history_api>(_app); auto hist_api = std::make_shared<history_api>(_app);
auto net_node_api = std::make_shared<network_node_api>(std::ref(_app));
_database_api = db_api; _database_api = db_api;
_network_api = net_api; _network_broadcast_api = net_broadcast_api;
_history_api = hist_api; _history_api = hist_api;
_network_node_api = net_node_api;
return true; return true;
} }
network_api::network_api(application& a):_app(a) network_broadcast_api::network_broadcast_api(application& a):_app(a)
{ {
_applied_block_connection = _app.chain_database()->applied_block.connect([this](const signed_block& b){ on_applied_block(b); }); _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 ) void network_broadcast_api::on_applied_block( const signed_block& b )
{ {
if( _callbacks.size() ) if( _callbacks.size() )
{ {
@ -381,18 +383,14 @@ namespace graphene { namespace app {
} }
} }
void network_api::add_node(const fc::ip::endpoint& ep) void network_broadcast_api::broadcast_transaction(const signed_transaction& trx)
{
_app.p2p_node()->add_node(ep);
}
void network_api::broadcast_transaction(const signed_transaction& trx)
{ {
trx.validate(); trx.validate();
_app.chain_database()->push_transaction(trx); _app.chain_database()->push_transaction(trx);
_app.p2p_node()->broadcast_transaction(trx); _app.p2p_node()->broadcast_transaction(trx);
} }
void network_api::broadcast_transaction_with_callback( confirmation_callback cb, const signed_transaction& trx)
void network_broadcast_api::broadcast_transaction_with_callback( confirmation_callback cb, const signed_transaction& trx)
{ {
trx.validate(); trx.validate();
_callbacks[trx.id()] = cb; _callbacks[trx.id()] = cb;
@ -400,16 +398,30 @@ namespace graphene { namespace app {
_app.p2p_node()->broadcast_transaction(trx); _app.p2p_node()->broadcast_transaction(trx);
} }
network_node_api::network_node_api( application& a ) : _app( a )
{
}
std::vector<net::peer_status> network_api::get_connected_peers() const void network_node_api::add_node(const fc::ip::endpoint& ep)
{
_app.p2p_node()->add_node(ep);
}
std::vector<net::peer_status> network_node_api::get_connected_peers() const
{ {
return _app.p2p_node()->get_connected_peers(); return _app.p2p_node()->get_connected_peers();
} }
fc::api<network_api> login_api::network()const fc::api<network_broadcast_api> login_api::network_broadcast()const
{ {
FC_ASSERT(_network_api); FC_ASSERT(_network_broadcast_api);
return *_network_api; return *_network_broadcast_api;
}
fc::api<network_node_api> login_api::network_node()const
{
FC_ASSERT(_network_node_api);
return *_network_node_api;
} }
fc::api<database_api> login_api::database()const fc::api<database_api> login_api::database()const

View file

@ -45,7 +45,7 @@ namespace graphene { namespace app {
* *
* This API exposes accessors on the database which query state tracked by a blockchain validating node. This API is * This API exposes accessors on the database which query state tracked by a blockchain validating node. This API is
* read-only; all modifications to the database must be performed via transactions. Transactions are broadcast via * read-only; all modifications to the database must be performed via transactions. Transactions are broadcast via
* the @ref network_api. * the @ref network_broadcast_api.
*/ */
class database_api class database_api
{ {
@ -332,14 +332,12 @@ namespace graphene { namespace app {
}; };
/** /**
* @brief The network_api class implements the RPC API for the network * @brief The network_broadcast_api class allows broadcasting of transactions.
*
* This API has methods to query the network status, connect to new peers, and send transactions.
*/ */
class network_api class network_broadcast_api
{ {
public: public:
network_api(application& a); network_broadcast_api(application& a);
struct transaction_confirmation struct transaction_confirmation
{ {
@ -367,15 +365,13 @@ namespace graphene { namespace app {
void broadcast_transaction_with_callback( confirmation_callback cb, const signed_transaction& trx); void broadcast_transaction_with_callback( confirmation_callback cb, const signed_transaction& trx);
/** /**
* @brief add_node Connect to a new peer * @brief Not reflected, thus not accessible to API clients.
* @param ep The IP/Port of the peer to connect to *
* This function is registered to receive the applied_block
* signal from the chain database when a block is received.
* It then dispatches callbacks to clients who have requested
* to be notified when a particular txid is included in a block.
*/ */
void add_node(const fc::ip::endpoint& ep);
/**
* @brief Get status of all current connections to peers
*/
std::vector<net::peer_status> get_connected_peers() const;
void on_applied_block( const signed_block& b ); void on_applied_block( const signed_block& b );
private: private:
boost::signals2::scoped_connection _applied_block_connection; boost::signals2::scoped_connection _applied_block_connection;
@ -383,6 +379,35 @@ namespace graphene { namespace app {
application& _app; application& _app;
}; };
/**
* @brief The network_node_api class allows maintenance of p2p connections.
*/
class network_node_api
{
public:
network_node_api(application& a);
/**
* @brief add_node Connect to a new peer
* @param ep The IP/Port of the peer to connect to
*/
void add_node(const fc::ip::endpoint& ep);
/**
* @brief Get status of all current connections to peers
* @brief Not reflected, thus not accessible to API clients.
*
* This function is registered to receive the applied_block
* signal from the chain database when a block is received.
* It then dispatches callbacks to clients who have requested
* to be notified when a particular txid is included in a block.
*/
std::vector<net::peer_status> get_connected_peers() const;
private:
application& _app;
};
/** /**
* @brief The login_api class implements the bottom layer of the RPC API * @brief The login_api class implements the bottom layer of the RPC API
* *
@ -404,23 +429,26 @@ namespace graphene { namespace app {
* has sucessfully authenticated. * has sucessfully authenticated.
*/ */
bool login(const string& user, const string& password); bool login(const string& user, const string& password);
/// @brief Retrieve the network API /// @brief Retrieve the network broadcast API
fc::api<network_api> network()const; fc::api<network_broadcast_api> network_broadcast()const;
/// @brief Retrieve the database API /// @brief Retrieve the database API
fc::api<database_api> database()const; fc::api<database_api> database()const;
/// @brief Retrieve the history API /// @brief Retrieve the history API
fc::api<history_api> history()const; fc::api<history_api> history()const;
/// @brief Retrieve the network node API
fc::api<network_node_api> network_node()const;
private: private:
application& _app; application& _app;
optional< fc::api<database_api> > _database_api; optional< fc::api<database_api> > _database_api;
optional< fc::api<network_api> > _network_api; optional< fc::api<network_broadcast_api> > _network_broadcast_api;
optional< fc::api<network_node_api> > _network_node_api;
optional< fc::api<history_api> > _history_api; optional< fc::api<history_api> > _history_api;
}; };
}} // graphene::app }} // graphene::app
FC_REFLECT( graphene::app::network_api::transaction_confirmation, FC_REFLECT( graphene::app::network_broadcast_api::transaction_confirmation,
(id)(block_num)(trx_num)(trx) ) (id)(block_num)(trx_num)(trx) )
FC_API(graphene::app::database_api, FC_API(graphene::app::database_api,
@ -461,13 +489,23 @@ FC_API(graphene::app::database_api,
(get_margin_positions) (get_margin_positions)
(get_balance_objects) (get_balance_objects)
) )
FC_API(graphene::app::history_api, (get_account_history)(get_market_history)(get_market_history_buckets)) FC_API(graphene::app::history_api,
FC_API(graphene::app::network_api, (broadcast_transaction)(broadcast_transaction_with_callback) (get_account_history)
/* (add_node)(get_connected_peers) */ (get_market_history)
) (get_market_history_buckets)
)
FC_API(graphene::app::network_broadcast_api,
(broadcast_transaction)
(broadcast_transaction_with_callback)
)
FC_API(graphene::app::network_node_api,
(add_node)
(get_connected_peers)
)
FC_API(graphene::app::login_api, FC_API(graphene::app::login_api,
(login) (login)
(network) (network_broadcast)
(database) (database)
(history) (history)
(network_node)
) )

View file

@ -301,7 +301,7 @@ public:
: self(s), : self(s),
_remote_api(rapi), _remote_api(rapi),
_remote_db(rapi->database()), _remote_db(rapi->database()),
_remote_net(rapi->network()), _remote_net_broadcast(rapi->network_broadcast()),
_remote_hist(rapi->history()) _remote_hist(rapi->history())
{ {
_remote_db->subscribe_to_objects( [=]( const fc::variant& obj ) _remote_db->subscribe_to_objects( [=]( const fc::variant& obj )
@ -737,7 +737,7 @@ public:
} }
if( broadcast ) if( broadcast )
_remote_net->broadcast_transaction( tx ); _remote_net_broadcast->broadcast_transaction( tx );
return tx; return tx;
} FC_CAPTURE_AND_RETHROW( (name)(owner)(active)(registrar_account)(referrer_account)(referrer_percent)(broadcast) ) } } FC_CAPTURE_AND_RETHROW( (name)(owner)(active)(registrar_account)(referrer_account)(referrer_percent)(broadcast) ) }
@ -863,7 +863,7 @@ public:
if( save_wallet ) if( save_wallet )
save_wallet_file(); save_wallet_file();
if( broadcast ) if( broadcast )
_remote_net->broadcast_transaction( tx ); _remote_net_broadcast->broadcast_transaction( tx );
return tx; return tx;
} FC_CAPTURE_AND_RETHROW( (account_name)(registrar_account)(referrer_account)(broadcast) ) } } FC_CAPTURE_AND_RETHROW( (account_name)(registrar_account)(referrer_account)(broadcast) ) }
@ -1446,7 +1446,7 @@ public:
} }
if( broadcast ) if( broadcast )
_remote_net->broadcast_transaction( tx ); _remote_net_broadcast->broadcast_transaction( tx );
return tx; return tx;
} }
@ -1691,7 +1691,7 @@ public:
fc::api<login_api> _remote_api; fc::api<login_api> _remote_api;
fc::api<database_api> _remote_db; fc::api<database_api> _remote_db;
fc::api<network_api> _remote_net; fc::api<network_broadcast_api> _remote_net_broadcast;
fc::api<history_api> _remote_hist; fc::api<history_api> _remote_hist;
#ifdef __unix__ #ifdef __unix__
@ -2436,7 +2436,7 @@ signed_transaction wallet_api::import_balance( string name_or_id, const vector<s
boost::erase(tx.signatures, boost::unique<boost::return_found_end>(boost::sort(tx.signatures))); boost::erase(tx.signatures, boost::unique<boost::return_found_end>(boost::sort(tx.signatures)));
if( broadcast ) if( broadcast )
my->_remote_net->broadcast_transaction(tx); my->_remote_net_broadcast->broadcast_transaction(tx);
return tx; return tx;
} FC_CAPTURE_AND_RETHROW( (name_or_id) ) } } FC_CAPTURE_AND_RETHROW( (name_or_id) ) }