Skip to content

Commit

Permalink
Notify about new P2Pool versions
Browse files Browse the repository at this point in the history
  • Loading branch information
SChernykh committed Oct 8, 2023
1 parent 4d944d3 commit 4e55f53
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/block_template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t>(SoftwareID::P2Pool);
#ifdef P2POOL_SIDECHAIN_EXTRA_1
sidechain_extra[1] = P2POOL_SIDECHAIN_EXTRA_1;
#else
Expand Down
16 changes: 16 additions & 0 deletions src/p2p_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,7 @@ void P2PServer::on_timer()
update_peer_connections();
check_host();
check_block_template();
check_for_updates();
api_update_local_stats();
}

Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/p2p_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ class P2PServer : public TCPServer
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);
Expand Down
24 changes: 24 additions & 0 deletions src/side_chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1304,6 +1304,30 @@ bool SideChain::get_difficulty(const PoolBlock* tip, std::vector<DifficultyData>
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<uint32_t>(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
Expand Down
2 changes: 2 additions & 0 deletions src/side_chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ class SideChain : public nocopy_nomove
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<hash, PoolBlock*>& blocksById() const { return m_blocksById; }
Expand Down

0 comments on commit 4e55f53

Please sign in to comment.