Debian Codegraph @

src/cryptonote_protocol/cryptonote_protocol_handler-base.cpp

raw symbols SCIP
1 /// @file
2 /// @author rfree (current maintainer in monero.cc project)
3 /// @brief This is the place to implement our handlers for protocol network actions, e.g. for ratelimit for download-requests
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 #include <string>
34 #include <vector>
35
36 #include <memory>
37
38 #include "syncobj.h"
39
40 #include "net/net_utils_base.h"
41 #include "misc_log_ex.h"
42 #include <boost/chrono.hpp>
43 #include <boost/date_time/posix_time/posix_time.hpp>
44 #include <boost/thread/thread.hpp>
45 #include "misc_language.h"
46 #include <algorithm>
47
48
49 #include "cryptonote_protocol_handler.h"
50 #include "net/network_throttle.hpp"
51
52 #include "cryptonote_core/cryptonote_core.h" // e.g. for the send_stop_signal()
53
54 #undef MONERO_DEFAULT_LOG_CATEGORY
55 #define MONERO_DEFAULT_LOG_CATEGORY "net.cn"
56
57 // ################################################################################################
58 // ################################################################################################
59 // the "header part". Not separated out for .hpp because point of this modification is
60 // to rebuild just 1 translation unit while working on this code.
61 // (But maybe common parts will be separated out later though - if needed)
62 // ################################################################################################
63 // ################################################################################################
64
65 namespace cryptonote {
66
67 class cryptonote_protocol_handler_base_pimpl { // placeholer if needed
68 public:
69
70 };
71
72 } // namespace
73
74 // ################################################################################################
75 // ################################################################################################
76 // ################################################################################################
77 // ################################################################################################
78
79 namespace cryptonote {
80
81 double cryptonote_protocol_handler_base::estimate_one_block_size() noexcept { // for estimating size of blocks to downloa
82 const double size_min = 500; // XXX 500
83 //const int history_len = 20; // how many blocks to average over
84
85 double avg=0;
86 try {
87 avg = get_avg_block_size(/*history_len*/);
88 } catch (...) { }
89 avg = std::max( size_min , avg);
90 return avg;
91 }
92
93 cryptonote_protocol_handler_base::cryptonote_protocol_handler_base() {
94 }
95
96 cryptonote_protocol_handler_base::~cryptonote_protocol_handler_base() {
97 }
98
99 void cryptonote_protocol_handler_base::handler_request_blocks_history(std::list<crypto::hash>& ids) {
100 }
101
102 void cryptonote_protocol_handler_base::handler_response_blocks_now(size_t packet_size) {
103 using namespace epee::net_utils;
104 double delay=0; // will be calculated
105 MDEBUG("Packet size: " << packet_size);
106 do
107 { // rate limiting
108 //XXX
109 /*if (::cryptonote::core::get_is_stopping()) {
110 MDEBUG("We are stopping - so abort sleep");
111 return;
112 }*/
113 /*if (m_was_shutdown) {
114 MDEBUG("m_was_shutdown - so abort sleep");
115 return;
116 }*/
117
118 {
119 CRITICAL_REGION_LOCAL( network_throttle_manager::m_lock_get_global_throttle_out );
120 delay = network_throttle_manager::get_global_throttle_out().get_sleep_time_after_tick( packet_size );
121 }
122
123
124 delay *= 0.50;
125 //delay = 0; // XXX
126 if (delay > 0) {
127 //delay += rand2*0.1;
128 long int ms = (long int)(delay * 1000);
129 MDEBUG("Sleeping for " << ms << " ms before packet_size="<<packet_size); // XXX debug sleep
130 boost::this_thread::sleep(boost::posix_time::milliseconds( ms ) ); // TODO randomize sleeps
131 }
132 } while(delay > 0);
133
134 // XXX LATER XXX
135 {
136 CRITICAL_REGION_LOCAL( network_throttle_manager::m_lock_get_global_throttle_out );
137 network_throttle_manager::get_global_throttle_out().handle_trafic_tcp( packet_size ); // increase counter - global
138 //epee::critical_region_t<decltype(m_throttle_global_lock)> guard(m_throttle_global_lock); // *** critical ***
139 //m_throttle_global.m_out.handle_trafic_tcp( packet_size ); // increase counter - global
140 }
141 }
142
143 } // namespace
144
145