Skip to content
Open
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
27 changes: 16 additions & 11 deletions src/test/evo_deterministicmns_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1338,7 +1338,7 @@ void FuncTestMempoolReorg(TestChainSetup& setup)
// Check mempool as if a new block with tx_reg_ds was connected instead of the old one with tx_reg
std::vector<CTransactionRef> block_reorg;
block_reorg.emplace_back(std::make_shared<CTransaction>(tx_reg_ds));
testPool.removeForBlock(block_reorg, nHeight + 2);
testPool.removeForBlock(block_reorg, nHeight + 2, chainman.ActiveChain().Tip());
BOOST_CHECK_EQUAL(testPool.size(), 0U);
}

Expand Down Expand Up @@ -1413,7 +1413,7 @@ void FuncTestMempoolProTxKeyChangedConflictChain(TestChainSetup& setup)

// Pre-fix this aborted the process instead of returning.
std::vector<CTransactionRef> block_txs{std::make_shared<CTransaction>(tx_revoke)};
testPool.removeForBlock(block_txs, chainman.ActiveChain().Height() + 1);
testPool.removeForBlock(block_txs, chainman.ActiveChain().Height() + 1, chainman.ActiveChain().Tip());
BOOST_CHECK_EQUAL(testPool.size(), 0U);
}

Expand Down Expand Up @@ -1528,11 +1528,11 @@ void FuncTestMempoolDualProregtx(TestChainSetup& setup)
BOOST_CHECK(testPool.existsProviderTxConflict(CTransaction(tx_reg1)));

// A confirmed claim evicts the pending claim of the same value in the other field
testPool.removeForBlock({MakeTransactionRef(tx_reg1)}, chainman.ActiveChain().Height() + 1);
testPool.removeForBlock({MakeTransactionRef(tx_reg1)}, chainman.ActiveChain().Height() + 1, chainman.ActiveChain().Tip());
BOOST_CHECK(!testPool.exists(tx_platform_reg.GetHash()));

testPool.addUnchecked(entry.FromTx(tx_reg1));
testPool.removeForBlock({MakeTransactionRef(tx_platform_reg)}, chainman.ActiveChain().Height() + 1);
testPool.removeForBlock({MakeTransactionRef(tx_platform_reg)}, chainman.ActiveChain().Height() + 1, chainman.ActiveChain().Tip());
BOOST_CHECK(!testPool.exists(tx_reg1.GetHash()));
BOOST_CHECK_EQUAL(testPool.size(), 0U);

Expand All @@ -1554,7 +1554,7 @@ void FuncTestMempoolDualProregtx(TestChainSetup& setup)
testPool.addUnchecked(entry.FromTx(MakeTransactionRef(tx_evo)));
BOOST_CHECK(testPool.existsProviderTxConflict(tx_shared_on_evo));
BOOST_CHECK(!testPool.existsProviderTxConflict(make_shared(uint160{Span{GetRandHash()}.first(uint160::size())})));
testPool.removeForBlock({MakeTransactionRef(tx_shared_on_evo)}, chainman.ActiveChain().Height() + 1);
testPool.removeForBlock({MakeTransactionRef(tx_shared_on_evo)}, chainman.ActiveChain().Height() + 1, chainman.ActiveChain().Tip());
BOOST_CHECK(!testPool.exists(tx_evo.GetHash()));
BOOST_CHECK_EQUAL(testPool.size(), 0U);

Expand All @@ -1572,7 +1572,7 @@ void FuncTestMempoolDualProregtx(TestChainSetup& setup)
testPool.addUnchecked(entry.FromTx(MakeTransactionRef(tx_shared)));
BOOST_CHECK(testPool.existsProviderTxConflict(tx_evo_on_share));
BOOST_CHECK(!testPool.existsProviderTxConflict(make_evo()));
testPool.removeForBlock({MakeTransactionRef(tx_evo_on_share)}, chainman.ActiveChain().Height() + 1);
testPool.removeForBlock({MakeTransactionRef(tx_evo_on_share)}, chainman.ActiveChain().Height() + 1, chainman.ActiveChain().Tip());
BOOST_CHECK(!testPool.exists(tx_shared.GetHash()));
BOOST_CHECK_EQUAL(testPool.size(), 0U);

