From c2ac444abf9873018b2d3ada3c0d3997919cfc55 Mon Sep 17 00:00:00 2001 From: pasta Date: Sun, 17 Dec 2023 13:26:59 -0600 Subject: [PATCH 1/5] refactor: create a immediately executed llamda; use some structed bindings inside of ti --- src/llmq/blockprocessor.cpp | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/llmq/blockprocessor.cpp b/src/llmq/blockprocessor.cpp index 432b4672a4..0849ec8866 100644 --- a/src/llmq/blockprocessor.cpp +++ b/src/llmq/blockprocessor.cpp @@ -646,28 +646,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) { From ee6dc2d6abb8e29ef795a1d0f5d38744769fa4b3 Mon Sep 17 00:00:00 2001 From: pasta Date: Sun, 17 Dec 2023 13:30:34 -0600 Subject: [PATCH 2/5] refactor: remove some nested code blocks --- src/llmq/blockprocessor.cpp | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/llmq/blockprocessor.cpp b/src/llmq/blockprocessor.cpp index 0849ec8866..426d982624 100644 --- a/src/llmq/blockprocessor.cpp +++ b/src/llmq/blockprocessor.cpp @@ -334,10 +334,7 @@ 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 +449,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))); From 4804aad2c98efa9693e7e1cdfeec62e0bf8cf3ca Mon Sep 17 00:00:00 2001 From: pasta Date: Sun, 17 Dec 2023 14:00:11 -0600 Subject: [PATCH 3/5] refactor: a few more structured bindings --- src/llmq/blockprocessor.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/llmq/blockprocessor.cpp b/src/llmq/blockprocessor.cpp index 426d982624..a1f75e7ce2 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_null Date: Sun, 17 Dec 2023 14:10:16 -0600 Subject: [PATCH 4/5] refactor: add some const --- src/llmq/chainlocks.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/llmq/chainlocks.cpp b/src/llmq/chainlocks.cpp index 22294c107f..9e69f89b31 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; } From bf57b15496b88ab40d0c2915c84debe4e94f997f Mon Sep 17 00:00:00 2001 From: pasta Date: Sun, 17 Dec 2023 14:13:04 -0600 Subject: [PATCH 5/5] refactor: remove unneeded variable --- src/llmq/chainlocks.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/llmq/chainlocks.cpp b/src/llmq/chainlocks.cpp index 9e69f89b31..8ad1f327df 100644 --- a/src/llmq/chainlocks.cpp +++ b/src/llmq/chainlocks.cpp @@ -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;