BitShares-Core  4.0.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 #include <list>
33 
34 namespace graphene { namespace net {
35 
36  using fc::variant_object;
38 
39  namespace detail
40  {
41  class node_impl;
43  {
44  void operator()(node_impl*);
45  };
46  }
47 
48  // during network development, we need to track message propagation across the network
49  // using a structure like this:
51  {
55  };
56 
62  {
63  public:
64  virtual ~node_delegate(){}
65 
69  virtual bool has_item( const net::item_id& id ) = 0;
70 
80  virtual bool handle_block( const graphene::net::block_message& blk_msg, bool sync_mode,
81  std::vector<fc::uint160_t>& contained_transaction_message_ids ) = 0;
82 
89  virtual void handle_transaction( const graphene::net::trx_message& trx_msg ) = 0;
90 
99  virtual void handle_message( const message& message_to_process ) = 0;
100 
108  virtual std::vector<item_hash_t> get_block_ids(const std::vector<item_hash_t>& blockchain_synopsis,
109  uint32_t& remaining_item_count,
110  uint32_t limit = 2000) = 0;
111 
115  virtual message get_item( const item_id& id ) = 0;
116 
117  virtual chain_id_type get_chain_id()const = 0;
118 
136  virtual std::vector<item_hash_t> get_blockchain_synopsis(const item_hash_t& reference_point,
137  uint32_t number_of_blocks_after_reference_point) = 0;
138 
146  virtual void sync_status( uint32_t item_type, uint32_t item_count ) = 0;
147 
151  virtual void connection_count_changed( uint32_t c ) = 0;
152 
153  virtual uint32_t get_block_number(const item_hash_t& block_id) = 0;
154 
159  virtual fc::time_point_sec get_block_time(const item_hash_t& block_id) = 0;
160 
161  virtual item_hash_t get_head_block_id() const = 0;
162 
163  virtual uint32_t estimate_last_known_fork_from_git_revision_timestamp(uint32_t unix_timestamp) const = 0;
164 
165  virtual void error_encountered(const std::string& message, const fc::oexception& error) = 0;
166  virtual uint8_t get_current_block_interval_in_seconds() const = 0;
167 
168  };
169 
174  struct peer_status
175  {
176  uint32_t version;
181  };
182 
192  class node : public std::enable_shared_from_this<node>
193  {
194  public:
195  node(const std::string& user_agent);
196  virtual ~node();
197 
198  void close();
199 
200  void set_node_delegate( node_delegate* del );
201 
202  void load_configuration( const fc::path& configuration_directory );
203 
204  virtual void listen_to_p2p_network();
205  virtual void connect_to_p2p_network();
206 
212  void add_node( const fc::ip::endpoint& ep );
213 
214  /*****
215  * @brief add a list of nodes to seed the p2p network
216  * @param seeds a vector of url strings
217  */
218  void add_seed_nodes( std::vector<std::string> seeds );
219 
220  /****
221  * @brief add a node to seed the p2p network
222  * @param in the url as a string
223  */
224  void add_seed_node( const std::string& in);
225 
229  virtual void connect_to_endpoint( const fc::ip::endpoint& ep );
230 
240  static std::vector<fc::ip::endpoint> resolve_string_to_ip_endpoints( const std::string& in );
241 
246  void listen_on_endpoint( const fc::ip::endpoint& ep, bool wait_if_not_available );
247 
251  void accept_incoming_connections(bool accept);
252 
261  void listen_on_port(uint16_t port, bool wait_if_not_available);
262 
268  virtual fc::ip::endpoint get_actual_listening_endpoint() const;
269 
273  std::vector<peer_status> get_connected_peers() const;
274 
276  virtual uint32_t get_connection_count() const;
277 
282  virtual void broadcast( const message& item_to_broadcast );
283  virtual void broadcast_transaction( const signed_transaction& trx )
284  {
285  broadcast( trx_message(trx) );
286  }
287 
292  virtual void sync_from(const item_id& current_head_block, const std::vector<uint32_t>& hard_fork_block_numbers);
293 
294  bool is_connected() const;
295 
296  void set_advanced_node_parameters(const fc::variant_object& params);
297  fc::variant_object get_advanced_node_parameters();
298  message_propagation_data get_transaction_propagation_data(const graphene::protocol::transaction_id_type& transaction_id);
299  message_propagation_data get_block_propagation_data(const graphene::protocol::block_id_type& block_id);
300  node_id_t get_node_id() const;
301  void set_allowed_peers(const std::vector<node_id_t>& allowed_peers);
302 
307  void clear_peer_database();
308 
309  void set_total_bandwidth_limit(uint32_t upload_bytes_per_second, uint32_t download_bytes_per_second);
310 
311  fc::variant_object network_get_info() const;
312  fc::variant_object network_get_usage_stats() const;
313 
314  std::vector<potential_peer_record> get_potential_peers() const;
315 
316  void disable_peer_advertising();
317  fc::variant_object get_call_statistics() const;
318  private:
319  std::unique_ptr<detail::node_impl, detail::node_impl_deleter> my;
320  };
321 
322  class simulated_network : public node
323  {
324  public:
326  simulated_network(const std::string& user_agent) : node(user_agent) {}
327  void listen_to_p2p_network() override {}
328  void connect_to_p2p_network() override {}
329  void connect_to_endpoint(const fc::ip::endpoint& ep) override {}
330 
332 
333  void sync_from(const item_id& current_head_block, const std::vector<uint32_t>& hard_fork_block_numbers) override {}
334  void broadcast(const message& item_to_broadcast) override;
335  void add_node_delegate(node_delegate* node_delegate_to_add);
336 
337  virtual uint32_t get_connection_count() const override { return 8; }
338  private:
339  struct node_info;
340  void message_sender(node_info* destination_node);
341  std::list<node_info*> network_nodes;
342  };
343 
344 
345  typedef std::shared_ptr<node> node_ptr;
346  typedef std::shared_ptr<simulated_network> simulated_network_ptr;
347 
348 } } // graphene::net
349 
350 FC_REFLECT(graphene::net::message_propagation_data, (received_time)(validated_time)(originating_peer));
351 FC_REFLECT( graphene::net::peer_status, (version)(host)(info) );
fc::sha256 chain_id_type
Definition: types.hpp:132
virtual uint32_t get_connection_count() const override
Definition: node.hpp:337
simulated_network(const std::string &user_agent)
Definition: node.hpp:326
std::shared_ptr< node > node_ptr
Definition: node.hpp:345
An order-perserving dictionary of variant&#39;s.
Definition: api.cpp:56
virtual void broadcast_transaction(const signed_transaction &trx)
Definition: node.hpp:283
fc::ip::endpoint host
Definition: node.hpp:177
used by node reports status to client or fetch data from client
Definition: node.hpp:61
FC_REFLECT(graphene::net::message_propagation_data,(received_time)(validated_time)(originating_peer))
void listen_to_p2p_network() override
Definition: node.hpp:327
void accept(AcceptorType &acc, SocketType &sock)
wraps boost::asio::async_accept
Definition: asio.hpp:251
provides stack-based nullable value similar to boost::optional
Definition: optional.hpp:20
fc::ip::endpoint get_actual_listening_endpoint() const override
Definition: node.hpp:331
provides application independent P2P broadcast and data synchronization
Definition: node.hpp:192
fc::variant_object info
Definition: node.hpp:180
void connect_to_p2p_network() override
Definition: node.hpp:328
adds a signature to a transaction
boost::asio::ip::tcp::endpoint endpoint
Definition: asio.hpp:240
void connect_to_endpoint(const fc::ip::endpoint &ep) override
Definition: node.hpp:329
void sync_from(const item_id &current_head_block, const std::vector< uint32_t > &hard_fork_block_numbers) override
Definition: node.hpp:333
std::shared_ptr< simulated_network > simulated_network_ptr
Definition: node.hpp:346
wraps boost::filesystem::path to provide platform independent path manipulation.
Definition: filesystem.hpp:28