Monero
cryptonote_protocol_handler.h
Go to the documentation of this file.
1 
5 // Copyright (c) 2014-2020, The Monero Project
6 //
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without modification, are
10 // permitted provided that the following conditions are met:
11 //
12 // 1. Redistributions of source code must retain the above copyright notice, this list of
13 // conditions and the following disclaimer.
14 //
15 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
16 // of conditions and the following disclaimer in the documentation and/or other
17 // materials provided with the distribution.
18 //
19 // 3. Neither the name of the copyright holder nor the names of its contributors may be
20 // used to endorse or promote products derived from this software without specific
21 // prior written permission.
22 //
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
24 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
26 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
31 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
34 
35 #pragma once
36 
37 #include <boost/program_options/variables_map.hpp>
38 #include <string>
39 
40 #include "math_helper.h"
41 #include "storages/levin_abstract_invoke2.h"
42 #include "warnings.h"
45 #include "block_queue.h"
46 #include "common/perf_timer.h"
48 #include <boost/circular_buffer.hpp>
49 
52 
53 #define LOCALHOST_INT 2130706433
54 #define CURRENCY_PROTOCOL_MAX_OBJECT_REQUEST_COUNT 100
55 static_assert(CURRENCY_PROTOCOL_MAX_OBJECT_REQUEST_COUNT >= BLOCKS_SYNCHRONIZING_DEFAULT_COUNT_PRE_V4, "Invalid CURRENCY_PROTOCOL_MAX_OBJECT_REQUEST_COUNT");
56 
57 namespace cryptonote
58 {
59 
60  class cryptonote_protocol_handler_base_pimpl;
62  private:
63  std::unique_ptr<cryptonote_protocol_handler_base_pimpl> mI;
64 
65  public:
68  void handler_request_blocks_history(std::list<crypto::hash>& ids); // before asking for list of objects, we can change the list still
69  void handler_response_blocks_now(size_t packet_size);
70 
71  virtual double get_avg_block_size() = 0;
72  virtual double estimate_one_block_size() noexcept; // for estimating size of blocks to download
73  };
74 
75  template<class t_core>
77  {
78  public:
82 
83  t_cryptonote_protocol_handler(t_core& rcore, nodetool::i_p2p_endpoint<connection_context>* p_net_layout, bool offline = false);
84 
85  BEGIN_INVOKE_MAP2(cryptonote_protocol_handler)
86  HANDLE_NOTIFY_T2(NOTIFY_NEW_BLOCK, &cryptonote_protocol_handler::handle_notify_new_block)
87  HANDLE_NOTIFY_T2(NOTIFY_NEW_TRANSACTIONS, &cryptonote_protocol_handler::handle_notify_new_transactions)
88  HANDLE_NOTIFY_T2(NOTIFY_REQUEST_GET_OBJECTS, &cryptonote_protocol_handler::handle_request_get_objects)
89  HANDLE_NOTIFY_T2(NOTIFY_RESPONSE_GET_OBJECTS, &cryptonote_protocol_handler::handle_response_get_objects)
90  HANDLE_NOTIFY_T2(NOTIFY_REQUEST_CHAIN, &cryptonote_protocol_handler::handle_request_chain)
91  HANDLE_NOTIFY_T2(NOTIFY_RESPONSE_CHAIN_ENTRY, &cryptonote_protocol_handler::handle_response_chain_entry)
92  HANDLE_NOTIFY_T2(NOTIFY_NEW_FLUFFY_BLOCK, &cryptonote_protocol_handler::handle_notify_new_fluffy_block)
93  HANDLE_NOTIFY_T2(NOTIFY_REQUEST_FLUFFY_MISSING_TX, &cryptonote_protocol_handler::handle_request_fluffy_missing_tx)
94  HANDLE_NOTIFY_T2(NOTIFY_GET_TXPOOL_COMPLEMENT, &cryptonote_protocol_handler::handle_notify_get_txpool_complement)
95  END_INVOKE_MAP2()
96 
97  bool on_idle();
98  bool init(const boost::program_options::variables_map& vm);
99  bool deinit();
100  void set_p2p_endpoint(nodetool::i_p2p_endpoint<connection_context>* p2p);
101  //bool process_handshake_data(const blobdata& data, cryptonote_connection_context& context);
102  bool process_payload_sync_data(const CORE_SYNC_DATA& hshd, cryptonote_connection_context& context, bool is_inital);
103  bool get_payload_sync_data(blobdata& data);
104  bool get_payload_sync_data(CORE_SYNC_DATA& hshd);
105  bool on_callback(cryptonote_connection_context& context);
106  t_core& get_core(){return m_core;}
107  bool is_synchronized(){return m_synchronized;}
108  void log_connections();
109  std::list<connection_info> get_connections();
110  const block_queue &get_block_queue() const { return m_block_queue; }
111  void stop();
112  void on_connection_close(cryptonote_connection_context &context);
113  void set_max_out_peers(unsigned int max) { m_max_out_peers = max; }
114  bool no_sync() const { return m_no_sync; }
115  void set_no_sync(bool value) { m_no_sync = value; }
116  std::string get_peers_overview() const;
117  std::pair<uint32_t, uint32_t> get_next_needed_pruning_stripe() const;
118  bool needs_new_sync_connections() const;
119  private:
120  //----------------- commands handlers ----------------------------------------------
121  int handle_notify_new_block(int command, NOTIFY_NEW_BLOCK::request& arg, cryptonote_connection_context& context);
122  int handle_notify_new_transactions(int command, NOTIFY_NEW_TRANSACTIONS::request& arg, cryptonote_connection_context& context);
123  int handle_request_get_objects(int command, NOTIFY_REQUEST_GET_OBJECTS::request& arg, cryptonote_connection_context& context);
124  int handle_response_get_objects(int command, NOTIFY_RESPONSE_GET_OBJECTS::request& arg, cryptonote_connection_context& context);
125  int handle_request_chain(int command, NOTIFY_REQUEST_CHAIN::request& arg, cryptonote_connection_context& context);
126  int handle_response_chain_entry(int command, NOTIFY_RESPONSE_CHAIN_ENTRY::request& arg, cryptonote_connection_context& context);
127  int handle_notify_new_fluffy_block(int command, NOTIFY_NEW_FLUFFY_BLOCK::request& arg, cryptonote_connection_context& context);
128  int handle_request_fluffy_missing_tx(int command, NOTIFY_REQUEST_FLUFFY_MISSING_TX::request& arg, cryptonote_connection_context& context);
129  int handle_notify_get_txpool_complement(int command, NOTIFY_GET_TXPOOL_COMPLEMENT::request& arg, cryptonote_connection_context& context);
130 
131  //----------------- i_bc_protocol_layout ---------------------------------------
132  virtual bool relay_block(NOTIFY_NEW_BLOCK::request& arg, cryptonote_connection_context& exclude_context);
133  virtual bool relay_transactions(NOTIFY_NEW_TRANSACTIONS::request& arg, const boost::uuids::uuid& source, epee::net_utils::zone zone, relay_method tx_relay);
134  //----------------------------------------------------------------------------------
135  //bool get_payload_sync_data(HANDSHAKE_DATA::request& hshd, cryptonote_connection_context& context);
136  bool should_drop_connection(cryptonote_connection_context& context, uint32_t next_stripe);
137  bool request_missing_objects(cryptonote_connection_context& context, bool check_having_blocks, bool force_next_span = false);
138  size_t get_synchronizing_connections_count();
139  bool on_connection_synchronized();
140  bool should_download_next_span(cryptonote_connection_context& context, bool standby);
141  bool should_ask_for_pruned_data(cryptonote_connection_context& context, uint64_t first_block_height, uint64_t nblocks, bool check_block_weights) const;
142  void drop_connection(cryptonote_connection_context &context, bool add_fail, bool flush_all_spans);
143  void drop_connection_with_score(cryptonote_connection_context &context, unsigned int score, bool flush_all_spans);
144  bool kick_idle_peers();
145  bool check_standby_peers();
146  bool update_sync_search();
147  int try_add_next_blocks(cryptonote_connection_context &context);
148  void notify_new_stripe(cryptonote_connection_context &context, uint32_t stripe);
149  void skip_unneeded_hashes(cryptonote_connection_context& context, bool check_block_queue) const;
150  bool request_txpool_complement(cryptonote_connection_context &context);
151 
152  t_core& m_core;
153 
156  std::atomic<uint32_t> m_syncronized_connections_count;
157  std::atomic<bool> m_synchronized;
158  std::atomic<bool> m_stopping;
159  std::atomic<bool> m_no_sync;
160  std::atomic<bool> m_ask_for_txpool_complement;
161  boost::mutex m_sync_lock;
163  epee::math_helper::once_a_time_seconds<30> m_idle_peer_kicker;
164  epee::math_helper::once_a_time_milliseconds<100> m_standby_checker;
165  epee::math_helper::once_a_time_seconds<101> m_sync_search_checker;
166  std::atomic<unsigned int> m_max_out_peers;
169  uint64_t m_sync_spans_downloaded, m_sync_old_spans_downloaded, m_sync_bad_spans_downloaded;
170  uint64_t m_sync_download_chain_size, m_sync_download_objects_size;
173 
174  // Values for sync time estimates
175  boost::posix_time::ptime m_sync_start_time;
176  boost::posix_time::ptime m_period_start_time;
179  uint64_t get_estimated_remaining_sync_seconds(uint64_t current_blockchain_height, uint64_t target_blockchain_height);
180  std::string get_periodic_sync_estimate(uint64_t current_blockchain_height, uint64_t target_blockchain_height);
181 
182  boost::mutex m_buffer_mutex;
183  double get_avg_block_size();
184  boost::circular_buffer<size_t> m_avg_buffer = boost::circular_buffer<size_t>(10);
185 
186  template<class t_parameter>
187  bool post_notify(typename t_parameter::request& arg, cryptonote_connection_context& context)
188  {
189  LOG_PRINT_L2("[" << epee::net_utils::print_connection_context_short(context) << "] post " << typeid(t_parameter).name() << " -->");
190  std::string blob;
191  epee::serialization::store_t_to_binary(arg, blob);
192  //handler_response_blocks_now(blob.size()); // XXX
193  return m_p2p->invoke_notify_to_peer(t_parameter::ID, epee::strspan<uint8_t>(blob), context);
194  }
195  };
196 
197 } // namespace
198 
199 POP_WARNINGS
cryptonote::t_cryptonote_protocol_handler::post_notify
bool post_notify(typename t_parameter::request &arg, cryptonote_connection_context &context)
Definition: cryptonote_protocol_handler.h:187
cryptonote::t_cryptonote_protocol_handler::m_stopping
std::atomic< bool > m_stopping
Definition: cryptonote_protocol_handler.h:158
cryptonote::cryptonote_connection_context
Definition: connection_context.h:43
cryptonote::relay_method
relay_method
Methods tracking how a tx was received and relayed.
Definition: enums.h:37
cryptonote::t_cryptonote_protocol_handler::m_period_start_time
boost::posix_time::ptime m_period_start_time
Definition: cryptonote_protocol_handler.h:176
cryptonote::NOTIFY_NEW_TRANSACTIONS
Definition: cryptonote_protocol_defs.h:193
cryptonote::t_cryptonote_protocol_handler::payload_type
CORE_SYNC_DATA payload_type
Definition: cryptonote_protocol_handler.h:81
cryptonote::NOTIFY_REQUEST_GET_OBJECTS::request
epee::misc_utils::struct_init< request_t > request
Definition: cryptonote_protocol_defs.h:227
perf_timer.h
cryptonote::t_cryptonote_protocol_handler::m_sync_lock
boost::mutex m_sync_lock
Definition: cryptonote_protocol_handler.h:161
connection_context.h
cryptonote::t_cryptonote_protocol_handler::m_synchronized
std::atomic< bool > m_synchronized
Definition: cryptonote_protocol_handler.h:157
cryptonote::cryptonote_protocol_handler_base
Definition: cryptonote_protocol_handler.h:61
cryptonote::t_cryptonote_protocol_handler::set_max_out_peers
void set_max_out_peers(unsigned int max)
Definition: cryptonote_protocol_handler.h:113
cryptonote::t_cryptonote_protocol_handler::m_ask_for_txpool_complement
std::atomic< bool > m_ask_for_txpool_complement
Definition: cryptonote_protocol_handler.h:160
boost
Definition: unordered_containers_boost_serialization.h:38
cryptonote::NOTIFY_GET_TXPOOL_COMPLEMENT
Definition: cryptonote_protocol_defs.h:363
cryptonote::NOTIFY_RESPONSE_CHAIN_ENTRY
Definition: cryptonote_protocol_defs.h:290
cryptonote::cryptonote_protocol_handler_base::~cryptonote_protocol_handler_base
virtual ~cryptonote_protocol_handler_base()
Definition: cryptonote_protocol_handler-base.cpp:97
cryptonote::block_queue
Definition: block_queue.h:48
cryptonote::t_cryptonote_protocol_handler::m_max_out_peers
std::atomic< unsigned int > m_max_out_peers
Definition: cryptonote_protocol_handler.h:166
cryptonote::NOTIFY_NEW_BLOCK
Definition: cryptonote_protocol_defs.h:173
nodetool::i_p2p_endpoint::invoke_notify_to_peer
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
DISABLE_VS_WARNINGS
DISABLE_VS_WARNINGS(4244 4345 4503) using namespace crypto
cryptonote::t_cryptonote_protocol_handler::m_standby_checker
epee::math_helper::once_a_time_milliseconds< 100 > m_standby_checker
Definition: cryptonote_protocol_handler.h:164
cryptonote::NOTIFY_NEW_FLUFFY_BLOCK
Definition: cryptonote_protocol_defs.h:321
init
static void init(std::string cache_filename)
Definition: blockchain_blackball.cpp:221
build_protob.const
const
Definition: build_protob.py:9
cryptonote::t_cryptonote_protocol_handler::m_sync_start_height
uint64_t m_sync_start_height
Definition: cryptonote_protocol_handler.h:177
cryptonote::t_cryptonote_protocol_handler::is_synchronized
bool is_synchronized()
Definition: cryptonote_protocol_handler.h:107
cryptonote::t_cryptonote_protocol_handler
Definition: cryptonote_protocol_handler.h:77
cryptonote_protocol_handler_common.h
cryptonote_protocol_defs.h
cryptonote::NOTIFY_RESPONSE_GET_OBJECTS
Definition: cryptonote_protocol_defs.h:231
cryptonote::t_cryptonote_protocol_handler::m_sync_start_time
boost::posix_time::ptime m_sync_start_time
Definition: cryptonote_protocol_handler.h:175
cryptonote::t_cryptonote_protocol_handler::m_sync_download_objects_size
uint64_t m_sync_download_objects_size
Definition: cryptonote_protocol_handler.h:170
cryptonote::t_cryptonote_protocol_handler::m_p2p_stub
nodetool::p2p_endpoint_stub< connection_context > m_p2p_stub
Definition: cryptonote_protocol_handler.h:154
cryptonote::t_cryptonote_protocol_handler::m_block_download_max_size
size_t m_block_download_max_size
Definition: cryptonote_protocol_handler.h:171
nodetool::uuid
boost::uuids::uuid uuid
Definition: net_node_common.h:46
nodetool::p2p_endpoint_stub
Definition: net_node_common.h:73
cryptonote::t_cryptonote_protocol_handler::m_period_start_height
uint64_t m_period_start_height
Definition: cryptonote_protocol_handler.h:178
cryptonote::cryptonote_protocol_handler_base::cryptonote_protocol_handler_base
cryptonote_protocol_handler_base()
Definition: cryptonote_protocol_handler-base.cpp:94
net::zmq::context
std::unique_ptr< void, terminate > context
Unique ZMQ context handle, calls zmq_term on destruction.
Definition: zmq.h:105
nodetool::i_p2p_endpoint
Definition: net_node_common.h:51
cryptonote::t_cryptonote_protocol_handler::m_idle_peer_kicker
epee::math_helper::once_a_time_seconds< 30 > m_idle_peer_kicker
Definition: cryptonote_protocol_handler.h:163
cryptonote::NOTIFY_RESPONSE_CHAIN_ENTRY::request
epee::misc_utils::struct_init< request_t > request
Definition: cryptonote_protocol_defs.h:314
cryptonote::NOTIFY_REQUEST_GET_OBJECTS
Definition: cryptonote_protocol_defs.h:214
BLOCKS_SYNCHRONIZING_DEFAULT_COUNT_PRE_V4
#define BLOCKS_SYNCHRONIZING_DEFAULT_COUNT_PRE_V4
Definition: cryptonote_config.h:96
cryptonote
Holds cryptonote related classes and helpers.
Definition: blockchain_db.cpp:45
cryptonote::NOTIFY_REQUEST_FLUFFY_MISSING_TX::request
epee::misc_utils::struct_init< request_t > request
Definition: cryptonote_protocol_defs.h:356
cryptonote::t_cryptonote_protocol_handler::get_block_queue
const block_queue & get_block_queue() const
Definition: cryptonote_protocol_handler.h:110
cryptonote::t_cryptonote_protocol_handler::m_no_sync
std::atomic< bool > m_no_sync
Definition: cryptonote_protocol_handler.h:159
cryptonote::t_cryptonote_protocol_handler::m_sync_pruned_blocks
bool m_sync_pruned_blocks
Definition: cryptonote_protocol_handler.h:172
tools::PerformanceTimer
Definition: perf_timer.h:48
cryptonote::t_cryptonote_protocol_handler::m_sync_search_checker
epee::math_helper::once_a_time_seconds< 101 > m_sync_search_checker
Definition: cryptonote_protocol_handler.h:165
cryptonote::CORE_SYNC_DATA
Definition: cryptonote_protocol_defs.h:251
cryptonote::NOTIFY_GET_TXPOOL_COMPLEMENT::request
epee::misc_utils::struct_init< request_t > request
Definition: cryptonote_protocol_defs.h:374
cryptonote::cryptonote_protocol_handler_base::estimate_one_block_size
virtual double estimate_one_block_size() noexcept
Definition: cryptonote_protocol_handler-base.cpp:82
cryptonote::blobdata
std::string blobdata
Definition: blobdatatype.h:39
cryptonote::t_cryptonote_protocol_handler::m_syncronized_connections_count
std::atomic< uint32_t > m_syncronized_connections_count
Definition: cryptonote_protocol_handler.h:156
bool
int bool
Definition: stdbool.h:35
cryptonote::t_cryptonote_protocol_handler::set_no_sync
void set_no_sync(bool value)
Definition: cryptonote_protocol_handler.h:115
cryptonote::cryptonote_protocol_handler_base::get_avg_block_size
virtual double get_avg_block_size()=0
cryptonote::t_cryptonote_protocol_handler::connection_context
cryptonote_connection_context connection_context
Definition: cryptonote_protocol_handler.h:79
cryptonote::NOTIFY_REQUEST_CHAIN::request
epee::misc_utils::struct_init< request_t > request
Definition: cryptonote_protocol_defs.h:286
cryptonote::cryptonote_protocol_handler_base::handler_request_blocks_history
void handler_request_blocks_history(std::list< crypto::hash > &ids)
Definition: cryptonote_protocol_handler-base.cpp:100
cryptonote::NOTIFY_NEW_BLOCK::request
epee::misc_utils::struct_init< request_t > request
Definition: cryptonote_protocol_defs.h:186
cryptonote::t_cryptonote_protocol_handler::m_p2p
nodetool::i_p2p_endpoint< connection_context > * m_p2p
Definition: cryptonote_protocol_handler.h:155
cryptonote::t_cryptonote_protocol_handler::no_sync
bool no_sync() const
Definition: cryptonote_protocol_handler.h:114
cryptonote::t_cryptonote_protocol_handler::m_sync_spans_downloaded
uint64_t m_sync_spans_downloaded
Definition: cryptonote_protocol_handler.h:169
cryptonote::NOTIFY_REQUEST_FLUFFY_MISSING_TX
Definition: cryptonote_protocol_defs.h:341
cryptonote::i_cryptonote_protocol
Definition: cryptonote_protocol_handler_common.h:42
cryptonote::t_cryptonote_protocol_handler::m_last_add_end_time
uint64_t m_last_add_end_time
Definition: cryptonote_protocol_handler.h:168
cryptonote::NOTIFY_REQUEST_CHAIN
Definition: cryptonote_protocol_defs.h:273
cryptonote::cryptonote_protocol_handler_base::mI
std::unique_ptr< cryptonote_protocol_handler_base_pimpl > mI
Definition: cryptonote_protocol_handler.h:63
cryptonote::NOTIFY_RESPONSE_GET_OBJECTS::request
epee::misc_utils::struct_init< request_t > request
Definition: cryptonote_protocol_defs.h:246
cryptonote::t_cryptonote_protocol_handler::m_block_queue
block_queue m_block_queue
Definition: cryptonote_protocol_handler.h:162
cryptonote::t_cryptonote_protocol_handler::m_core
t_core & m_core
Definition: cryptonote_protocol_handler.h:152
PUSH_WARNINGS
PUSH_WARNINGS
Definition: hash-ops.h:53
cryptonote::t_cryptonote_protocol_handler::cryptonote_protocol_handler
t_cryptonote_protocol_handler< t_core > cryptonote_protocol_handler
Definition: cryptonote_protocol_handler.h:80
CURRENCY_PROTOCOL_MAX_OBJECT_REQUEST_COUNT
#define CURRENCY_PROTOCOL_MAX_OBJECT_REQUEST_COUNT
Definition: cryptonote_protocol_handler.h:54
cryptonote::t_cryptonote_protocol_handler::m_sync_timer
tools::PerformanceTimer m_sync_timer
Definition: cryptonote_protocol_handler.h:167
anonymous_namespace{simplewallet.cpp}::name
const char * name
Definition: simplewallet.cpp:417
block_queue.h
cryptonote::NOTIFY_NEW_TRANSACTIONS::request
epee::misc_utils::struct_init< request_t > request
Definition: cryptonote_protocol_defs.h:208
cryptonote::cryptonote_protocol_handler_base::handler_response_blocks_now
void handler_response_blocks_now(size_t packet_size)
Definition: cryptonote_protocol_handler-base.cpp:103
cryptonote::t_cryptonote_protocol_handler::m_buffer_mutex
boost::mutex m_buffer_mutex
Definition: cryptonote_protocol_handler.h:182
nodetool
Definition: levin_notify.h:52
cryptonote::NOTIFY_NEW_FLUFFY_BLOCK::request
epee::misc_utils::struct_init< request_t > request
Definition: cryptonote_protocol_defs.h:334