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
7 changes: 5 additions & 2 deletions src/llmq/quorums_instantsend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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. 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);
Expand Down
10 changes: 10 additions & 0 deletions src/llmq/quorums_instantsend.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class CInstantSendDb

class CInstantSendManager : public CRecoveredSigsListener
{
friend struct CInstantSendManagerTestAccess;

private:
CCriticalSection cs;
CInstantSendDb db;
Expand Down Expand Up @@ -120,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);
Expand Down
1 change: 1 addition & 0 deletions src/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
242 changes: 242 additions & 0 deletions src/test/llmq_instantsend_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
// 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 "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"
#include "validationinterface.h"

#include <boost/test/unit_test.hpp>

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;
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

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;
masternodeSync.Reset();
masternodeSync.SwitchToNextAsset(*connman);
masternodeSync.SwitchToNextAsset(*connman);
llmq::CInstantSendManagerTestAccess::SetEnabled(*llmq::quorumInstantSendManager, true);
RegisterValidationInterface(this);
}

/** Unsubscribe and restore the global masternode state. */
~InstantSendTestingSetup()
{
UnregisterValidationInterface(this);
llmq::CInstantSendManagerTestAccess::SetEnabled(*llmq::quorumInstantSendManager, false);
masternodeSync.Reset();
fMasternodeMode = previousMasternodeMode;
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;
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)
{
const CTransaction tx(MakeTestTransaction());
const uint256 txid = tx.GetHash();

auto& manager = *llmq::quorumInstantSendManager;
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);

// 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);
}

/** 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());
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());
}

/** 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());
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);
}

/** 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());
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);
}

/** 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;
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()
Loading