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