diff --git a/src/test/evo_deterministicmns_tests.cpp b/src/test/evo_deterministicmns_tests.cpp index b3842b7ffca6..8b12aa1cde4e 100644 --- a/src/test/evo_deterministicmns_tests.cpp +++ b/src/test/evo_deterministicmns_tests.cpp @@ -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 block_reorg; block_reorg.emplace_back(std::make_shared(tx_reg_ds)); - testPool.removeForBlock(block_reorg, nHeight + 2); + testPool.removeForBlock(block_reorg, nHeight + 2, chainman.ActiveChain().Tip()); BOOST_CHECK_EQUAL(testPool.size(), 0U); } @@ -1413,7 +1413,7 @@ void FuncTestMempoolProTxKeyChangedConflictChain(TestChainSetup& setup) // Pre-fix this aborted the process instead of returning. std::vector block_txs{std::make_shared(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); } @@ -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); @@ -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); @@ -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); @@ -1584,7 +1584,7 @@ void FuncTestMempoolDualProregtx(TestChainSetup& setup) const auto tx_same_value = with_platform_node_id(tx_evo_base, uint160{Assert(GetTxPayload(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())); } @@ -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 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); } @@ -1716,10 +1721,10 @@ 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. @@ -1727,7 +1732,7 @@ void FuncTestMempoolProRegReplacementUpdateConflict(TestChainSetup& setup) 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); } } diff --git a/src/test/mempool_tests.cpp b/src/test/mempool_tests.cpp index 11fd977351fc..1df8599c44b9 100644 --- a/src/test/mempool_tests.cpp +++ b/src/test/mempool_tests.cpp @@ -397,7 +397,7 @@ BOOST_AUTO_TEST_CASE(MempoolAncestorIndexingTest) /* after tx6 is mined, tx7 should move up in the sort */ std::vector 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 @@ -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 diff --git a/src/test/policyestimator_tests.cpp b/src/test/policyestimator_tests.cpp index bc9c672560fc..f5a5bc3bf921 100644 --- a/src/test/policyestimator_tests.cpp +++ b/src/test/policyestimator_tests.cpp @@ -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) { @@ -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++) { @@ -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++) { @@ -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++) { @@ -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)); diff --git a/src/txmempool.cpp b/src/txmempool.cpp index 7511e73d029f..c943919b860e 100644 --- a/src/txmempool.cpp +++ b/src/txmempool.cpp @@ -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()) { @@ -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{}}; + removeProTxSpentCollateralConflicts(tx, prev_mn_list); const uint256 tx_hash{tx.GetHash()}; if (tx.nType == TRANSACTION_PROVIDER_REGISTER) { @@ -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 { @@ -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& vtx, unsigned int nBlockHeight) +void CTxMemPool::removeForBlock(const std::vector& vtx, unsigned int nBlockHeight, const CBlockIndex* pindex_prev) { AssertLockHeld(cs); std::vector entries; @@ -1355,7 +1357,7 @@ void CTxMemPool::removeForBlock(const std::vector& vtx, unsigne RemoveStaged(stage, true, MemPoolRemovalReason::BLOCK); } removeConflicts(*tx); - removeProTxConflicts(*tx); + removeProTxConflicts(*tx, pindex_prev); removeAssetUnlockConflicts(*tx); ClearPrioritisation(tx->GetHash()); } diff --git a/src/txmempool.h b/src/txmempool.h index 328a0b13b0f5..0bf09bf9abdd 100644 --- a/src/txmempool.h +++ b/src/txmempool.h @@ -198,7 +198,9 @@ struct descendant_score {}; struct entry_time {}; struct ancestor_score {}; +class CBlockIndex; class CBlockPolicyEstimator; +class CDeterministicMNList; class CDeterministicMNManager; namespace llmq { @@ -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& 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& 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 diff --git a/src/validation.cpp b/src/validation.cpp index 12db50076cc0..bcec808d7fdf 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -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 @@ -3660,7 +3660,7 @@ bool Chainstate::ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew, Ticks(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); }