From 1657b11b6e856bbd9c8b77c7b882f86cac123f77 Mon Sep 17 00:00:00 2001 From: pasta Date: Tue, 10 Nov 2026 00:41:59 -0600 Subject: [PATCH] fix(llmq): prefer final commitments with more signers The pre-verification check in ProcessMessage dropped an incoming final commitment when the stored one had fewer or equal signers, so a better commitment was discarded instead of a worse one. AddMineableCommitment also erased the new hash instead of the replaced one, leaking the old commitment in minableCommitments. Co-Authored-By: Claude Opus 5.5 --- src/llmq/blockprocessor.cpp | 4 +- src/test/llmq_blockprocessor_tests.cpp | 64 ++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/src/llmq/blockprocessor.cpp b/src/llmq/blockprocessor.cpp index f8442e093ab4..4e4de64d1f6a 100644 --- a/src/llmq/blockprocessor.cpp +++ b/src/llmq/blockprocessor.cpp @@ -186,7 +186,7 @@ MessageProcessingResult CQuorumBlockProcessor::ProcessMessage(const CNode& peer, auto it = minableCommitmentsByQuorum.find(k); if (it != minableCommitmentsByQuorum.end()) { auto jt = minableCommitments.find(it->second); - if (jt != minableCommitments.end() && jt->second.CountSigners() <= qc.CountSigners()) { + if (jt != minableCommitments.end() && jt->second.CountSigners() >= qc.CountSigners()) { return ret; } } @@ -873,8 +873,8 @@ std::optional CQuorumBlockProcessor::AddMineableCommitment(const CFinalCom const auto& oldFqc = minableCommitments.at(insertedQuorumHash); if (fqc.CountSigners() > oldFqc.CountSigners()) { // new commitment has more signers, so override the known one - insertedQuorumHash = commitmentHash; minableCommitments.erase(insertedQuorumHash); + insertedQuorumHash = commitmentHash; minableCommitments.try_emplace(commitmentHash, fqc); return true; } diff --git a/src/test/llmq_blockprocessor_tests.cpp b/src/test/llmq_blockprocessor_tests.cpp index fc12bc0b810f..f2f5907650ad 100644 --- a/src/test/llmq_blockprocessor_tests.cpp +++ b/src/test/llmq_blockprocessor_tests.cpp @@ -135,4 +135,68 @@ BOOST_FIXTURE_TEST_CASE(unrequested_qfcommit_is_dropped_and_scored, TestChain100 SetMockTime(0s); } +// A relayed commitment for a quorum we already hold a mineable commitment for is only worth +// verifying, and relaying, when it has more signers than ours. +BOOST_FIXTURE_TEST_CASE(qfcommit_with_more_signers_replaces_stored_one, TestChain100Setup) +{ + LOCK(NetEventsInterface::g_msgproc_mutex); + BOOST_REQUIRE(!m_node.chainman->IsInitialBlockDownload()); + BOOST_REQUIRE(m_node.mn_metaman->LoadCache(/*load_cache=*/false)); + + auto& blockprocessor = *m_node.llmq_ctx->quorum_block_processor; + const auto& llmq_params = GetLLMQParams(Consensus::LLMQType::LLMQ_TEST_V17); + BOOST_REQUIRE(Params().GetLLMQ(llmq_params.type).has_value()); + BOOST_REQUIRE_EQUAL(llmq_params.size, 3); + + const CBlockIndex* base_index{WITH_LOCK(::cs_main, return m_node.chainman->ActiveChain().Tip())}; + base_index = base_index->GetAncestor(base_index->nHeight - (base_index->nHeight % llmq_params.dkgInterval)); + + auto with_signers = [&](const CFinalCommitment& qc, size_t count) { + CFinalCommitment ret{qc}; + for (size_t i = 0; i < ret.signers.size(); ++i) { + ret.signers[i] = i < count; + } + return ret; + }; + const auto base_qc = CreateValidCommitment(llmq_params, base_index->GetBlockHash()); + const auto qc_one = with_signers(base_qc, 1); + const auto qc_two = with_signers(base_qc, 2); + const auto qc_three = with_signers(base_qc, 3); + auto qc_two_other{qc_three}; + qc_two_other.signers[0] = false; + + BOOST_REQUIRE(blockprocessor.AddMineableCommitment(qc_two).has_value()); + + NodeId next_id{61}; + // Returns the misbehavior score the peer collected. The payloads carry random signatures, so a + // commitment that reaches verification is rejected with 100, while one dropped by the + // signer-count pre-check costs nothing. + auto deliver = [&](const CFinalCommitment& qc) EXCLUSIVE_LOCKS_REQUIRED(NetEventsInterface::g_msgproc_mutex) { + auto peer{MakeTestPeer(next_id++)}; + m_node.peerman->InitializeNode(*peer, NODE_NETWORK); + AnnounceInv(*m_node.peerman, *peer, CInv{MSG_QUORUM_FINAL_COMMITMENT, ::SerializeHash(qc)}); + SetMockTime(GetTime() + 61s); + m_node.peerman->SendMessages(peer.get()); + SendMessage(*m_node.peerman, *peer, NetMsgType::QFCOMMITMENT, Serialized(qc)); + const int score{MisbehaviorScore(*m_node.peerman, *peer)}; + m_node.peerman->FinalizeNode(*peer); + return score; + }; + + BOOST_CHECK_EQUAL(deliver(qc_one), 0); + BOOST_CHECK_EQUAL(deliver(qc_two_other), 0); + BOOST_CHECK_EQUAL(deliver(qc_three), 100); + + BOOST_CHECK(!blockprocessor.AddMineableCommitment(qc_one).has_value()); + BOOST_CHECK(blockprocessor.HasMineableCommitment(::SerializeHash(qc_two))); + + const auto inv = blockprocessor.AddMineableCommitment(qc_three); + BOOST_REQUIRE(inv.has_value()); + BOOST_CHECK(inv->hash == ::SerializeHash(qc_three)); + BOOST_CHECK(blockprocessor.HasMineableCommitment(::SerializeHash(qc_three))); + BOOST_CHECK(!blockprocessor.HasMineableCommitment(::SerializeHash(qc_two))); + + SetMockTime(0s); +} + BOOST_AUTO_TEST_SUITE_END()