diff --git a/contrib/epee/include/misc_language.h b/contrib/epee/include/misc_language.h index 5ccfe6fcc..ee07bbe8f 100644 --- a/contrib/epee/include/misc_language.h +++ b/contrib/epee/include/misc_language.h @@ -30,74 +30,14 @@ #include #include -#include -#include #include namespace epee { -#define STD_TRY_BEGIN() try { - -#define STD_TRY_CATCH(where_, ret_val) \ - } \ - catch (const std::exception &e) \ - { \ - LOG_ERROR("EXCEPTION: " << where_ << ", mes: "<< e.what()); \ - return ret_val; \ - } \ - catch (...) \ - { \ - LOG_ERROR("EXCEPTION: " << where_ ); \ - return ret_val; \ - } - - - #define AUTO_VAL_INIT(v) boost::value_initialized() namespace misc_utils { - template - t_type get_max_t_val(t_type t) - { - return (std::numeric_limits::max)(); - } - - - template - t_iterator move_it_forward(t_iterator it, size_t count) - { - while(count--) - it++; - return it; - } - - template - t_iterator move_it_backward(t_iterator it, size_t count) - { - while(count--) - it--; - return it; - } - - - // TEMPLATE STRUCT less - template - struct less_as_pod - : public std::binary_function<_Ty, _Ty, bool> - { // functor for operator< - bool operator()(const _Ty& _Left, const _Ty& _Right) const - { // apply operator< to operands - return memcmp(&_Left, &_Right, sizeof(_Left)) < 0; - } - }; - - template - bool is_less_as_pod(const _Ty& _Left, const _Ty& _Right) - { // apply operator< to operands - return memcmp(&_Left, &_Right, sizeof(_Left)) < 0; - } - - bool sleep_no_w(long ms ); + bool sleep_no_w(long ms); template T get_mid(const T &a, const T &b) diff --git a/src/p2p/net_peerlist.h b/src/p2p/net_peerlist.h index 0662789b9..a3b0d6871 100644 --- a/src/p2p/net_peerlist.h +++ b/src/p2p/net_peerlist.h @@ -31,6 +31,7 @@ #pragma once #include +#include #include #include #include @@ -184,6 +185,7 @@ namespace nodetool private: void trim_white_peerlist(); void trim_gray_peerlist(); + static peerlist_entry get_nth_latest_peer(peers_indexed& peerlist, size_t n); friend class boost::serialization::access; epee::critical_section m_peerlist_lock; @@ -214,6 +216,16 @@ namespace nodetool } } //-------------------------------------------------------------------------------------------------- + inline + peerlist_entry peerlist_manager::get_nth_latest_peer(peers_indexed& peerlist, const size_t n) + { + // Is not thread-safe nor does it check bounds. Do this before calling. Indexing starts at 0. + peers_indexed::index::type& by_time_index = peerlist.get(); + auto by_time_it = --by_time_index.end(); + std::advance(by_time_it, -((long long) n)); + return *by_time_it; + } + //-------------------------------------------------------------------------------------------------- inline bool peerlist_manager::merge_peerlist(const std::vector& outer_bs, const std::function &f) { @@ -235,8 +247,7 @@ namespace nodetool if(i >= m_peers_white.size()) return false; - peers_indexed::index::type& by_time_index = m_peers_white.get(); - p = *epee::misc_utils::move_it_backward(--by_time_index.end(), i); + p = peerlist_manager::get_nth_latest_peer(m_peers_white, i); return true; } //-------------------------------------------------------------------------------------------------- @@ -247,8 +258,7 @@ namespace nodetool if(i >= m_peers_gray.size()) return false; - peers_indexed::index::type& by_time_index = m_peers_gray.get(); - p = *epee::misc_utils::move_it_backward(--by_time_index.end(), i); + p = peerlist_manager::get_nth_latest_peer(m_peers_gray, i); return true; } //-------------------------------------------------------------------------------------------------- @@ -437,9 +447,7 @@ namespace nodetool } size_t random_index = crypto::rand_idx(m_peers_gray.size()); - - peers_indexed::index::type& by_time_index = m_peers_gray.get(); - pe = *epee::misc_utils::move_it_backward(--by_time_index.end(), random_index); + pe = peerlist_manager::get_nth_latest_peer(m_peers_gray, random_index); return true;