2021-08-22 10:20:59 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
2023-01-04 12:07:55 +00:00
|
|
|
* Copyright (c) 2021-2023 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"
|
2022-11-02 11:49:12 +00:00
|
|
|
#include "pool_block.h"
|
2021-08-22 10:20:59 +00:00
|
|
|
#include <map>
|
2022-07-14 07:04:14 +00:00
|
|
|
#include <thread>
|
2021-08-22 10:20:59 +00:00
|
|
|
|
|
|
|
namespace p2pool {
|
|
|
|
|
|
|
|
class p2pool;
|
2021-11-01 17:53:34 +00:00
|
|
|
class P2PServer;
|
2021-08-22 10:20:59 +00:00
|
|
|
|
|
|
|
struct MinerShare
|
|
|
|
{
|
2022-11-15 21:20:54 +00:00
|
|
|
FORCEINLINE MinerShare() : m_weight(), m_wallet(nullptr) {}
|
|
|
|
FORCEINLINE MinerShare(const difficulty_type& w, const Wallet* x) : m_weight(w), m_wallet(x) {}
|
2021-08-22 10:20:59 +00:00
|
|
|
|
2022-11-15 21:20:54 +00:00
|
|
|
difficulty_type m_weight;
|
2022-07-10 08:24:03 +00:00
|
|
|
const Wallet* m_wallet;
|
2021-08-22 10:20:59 +00:00
|
|
|
};
|
|
|
|
|
2022-07-14 07:04:14 +00:00
|
|
|
class SideChain : public nocopy_nomove
|
2021-08-22 10:20:59 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-09-06 13:49:39 +00:00
|
|
|
SideChain(p2pool* pool, NetworkType type, const char* pool_name = nullptr);
|
2021-08-22 10:20:59 +00:00
|
|
|
~SideChain();
|
|
|
|
|
2023-01-09 00:15:06 +00:00
|
|
|
void fill_sidechain_data(PoolBlock& block, std::vector<MinerShare>& shares) const;
|
2021-08-22 10:20:59 +00:00
|
|
|
|
|
|
|
bool block_seen(const PoolBlock& block);
|
2021-10-16 07:42:58 +00:00
|
|
|
void unsee_block(const PoolBlock& block);
|
2021-08-22 10:20:59 +00:00
|
|
|
bool add_external_block(PoolBlock& block, std::vector<hash>& missing_blocks);
|
2023-01-07 14:50:02 +00:00
|
|
|
bool add_block(const PoolBlock& block);
|
2022-05-12 13:19:58 +00:00
|
|
|
void get_missing_blocks(std::vector<hash>& missing_blocks) const;
|
2021-08-22 10:20:59 +00:00
|
|
|
|
2022-05-12 13:19:58 +00:00
|
|
|
PoolBlock* find_block(const hash& id) const;
|
2021-09-06 21:33:52 +00:00
|
|
|
void watch_mainchain_block(const ChainMain& data, const hash& possible_id);
|
|
|
|
|
2022-05-12 13:19:58 +00:00
|
|
|
bool get_block_blob(const hash& id, std::vector<uint8_t>& blob) const;
|
2022-08-15 09:16:00 +00:00
|
|
|
bool get_outputs_blob(PoolBlock* block, uint64_t total_reward, std::vector<uint8_t>& blob, uv_loop_t* loop) const;
|
2021-08-22 10:20:59 +00:00
|
|
|
|
2022-08-23 12:13:09 +00:00
|
|
|
void print_status(bool obtain_sidechain_lock = true) const;
|
2022-05-28 20:27:46 +00:00
|
|
|
double get_reward_share(const Wallet& w) const;
|
2021-08-22 10:20:59 +00:00
|
|
|
|
|
|
|
// Consensus ID can be used to spawn independent P2Pools with their own sidechains
|
|
|
|
// It's never sent over the network to avoid revealing it to the possible man in the middle
|
|
|
|
// Consensus ID can therefore be used as a password to create private P2Pools
|
|
|
|
const std::vector<uint8_t>& consensus_id() const { return m_consensusId; }
|
|
|
|
uint64_t chain_window_size() const { return m_chainWindowSize; }
|
2021-08-27 09:25:25 +00:00
|
|
|
NetworkType network_type() const { return m_networkType; }
|
2022-12-19 08:58:43 +00:00
|
|
|
uint64_t network_major_version(uint64_t height) const;
|
2022-05-09 14:07:49 +00:00
|
|
|
FORCEINLINE difficulty_type difficulty() const { ReadLock lock(m_curDifficultyLock); return m_curDifficulty; }
|
2021-09-01 14:26:56 +00:00
|
|
|
difficulty_type total_hashes() const;
|
|
|
|
uint64_t block_time() const { return m_targetBlockTime; }
|
2021-09-01 18:48:03 +00:00
|
|
|
uint64_t miner_count();
|
2022-03-23 10:30:38 +00:00
|
|
|
uint64_t last_updated() const;
|
2021-09-07 19:30:52 +00:00
|
|
|
bool is_default() const;
|
2021-12-30 10:10:18 +00:00
|
|
|
bool is_mini() const;
|
2021-08-22 10:20:59 +00:00
|
|
|
|
2021-11-01 17:53:34 +00:00
|
|
|
const PoolBlock* chainTip() const { return m_chainTip; }
|
2022-09-13 08:51:15 +00:00
|
|
|
bool precalcFinished() const { return m_precalcFinished.load(); }
|
2021-11-01 17:53:34 +00:00
|
|
|
|
2023-01-07 23:34:38 +00:00
|
|
|
#ifdef P2POOL_UNIT_TESTS
|
|
|
|
difficulty_type m_testMainChainDiff;
|
|
|
|
#endif
|
|
|
|
|
2021-08-22 10:20:59 +00:00
|
|
|
static bool split_reward(uint64_t reward, const std::vector<MinerShare>& shares, std::vector<uint64_t>& rewards);
|
|
|
|
|
|
|
|
private:
|
|
|
|
p2pool* m_pool;
|
2021-11-01 18:35:11 +00:00
|
|
|
P2PServer* p2pServer() const;
|
2021-08-27 09:25:25 +00:00
|
|
|
NetworkType m_networkType;
|
2021-08-22 10:20:59 +00:00
|
|
|
|
|
|
|
private:
|
2023-01-07 23:34:38 +00:00
|
|
|
bool get_shares(const PoolBlock* tip, std::vector<MinerShare>& shares, bool quiet = false) const;
|
2022-07-10 08:24:03 +00:00
|
|
|
bool get_difficulty(const PoolBlock* tip, std::vector<DifficultyData>& difficultyData, difficulty_type& curDifficulty) const;
|
2021-08-22 10:20:59 +00:00
|
|
|
void verify_loop(PoolBlock* block);
|
|
|
|
void verify(PoolBlock* block);
|
2022-07-10 08:24:03 +00:00
|
|
|
void update_chain_tip(const PoolBlock* block);
|
2022-05-12 13:19:58 +00:00
|
|
|
PoolBlock* get_parent(const PoolBlock* block) const;
|
2021-08-22 10:20:59 +00:00
|
|
|
|
|
|
|
// Checks if "candidate" has longer (higher difficulty) chain than "block"
|
2022-02-17 10:19:11 +00:00
|
|
|
bool is_longer_chain(const PoolBlock* block, const PoolBlock* candidate, bool& is_alternative);
|
2021-08-22 10:20:59 +00:00
|
|
|
void update_depths(PoolBlock* block);
|
|
|
|
void prune_old_blocks();
|
|
|
|
|
|
|
|
bool load_config(const std::string& filename);
|
2022-12-22 12:33:44 +00:00
|
|
|
bool check_config() const;
|
2021-08-22 10:20:59 +00:00
|
|
|
|
2022-05-12 13:19:58 +00:00
|
|
|
mutable uv_rwlock_t m_sidechainLock;
|
2022-04-08 21:14:08 +00:00
|
|
|
std::atomic<PoolBlock*> m_chainTip;
|
2021-08-22 10:20:59 +00:00
|
|
|
std::map<uint64_t, std::vector<PoolBlock*>> m_blocksByHeight;
|
2021-10-22 16:18:38 +00:00
|
|
|
unordered_map<hash, PoolBlock*> m_blocksById;
|
2022-05-12 13:19:58 +00:00
|
|
|
|
|
|
|
uv_mutex_t m_seenWalletsLock;
|
2022-03-23 10:30:38 +00:00
|
|
|
unordered_map<hash, uint64_t> m_seenWallets;
|
2022-05-12 20:18:08 +00:00
|
|
|
uint64_t m_seenWalletsLastPruneTime;
|
2021-08-22 10:20:59 +00:00
|
|
|
|
2021-10-31 09:26:13 +00:00
|
|
|
uv_mutex_t m_seenBlocksLock;
|
2022-11-02 11:49:12 +00:00
|
|
|
unordered_set<PoolBlock::full_id> m_seenBlocks;
|
2021-10-31 09:26:13 +00:00
|
|
|
|
2021-08-22 10:20:59 +00:00
|
|
|
std::vector<DifficultyData> m_difficultyData;
|
|
|
|
|
|
|
|
std::string m_poolName;
|
|
|
|
std::string m_poolPassword;
|
|
|
|
uint64_t m_targetBlockTime;
|
|
|
|
difficulty_type m_minDifficulty;
|
|
|
|
uint64_t m_chainWindowSize;
|
|
|
|
uint64_t m_unclePenalty;
|
|
|
|
|
|
|
|
std::vector<uint8_t> m_consensusId;
|
2022-03-23 13:17:40 +00:00
|
|
|
std::string m_consensusIdDisplayStr;
|
2021-08-22 10:20:59 +00:00
|
|
|
|
2022-05-09 14:07:49 +00:00
|
|
|
mutable uv_rwlock_t m_curDifficultyLock;
|
2021-08-22 10:20:59 +00:00
|
|
|
difficulty_type m_curDifficulty;
|
2021-09-06 21:33:52 +00:00
|
|
|
|
|
|
|
ChainMain m_watchBlock;
|
|
|
|
hash m_watchBlockSidechainId;
|
2022-07-14 07:04:14 +00:00
|
|
|
|
|
|
|
struct PrecalcJob
|
|
|
|
{
|
|
|
|
const PoolBlock* b;
|
2023-01-07 23:34:38 +00:00
|
|
|
std::vector<MinerShare> shares;
|
2022-07-14 07:04:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
uv_cond_t m_precalcJobsCond;
|
|
|
|
uv_mutex_t m_precalcJobsMutex;
|
|
|
|
|
|
|
|
std::vector<PrecalcJob*> m_precalcJobs;
|
|
|
|
std::vector<std::thread> m_precalcWorkers;
|
|
|
|
unordered_set<size_t>* m_uniquePrecalcInputs;
|
|
|
|
|
|
|
|
std::atomic<bool> m_precalcFinished;
|
|
|
|
|
2023-01-09 18:47:35 +00:00
|
|
|
hash m_consensusHash;
|
|
|
|
|
2022-07-14 07:04:14 +00:00
|
|
|
void launch_precalc(const PoolBlock* block);
|
|
|
|
void precalc_worker();
|
|
|
|
void finish_precalc();
|
2021-08-22 10:20:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace p2pool
|