From 8767ef9e190647bbeafd556ba93f16dbce2a54c3 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Sat, 29 Jan 2022 17:00:12 +0100 Subject: [PATCH] Display payout amount when a block is found --- src/p2pool.cpp | 8 +++++++- src/pool_block.cpp | 12 ++++++++++++ src/pool_block.h | 2 ++ src/side_chain.cpp | 15 +++++++++++++-- src/side_chain.h | 2 +- tests/src/pool_block_tests.cpp | 2 +- 6 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/p2pool.cpp b/src/p2pool.cpp index 919ba9d..8dafc09 100644 --- a/src/p2pool.cpp +++ b/src/p2pool.cpp @@ -32,6 +32,7 @@ #include "console_commands.h" #include "crypto.h" #include "p2pool_api.h" +#include "pool_block.h" #include #include @@ -339,8 +340,13 @@ void p2pool::handle_chain_main(ChainMain& data, const char* extra) ", reward = " << log::Gray() << log::XMRAmount(data.reward)); if (!sidechain_id.empty()) { - if (side_chain().has_block(sidechain_id)) { + PoolBlock* block = side_chain().find_block(sidechain_id); + if (block) { LOGINFO(0, log::LightGreen() << "BLOCK FOUND: main chain block at height " << data.height << " was mined by this p2pool" << BLOCK_FOUND); + const uint64_t payout = block->get_payout(params().m_wallet); + if (payout) { + LOGINFO(0, log::LightCyan() << "You received a payout of " << log::LightGreen() << log::XMRAmount(payout) << log::LightCyan() << " in block " << log::LightGreen() << data.height); + } api_update_block_found(&data); } else { diff --git a/src/pool_block.cpp b/src/pool_block.cpp index 7ed0172..ecc7a2c 100644 --- a/src/pool_block.cpp +++ b/src/pool_block.cpp @@ -298,4 +298,16 @@ bool PoolBlock::get_pow_hash(RandomX_Hasher_Base* hasher, uint64_t height, const return hasher->calculate(blob, blob_size, height, seed_hash, pow_hash); } +uint64_t PoolBlock::get_payout(const Wallet& w) const +{ + for (size_t i = 0, n = m_outputs.size(); i < n; ++i) { + hash eph_public_key; + if ((w.get_eph_public_key(m_txkeySec, i, eph_public_key)) && (eph_public_key == m_outputs[i].m_ephPublicKey)) { + return m_outputs[i].m_reward; + } + } + + return 0; +} + } // namespace p2pool diff --git a/src/pool_block.h b/src/pool_block.h index 91b6efe..0d78c4f 100644 --- a/src/pool_block.h +++ b/src/pool_block.h @@ -137,6 +137,8 @@ struct PoolBlock int deserialize(const uint8_t* data, size_t size, SideChain& sidechain); bool get_pow_hash(RandomX_Hasher_Base* hasher, uint64_t height, const hash& seed_hash, hash& pow_hash); + + uint64_t get_payout(const Wallet& w) const; }; } // namespace p2pool diff --git a/src/side_chain.cpp b/src/side_chain.cpp index 3d65703..c8d285f 100644 --- a/src/side_chain.cpp +++ b/src/side_chain.cpp @@ -481,6 +481,11 @@ bool SideChain::add_external_block(PoolBlock& block, std::vector& missing_ m_watchBlockSidechainId = {}; data = m_watchBlock; block_found = true; + + const uint64_t payout = block.get_payout(m_pool->params().m_wallet); + if (payout) { + LOGINFO(0, log::LightCyan() << "You received a payout of " << log::LightGreen() << log::XMRAmount(payout) << log::LightCyan() << " in block " << log::LightGreen() << data.height); + } } } @@ -536,10 +541,16 @@ void SideChain::add_block(const PoolBlock& block) m_seenWallets[new_block->m_minerWallet.spend_public_key()] = new_block->m_localTimestamp; } -bool SideChain::has_block(const hash& id) +PoolBlock* SideChain::find_block(const hash& id) { MutexLock lock(m_sidechainLock); - return m_blocksById.find(id) != m_blocksById.end(); + + auto it = m_blocksById.find(id); + if (it != m_blocksById.end()) { + return it->second; + } + + return nullptr; } void SideChain::watch_mainchain_block(const ChainMain& data, const hash& possible_id) diff --git a/src/side_chain.h b/src/side_chain.h index fe19c7f..be771dd 100644 --- a/src/side_chain.h +++ b/src/side_chain.h @@ -51,7 +51,7 @@ public: void add_block(const PoolBlock& block); void get_missing_blocks(std::vector& missing_blocks); - bool has_block(const hash& id); + PoolBlock* find_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); diff --git a/tests/src/pool_block_tests.cpp b/tests/src/pool_block_tests.cpp index 72e73d6..f51f4c8 100644 --- a/tests/src/pool_block_tests.cpp +++ b/tests/src/pool_block_tests.cpp @@ -125,7 +125,7 @@ TEST(pool_block, verify) p += n; sidechain.add_block(b); - ASSERT_TRUE(sidechain.has_block(b.m_sidechainId)); + ASSERT_TRUE(sidechain.find_block(b.m_sidechainId) != nullptr); } const PoolBlock* tip = sidechain.chainTip();