Skip to content

Commit

Permalink
Stratum: disconnect miners when not connected to P2Pool network
Browse files Browse the repository at this point in the history
  • Loading branch information
SChernykh committed Oct 18, 2024
1 parent fb78eb6 commit 83b9306
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/p2p_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "json_parsers.h"
#include "block_template.h"
#include "p2pool_api.h"
#include "stratum_server.h"
#include <rapidjson/document.h>
#include <fstream>
#include <numeric>
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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()
Expand Down
3 changes: 3 additions & 0 deletions src/p2p_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -244,6 +246,7 @@ class P2PServer : public TCPServer
uint64_t m_lastSeen;
};

std::atomic<bool> m_seenGoodPeers;
std::vector<Peer> m_peerList;
std::vector<Peer> m_peerListMonero;
std::atomic<uint64_t> m_peerListLastSaved;
Expand Down
19 changes: 19 additions & 0 deletions src/stratum_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "side_chain.h"
#include "params.h"
#include "p2pool_api.h"
#include "p2p_server.h"

LOG_CATEGORY(StratumServer)

Expand Down Expand Up @@ -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, [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<char*>(client->m_addrString) << " tried to login, but it's already logged in");
return false;
Expand Down

0 comments on commit 83b9306

Please sign in to comment.