From 899d5a82dd29da07d99efce28eb5b9cbec3d3816 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Mon, 6 Sep 2021 23:33:52 +0200 Subject: [PATCH] data api: check p2pool shares that come after a Monero block --- src/p2pool.cpp | 13 +++++++++---- src/p2pool.h | 3 ++- src/side_chain.cpp | 23 ++++++++++++++++++++++- src/side_chain.h | 5 +++++ tests/src/hash_tests.cpp | 4 ++++ 5 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/p2pool.cpp b/src/p2pool.cpp index f3f5d00..1bfe444 100644 --- a/src/p2pool.cpp +++ b/src/p2pool.cpp @@ -220,7 +220,7 @@ void p2pool::handle_miner_data(MinerData& data) } } -static constexpr char BLOCK_FOUND[] = "\n\ +const char* BLOCK_FOUND = "\n\ -----------------------------------------------------------------------------------------------\n\ | ###### # ####### ##### # # ####### ####### # # # # ###### |\n\ | # # # # # # # # # # # # # # ## # # # |\n\ @@ -269,9 +269,14 @@ void p2pool::handle_chain_main(ChainMain& data, const char* extra) ", timestamp = " << log::Gray() << data.timestamp << log::NoColor() << ", reward = " << log::Gray() << log::XMRAmount(data.reward)); - if (!sidechain_id.empty() && side_chain().has_block(sidechain_id)) { - LOGINFO(0, log::LightGreen() << "BLOCK FOUND: main chain block at height " << data.height << " was mined by this p2pool" << BLOCK_FOUND); - api_update_block_found(&data); + if (!sidechain_id.empty()) { + if (side_chain().has_block(sidechain_id)) { + LOGINFO(0, log::LightGreen() << "BLOCK FOUND: main chain block at height " << data.height << " was mined by this p2pool" << BLOCK_FOUND); + api_update_block_found(&data); + } + else { + side_chain().watch_mainchain_block(data, sidechain_id); + } } api_update_network_stats(); diff --git a/src/p2pool.h b/src/p2pool.h index e156a1e..05828e8 100644 --- a/src/p2pool.h +++ b/src/p2pool.h @@ -72,6 +72,8 @@ public: bool chainmain_get_by_hash(const hash& id, ChainMain& data) const; + void api_update_block_found(const ChainMain* data); + private: p2pool(const p2pool&) = delete; p2pool(p2pool&&) = delete; @@ -115,7 +117,6 @@ private: void api_update_network_stats(); void api_update_pool_stats(); - void api_update_block_found(const ChainMain* data); struct FoundBlock { diff --git a/src/side_chain.cpp b/src/side_chain.cpp index 719e9b3..e69730e 100644 --- a/src/side_chain.cpp +++ b/src/side_chain.cpp @@ -348,6 +348,8 @@ bool SideChain::block_seen(const PoolBlock& block) return !m_seenBlocks.insert(block.m_sidechainId).second; } +extern const char* BLOCK_FOUND; + bool SideChain::add_external_block(PoolBlock& block, std::vector& missing_blocks) { if (block.m_difficulty < m_minDifficulty) { @@ -419,6 +421,8 @@ bool SideChain::add_external_block(PoolBlock& block, std::vector& missing_ return false; } + bool block_found = false; + missing_blocks.clear(); { MutexLock lock(m_sidechainLock); @@ -431,6 +435,17 @@ bool SideChain::add_external_block(PoolBlock& block, std::vector& missing_ missing_blocks.push_back(h); } } + + if (block.m_sidechainId == m_watchBlockSidechainId) { + LOGINFO(0, log::LightGreen() << "BLOCK FOUND: main chain block at height " << m_watchBlock.height << " was mined by this p2pool" << BLOCK_FOUND); + m_watchBlockSidechainId = {}; + data = m_watchBlock; + block_found = true; + } + } + + if (block_found) { + m_pool->api_update_block_found(&data); } add_block(block); @@ -487,6 +502,13 @@ bool SideChain::has_block(const hash& id) return m_blocksById.find(id) != m_blocksById.end(); } +void SideChain::watch_mainchain_block(const ChainMain& data, const hash& possible_id) +{ + MutexLock lock(m_sidechainLock); + m_watchBlock = data; + m_watchBlockSidechainId = possible_id; +} + bool SideChain::get_block_blob(const hash& id, std::vector& blob) { MutexLock lock(m_sidechainLock); @@ -659,7 +681,6 @@ void SideChain::print_status() difficulty_type SideChain::total_hashes() const { - MutexLock lock(m_sidechainLock); return m_chainTip ? m_chainTip->m_cumulativeDifficulty : difficulty_type(); } diff --git a/src/side_chain.h b/src/side_chain.h index 68d48f7..acd9495 100644 --- a/src/side_chain.h +++ b/src/side_chain.h @@ -52,6 +52,8 @@ public: void get_missing_blocks(std::vector& missing_blocks); bool has_block(const hash& id); + void watch_mainchain_block(const ChainMain& data, const hash& possible_id); + bool get_block_blob(const hash& id, std::vector& blob); bool get_outputs_blob(PoolBlock* block, uint64_t total_reward, std::vector& blob); @@ -109,6 +111,9 @@ private: std::vector m_consensusId; difficulty_type m_curDifficulty; + + ChainMain m_watchBlock; + hash m_watchBlockSidechainId; }; } // namespace p2pool diff --git a/tests/src/hash_tests.cpp b/tests/src/hash_tests.cpp index 59073b1..50fd83f 100644 --- a/tests/src/hash_tests.cpp +++ b/tests/src/hash_tests.cpp @@ -27,6 +27,10 @@ TEST(hash, constructor) hash h; uint8_t buf[HASH_SIZE]{}; ASSERT_EQ(memcmp(h.h, buf, HASH_SIZE), 0); + + memset(h.h, -1, HASH_SIZE); + h = {}; + ASSERT_EQ(memcmp(h.h, buf, HASH_SIZE), 0); } TEST(hash, compare)