SideChain: added some extra checks, removed const_cast

This commit is contained in:
SChernykh
2025-10-01 18:05:26 +02:00
parent e1da9ff4d3
commit 6b4c39f4c9
4 changed files with 17 additions and 16 deletions
+1 -1
View File
@@ -37,4 +37,4 @@ jobs:
- name: Run clang-tidy - name: Run clang-tidy
run: | run: |
cd src 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=^[^\./]
+4 -1
View File
@@ -271,7 +271,10 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, const
m_poolBlockTemplate->m_minerWallet = miner_wallet; 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 // Pre-calculate outputs to speed up miner tx generation
if (!m_shares.empty()) { if (!m_shares.empty()) {
+10 -12
View File
@@ -238,7 +238,7 @@ SideChain::~SideChain()
s_networkType = NetworkType::Invalid; s_networkType = NetworkType::Invalid;
} }
void SideChain::fill_sidechain_data(PoolBlock& block, std::vector<MinerShare>& shares) const bool SideChain::fill_sidechain_data(PoolBlock& block, std::vector<MinerShare>& shares) const
{ {
block.m_uncles.clear(); block.m_uncles.clear();
@@ -254,10 +254,7 @@ void SideChain::fill_sidechain_data(PoolBlock& block, std::vector<MinerShare>& s
block.m_txkeySecSeed = m_consensusHash; block.m_txkeySecSeed = m_consensusHash;
get_tx_keys(block.m_txkeyPub, block.m_txkeySec, block.m_txkeySecSeed, block.m_prevId); get_tx_keys(block.m_txkeyPub, block.m_txkeySec, block.m_txkeySecSeed, block.m_prevId);
if (!get_shares(&block, shares)) { return get_shares(&block, shares);
LOGERR(6, "fill_sidechain_data: get_shares failed");
}
return;
} }
block.m_txkeySecSeed = (block.m_prevId == tip->m_prevId) ? tip->m_txkeySecSeed : tip->calculate_tx_key_seed(); 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<MinerShare>& s
block.m_cumulativeDifficulty += it->second->m_difficulty; block.m_cumulativeDifficulty += it->second->m_difficulty;
} }
if (!get_shares(&block, shares)) { return get_shares(&block, shares);
LOGERR(6, "fill_sidechain_data: get_shares failed");
}
} }
P2PServer* SideChain::p2pServer() const P2PServer* SideChain::p2pServer() const
@@ -1376,7 +1371,7 @@ void SideChain::verify_loop(PoolBlock* block)
// PoW is already checked at this point // PoW is already checked at this point
std::vector<PoolBlock*> blocks_to_verify(1, block); std::vector<PoolBlock*> blocks_to_verify(1, block);
const PoolBlock* highest_block = nullptr; PoolBlock* highest_block = nullptr;
while (!blocks_to_verify.empty()) { while (!blocks_to_verify.empty()) {
block = blocks_to_verify.back(); block = blocks_to_verify.back();
@@ -1803,7 +1798,7 @@ void SideChain::verify(PoolBlock* block)
block->m_invalid = false; 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) { if (!block->m_verified || block->m_invalid) {
LOGERR(1, "trying to update chain tip to an unverified or invalid block, fix the code!"); 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; return;
} }
const PoolBlock* tip = m_chainTip; PoolBlock* tip = m_chainTip;
if (block == tip) { if (block == tip) {
LOGINFO(5, "Trying to update chain tip to the same block again. Ignoring it."); 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)) { if (is_longer_chain(tip, block, is_alternative)) {
difficulty_type diff; difficulty_type diff;
if (get_difficulty(block, m_difficultyData, diff)) { if (get_difficulty(block, m_difficultyData, diff)) {
m_chainTip = const_cast<PoolBlock*>(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); WriteLock lock(m_curDifficultyLock);
m_curDifficulty = diff; m_curDifficulty = diff;
+2 -2
View File
@@ -43,7 +43,7 @@ public:
SideChain(p2pool* pool, NetworkType type, const char* pool_name = nullptr); SideChain(p2pool* pool, NetworkType type, const char* pool_name = nullptr);
~SideChain(); ~SideChain();
void fill_sidechain_data(PoolBlock& block, std::vector<MinerShare>& shares) const; [[nodiscard]] bool fill_sidechain_data(PoolBlock& block, std::vector<MinerShare>& shares) const;
[[nodiscard]] bool incoming_block_seen(const PoolBlock& block); [[nodiscard]] bool incoming_block_seen(const PoolBlock& block);
void forget_incoming_block(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>& difficultyData, difficulty_type& curDifficulty) const; [[nodiscard]] bool get_difficulty(const PoolBlock* tip, std::vector<DifficultyData>& difficultyData, difficulty_type& curDifficulty) const;
void verify_loop(PoolBlock* block); void verify_loop(PoolBlock* block);
void verify(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; [[nodiscard]] PoolBlock* get_parent(const PoolBlock* block) const;
// Checks if "candidate" has longer (higher difficulty) chain than "block" // Checks if "candidate" has longer (higher difficulty) chain than "block"