From 4e55f53fb4ae0482216bb3495b4af9cc0fb20799 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Sun, 8 Oct 2023 18:52:06 +0200 Subject: [PATCH] Notify about new P2Pool versions --- src/block_template.cpp | 2 +- src/p2p_server.cpp | 16 ++++++++++++++++ src/p2p_server.h | 1 + src/side_chain.cpp | 24 ++++++++++++++++++++++++ src/side_chain.h | 2 ++ 5 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/block_template.cpp b/src/block_template.cpp index b453873..22ec36e 100644 --- a/src/block_template.cpp +++ b/src/block_template.cpp @@ -646,7 +646,7 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, const // Layout: [software id, version, random number, sidechain extra_nonce] uint32_t* sidechain_extra = m_poolBlockTemplate->m_sidechainExtraBuf; - sidechain_extra[0] = 0; + sidechain_extra[0] = static_cast(SoftwareID::P2Pool); #ifdef P2POOL_SIDECHAIN_EXTRA_1 sidechain_extra[1] = P2POOL_SIDECHAIN_EXTRA_1; #else diff --git a/src/p2p_server.cpp b/src/p2p_server.cpp index 97d5d5f..e6f87f7 100644 --- a/src/p2p_server.cpp +++ b/src/p2p_server.cpp @@ -1081,6 +1081,7 @@ void P2PServer::on_timer() update_peer_connections(); check_host(); check_block_template(); + check_for_updates(); api_update_local_stats(); } @@ -1255,6 +1256,21 @@ void P2PServer::check_block_template() } } +void P2PServer::check_for_updates() const +{ + if (m_timerCounter % (3600 / m_timerInterval) != 0) { + return; + } + + const SideChain& s = m_pool->side_chain(); + + if (s.precalcFinished() && s.p2pool_update_available()) { + LOGINFO(0, log::LightCyan() << "********************************************************************************"); + LOGINFO(0, log::LightCyan() << "* An updated P2Pool version is available, visit p2pool.io for more information *"); + LOGINFO(0, log::LightCyan() << "********************************************************************************"); + } +} + P2PServer::P2PClient::P2PClient() : Client(m_p2pReadBuf, sizeof(m_p2pReadBuf)) , m_peerId(0) diff --git a/src/p2p_server.h b/src/p2p_server.h index 1006c60..90f46c9 100644 --- a/src/p2p_server.h +++ b/src/p2p_server.h @@ -198,6 +198,7 @@ private: void download_missing_blocks(); void check_host(); void check_block_template(); + void check_for_updates() const; void update_peer_connections(); void update_peer_list(); void send_peer_list_request(P2PClient* client, uint64_t cur_time); diff --git a/src/side_chain.cpp b/src/side_chain.cpp index b0feaf9..e571ab1 100644 --- a/src/side_chain.cpp +++ b/src/side_chain.cpp @@ -1304,6 +1304,30 @@ bool SideChain::get_difficulty(const PoolBlock* tip, std::vector return true; } +bool SideChain::p2pool_update_available() const +{ + constexpr uint32_t version = (P2POOL_VERSION_MAJOR << 16) | P2POOL_VERSION_MINOR; + + difficulty_type total_p2pool_diff, newer_p2pool_diff; + { + ReadLock lock(m_sidechainLock); + + const PoolBlock* cur = m_chainTip; + + for (uint64_t i = 0; (i < m_chainWindowSize) && cur; ++i, cur = get_parent(cur)) { + if (cur->m_sidechainExtraBuf[0] == static_cast(SoftwareID::P2Pool)) { + total_p2pool_diff += cur->m_difficulty; + if (cur->m_sidechainExtraBuf[1] > version) { + newer_p2pool_diff += cur->m_difficulty; + } + } + } + } + + // Assume that a new version is out if >= 20% of hashrate is using it already + return newer_p2pool_diff * 5 >= total_p2pool_diff; +} + void SideChain::verify_loop(PoolBlock* block) { // PoW is already checked at this point diff --git a/src/side_chain.h b/src/side_chain.h index e5dc0bb..a0e1dd3 100644 --- a/src/side_chain.h +++ b/src/side_chain.h @@ -82,6 +82,8 @@ public: const PoolBlock* chainTip() const { return m_chainTip; } bool precalcFinished() const { return m_precalcFinished.load(); } + bool p2pool_update_available() const; + #ifdef P2POOL_UNIT_TESTS difficulty_type m_testMainChainDiff; const unordered_map& blocksById() const { return m_blocksById; }