More C++17

This commit is contained in:
SChernykh 2024-01-05 14:36:35 +01:00
parent 010bdda236
commit ec15417fd2
7 changed files with 97 additions and 86 deletions

View file

@ -241,7 +241,9 @@ void P2PServer::connect_to_peers(const std::string& peer_list)
[this](bool is_v6, const std::string& /*address*/, std::string ip, int port)
{
if (!m_pool->params().m_dns || resolve_host(ip, is_v6)) {
connect_to_peer(is_v6, ip.c_str(), port);
if (!connect_to_peer(is_v6, ip.c_str(), port)) {
LOGERR(5, "connect_to_peers: failed to connect to " << ip);
}
}
});
}
@ -1214,7 +1216,9 @@ void P2PServer::download_missing_blocks()
auto it = m_cachedBlocks->find(id);
if (it != m_cachedBlocks->end()) {
LOGINFO(5, "using cached block for id = " << id);
client->handle_incoming_block_async(it->second);
if (!client->handle_incoming_block_async(it->second)) {
LOGERR(5, "download_missing_blocks: handle_incoming_block_async failed");
}
continue;
}
}
@ -2645,7 +2649,9 @@ void P2PServer::P2PClient::post_handle_incoming_block(p2pool* pool, const PoolBl
auto it = server->m_cachedBlocks->find(id);
if (it != server->m_cachedBlocks->end()) {
LOGINFO(5, "using cached block for id = " << id);
handle_incoming_block_async(it->second);
if (!handle_incoming_block_async(it->second)) {
LOGERR(5, "post_handle_incoming_block: handle_incoming_block_async failed");
}
continue;
}
}

View file

@ -78,8 +78,8 @@ public:
virtual size_t size() const override { return sizeof(P2PClient); }
void reset() override;
bool on_connect() override;
bool on_read(char* data, uint32_t size) 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;
@ -99,28 +99,28 @@ public:
CHALLENGE_DIFFICULTY = 10000,
};
bool send_handshake_challenge();
[[nodiscard]] bool send_handshake_challenge();
void send_handshake_solution(const uint8_t (&challenge)[CHALLENGE_SIZE]);
bool check_handshake_solution(const hash& solution, const uint8_t (&solution_salt)[CHALLENGE_SIZE]);
[[nodiscard]] bool check_handshake_solution(const hash& solution, const uint8_t (&solution_salt)[CHALLENGE_SIZE]);
bool on_handshake_challenge(const uint8_t* buf);
bool on_handshake_solution(const uint8_t* buf);
[[nodiscard]] bool on_handshake_challenge(const uint8_t* buf);
[[nodiscard]] bool on_handshake_solution(const uint8_t* buf);
void on_after_handshake(uint8_t* &p);
bool on_listen_port(const uint8_t* buf);
bool on_block_request(const uint8_t* buf);
bool on_block_response(const uint8_t* buf, uint32_t size, uint64_t expected_id);
bool on_block_broadcast(const uint8_t* buf, uint32_t size, bool compact);
bool on_peer_list_request(const uint8_t* buf);
[[nodiscard]] bool on_listen_port(const uint8_t* buf);
[[nodiscard]] bool on_block_request(const uint8_t* buf);
[[nodiscard]] bool on_block_response(const uint8_t* buf, uint32_t size, uint64_t expected_id);
[[nodiscard]] bool on_block_broadcast(const uint8_t* buf, uint32_t size, bool compact);
[[nodiscard]] bool on_peer_list_request(const uint8_t* buf);
void on_peer_list_response(const uint8_t* buf);
void on_block_notify(const uint8_t* buf);
bool handle_incoming_block_async(const PoolBlock* block, uint64_t max_time_delta = 0);
[[nodiscard]] bool handle_incoming_block_async(const PoolBlock* block, uint64_t max_time_delta = 0);
void handle_incoming_block(p2pool* pool, PoolBlock& block, std::vector<hash>& missing_blocks, bool& result);
void post_handle_incoming_block(p2pool* pool, const PoolBlock& block, const uint32_t reset_counter, bool is_v6, const raw_ip& addr, std::vector<hash>& missing_blocks, const bool result);
bool is_good() const { return m_handshakeComplete && !m_handshakeInvalid && (m_listenPort >= 0); }
[[nodiscard]] bool is_good() const { return m_handshakeComplete && !m_handshakeInvalid && (m_listenPort >= 0); }
const char* software_name() const;
[[nodiscard]] const char* software_name() const;
alignas(8) char m_p2pReadBuf[P2P_BUF_SIZE];
@ -157,30 +157,30 @@ public:
};
void broadcast(const PoolBlock& block, const PoolBlock* parent);
uint64_t get_random64();
uint64_t get_peerId() const { return m_peerId; }
[[nodiscard]] uint64_t get_random64();
[[nodiscard]] uint64_t get_peerId() const { return m_peerId; }
void print_status() override;
void show_peers_async();
size_t peer_list_size() const { MutexLock lock(m_peerListLock); return m_peerList.size(); }
[[nodiscard]] size_t peer_list_size() const { MutexLock lock(m_peerListLock); return m_peerList.size(); }
int external_listen_port() const override;
[[nodiscard]] int external_listen_port() const override;
uint32_t max_outgoing_peers() const { return m_maxOutgoingPeers; }
uint32_t max_incoming_peers() const { return m_maxIncomingPeers; }
[[nodiscard]] uint32_t max_outgoing_peers() const { return m_maxOutgoingPeers; }
[[nodiscard]] uint32_t max_incoming_peers() const { return m_maxIncomingPeers; }
void set_max_outgoing_peers(uint32_t n) { m_maxOutgoingPeers = std::min(std::max(n, 10U), 450U); }
void set_max_incoming_peers(uint32_t n) { m_maxIncomingPeers = std::min(std::max(n, 10U), 450U); }
int deserialize_block(const uint8_t* buf, uint32_t size, bool compact, uint64_t received_timestamp);
const PoolBlock* get_block() const { return m_block; }
[[nodiscard]] int deserialize_block(const uint8_t* buf, uint32_t size, bool compact, uint64_t received_timestamp);
[[nodiscard]] const PoolBlock* get_block() const { return m_block; }
const PoolBlock* find_block(const hash& id) const;
[[nodiscard]] const PoolBlock* find_block(const hash& id) const;
void check_for_updates(bool forced = false) const;
private:
const char* get_log_category() const override;
[[nodiscard]] const char* get_log_category() const override;
p2pool* m_pool;
BlockCache* m_cache;

