-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
285 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
/* | ||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool> | ||
* Copyright (c) 2021-2024 SChernykh <https://github.com/SChernykh> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, version 3. | ||
* | ||
* This program is distributed in the hope that it will be useful, but | ||
* WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "common.h" | ||
#include "merge_mining_client.h" | ||
#include "merge_mining_client_tari.h" | ||
#include "p2pool.h" | ||
#include "params.h" | ||
#include "Tari/proto.h" | ||
|
||
LOG_CATEGORY(MergeMiningClientTari) | ||
|
||
namespace p2pool { | ||
|
||
MergeMiningClientTari::MergeMiningClientTari(p2pool* pool, const std::string& host, const std::string& wallet) | ||
: m_server(new gRPC_Server(pool->params().m_socks5Proxy)) | ||
, m_host(host) | ||
, m_port(0) | ||
, m_auxWallet(wallet) | ||
, m_pool(pool) | ||
{ | ||
if (host.find(TARI_PREFIX) != 0) { | ||
LOGERR(1, "Invalid host " << host << " - \"" << TARI_PREFIX << "\" prefix not found"); | ||
throw std::exception(); | ||
} | ||
|
||
const size_t k = host.find_last_of(':'); | ||
if (k != std::string::npos) { | ||
m_host = host.substr(sizeof(TARI_PREFIX) - 1, k - (sizeof(TARI_PREFIX) - 1)); | ||
m_port = std::stoul(host.substr(k + 1), nullptr, 10); | ||
} | ||
|
||
if (m_host.empty() || (m_port == 0) || (m_port >= 65536)) { | ||
LOGERR(1, "Invalid host " << host); | ||
throw std::exception(); | ||
} | ||
|
||
uv_rwlock_init_checked(&m_lock); | ||
|
||
if (!m_server->start(m_pool->params().m_dns, m_host, m_port)) { | ||
throw std::exception(); | ||
} | ||
} | ||
|
||
MergeMiningClientTari::~MergeMiningClientTari() | ||
{ | ||
m_server->shutdown_tcp(); | ||
delete m_server; | ||
|
||
LOGINFO(1, "stopped"); | ||
} | ||
|
||
bool MergeMiningClientTari::get_params(ChainParameters& out_params) const | ||
{ | ||
ReadLock lock(m_lock); | ||
|
||
if (m_chainParams.aux_id.empty() || m_chainParams.aux_diff.empty()) { | ||
return false; | ||
} | ||
|
||
out_params = m_chainParams; | ||
return true; | ||
} | ||
|
||
void MergeMiningClientTari::submit_solution(const std::vector<uint8_t>& blob, const std::vector<hash>& merkle_proof) | ||
{ | ||
(void)blob; | ||
(void)merkle_proof; | ||
} | ||
|
||
void MergeMiningClientTari::gRPC_Client::reset() | ||
{ | ||
m_data.clear(); | ||
} | ||
|
||
bool MergeMiningClientTari::gRPC_Client::on_connect() | ||
{ | ||
const MergeMiningClientTari::gRPC_Server* server = static_cast<MergeMiningClientTari::gRPC_Server*>(m_owner); | ||
if (server) { | ||
LOGINFO(4, "Connected to " << server->m_host << ':' << server->m_port); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
bool MergeMiningClientTari::gRPC_Client::on_read(char* data, uint32_t size) | ||
{ | ||
const MergeMiningClientTari::gRPC_Server* server = static_cast<MergeMiningClientTari::gRPC_Server*>(m_owner); | ||
if (server) { | ||
LOGINFO(4, "Read " << size << " bytes from " << server->m_host << ':' << server->m_port); | ||
LOGINFO(4, log::hex_buf(data, size)); | ||
} | ||
|
||
m_data.insert(m_data.end(), data, data + size); | ||
|
||
return true; | ||
} | ||
|
||
void MergeMiningClientTari::gRPC_Client::on_read_failed(int err) | ||
{ | ||
const MergeMiningClientTari::gRPC_Server* server = static_cast<MergeMiningClientTari::gRPC_Server*>(m_owner); | ||
if (server) { | ||
LOGERR(1, "Read from " << server->m_host << ':' << server->m_port << "failed, error " << err); | ||
} | ||
} | ||
|
||
void MergeMiningClientTari::gRPC_Client::on_disconnected() | ||
{ | ||
const MergeMiningClientTari::gRPC_Server* server = static_cast<MergeMiningClientTari::gRPC_Server*>(m_owner); | ||
if (server) { | ||
LOGINFO(4, "Disconnected from " << server->m_host << ':' << server->m_port); | ||
} | ||
} | ||
|
||
MergeMiningClientTari::gRPC_Server::gRPC_Server(const std::string& socks5Proxy) | ||
: TCPServer(1, MergeMiningClientTari::gRPC_Client::allocate, socks5Proxy) | ||
, m_port(0) | ||
{ | ||
} | ||
|
||
MergeMiningClientTari::gRPC_Server::~gRPC_Server() | ||
{ | ||
} | ||
|
||
bool MergeMiningClientTari::gRPC_Server::start(bool use_dns, const std::string& host, int port) | ||
{ | ||
const int err = uv_thread_create(&m_loopThread, loop, this); | ||
if (err) { | ||
LOGERR(1, "failed to start event loop thread, error " << uv_err_name(err)); | ||
return false; | ||
} | ||
|
||
m_loopThreadCreated = true; | ||
|
||
m_host = host; | ||
m_port = port; | ||
|
||
std::string ip = host; | ||
bool is_v6 = host.find_first_of(':') != std::string::npos; | ||
|
||
if (!use_dns || resolve_host(ip, is_v6)) { | ||
if (!connect_to_peer(is_v6, ip.c_str(), port)) { | ||
LOGERR(1, "Failed to connect to " << host << ':' << port); | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
void MergeMiningClientTari::gRPC_Server::on_shutdown() | ||
{ | ||
} | ||
|
||
const char* MergeMiningClientTari::gRPC_Server::get_log_category() const | ||
{ | ||
return log_category_prefix; | ||
} | ||
|
||
} // namespace p2pool |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool> | ||
* Copyright (c) 2021-2024 SChernykh <https://github.com/SChernykh> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, version 3. | ||
* | ||
* This program is distributed in the hope that it will be useful, but | ||
* WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "tcp_server.h" | ||
|
||
namespace p2pool { | ||
|
||
class p2pool; | ||
|
||
class MergeMiningClientTari : public IMergeMiningClient | ||
{ | ||
public: | ||
MergeMiningClientTari(p2pool* pool, const std::string& host, const std::string& wallet); | ||
~MergeMiningClientTari(); | ||
|
||
bool get_params(ChainParameters& out_params) const override; | ||
void submit_solution(const std::vector<uint8_t>& blob, const std::vector<hash>& merkle_proof) override; | ||
|
||
static constexpr char TARI_PREFIX[] = "tari://"; | ||
|
||
private: | ||
struct gRPC_Server : public TCPServer | ||
{ | ||
explicit gRPC_Server(const std::string& socks5Proxy); | ||
~gRPC_Server(); | ||
|
||
[[nodiscard]] bool start(bool use_dns, const std::string& host, int port); | ||
|
||
void on_shutdown() override; | ||
|
||
[[nodiscard]] const char* get_log_category() const override; | ||
|
||
std::string m_host; | ||
int m_port; | ||
} *m_server; | ||
|
||
struct gRPC_Client : public TCPServer::Client | ||
{ | ||
gRPC_Client() : Client(m_readBuf, sizeof(m_readBuf)) {} | ||
~gRPC_Client() {} | ||
|
||
static Client* allocate() { return new gRPC_Client(); } | ||
virtual size_t size() const override { return sizeof(gRPC_Client); } | ||
|
||
void reset() override; | ||
[[nodiscard]] bool on_connect() override; | ||
[[nodiscard]] bool on_read(char* data, uint32_t size) override; | ||
void on_read_failed(int err) override; | ||
void on_disconnected() override; | ||
|
||
char m_readBuf[1024]; | ||
std::vector<char> m_data; | ||
}; | ||
|
||
std::string m_host; | ||
uint32_t m_port; | ||
|
||
mutable uv_rwlock_t m_lock; | ||
ChainParameters m_chainParams; | ||
|
||
std::string m_auxWallet; | ||
|
||
p2pool* m_pool; | ||
}; | ||
|
||
} // namespace p2pool |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.