Monero
Loading...
Searching...
No Matches
cryptonote_protocol_handler.h
Go to the documentation of this file.
1
4
5// Copyright (c) 2014-2022, 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 "byte_slice.h"
41#include "math_helper.h"
43#include "warnings.h"
46#include "block_queue.h"
47#include "common/perf_timer.h"
49#include "net/levin_base.h"
50#include "p2p/net_node_common.h"
51#include <boost/circular_buffer.hpp>
52
55
56#define LOCALHOST_INT 2130706433
57#define CURRENCY_PROTOCOL_MAX_OBJECT_REQUEST_COUNT 100
58static_assert(CURRENCY_PROTOCOL_MAX_OBJECT_REQUEST_COUNT >= BLOCKS_SYNCHRONIZING_DEFAULT_COUNT_PRE_V4, "Invalid CURRENCY_PROTOCOL_MAX_OBJECT_REQUEST_COUNT");
59
60namespace cryptonote
61{
62
63 class cryptonote_protocol_handler_base_pimpl;
65 private:
66 std::unique_ptr<cryptonote_protocol_handler_base_pimpl> mI;
67
68 public:
71 void handler_request_blocks_history(std::list<crypto::hash>& ids); // before asking for list of objects, we can change the list still
72 void handler_response_blocks_now(size_t packet_size);
73
74 virtual double get_avg_block_size() = 0;
75 virtual double estimate_one_block_size() noexcept; // for estimating size of blocks to download
76 };
77
78 template<class t_core>
80 {
81 public:
85
86 t_cryptonote_protocol_handler(t_core& rcore, nodetool::i_p2p_endpoint<connection_context>* p_net_layout, bool offline = false);
87
99
100 bool on_idle();
101 bool init(const boost::program_options::variables_map& vm);
102 bool deinit();
104 //bool process_handshake_data(const blobdata& data, cryptonote_connection_context& context);
106 bool get_payload_sync_data(epee::byte_slice& data);
109 t_core& get_core(){return m_core;}
110 virtual bool is_synchronized() const final { return !no_sync() && m_synchronized; }
112 std::list<connection_info> get_connections();
113 const block_queue &get_block_queue() const { return m_block_queue; }
114 void stop();
118 {
120 const auto it = m_max_out_peers.find(zone);
121 if (it == m_max_out_peers.end())
122 {
123 MWARNING(epee::net_utils::zone_to_string(zone) << " max out peers not set, using default");
125 }
126 return it->second;
127 }
128 bool no_sync() const { return m_no_sync; }
130 std::string get_peers_overview() const;
131 std::pair<uint32_t, uint32_t> get_next_needed_pruning_stripe() const;
134
135 private:
136 //----------------- commands handlers ----------------------------------------------
146
147 //----------------- i_bc_protocol_layout ---------------------------------------
149 virtual bool relay_transactions(NOTIFY_NEW_TRANSACTIONS::request& arg, const boost::uuids::uuid& source, epee::net_utils::zone zone, relay_method tx_relay);
150 //----------------------------------------------------------------------------------
151 //bool get_payload_sync_data(HANDSHAKE_DATA::request& hshd, cryptonote_connection_context& context);
153 bool request_missing_objects(cryptonote_connection_context& context, bool check_having_blocks, bool force_next_span = false);
157 bool should_ask_for_pruned_data(cryptonote_connection_context& context, uint64_t first_block_height, uint64_t nblocks, bool check_block_weights) const;
158 void drop_connection(cryptonote_connection_context &context, bool add_fail, bool flush_all_spans);
159 void drop_connection_with_score(cryptonote_connection_context &context, unsigned int score, bool flush_all_spans);
160 void drop_connection(const boost::uuids::uuid&);
167 size_t skip_unneeded_hashes(cryptonote_connection_context& context, bool check_block_queue) const;
170
171 t_core& m_core;
172
175 std::atomic<uint32_t> m_syncronized_connections_count;
176 std::atomic<bool> m_synchronized;
177 std::atomic<bool> m_stopping;
178 std::atomic<bool> m_no_sync;
180 boost::mutex m_sync_lock;
186 std::unordered_map<epee::net_utils::zone, unsigned int> m_max_out_peers;
194
195 // Values for sync time estimates
196 boost::posix_time::ptime m_sync_start_time;
197 boost::posix_time::ptime m_period_start_time;
200 uint64_t get_estimated_remaining_sync_seconds(uint64_t current_blockchain_height, uint64_t target_blockchain_height);
201 std::string get_periodic_sync_estimate(uint64_t current_blockchain_height, uint64_t target_blockchain_height);
202
203 boost::mutex m_buffer_mutex;
205 boost::circular_buffer<size_t> m_avg_buffer = boost::circular_buffer<size_t>(10);
206
208
209 template<class t_parameter>
210 bool post_notify(typename t_parameter::request& arg, cryptonote_connection_context& context)
211 {
212 LOG_PRINT_L2("[" << epee::net_utils::print_connection_context_short(context) << "] post " << typeid(t_parameter).name() << " -->");
213
214 epee::levin::message_writer out{256 * 1024}; // optimize for block responses
216 //handler_response_blocks_now(blob.size()); // XXX
217 return m_p2p->invoke_notify_to_peer(t_parameter::ID, std::move(out), context);
218 }
219 };
220
221} // namespace
222
Definition block_queue.h:49
virtual double estimate_one_block_size() noexcept
Definition cryptonote_protocol_handler-base.cpp:81
std::unique_ptr< cryptonote_protocol_handler_base_pimpl > mI
Definition cryptonote_protocol_handler.h:66
void handler_response_blocks_now(size_t packet_size)
Definition cryptonote_protocol_handler-base.cpp:102
virtual ~cryptonote_protocol_handler_base()
Definition cryptonote_protocol_handler-base.cpp:96
void handler_request_blocks_history(std::list< crypto::hash > &ids)
Definition cryptonote_protocol_handler-base.cpp:99
cryptonote_protocol_handler_base()
Definition cryptonote_protocol_handler-base.cpp:93
std::atomic< bool > m_ask_for_txpool_complement
Definition cryptonote_protocol_handler.h:179
uint64_t get_estimated_remaining_sync_seconds(uint64_t current_blockchain_height, uint64_t target_blockchain_height)
Definition cryptonote_protocol_handler.inl:1276
bool m_sync_pruned_blocks
Definition cryptonote_protocol_handler.h:193
void drop_connection(cryptonote_connection_context &context, bool add_fail, bool flush_all_spans)
Definition cryptonote_protocol_handler.inl:2806
boost::mutex m_bad_peer_check_lock
Definition cryptonote_protocol_handler.h:207
virtual bool is_synchronized() const final
Definition cryptonote_protocol_handler.h:110
epee::math_helper::once_a_time_seconds< 101 > m_sync_search_checker
Definition cryptonote_protocol_handler.h:184
uint64_t m_last_add_end_time
Definition cryptonote_protocol_handler.h:189
std::pair< uint32_t, uint32_t > get_next_needed_pruning_stripe() const
Definition cryptonote_protocol_handler.inl:2725
t_cryptonote_protocol_handler(t_core &rcore, nodetool::i_p2p_endpoint< connection_context > *p_net_layout, bool offline=false)
Definition cryptonote_protocol_handler.inl:182
void set_p2p_endpoint(nodetool::i_p2p_endpoint< connection_context > *p2p)
int handle_notify_new_transactions(int command, NOTIFY_NEW_TRANSACTIONS::request &arg, cryptonote_connection_context &context)
bool request_missing_objects(cryptonote_connection_context &context, bool check_having_blocks, bool force_next_span=false)
Definition cryptonote_protocol_handler.inl:2007
uint64_t m_sync_download_chain_size
Definition cryptonote_protocol_handler.h:191
std::atomic< bool > m_no_sync
Definition cryptonote_protocol_handler.h:178
void set_no_sync(bool value)
Definition cryptonote_protocol_handler.h:129
bool post_notify(typename t_parameter::request &arg, cryptonote_connection_context &context)
Definition cryptonote_protocol_handler.h:210
void hit_score(cryptonote_connection_context &context, int32_t score)
Definition cryptonote_protocol_handler.inl:2695
void set_max_out_peers(epee::net_utils::zone zone, unsigned int max)
Definition cryptonote_protocol_handler.h:116
boost::mutex m_buffer_mutex
Definition cryptonote_protocol_handler.h:203
std::string get_peers_overview() const
Definition cryptonote_protocol_handler.inl:2708
bool process_payload_sync_data(const CORE_SYNC_DATA &hshd, cryptonote_connection_context &context, bool is_inital)
void notify_new_stripe(cryptonote_connection_context &context, uint32_t stripe)
Definition cryptonote_protocol_handler.inl:1654
double get_avg_block_size()
Definition cryptonote_protocol_handler.inl:1013
std::string get_periodic_sync_estimate(uint64_t current_blockchain_height, uint64_t target_blockchain_height)
Definition cryptonote_protocol_handler.inl:1306
bool should_ask_for_pruned_data(cryptonote_connection_context &context, uint64_t first_block_height, uint64_t nblocks, bool check_block_weights) const
Definition cryptonote_protocol_handler.inl:1983
bool init(const boost::program_options::variables_map &vm)
uint64_t m_sync_old_spans_downloaded
Definition cryptonote_protocol_handler.h:190
virtual bool relay_transactions(NOTIFY_NEW_TRANSACTIONS::request &arg, const boost::uuids::uuid &source, epee::net_utils::zone zone, relay_method tx_relay)
Definition cryptonote_protocol_handler.inl:2669
bool request_txpool_complement(cryptonote_connection_context &context)
Definition cryptonote_protocol_handler.inl:2680
t_cryptonote_protocol_handler< t_core > cryptonote_protocol_handler
Definition cryptonote_protocol_handler.h:83
void drop_connections(const epee::net_utils::network_address address)
Definition cryptonote_protocol_handler.inl:2822
nodetool::i_p2p_endpoint< connection_context > * m_p2p
Definition cryptonote_protocol_handler.h:174
int handle_request_get_objects(int command, NOTIFY_REQUEST_GET_OBJECTS::request &arg, cryptonote_connection_context &context)
int try_add_next_blocks(cryptonote_connection_context &context)
Definition cryptonote_protocol_handler.inl:1324
unsigned int get_max_out_peers(epee::net_utils::zone zone) const
Definition cryptonote_protocol_handler.h:117
bool kick_idle_peers()
Definition cryptonote_protocol_handler.inl:1685
bool update_sync_search()
Definition cryptonote_protocol_handler.inl:1711
int handle_request_chain(int command, NOTIFY_REQUEST_CHAIN::request &arg, cryptonote_connection_context &context)
void drop_connection(const boost::uuids::uuid &)
Definition cryptonote_protocol_handler.inl:2812
epee::math_helper::once_a_time_seconds< 8 > m_idle_peer_kicker
Definition cryptonote_protocol_handler.h:182
uint64_t m_sync_download_objects_size
Definition cryptonote_protocol_handler.h:191
block_queue m_block_queue
Definition cryptonote_protocol_handler.h:181
int handle_response_chain_entry(int command, NOTIFY_RESPONSE_CHAIN_ENTRY::request &arg, cryptonote_connection_context &context)
boost::circular_buffer< size_t > m_avg_buffer
Definition cryptonote_protocol_handler.h:205
bool should_download_next_span(cryptonote_connection_context &context, bool standby)
Definition cryptonote_protocol_handler.inl:1815
cryptonote::core & m_core
Definition cryptonote_protocol_handler.h:171
CORE_SYNC_DATA payload_type
Definition cryptonote_protocol_handler.h:84
cryptonote::core & get_core()
Definition cryptonote_protocol_handler.h:109
size_t get_synchronizing_connections_count()
Definition cryptonote_protocol_handler.inl:2462
size_t skip_unneeded_hashes(cryptonote_connection_context &context, bool check_block_queue) const
Definition cryptonote_protocol_handler.inl:1962
virtual bool relay_block(NOTIFY_NEW_FLUFFY_BLOCK::request &arg, cryptonote_connection_context &exclude_context)
Definition cryptonote_protocol_handler.inl:2642
bool is_busy_syncing()
Definition cryptonote_protocol_handler.inl:2784
epee::math_helper::once_a_time_milliseconds< 100 > m_standby_checker
Definition cryptonote_protocol_handler.h:183
std::unordered_map< epee::net_utils::zone, unsigned int > m_max_out_peers
Definition cryptonote_protocol_handler.h:186
int handle_notify_new_fluffy_block(int command, NOTIFY_NEW_FLUFFY_BLOCK::request &arg, cryptonote_connection_context &context)
bool on_callback(cryptonote_connection_context &context)
boost::posix_time::ptime m_sync_start_time
Definition cryptonote_protocol_handler.h:196
bool check_standby_peers()
Definition cryptonote_protocol_handler.inl:1768
std::list< connection_info > get_connections()
Definition cryptonote_protocol_handler.inl:361
uint64_t m_sync_start_height
Definition cryptonote_protocol_handler.h:198
int handle_notify_new_block(int command, NOTIFY_NEW_BLOCK::request &arg, cryptonote_connection_context &context)
epee::critical_section m_max_out_peers_lock
Definition cryptonote_protocol_handler.h:187
bool no_sync() const
Definition cryptonote_protocol_handler.h:128
std::atomic< uint32_t > m_syncronized_connections_count
Definition cryptonote_protocol_handler.h:175
std::atomic< bool > m_stopping
Definition cryptonote_protocol_handler.h:177
epee::math_helper::once_a_time_seconds< 43 > m_bad_peer_checker
Definition cryptonote_protocol_handler.h:185
nodetool::p2p_endpoint_stub< connection_context > m_p2p_stub
Definition cryptonote_protocol_handler.h:173
size_t m_block_download_max_size
Definition cryptonote_protocol_handler.h:192
boost::mutex m_sync_lock
Definition cryptonote_protocol_handler.h:180
tools::PerformanceTimer m_add_timer
Definition cryptonote_protocol_handler.h:188
int handle_notify_get_txpool_complement(int command, NOTIFY_GET_TXPOOL_COMPLEMENT::request &arg, cryptonote_connection_context &context)
bool on_connection_synchronized()
Definition cryptonote_protocol_handler.inl:2390
cryptonote_connection_context connection_context
Definition cryptonote_protocol_handler.h:82
void stop()
Definition cryptonote_protocol_handler.inl:2872
int handle_response_get_objects(int command, NOTIFY_RESPONSE_GET_OBJECTS::request &arg, cryptonote_connection_context &context)
void drop_connection_with_score(cryptonote_connection_context &context, unsigned int score, bool flush_all_spans)
Definition cryptonote_protocol_handler.inl:2791
uint64_t m_sync_spans_downloaded
Definition cryptonote_protocol_handler.h:190
bool needs_new_sync_connections(epee::net_utils::zone zone) const
Definition cryptonote_protocol_handler.inl:2766
uint64_t m_sync_bad_spans_downloaded
Definition cryptonote_protocol_handler.h:190
boost::posix_time::ptime m_period_start_time
Definition cryptonote_protocol_handler.h:197
uint64_t m_period_start_height
Definition cryptonote_protocol_handler.h:199
std::atomic< bool > m_synchronized
Definition cryptonote_protocol_handler.h:176
int handle_request_fluffy_missing_tx(int command, NOTIFY_REQUEST_FLUFFY_MISSING_TX::request &arg, cryptonote_connection_context &context)
void on_connection_close(cryptonote_connection_context &context)
Definition cryptonote_protocol_handler.inl:2846
bool should_drop_connection(cryptonote_connection_context &context, uint32_t next_stripe)
Definition cryptonote_protocol_handler.inl:1900
tools::PerformanceTimer m_sync_timer
Definition cryptonote_protocol_handler.h:188
void log_connections()
Definition cryptonote_protocol_handler.inl:296
const block_queue & get_block_queue() const
Definition cryptonote_protocol_handler.h:113
Definition syncobj.h:82
Provides space for levin (p2p) header, so that payload can be sent without copy.
Definition levin_base.h:132
Definition math_helper.h:297
Definition net_utils_base.h:225
Definition perf_timer.h:49
#define BLOCKS_SYNCHRONIZING_DEFAULT_COUNT_PRE_V4
Definition cryptonote_config.h:98
#define P2P_DEFAULT_CONNECTIONS_COUNT
Definition cryptonote_config.h:139
#define CURRENCY_PROTOCOL_MAX_OBJECT_REQUEST_COUNT
Definition cryptonote_protocol_handler.h:57
#define const
Definition ipfrdr.c:80
#define END_INVOKE_MAP2()
Definition levin_abstract_invoke2.h:285
#define HANDLE_NOTIFY_T2(NOTIFY, func)
Definition levin_abstract_invoke2.h:259
#define BEGIN_INVOKE_MAP2(owner_type)
Definition levin_abstract_invoke2.h:240
uint32_t address
Definition getifaddr.c:269
Definition portable_binary_archive.hpp:29
Holds cryptonote related classes and helpers.
Definition blockchain_db.cpp:45
relay_method
Methods tracking how a tx was received and relayed.
Definition enums.h:37
const char * zone_to_string(zone value) noexcept
Definition net_utils_base.cpp:135
zone
Definition enums.h:50
std::string print_connection_context_short(const connection_context_base &ctx)
Definition net_utils_base.cpp:128
bool store_t_to_binary(t_struct &str_in, byte_slice &binary_buff, size_t initial_buffer_size=8192)
Definition portable_storage_template_helper.h:118
TODO: (mj-xmr) This will be reduced in an another PR.
Definition byte_slice.h:40
Definition levin_notify.h:52
Definition p2p.py:1
const char * name
Definition options.c:30
const GenericPointer< typename T::ValueType > T2 value
Definition pointer.h:1225
const CharType(& source)[N]
Definition pointer.h:1147
unsigned int uint32_t
Definition stdint.h:126
signed int int32_t
Definition stdint.h:123
unsigned __int64 uint64_t
Definition stdint.h:136
Definition cryptonote_protocol_defs.h:251
Definition cryptonote_protocol_defs.h:365
epee::misc_utils::struct_init< request_t > request
Definition cryptonote_protocol_defs.h:376
Definition cryptonote_protocol_defs.h:173
epee::misc_utils::struct_init< request_t > request
Definition cryptonote_protocol_defs.h:186
Definition cryptonote_protocol_defs.h:323
epee::misc_utils::struct_init< request_t > request
Definition cryptonote_protocol_defs.h:336
Definition cryptonote_protocol_defs.h:193
epee::misc_utils::struct_init< request_t > request
Definition cryptonote_protocol_defs.h:208
Definition cryptonote_protocol_defs.h:273
epee::misc_utils::struct_init< request_t > request
Definition cryptonote_protocol_defs.h:286
Definition cryptonote_protocol_defs.h:343
epee::misc_utils::struct_init< request_t > request
Definition cryptonote_protocol_defs.h:358
Definition cryptonote_protocol_defs.h:214
epee::misc_utils::struct_init< request_t > request
Definition cryptonote_protocol_defs.h:227
Definition cryptonote_protocol_defs.h:290
epee::misc_utils::struct_init< request_t > request
Definition cryptonote_protocol_defs.h:316
Definition cryptonote_protocol_defs.h:231
epee::misc_utils::struct_init< request_t > request
Definition cryptonote_protocol_defs.h:246
Definition connection_context.h:44
Definition cryptonote_protocol_handler_common.h:42
Definition net_node_common.h:53
Definition net_node_common.h:74
#define CRITICAL_REGION_LOCAL(x)
Definition syncobj.h:153
std::string data
Definition base58.cpp:37
randomx_vm * vm
Definition tests.cpp:20
#define DISABLE_VS_WARNINGS(w)
Definition warnings.h:18
#define POP_WARNINGS
Definition warnings.h:17
#define PUSH_WARNINGS
Definition warnings.h:16