Stratum: disconnect miners when not connected to P2Pool network

This commit is contained in:
SChernykh 2024-10-18 20:13:54 +02:00
parent fb78eb615b
commit 02006e1b50
3 changed files with 35 additions and 1 deletions

View file

@ -27,6 +27,7 @@
#include "json_parsers.h" #include "json_parsers.h"
#include "block_template.h" #include "block_template.h"
#include "p2pool_api.h" #include "p2pool_api.h"
#include "stratum_server.h"
#include <rapidjson/document.h> #include <rapidjson/document.h>
#include <fstream> #include <fstream>
#include <numeric> #include <numeric>
@ -56,6 +57,7 @@ P2PServer::P2PServer(p2pool* pool)
, m_timer{} , m_timer{}
, m_timerCounter(0) , m_timerCounter(0)
, m_timerInterval(2) , m_timerInterval(2)
, m_seenGoodPeers(false)
, m_peerListLastSaved(0) , m_peerListLastSaved(0)
, m_lookForMissingBlocks(true) , m_lookForMissingBlocks(true)
, m_fastestPeer(nullptr) , m_fastestPeer(nullptr)
@ -329,7 +331,10 @@ void P2PServer::update_peer_connections()
uint32_t N = m_maxOutgoingPeers; uint32_t N = m_maxOutgoingPeers;
// Special case: when we can't find p2pool peers, scan through monerod peers (try 25 peers at a time) // 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"); LOGINFO(3, "Scanning monerod peers, " << m_peerListMonero.size() << " left");
for (uint32_t i = 0; (i < 25) && !m_peerListMonero.empty(); ++i) { for (uint32_t i = 0; (i < 25) && !m_peerListMonero.empty(); ++i) {
peer_list.push_back(m_peerListMonero.back()); peer_list.push_back(m_peerListMonero.back());
@ -360,6 +365,13 @@ void P2PServer::update_peer_connections()
load_monerod_peer_list(); 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() void P2PServer::update_peer_list()

View file

@ -186,6 +186,8 @@ public:
void check_for_updates(bool forced = false) const; void check_for_updates(bool forced = false) const;
bool disconnected() const { return m_seenGoodPeers && (m_numConnections == 0); };
private: private:
[[nodiscard]] const char* get_log_category() const override; [[nodiscard]] const char* get_log_category() const override;
@ -244,6 +246,7 @@ private:
uint64_t m_lastSeen; uint64_t m_lastSeen;
}; };
std::atomic<bool> m_seenGoodPeers;
std::vector<Peer> m_peerList; std::vector<Peer> m_peerList;
std::vector<Peer> m_peerListMonero; std::vector<Peer> m_peerListMonero;
std::atomic<uint64_t> m_peerListLastSaved; std::atomic<uint64_t> m_peerListLastSaved;

View file

@ -22,6 +22,7 @@
#include "side_chain.h" #include "side_chain.h"
#include "params.h" #include "params.h"
#include "p2pool_api.h" #include "p2pool_api.h"
#include "p2p_server.h"
LOG_CATEGORY(StratumServer) 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) 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) { if (client->m_rpcId) {
LOGWARN(4, "client " << static_cast<char*>(client->m_addrString) << " tried to login, but it's already logged in"); LOGWARN(4, "client " << static_cast<char*>(client->m_addrString) << " tried to login, but it's already logged in");
return false; return false;