View file

@ -155,7 +155,7 @@ struct PoolBlock
std::vector<uint8_t> serialize_mainchain_data(size_t* header_size = nullptr, size_t* miner_tx_size = nullptr, int* outputs_offset = nullptr, int* outputs_blob_size = nullptr, const uint32_t* nonce = nullptr, const uint32_t* extra_nonce = nullptr) const;
std::vector<uint8_t> serialize_sidechain_data() const;
int deserialize(const uint8_t* data, size_t size, const SideChain& sidechain, uv_loop_t* loop, bool compact);
[[nodiscard]] int deserialize(const uint8_t* data, size_t size, const SideChain& sidechain, uv_loop_t* loop, bool compact);
void reset_offchain_data();
bool get_pow_hash(RandomX_Hasher_Base* hasher, uint64_t height, const hash& seed_hash, hash& pow_hash, bool force_light_mode = false);

View file

@ -238,7 +238,9 @@ void SideChain::fill_sidechain_data(PoolBlock& block, std::vector<MinerShare>& s
block.m_txkeySecSeed = m_consensusHash;
get_tx_keys(block.m_txkeyPub, block.m_txkeySec, block.m_txkeySecSeed, block.m_prevId);
get_shares(&block, shares);
if (!get_shares(&block, shares)) {
LOGERR(6, "fill_sidechain_data: get_shares failed");
}
return;
}
@ -332,7 +334,9 @@ void SideChain::fill_sidechain_data(PoolBlock& block, std::vector<MinerShare>& s
block.m_cumulativeDifficulty += it->second->m_difficulty;
}
get_shares(&block, shares);
if (!get_shares(&block, shares)) {
LOGERR(6, "fill_sidechain_data: get_shares failed");
}
}
P2PServer* SideChain::p2pServer() const
@ -661,8 +665,7 @@ bool SideChain::add_external_block(PoolBlock& block, std::vector<hash>& missing_
m_pool->api_update_block_found(&data, &block);
}
add_block(block);
return true;
return add_block(block);
}
bool SideChain::add_block(const PoolBlock& block)
@ -929,7 +932,9 @@ void SideChain::print_status(bool obtain_sidechain_lock) const
std::vector<MinerShare> shares;
uint64_t bh = 0;
if (tip) {
get_shares(tip, shares, &bh, true);
if (!get_shares(tip, shares, &bh, true)) {
LOGERR(6, "print_status: get_shares failed");
}
}
const uint64_t window_size = (tip && bh) ? (tip->m_sidechainHeight - bh + 1U) : m_chainWindowSize;