Expand All @@ -1584,7 +1584,7 @@ void FuncTestMempoolDualProregtx(TestChainSetup& setup)
const auto tx_same_value = with_platform_node_id(tx_evo_base, uint160{Assert(GetTxPayload<CProRegTx>(tx_evo_base))->keyIDOwner});
BOOST_CHECK(!testPool.existsProviderTxConflict(tx_same_value));
testPool.addUnchecked(entry.FromTx(MakeTransactionRef(tx_same_value)));
testPool.removeForBlock({MakeTransactionRef(tx_same_value)}, chainman.ActiveChain().Height() + 1);
testPool.removeForBlock({MakeTransactionRef(tx_same_value)}, chainman.ActiveChain().Height() + 1, chainman.ActiveChain().Tip());
BOOST_CHECK(testPool.exists(tx_pending.GetHash()));
}

Expand Down Expand Up @@ -1685,12 +1685,17 @@ void FuncTestMempoolProRegReplacementUpdateConflict(TestChainSetup& setup)
BOOST_CHECK_EQUAL(testPool.size(), 1U);
BOOST_CHECK(testPool.existsProviderTxConflict(CTransaction(tx_reg_replace)));

// A reorg re-mining the MN's own ProRegTx connects it on the block before the registration,
// whose list has no MN on that collateral yet: nothing is replaced and the update stays.
testPool.removeForBlock({MakeTransactionRef(tx_reg)}, tip_height(), chainman.ActiveChain().Tip()->pprev);
BOOST_CHECK(testPool.exists(tx_up_serv.GetHash()));

// existsProviderTxConflict only gates our own acceptance; it cannot stop a miner from
// confirming the replacement. Once that block arrives, removeForBlock must evict the
// now-unmineable update, otherwise it lingers and stalls our own block assembly.
std::vector<CTransactionRef> connected{MakeTransactionRef(CMutableTransaction()),
MakeTransactionRef(tx_reg_replace)};
testPool.removeForBlock(connected, tip_height() + 1);
testPool.removeForBlock(connected, tip_height() + 1, chainman.ActiveChain().Tip());
BOOST_CHECK_EQUAL(testPool.size(), 0U);
}

Expand All @@ -1716,18 +1721,18 @@ void FuncTestMempoolProRegReplacementUpdateConflict(TestChainSetup& setup)
// confirmed ProUpReg that keeps it, and across a spend of anything but the collateral.
testPool.addUnchecked(entry.FromTx(tx_up_serv));
testPool.addUnchecked(entry.FromTx(tx_up_rev));
testPool.removeForBlock({MakeTransactionRef(tx_up_reg), MakeTransactionRef(tx_spend_other)}, tip_height() + 1);
testPool.removeForBlock({MakeTransactionRef(tx_up_reg), MakeTransactionRef(tx_spend_other)}, tip_height() + 1, chainman.ActiveChain().Tip());
BOOST_CHECK_EQUAL(testPool.size(), 2U);

testPool.removeForBlock({MakeTransactionRef(tx_up_reg_new_key)}, tip_height() + 1);
testPool.removeForBlock({MakeTransactionRef(tx_up_reg_new_key)}, tip_height() + 1, chainman.ActiveChain().Tip());
BOOST_CHECK_EQUAL(testPool.size(), 0U);

// Spending the collateral removes the MN, and no update of any kind can be mined after that.
testPool.addUnchecked(entry.FromTx(tx_up_serv));
testPool.addUnchecked(entry.FromTx(tx_up_reg));
testPool.addUnchecked(entry.FromTx(tx_up_rev));
BOOST_CHECK_EQUAL(testPool.size(), 3U);
testPool.removeForBlock({MakeTransactionRef(tx_spend_collateral)}, tip_height() + 1);
testPool.removeForBlock({MakeTransactionRef(tx_spend_collateral)}, tip_height() + 1, chainman.ActiveChain().Tip());
BOOST_CHECK_EQUAL(testPool.size(), 0U);
Comment on lines +1735 to 1736

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🟡 Suggestion: Cover collateral-spend eviction with a stale parent list

