2021-08-22 10:20:59 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
2022-03-30 12:42:26 +00:00
|
|
|
* Copyright (c) 2021-2022 SChernykh <https://github.com/SChernykh>
|
2021-08-22 10:20:59 +00:00
|
|
|
*
|
|
|
|
* 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 "uv_util.h"
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
namespace p2pool {
|
|
|
|
|
|
|
|
struct Params;
|
2021-11-20 10:51:22 +00:00
|
|
|
class RandomX_Hasher_Base;
|
2021-08-22 10:20:59 +00:00
|
|
|
class BlockTemplate;
|
|
|
|
class Mempool;
|
|
|
|
class SideChain;
|
|
|
|
class StratumServer;
|
|
|
|
class P2PServer;
|
2022-01-22 22:09:29 +00:00
|
|
|
class Miner;
|
2021-08-22 10:20:59 +00:00
|
|
|
class ConsoleCommands;
|
2021-09-01 11:49:58 +00:00
|
|
|
class p2pool_api;
|
2021-10-28 18:05:40 +00:00
|
|
|
class ZMQReader;
|
2021-08-22 10:20:59 +00:00
|
|
|
|
|
|
|
class p2pool : public MinerCallbackHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
p2pool(int argc, char* argv[]);
|
|
|
|
~p2pool();
|
|
|
|
|
|
|
|
int run();
|
|
|
|
|
|
|
|
bool stopped() const { return m_stopped; }
|
2021-08-23 21:25:35 +00:00
|
|
|
void stop();
|
2021-08-22 10:20:59 +00:00
|
|
|
|
|
|
|
const Params& params() const { return *m_params; }
|
|
|
|
BlockTemplate& block_template() { return *m_blockTemplate; }
|
|
|
|
SideChain& side_chain() { return *m_sideChain; }
|
2022-04-08 21:14:08 +00:00
|
|
|
|
|
|
|
FORCEINLINE MinerData miner_data() const
|
|
|
|
{
|
|
|
|
ReadLock lock(m_minerDataLock);
|
|
|
|
return m_minerData;
|
|
|
|
}
|
2021-08-22 10:20:59 +00:00
|
|
|
|
2021-10-04 08:28:56 +00:00
|
|
|
p2pool_api* api() const { return m_api; }
|
2021-10-01 23:09:42 +00:00
|
|
|
|
2021-11-20 10:51:22 +00:00
|
|
|
RandomX_Hasher_Base* hasher() const { return m_hasher; }
|
|
|
|
bool calculate_hash(const void* data, size_t size, uint64_t height, const hash& seed, hash& result);
|
2021-08-22 10:20:59 +00:00
|
|
|
static uint64_t get_seed_height(uint64_t height);
|
|
|
|
bool get_seed(uint64_t height, hash& seed) const;
|
|
|
|
|
|
|
|
StratumServer* stratum_server() const { return m_stratumServer; }
|
|
|
|
P2PServer* p2p_server() const { return m_p2pServer; }
|
2022-04-09 16:08:37 +00:00
|
|
|
|
2022-03-15 15:56:37 +00:00
|
|
|
#ifdef WITH_RANDOMX
|
2022-04-09 16:08:37 +00:00
|
|
|
void print_miner_status();
|
2022-03-15 15:56:37 +00:00
|
|
|
#endif
|
2021-08-22 10:20:59 +00:00
|
|
|
|
|
|
|
virtual void handle_tx(TxMempoolData& tx) override;
|
|
|
|
virtual void handle_miner_data(MinerData& data) override;
|
|
|
|
virtual void handle_chain_main(ChainMain& data, const char* extra) override;
|
|
|
|
|
2021-08-25 10:45:14 +00:00
|
|
|
void submit_block_async(uint32_t template_id, uint32_t nonce, uint32_t extra_nonce);
|
2021-08-25 20:07:42 +00:00
|
|
|
void submit_block_async(const std::vector<uint8_t>& blob);
|
2021-08-22 10:20:59 +00:00
|
|
|
void submit_sidechain_block(uint32_t template_id, uint32_t nonce, uint32_t extra_nonce);
|
2021-08-25 10:17:14 +00:00
|
|
|
|
2021-08-22 10:20:59 +00:00
|
|
|
void update_block_template_async();
|
2021-08-25 10:17:14 +00:00
|
|
|
void update_block_template();
|
2021-08-22 10:20:59 +00:00
|
|
|
|
|
|
|
void download_block_headers(uint64_t current_height);
|
|
|
|
|
|
|
|
bool chainmain_get_by_hash(const hash& id, ChainMain& data) const;
|
|
|
|
|
2021-09-06 21:33:52 +00:00
|
|
|
void api_update_block_found(const ChainMain* data);
|
|
|
|
|
2021-09-13 08:14:53 +00:00
|
|
|
bool get_difficulty_at_height(uint64_t height, difficulty_type& diff);
|
|
|
|
|
2022-03-15 15:56:37 +00:00
|
|
|
#ifdef WITH_RANDOMX
|
2022-01-22 22:09:29 +00:00
|
|
|
void start_mining(uint32_t threads);
|
|
|
|
void stop_mining();
|
2022-03-15 15:56:37 +00:00
|
|
|
#endif
|
2022-01-22 22:09:29 +00:00
|
|
|
|
2022-03-23 10:30:38 +00:00
|
|
|
uint64_t zmq_last_active() const { return m_zmqLastActive; }
|
|
|
|
uint64_t start_time() const { return m_startTime; }
|
2021-10-09 09:01:26 +00:00
|
|
|
|
2021-08-22 10:20:59 +00:00
|
|
|
private:
|
|
|
|
p2pool(const p2pool&) = delete;
|
|
|
|
p2pool(p2pool&&) = delete;
|
|
|
|
|
2021-08-25 10:45:14 +00:00
|
|
|
static void on_submit_block(uv_async_t* async) { reinterpret_cast<p2pool*>(async->data)->submit_block(); }
|
2021-08-25 10:17:14 +00:00
|
|
|
static void on_update_block_template(uv_async_t* async) { reinterpret_cast<p2pool*>(async->data)->update_block_template(); }
|
2021-08-27 08:13:33 +00:00
|
|
|
static void on_stop(uv_async_t*);
|
2021-08-25 10:17:14 +00:00
|
|
|
|
2021-08-25 10:45:14 +00:00
|
|
|
void submit_block() const;
|
|
|
|
|
2021-08-22 10:20:59 +00:00
|
|
|
bool m_stopped;
|
|
|
|
|
|
|
|
Params* m_params;
|
|
|
|
|
2021-10-04 08:28:56 +00:00
|
|
|
p2pool_api* m_api;
|
2021-08-22 10:20:59 +00:00
|
|
|
SideChain* m_sideChain;
|
2021-11-20 10:51:22 +00:00
|
|
|
RandomX_Hasher_Base* m_hasher;
|
2021-08-22 10:20:59 +00:00
|
|
|
BlockTemplate* m_blockTemplate;
|
2021-08-25 10:17:14 +00:00
|
|
|
bool m_updateSeed;
|
2021-08-22 10:20:59 +00:00
|
|
|
Mempool* m_mempool;
|
|
|
|
|
|
|
|
mutable uv_rwlock_t m_mainchainLock;
|
|
|
|
std::map<uint64_t, ChainMain> m_mainchainByHeight;
|
2021-10-22 16:18:38 +00:00
|
|
|
unordered_map<hash, ChainMain> m_mainchainByHash;
|
2021-08-22 10:20:59 +00:00
|
|
|
|
2022-04-08 21:14:08 +00:00
|
|
|
mutable uv_rwlock_t m_minerDataLock;
|
|
|
|
MinerData m_minerData;
|
|
|
|
|
2021-08-22 10:20:59 +00:00
|
|
|
enum { TIMESTAMP_WINDOW = 60 };
|
|
|
|
bool get_timestamps(uint64_t (×tamps)[TIMESTAMP_WINDOW]) const;
|
|
|
|
void update_median_timestamp();
|
|
|
|
|
|
|
|
void stratum_on_block();
|
|
|
|
|
2021-08-27 09:25:25 +00:00
|
|
|
void get_info();
|
2021-09-10 14:18:16 +00:00
|
|
|
void load_found_blocks();
|
2021-08-27 09:25:25 +00:00
|
|
|
void parse_get_info_rpc(const char* data, size_t size);
|
|
|
|
|
2021-09-13 16:27:47 +00:00
|
|
|
void get_version();
|
|
|
|
void parse_get_version_rpc(const char* data, size_t size);
|
|
|
|
|
2021-08-22 10:20:59 +00:00
|
|
|
void get_miner_data();
|
|
|
|
void parse_get_miner_data_rpc(const char* data, size_t size);
|
|
|
|
|
|
|
|
bool parse_block_header(const char* data, size_t size, ChainMain& result);
|
|
|
|
uint32_t parse_block_headers_range(const char* data, size_t size);
|
|
|
|
|
2021-10-04 08:28:56 +00:00
|
|
|
void api_update_network_stats();
|
|
|
|
void api_update_pool_stats();
|
|
|
|
void api_update_stats_mod();
|
|
|
|
|
2021-10-29 09:14:28 +00:00
|
|
|
void cleanup_mainchain_data(uint64_t height);
|
|
|
|
|
2021-09-03 16:04:54 +00:00
|
|
|
struct FoundBlock
|
|
|
|
{
|
2021-09-05 20:28:57 +00:00
|
|
|
FORCEINLINE FoundBlock(time_t _t, uint64_t _h, const hash& _id, const difficulty_type& _block_diff, const difficulty_type& _total_hashes)
|
2021-09-03 16:04:54 +00:00
|
|
|
: timestamp(_t)
|
|
|
|
, height(_h)
|
2021-09-05 20:28:57 +00:00
|
|
|
, id(_id)
|
2021-09-03 16:04:54 +00:00
|
|
|
, block_diff(_block_diff)
|
|
|
|
, total_hashes(_total_hashes)
|
|
|
|
{}
|
|
|
|
|
|
|
|
time_t timestamp;
|
|
|
|
uint64_t height;
|
2021-09-05 20:28:57 +00:00
|
|
|
hash id;
|
2021-09-03 16:04:54 +00:00
|
|
|
difficulty_type block_diff;
|
|
|
|
difficulty_type total_hashes;
|
|
|
|
};
|
|
|
|
|
2021-09-01 15:21:27 +00:00
|
|
|
uv_mutex_t m_foundBlocksLock;
|
2021-09-03 16:04:54 +00:00
|
|
|
std::vector<FoundBlock> m_foundBlocks;
|
2021-09-01 11:49:58 +00:00
|
|
|
|
2021-08-22 10:20:59 +00:00
|
|
|
std::atomic<uint32_t> m_serversStarted{ 0 };
|
|
|
|
StratumServer* m_stratumServer = nullptr;
|
|
|
|
P2PServer* m_p2pServer = nullptr;
|
2022-04-09 16:08:37 +00:00
|
|
|
|
2022-03-15 15:56:37 +00:00
|
|
|
#ifdef WITH_RANDOMX
|
2022-04-09 16:08:37 +00:00
|
|
|
uv_mutex_t m_minerLock;
|
2022-01-22 22:09:29 +00:00
|
|
|
Miner* m_miner = nullptr;
|
2022-03-15 15:56:37 +00:00
|
|
|
#endif
|
2021-08-22 10:20:59 +00:00
|
|
|
|
|
|
|
ConsoleCommands* m_consoleCommands;
|
2021-08-25 10:17:14 +00:00
|
|
|
|
2021-08-25 20:07:42 +00:00
|
|
|
struct SubmitBlockData
|
|
|
|
{
|
2021-10-27 13:16:25 +00:00
|
|
|
uint32_t template_id = 0;
|
|
|
|
uint32_t nonce = 0;
|
|
|
|
uint32_t extra_nonce = 0;
|
2021-08-25 20:07:42 +00:00
|
|
|
std::vector<uint8_t> blob;
|
|
|
|
};
|
|
|
|
|
|
|
|
mutable uv_mutex_t m_submitBlockDataLock;
|
|
|
|
SubmitBlockData m_submitBlockData;
|
2021-08-25 10:45:14 +00:00
|
|
|
|
|
|
|
uv_async_t m_submitBlockAsync;
|
2021-08-25 10:17:14 +00:00
|
|
|
uv_async_t m_blockTemplateAsync;
|
|
|
|
uv_async_t m_stopAsync;
|
2021-10-09 09:01:26 +00:00
|
|
|
|
2022-04-08 21:14:08 +00:00
|
|
|
std::atomic<uint64_t> m_zmqLastActive;
|
2022-03-23 10:30:38 +00:00
|
|
|
uint64_t m_startTime;
|
2021-10-28 18:05:40 +00:00
|
|
|
|
|
|
|
ZMQReader* m_ZMQReader = nullptr;
|
2021-08-22 10:20:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace p2pool
|