From 2d7a4f5002a08aaf90268be52f2b0704b23ecba1 Mon Sep 17 00:00:00 2001 From: pasta Date: Wed, 4 Nov 2026 05:34:23 +0000 Subject: [PATCH] fix(llmq): erase the superseded mineable commitment on replacement When AddMineableCommitment received a commitment with more signers than the one already pooled for that quorum, it pointed the quorum's entry at the new hash first and then erased by that same reference. The erase therefore targeted the new hash, which was not in the map yet, and the superseded commitment stayed in minableCommitments. This has been the case since commitments were first pooled in #2477. The replacement runs when a DKG member's own finalization yields a commitment with more signers than one it already pooled. Nothing pointed at the old entry afterwards, so it was never chosen for a block template, but HasMineableCommitment and GetMineableCommitmentByHash still found it: the node kept answering AlreadyHave for it and serving it on GETDATA, and held it for the life of the process unless that exact commitment was later mined. Erase the old entry before repointing the quorum's entry at the new one. Co-Authored-By: Claude Opus 5.5 --- src/llmq/blockprocessor.cpp | 9 +++--- src/test/llmq_blockprocessor_tests.cpp | 41 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/llmq/blockprocessor.cpp b/src/llmq/blockprocessor.cpp index f8442e093ab4..99c7c7bfedb0 100644 --- a/src/llmq/blockprocessor.cpp +++ b/src/llmq/blockprocessor.cpp @@ -869,12 +869,11 @@ std::optional CQuorumBlockProcessor::AddMineableCommitment(const CFinalCom minableCommitments.try_emplace(commitmentHash, fqc); return true; } else { - auto& insertedQuorumHash = itInserted->second; - const auto& oldFqc = minableCommitments.at(insertedQuorumHash); - if (fqc.CountSigners() > oldFqc.CountSigners()) { + auto& knownCommitmentHash = itInserted->second; + if (fqc.CountSigners() > minableCommitments.at(knownCommitmentHash).CountSigners()) { // new commitment has more signers, so override the known one - insertedQuorumHash = commitmentHash; - minableCommitments.erase(insertedQuorumHash); + minableCommitments.erase(knownCommitmentHash); + knownCommitmentHash = 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 46ccfc1f1f16..51b59210ad9a 100644 --- a/src/test/llmq_blockprocessor_tests.cpp +++ b/src/test/llmq_blockprocessor_tests.cpp @@ -135,4 +135,45 @@ BOOST_FIXTURE_TEST_CASE(unrequested_qfcommit_is_dropped_and_scored, TestChain100 SetMockTime(0s); } +// net_processing answers AlreadyHave for QFCOMMITMENT invs with HasMineableCommitment and GETDATA +// with GetMineableCommitmentByHash, so a superseded commitment that either lookup still finds is one +// this node keeps serving to peers. +BOOST_FIXTURE_TEST_CASE(superseded_mineable_commitment_is_dropped, RegTestingSetup) +{ + auto& blockprocessor = *m_node.llmq_ctx->quorum_block_processor; + const auto& llmq_params = GetLLMQParams(Consensus::LLMQType::LLMQ_TEST); + const uint256 quorum_hash = GetTestQuorumHash(1); + + auto worse = CreateValidCommitment(llmq_params, quorum_hash); + worse.signers = CreateBitVector(llmq_params.size, {0, 1}); + auto better = CreateValidCommitment(llmq_params, quorum_hash); + auto tied = better; + tied.quorumVvecHash = InsecureRand256(); + const uint256 worse_hash = ::SerializeHash(worse); + const uint256 better_hash = ::SerializeHash(better); + const uint256 tied_hash = ::SerializeHash(tied); + + const auto worse_inv = blockprocessor.AddMineableCommitment(worse); + BOOST_REQUIRE(worse_inv.has_value()); + BOOST_CHECK(worse_inv->hash == worse_hash); + BOOST_CHECK(blockprocessor.HasMineableCommitment(worse_hash)); + + const auto better_inv = blockprocessor.AddMineableCommitment(better); + BOOST_REQUIRE(better_inv.has_value()); + BOOST_CHECK(better_inv->hash == better_hash); + + CFinalCommitment found; + BOOST_CHECK(blockprocessor.GetMineableCommitmentByHash(better_hash, found)); + BOOST_CHECK(::SerializeHash(found) == better_hash); + BOOST_CHECK(!blockprocessor.HasMineableCommitment(worse_hash)); + BOOST_CHECK(!blockprocessor.GetMineableCommitmentByHash(worse_hash, found)); + + // Neither a worse nor an equally good commitment displaces the kept one, and neither is stored. + BOOST_CHECK(!blockprocessor.AddMineableCommitment(worse).has_value()); + BOOST_CHECK(!blockprocessor.AddMineableCommitment(tied).has_value()); + BOOST_CHECK(blockprocessor.HasMineableCommitment(better_hash)); + BOOST_CHECK(!blockprocessor.HasMineableCommitment(worse_hash)); + BOOST_CHECK(!blockprocessor.HasMineableCommitment(tied_hash)); +} + BOOST_AUTO_TEST_SUITE_END()