Electroneum
cryptonote_protocol_handler.h
Go to the documentation of this file.
1 
5 // Copyrights(c) 2017-2021, The Electroneum Project
6 // Copyrights(c) 2014-2019, 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 
41 #include "math_helper.h"
43 #include "warnings.h"
46 #include "block_queue.h"
47 #include "common/perf_timer.h"
50 #include <boost/circular_buffer.hpp>
51 
54 
55 #define LOCALHOST_INT 2130706433
56 #define CURRENCY_PROTOCOL_MAX_OBJECT_REQUEST_COUNT 500
57 
58 namespace cryptonote
59 {
60 
61  class cryptonote_protocol_handler_base_pimpl;
63  private:
64  std::unique_ptr<cryptonote_protocol_handler_base_pimpl> mI;
65 
66  public:
69  void handler_request_blocks_history(std::list<crypto::hash>& ids); // before asking for list of objects, we can change the list still
70  void handler_response_blocks_now(size_t packet_size);
71 
72  virtual double get_avg_block_size() = 0;
73  virtual double estimate_one_block_size() noexcept; // for estimating size of blocks to download
74  };
75 
76  template<class t_core>
78  {
79  public:
84 
85  t_cryptonote_protocol_handler(t_core& rcore, nodetool::i_p2p_endpoint<connection_context>* p_net_layout, bool offline = false);
86 
88  HANDLE_NOTIFY_T2(NOTIFY_NEW_BLOCK, &cryptonote_protocol_handler::handle_notify_new_block)
89  HANDLE_NOTIFY_T2(NOTIFY_NEW_TRANSACTIONS, &cryptonote_protocol_handler::handle_notify_new_transactions)
90  HANDLE_NOTIFY_T2(NOTIFY_REQUEST_GET_OBJECTS, &cryptonote_protocol_handler::handle_request_get_objects)
91  HANDLE_NOTIFY_T2(NOTIFY_RESPONSE_GET_OBJECTS, &cryptonote_protocol_handler::handle_response_get_objects)
92  HANDLE_NOTIFY_T2(NOTIFY_REQUEST_CHAIN, &cryptonote_protocol_handler::handle_request_chain)
93  HANDLE_NOTIFY_T2(NOTIFY_RESPONSE_CHAIN_ENTRY, &cryptonote_protocol_handler::handle_response_chain_entry)
94  HANDLE_NOTIFY_T2(NOTIFY_NEW_FLUFFY_BLOCK, &cryptonote_protocol_handler::handle_notify_new_fluffy_block)
95  HANDLE_NOTIFY_T2(NOTIFY_REQUEST_FLUFFY_MISSING_TX, &cryptonote_protocol_handler::handle_request_fluffy_missing_tx)
96  HANDLE_INVOKE_T2(NOTIFY_REQUEST_VALIDATORS_LIST, &cryptonote_protocol_handler::handle_request_validators_list)
97  HANDLE_NOTIFY_T2(NOTIFY_EMERGENCY_VALIDATORS_LIST, &cryptonote_protocol_handler::handle_notify_emergency_validators_list)
99 
100  bool on_idle();
101  bool init(const boost::program_options::variables_map& vm);
102  bool deinit();
103  void set_p2p_endpoint(nodetool::i_p2p_endpoint<connection_context>* p2p);
104  //bool process_handshake_data(const blobdata& data, cryptonote_connection_context& context);
106  bool get_payload_sync_data(blobdata& data);
108  bool get_stat_info(core_stat_info& stat_inf);
110  t_core& get_core(){return m_core;}
111  bool is_synchronized(){return m_synchronized;}
112  void log_connections();
113  std::list<connection_info> get_connections();
114  const block_queue &get_block_queue() const { return m_block_queue; }
115  void stop();
117  void set_max_out_peers(unsigned int max) { m_max_out_peers = max; }
118  void set_no_sync(bool value) { m_no_sync = value; }
120  std::pair<uint32_t, uint32_t> get_next_needed_pruning_stripe() const;
121  bool needs_new_sync_connections() const;
122  private:
123  //----------------- commands handlers ----------------------------------------------
124  int handle_notify_new_block(int command, NOTIFY_NEW_BLOCK::request& arg, cryptonote_connection_context& context);
125  int handle_notify_new_transactions(int command, NOTIFY_NEW_TRANSACTIONS::request& arg, cryptonote_connection_context& context);
126  int handle_request_get_objects(int command, NOTIFY_REQUEST_GET_OBJECTS::request& arg, cryptonote_connection_context& context);
127  int handle_response_get_objects(int command, NOTIFY_RESPONSE_GET_OBJECTS::request& arg, cryptonote_connection_context& context);
128  int handle_request_chain(int command, NOTIFY_REQUEST_CHAIN::request& arg, cryptonote_connection_context& context);
129  int handle_response_chain_entry(int command, NOTIFY_RESPONSE_CHAIN_ENTRY::request& arg, cryptonote_connection_context& context);
130  int handle_notify_new_fluffy_block(int command, NOTIFY_NEW_FLUFFY_BLOCK::request& arg, cryptonote_connection_context& context);
131  int handle_request_fluffy_missing_tx(int command, NOTIFY_REQUEST_FLUFFY_MISSING_TX::request& arg, cryptonote_connection_context& context);
133  int handle_notify_emergency_validators_list(int command, NOTIFY_EMERGENCY_VALIDATORS_LIST::request& arg, cryptonote_connection_context& context);
134  //----------------- i_bc_protocol_layout ---------------------------------------
135  virtual bool relay_block(NOTIFY_NEW_BLOCK::request& arg, cryptonote_connection_context& exclude_context);
136  virtual bool relay_transactions(NOTIFY_NEW_TRANSACTIONS::request& arg, cryptonote_connection_context& exclude_context);
137  virtual bool request_validators_list(cryptonote_connection_context& context);
138  virtual bool request_validators_list_to_all();
139  virtual bool relay_emergency_validator_list(NOTIFY_EMERGENCY_VALIDATORS_LIST::request& arg, cryptonote_connection_context& exclude_context);
140  //----------------------------------------------------------------------------------
141  //bool get_payload_sync_data(HANDSHAKE_DATA::request& hshd, cryptonote_connection_context& context);
142  bool should_drop_connection(cryptonote_connection_context& context, uint32_t next_stripe);
143  bool request_missing_objects(cryptonote_connection_context& context, bool check_having_blocks, bool force_next_span = false);
144  size_t get_synchronizing_connections_count();
145  bool on_connection_synchronized();
146  bool should_download_next_span(cryptonote_connection_context& context, bool standby);
147  void drop_connection(cryptonote_connection_context &context, bool add_fail, bool flush_all_spans);
148  bool kick_idle_peers();
149  bool check_standby_peers();
150  bool update_sync_search();
151  int try_add_next_blocks(cryptonote_connection_context &context);
152  void notify_new_stripe(cryptonote_connection_context &context, uint32_t stripe);
153  void skip_unneeded_hashes(cryptonote_connection_context& context, bool check_block_queue) const;
154 
155  t_core& m_core;
156 
159  std::atomic<uint32_t> m_syncronized_connections_count;
160  std::atomic<bool> m_synchronized;
161  std::atomic<bool> m_stopping;
162  std::atomic<bool> m_no_sync;
163  boost::mutex m_sync_lock;
164  block_queue m_block_queue;
167  epee::math_helper::once_a_time_seconds<101> m_sync_search_checker;
168  std::atomic<unsigned int> m_max_out_peers;
169  tools::PerformanceTimer m_sync_timer, m_add_timer;
170  uint64_t m_last_add_end_time;
171  uint64_t m_sync_spans_downloaded, m_sync_old_spans_downloaded, m_sync_bad_spans_downloaded;
172  uint64_t m_sync_download_chain_size, m_sync_download_objects_size;
173  size_t m_block_download_max_size;
174 
175  boost::mutex m_buffer_mutex;
176  double get_avg_block_size();
177  boost::circular_buffer<size_t> m_avg_buffer = boost::circular_buffer<size_t>(10);
178 
179  template<class t_parameter>
180  bool post_notify(typename t_parameter::request& arg, cryptonote_connection_context& context)
181  {
182  LOG_PRINT_L2("[" << epee::net_utils::print_connection_context_short(context) << "] post " << typeid(t_parameter).name() << " -->");
183  std::string blob;
185  //handler_response_blocks_now(blob.size()); // XXX
186  return m_p2p->invoke_notify_to_peer(t_parameter::ID, epee::strspan<uint8_t>(blob), context);
187  }
188  };
189 
190 } // namespace
191 
const char * res
Definition: hmac_keccak.cpp:41
void handler_request_blocks_history(std::list< crypto::hash > &ids)
#define HANDLE_NOTIFY_T2(NOTIFY, func)
bool store_t_to_binary(t_struct &str_in, std::string &binary_buff, size_t indent=0)
#define LOG_PRINT_L2(x)
Definition: misc_log_ex.h:101
PUSH_WARNINGS
Definition: hash-ops.h:54
std::string print_connection_context_short(const connection_context_base &ctx)
bool process_payload_sync_data(const CORE_SYNC_DATA &hshd, cryptonote_connection_context &context, bool is_inital)
void set_p2p_endpoint(nodetool::i_p2p_endpoint< connection_context > *p2p)
::std::string string
Definition: gtest-port.h:1097
virtual bool invoke_notify_to_peer(int command, const epee::span< const uint8_t > req_buff, const epee::net_utils::connection_context_base &context)=0
const char * name
#define BEGIN_INVOKE_MAP2(owner_type)
bool init(const boost::program_options::variables_map &vm)
Holds cryptonote related classes and helpers.
Definition: ban.cpp:40
t_cryptonote_protocol_handler< t_core > cryptonote_protocol_handler
bool on_callback(cryptonote_connection_context &context)
unsigned int uint32_t
Definition: stdint.h:126
#define POP_WARNINGS
Definition: warnings.h:17
void on_connection_close(cryptonote_connection_context &context)
bool get_stat_info(core_stat_info &stat_inf)
unsigned __int64 uint64_t
Definition: stdint.h:136
std::unique_ptr< void, terminate > context
Unique ZMQ context handle, calls zmq_term on destruction.
Definition: zmq.h:98
#define END_INVOKE_MAP2()
std::pair< uint32_t, uint32_t > get_next_needed_pruning_stripe() const
std::string blobdata
Definition: blobdatatype.h:39
#define HANDLE_INVOKE_T2(COMMAND, func)
std::list< connection_info > get_connections()
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
int bool
Definition: stdbool.h:36
t_cryptonote_protocol_handler(t_core &rcore, nodetool::i_p2p_endpoint< connection_context > *p_net_layout, bool offline=false)
DISABLE_VS_WARNINGS(4244 4345 4503) using namespace crypto