View file

@ -46,52 +46,52 @@ public:
void fill_sidechain_data(PoolBlock& block, std::vector<MinerShare>& shares) const;
bool incoming_block_seen(const PoolBlock& block);
[[nodiscard]] bool incoming_block_seen(const PoolBlock& block);
void forget_incoming_block(const PoolBlock& block);
void cleanup_incoming_blocks();
bool add_external_block(PoolBlock& block, std::vector<hash>& missing_blocks);
bool add_block(const PoolBlock& block);
[[nodiscard]] bool add_external_block(PoolBlock& block, std::vector<hash>& missing_blocks);
[[nodiscard]] bool add_block(const PoolBlock& block);
void get_missing_blocks(unordered_set<hash>& missing_blocks) const;
PoolBlock* find_block(const hash& id) const;
PoolBlock* find_block_by_merkle_root(const root_hash& merkle_root) const;
[[nodiscard]] PoolBlock* find_block(const hash& id) const;
[[nodiscard]] PoolBlock* find_block_by_merkle_root(const root_hash& merkle_root) const;
void watch_mainchain_block(const ChainMain& data, const hash& possible_merkle_root);
const PoolBlock* get_block_blob(const hash& id, std::vector<uint8_t>& blob) const;
bool get_outputs_blob(PoolBlock* block, uint64_t total_reward, std::vector<uint8_t>& blob, uv_loop_t* loop) const;
[[nodiscard]] const PoolBlock* get_block_blob(const hash& id, std::vector<uint8_t>& blob) const;
[[nodiscard]] bool get_outputs_blob(PoolBlock* block, uint64_t total_reward, std::vector<uint8_t>& blob, uv_loop_t* loop) const;
void print_status(bool obtain_sidechain_lock = true) const;
double get_reward_share(const Wallet& w) const;
[[nodiscard]] double get_reward_share(const Wallet& w) const;
// 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; }
const hash& consensus_hash() const { return m_consensusHash; }
uint64_t chain_window_size() const { return m_chainWindowSize; }
static NetworkType network_type() { return s_networkType; }
static uint64_t network_major_version(uint64_t height);
FORCEINLINE difficulty_type difficulty() const { ReadLock lock(m_curDifficultyLock); return m_curDifficulty; }
difficulty_type total_hashes() const;
uint64_t block_time() const { return m_targetBlockTime; }
uint64_t miner_count();
uint64_t last_updated() const;
bool is_default() const;
bool is_mini() const;
uint64_t bottom_height(const PoolBlock* tip) const;
[[nodiscard]] const std::vector<uint8_t>& consensus_id() const { return m_consensusId; }
[[nodiscard]] const hash& consensus_hash() const { return m_consensusHash; }
[[nodiscard]] uint64_t chain_window_size() const { return m_chainWindowSize; }
[[nodiscard]] static NetworkType network_type() { return s_networkType; }
[[nodiscard]] static uint64_t network_major_version(uint64_t height);
[[nodiscard]] FORCEINLINE difficulty_type difficulty() const { ReadLock lock(m_curDifficultyLock); return m_curDifficulty; }
[[nodiscard]] difficulty_type total_hashes() const;
[[nodiscard]] uint64_t block_time() const { return m_targetBlockTime; }
[[nodiscard]] uint64_t miner_count();
[[nodiscard]] uint64_t last_updated() const;
[[nodiscard]] bool is_default() const;
[[nodiscard]] bool is_mini() const;
[[nodiscard]] uint64_t bottom_height(const PoolBlock* tip) const;
const PoolBlock* chainTip() const { return m_chainTip; }
bool precalcFinished() const { return m_precalcFinished.load(); }
[[nodiscard]] const PoolBlock* chainTip() const { return m_chainTip; }
[[nodiscard]] bool precalcFinished() const { return m_precalcFinished.load(); }
bool p2pool_update_available() const;
[[nodiscard]] bool p2pool_update_available() const;
#ifdef P2POOL_UNIT_TESTS
difficulty_type m_testMainChainDiff;
const unordered_map<hash, PoolBlock*>& blocksById() const { return m_blocksById; }
#endif
static bool split_reward(uint64_t reward, const std::vector<MinerShare>& shares, std::vector<uint64_t>& rewards);
[[nodiscard]] static bool split_reward(uint64_t reward, const std::vector<MinerShare>& shares, std::vector<uint64_t>& rewards);
private:
p2pool* m_pool;
@ -99,20 +99,20 @@ private:
static NetworkType s_networkType;
private:
bool get_shares(const PoolBlock* tip, std::vector<MinerShare>& shares, uint64_t* bottom_height = nullptr, bool quiet = false) const;
bool get_difficulty(const PoolBlock* tip, std::vector<DifficultyData>& difficultyData, difficulty_type& curDifficulty) const;
[[nodiscard]] bool get_shares(const PoolBlock* tip, std::vector<MinerShare>& shares, uint64_t* bottom_height = nullptr, bool quiet = false) const;
[[nodiscard]] bool get_difficulty(const PoolBlock* tip, std::vector<DifficultyData>& difficultyData, difficulty_type& curDifficulty) const;
void verify_loop(PoolBlock* block);
void verify(PoolBlock* block);
void update_chain_tip(const PoolBlock* block);
PoolBlock* get_parent(const PoolBlock* block) const;
[[nodiscard]] PoolBlock* get_parent(const PoolBlock* block) const;
// Checks if "candidate" has longer (higher difficulty) chain than "block"
bool is_longer_chain(const PoolBlock* block, const PoolBlock* candidate, bool& is_alternative) const;
[[nodiscard]] bool is_longer_chain(const PoolBlock* block, const PoolBlock* candidate, bool& is_alternative) const;
void update_depths(PoolBlock* block);
void prune_old_blocks();
bool load_config(const std::string& filename);
bool check_config() const;
[[nodiscard]] bool load_config(const std::string& filename);
[[nodiscard]] bool check_config() const;
mutable uv_rwlock_t m_sidechainLock;
std::atomic<PoolBlock*> m_chainTip;

