BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet 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 
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;
302  const graphene::protocol::transaction_id_type& transaction_id) const;
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 
318 
319  std::vector<potential_peer_record> get_potential_peers() const;
320 
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) );
graphene::net::block_message
Definition: core_messages.hpp:104
graphene::net::node_delegate::handle_block
virtual bool handle_block(const graphene::net::block_message &blk_msg, bool sync_mode, std::vector< message_hash_type > &contained_transaction_msg_ids)=0
Called when a new block comes in from the network.
fc::variant_object
An order-perserving dictionary of variant's.
Definition: variant_object.hpp:20
graphene::net::node::get_actual_listening_endpoint
virtual fc::ip::endpoint get_actual_listening_endpoint() const
Definition: node.cpp:5132
graphene::net::node_delegate::get_current_block_interval_in_seconds
virtual uint8_t get_current_block_interval_in_seconds() const =0
graphene::net::node::get_node_id
node_id_t get_node_id() const
Definition: node.cpp:5188
graphene::net::node::broadcast_transaction
virtual void broadcast_transaction(const signed_transaction &trx) const
Definition: node.hpp:285
FC_REFLECT
FC_REFLECT(graphene::net::message_propagation_data,(received_time)(validated_time)(originating_peer))
graphene::net::node::load_configuration
void load_configuration(const fc::path &configuration_directory) const
Definition: node.cpp:5087
graphene::net::node::set_accept_incoming_connections
void set_accept_incoming_connections(bool accept) const
Definition: node.cpp:5122
graphene::net::message_propagation_data::received_time
fc::time_point received_time
Definition: node.hpp:47
message.hpp
graphene::net::node::node
node(const std::string &user_agent)
Definition: node.cpp:5070
graphene::net::node::set_advertise_algorithm
void set_advertise_algorithm(const std::string &algo, const std::vector< std::string > &advertise_or_exclude_list=std::vector< std::string >()) const
Definition: node.cpp:5439
graphene::net::peer_status::info
fc::variant_object info
Definition: node.hpp:178
graphene::net::node::~node
virtual ~node()
Definition: node.cpp:5076
graphene::net::node_delegate::get_chain_id
virtual chain_id_type get_chain_id() const =0
graphene::net::node::get_block_propagation_data
message_propagation_data get_block_propagation_data(const graphene::protocol::block_id_type &block_id) const
Definition: node.cpp:5183
graphene::net::node::connect_to_endpoint
virtual void connect_to_endpoint(const fc::ip::endpoint &ep) const
Definition: node.cpp:5107
graphene::net::message
Definition: message.hpp:58
graphene::net::node_delegate::handle_transaction
virtual void handle_transaction(const graphene::net::trx_message &trx_msg)=0
Called when a new transaction comes in from the network.
graphene::net::node_impl_ptr
std::shared_ptr< detail::node_impl > node_impl_ptr
Definition: node.hpp:41
graphene::net::node::get_tx_propagation_data
message_propagation_data get_tx_propagation_data(const graphene::protocol::transaction_id_type &transaction_id) const
Definition: node.cpp:5177
graphene::net::node_ptr
std::shared_ptr< node > node_ptr
Definition: node.hpp:326
fc::zero_initialized_array< unsigned char, 33 >
graphene::net::peer_status::version
uint32_t version
Definition: node.hpp:174
graphene::net::node::set_node_delegate
void set_node_delegate(std::shared_ptr< node_delegate > del) const
Definition: node.cpp:5081
graphene::net::node_delegate::get_head_block_id
virtual item_hash_t get_head_block_id() const =0
graphene::net::node::add_seed_node
void add_seed_node(const std::string &in) const
Definition: node.cpp:5434
core_messages.hpp
graphene::net::node::connect_to_p2p_network
virtual void connect_to_p2p_network() const
Definition: node.cpp:5097
fc::ip::endpoint
Definition: ip.hpp:65
graphene::net::node::clear_peer_database
void clear_peer_database() const
Definition: node.cpp:5198
graphene::net::node
provides application independent P2P broadcast and data synchronization
Definition: node.hpp:190
graphene::net::node::close
void close() const
Definition: node.cpp:5224
graphene::protocol::signed_transaction
adds a signature to a transaction
Definition: transaction.hpp:134
graphene::net::peer_status
Definition: node.hpp:172
graphene::net::item_id
Definition: core_messages.hpp:49
graphene::net::message_propagation_data::originating_peer
node_id_t originating_peer
Definition: node.hpp:49
graphene::net::node_delegate::sync_status
virtual void sync_status(uint32_t item_type, uint32_t item_count)=0
types.hpp
graphene::net::node::my
node_impl_ptr my
Definition: node.hpp:323
fc::path
wraps boost::filesystem::path to provide platform independent path manipulation.
Definition: filesystem.hpp:28
fc::asio::tcp::connect
void connect(AsyncSocket &sock, const EndpointType &ep)
wraps boost::asio::socket::async_connect
Definition: asio.hpp:262
graphene::net::node_delegate::handle_message
virtual void handle_message(const message &message_to_process)=0
Called when a new message comes in from the network other than a block or a transaction....
fc::time_point_sec
Definition: time.hpp:74
graphene::net::node::network_get_usage_stats
fc::variant_object network_get_usage_stats() const
Definition: node.cpp:5219
graphene::net::node::broadcast
virtual void broadcast(const message &item_to_broadcast) const
Definition: node.cpp:5147
fc::ripemd160
Definition: ripemd160.hpp:11
graphene::net::node_delegate::get_block_ids
virtual std::vector< item_hash_t > get_block_ids(const std::vector< item_hash_t > &blockchain_synopsis, uint32_t &remaining_item_count, uint32_t limit=2000)=0
graphene::net::node::get_call_statistics
fc::variant_object get_call_statistics() const
Definition: node.cpp:5209
graphene::net::node::set_inbound_endpoint
void set_inbound_endpoint(const fc::ip::endpoint &ep) const
Definition: node.cpp:5117
graphene::net::node::get_connection_count
virtual uint32_t get_connection_count() const
Definition: node.cpp:5142
graphene::net::peer_status::host
fc::ip::endpoint host
Definition: node.hpp:175
graphene::net::node_delegate::connection_count_changed
virtual void connection_count_changed(uint32_t c)=0
fc::asio::tcp::accept
void accept(AcceptorType &acc, SocketType &sock)
wraps boost::asio::async_accept
Definition: asio.hpp:250
graphene::net::node_delegate::error_encountered
virtual void error_encountered(const std::string &message, const fc::oexception &error)=0
graphene::net::node::add_node
void add_node(const fc::ip::endpoint &ep) const
Definition: node.cpp:5102
graphene::net::message_propagation_data::validated_time
fc::time_point validated_time
Definition: node.hpp:48
graphene::net::node::set_advanced_node_parameters
void set_advanced_node_parameters(const fc::variant_object &params) const
Definition: node.cpp:5167
fc::time_point
Definition: time.hpp:44
graphene::net::node::set_connect_to_new_peers
void set_connect_to_new_peers(bool connect) const
Definition: node.cpp:5127
graphene::net::node::network_get_info
fc::variant_object network_get_info() const
Definition: node.cpp:5214
graphene::net::node_delegate::estimate_last_known_fork_from_git_revision_timestamp
virtual uint32_t estimate_last_known_fork_from_git_revision_timestamp(uint32_t unix_timestamp) const =0
graphene::net::node::sync_from
virtual void sync_from(const item_id &current_head_block, const std::vector< uint32_t > &hard_fork_block_numbers) const
Definition: node.cpp:5152
graphene::net::node_delegate::get_block_number
virtual uint32_t get_block_number(const item_hash_t &block_id)=0
graphene::net::node_delegate::get_block_time
virtual fc::time_point_sec get_block_time(const item_hash_t &block_id)=0
graphene::net::node::set_total_bandwidth_limit
void set_total_bandwidth_limit(uint32_t upload_bytes_per_second, uint32_t download_bytes_per_second) const
Definition: node.cpp:5203
graphene::net::message_propagation_data
Definition: node.hpp:45
graphene::net::trx_message
Definition: core_messages.hpp:93
graphene::net::node::add_seed_nodes
void add_seed_nodes(const std::vector< std::string > &seeds) const
Definition: node.cpp:5421
fc::optional
provides stack-based nullable value similar to boost::optional
Definition: optional.hpp:20
graphene::net::node_delegate::has_item
virtual bool has_item(const net::item_id &id)=0
graphene::net::node::get_advanced_node_parameters
fc::variant_object get_advanced_node_parameters() const
Definition: node.cpp:5172
graphene::protocol::chain_id_type
fc::sha256 chain_id_type
Definition: types.hpp:149
graphene::net::node::get_connected_peers
std::vector< peer_status > get_connected_peers() const
Definition: node.cpp:5137
graphene::net::node::set_listen_endpoint
void set_listen_endpoint(const fc::ip::endpoint &ep, bool wait_if_not_available) const
Definition: node.cpp:5112
peer_database.hpp
graphene::net::node::set_allowed_peers
void set_allowed_peers(const std::vector< node_id_t > &allowed_peers) const
Definition: node.cpp:5193
graphene::net::node::listen_to_p2p_network
virtual void listen_to_p2p_network() const
Definition: node.cpp:5092
graphene::net::node::get_potential_peers
std::vector< potential_peer_record > get_potential_peers() const
Definition: node.cpp:5162
graphene::net::node_delegate::~node_delegate
virtual ~node_delegate()=default
graphene
Definition: api.cpp:48
graphene::net::node_delegate::get_item
virtual message get_item(const item_id &id)=0
graphene::net::node_delegate::get_blockchain_synopsis
virtual std::vector< item_hash_t > get_blockchain_synopsis(const item_hash_t &reference_point, uint32_t number_of_blocks_after_reference_point)=0
graphene::net::node::is_connected
bool is_connected() const
Definition: node.cpp:5157
graphene::net::node_delegate
used by node reports status to client or fetch data from client
Definition: node.hpp:56