diff --git a/src/llmq/blockprocessor.cpp b/src/llmq/blockprocessor.cpp index 432b4672a46f..a1f75e7ce28c 100644 --- a/src/llmq/blockprocessor.cpp +++ b/src/llmq/blockprocessor.cpp @@ -186,8 +186,7 @@ bool CQuorumBlockProcessor::ProcessBlock(const CBlock& block, gsl::not_nullnHeight, blockHash, qc, state, fJustCheck, fBLSChecks)) { LogPrintf("[ProcessBlock] failed h[%d] llmqType[%d] version[%d] quorumIndex[%d] quorumHash[%s]\n", pindex->nHeight, ToUnderlying(qc.llmqType), qc.nVersion, qc.quorumIndex, qc.quorumHash.ToString()); return false; @@ -317,8 +316,8 @@ bool CQuorumBlockProcessor::UndoBlock(const CBlock& block, gsl::not_nullnHeight)); } - { - LOCK(minableCommitmentsCs); - mapHasMinedCommitmentCache[qc.llmqType].erase(qc.quorumHash); - } + WITH_LOCK(minableCommitmentsCs, mapHasMinedCommitmentCache[qc.llmqType].erase(qc.quorumHash)); // if a reorg happened, we should allow to mine this commitment later AddMineableCommitment(qc); @@ -452,11 +448,8 @@ uint256 CQuorumBlockProcessor::GetQuorumBlockHash(const Consensus::LLMQParams& l bool CQuorumBlockProcessor::HasMinedCommitment(Consensus::LLMQType llmqType, const uint256& quorumHash) const { bool fExists; - { - LOCK(minableCommitmentsCs); - if (mapHasMinedCommitmentCache[llmqType].get(quorumHash, fExists)) { - return fExists; - } + if (LOCK(minableCommitmentsCs); mapHasMinedCommitmentCache[llmqType].get(quorumHash, fExists)) { + return fExists; } fExists = m_evoDb.Exists(std::make_pair(DB_MINED_COMMITMENT, std::make_pair(llmqType, quorumHash))); @@ -646,28 +639,29 @@ bool CQuorumBlockProcessor::HasMineableCommitment(const uint256& hash) const void CQuorumBlockProcessor::AddMineableCommitment(const CFinalCommitment& fqc) { - bool relay = false; - uint256 commitmentHash = ::SerializeHash(fqc); + const uint256 commitmentHash = ::SerializeHash(fqc); - { + const bool relay = [&]() { LOCK(minableCommitmentsCs); auto k = std::make_pair(fqc.llmqType, fqc.quorumHash); - auto ins = minableCommitmentsByQuorum.try_emplace(k, commitmentHash); - if (ins.second) { + auto [itInserted, successfullyInserted] = minableCommitmentsByQuorum.try_emplace(k, commitmentHash); + if (successfullyInserted) { minableCommitments.try_emplace(commitmentHash, fqc); - relay = true; + return true; } else { - const auto& oldFqc = minableCommitments.at(ins.first->second); + auto& insertedQuorumHash = itInserted->second; + const auto& oldFqc = minableCommitments.at(insertedQuorumHash); if (fqc.CountSigners() > oldFqc.CountSigners()) { // new commitment has more signers, so override the known one - ins.first->second = commitmentHash; - minableCommitments.erase(ins.first->second); + insertedQuorumHash = commitmentHash; + minableCommitments.erase(insertedQuorumHash); minableCommitments.try_emplace(commitmentHash, fqc); - relay = true; + return true; } } - } + return false; + }(); // We only relay the new commitment if it's new or better then the old one if (relay) { diff --git a/src/llmq/chainlocks.cpp b/src/llmq/chainlocks.cpp index 22294c107f19..8ad1f327df2d 100644 --- a/src/llmq/chainlocks.cpp +++ b/src/llmq/chainlocks.cpp @@ -135,7 +135,7 @@ PeerMsgRet CChainLocksHandler::ProcessNewChainLock(const NodeId from, const llmq return {}; } - CBlockIndex* pindex = WITH_LOCK(cs_main, return m_chainstate.m_blockman.LookupBlockIndex(clsig.getBlockHash())); + const CBlockIndex* pindex = WITH_LOCK(cs_main, return m_chainstate.m_blockman.LookupBlockIndex(clsig.getBlockHash())); { LOCK(cs); @@ -428,7 +428,7 @@ CChainLocksHandler::BlockTxs::mapped_type CChainLocksHandler::GetBlockTxs(const } ret = std::make_shared>(); - for (auto& tx : block.vtx) { + for (const auto& tx : block.vtx) { if (tx->IsCoinBase() || tx->vin.empty()) { continue; } @@ -495,9 +495,8 @@ void CChainLocksHandler::EnforceBestChainLock() LogPrint(BCLog::CHAINLOCKS, "CChainLocksHandler::%s -- enforcing block %s via CLSIG (%s)\n", __func__, pindex->GetBlockHash().ToString(), clsig->ToString()); m_chainstate.EnforceBlock(dummy_state, pindex); - bool activateNeeded = WITH_LOCK(::cs_main, return m_chainstate.m_chain.Tip()->GetAncestor(currentBestChainLockBlockIndex->nHeight)) != currentBestChainLockBlockIndex; - if (activateNeeded) { + if (/*activateNeeded =*/ WITH_LOCK(::cs_main, return m_chainstate.m_chain.Tip()->GetAncestor(currentBestChainLockBlockIndex->nHeight)) != currentBestChainLockBlockIndex) { if (!m_chainstate.ActivateBestChain(dummy_state)) { LogPrintf("CChainLocksHandler::%s -- ActivateBestChain failed: %s\n", __func__, dummy_state.ToString()); return;