View file

@ -45,12 +45,12 @@ public:
virtual size_t size() const override { return sizeof(StratumClient); }
void reset() override;
bool on_connect() override;
bool on_read(char* data, uint32_t size) override;
[[nodiscard]] bool on_connect() override;
[[nodiscard]] bool on_read(char* data, uint32_t size) override;
bool process_request(char* data, uint32_t size);
bool process_login(rapidjson::Document& doc, uint32_t id);
bool process_submit(rapidjson::Document& doc, uint32_t id);
[[nodiscard]] bool process_request(char* data, uint32_t size);
[[nodiscard]] bool process_login(rapidjson::Document& doc, uint32_t id);
[[nodiscard]] bool process_submit(rapidjson::Document& doc, uint32_t id);
alignas(8) char m_stratumReadBuf[STRATUM_BUF_SIZE];
@ -88,9 +88,9 @@ public:
int32_t m_score;
};
bool on_login(StratumClient* client, uint32_t id, const char* login);
bool on_submit(StratumClient* client, uint32_t id, const char* job_id_str, const char* nonce_str, const char* result_str);
uint32_t get_random32();
[[nodiscard]] bool on_login(StratumClient* client, uint32_t id, const char* login);
[[nodiscard]] bool on_submit(StratumClient* client, uint32_t id, const char* job_id_str, const char* nonce_str, const char* result_str);
[[nodiscard]] uint32_t get_random32();
void print_status() override;
void show_workers_async();
@ -98,7 +98,7 @@ public:
void reset_share_counters();
private:
const char* get_log_category() const override;
[[nodiscard]] const char* get_log_category() const override;
void print_stratum_status() const;
void update_auto_diff(StratumClient* client, const uint64_t timestamp, const uint64_t hashes);