This collateral-spend check passes chainman.ActiveChain().Tip() as pindex_prev, so the new historical lookup selects the same list as the old tip-based implementation. It therefore cannot fail if the collateral-spend path is accidentally reverted to GetListAtChainTip(). Add a separate case where the active tip contains the masternode but pindex_prev is an ancestor whose list does not, assert that the spend preserves the pending updates under that historical parent, and retain the existing active-tip case to verify that a real collateral spend still evicts them.

source: gpt-6.1-sol (phase2-reviewer: general, dash-core-commit-history)

}
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/mempool_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ BOOST_AUTO_TEST_CASE(MempoolAncestorIndexingTest)
/* after tx6 is mined, tx7 should move up in the sort */
std::vector<CTransactionRef> vtx;
vtx.push_back(MakeTransactionRef(tx6));
pool.removeForBlock(vtx, 1);
pool.removeForBlock(vtx, 1, /*pindex_prev=*/nullptr);

sortedOrder.erase(sortedOrder.begin()+1);
// Ties are broken by hash
Expand Down Expand Up @@ -557,7 +557,7 @@ BOOST_AUTO_TEST_CASE(MempoolSizeLimitTest)
SetMockTime(42 + CTxMemPool::ROLLING_FEE_HALFLIFE);
BOOST_CHECK_EQUAL(pool.GetMinFee(1).GetFeePerK(), maxFeeRateRemoved.GetFeePerK() + 1000);
// ... we should keep the same min fee until we get a block
pool.removeForBlock(vtx, 1);
pool.removeForBlock(vtx, 1, /*pindex_prev=*/nullptr);
SetMockTime(42 + 2*CTxMemPool::ROLLING_FEE_HALFLIFE);
BOOST_CHECK_EQUAL(pool.GetMinFee(1).GetFeePerK(), llround((maxFeeRateRemoved.GetFeePerK() + 1000)/2.0));
// ... then feerate should drop 1/2 each halflife
Expand Down
10 changes: 5 additions & 5 deletions src/test/policyestimator_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ BOOST_AUTO_TEST_CASE(BlockPolicyEstimates)
txHashes[9-h].pop_back();
}
}
mpool.removeForBlock(block, ++blocknum);
mpool.removeForBlock(block, ++blocknum, /*pindex_prev=*/nullptr);
block.clear();
// Check after just a few txs that combining buckets works as expected
if (blocknum == 3) {
Expand Down Expand Up @@ -115,7 +115,7 @@ BOOST_AUTO_TEST_CASE(BlockPolicyEstimates)
// Mine 50 more blocks with no transactions happening, estimates shouldn't change
// We haven't decayed the moving average enough so we still have enough data points in every bucket
while (blocknum < 250)
mpool.removeForBlock(block, ++blocknum);
mpool.removeForBlock(block, ++blocknum, /*pindex_prev=*/nullptr);

BOOST_CHECK(feeEst.estimateFee(1) == CFeeRate(0));
for (int i = 2; i < 10;i++) {
Expand All @@ -135,7 +135,7 @@ BOOST_AUTO_TEST_CASE(BlockPolicyEstimates)
txHashes[j].push_back(hash);
}
}
mpool.removeForBlock(block, ++blocknum);
mpool.removeForBlock(block, ++blocknum, /*pindex_prev=*/nullptr);
}

