Files
p2pool-salvium/src/merge_mining_client_tari.h

164 lines
4.3 KiB
C++

/*
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
* Copyright (c) 2021-2025 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"
#include "Tari/proto.h"
namespace p2pool {
class p2pool;
struct PoolBlock;
class MergeMiningClientTari : public IMergeMiningClient, public nocopy_nomove
{
public:
MergeMiningClientTari(p2pool* pool, std::string host, const std::string& wallet);
~MergeMiningClientTari() override;
static constexpr char TARI_PREFIX[] = "tari://";
bool get_params(ChainParameters& out_params) const override;
void on_external_block(const PoolBlock& block) override;
void submit_solution(const std::vector<uint8_t>& coinbase_merkle_proof, const uint8_t (&hashing_blob)[128], size_t nonce_offset, const hash& seed_hash, const std::vector<uint8_t>& blob, const std::vector<hash>& merkle_proof, uint32_t merkle_proof_path) override;
void print_status() const override;
void api_status(log::Stream&) const override;
private:
mutable uv_rwlock_t m_chainParamsLock;
ChainParameters m_chainParams;
uint64_t m_chainParamsTimestamp;
enum {
NUM_PREVIOUS_HASHES = 8,
};
uint64_t m_previousAuxHashes[NUM_PREVIOUS_HASHES];
tari::rpc::Block m_previousTariBlocks[NUM_PREVIOUS_HASHES];
uint32_t m_previousAuxHashesIndex;
std::atomic<uint32_t> m_previousAuxHashesFoundIndex;
tari::rpc::Block m_tariBlock;
std::string m_auxWallet;
p2pool* m_pool;
struct TariJobParams
{
uint64_t height;
uint64_t diff;
uint64_t reward;
uint64_t fees;
FORCEINLINE bool operator!=(const TariJobParams& job) const {
static_assert(sizeof(TariJobParams) == sizeof(uint64_t) * 4, "Invalid TariJobParams size");
return memcmp(this, &job, sizeof(TariJobParams)) != 0;
}
};
TariJobParams m_tariJobParams;
private:
static constexpr uint64_t BUF_SIZE = 16384;
struct TariClient;
struct TariServer : public TCPServer
{
explicit TariServer(const std::string& socks5Proxy);
~TariServer() {}
[[nodiscard]] bool start();
[[nodiscard]] bool connect_upstream(TariClient* downstream);
void on_shutdown() override;
[[nodiscard]] const char* get_log_category() const override;
bool m_TariNodeIsV6;
std::string m_TariNodeHost;
int m_TariNodePort;
int m_internalPort;
} *m_server;
const std::string m_hostStr;
grpc::ChannelArguments m_channelArgs;
std::shared_ptr<grpc::Channel> m_channel;
std::unique_ptr<tari::rpc::BaseNode::Stub> m_TariNode;
struct TariClient : public TCPServer::Client
{
TariClient();
~TariClient() override {}
static Client* allocate() { return new TariClient(); }
virtual size_t size() const override { return sizeof(TariClient); }
void reset() override;
[[nodiscard]] bool on_connect() override;
[[nodiscard]] bool on_read(const char* data, uint32_t size) override;
void on_connect_failed(int /*err*/) override { break_pairing(); }
void on_disconnected() override { break_pairing(); }
alignas(8) char m_buf[BUF_SIZE];
std::vector<uint8_t> m_pendingData;
bool m_connectionPending;
bool is_paired() const { return m_pairedClient && (m_pairedClient->m_resetCounter == m_pairedClientSavedResetCounter); }
void break_pairing();
TariClient* m_pairedClient;
uint32_t m_pairedClientSavedResetCounter;
};
uv_thread_t m_worker;
uv_mutex_t m_workerLock;
uv_cond_t m_workerCond;
std::atomic<uint32_t> m_workerStop;
static void run_wrapper(void* arg);
void run();
uv_mutex_t m_pushBlockLock;
uv_cond_t m_pushBlockCond;
std::atomic<uint32_t> m_pushBlockStop;
tari::rpc::Block m_blockToPush;
struct PushBlockThreadData
{
uv_thread_t m_worker;
MergeMiningClientTari* m_client;
std::string m_node;
};
std::vector<PushBlockThreadData*> m_pushBlockThreads;
static void push_block_thread(void* arg);
void push_blocks_to(const std::string& node_address);
};
} // namespace p2pool