diff --git a/src/p2p_server.cpp b/src/p2p_server.cpp index 855cb31..21e9489 100644 --- a/src/p2p_server.cpp +++ b/src/p2p_server.cpp @@ -52,6 +52,7 @@ P2PServer::P2PServer(p2pool* pool) , m_block(new PoolBlock()) , m_timer{} , m_timerCounter(0) + , m_timerInterval(2) , m_peerId(m_rng()) , m_peerListLastSaved(0) { @@ -86,7 +87,7 @@ P2PServer::P2PServer(p2pool* pool) } m_timer.data = this; - err = uv_timer_start(&m_timer, on_timer, 1000, 2000); + err = uv_timer_start(&m_timer, on_timer, 1000, m_timerInterval * 1000); if (err) { LOGERR(1, "failed to start timer, error " << uv_err_name(err)); panic(); @@ -281,7 +282,6 @@ void P2PServer::update_peer_connections() void P2PServer::update_peer_list() { - const time_t cur_time = time(nullptr); { MutexLock lock(m_clientsListLock); @@ -290,9 +290,9 @@ void P2PServer::update_peer_list() continue; } - if (cur_time >= client->m_nextOutgoingPeerListRequest) { + if (m_timerCounter >= client->m_nextOutgoingPeerListRequest) { // Send peer list requests at random intervals (60-120 seconds) - client->m_nextOutgoingPeerListRequest = cur_time + 60 + (get_random64() % 61); + client->m_nextOutgoingPeerListRequest = m_timerCounter + (60 + (get_random64() % 61)) / m_timerInterval; const bool result = send(client, [](void* buf) diff --git a/src/p2p_server.h b/src/p2p_server.h index d5f5f9e..6afe72f 100644 --- a/src/p2p_server.h +++ b/src/p2p_server.h @@ -111,7 +111,7 @@ public: int m_listenPort; time_t m_prevIncomingPeerListRequest; - time_t m_nextOutgoingPeerListRequest; + uint64_t m_nextOutgoingPeerListRequest; std::chrono::system_clock::time_point m_lastPeerListRequestTime; int m_peerListPendingRequests; int64_t m_pingTime; @@ -176,7 +176,8 @@ private: PoolBlock* m_block; uv_timer_t m_timer; - uint32_t m_timerCounter; + uint64_t m_timerCounter; + uint64_t m_timerInterval; uint64_t m_peerId; diff --git a/src/tcp_server.h b/src/tcp_server.h index fa43177..5be8826 100644 --- a/src/tcp_server.h +++ b/src/tcp_server.h @@ -160,7 +160,7 @@ protected: uint32_t m_numIncomingConnections; uv_mutex_t m_bansLock; - unordered_map m_bans; + unordered_map m_bans; bool is_banned(const raw_ip& ip); diff --git a/src/tcp_server.inl b/src/tcp_server.inl index 87aa850..5a68ee8 100644 --- a/src/tcp_server.inl +++ b/src/tcp_server.inl @@ -322,11 +322,13 @@ void TCPServer::on_connect_failed(bool, const raw template bool TCPServer::is_banned(const raw_ip& ip) { + const auto cur_time = std::chrono::steady_clock::now(); + MutexLock lock(m_bansLock); auto it = m_bans.find(ip); if (it != m_bans.end()) { - const bool banned = (time(nullptr) < it->second); + const bool banned = (cur_time < it->second); if (!banned) { m_bans.erase(it); } @@ -435,14 +437,14 @@ void TCPServer::shutdown_tcp() using namespace std::chrono; - const system_clock::time_point start_time = system_clock::now(); + const auto start_time = steady_clock::now(); int64_t counter = 0; uv_async_t asy; constexpr uint32_t timeout_seconds = 30; while (!m_loopStopped) { - const int64_t elapsed_time = duration_cast(system_clock::now() - start_time).count(); + const int64_t elapsed_time = duration_cast(steady_clock::now() - start_time).count(); if (elapsed_time >= (counter + 1) * 1000) { ++counter; @@ -493,8 +495,10 @@ void TCPServer::print_status() template void TCPServer::ban(const raw_ip& ip, uint64_t seconds) { + const auto ban_time = std::chrono::steady_clock::now() + std::chrono::seconds(seconds); + MutexLock lock(m_bansLock); - m_bans[ip] = time(nullptr) + seconds; + m_bans[ip] = ban_time; } template