for (int i = 1; i < 10;i++) {
Expand All @@ -152,7 +152,7 @@ BOOST_AUTO_TEST_CASE(BlockPolicyEstimates)
txHashes[j].pop_back();
}
}
mpool.removeForBlock(block, 266);
mpool.removeForBlock(block, 266, /*pindex_prev=*/nullptr);
block.clear();
BOOST_CHECK(feeEst.estimateFee(1) == CFeeRate(0));
for (int i = 2; i < 10;i++) {
Expand All @@ -173,7 +173,7 @@ BOOST_AUTO_TEST_CASE(BlockPolicyEstimates)

}
}
mpool.removeForBlock(block, ++blocknum);
mpool.removeForBlock(block, ++blocknum, /*pindex_prev=*/nullptr);
block.clear();
}
BOOST_CHECK(feeEst.estimateFee(1) == CFeeRate(0));
Expand Down
16 changes: 9 additions & 7 deletions src/txmempool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1077,10 +1077,9 @@ void CTxMemPool::removeProTxReferences(const uint256& proTxHash)
}
}

void CTxMemPool::removeProTxSpentCollateralConflicts(const CTransaction &tx)
void CTxMemPool::removeProTxSpentCollateralConflicts(const CTransaction &tx, const CDeterministicMNList& mnList)
{
// Remove TXs that refer to a MN for which the collateral was spent
auto mnList = m_dmnman.GetListAtChainTip();
for (const auto& in : tx.vin) {
auto collateralIt = mapProTxCollaterals.find(in.prevout);
if (collateralIt != mapProTxCollaterals.end()) {
Expand Down Expand Up @@ -1223,9 +1222,12 @@ void CTxMemPool::removeProTxVotingPayeeConflicts(const uint256& proTxHash, const
}
}

void CTxMemPool::removeProTxConflicts(const CTransaction &tx)
void CTxMemPool::removeProTxConflicts(const CTransaction &tx, const CBlockIndex* pindex_prev)
{
removeProTxSpentCollateralConflicts(tx);
// The list tx is applied to, not the list at the chain tip: while a reorg is connecting its
// new branch, the tip list is still the one of the disconnected branch.
const CDeterministicMNList prev_mn_list{pindex_prev ? m_dmnman.GetListForBlock(pindex_prev) : CDeterministicMNList{}};

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🟡 Suggestion: Cache the historical masternode list for the whole block

removeProTxConflicts() obtains GetListForBlock(pindex_prev) for every transaction processed by removeForBlock(), including ordinary transactions. GetListForBlock() can rebuild historical state by applying diffs and each call copies the resulting list, so a block with many transactions repeats work that is identical for every transaction. Resolve the parent list once in removeForBlock() and pass it to the conflict helper, or defer a single lookup until it is needed, while preserving the empty-list behavior for a null parent.

source: muse-spark-1.3-contributor (phase1-reviewer: general)

removeProTxSpentCollateralConflicts(tx, prev_mn_list);

const uint256 tx_hash{tx.GetHash()};
if (tx.nType == TRANSACTION_PROVIDER_REGISTER) {
Expand Down Expand Up @@ -1261,7 +1263,7 @@ void CTxMemPool::removeProTxConflicts(const CTransaction &tx)
// currently backs, so that MN ceases to exist. Drop any mempool update that
// still targets its proTxHash; such an update can never be mined afterwards.
// removeProTxSpentCollateralConflicts only covers collateral *spends*, not reuse.
if (auto dmn = m_dmnman.GetListAtChainTip().GetMNByCollateral(proTx.collateralOutpoint)) {
if (auto dmn = prev_mn_list.GetMNByCollateral(proTx.collateralOutpoint)) {
removeProTxReferences(dmn->proTxHash);
}
} else {
Expand Down Expand Up @@ -1332,7 +1334,7 @@ void CTxMemPool::removeProTxConflicts(const CTransaction &tx)
/**
* Called when a block is connected. Removes from mempool and updates the miner fee estimator.
*/
void CTxMemPool::removeForBlock(const std::vector<CTransactionRef>& vtx, unsigned int nBlockHeight)
void CTxMemPool::removeForBlock(const std::vector<CTransactionRef>& vtx, unsigned int nBlockHeight, const CBlockIndex* pindex_prev)
{
AssertLockHeld(cs);
std::vector<const CTxMemPoolEntry*> entries;
Expand All @@ -1355,7 +1357,7 @@ void CTxMemPool::removeForBlock(const std::vector<CTransactionRef>& vtx, unsigne
RemoveStaged(stage, true, MemPoolRemovalReason::BLOCK);
}
removeConflicts(*tx);
removeProTxConflicts(*tx);
removeProTxConflicts(*tx, pindex_prev);
removeAssetUnlockConflicts(*tx);
ClearPrioritisation(tx->GetHash());
}
Expand Down
10 changes: 7 additions & 3 deletions src/txmempool.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ struct descendant_score {};
struct entry_time {};
struct ancestor_score {};

class CBlockIndex;
class CBlockPolicyEstimator;
class CDeterministicMNList;
class CDeterministicMNManager;

namespace llmq {
Expand Down Expand Up @@ -518,13 +520,15 @@ class CTxMemPool
* (collateral spent, or collateral reused by a replacement ProRegTx), since such TXs can never
* be mined afterwards and would abort block assembly with "bad-protx-hash". */
void removeProTxReferences(const uint256& proTxHash) EXCLUSIVE_LOCKS_REQUIRED(cs);
void removeProTxSpentCollateralConflicts(const CTransaction &tx) EXCLUSIVE_LOCKS_REQUIRED(cs);
void removeProTxSpentCollateralConflicts(const CTransaction &tx, const CDeterministicMNList& mnList) EXCLUSIVE_LOCKS_REQUIRED(cs);
void removeProTxKeyChangedConflicts(const CTransaction &tx, const uint256& proTxHash, const uint256& newKeyHash) EXCLUSIVE_LOCKS_REQUIRED(cs);
/** Remove pending TXs of refType for proTxHash that pair the P2PKH destination of keyIDVoting with a
* share reward script: ProUpShareTxs paying it, or ProUpSharedRegTxs setting it as the voting key. */
void removeProTxVotingPayeeConflicts(const uint256& proTxHash, const CKeyID& keyIDVoting, uint16_t refType) EXCLUSIVE_LOCKS_REQUIRED(cs);
void removeProTxConflicts(const CTransaction &tx) EXCLUSIVE_LOCKS_REQUIRED(cs);
void removeForBlock(const std::vector<CTransactionRef>& vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs);
/** @param[in] pindex_prev Block that tx is mined on top of; nullptr for the genesis block.
* Its masternode list decides which masternodes tx removes. */
void removeProTxConflicts(const CTransaction &tx, const CBlockIndex* pindex_prev) EXCLUSIVE_LOCKS_REQUIRED(cs);
void removeForBlock(const std::vector<CTransactionRef>& vtx, unsigned int nBlockHeight, const CBlockIndex* pindex_prev) EXCLUSIVE_LOCKS_REQUIRED(cs);
void removeExpiredAssetUnlock(int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs);
/** If entry changes or revokes the operator key a masternode has at the chain tip, return that
* masternode. Decided against the current list rather than isKeyChangeProTx, which records the list
Expand Down
4 changes: 2 additions & 2 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ bool MemPoolAccept::PreChecks(ATMPArgs& args, Workspace& ws)

if (m_isman && m_isman->IsWaitingForTx(hash)) {
m_pool.removeConflicts(tx);
m_pool.removeProTxConflicts(tx);
m_pool.removeProTxConflicts(tx, m_active_chainstate.m_chain.Tip());
m_pool.removeAssetUnlockConflicts(tx);
} else {
// Check for conflicts with in-memory transactions
Expand Down Expand Up @@ -3660,7 +3660,7 @@ bool Chainstate::ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew,
Ticks<MillisecondsDouble>(time_chainstate) / num_blocks_total);
// Remove conflicting transactions from the mempool.;
if (m_mempool) {
m_mempool->removeForBlock(blockConnecting.vtx, pindexNew->nHeight);
m_mempool->removeForBlock(blockConnecting.vtx, pindexNew->nHeight, pindexNew->pprev);
m_mempool->removeExpiredAssetUnlock(pindexNew->nHeight);
disconnectpool.removeForBlock(blockConnecting.vtx);
}
Expand Down
Loading