View file

@ -31,17 +31,17 @@ public:
TCPServer(int default_backlog, allocate_client_callback allocate_new_client);
virtual ~TCPServer();
bool connect_to_peer(bool is_v6, const char* ip, int port);
[[nodiscard]] bool connect_to_peer(bool is_v6, const char* ip, int port);
void drop_connections_async() { if (m_finished.load() == 0) { uv_async_send(&m_dropConnectionsAsync); } }
void shutdown_tcp();
virtual void print_status();
uv_loop_t* get_loop() { return &m_loop; }
[[nodiscard]] uv_loop_t* get_loop() { return &m_loop; }
virtual int external_listen_port() const { return m_listenPort; }
[[nodiscard]] virtual int external_listen_port() const { return m_listenPort; }
bool connect_to_peer(bool is_v6, const raw_ip& ip, int port);
[[nodiscard]] bool connect_to_peer(bool is_v6, const raw_ip& ip, int port);
virtual void on_connect_failed(bool /*is_v6*/, const raw_ip& /*ip*/, int /*port*/) {}
void ban(bool is_v6, raw_ip ip, uint64_t seconds);
@ -55,9 +55,9 @@ public:
virtual size_t size() const = 0;
virtual void reset();
virtual bool on_connect() = 0;
virtual bool on_read(char* data, uint32_t size) = 0;
bool on_proxy_handshake(char* data, uint32_t size);
[[nodiscard]] virtual bool on_connect() = 0;
[[nodiscard]] virtual bool on_read(char* data, uint32_t size) = 0;
[[nodiscard]] bool on_proxy_handshake(char* data, uint32_t size);
virtual void on_read_failed(int /*err*/) {}
virtual void on_disconnected() {}
@ -116,17 +116,17 @@ public:
std::multimap<size_t, WriteBuf*> m_writeBuffers;
WriteBuf* get_write_buffer(size_t size_hint);
[[nodiscard]] WriteBuf* get_write_buffer(size_t size_hint);
void return_write_buffer(WriteBuf* buf);
template<typename T>
FORCEINLINE static void parse_address_list(const std::string& address_list, T&& callback)
{
return parse_address_list_internal(address_list, Callback<void, bool, const std::string&, const std::string&, int>::Derived<T>(std::move(callback)));
parse_address_list_internal(address_list, Callback<void, bool, const std::string&, const std::string&, int>::Derived<T>(std::move(callback)));
}
template<typename T>
FORCEINLINE bool send(Client* client, T&& callback) { return send_internal(client, Callback<size_t, uint8_t*, size_t>::Derived<T>(std::move(callback))); }
[[nodiscard]] FORCEINLINE bool send(Client* client, T&& callback) { return send_internal(client, Callback<size_t, uint8_t*, size_t>::Derived<T>(std::move(callback))); }
private:
static void on_new_connection(uv_stream_t* server, int status);
@ -136,9 +136,9 @@ private:
void on_new_client(uv_stream_t* server);
void on_new_client(uv_stream_t* server, Client* client);
bool connect_to_peer(Client* client);
[[nodiscard]] bool connect_to_peer(Client* client);
bool send_internal(Client* client, Callback<size_t, uint8_t*, size_t>::Base&& callback);
[[nodiscard]] bool send_internal(Client* client, Callback<size_t, uint8_t*, size_t>::Base&& callback);
allocate_client_callback m_allocateNewClient;
@ -195,7 +195,7 @@ protected:
uv_mutex_t m_bansLock;
unordered_map<raw_ip, std::chrono::steady_clock::time_point> m_bans;
bool is_banned(bool is_v6, raw_ip ip);
[[nodiscard]] bool is_banned(bool is_v6, raw_ip ip);
unordered_set<raw_ip> m_pendingConnections;