Electroneum
cryptonote_protocol_handler.h
Go to the documentation of this file.
1 
5 // Copyrights(c) 2017-2018, The Electroneum Project
6 // Copyrights(c) 2014-2017, The Monero Project
7 //
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without modification, are
11 // permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright notice, this list of
14 // conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
17 // of conditions and the following disclaimer in the documentation and/or other
18 // materials provided with the distribution.
19 //
20 // 3. Neither the name of the copyright holder nor the names of its contributors may be
21 // used to endorse or promote products derived from this software without specific
22 // prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
25 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
32 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 //
34 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
35 
36 #pragma once
37 
38 #include <boost/program_options/variables_map.hpp>
39 #include <string>
40 #include <ctime>
41 
42 #include "math_helper.h"
43 #include "storages/levin_abstract_invoke2.h"
44 #include "warnings.h"
47 #include "block_queue.h"
51 // #include <netinet/in.h>
52 #include <boost/circular_buffer.hpp>
53 
56 
57 #define LOCALHOST_INT 2130706433
58 
59 namespace cryptonote
60 {
61 
62  class cryptonote_protocol_handler_base_pimpl;
64  private:
65  std::unique_ptr<cryptonote_protocol_handler_base_pimpl> mI;
66 
67  public:
70  void handler_request_blocks_history(std::list<crypto::hash>& ids); // before asking for list of objects, we can change the list still
71  void handler_response_blocks_now(size_t packet_size);
72 
73  virtual double get_avg_block_size() = 0;
74  virtual double estimate_one_block_size() noexcept; // for estimating size of blocks to download
75  };
76 
77  template<class t_core>
79  {
80  public:
85 
87 
88  BEGIN_INVOKE_MAP2(cryptonote_protocol_handler)
89  HANDLE_NOTIFY_T2(NOTIFY_NEW_BLOCK, &cryptonote_protocol_handler::handle_notify_new_block)
90  HANDLE_NOTIFY_T2(NOTIFY_NEW_TRANSACTIONS, &cryptonote_protocol_handler::handle_notify_new_transactions)
91  HANDLE_NOTIFY_T2(NOTIFY_REQUEST_GET_OBJECTS, &cryptonote_protocol_handler::handle_request_get_objects)
92  HANDLE_NOTIFY_T2(NOTIFY_RESPONSE_GET_OBJECTS, &cryptonote_protocol_handler::handle_response_get_objects)
93  HANDLE_NOTIFY_T2(NOTIFY_REQUEST_CHAIN, &cryptonote_protocol_handler::handle_request_chain)
94  HANDLE_NOTIFY_T2(NOTIFY_RESPONSE_CHAIN_ENTRY, &cryptonote_protocol_handler::handle_response_chain_entry)
95  HANDLE_NOTIFY_T2(NOTIFY_NEW_FLUFFY_BLOCK, &cryptonote_protocol_handler::handle_notify_new_fluffy_block)
96  HANDLE_NOTIFY_T2(NOTIFY_REQUEST_FLUFFY_MISSING_TX, &cryptonote_protocol_handler::handle_request_fluffy_missing_tx)
97  END_INVOKE_MAP2()
98 
99  bool on_idle();
100  bool init(const boost::program_options::variables_map& vm);
101  bool deinit();
102  void set_p2p_endpoint(nodetool::i_p2p_endpoint<connection_context>* p2p);
103  //bool process_handshake_data(const blobdata& data, cryptonote_connection_context& context);
104  bool process_payload_sync_data(const CORE_SYNC_DATA& hshd, cryptonote_connection_context& context, bool is_inital);
105  bool get_payload_sync_data(blobdata& data);
106  bool get_payload_sync_data(CORE_SYNC_DATA& hshd);
107  bool get_stat_info(core_stat_info& stat_inf);
108  bool on_callback(cryptonote_connection_context& context);
109  t_core& get_core(){return m_core;}
110  bool is_synchronized(){return m_synchronized;}
111  void log_connections();
112  std::list<connection_info> get_connections();
113  const block_queue &get_block_queue() const { return m_block_queue; }
114  void stop();
115  void on_connection_close(cryptonote_connection_context &context);
116  private:
117  //----------------- commands handlers ----------------------------------------------
118  int handle_notify_new_block(int command, NOTIFY_NEW_BLOCK::request& arg, cryptonote_connection_context& context);
119  int handle_notify_new_transactions(int command, NOTIFY_NEW_TRANSACTIONS::request& arg, cryptonote_connection_context& context);
120  int handle_request_get_objects(int command, NOTIFY_REQUEST_GET_OBJECTS::request& arg, cryptonote_connection_context& context);
121  int handle_response_get_objects(int command, NOTIFY_RESPONSE_GET_OBJECTS::request& arg, cryptonote_connection_context& context);
122  int handle_request_chain(int command, NOTIFY_REQUEST_CHAIN::request& arg, cryptonote_connection_context& context);
123  int handle_response_chain_entry(int command, NOTIFY_RESPONSE_CHAIN_ENTRY::request& arg, cryptonote_connection_context& context);
124  int handle_notify_new_fluffy_block(int command, NOTIFY_NEW_FLUFFY_BLOCK::request& arg, cryptonote_connection_context& context);
125  int handle_request_fluffy_missing_tx(int command, NOTIFY_REQUEST_FLUFFY_MISSING_TX::request& arg, cryptonote_connection_context& context);
126 
127  //----------------- i_bc_protocol_layout ---------------------------------------
128  virtual bool relay_block(NOTIFY_NEW_BLOCK::request& arg, cryptonote_connection_context& exclude_context);
129  virtual bool relay_transactions(NOTIFY_NEW_TRANSACTIONS::request& arg, cryptonote_connection_context& exclude_context);
130  //----------------------------------------------------------------------------------
131  //bool get_payload_sync_data(HANDSHAKE_DATA::request& hshd, cryptonote_connection_context& context);
132  bool request_missing_objects(cryptonote_connection_context& context, bool check_having_blocks, bool force_next_span = false);
133  size_t get_synchronizing_connections_count();
134  bool on_connection_synchronized();
135  bool should_download_next_span(cryptonote_connection_context& context) const;
136  void drop_connection(cryptonote_connection_context &context, bool add_fail, bool flush_all_spans);
137  bool kick_idle_peers();
138  int try_add_next_blocks(cryptonote_connection_context &context);
139 
140  t_core& m_core;
141 
144  std::atomic<uint32_t> m_syncronized_connections_count;
145  std::atomic<bool> m_synchronized;
146  std::atomic<bool> m_stopping;
147  boost::mutex m_sync_lock;
149  epee::math_helper::once_a_time_seconds<30> m_idle_peer_kicker;
150 
151  boost::mutex m_buffer_mutex;
152  double get_avg_block_size();
153  boost::circular_buffer<size_t> m_avg_buffer = boost::circular_buffer<size_t>(10);
154 
155  template<class t_parameter>
156  bool post_notify(typename t_parameter::request& arg, cryptonote_connection_context& context)
157  {
158  LOG_PRINT_L2("[" << epee::net_utils::print_connection_context_short(context) << "] post " << typeid(t_parameter).name() << " -->");
159  std::string blob;
160  epee::serialization::store_t_to_binary(arg, blob);
161  //handler_response_blocks_now(blob.size()); // XXX
162  return m_p2p->invoke_notify_to_peer(t_parameter::ID, blob, context);
163  }
164 
165  template<class t_parameter>
166  bool relay_post_notify(typename t_parameter::request& arg, cryptonote_connection_context& exclude_context)
167  {
168  LOG_PRINT_L2("[" << epee::net_utils::print_connection_context_short(exclude_context) << "] post relay " << typeid(t_parameter).name() << " -->");
169  std::string arg_buff;
170  epee::serialization::store_t_to_binary(arg, arg_buff);
171  return m_p2p->relay_notify_to_all(t_parameter::ID, arg_buff, exclude_context);
172  }
173  };
174 
175 } // namespace
176 
177 
179 
180 POP_WARNINGS
Definition: cryptonote_protocol_defs.h:165
nodetool::p2p_endpoint_stub< connection_context > m_p2p_stub
Definition: cryptonote_protocol_handler.h:142
epee::math_helper::once_a_time_seconds< 30 > m_idle_peer_kicker
Definition: cryptonote_protocol_handler.h:149
void handler_request_blocks_history(std::list< crypto::hash > &ids)
Definition: cryptonote_protocol_handler-base.cpp:123
Definition: block_queue.h:47
virtual double estimate_one_block_size() noexcept
Definition: cryptonote_protocol_handler-base.cpp:105
PUSH_WARNINGS
Definition: hash-ops.h:54
std::unique_ptr< cryptonote_protocol_handler_base_pimpl > mI
Definition: cryptonote_protocol_handler.h:65
Definition: net_node_common.h:63
Definition: unordered_containers_boost_serialization.h:38
const block_queue & get_block_queue() const
Definition: cryptonote_protocol_handler.h:113
cryptonote_connection_context connection_context
Definition: cryptonote_protocol_handler.h:81
std::atomic< uint32_t > m_syncronized_connections_count
Definition: cryptonote_protocol_handler.h:144
virtual ~cryptonote_protocol_handler_base()
Definition: cryptonote_protocol_handler-base.cpp:120
Definition: connection_context.h:41
Definition: cryptonote_stat_info.h:38
virtual bool invoke_notify_to_peer(int command, const std::string &req_buff, const epee::net_utils::connection_context_base &context)=0
Definition: cryptonote_protocol_defs.h:227
void handler_response_blocks_now(size_t packet_size)
Definition: cryptonote_protocol_handler-base.cpp:126
core_stat_info stat_info
Definition: cryptonote_protocol_handler.h:82
Definition: cryptonote_protocol_defs.h:145
t_core & m_core
Definition: cryptonote_protocol_handler.h:140
nodetool::i_p2p_endpoint< connection_context > * m_p2p
Definition: cryptonote_protocol_handler.h:143
boost::mutex m_buffer_mutex
Definition: cryptonote_protocol_handler.h:151
Definition: cryptonote_protocol_defs.h:275
Definition: cryptonote_protocol_defs.h:254
Holds cryptonote related classes and helpers.
Definition: db_bdb.cpp:225
block_queue m_block_queue
Definition: cryptonote_protocol_handler.h:148
virtual bool relay_notify_to_all(int command, const std::string &data_buff, const epee::net_utils::connection_context_base &context)=0
t_cryptonote_protocol_handler< t_core > cryptonote_protocol_handler
Definition: cryptonote_protocol_handler.h:83
Definition: cryptonote_protocol_defs.h:177
Definition: cryptonote_protocol_defs.h:250
Definition: cryptonote_protocol_handler.h:78
Definition: cryptonote_protocol_defs.h:181
std::atomic< bool > m_stopping
Definition: cryptonote_protocol_handler.h:146
DISABLE_VS_WARNINGS(4244 4345 4503) namespace cryptonote
Definition: tx_pool.cpp:53
Definition: cryptonote_protocol_defs.h:124
bool post_notify(typename t_parameter::request &arg, cryptonote_connection_context &context)
Definition: cryptonote_protocol_handler.h:156
Definition: cryptonote_protocol_defs.h:128
Definition: cryptonote_protocol_defs.h:213
Definition: cryptonote_protocol_defs.h:198
CORE_SYNC_DATA payload_type
Definition: cryptonote_protocol_handler.h:84
bool relay_post_notify(typename t_parameter::request &arg, cryptonote_connection_context &exclude_context)
Definition: cryptonote_protocol_handler.h:166
Definition: cryptonote_protocol_handler.h:63
std::atomic< bool > m_synchronized
Definition: cryptonote_protocol_handler.h:145
std::string blobdata
Definition: blobdatatype.h:36
boost::mutex m_sync_lock
Definition: cryptonote_protocol_handler.h:147
Definition: cryptonote_protocol_defs.h:231
int bool
Definition: stdbool.h:36
Definition: net_node.h:60
cryptonote_protocol_handler_base()
Definition: cryptonote_protocol_handler-base.cpp:117
bool is_synchronized()
Definition: cryptonote_protocol_handler.h:110
Definition: cryptonote_protocol_defs.h:161
Definition: cryptonote_protocol_defs.h:271
Definition: cryptonote_protocol_defs.h:149
const char * name
Definition: simplewallet.cpp:181
This is the orginal cryptonote protocol network-events handler, modified by us.
Definition: cryptonote_protocol_defs.h:217
Definition: net_node_common.h:45
Definition: cryptonote_protocol_handler_common.h:42