From 6b4c39f4c9ca471f9037545e5d768b1ec2b9aa00 Mon Sep 17 00:00:00 2001 From: SChernykh <15806605+SChernykh@users.noreply.github.com> Date: Wed, 1 Oct 2025 18:05:26 +0200 Subject: [PATCH] SideChain: added some extra checks, removed const_cast --- .github/workflows/clang-tidy.yml | 2 +- src/block_template.cpp | 5 ++++- src/side_chain.cpp | 22 ++++++++++------------ src/side_chain.h | 4 ++-- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/.github/workflows/clang-tidy.yml b/.github/workflows/clang-tidy.yml index e370d7b..3ed4078 100644 --- a/.github/workflows/clang-tidy.yml +++ b/.github/workflows/clang-tidy.yml @@ -37,4 +37,4 @@ jobs: - name: Run clang-tidy run: | cd src - clang-tidy-21 *.cpp -p ../build -checks=bugprone-*,clang-analyzer-*,llvm-*,cppcoreguidelines-pro-type-cstyle-cast,cppcoreguidelines-rvalue-reference-param-not-moved,hicpp-use-emplace,hicpp-use-override,-bugprone-easily-swappable-parameters,-bugprone-empty-catch,-llvm-include-order -warnings-as-errors=* -header-filter=^[^\./] + clang-tidy-21 *.cpp -p ../build -checks=bugprone-*,clang-analyzer-*,llvm-*,cppcoreguidelines-pro-type-const-cast,cppcoreguidelines-pro-type-cstyle-cast,cppcoreguidelines-rvalue-reference-param-not-moved,hicpp-use-emplace,hicpp-use-override,-bugprone-easily-swappable-parameters,-bugprone-empty-catch,-llvm-include-order -warnings-as-errors=* -header-filter=^[^\./] diff --git a/src/block_template.cpp b/src/block_template.cpp index 78ae531..5b7b711 100644 --- a/src/block_template.cpp +++ b/src/block_template.cpp @@ -271,7 +271,10 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, const m_poolBlockTemplate->m_minerWallet = miner_wallet; - m_sidechain->fill_sidechain_data(*m_poolBlockTemplate, m_shares); + if (!m_sidechain->fill_sidechain_data(*m_poolBlockTemplate, m_shares)) { + use_old_template(); + return; + } // Pre-calculate outputs to speed up miner tx generation if (!m_shares.empty()) { diff --git a/src/side_chain.cpp b/src/side_chain.cpp index 7632023..ba9ac3e 100644 --- a/src/side_chain.cpp +++ b/src/side_chain.cpp @@ -238,7 +238,7 @@ SideChain::~SideChain() s_networkType = NetworkType::Invalid; } -void SideChain::fill_sidechain_data(PoolBlock& block, std::vector& shares) const +bool SideChain::fill_sidechain_data(PoolBlock& block, std::vector& shares) const { block.m_uncles.clear(); @@ -254,10 +254,7 @@ void SideChain::fill_sidechain_data(PoolBlock& block, std::vector& s block.m_txkeySecSeed = m_consensusHash; get_tx_keys(block.m_txkeyPub, block.m_txkeySec, block.m_txkeySecSeed, block.m_prevId); - if (!get_shares(&block, shares)) { - LOGERR(6, "fill_sidechain_data: get_shares failed"); - } - return; + return get_shares(&block, shares); } block.m_txkeySecSeed = (block.m_prevId == tip->m_prevId) ? tip->m_txkeySecSeed : tip->calculate_tx_key_seed(); @@ -350,9 +347,7 @@ void SideChain::fill_sidechain_data(PoolBlock& block, std::vector& s block.m_cumulativeDifficulty += it->second->m_difficulty; } - if (!get_shares(&block, shares)) { - LOGERR(6, "fill_sidechain_data: get_shares failed"); - } + return get_shares(&block, shares); } P2PServer* SideChain::p2pServer() const @@ -1376,7 +1371,7 @@ void SideChain::verify_loop(PoolBlock* block) // PoW is already checked at this point std::vector blocks_to_verify(1, block); - const PoolBlock* highest_block = nullptr; + PoolBlock* highest_block = nullptr; while (!blocks_to_verify.empty()) { block = blocks_to_verify.back(); @@ -1803,7 +1798,7 @@ void SideChain::verify(PoolBlock* block) block->m_invalid = false; } -void SideChain::update_chain_tip(const PoolBlock* block) +void SideChain::update_chain_tip(PoolBlock* block) { if (!block->m_verified || block->m_invalid) { LOGERR(1, "trying to update chain tip to an unverified or invalid block, fix the code!"); @@ -1815,7 +1810,7 @@ void SideChain::update_chain_tip(const PoolBlock* block) return; } - const PoolBlock* tip = m_chainTip; + PoolBlock* tip = m_chainTip; if (block == tip) { LOGINFO(5, "Trying to update chain tip to the same block again. Ignoring it."); @@ -1826,7 +1821,10 @@ void SideChain::update_chain_tip(const PoolBlock* block) if (is_longer_chain(tip, block, is_alternative)) { difficulty_type diff; if (get_difficulty(block, m_difficultyData, diff)) { - m_chainTip = const_cast(block); + if (!m_chainTip.compare_exchange_strong(tip, block)) { + LOGINFO(5, "Trying to update an outdated chain tip. Ignoring it."); + return; + } { WriteLock lock(m_curDifficultyLock); m_curDifficulty = diff; diff --git a/src/side_chain.h b/src/side_chain.h index 8103865..6c36c47 100644 --- a/src/side_chain.h +++ b/src/side_chain.h @@ -43,7 +43,7 @@ public: SideChain(p2pool* pool, NetworkType type, const char* pool_name = nullptr); ~SideChain(); - void fill_sidechain_data(PoolBlock& block, std::vector& shares) const; + [[nodiscard]] bool fill_sidechain_data(PoolBlock& block, std::vector& shares) const; [[nodiscard]] bool incoming_block_seen(const PoolBlock& block); void forget_incoming_block(const PoolBlock& block); @@ -103,7 +103,7 @@ private: [[nodiscard]] bool get_difficulty(const PoolBlock* tip, std::vector& difficultyData, difficulty_type& curDifficulty) const; void verify_loop(PoolBlock* block); void verify(PoolBlock* block); - void update_chain_tip(const PoolBlock* block); + void update_chain_tip(PoolBlock* block); [[nodiscard]] PoolBlock* get_parent(const PoolBlock* block) const; // Checks if "candidate" has longer (higher difficulty) chain than "block"