diff options
author | Miguel Herranz <miguel@ipglider.org> | 2017-01-21 00:59:04 +0100 |
---|---|---|
committer | Miguel Herranz <miguel@ipglider.org> | 2017-01-21 10:09:38 +0100 |
commit | 82dbeedd1bb92a6db31b2e887fee59b4382dc582 (patch) | |
tree | 4d14d35ae9a31bc2449a93ec458f509c75fd4193 /src/p2p/net_peerlist.h | |
parent | Merge pull request #1599 (diff) | |
download | monero-82dbeedd1bb92a6db31b2e887fee59b4382dc582.tar.xz |
Add gray peer list housekeeping system
A random peer from the gray peer list is selected and a connection is
made to check if the peer is alive.
If the connection and handshake are successful the peer is promoted to
the white peer list, in case of failure the peer is evicted from the
gray peer list.
The connection is closed after the check in either case.
Diffstat (limited to '')
-rw-r--r-- | src/p2p/net_peerlist.h | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/p2p/net_peerlist.h b/src/p2p/net_peerlist.h index db9387ceb..c230c8c71 100644 --- a/src/p2p/net_peerlist.h +++ b/src/p2p/net_peerlist.h @@ -81,6 +81,8 @@ namespace nodetool bool set_peer_just_seen(peerid_type peer, const net_address& addr); bool set_peer_unreachable(const peerlist_entry& pr); bool is_ip_allowed(uint32_t ip); + bool get_gray_peer_random(peerlist_entry& pe); + bool remove_from_peer_gray(const peerlist_entry& pe); private: @@ -396,6 +398,50 @@ namespace nodetool return true; } //-------------------------------------------------------------------------------------------------- + inline + bool peerlist_manager::get_gray_peer_random(peerlist_entry& pe) + { + TRY_ENTRY(); + + CRITICAL_REGION_LOCAL(m_peerlist_lock); + + if (m_peers_gray.empty()) { + return false; + } + + size_t x = crypto::rand<size_t>() % (m_peers_gray.size() + 1); + size_t res = (x * x * x) / (m_peers_gray.size() * m_peers_gray.size()); //parabola \/ + + LOG_PRINT_L3("Random gray peer index=" << res << "(x="<< x << ", max_index=" << m_peers_gray.size() << ")"); + + peers_indexed::index<by_time>::type& by_time_index = m_peers_gray.get<by_time>(); + pe = *epee::misc_utils::move_it_backward(--by_time_index.end(), res); + + return true; + + CATCH_ENTRY_L0("peerlist_manager::get_gray_peer_random()", false); + return true; + } + //-------------------------------------------------------------------------------------------------- + inline + bool peerlist_manager::remove_from_peer_gray(const peerlist_entry& pe) + { + TRY_ENTRY(); + + CRITICAL_REGION_LOCAL(m_peerlist_lock); + + peers_indexed::index_iterator<by_addr>::type iterator = m_peers_gray.get<by_addr>().find(pe.adr); + + if (iterator != m_peers_gray.get<by_addr>().end()) { + m_peers_gray.erase(iterator); + } + + return true; + + CATCH_ENTRY_L0("peerlist_manager::remove_from_peer_gray()", false); + return true; + } + //-------------------------------------------------------------------------------------------------- } BOOST_CLASS_VERSION(nodetool::peerlist_manager, CURRENT_PEERLIST_STORAGE_ARCHIVE_VER) |