| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 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" |
| 42 |
#include "storages/levin_abstract_invoke2.h" |
| 43 |
#include "warnings.h" |
| 44 |
#include "cryptonote_protocol_defs.h" |
| 45 |
#include "cryptonote_protocol_handler_common.h" |
| 46 |
#include "block_queue.h" |
| 47 |
#include "common/perf_timer.h" |
| 48 |
#include "cryptonote_basic/connection_context.h" |
| 49 |
#include "net/levin_base.h" |
| 50 |
#include "p2p/net_node_common.h" |
| 51 |
#include <boost/circular_buffer.hpp> |
| 52 |
|
| 53 |
PUSH_WARNINGS |
| 54 |
DISABLE_VS_WARNINGS(4355) |
| 55 |
|
| 56 |
#define LOCALHOST_INT 2130706433 |
| 57 |
#define CURRENCY_PROTOCOL_MAX_OBJECT_REQUEST_COUNT 100 |
| 58 |
static_assert(CURRENCY_PROTOCOL_MAX_OBJECT_REQUEST_COUNT >= BLOCKS_SYNCHRONIZING_DEFAULT_COUNT_PRE_V4, "Invalid CURRENCY_PROTOCOL_MAX_OBJECT_REQUEST_COUNT"); |
| 59 |
|
| 60 |
namespace cryptonote |
| 61 |
{ |
| 62 |
|
| 63 |
class cryptonote_protocol_handler_base_pimpl; |
| 64 |
class cryptonote_protocol_handler_base { |
| 65 |
private: |
| 66 |
std::unique_ptr<cryptonote_protocol_handler_base_pimpl> mI; |
| 67 |
|
| 68 |
public: |
| 69 |
cryptonote_protocol_handler_base(); |
| 70 |
virtual ~cryptonote_protocol_handler_base(); |
| 71 |
void handler_request_blocks_history(std::list<crypto::hash>& ids); |
| 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; |
| 76 |
}; |
| 77 |
|
| 78 |
template<class t_core> |
| 79 |
class t_cryptonote_protocol_handler: public i_cryptonote_protocol, cryptonote_protocol_handler_base |
| 80 |
{ |
| 81 |
public: |
| 82 |
typedef cryptonote_connection_context connection_context; |
| 83 |
typedef t_cryptonote_protocol_handler<t_core> cryptonote_protocol_handler; |
| 84 |
typedef CORE_SYNC_DATA payload_type; |
| 85 |
|
| 86 |
t_cryptonote_protocol_handler(t_core& rcore, nodetool::i_p2p_endpoint<connection_context>* p_net_layout, bool offline = false); |
| 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 |
HANDLE_NOTIFY_T2(NOTIFY_GET_TXPOOL_COMPLEMENT, &cryptonote_protocol_handler::handle_notify_get_txpool_complement) |
| 98 |
END_INVOKE_MAP2() |
| 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 |
|
| 105 |
bool process_payload_sync_data(const CORE_SYNC_DATA& hshd, cryptonote_connection_context& context, bool is_inital); |
| 106 |
bool get_payload_sync_data(epee::byte_slice& data); |
| 107 |
bool get_payload_sync_data(CORE_SYNC_DATA& hshd); |
| 108 |
bool on_callback(cryptonote_connection_context& context); |
| 109 |
t_core& get_core(){return m_core;} |
| 110 |
virtual bool is_synchronized() const final { return !no_sync() && 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 |
void set_max_out_peers(epee::net_utils::zone zone, unsigned int max) { CRITICAL_REGION_LOCAL(m_max_out_peers_lock); m_max_out_peers[zone] = max; } |
| 117 |
unsigned int get_max_out_peers(epee::net_utils::zone zone) const |
| 118 |
{ |
| 119 |
CRITICAL_REGION_LOCAL(m_max_out_peers_lock); |
| 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"); |
| 124 |
return P2P_DEFAULT_CONNECTIONS_COUNT; |
| 125 |
} |
| 126 |
return it->second; |
| 127 |
} |
| 128 |
bool no_sync() const { return m_no_sync; } |
| 129 |
void set_no_sync(bool value) { m_no_sync = value; } |
| 130 |
std::string get_peers_overview() const; |
| 131 |
std::pair<uint32_t, uint32_t> get_next_needed_pruning_stripe() const; |
| 132 |
bool needs_new_sync_connections(epee::net_utils::zone zone) const; |
| 133 |
bool is_busy_syncing(); |
| 134 |
|
| 135 |
private: |
| 136 |
|
| 137 |
int handle_notify_new_block(int command, NOTIFY_NEW_BLOCK::request& arg, cryptonote_connection_context& context); |
| 138 |
int handle_notify_new_transactions(int command, NOTIFY_NEW_TRANSACTIONS::request& arg, cryptonote_connection_context& context); |
| 139 |
int handle_request_get_objects(int command, NOTIFY_REQUEST_GET_OBJECTS::request& arg, cryptonote_connection_context& context); |
| 140 |
int handle_response_get_objects(int command, NOTIFY_RESPONSE_GET_OBJECTS::request& arg, cryptonote_connection_context& context); |
| 141 |
int handle_request_chain(int command, NOTIFY_REQUEST_CHAIN::request& arg, cryptonote_connection_context& context); |
| 142 |
int handle_response_chain_entry(int command, NOTIFY_RESPONSE_CHAIN_ENTRY::request& arg, cryptonote_connection_context& context); |
| 143 |
int handle_notify_new_fluffy_block(int command, NOTIFY_NEW_FLUFFY_BLOCK::request& arg, cryptonote_connection_context& context); |
| 144 |
int handle_request_fluffy_missing_tx(int command, NOTIFY_REQUEST_FLUFFY_MISSING_TX::request& arg, cryptonote_connection_context& context); |
| 145 |
int handle_notify_get_txpool_complement(int command, NOTIFY_GET_TXPOOL_COMPLEMENT::request& arg, cryptonote_connection_context& context); |
| 146 |
|
| 147 |
|
| 148 |
virtual bool relay_block(NOTIFY_NEW_FLUFFY_BLOCK::request& arg, cryptonote_connection_context& exclude_context); |
| 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 |
|
| 152 |
bool should_drop_connection(cryptonote_connection_context& context, uint32_t next_stripe); |
| 153 |
bool request_missing_objects(cryptonote_connection_context& context, bool check_having_blocks, bool force_next_span = false); |
| 154 |
size_t get_synchronizing_connections_count(); |
| 155 |
bool on_connection_synchronized(); |
| 156 |
bool should_download_next_span(cryptonote_connection_context& context, bool standby); |
| 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&); |
| 161 |
void drop_connections(const epee::net_utils::network_address address); |
| 162 |
bool kick_idle_peers(); |
| 163 |
bool check_standby_peers(); |
| 164 |
bool update_sync_search(); |
| 165 |
int try_add_next_blocks(cryptonote_connection_context &context); |
| 166 |
void notify_new_stripe(cryptonote_connection_context &context, uint32_t stripe); |
| 167 |
size_t skip_unneeded_hashes(cryptonote_connection_context& context, bool check_block_queue) const; |
| 168 |
bool request_txpool_complement(cryptonote_connection_context &context); |
| 169 |
void hit_score(cryptonote_connection_context &context, int32_t score); |
| 170 |
|
| 171 |
t_core& m_core; |
| 172 |
|
| 173 |
nodetool::p2p_endpoint_stub<connection_context> m_p2p_stub; |
| 174 |
nodetool::i_p2p_endpoint<connection_context>* m_p2p; |
| 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; |
| 179 |
std::atomic<bool> m_ask_for_txpool_complement; |
| 180 |
boost::mutex m_sync_lock; |
| 181 |
block_queue m_block_queue; |
| 182 |
epee::math_helper::once_a_time_seconds<8> m_idle_peer_kicker; |
| 183 |
epee::math_helper::once_a_time_milliseconds<100> m_standby_checker; |
| 184 |
epee::math_helper::once_a_time_seconds<101> m_sync_search_checker; |
| 185 |
epee::math_helper::once_a_time_seconds<43> m_bad_peer_checker; |
| 186 |
std::unordered_map<epee::net_utils::zone, unsigned int> m_max_out_peers; |
| 187 |
mutable epee::critical_section m_max_out_peers_lock; |
| 188 |
tools::PerformanceTimer m_sync_timer, m_add_timer; |
| 189 |
uint64_t m_last_add_end_time; |
| 190 |
uint64_t m_sync_spans_downloaded, m_sync_old_spans_downloaded, m_sync_bad_spans_downloaded; |
| 191 |
uint64_t m_sync_download_chain_size, m_sync_download_objects_size; |
| 192 |
size_t m_block_download_max_size; |
| 193 |
bool m_sync_pruned_blocks; |
| 194 |
|
| 195 |
|
| 196 |
boost::posix_time::ptime m_sync_start_time; |
| 197 |
boost::posix_time::ptime m_period_start_time; |
| 198 |
uint64_t m_sync_start_height; |
| 199 |
uint64_t m_period_start_height; |
| 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; |
| 204 |
double get_avg_block_size(); |
| 205 |
boost::circular_buffer<size_t> m_avg_buffer = boost::circular_buffer<size_t>(10); |
| 206 |
|
| 207 |
boost::mutex m_bad_peer_check_lock; |
| 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}; |
| 215 |
epee::serialization::store_t_to_binary(arg, out.buffer); |
| 216 |
|
| 217 |
return m_p2p->invoke_notify_to_peer(t_parameter::ID, std::move(out), context); |
| 218 |
} |
| 219 |
}; |
| 220 |
|
| 221 |
} |
| 222 |
|
| 223 |
POP_WARNINGS |