Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 18 additions & 24 deletions src/llmq/blockprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ bool CQuorumBlockProcessor::ProcessBlock(const CBlock& block, gsl::not_null<cons
}
}

for (const auto& p : qcs) {
const auto& qc = p.second;
for (const auto& [_, qc] : qcs) {
if (!ProcessCommitment(pindex->nHeight, 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;
Expand Down Expand Up @@ -317,8 +316,8 @@ bool CQuorumBlockProcessor::UndoBlock(const CBlock& block, gsl::not_null<const C
return false;
}

for (auto& p : qcs) {
auto& qc = p.second;
for (auto& [_, qc2] : qcs) {
auto& qc = qc2; // cannot capture structured binding into lambda
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could do 22e9655 instead

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh; interesting, but I think it's fine as is

if (qc.IsNull()) {
continue;
}
Expand All @@ -334,10 +333,7 @@ bool CQuorumBlockProcessor::UndoBlock(const CBlock& block, gsl::not_null<const C
m_evoDb.Erase(BuildInversedHeightKey(qc.llmqType, pindex->nHeight));
}

{
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);
Expand Down Expand Up @@ -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)));
Expand Down Expand Up @@ -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 = [&]() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

smart :D I am always annoyed by passing variables out of LOCK scope

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) {
Expand Down
7 changes: 3 additions & 4 deletions src/llmq/chainlocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -428,7 +428,7 @@ CChainLocksHandler::BlockTxs::mapped_type CChainLocksHandler::GetBlockTxs(const
}

ret = std::make_shared<std::unordered_set<uint256, StaticSaltedHasher>>();
for (auto& tx : block.vtx) {
for (const auto& tx : block.vtx) {
if (tx->IsCoinBase() || tx->vin.empty()) {
continue;
}
Expand Down Expand Up @@ -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;
Expand Down