From 02006e1b50c32b418029aa30e8513e8ad5e6e50b Mon Sep 17 00:00:00 2001 From: SChernykh <15806605+SChernykh@users.noreply.github.com> Date: Fri, 18 Oct 2024 20:13:54 +0200 Subject: [PATCH] Stratum: disconnect miners when not connected to P2Pool network --- src/p2p_server.cpp | 14 +++++++++++++- src/p2p_server.h | 3 +++ src/stratum_server.cpp | 19 +++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/p2p_server.cpp b/src/p2p_server.cpp index 1b7513cb..ab3e0c75 100644 --- a/src/p2p_server.cpp +++ b/src/p2p_server.cpp @@ -27,6 +27,7 @@ #include "json_parsers.h" #include "block_template.h" #include "p2pool_api.h" +#include "stratum_server.h" #include #include #include @@ -56,6 +57,7 @@ P2PServer::P2PServer(p2pool* pool) , m_timer{} , m_timerCounter(0) , m_timerInterval(2) + , m_seenGoodPeers(false) , m_peerListLastSaved(0) , m_lookForMissingBlocks(true) , m_fastestPeer(nullptr) @@ -329,7 +331,10 @@ void P2PServer::update_peer_connections() uint32_t N = m_maxOutgoingPeers; // Special case: when we can't find p2pool peers, scan through monerod peers (try 25 peers at a time) - if (!has_good_peers && !m_peerListMonero.empty()) { + if (has_good_peers) { + m_seenGoodPeers = true; + } + else if (!m_peerListMonero.empty()) { LOGINFO(3, "Scanning monerod peers, " << m_peerListMonero.size() << " left"); for (uint32_t i = 0; (i < 25) && !m_peerListMonero.empty(); ++i) { peer_list.push_back(m_peerListMonero.back()); @@ -360,6 +365,13 @@ void P2PServer::update_peer_connections() load_monerod_peer_list(); } } + + if (disconnected()) { + StratumServer* stratum_server = m_pool->stratum_server(); + if (stratum_server) { + stratum_server->drop_connections_async(); + } + } } void P2PServer::update_peer_list() diff --git a/src/p2p_server.h b/src/p2p_server.h index b96867db..7433414f 100644 --- a/src/p2p_server.h +++ b/src/p2p_server.h @@ -186,6 +186,8 @@ class P2PServer : public TCPServer void check_for_updates(bool forced = false) const; + bool disconnected() const { return m_seenGoodPeers && (m_numConnections == 0); }; + private: [[nodiscard]] const char* get_log_category() const override; @@ -244,6 +246,7 @@ class P2PServer : public TCPServer uint64_t m_lastSeen; }; + std::atomic m_seenGoodPeers; std::vector m_peerList; std::vector m_peerListMonero; std::atomic m_peerListLastSaved; diff --git a/src/stratum_server.cpp b/src/stratum_server.cpp index 7c2e5d0c..3bd6fa2c 100644 --- a/src/stratum_server.cpp +++ b/src/stratum_server.cpp @@ -22,6 +22,7 @@ #include "side_chain.h" #include "params.h" #include "p2pool_api.h" +#include "p2p_server.h" LOG_CATEGORY(StratumServer) @@ -250,6 +251,24 @@ static bool get_custom_diff(const char* s, difficulty_type& diff) bool StratumServer::on_login(StratumClient* client, uint32_t id, const char* login) { + const P2PServer* p2p_server = m_pool->p2p_server(); + + // If there are no connections to other P2Pool peers, don't let Stratum clients connect + if (p2p_server && p2p_server->disconnected()) { + const bool result = send(client, [id](uint8_t* buf, size_t buf_size) { + log::Stream s(buf, buf_size); + s << "{\"id\":" << id << ",\"jsonrpc\":\"2.0\",\"error\":{\"message\":\"Disconnected from P2Pool network\"}}\n"; + return s.m_pos; + }); + + if (!result) { + return false; + } + + client->close(); + return true; + } + if (client->m_rpcId) { LOGWARN(4, "client " << static_cast(client->m_addrString) << " tried to login, but it's already logged in"); return false;