Skip to content

Commit 768c4ed

Browse files
UdjinM6claude
andcommitted
refactor: check provider transactions in the mempool against an explicit block's masternode list
The mempool read the masternode list at CDeterministicMNManager's tip, which the block tip notifications move only after a chain update, so mempool work done during one (block connect removals, reorg re-adds) used a list that lagged the chain. Callers now pass the block whose list applies: the chain tip on admission, the parent of a connected block in removeForBlock(), and pindexPrev in block assembly. This replaces moving the list tip before a reorg re-adds transactions. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent ed244e5 commit 768c4ed

9 files changed

Lines changed: 115 additions & 94 deletions

‎src/evo/chainhelper.cpp‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ uint256 CChainstateHelper::GetDeterministicMNListHash(const CBlockIndex* pindex)
6969
return SerializeHash(m_dmnman.GetListForBlock(Assert(pindex)));
7070
}
7171

72-
void CChainstateHelper::UpdatedMNListTip(const CBlockIndex* pindex) { m_dmnman.UpdatedBlockTip(Assert(pindex)); }
73-
7472
/** Passthrough functions to CCreditPoolManager */
7573
CCreditPool CChainstateHelper::GetCreditPool(const CBlockIndex* const pindex)
7674
{

‎src/evo/chainhelper.h‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@ class CChainstateHelper
7676
/** Return a canonical hash of the deterministic MN list derived at a block. */
7777
uint256 GetDeterministicMNListHash(const CBlockIndex* pindex) const;
7878

79-
/** Passthrough to CDeterministicMNManager::UpdatedBlockTip */
80-
void UpdatedMNListTip(const CBlockIndex* pindex);
81-
8279
/** Passthrough functions to CCreditPoolManager */
8380
CCreditPool GetCreditPool(const CBlockIndex* const pindex);
8481

‎src/node/miner.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ void BlockAssembler::addPackageTxs(const CTxMemPool& mempool, int& nPackagesSele
637637
// holding the key change back, which pending updates could otherwise do forever.
638638
CTxMemPool::setEntries serviceUpdates;
639639
for (const auto& entry : ancestors) {
640-
for (const auto& update : mempool.GetServiceUpdatesBeforeKeyChange(entry)) {
640+
for (const auto& update : mempool.GetServiceUpdatesBeforeKeyChange(entry, pindexPrev)) {
641641
if (inBlock.count(update) || serviceUpdates.count(update)) {
642642
continue;
643643
}
@@ -652,7 +652,7 @@ void BlockAssembler::addPackageTxs(const CTxMemPool& mempool, int& nPackagesSele
652652
return target && keyChangedMNs.count(*target);
653653
})};
654654
if (!followsKeyChange && TestPackageTransactions(updatePackage) &&
655-
!mempool.IsUnorderableServiceUpdate(update->GetTx(), updatePackage)) {
655+
!mempool.IsUnorderableServiceUpdate(update->GetTx(), updatePackage, pindexPrev)) {
656656
serviceUpdates.insert(updatePackage.begin(), updatePackage.end());
657657
}
658658
}
@@ -709,7 +709,7 @@ void BlockAssembler::addPackageTxs(const CTxMemPool& mempool, int& nPackagesSele
709709
validPackage = false;
710710
break;
711711
}
712-
if (const auto target = mempool.GetKeyChangeTarget(*entry)) {
712+
if (const auto target = mempool.GetKeyChangeTarget(*entry, pindexPrev)) {
713713
packageKeyChangedMNs.push_back(*target);
714714
}
715715
if (std::optional<uint8_t> signal = extractEHFSignal(tx); signal != std::nullopt) {

‎src/test/evo_deterministicmns_tests.cpp‎

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,6 +1271,15 @@ void FuncProUpServInvalidNType(TestChainSetup& setup)
12711271
}
12721272
}
12731273

1274+
// Adds entry to pool as AcceptToMemoryPool does, recording a provider transaction against the
1275+
// masternode list at pindex.
1276+
static void AddToMempool(CTxMemPool& pool, const CTxMemPoolEntry& entry, const CBlockIndex* pindex)
1277+
EXCLUSIVE_LOCKS_REQUIRED(::cs_main, pool.cs)
1278+
{
1279+
auto ancestors{pool.AssumeCalculateMemPoolAncestors(__func__, entry, CTxMemPool::Limits::NoLimits())};
1280+
pool.addUnchecked(entry, ancestors, pindex);
1281+
}
1282+
12741283
void FuncTestMempoolReorg(TestChainSetup& setup)
12751284
{
12761285
auto& chainman = *Assert(setup.m_node.chainman.get());
@@ -1315,7 +1324,7 @@ void FuncTestMempoolReorg(TestChainSetup& setup)
13151324

13161325
// Create ProUpServ and test block reorg which double-spend ProRegTx
13171326
auto tx_up_serv = CreateProUpServTx(chainman, utxos, tx_reg.GetHash(), operatorKey, 2, CScript(), setup.coinbaseKey);
1318-
testPool.addUnchecked(entry.FromTx(tx_up_serv));
1327+
AddToMempool(testPool, entry.FromTx(tx_up_serv), chainman.ActiveChain().Tip());
13191328
// A disconnected block would insert ProRegTx back into mempool
13201329
testPool.addUnchecked(entry.FromTx(tx_reg));
13211330
BOOST_CHECK_EQUAL(testPool.size(), 2U);
@@ -1329,7 +1338,7 @@ void FuncTestMempoolReorg(TestChainSetup& setup)
13291338
// Check mempool as if a new block with tx_reg_ds was connected instead of the old one with tx_reg
13301339
std::vector<CTransactionRef> block_reorg;
13311340
block_reorg.emplace_back(std::make_shared<CTransaction>(tx_reg_ds));
1332-
testPool.removeForBlock(block_reorg, nHeight + 2);
1341+
testPool.removeForBlock(block_reorg, nHeight + 2, chainman.ActiveChain().Tip());
13331342
BOOST_CHECK_EQUAL(testPool.size(), 0U);
13341343
}
13351344

@@ -1397,13 +1406,13 @@ void FuncTestMempoolProTxKeyChangedConflictChain(TestChainSetup& setup)
13971406
TestMemPoolEntryHelper entry;
13981407
LOCK2(cs_main, testPool.cs);
13991408

1400-
testPool.addUnchecked(entry.FromTx(tx_parent));
1401-
testPool.addUnchecked(entry.FromTx(tx_child));
1409+
AddToMempool(testPool, entry.FromTx(tx_parent), chainman.ActiveChain().Tip());
1410+
AddToMempool(testPool, entry.FromTx(tx_child), chainman.ActiveChain().Tip());
14021411
BOOST_CHECK_EQUAL(testPool.size(), 2U);
14031412

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

@@ -1440,7 +1449,7 @@ void FuncTestMempoolDualProregtx(TestChainSetup& setup)
14401449

14411450
testPool.addUnchecked(entry.FromTx(tx_reg1));
14421451
BOOST_CHECK_EQUAL(testPool.size(), 1U);
1443-
BOOST_CHECK(testPool.existsProviderTxConflict(CTransaction(tx_reg2)));
1452+
BOOST_CHECK(testPool.existsProviderTxConflict(CTransaction(tx_reg2), chainman.ActiveChain().Tip()));
14441453
}
14451454

14461455
// A ProRegTx that reuses a confirmed external collateral replaces the live MN at block
@@ -1531,21 +1540,21 @@ void FuncTestMempoolProRegReplacementUpdateConflict(TestChainSetup& setup)
15311540
// Replacement already in mempool => update for the MN it replaces is a conflict.
15321541
testPool.addUnchecked(entry.FromTx(tx_reg_replace));
15331542
BOOST_CHECK_EQUAL(testPool.size(), 1U);
1534-
BOOST_CHECK(testPool.existsProviderTxConflict(CTransaction(tx_up_serv)));
1543+
BOOST_CHECK(testPool.existsProviderTxConflict(CTransaction(tx_up_serv), tip_index()));
15351544
testPool.removeRecursive(CTransaction(tx_reg_replace), MemPoolRemovalReason::MANUAL);
15361545
BOOST_CHECK_EQUAL(testPool.size(), 0U);
15371546

15381547
// Update already in mempool => replacement ProRegTx reusing that MN's collateral conflicts.
1539-
testPool.addUnchecked(entry.FromTx(tx_up_serv));
1548+
AddToMempool(testPool, entry.FromTx(tx_up_serv), tip_index());
15401549
BOOST_CHECK_EQUAL(testPool.size(), 1U);
1541-
BOOST_CHECK(testPool.existsProviderTxConflict(CTransaction(tx_reg_replace)));
1550+
BOOST_CHECK(testPool.existsProviderTxConflict(CTransaction(tx_reg_replace), tip_index()));
15421551

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

@@ -1569,20 +1578,21 @@ void FuncTestMempoolProRegReplacementUpdateConflict(TestChainSetup& setup)
15691578
LOCK2(cs_main, testPool.cs);
15701579
// ProUpServ and ProUpRev are signed by the operator key, so they stay valid across a
15711580
// confirmed ProUpReg that keeps it, and across a spend of anything but the collateral.
1572-
testPool.addUnchecked(entry.FromTx(tx_up_serv));
1573-
testPool.addUnchecked(entry.FromTx(tx_up_rev));
1574-
testPool.removeForBlock({MakeTransactionRef(tx_up_reg), MakeTransactionRef(tx_spend_other)}, tip_height() + 1);
1581+
AddToMempool(testPool, entry.FromTx(tx_up_serv), tip_index());
1582+
AddToMempool(testPool, entry.FromTx(tx_up_rev), tip_index());
1583+
testPool.removeForBlock({MakeTransactionRef(tx_up_reg), MakeTransactionRef(tx_spend_other)}, tip_height() + 1,
1584+
tip_index());
15751585
BOOST_CHECK_EQUAL(testPool.size(), 2U);
15761586

1577-
testPool.removeForBlock({MakeTransactionRef(tx_up_reg_new_key)}, tip_height() + 1);
1587+
testPool.removeForBlock({MakeTransactionRef(tx_up_reg_new_key)}, tip_height() + 1, tip_index());
15781588
BOOST_CHECK_EQUAL(testPool.size(), 0U);
15791589

15801590
// Spending the collateral removes the MN, and no update of any kind can be mined after that.
1581-
testPool.addUnchecked(entry.FromTx(tx_up_serv));
1582-
testPool.addUnchecked(entry.FromTx(tx_up_reg));
1583-
testPool.addUnchecked(entry.FromTx(tx_up_rev));
1591+
AddToMempool(testPool, entry.FromTx(tx_up_serv), tip_index());
1592+
AddToMempool(testPool, entry.FromTx(tx_up_reg), tip_index());
1593+
AddToMempool(testPool, entry.FromTx(tx_up_rev), tip_index());
15841594
BOOST_CHECK_EQUAL(testPool.size(), 3U);
1585-
testPool.removeForBlock({MakeTransactionRef(tx_spend_collateral)}, tip_height() + 1);
1595+
testPool.removeForBlock({MakeTransactionRef(tx_spend_collateral)}, tip_height() + 1, tip_index());
15861596
BOOST_CHECK_EQUAL(testPool.size(), 0U);
15871597
}
15881598
}
@@ -1599,7 +1609,7 @@ static std::map<uint256, size_t> TemplatePositions(TestChainSetup& setup,
15991609
{
16001610
LOCK2(cs_main, mempool.cs);
16011611
for (const auto& [tx, fee] : txs) {
1602-
mempool.addUnchecked(entry.Fee(fee).FromTx(tx));
1612+
AddToMempool(mempool, entry.Fee(fee).FromTx(tx), chainman.ActiveChain().Tip());
16031613
}
16041614
}
16051615
node::BlockAssembler::Options options;
@@ -2993,7 +3003,7 @@ void FuncEvoNodeRegistrarRaiseNotMined(TestChainV24SignalBeforeV19Setup& setup)
29933003
TestMemPoolEntryHelper entry;
29943004
{
29953005
LOCK2(cs_main, mempool.cs);
2996-
mempool.addUnchecked(entry.Fee(50000).Time(Now<NodeSeconds>()).FromTx(tx));
3006+
AddToMempool(mempool, entry.Fee(50000).Time(Now<NodeSeconds>()).FromTx(tx), chainman.ActiveChain().Tip());
29973007
BOOST_REQUIRE(mempool.exists(tx.GetHash()));
29983008
}
29993009
const auto block_template = node::BlockAssembler{chainman.ActiveChainstate(), setup.m_node, &mempool}.CreateNewBlock(

‎src/test/mempool_tests.cpp‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ BOOST_AUTO_TEST_CASE(MempoolIndexingTest)
206206
BOOST_REQUIRE(ancestors_calculated.has_value());
207207
BOOST_CHECK(*ancestors_calculated == setAncestors);
208208

209-
pool.addUnchecked(entry.FromTx(tx7), setAncestors);
209+
pool.addUnchecked(entry.FromTx(tx7), setAncestors, /*pindex=*/nullptr);
210210
BOOST_CHECK_EQUAL(pool.size(), 7U);
211211

212212
// Now tx6 should be sorted higher (high fee child): tx7, tx6, tx2, ...
@@ -224,7 +224,7 @@ BOOST_AUTO_TEST_CASE(MempoolIndexingTest)
224224
tx8.vout[0].scriptPubKey = CScript() << OP_11 << OP_EQUAL;
225225
tx8.vout[0].nValue = 10 * COIN;
226226
setAncestors.insert(pool.mapTx.find(tx7.GetHash()));
227-
pool.addUnchecked(entry.Fee(0LL).Time(NodeSeconds{2s}).FromTx(tx8), setAncestors);
227+
pool.addUnchecked(entry.Fee(0LL).Time(NodeSeconds{2s}).FromTx(tx8), setAncestors, /*pindex=*/nullptr);
228228

229229
// Now tx8 should be sorted low, but tx6/tx both high
230230
sortedOrder.insert(sortedOrder.begin(), tx8.GetHash().ToString());
@@ -238,7 +238,7 @@ BOOST_AUTO_TEST_CASE(MempoolIndexingTest)
238238
tx9.vout.resize(1);
239239
tx9.vout[0].scriptPubKey = CScript() << OP_11 << OP_EQUAL;
240240
tx9.vout[0].nValue = 1 * COIN;
241-
pool.addUnchecked(entry.Fee(0LL).Time(NodeSeconds{3s}).FromTx(tx9), setAncestors);
241+
pool.addUnchecked(entry.Fee(0LL).Time(NodeSeconds{3s}).FromTx(tx9), setAncestors, /*pindex=*/nullptr);
242242

243243
// tx9 should be sorted low
244244
BOOST_CHECK_EQUAL(pool.size(), 9U);
@@ -264,7 +264,7 @@ BOOST_AUTO_TEST_CASE(MempoolIndexingTest)
264264
BOOST_REQUIRE(ancestors_calculated);
265265
BOOST_CHECK(*ancestors_calculated == setAncestors);
266266

267-
pool.addUnchecked(entry.FromTx(tx10), setAncestors);
267+
pool.addUnchecked(entry.FromTx(tx10), setAncestors, /*pindex=*/nullptr);
268268

269269
/**
270270
* tx8 and tx9 should both now be sorted higher
@@ -397,7 +397,7 @@ BOOST_AUTO_TEST_CASE(MempoolAncestorIndexingTest)
397397
/* after tx6 is mined, tx7 should move up in the sort */
398398
std::vector<CTransactionRef> vtx;
399399
vtx.push_back(MakeTransactionRef(tx6));
400-
pool.removeForBlock(vtx, 1);
400+
pool.removeForBlock(vtx, 1, /*pindexPrev=*/nullptr);
401401

402402
sortedOrder.erase(sortedOrder.begin()+1);
403403
// Ties are broken by hash
@@ -557,7 +557,7 @@ BOOST_AUTO_TEST_CASE(MempoolSizeLimitTest)
557557
SetMockTime(42 + CTxMemPool::ROLLING_FEE_HALFLIFE);
558558
BOOST_CHECK_EQUAL(pool.GetMinFee(1).GetFeePerK(), maxFeeRateRemoved.GetFeePerK() + 1000);
559559
// ... we should keep the same min fee until we get a block
560-
pool.removeForBlock(vtx, 1);
560+
pool.removeForBlock(vtx, 1, /*pindexPrev=*/nullptr);
561561
SetMockTime(42 + 2*CTxMemPool::ROLLING_FEE_HALFLIFE);
562562
BOOST_CHECK_EQUAL(pool.GetMinFee(1).GetFeePerK(), llround((maxFeeRateRemoved.GetFeePerK() + 1000)/2.0));
563563
// ... then feerate should drop 1/2 each halflife

‎src/test/policyestimator_tests.cpp‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ BOOST_AUTO_TEST_CASE(BlockPolicyEstimates)
7676
txHashes[9-h].pop_back();
7777
}
7878
}
79-
mpool.removeForBlock(block, ++blocknum);
79+
mpool.removeForBlock(block, ++blocknum, /*pindexPrev=*/nullptr);
8080
block.clear();
8181
// Check after just a few txs that combining buckets works as expected
8282
if (blocknum == 3) {
@@ -115,7 +115,7 @@ BOOST_AUTO_TEST_CASE(BlockPolicyEstimates)
115115
// Mine 50 more blocks with no transactions happening, estimates shouldn't change
116116
// We haven't decayed the moving average enough so we still have enough data points in every bucket
117117
while (blocknum < 250)
118-
mpool.removeForBlock(block, ++blocknum);
118+
mpool.removeForBlock(block, ++blocknum, /*pindexPrev=*/nullptr);
119119

120120
BOOST_CHECK(feeEst.estimateFee(1) == CFeeRate(0));
121121
for (int i = 2; i < 10;i++) {
@@ -135,7 +135,7 @@ BOOST_AUTO_TEST_CASE(BlockPolicyEstimates)
135135
txHashes[j].push_back(hash);
136136
}
137137
}
138-
mpool.removeForBlock(block, ++blocknum);
138+
mpool.removeForBlock(block, ++blocknum, /*pindexPrev=*/nullptr);
139139
}
140140

141141
for (int i = 1; i < 10;i++) {
@@ -152,7 +152,7 @@ BOOST_AUTO_TEST_CASE(BlockPolicyEstimates)
152152
txHashes[j].pop_back();
153153
}
154154
}
155-
mpool.removeForBlock(block, 266);
155+
mpool.removeForBlock(block, 266, /*pindexPrev=*/nullptr);
156156
block.clear();
157157
BOOST_CHECK(feeEst.estimateFee(1) == CFeeRate(0));
158158
for (int i = 2; i < 10;i++) {
@@ -173,7 +173,7 @@ BOOST_AUTO_TEST_CASE(BlockPolicyEstimates)
173173

174174
}
175175
}
176-
mpool.removeForBlock(block, ++blocknum);
176+
mpool.removeForBlock(block, ++blocknum, /*pindexPrev=*/nullptr);
177177
block.clear();
178178
}
179179
BOOST_CHECK(feeEst.estimateFee(1) == CFeeRate(0));

0 commit comments

Comments
 (0)