BitShares-Core  6.1.0
BitShares blockchain implementation and command-line interface software
node.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cryptonomex, Inc., and contributors.
3  *
4  * The MIT License
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #pragma once
25 
27 #include <graphene/net/message.hpp>
29 
31 
32 namespace graphene { namespace net {
33 
34  using fc::variant_object;
36 
37  namespace detail
38  {
39  class node_impl;
40  }
41  using node_impl_ptr = std::shared_ptr<detail::node_impl>;
42 
43  // during network development, we need to track message propagation across the network
44  // using a structure like this:
46  {
50  };
51 
57  {
58  public:
59  virtual ~node_delegate() = default;
60 
64  virtual bool has_item( const net::item_id& id ) = 0;
65 
77  virtual bool handle_block( const graphene::net::block_message& blk_msg, bool sync_mode,
78  std::vector<message_hash_type>& contained_transaction_msg_ids ) = 0;
79 
86  virtual void handle_transaction( const graphene::net::trx_message& trx_msg ) = 0;
87 
96  virtual void handle_message( const message& message_to_process ) = 0;
97 
105  virtual std::vector<item_hash_t> get_block_ids(const std::vector<item_hash_t>& blockchain_synopsis,
106  uint32_t& remaining_item_count,
107  uint32_t limit = 2000) = 0;
108 
112  virtual message get_item( const item_id& id ) = 0;
113 
114  virtual chain_id_type get_chain_id()const = 0;
115 
133  virtual std::vector<item_hash_t> get_blockchain_synopsis(const item_hash_t& reference_point,
134  uint32_t number_of_blocks_after_reference_point) = 0;
135 
144  virtual void sync_status( uint32_t item_type, uint32_t item_count ) = 0;
145 
149  virtual void connection_count_changed( uint32_t c ) = 0;
150 
151  virtual uint32_t get_block_number(const item_hash_t& block_id) = 0;
152 
157  virtual fc::time_point_sec get_block_time(const item_hash_t& block_id) = 0;
158 
159  virtual item_hash_t get_head_block_id() const = 0;
160 
161  virtual uint32_t estimate_last_known_fork_from_git_revision_timestamp(uint32_t unix_timestamp) const = 0;
162 
163  virtual void error_encountered(const std::string& message, const fc::oexception& error) = 0;
164  virtual uint8_t get_current_block_interval_in_seconds() const = 0;
165 
166  };
167 
172  struct peer_status
173  {
174  uint32_t version;
179  };
180 
190  class node : public std::enable_shared_from_this<node>
191  {
192  public:
193  explicit node(const std::string& user_agent);
194  virtual ~node();
195 
196  void close() const;
197 
198  void set_node_delegate( std::shared_ptr<node_delegate> del ) const;
199 
200  void load_configuration( const fc::path& configuration_directory ) const;
201 
202  virtual void listen_to_p2p_network() const;
203  virtual void connect_to_p2p_network() const;
204 
210  void add_node( const fc::ip::endpoint& ep ) const;
211 
212  /*****
213  * @brief add a list of nodes to seed the p2p network
214  * @param seeds a vector of url strings
215  */
216  void add_seed_nodes( const std::vector<std::string>& seeds ) const;
217 
218  /****
219  * @brief add a node to seed the p2p network
220  * @param in the url as a string
221  */
222  void add_seed_node( const std::string& in) const;
223 
227  virtual void connect_to_endpoint( const fc::ip::endpoint& ep ) const;
228 
235  void set_listen_endpoint( const fc::ip::endpoint& ep, bool wait_if_not_available ) const;
236 
242  void set_inbound_endpoint( const fc::ip::endpoint& ep ) const;
243 
248  void set_accept_incoming_connections( bool accept ) const;
249 
250  /***
251  * Enable or disable connection attempts when new connections are advertised
252  * @param connect true to attempt new connections, false otherwise
253  */
254  void set_connect_to_new_peers( bool connect ) const;
255 
261  virtual fc::ip::endpoint get_actual_listening_endpoint() const;
262 
263  /***
264  * Allows the caller to determine how to respond to requests for peers
265  * @param algo the algorithm to use ("exclude_list", "list", "nothing", "all")
266  * @param advertise_or_exclude_list a list of nodes to
267  * advertise (if algo is "list") or exclude (if algo is "exclude_list")
268  */
269  void set_advertise_algorithm( const std::string& algo,
270  const std::vector<std::string>& advertise_or_exclude_list = std::vector<std::string>() ) const;
271 
275  std::vector<peer_status> get_connected_peers() const;
276 
278  virtual uint32_t get_connection_count() const;
279 
284  virtual void broadcast( const message& item_to_broadcast ) const;
285  virtual void broadcast_transaction( const signed_transaction& trx ) const
286  {
287  broadcast( trx_message(trx) );
288  }
289 
294  virtual void sync_from( const item_id& current_head_block,
295  const std::vector<uint32_t>& hard_fork_block_numbers ) const;
296 
297  bool is_connected() const;
298 
299  void set_advanced_node_parameters(const fc::variant_object& params) const;
300  fc::variant_object get_advanced_node_parameters() const;
301  message_propagation_data get_tx_propagation_data(
302  const graphene::protocol::transaction_id_type& transaction_id) const;
303  message_propagation_data get_block_propagation_data(
304  const graphene::protocol::block_id_type& block_id) const;
305  node_id_t get_node_id() const;
306  void set_allowed_peers(const std::vector<node_id_t>& allowed_peers) const;
307 
312  void clear_peer_database() const;
313 
314  void set_total_bandwidth_limit(uint32_t upload_bytes_per_second, uint32_t download_bytes_per_second) const;
315 
316  fc::variant_object network_get_info() const;
317  fc::variant_object network_get_usage_stats() const;
318 
319  std::vector<potential_peer_record> get_potential_peers() const;
320 
321  fc::variant_object get_call_statistics() const;
322  protected:
324  };
325 
326  using node_ptr = std::shared_ptr<node>;
327 
328 } } // graphene::net
329 
330 FC_REFLECT(graphene::net::message_propagation_data, (received_time)(validated_time)(originating_peer));
331 FC_REFLECT( graphene::net::peer_status, (version)(host)(info) );
fc::sha256 chain_id_type
Definition: types.hpp:149
std::shared_ptr< detail::node_impl > node_impl_ptr
Definition: node.hpp:41
An order-perserving dictionary of variant&#39;s.
Definition: api.cpp:48
fc::ip::endpoint host
Definition: node.hpp:175
virtual void broadcast_transaction(const signed_transaction &trx) const
Definition: node.hpp:285
used by node reports status to client or fetch data from client
Definition: node.hpp:56
FC_REFLECT(graphene::net::message_propagation_data,(received_time)(validated_time)(originating_peer))
std::shared_ptr< node > node_ptr
Definition: node.hpp:326
void connect(AsyncSocket &sock, const EndpointType &ep)
wraps boost::asio::socket::async_connect
Definition: asio.hpp:262
void accept(AcceptorType &acc, SocketType &sock)
wraps boost::asio::async_accept
Definition: asio.hpp:250
provides stack-based nullable value similar to boost::optional
Definition: optional.hpp:20
provides application independent P2P broadcast and data synchronization
Definition: node.hpp:190
fc::variant_object info
Definition: node.hpp:178
adds a signature to a transaction
node_impl_ptr my
Definition: node.hpp:323
wraps boost::filesystem::path to provide platform independent path manipulation.
Definition: filesystem.hpp:28