From f9e0d41f3ff6d2b827658b6946c2ce01b2ecdd76 Mon Sep 17 00:00:00 2001 From: Reuben Yap Date: Wed, 4 Nov 2026 16:40:22 +0800 Subject: [PATCH 1/5] LLMQ: untrack txs evicted by block conflicts MemPoolConflictRemovalTracker reports a tx that a connected block evicted from the mempool with SyncTransaction(tx, NULL, SYNC_TRANSACTION_NOT_IN_BLOCK). CInstantSendManager::SyncTransaction only recognised a conflict removal when pindex was set (the DisconnectTip emission), a regression inherited verbatim from Dash #2951 (bb83fc47a), so an evicted tx fell through to the mined-tx path instead: - ProcessTx(tx, allowReSigning=true) re-signed and re-announced the input lock shares of a tx whose inputs the block had just spent, and a member that had not voted yet voted for the dead tx and then rejected the mined tx as conflicting with its previous vote. - AddNonLockedTx tracked it with pindexMined unset, which nothing ever removes, so nonLockedTxs and nonLockedTxsByInputs grew until restart. Treat a tx that is not in a block and in neither the mempool nor the Dandelion stem pool as gone and route it to RemoveConflictedTx, which is a no-op for a tx that was never tracked. The stem pool check keeps stem-phase accepts, which are also reported without a block index, out of that path. Add a unit test for the accept/evict sequence. Co-Authored-By: Claude Fable 5.1 --- src/llmq/quorums_instantsend.cpp | 7 ++- src/llmq/quorums_instantsend.h | 2 + src/test/CMakeLists.txt | 1 + src/test/llmq_instantsend_tests.cpp | 83 +++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 src/test/llmq_instantsend_tests.cpp diff --git a/src/llmq/quorums_instantsend.cpp b/src/llmq/quorums_instantsend.cpp index f3b7d3696e..a68f29d23b 100644 --- a/src/llmq/quorums_instantsend.cpp +++ b/src/llmq/quorums_instantsend.cpp @@ -1027,10 +1027,13 @@ void CInstantSendManager::SyncTransaction(const CTransaction& tx_, const CBlockI bool inMempool = mempool.get(tx.GetHash()) != nullptr; bool isDisconnect = pindex && posInBlock == CMainSignals::SYNC_TRANSACTION_NOT_IN_BLOCK; - // Are we called from validation.cpp/MemPoolConflictRemovalTracker? + // Are we called from validation.cpp/MemPoolConflictRemovalTracker (pindex is null, the TX was evicted from the + // mempool by a conflicting mined TX) or from DisconnectTip for a TX which did not make it back into the mempool? + // Either way the TX is gone, so stop tracking it and forget the input votes we hold for it. A Dandelion stem-phase + // TX is not in the mempool yet, but it is not gone either. // TODO refactor this when we backport the BlockConnected signal from Bitcoin, as it gives better info about // conflicted TXs - bool isConflictRemoved = isDisconnect && !inMempool; + bool isConflictRemoved = !inMempool && posInBlock == CMainSignals::SYNC_TRANSACTION_NOT_IN_BLOCK && !txpools.getStemTxPool().exists(tx.GetHash()); if (isConflictRemoved) { LOCK(cs); diff --git a/src/llmq/quorums_instantsend.h b/src/llmq/quorums_instantsend.h index 2cc0722c7b..2fb579ac8e 100644 --- a/src/llmq/quorums_instantsend.h +++ b/src/llmq/quorums_instantsend.h @@ -74,6 +74,8 @@ class CInstantSendDb class CInstantSendManager : public CRecoveredSigsListener { + friend struct CInstantSendManagerTestAccess; + private: CCriticalSection cs; CInstantSendDb db; diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 2f2df8e9bf..6aff307468 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -54,6 +54,7 @@ add_executable(test_firo ${CMAKE_CURRENT_SOURCE_DIR}/dbwrapper_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/limitedmap_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/llmq_chainlock_cache_tests.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/llmq_instantsend_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/logging_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/llmq_dkgsessionhandler_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/llmq_signing_shares_tests.cpp diff --git a/src/test/llmq_instantsend_tests.cpp b/src/test/llmq_instantsend_tests.cpp new file mode 100644 index 0000000000..cac0cead17 --- /dev/null +++ b/src/test/llmq_instantsend_tests.cpp @@ -0,0 +1,83 @@ +// Copyright (c) 2026 The Firo Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include "llmq/quorums_instantsend.h" +#include "masternode-sync.h" +#include "test/test_bitcoin.h" +#include "txmempool.h" +#include "validation.h" +#include "validationinterface.h" + +#include + +namespace llmq +{ + +struct CInstantSendManagerTestAccess +{ + static void SetEnabled(CInstantSendManager& manager, bool enabled) + { + manager.isNewInstantSendEnabled = enabled; + } + + static bool IsTracked(CInstantSendManager& manager, const uint256& txid) + { + LOCK(manager.cs); + return manager.nonLockedTxs.count(txid) > 0; + } + + static size_t TrackedEntries(CInstantSendManager& manager) + { + LOCK(manager.cs); + return manager.nonLockedTxs.size() + manager.nonLockedTxsByInputs.size(); + } +}; + +} // namespace llmq + +BOOST_FIXTURE_TEST_SUITE(llmq_instantsend_tests, TestingSetup) + +// MemPoolConflictRemovalTracker reports a TX evicted from the mempool by a conflicting mined TX with a null block +// index. InstantSend must drop it instead of treating it like a mined TX, which left it in nonLockedTxs forever. +BOOST_AUTO_TEST_CASE(conflict_evicted_tx_is_untracked) +{ + CMutableTransaction mtx; + mtx.vin.resize(1); + mtx.vin[0].prevout = COutPoint(uint256S("1"), 0); + mtx.vin[0].scriptSig = CScript() << OP_11; + mtx.vout.resize(1); + mtx.vout[0].nValue = 1; + mtx.vout[0].scriptPubKey = CScript() << OP_TRUE; + const CTransaction tx(mtx); + const uint256 txid = tx.GetHash(); + + auto& manager = *llmq::quorumInstantSendManager; + llmq::CInstantSendManagerTestAccess::SetEnabled(manager, true); + masternodeSync.SwitchToNextAsset(*connman); + masternodeSync.SwitchToNextAsset(*connman); + BOOST_REQUIRE(masternodeSync.IsBlockchainSynced()); + + // Dandelion stem phase: not in the mempool yet, but not gone either + txpools.getStemTxPool().addUnchecked(txid, TestMemPoolEntryHelper().FromTx(tx)); + manager.SyncTransaction(tx, nullptr, CMainSignals::SYNC_TRANSACTION_NOT_IN_BLOCK); + BOOST_CHECK(llmq::CInstantSendManagerTestAccess::IsTracked(manager, txid)); + + // Fluffed into the mempool + mempool.addUnchecked(txid, TestMemPoolEntryHelper().FromTx(tx)); + manager.SyncTransaction(tx, nullptr, CMainSignals::SYNC_TRANSACTION_NOT_IN_BLOCK); + BOOST_CHECK(llmq::CInstantSendManagerTestAccess::IsTracked(manager, txid)); + + // Evicted by a conflicting mined TX: validation reports it with no block index once it is in neither pool + txpools.removeRecursive(tx, MemPoolRemovalReason::CONFLICT); + BOOST_REQUIRE(!mempool.exists(txid)); + BOOST_REQUIRE(!txpools.getStemTxPool().exists(txid)); + manager.SyncTransaction(tx, nullptr, CMainSignals::SYNC_TRANSACTION_NOT_IN_BLOCK); + BOOST_CHECK(!llmq::CInstantSendManagerTestAccess::IsTracked(manager, txid)); + BOOST_CHECK_EQUAL(llmq::CInstantSendManagerTestAccess::TrackedEntries(manager), 0U); + + masternodeSync.Reset(); + llmq::CInstantSendManagerTestAccess::SetEnabled(manager, false); +} + +BOOST_AUTO_TEST_SUITE_END() From a0f578399eb78c0be69ea6c05c5c7b3d510944f3 Mon Sep 17 00:00:00 2001 From: Reuben Yap Date: Wed, 4 Nov 2026 17:39:57 +0800 Subject: [PATCH 2/5] LLMQ: fix IS conflict-removal comment The comment above the conflict-removal branch claimed that RemoveConflictedTx forgets the input votes held for the tx. Its inputRequestIds erase hashes the whole CTxIn and so never matches the ids ProcessTx records from in.prevout. That mismatch is pre-existing (identical in Dash through v21) and left alone: making the erase effective would also stop a member that voted for the losing tx from acting on the mined winner's recovered input locks, which is a behaviour change for a separate PR. Co-Authored-By: Claude Fable 5.1 --- src/llmq/quorums_instantsend.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/llmq/quorums_instantsend.cpp b/src/llmq/quorums_instantsend.cpp index a68f29d23b..ec95e6a39a 100644 --- a/src/llmq/quorums_instantsend.cpp +++ b/src/llmq/quorums_instantsend.cpp @@ -1029,8 +1029,8 @@ void CInstantSendManager::SyncTransaction(const CTransaction& tx_, const CBlockI // Are we called from validation.cpp/MemPoolConflictRemovalTracker (pindex is null, the TX was evicted from the // mempool by a conflicting mined TX) or from DisconnectTip for a TX which did not make it back into the mempool? - // Either way the TX is gone, so stop tracking it and forget the input votes we hold for it. A Dandelion stem-phase - // TX is not in the mempool yet, but it is not gone either. + // Either way the TX is gone, so stop tracking it. A Dandelion stem-phase TX is not in the mempool yet, but it is + // not gone either. // TODO refactor this when we backport the BlockConnected signal from Bitcoin, as it gives better info about // conflicted TXs bool isConflictRemoved = !inMempool && posInBlock == CMainSignals::SYNC_TRANSACTION_NOT_IN_BLOCK && !txpools.getStemTxPool().exists(tx.GetHash()); From 7c843bf087c78772aa6e2c978da65a57a627ebcc Mon Sep 17 00:00:00 2001 From: Reuben Yap Date: Fri, 6 Nov 2026 01:43:50 +0800 Subject: [PATCH 3/5] Tests: cover InstantSend conflict notifications Connect a conflicting regtest block through validation notifications and check that the mined winner receives the input vote. Cover mined and disconnected transaction tracking, duplicate removals, and fixture cleanup. The five InstantSend cases pass with the fix and the #2019 notification gate applied. Reverting only the fix reproduces the leaked tracking and vote for the evicted transaction. Six LLMQ suites pass (15 cases). refs #2023 --- src/test/llmq_instantsend_tests.cpp | 170 +++++++++++++++++++++++++--- 1 file changed, 156 insertions(+), 14 deletions(-) diff --git a/src/test/llmq_instantsend_tests.cpp b/src/test/llmq_instantsend_tests.cpp index cac0cead17..51a1f46a36 100644 --- a/src/test/llmq_instantsend_tests.cpp +++ b/src/test/llmq_instantsend_tests.cpp @@ -2,8 +2,11 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include "activemasternode.h" +#include "keystore.h" #include "llmq/quorums_instantsend.h" #include "masternode-sync.h" +#include "script/sign.h" #include "test/test_bitcoin.h" #include "txmempool.h" #include "validation.h" @@ -32,30 +35,80 @@ struct CInstantSendManagerTestAccess LOCK(manager.cs); return manager.nonLockedTxs.size() + manager.nonLockedTxsByInputs.size(); } + + static const CBlockIndex* MinedBlock(CInstantSendManager& manager, const uint256& txid) + { + LOCK(manager.cs); + return manager.nonLockedTxs.at(txid).pindexMined; + } + + static void MarkParentLocked(CInstantSendManager& manager, const CTransaction& tx) + { + CInstantSendLock islock; + islock.txid = tx.GetHash(); + for (const auto& input : tx.vin) { + islock.inputs.push_back(input.prevout); + } + LOCK(manager.cs); + manager.db.WriteNewInstantSendLock(::SerializeHash(islock), islock); + } }; } // namespace llmq -BOOST_FIXTURE_TEST_SUITE(llmq_instantsend_tests, TestingSetup) +namespace +{ + +struct InstantSendTestingSetup : TestChain100Setup, CValidationInterface +{ + const bool previousMasternodeMode{fMasternodeMode}; + const uint256 previousProTxHash{activeMasternodeInfo.proTxHash}; + + InstantSendTestingSetup() + { + fMasternodeMode = false; + masternodeSync.Reset(); + masternodeSync.SwitchToNextAsset(*connman); + masternodeSync.SwitchToNextAsset(*connman); + llmq::CInstantSendManagerTestAccess::SetEnabled(*llmq::quorumInstantSendManager, true); + RegisterValidationInterface(this); + } + + ~InstantSendTestingSetup() + { + UnregisterValidationInterface(this); + llmq::CInstantSendManagerTestAccess::SetEnabled(*llmq::quorumInstantSendManager, false); + masternodeSync.Reset(); + fMasternodeMode = previousMasternodeMode; + activeMasternodeInfo.proTxHash = previousProTxHash; + } + + void SyncTransaction(const CTransaction& tx, const CBlockIndex* pindex, int posInBlock) override + { + llmq::quorumInstantSendManager->SyncTransaction(tx, pindex, posInBlock); + } +}; + +CMutableTransaction MakeTestTransaction(const COutPoint& prevout = COutPoint(uint256S("1"), 0)) +{ + CMutableTransaction tx; + tx.vin.emplace_back(prevout); + tx.vout.emplace_back(1, CScript() << OP_TRUE); + return tx; +} + +} // namespace + +BOOST_FIXTURE_TEST_SUITE(llmq_instantsend_tests, InstantSendTestingSetup) // MemPoolConflictRemovalTracker reports a TX evicted from the mempool by a conflicting mined TX with a null block // index. InstantSend must drop it instead of treating it like a mined TX, which left it in nonLockedTxs forever. BOOST_AUTO_TEST_CASE(conflict_evicted_tx_is_untracked) { - CMutableTransaction mtx; - mtx.vin.resize(1); - mtx.vin[0].prevout = COutPoint(uint256S("1"), 0); - mtx.vin[0].scriptSig = CScript() << OP_11; - mtx.vout.resize(1); - mtx.vout[0].nValue = 1; - mtx.vout[0].scriptPubKey = CScript() << OP_TRUE; - const CTransaction tx(mtx); + const CTransaction tx(MakeTestTransaction()); const uint256 txid = tx.GetHash(); auto& manager = *llmq::quorumInstantSendManager; - llmq::CInstantSendManagerTestAccess::SetEnabled(manager, true); - masternodeSync.SwitchToNextAsset(*connman); - masternodeSync.SwitchToNextAsset(*connman); BOOST_REQUIRE(masternodeSync.IsBlockchainSynced()); // Dandelion stem phase: not in the mempool yet, but not gone either @@ -76,8 +129,97 @@ BOOST_AUTO_TEST_CASE(conflict_evicted_tx_is_untracked) BOOST_CHECK(!llmq::CInstantSendManagerTestAccess::IsTracked(manager, txid)); BOOST_CHECK_EQUAL(llmq::CInstantSendManagerTestAccess::TrackedEntries(manager), 0U); - masternodeSync.Reset(); - llmq::CInstantSendManagerTestAccess::SetEnabled(manager, false); + // A duplicate removal notification must not recreate tracking entries. + manager.SyncTransaction(tx, nullptr, CMainSignals::SYNC_TRANSACTION_NOT_IN_BLOCK); + BOOST_CHECK_EQUAL(llmq::CInstantSendManagerTestAccess::TrackedEntries(manager), 0U); +} + +BOOST_AUTO_TEST_CASE(mined_tx_absent_from_pools_is_tracked) +{ + const CTransaction tx(MakeTestTransaction()); + auto& manager = *llmq::quorumInstantSendManager; + BOOST_REQUIRE(!mempool.exists(tx.GetHash())); + BOOST_REQUIRE(!txpools.getStemTxPool().exists(tx.GetHash())); + + manager.SyncTransaction(tx, chainActive.Tip(), 0); + BOOST_REQUIRE(llmq::CInstantSendManagerTestAccess::IsTracked(manager, tx.GetHash())); + BOOST_CHECK(llmq::CInstantSendManagerTestAccess::MinedBlock(manager, tx.GetHash()) == chainActive.Tip()); +} + +BOOST_AUTO_TEST_CASE(disconnected_tx_reaccepted_into_mempool_stays_tracked) +{ + const CTransaction tx(MakeTestTransaction()); + auto& manager = *llmq::quorumInstantSendManager; + const CBlockIndex* pindex = chainActive.Tip(); + manager.SyncTransaction(tx, pindex, 0); + BOOST_REQUIRE(llmq::CInstantSendManagerTestAccess::IsTracked(manager, tx.GetHash())); + + txpools.getStemTxPool().addUnchecked(tx.GetHash(), TestMemPoolEntryHelper().FromTx(tx)); + mempool.addUnchecked(tx.GetHash(), TestMemPoolEntryHelper().FromTx(tx)); + manager.SyncTransaction(tx, pindex->pprev, CMainSignals::SYNC_TRANSACTION_NOT_IN_BLOCK); + + BOOST_REQUIRE(llmq::CInstantSendManagerTestAccess::IsTracked(manager, tx.GetHash())); + BOOST_CHECK(llmq::CInstantSendManagerTestAccess::MinedBlock(manager, tx.GetHash()) == nullptr); +} + +BOOST_AUTO_TEST_CASE(disconnected_tx_not_reaccepted_is_untracked) +{ + const CTransaction tx(MakeTestTransaction()); + auto& manager = *llmq::quorumInstantSendManager; + const CBlockIndex* pindex = chainActive.Tip(); + manager.SyncTransaction(tx, pindex, 0); + BOOST_REQUIRE(llmq::CInstantSendManagerTestAccess::IsTracked(manager, tx.GetHash())); + + manager.SyncTransaction(tx, pindex->pprev, CMainSignals::SYNC_TRANSACTION_NOT_IN_BLOCK); + BOOST_CHECK(!llmq::CInstantSendManagerTestAccess::IsTracked(manager, tx.GetHash())); + BOOST_CHECK_EQUAL(llmq::CInstantSendManagerTestAccess::TrackedEntries(manager), 0U); +} + +BOOST_AUTO_TEST_CASE(block_conflict_removal_does_not_vote_for_evicted_tx) +{ + auto& manager = *llmq::quorumInstantSendManager; + CMutableTransaction parent = MakeTestTransaction(COutPoint(coinbaseTxns[0].GetHash(), 0)); + parent.vout[0].nValue = coinbaseTxns[0].vout[0].nValue - COIN; + CBasicKeyStore keystore; + keystore.AddKey(coinbaseKey); + BOOST_REQUIRE(SignSignature(keystore, coinbaseTxns[0], parent, 0, SIGHASH_ALL)); + bool accepted = false; + const CBlock parentBlock = CreateAndProcessBlock({parent}, coinbaseKey, &accepted); + BOOST_REQUIRE(accepted); + BOOST_REQUIRE(chainActive.Tip()->GetBlockHash() == parentBlock.GetHash()); + + // Seed a parent lock so input eligibility does not depend on txindex or a live quorum. + llmq::CInstantSendManagerTestAccess::MarkParentLocked(manager, CTransaction(parent)); + CMutableTransaction loser = MakeTestTransaction(COutPoint(parent.GetHash(), 0)); + loser.vout[0].nValue = parent.vout[0].nValue - 1000; + CMutableTransaction winner(loser); + --winner.vout[0].nValue; + const CTransaction losingTx(loser); + const uint256 losingTxid = losingTx.GetHash(); + txpools.getStemTxPool().addUnchecked(losingTxid, TestMemPoolEntryHelper().Fee(1000).FromTx(losingTx)); + mempool.addUnchecked(losingTxid, TestMemPoolEntryHelper().Fee(1000).FromTx(losingTx)); + GetMainSignals().SyncTransaction(losingTx, nullptr, CMainSignals::SYNC_TRANSACTION_NOT_IN_BLOCK); + BOOST_REQUIRE(llmq::CInstantSendManagerTestAccess::IsTracked(manager, losingTxid)); + + // No quorum is needed: AsyncSignIfMember records the vote before selecting one. + // Turn on voting only after admission, so any vote for the loser comes from block eviction. + fMasternodeMode = true; + activeMasternodeInfo.proTxHash = uint256S("2"); + const auto llmqType = Params().GetConsensus().llmqForInstantSend; + const uint256 inputId = ::SerializeHash(std::make_pair(std::string("inlock"), losingTx.vin[0].prevout)); + uint256 votedTxid; + BOOST_REQUIRE(!llmq::quorumSigningManager->GetVoteForId(llmqType, inputId, votedTxid)); + + const CBlock block = CreateAndProcessBlock({winner}, coinbaseKey, &accepted); + BOOST_REQUIRE(accepted); + BOOST_REQUIRE(chainActive.Tip()->GetBlockHash() == block.GetHash()); + BOOST_CHECK(!mempool.exists(losingTxid)); + BOOST_CHECK(!txpools.getStemTxPool().exists(losingTxid)); + BOOST_CHECK(!llmq::CInstantSendManagerTestAccess::IsTracked(manager, losingTxid)); + BOOST_REQUIRE(llmq::quorumSigningManager->GetVoteForId(llmqType, inputId, votedTxid)); + BOOST_CHECK(votedTxid == winner.GetHash()); + BOOST_REQUIRE(llmq::CInstantSendManagerTestAccess::IsTracked(manager, winner.GetHash())); + BOOST_CHECK(llmq::CInstantSendManagerTestAccess::MinedBlock(manager, winner.GetHash()) == chainActive.Tip()); } BOOST_AUTO_TEST_SUITE_END() From b1367ba3482c58272f9d4af86a0b7f11d4281184 Mon Sep 17 00:00:00 2001 From: Reuben Yap Date: Fri, 6 Nov 2026 02:01:08 +0800 Subject: [PATCH 4/5] Tests: document InstantSend test helpers Add Doxygen comments to the test-access helpers, the fixture and the test cases in llmq_instantsend_tests.cpp. CodeRabbit's docstring coverage check on #2023 flagged them as undocumented. Co-Authored-By: Claude Fable 5.1 --- src/test/llmq_instantsend_tests.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/test/llmq_instantsend_tests.cpp b/src/test/llmq_instantsend_tests.cpp index 51a1f46a36..113d9ea4ee 100644 --- a/src/test/llmq_instantsend_tests.cpp +++ b/src/test/llmq_instantsend_tests.cpp @@ -17,31 +17,37 @@ namespace llmq { +/** Friend of CInstantSendManager: reaches the private state the tests set up and inspect. */ struct CInstantSendManagerTestAccess { + //! Flip the private InstantSend-enabled flag without a spork. static void SetEnabled(CInstantSendManager& manager, bool enabled) { manager.isNewInstantSendEnabled = enabled; } + //! Whether txid has a nonLockedTxs entry. static bool IsTracked(CInstantSendManager& manager, const uint256& txid) { LOCK(manager.cs); return manager.nonLockedTxs.count(txid) > 0; } + //! Entries across nonLockedTxs and nonLockedTxsByInputs; 0 means nothing leaked. static size_t TrackedEntries(CInstantSendManager& manager) { LOCK(manager.cs); return manager.nonLockedTxs.size() + manager.nonLockedTxsByInputs.size(); } + //! Block index recorded for a tracked txid, nullptr while it is unmined. static const CBlockIndex* MinedBlock(CInstantSendManager& manager, const uint256& txid) { LOCK(manager.cs); return manager.nonLockedTxs.at(txid).pindexMined; } + //! Store an islock for tx so CheckCanLock accepts spends of its outputs without txindex or confirmations. static void MarkParentLocked(CInstantSendManager& manager, const CTransaction& tx) { CInstantSendLock islock; @@ -59,11 +65,14 @@ struct CInstantSendManagerTestAccess namespace { +/** 100-block regtest chain with InstantSend enabled and the masternode sync marked finished; validation signals + * are forwarded to InstantSend as firod does. */ struct InstantSendTestingSetup : TestChain100Setup, CValidationInterface { const bool previousMasternodeMode{fMasternodeMode}; const uint256 previousProTxHash{activeMasternodeInfo.proTxHash}; + //! Enable InstantSend as a non-masternode and subscribe to validation signals. InstantSendTestingSetup() { fMasternodeMode = false; @@ -74,6 +83,7 @@ struct InstantSendTestingSetup : TestChain100Setup, CValidationInterface RegisterValidationInterface(this); } + //! Unsubscribe and restore the global masternode state. ~InstantSendTestingSetup() { UnregisterValidationInterface(this); @@ -83,12 +93,14 @@ struct InstantSendTestingSetup : TestChain100Setup, CValidationInterface activeMasternodeInfo.proTxHash = previousProTxHash; } + //! Feed InstantSend from validation, as CDSNotificationInterface does in firod. void SyncTransaction(const CTransaction& tx, const CBlockIndex* pindex, int posInBlock) override { llmq::quorumInstantSendManager->SyncTransaction(tx, pindex, posInBlock); } }; +/** One input and one anyone-can-spend (OP_TRUE) output, so a child needs no signature. */ CMutableTransaction MakeTestTransaction(const COutPoint& prevout = COutPoint(uint256S("1"), 0)) { CMutableTransaction tx; @@ -101,8 +113,8 @@ CMutableTransaction MakeTestTransaction(const COutPoint& prevout = COutPoint(uin BOOST_FIXTURE_TEST_SUITE(llmq_instantsend_tests, InstantSendTestingSetup) -// MemPoolConflictRemovalTracker reports a TX evicted from the mempool by a conflicting mined TX with a null block -// index. InstantSend must drop it instead of treating it like a mined TX, which left it in nonLockedTxs forever. +/** MemPoolConflictRemovalTracker reports a TX evicted from the mempool by a conflicting mined TX with a null block + * index. InstantSend must drop it instead of treating it like a mined TX, which left it in nonLockedTxs forever. */ BOOST_AUTO_TEST_CASE(conflict_evicted_tx_is_untracked) { const CTransaction tx(MakeTestTransaction()); @@ -134,6 +146,7 @@ BOOST_AUTO_TEST_CASE(conflict_evicted_tx_is_untracked) BOOST_CHECK_EQUAL(llmq::CInstantSendManagerTestAccess::TrackedEntries(manager), 0U); } +/** A TX reported with a block index was mined, not evicted, even though it is in neither pool. */ BOOST_AUTO_TEST_CASE(mined_tx_absent_from_pools_is_tracked) { const CTransaction tx(MakeTestTransaction()); @@ -146,6 +159,7 @@ BOOST_AUTO_TEST_CASE(mined_tx_absent_from_pools_is_tracked) BOOST_CHECK(llmq::CInstantSendManagerTestAccess::MinedBlock(manager, tx.GetHash()) == chainActive.Tip()); } +/** DisconnectTip put the TX back into both pools and reports it with pprev: it stays tracked as unmined. */ BOOST_AUTO_TEST_CASE(disconnected_tx_reaccepted_into_mempool_stays_tracked) { const CTransaction tx(MakeTestTransaction()); @@ -162,6 +176,7 @@ BOOST_AUTO_TEST_CASE(disconnected_tx_reaccepted_into_mempool_stays_tracked) BOOST_CHECK(llmq::CInstantSendManagerTestAccess::MinedBlock(manager, tx.GetHash()) == nullptr); } +/** DisconnectTip could not put the TX back into the pools: it is gone and must be untracked. */ BOOST_AUTO_TEST_CASE(disconnected_tx_not_reaccepted_is_untracked) { const CTransaction tx(MakeTestTransaction()); @@ -175,6 +190,8 @@ BOOST_AUTO_TEST_CASE(disconnected_tx_not_reaccepted_is_untracked) BOOST_CHECK_EQUAL(llmq::CInstantSendManagerTestAccess::TrackedEntries(manager), 0U); } +/** Through validation: mining a conflicting TX evicts the loser, which must be untracked and must not take the + * input vote from the mined winner. */ BOOST_AUTO_TEST_CASE(block_conflict_removal_does_not_vote_for_evicted_tx) { auto& manager = *llmq::quorumInstantSendManager; From c42aa284cd961f586deef41b3b4f446b957ee28d Mon Sep 17 00:00:00 2001 From: Reuben Yap Date: Fri, 6 Nov 2026 02:12:51 +0800 Subject: [PATCH 5/5] LLMQ: document IS SyncTransaction contract Describe in the header which (pindex, posInBlock) shapes validation sends and what each one means, since the fix hinges on them. Switch the test helpers' //! comments to /** */ blocks, the style the header and the other test files use. Both are comment-only changes; CodeRabbit's docstring coverage check still flagged the PR after b1367ba34. Co-Authored-By: Claude Fable 5.1 --- src/llmq/quorums_instantsend.h | 8 ++++++++ src/test/llmq_instantsend_tests.cpp | 16 ++++++++-------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/llmq/quorums_instantsend.h b/src/llmq/quorums_instantsend.h index 2fb579ac8e..44bbe853a0 100644 --- a/src/llmq/quorums_instantsend.h +++ b/src/llmq/quorums_instantsend.h @@ -122,6 +122,14 @@ class CInstantSendManager : public CRecoveredSigsListener void Stop(); void InterruptWorkerThread(); + /** + * Called through CDSNotificationInterface for every validation SyncTransaction signal. The arguments say why: + * pindex set and posInBlock >= 0: tx was mined in that block; + * pindex set and posInBlock == CMainSignals::SYNC_TRANSACTION_NOT_IN_BLOCK: its block was disconnected, pindex is + * the new tip, and tx is back in the pools unless it could not be re-accepted; + * pindex null: tx was accepted into a pool, or evicted from the mempool by a conflicting mined TX. The two are told + * apart by whether tx is still in a pool. + */ void SyncTransaction(const CTransaction& tx, const CBlockIndex *pindex, int posInBlock); bool IsLocked(const uint256& txHash); diff --git a/src/test/llmq_instantsend_tests.cpp b/src/test/llmq_instantsend_tests.cpp index 113d9ea4ee..f7845395a2 100644 --- a/src/test/llmq_instantsend_tests.cpp +++ b/src/test/llmq_instantsend_tests.cpp @@ -20,34 +20,34 @@ namespace llmq /** Friend of CInstantSendManager: reaches the private state the tests set up and inspect. */ struct CInstantSendManagerTestAccess { - //! Flip the private InstantSend-enabled flag without a spork. + /** Flip the private InstantSend-enabled flag without a spork. */ static void SetEnabled(CInstantSendManager& manager, bool enabled) { manager.isNewInstantSendEnabled = enabled; } - //! Whether txid has a nonLockedTxs entry. + /** Whether txid has a nonLockedTxs entry. */ static bool IsTracked(CInstantSendManager& manager, const uint256& txid) { LOCK(manager.cs); return manager.nonLockedTxs.count(txid) > 0; } - //! Entries across nonLockedTxs and nonLockedTxsByInputs; 0 means nothing leaked. + /** Entries across nonLockedTxs and nonLockedTxsByInputs; 0 means nothing leaked. */ static size_t TrackedEntries(CInstantSendManager& manager) { LOCK(manager.cs); return manager.nonLockedTxs.size() + manager.nonLockedTxsByInputs.size(); } - //! Block index recorded for a tracked txid, nullptr while it is unmined. + /** Block index recorded for a tracked txid, nullptr while it is unmined. */ static const CBlockIndex* MinedBlock(CInstantSendManager& manager, const uint256& txid) { LOCK(manager.cs); return manager.nonLockedTxs.at(txid).pindexMined; } - //! Store an islock for tx so CheckCanLock accepts spends of its outputs without txindex or confirmations. + /** Store an islock for tx so CheckCanLock accepts spends of its outputs without txindex or confirmations. */ static void MarkParentLocked(CInstantSendManager& manager, const CTransaction& tx) { CInstantSendLock islock; @@ -72,7 +72,7 @@ struct InstantSendTestingSetup : TestChain100Setup, CValidationInterface const bool previousMasternodeMode{fMasternodeMode}; const uint256 previousProTxHash{activeMasternodeInfo.proTxHash}; - //! Enable InstantSend as a non-masternode and subscribe to validation signals. + /** Enable InstantSend as a non-masternode and subscribe to validation signals. */ InstantSendTestingSetup() { fMasternodeMode = false; @@ -83,7 +83,7 @@ struct InstantSendTestingSetup : TestChain100Setup, CValidationInterface RegisterValidationInterface(this); } - //! Unsubscribe and restore the global masternode state. + /** Unsubscribe and restore the global masternode state. */ ~InstantSendTestingSetup() { UnregisterValidationInterface(this); @@ -93,7 +93,7 @@ struct InstantSendTestingSetup : TestChain100Setup, CValidationInterface activeMasternodeInfo.proTxHash = previousProTxHash; } - //! Feed InstantSend from validation, as CDSNotificationInterface does in firod. + /** Feed InstantSend from validation, as CDSNotificationInterface does in firod. */ void SyncTransaction(const CTransaction& tx, const CBlockIndex* pindex, int posInBlock) override { llmq::quorumInstantSendManager->SyncTransaction(tx, pindex, posInBlock);