2021-08-22 10:20:59 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
|
|
|
* Copyright (c) 2021 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 "uv_util.h"
|
|
|
|
#include <map>
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
namespace p2pool {
|
|
|
|
|
|
|
|
struct Params;
|
|
|
|
class RandomX_Hasher;
|
|
|
|
class BlockTemplate;
|
|
|
|
class Mempool;
|
|
|
|
class SideChain;
|
|
|
|
class StratumServer;
|
|
|
|
class P2PServer;
|
|
|
|
class ConsoleCommands;
|
2021-09-01 11:49:58 +00:00
|
|
|
class p2pool_api;
|
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; }
|
|
|
|
const MinerData& miner_data() const { return m_minerData; }
|
|
|
|
|
2021-10-04 08:28:56 +00:00
|
|
|
p2pool_api* api() const { return m_api; }
|
2021-10-01 23:09:42 +00:00
|
|
|
|
2021-08-22 10:20:59 +00:00
|
|
|
RandomX_Hasher* hasher() const { return m_hasher; }
|
|
|
|
bool calculate_hash(const void* data, size_t size, const hash& seed, hash& result);
|
|
|
|
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; }
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
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;
|
|
|
|
RandomX_Hasher* m_hasher;
|
|
|
|
BlockTemplate* m_blockTemplate;
|
|
|
|
MinerData m_minerData;
|
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;
|
|
|
|
std::unordered_map<hash, ChainMain> m_mainchainByHash;
|
|
|
|
|
|
|
|
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-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;
|
|
|
|
|
|
|
|
ConsoleCommands* m_consoleCommands;
|
2021-08-25 10:17:14 +00:00
|
|
|
|
2021-08-25 20:07:42 +00:00
|
|
|
struct SubmitBlockData
|
|
|
|
{
|
2021-08-25 10:45:14 +00:00
|
|
|
uint32_t template_id;
|
|
|
|
uint32_t nonce;
|
|
|
|
uint32_t extra_nonce;
|
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-08-22 10:20:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace p2pool
|