miniminer_tests.cpp
1 // Copyright (c) 2021 The Bitcoin Core developers 2 // Distributed under the MIT software license, see the accompanying 3 // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 #include <node/mini_miner.h> 5 #include <random.h> 6 #include <txmempool.h> 7 #include <util/time.h> 8 9 #include <test/util/setup_common.h> 10 #include <test/util/txmempool.h> 11 12 #include <boost/test/unit_test.hpp> 13 #include <optional> 14 #include <vector> 15 16 BOOST_FIXTURE_TEST_SUITE(miniminer_tests, TestingSetup) 17 18 const CAmount low_fee{CENT/2000}; // 500 ṩ 19 const CAmount med_fee{CENT/200}; // 5000 ṩ 20 const CAmount high_fee{CENT/10}; // 100_000 ṩ 21 22 23 static inline CTransactionRef make_tx(const std::vector<COutPoint>& inputs, size_t num_outputs) 24 { 25 CMutableTransaction tx = CMutableTransaction(); 26 tx.vin.resize(inputs.size()); 27 tx.vout.resize(num_outputs); 28 for (size_t i = 0; i < inputs.size(); ++i) { 29 tx.vin[i].prevout = inputs[i]; 30 } 31 for (size_t i = 0; i < num_outputs; ++i) { 32 tx.vout[i].scriptPubKey = CScript() << OP_11 << OP_EQUAL; 33 // The actual input and output values of these transactions don't really 34 // matter, since all accounting will use the entries' cached fees. 35 tx.vout[i].nValue = COIN; 36 } 37 return MakeTransactionRef(tx); 38 } 39 40 static inline bool sanity_check(const std::vector<CTransactionRef>& transactions, 41 const std::map<COutPoint, CAmount>& bumpfees) 42 { 43 // No negative bumpfees. 44 for (const auto& [outpoint, fee] : bumpfees) { 45 if (fee < 0) return false; 46 if (fee == 0) continue; 47 auto outpoint_ = outpoint; // structured bindings can't be captured in C++17, so we need to use a variable 48 const bool found = std::any_of(transactions.cbegin(), transactions.cend(), [&](const auto& tx) { 49 return outpoint_.hash == tx->GetHash() && outpoint_.n < tx->vout.size(); 50 }); 51 if (!found) return false; 52 } 53 for (const auto& tx : transactions) { 54 // If tx has multiple outputs, they must all have the same bumpfee (if they exist). 55 if (tx->vout.size() > 1) { 56 std::set<CAmount> distinct_bumpfees; 57 for (size_t i{0}; i < tx->vout.size(); ++i) { 58 const auto bumpfee = bumpfees.find(COutPoint{tx->GetHash(), static_cast<uint32_t>(i)}); 59 if (bumpfee != bumpfees.end()) distinct_bumpfees.insert(bumpfee->second); 60 } 61 if (distinct_bumpfees.size() > 1) return false; 62 } 63 } 64 return true; 65 } 66 67 template <typename Key, typename Value> 68 Value Find(const std::map<Key, Value>& map, const Key& key) 69 { 70 auto it = map.find(key); 71 BOOST_CHECK_MESSAGE(it != map.end(), strprintf("Cannot find %s", key.ToString())); 72 return it->second; 73 } 74 75 BOOST_FIXTURE_TEST_CASE(miniminer_negative, TestChain100Setup) 76 { 77 CTxMemPool& pool = *Assert(m_node.mempool); 78 LOCK2(::cs_main, pool.cs); 79 TestMemPoolEntryHelper entry; 80 81 // Create a transaction that will be prioritised to have a negative modified fee. 82 const CAmount positive_base_fee{1000}; 83 const CAmount negative_fee_delta{-50000}; 84 const CAmount negative_modified_fees{positive_base_fee + negative_fee_delta}; 85 BOOST_CHECK(negative_modified_fees < 0); 86 const auto tx_mod_negative = make_tx({COutPoint{m_coinbase_txns[4]->GetHash(), 0}}, /*num_outputs=*/1); 87 pool.addUnchecked(entry.Fee(positive_base_fee).FromTx(tx_mod_negative)); 88 pool.PrioritiseTransaction(tx_mod_negative->GetHash(), negative_fee_delta); 89 const COutPoint only_outpoint{tx_mod_negative->GetHash(), 0}; 90 91 // When target feerate is 0, transactions with negative fees are not selected. 92 node::MiniMiner mini_miner_target0(pool, {only_outpoint}); 93 BOOST_CHECK(mini_miner_target0.IsReadyToCalculate()); 94 const CFeeRate feerate_zero(0); 95 mini_miner_target0.BuildMockTemplate(feerate_zero); 96 // Check the quit condition: 97 BOOST_CHECK(negative_modified_fees < feerate_zero.GetFee(Assert(pool.GetEntry(tx_mod_negative->GetHash()))->GetTxSize())); 98 BOOST_CHECK(mini_miner_target0.GetMockTemplateTxids().empty()); 99 100 // With no target feerate, the template includes all transactions, even negative feerate ones. 101 node::MiniMiner mini_miner_no_target(pool, {only_outpoint}); 102 BOOST_CHECK(mini_miner_no_target.IsReadyToCalculate()); 103 mini_miner_no_target.BuildMockTemplate(std::nullopt); 104 const auto template_txids{mini_miner_no_target.GetMockTemplateTxids()}; 105 BOOST_CHECK_EQUAL(template_txids.size(), 1); 106 BOOST_CHECK(template_txids.count(tx_mod_negative->GetHash().ToUint256()) > 0); 107 } 108 109 BOOST_FIXTURE_TEST_CASE(miniminer_1p1c, TestChain100Setup) 110 { 111 CTxMemPool& pool = *Assert(m_node.mempool); 112 LOCK2(::cs_main, pool.cs); 113 TestMemPoolEntryHelper entry; 114 115 // Create a parent tx0 and child tx1 with normal fees: 116 const auto tx0 = make_tx({COutPoint{m_coinbase_txns[0]->GetHash(), 0}}, /*num_outputs=*/2); 117 pool.addUnchecked(entry.Fee(med_fee).FromTx(tx0)); 118 const auto tx1 = make_tx({COutPoint{tx0->GetHash(), 0}}, /*num_outputs=*/1); 119 pool.addUnchecked(entry.Fee(med_fee).FromTx(tx1)); 120 121 // Create a low-feerate parent tx2 and high-feerate child tx3 (cpfp) 122 const auto tx2 = make_tx({COutPoint{m_coinbase_txns[1]->GetHash(), 0}}, /*num_outputs=*/2); 123 pool.addUnchecked(entry.Fee(low_fee).FromTx(tx2)); 124 const auto tx3 = make_tx({COutPoint{tx2->GetHash(), 0}}, /*num_outputs=*/1); 125 pool.addUnchecked(entry.Fee(high_fee).FromTx(tx3)); 126 127 // Create a parent tx4 and child tx5 where both have low fees 128 const auto tx4 = make_tx({COutPoint{m_coinbase_txns[2]->GetHash(), 0}}, /*num_outputs=*/2); 129 pool.addUnchecked(entry.Fee(low_fee).FromTx(tx4)); 130 const auto tx5 = make_tx({COutPoint{tx4->GetHash(), 0}}, /*num_outputs=*/1); 131 pool.addUnchecked(entry.Fee(low_fee).FromTx(tx5)); 132 const CAmount tx5_delta{CENT/100}; 133 // Make tx5's modified fee much higher than its base fee. This should cause it to pass 134 // the fee-related checks despite being low-feerate. 135 pool.PrioritiseTransaction(tx5->GetHash(), tx5_delta); 136 const CAmount tx5_mod_fee{low_fee + tx5_delta}; 137 138 // Create a high-feerate parent tx6, low-feerate child tx7 139 const auto tx6 = make_tx({COutPoint{m_coinbase_txns[3]->GetHash(), 0}}, /*num_outputs=*/2); 140 pool.addUnchecked(entry.Fee(high_fee).FromTx(tx6)); 141 const auto tx7 = make_tx({COutPoint{tx6->GetHash(), 0}}, /*num_outputs=*/1); 142 pool.addUnchecked(entry.Fee(low_fee).FromTx(tx7)); 143 144 std::vector<COutPoint> all_unspent_outpoints({ 145 COutPoint{tx0->GetHash(), 1}, 146 COutPoint{tx1->GetHash(), 0}, 147 COutPoint{tx2->GetHash(), 1}, 148 COutPoint{tx3->GetHash(), 0}, 149 COutPoint{tx4->GetHash(), 1}, 150 COutPoint{tx5->GetHash(), 0}, 151 COutPoint{tx6->GetHash(), 1}, 152 COutPoint{tx7->GetHash(), 0} 153 }); 154 for (const auto& outpoint : all_unspent_outpoints) BOOST_CHECK(!pool.isSpent(outpoint)); 155 156 std::vector<COutPoint> all_spent_outpoints({ 157 COutPoint{tx0->GetHash(), 0}, 158 COutPoint{tx2->GetHash(), 0}, 159 COutPoint{tx4->GetHash(), 0}, 160 COutPoint{tx6->GetHash(), 0} 161 }); 162 for (const auto& outpoint : all_spent_outpoints) BOOST_CHECK(pool.GetConflictTx(outpoint) != nullptr); 163 164 std::vector<COutPoint> all_parent_outputs({ 165 COutPoint{tx0->GetHash(), 0}, 166 COutPoint{tx0->GetHash(), 1}, 167 COutPoint{tx2->GetHash(), 0}, 168 COutPoint{tx2->GetHash(), 1}, 169 COutPoint{tx4->GetHash(), 0}, 170 COutPoint{tx4->GetHash(), 1}, 171 COutPoint{tx6->GetHash(), 0}, 172 COutPoint{tx6->GetHash(), 1} 173 }); 174 175 176 std::vector<CTransactionRef> all_transactions{tx0, tx1, tx2, tx3, tx4, tx5, tx6, tx7}; 177 struct TxDimensions { 178 int32_t vsize; CAmount mod_fee; CFeeRate feerate; 179 }; 180 std::map<uint256, TxDimensions> tx_dims; 181 for (const auto& tx : all_transactions) { 182 const auto& entry{*Assert(pool.GetEntry(tx->GetHash()))}; 183 tx_dims.emplace(tx->GetHash(), TxDimensions{entry.GetTxSize(), entry.GetModifiedFee(), 184 CFeeRate(entry.GetModifiedFee(), entry.GetTxSize())}); 185 } 186 187 const std::vector<CFeeRate> various_normal_feerates({CFeeRate(0), CFeeRate(500), CFeeRate(999), 188 CFeeRate(1000), CFeeRate(2000), CFeeRate(2500), 189 CFeeRate(3333), CFeeRate(7800), CFeeRate(11199), 190 CFeeRate(23330), CFeeRate(50000), CFeeRate(5*CENT)}); 191 192 // All nonexistent entries have a bumpfee of zero, regardless of feerate 193 std::vector<COutPoint> nonexistent_outpoints({ COutPoint{Txid::FromUint256(GetRandHash()), 0}, COutPoint{Txid::FromUint256(GetRandHash()), 3} }); 194 for (const auto& outpoint : nonexistent_outpoints) BOOST_CHECK(!pool.isSpent(outpoint)); 195 for (const auto& feerate : various_normal_feerates) { 196 node::MiniMiner mini_miner(pool, nonexistent_outpoints); 197 BOOST_CHECK(mini_miner.IsReadyToCalculate()); 198 auto bump_fees = mini_miner.CalculateBumpFees(feerate); 199 BOOST_CHECK(!mini_miner.IsReadyToCalculate()); 200 BOOST_CHECK(sanity_check(all_transactions, bump_fees)); 201 BOOST_CHECK(bump_fees.size() == nonexistent_outpoints.size()); 202 for (const auto& outpoint: nonexistent_outpoints) { 203 auto it = bump_fees.find(outpoint); 204 BOOST_CHECK(it != bump_fees.end()); 205 BOOST_CHECK_EQUAL(it->second, 0); 206 } 207 } 208 209 // Gather bump fees for all available UTXOs. 210 for (const auto& target_feerate : various_normal_feerates) { 211 node::MiniMiner mini_miner(pool, all_unspent_outpoints); 212 BOOST_CHECK(mini_miner.IsReadyToCalculate()); 213 auto bump_fees = mini_miner.CalculateBumpFees(target_feerate); 214 BOOST_CHECK(!mini_miner.IsReadyToCalculate()); 215 BOOST_CHECK(sanity_check(all_transactions, bump_fees)); 216 BOOST_CHECK_EQUAL(bump_fees.size(), all_unspent_outpoints.size()); 217 218 // Check tx0 bumpfee: no other bumper. 219 const TxDimensions& tx0_dimensions = tx_dims.find(tx0->GetHash())->second; 220 CAmount bumpfee0 = Find(bump_fees, COutPoint{tx0->GetHash(), 1}); 221 if (target_feerate <= tx0_dimensions.feerate) { 222 BOOST_CHECK_EQUAL(bumpfee0, 0); 223 } else { 224 // Difference is fee to bump tx0 from current to target feerate. 225 BOOST_CHECK_EQUAL(bumpfee0, target_feerate.GetFee(tx0_dimensions.vsize) - tx0_dimensions.mod_fee); 226 } 227 228 // Check tx2 bumpfee: assisted by tx3. 229 const TxDimensions& tx2_dimensions = tx_dims.find(tx2->GetHash())->second; 230 const TxDimensions& tx3_dimensions = tx_dims.find(tx3->GetHash())->second; 231 const CFeeRate tx2_feerate = CFeeRate(tx2_dimensions.mod_fee + tx3_dimensions.mod_fee, tx2_dimensions.vsize + tx3_dimensions.vsize); 232 CAmount bumpfee2 = Find(bump_fees, COutPoint{tx2->GetHash(), 1}); 233 if (target_feerate <= tx2_feerate) { 234 // As long as target feerate is below tx3's ancestor feerate, there is no bump fee. 235 BOOST_CHECK_EQUAL(bumpfee2, 0); 236 } else { 237 // Difference is fee to bump tx2 from current to target feerate, without tx3. 238 BOOST_CHECK_EQUAL(bumpfee2, target_feerate.GetFee(tx2_dimensions.vsize) - tx2_dimensions.mod_fee); 239 } 240 241 // If tx5’s modified fees are sufficient for tx4 and tx5 to be picked 242 // into the block, our prospective new transaction would not need to 243 // bump tx4 when using tx4’s second output. If however even tx5’s 244 // modified fee (which essentially indicates "effective feerate") is 245 // not sufficient to bump tx4, using the second output of tx4 would 246 // require our transaction to bump tx4 from scratch since we evaluate 247 // transaction packages per ancestor sets and do not consider multiple 248 // children’s fees. 249 const TxDimensions& tx4_dimensions = tx_dims.find(tx4->GetHash())->second; 250 const TxDimensions& tx5_dimensions = tx_dims.find(tx5->GetHash())->second; 251 const CFeeRate tx4_feerate = CFeeRate(tx4_dimensions.mod_fee + tx5_dimensions.mod_fee, tx4_dimensions.vsize + tx5_dimensions.vsize); 252 CAmount bumpfee4 = Find(bump_fees, COutPoint{tx4->GetHash(), 1}); 253 if (target_feerate <= tx4_feerate) { 254 // As long as target feerate is below tx5's ancestor feerate, there is no bump fee. 255 BOOST_CHECK_EQUAL(bumpfee4, 0); 256 } else { 257 // Difference is fee to bump tx4 from current to target feerate, without tx5. 258 BOOST_CHECK_EQUAL(bumpfee4, target_feerate.GetFee(tx4_dimensions.vsize) - tx4_dimensions.mod_fee); 259 } 260 } 261 // Spent outpoints should usually not be requested as they would not be 262 // considered available. However, when they are explicitly requested, we 263 // can calculate their bumpfee to facilitate RBF-replacements 264 for (const auto& target_feerate : various_normal_feerates) { 265 node::MiniMiner mini_miner_all_spent(pool, all_spent_outpoints); 266 BOOST_CHECK(mini_miner_all_spent.IsReadyToCalculate()); 267 auto bump_fees_all_spent = mini_miner_all_spent.CalculateBumpFees(target_feerate); 268 BOOST_CHECK(!mini_miner_all_spent.IsReadyToCalculate()); 269 BOOST_CHECK_EQUAL(bump_fees_all_spent.size(), all_spent_outpoints.size()); 270 node::MiniMiner mini_miner_all_parents(pool, all_parent_outputs); 271 BOOST_CHECK(mini_miner_all_parents.IsReadyToCalculate()); 272 auto bump_fees_all_parents = mini_miner_all_parents.CalculateBumpFees(target_feerate); 273 BOOST_CHECK(!mini_miner_all_parents.IsReadyToCalculate()); 274 BOOST_CHECK_EQUAL(bump_fees_all_parents.size(), all_parent_outputs.size()); 275 for (auto& bump_fees : {bump_fees_all_parents, bump_fees_all_spent}) { 276 // For all_parents case, both outputs from the parent should have the same bump fee, 277 // even though only one of them is in a to-be-replaced transaction. 278 BOOST_CHECK(sanity_check(all_transactions, bump_fees)); 279 280 // Check tx0 bumpfee: no other bumper. 281 const TxDimensions& tx0_dimensions = tx_dims.find(tx0->GetHash())->second; 282 CAmount it0_spent = Find(bump_fees, COutPoint{tx0->GetHash(), 0}); 283 if (target_feerate <= tx0_dimensions.feerate) { 284 BOOST_CHECK_EQUAL(it0_spent, 0); 285 } else { 286 // Difference is fee to bump tx0 from current to target feerate. 287 BOOST_CHECK_EQUAL(it0_spent, target_feerate.GetFee(tx0_dimensions.vsize) - tx0_dimensions.mod_fee); 288 } 289 290 // Check tx2 bumpfee: no other bumper, because tx3 is to-be-replaced. 291 const TxDimensions& tx2_dimensions = tx_dims.find(tx2->GetHash())->second; 292 const CFeeRate tx2_feerate_unbumped = tx2_dimensions.feerate; 293 auto it2_spent = Find(bump_fees, COutPoint{tx2->GetHash(), 0}); 294 if (target_feerate <= tx2_feerate_unbumped) { 295 BOOST_CHECK_EQUAL(it2_spent, 0); 296 } else { 297 // Difference is fee to bump tx2 from current to target feerate, without tx3. 298 BOOST_CHECK_EQUAL(it2_spent, target_feerate.GetFee(tx2_dimensions.vsize) - tx2_dimensions.mod_fee); 299 } 300 301 // Check tx4 bumpfee: no other bumper, because tx5 is to-be-replaced. 302 const TxDimensions& tx4_dimensions = tx_dims.find(tx4->GetHash())->second; 303 const CFeeRate tx4_feerate_unbumped = tx4_dimensions.feerate; 304 auto it4_spent = Find(bump_fees, COutPoint{tx4->GetHash(), 0}); 305 if (target_feerate <= tx4_feerate_unbumped) { 306 BOOST_CHECK_EQUAL(it4_spent, 0); 307 } else { 308 // Difference is fee to bump tx4 from current to target feerate, without tx5. 309 BOOST_CHECK_EQUAL(it4_spent, target_feerate.GetFee(tx4_dimensions.vsize) - tx4_dimensions.mod_fee); 310 } 311 } 312 } 313 314 // Check m_inclusion_order for equivalent mempool- and manually-constructed MiniMiners. 315 // (We cannot check bump fees in manually-constructed MiniMiners because it doesn't know what 316 // outpoints are requested). 317 std::vector<node::MiniMinerMempoolEntry> miniminer_info; 318 { 319 const int32_t tx0_vsize{tx_dims.at(tx0->GetHash()).vsize}; 320 const int32_t tx1_vsize{tx_dims.at(tx1->GetHash()).vsize}; 321 const int32_t tx2_vsize{tx_dims.at(tx2->GetHash()).vsize}; 322 const int32_t tx3_vsize{tx_dims.at(tx3->GetHash()).vsize}; 323 const int32_t tx4_vsize{tx_dims.at(tx4->GetHash()).vsize}; 324 const int32_t tx5_vsize{tx_dims.at(tx5->GetHash()).vsize}; 325 const int32_t tx6_vsize{tx_dims.at(tx6->GetHash()).vsize}; 326 const int32_t tx7_vsize{tx_dims.at(tx7->GetHash()).vsize}; 327 328 miniminer_info.emplace_back(tx0,/*vsize_self=*/tx0_vsize,/*vsize_ancestor=*/tx0_vsize,/*fee_self=*/med_fee,/*fee_ancestor=*/med_fee); 329 miniminer_info.emplace_back(tx1, tx1_vsize, tx0_vsize + tx1_vsize, med_fee, 2*med_fee); 330 miniminer_info.emplace_back(tx2, tx2_vsize, tx2_vsize, low_fee, low_fee); 331 miniminer_info.emplace_back(tx3, tx3_vsize, tx2_vsize + tx3_vsize, high_fee, low_fee + high_fee); 332 miniminer_info.emplace_back(tx4, tx4_vsize, tx4_vsize, low_fee, low_fee); 333 miniminer_info.emplace_back(tx5, tx5_vsize, tx4_vsize + tx5_vsize, tx5_mod_fee, low_fee + tx5_mod_fee); 334 miniminer_info.emplace_back(tx6, tx6_vsize, tx6_vsize, high_fee, high_fee); 335 miniminer_info.emplace_back(tx7, tx7_vsize, tx6_vsize + tx7_vsize, low_fee, high_fee + low_fee); 336 } 337 std::map<Txid, std::set<Txid>> descendant_caches; 338 descendant_caches.emplace(tx0->GetHash(), std::set<Txid>{tx0->GetHash(), tx1->GetHash()}); 339 descendant_caches.emplace(tx1->GetHash(), std::set<Txid>{tx1->GetHash()}); 340 descendant_caches.emplace(tx2->GetHash(), std::set<Txid>{tx2->GetHash(), tx3->GetHash()}); 341 descendant_caches.emplace(tx3->GetHash(), std::set<Txid>{tx3->GetHash()}); 342 descendant_caches.emplace(tx4->GetHash(), std::set<Txid>{tx4->GetHash(), tx5->GetHash()}); 343 descendant_caches.emplace(tx5->GetHash(), std::set<Txid>{tx5->GetHash()}); 344 descendant_caches.emplace(tx6->GetHash(), std::set<Txid>{tx6->GetHash(), tx7->GetHash()}); 345 descendant_caches.emplace(tx7->GetHash(), std::set<Txid>{tx7->GetHash()}); 346 347 node::MiniMiner miniminer_manual(miniminer_info, descendant_caches); 348 // Use unspent outpoints to avoid entries being omitted. 349 node::MiniMiner miniminer_pool(pool, all_unspent_outpoints); 350 BOOST_CHECK(miniminer_manual.IsReadyToCalculate()); 351 BOOST_CHECK(miniminer_pool.IsReadyToCalculate()); 352 for (const auto& sequences : {miniminer_manual.Linearize(), miniminer_pool.Linearize()}) { 353 // tx6 is selected first: high feerate with no parents to bump 354 BOOST_CHECK_EQUAL(Find(sequences, tx6->GetHash()), 0); 355 356 // tx2 + tx3 CPFP are selected next 357 BOOST_CHECK_EQUAL(Find(sequences, tx2->GetHash()), 1); 358 BOOST_CHECK_EQUAL(Find(sequences, tx3->GetHash()), 1); 359 360 // tx4 + prioritised tx5 CPFP 361 BOOST_CHECK_EQUAL(Find(sequences, tx4->GetHash()), 2); 362 BOOST_CHECK_EQUAL(Find(sequences, tx5->GetHash()), 2); 363 364 BOOST_CHECK_EQUAL(Find(sequences, tx0->GetHash()), 3); 365 BOOST_CHECK_EQUAL(Find(sequences, tx1->GetHash()), 3); 366 367 368 // tx7 is selected last: low feerate with no children 369 BOOST_CHECK_EQUAL(Find(sequences, tx7->GetHash()), 4); 370 } 371 } 372 373 BOOST_FIXTURE_TEST_CASE(miniminer_overlap, TestChain100Setup) 374 { 375 /* Tx graph for `miniminer_overlap` unit test: 376 * 377 * coinbase_tx [mined] ... block-chain 378 * ------------------------------------------------- 379 * / | \ \ ... mempool 380 * / | \ | 381 * tx0 tx1 tx2 tx4 382 * [low] [med] [high] [high] 383 * \ | / | 384 * \ | / tx5 385 * \ | / [low] 386 * tx3 / \ 387 * [high] tx6 tx7 388 * [med] [high] 389 * 390 * NOTE: 391 * -> "low"/"med"/"high" denote the _absolute_ fee of each tx 392 * -> tx3 has 3 inputs and 3 outputs, all other txs have 1 input and 2 outputs 393 * -> tx3's feerate is lower than tx2's, as tx3 has more weight (due to having more inputs and outputs) 394 * 395 * -> tx2_FR = high / tx2_vsize 396 * -> tx3_FR = high / tx3_vsize 397 * -> tx3_ASFR = (low+med+high+high) / (tx0_vsize + tx1_vsize + tx2_vsize + tx3_vsize) 398 * -> tx4_FR = high / tx4_vsize 399 * -> tx6_ASFR = (high+low+med) / (tx4_vsize + tx5_vsize + tx6_vsize) 400 * -> tx7_ASFR = (high+low+high) / (tx4_vsize + tx5_vsize + tx7_vsize) */ 401 402 CTxMemPool& pool = *Assert(m_node.mempool); 403 LOCK2(::cs_main, pool.cs); 404 TestMemPoolEntryHelper entry; 405 406 // Create 3 parents of different feerates, and 1 child spending outputs from all 3 parents. 407 const auto tx0 = make_tx({COutPoint{m_coinbase_txns[0]->GetHash(), 0}}, /*num_outputs=*/2); 408 pool.addUnchecked(entry.Fee(low_fee).FromTx(tx0)); 409 const auto tx1 = make_tx({COutPoint{m_coinbase_txns[1]->GetHash(), 0}}, /*num_outputs=*/2); 410 pool.addUnchecked(entry.Fee(med_fee).FromTx(tx1)); 411 const auto tx2 = make_tx({COutPoint{m_coinbase_txns[2]->GetHash(), 0}}, /*num_outputs=*/2); 412 pool.addUnchecked(entry.Fee(high_fee).FromTx(tx2)); 413 const auto tx3 = make_tx({COutPoint{tx0->GetHash(), 0}, COutPoint{tx1->GetHash(), 0}, COutPoint{tx2->GetHash(), 0}}, /*num_outputs=*/3); 414 pool.addUnchecked(entry.Fee(high_fee).FromTx(tx3)); 415 416 // Create 1 grandparent and 1 parent, then 2 children. 417 const auto tx4 = make_tx({COutPoint{m_coinbase_txns[3]->GetHash(), 0}}, /*num_outputs=*/2); 418 pool.addUnchecked(entry.Fee(high_fee).FromTx(tx4)); 419 const auto tx5 = make_tx({COutPoint{tx4->GetHash(), 0}}, /*num_outputs=*/3); 420 pool.addUnchecked(entry.Fee(low_fee).FromTx(tx5)); 421 const auto tx6 = make_tx({COutPoint{tx5->GetHash(), 0}}, /*num_outputs=*/2); 422 pool.addUnchecked(entry.Fee(med_fee).FromTx(tx6)); 423 const auto tx7 = make_tx({COutPoint{tx5->GetHash(), 1}}, /*num_outputs=*/2); 424 pool.addUnchecked(entry.Fee(high_fee).FromTx(tx7)); 425 426 std::vector<CTransactionRef> all_transactions{tx0, tx1, tx2, tx3, tx4, tx5, tx6, tx7}; 427 std::vector<int64_t> tx_vsizes; 428 tx_vsizes.reserve(all_transactions.size()); 429 for (const auto& tx : all_transactions) tx_vsizes.push_back(GetVirtualTransactionSize(*tx)); 430 431 std::vector<COutPoint> all_unspent_outpoints({ 432 COutPoint{tx0->GetHash(), 1}, 433 COutPoint{tx1->GetHash(), 1}, 434 COutPoint{tx2->GetHash(), 1}, 435 COutPoint{tx3->GetHash(), 0}, 436 COutPoint{tx3->GetHash(), 1}, 437 COutPoint{tx3->GetHash(), 2}, 438 COutPoint{tx4->GetHash(), 1}, 439 COutPoint{tx5->GetHash(), 2}, 440 COutPoint{tx6->GetHash(), 0}, 441 COutPoint{tx7->GetHash(), 0} 442 }); 443 for (const auto& outpoint : all_unspent_outpoints) BOOST_CHECK(!pool.isSpent(outpoint)); 444 445 const auto tx2_feerate = CFeeRate(high_fee, tx_vsizes[2]); 446 const auto tx3_feerate = CFeeRate(high_fee, tx_vsizes[3]); 447 // tx3's feerate is lower than tx2's. same fee, different weight. 448 BOOST_CHECK(tx2_feerate > tx3_feerate); 449 const auto tx3_anc_feerate = CFeeRate(low_fee + med_fee + high_fee + high_fee, tx_vsizes[0] + tx_vsizes[1] + tx_vsizes[2] + tx_vsizes[3]); 450 const auto& tx3_entry{*Assert(pool.GetEntry(tx3->GetHash()))}; 451 BOOST_CHECK(tx3_anc_feerate == CFeeRate(tx3_entry.GetModFeesWithAncestors(), tx3_entry.GetSizeWithAncestors())); 452 const auto tx4_feerate = CFeeRate(high_fee, tx_vsizes[4]); 453 const auto tx6_anc_feerate = CFeeRate(high_fee + low_fee + med_fee, tx_vsizes[4] + tx_vsizes[5] + tx_vsizes[6]); 454 const auto& tx6_entry{*Assert(pool.GetEntry(tx6->GetHash()))}; 455 BOOST_CHECK(tx6_anc_feerate == CFeeRate(tx6_entry.GetModFeesWithAncestors(), tx6_entry.GetSizeWithAncestors())); 456 const auto tx7_anc_feerate = CFeeRate(high_fee + low_fee + high_fee, tx_vsizes[4] + tx_vsizes[5] + tx_vsizes[7]); 457 const auto& tx7_entry{*Assert(pool.GetEntry(tx7->GetHash()))}; 458 BOOST_CHECK(tx7_anc_feerate == CFeeRate(tx7_entry.GetModFeesWithAncestors(), tx7_entry.GetSizeWithAncestors())); 459 BOOST_CHECK(tx4_feerate > tx6_anc_feerate); 460 BOOST_CHECK(tx4_feerate > tx7_anc_feerate); 461 462 // Extremely high feerate: everybody's bumpfee is from their full ancestor set. 463 { 464 node::MiniMiner mini_miner(pool, all_unspent_outpoints); 465 const CFeeRate very_high_feerate(COIN); 466 BOOST_CHECK(tx3_anc_feerate < very_high_feerate); 467 BOOST_CHECK(mini_miner.IsReadyToCalculate()); 468 auto bump_fees = mini_miner.CalculateBumpFees(very_high_feerate); 469 BOOST_CHECK_EQUAL(bump_fees.size(), all_unspent_outpoints.size()); 470 BOOST_CHECK(!mini_miner.IsReadyToCalculate()); 471 BOOST_CHECK(sanity_check(all_transactions, bump_fees)); 472 const auto tx0_bumpfee = bump_fees.find(COutPoint{tx0->GetHash(), 1}); 473 BOOST_CHECK(tx0_bumpfee != bump_fees.end()); 474 BOOST_CHECK_EQUAL(tx0_bumpfee->second, very_high_feerate.GetFee(tx_vsizes[0]) - low_fee); 475 const auto tx3_bumpfee = bump_fees.find(COutPoint{tx3->GetHash(), 0}); 476 BOOST_CHECK(tx3_bumpfee != bump_fees.end()); 477 BOOST_CHECK_EQUAL(tx3_bumpfee->second, 478 very_high_feerate.GetFee(tx_vsizes[0] + tx_vsizes[1] + tx_vsizes[2] + tx_vsizes[3]) - (low_fee + med_fee + high_fee + high_fee)); 479 const auto tx6_bumpfee = bump_fees.find(COutPoint{tx6->GetHash(), 0}); 480 BOOST_CHECK(tx6_bumpfee != bump_fees.end()); 481 BOOST_CHECK_EQUAL(tx6_bumpfee->second, 482 very_high_feerate.GetFee(tx_vsizes[4] + tx_vsizes[5] + tx_vsizes[6]) - (high_fee + low_fee + med_fee)); 483 const auto tx7_bumpfee = bump_fees.find(COutPoint{tx7->GetHash(), 0}); 484 BOOST_CHECK(tx7_bumpfee != bump_fees.end()); 485 BOOST_CHECK_EQUAL(tx7_bumpfee->second, 486 very_high_feerate.GetFee(tx_vsizes[4] + tx_vsizes[5] + tx_vsizes[7]) - (high_fee + low_fee + high_fee)); 487 // Total fees: if spending multiple outputs from tx3 don't double-count fees. 488 node::MiniMiner mini_miner_total_tx3(pool, {COutPoint{tx3->GetHash(), 0}, COutPoint{tx3->GetHash(), 1}}); 489 BOOST_CHECK(mini_miner_total_tx3.IsReadyToCalculate()); 490 const auto tx3_bump_fee = mini_miner_total_tx3.CalculateTotalBumpFees(very_high_feerate); 491 BOOST_CHECK(!mini_miner_total_tx3.IsReadyToCalculate()); 492 BOOST_CHECK(tx3_bump_fee.has_value()); 493 BOOST_CHECK_EQUAL(tx3_bump_fee.value(), 494 very_high_feerate.GetFee(tx_vsizes[0] + tx_vsizes[1] + tx_vsizes[2] + tx_vsizes[3]) - (low_fee + med_fee + high_fee + high_fee)); 495 // Total fees: if spending both tx6 and tx7, don't double-count fees. 496 node::MiniMiner mini_miner_tx6_tx7(pool, {COutPoint{tx6->GetHash(), 0}, COutPoint{tx7->GetHash(), 0}}); 497 BOOST_CHECK(mini_miner_tx6_tx7.IsReadyToCalculate()); 498 const auto tx6_tx7_bumpfee = mini_miner_tx6_tx7.CalculateTotalBumpFees(very_high_feerate); 499 BOOST_CHECK(!mini_miner_tx6_tx7.IsReadyToCalculate()); 500 BOOST_CHECK(tx6_tx7_bumpfee.has_value()); 501 BOOST_CHECK_EQUAL(tx6_tx7_bumpfee.value(), 502 very_high_feerate.GetFee(tx_vsizes[4] + tx_vsizes[5] + tx_vsizes[6] + tx_vsizes[7]) - (high_fee + low_fee + med_fee + high_fee)); 503 } 504 // Feerate just below tx4: tx6 and tx7 have different bump fees. 505 { 506 const auto just_below_tx4 = CFeeRate(tx4_feerate.GetFeePerK() - 5); 507 node::MiniMiner mini_miner(pool, all_unspent_outpoints); 508 BOOST_CHECK(mini_miner.IsReadyToCalculate()); 509 auto bump_fees = mini_miner.CalculateBumpFees(just_below_tx4); 510 BOOST_CHECK(!mini_miner.IsReadyToCalculate()); 511 BOOST_CHECK_EQUAL(bump_fees.size(), all_unspent_outpoints.size()); 512 BOOST_CHECK(sanity_check(all_transactions, bump_fees)); 513 const auto tx6_bumpfee = bump_fees.find(COutPoint{tx6->GetHash(), 0}); 514 BOOST_CHECK(tx6_bumpfee != bump_fees.end()); 515 BOOST_CHECK_EQUAL(tx6_bumpfee->second, just_below_tx4.GetFee(tx_vsizes[5] + tx_vsizes[6]) - (low_fee + med_fee)); 516 const auto tx7_bumpfee = bump_fees.find(COutPoint{tx7->GetHash(), 0}); 517 BOOST_CHECK(tx7_bumpfee != bump_fees.end()); 518 BOOST_CHECK_EQUAL(tx7_bumpfee->second, just_below_tx4.GetFee(tx_vsizes[5] + tx_vsizes[7]) - (low_fee + high_fee)); 519 // Total fees: if spending both tx6 and tx7, don't double-count fees. 520 node::MiniMiner mini_miner_tx6_tx7(pool, {COutPoint{tx6->GetHash(), 0}, COutPoint{tx7->GetHash(), 0}}); 521 BOOST_CHECK(mini_miner_tx6_tx7.IsReadyToCalculate()); 522 const auto tx6_tx7_bumpfee = mini_miner_tx6_tx7.CalculateTotalBumpFees(just_below_tx4); 523 BOOST_CHECK(!mini_miner_tx6_tx7.IsReadyToCalculate()); 524 BOOST_CHECK(tx6_tx7_bumpfee.has_value()); 525 BOOST_CHECK_EQUAL(tx6_tx7_bumpfee.value(), just_below_tx4.GetFee(tx_vsizes[5] + tx_vsizes[6]) - (low_fee + med_fee)); 526 } 527 // Feerate between tx6 and tx7's ancestor feerates: don't need to bump tx5 because tx7 already does. 528 { 529 const auto just_above_tx6 = CFeeRate(med_fee + 10, tx_vsizes[6]); 530 BOOST_CHECK(just_above_tx6 <= CFeeRate(low_fee + high_fee, tx_vsizes[5] + tx_vsizes[7])); 531 node::MiniMiner mini_miner(pool, all_unspent_outpoints); 532 BOOST_CHECK(mini_miner.IsReadyToCalculate()); 533 auto bump_fees = mini_miner.CalculateBumpFees(just_above_tx6); 534 BOOST_CHECK(!mini_miner.IsReadyToCalculate()); 535 BOOST_CHECK_EQUAL(bump_fees.size(), all_unspent_outpoints.size()); 536 BOOST_CHECK(sanity_check(all_transactions, bump_fees)); 537 const auto tx6_bumpfee = bump_fees.find(COutPoint{tx6->GetHash(), 0}); 538 BOOST_CHECK(tx6_bumpfee != bump_fees.end()); 539 BOOST_CHECK_EQUAL(tx6_bumpfee->second, just_above_tx6.GetFee(tx_vsizes[6]) - (med_fee)); 540 const auto tx7_bumpfee = bump_fees.find(COutPoint{tx7->GetHash(), 0}); 541 BOOST_CHECK(tx7_bumpfee != bump_fees.end()); 542 BOOST_CHECK_EQUAL(tx7_bumpfee->second, 0); 543 } 544 // Check linearization order 545 std::vector<node::MiniMinerMempoolEntry> miniminer_info; 546 miniminer_info.emplace_back(tx0,/*vsize_self=*/tx_vsizes[0], /*vsize_ancestor=*/tx_vsizes[0], /*fee_self=*/low_fee, /*fee_ancestor=*/low_fee); 547 miniminer_info.emplace_back(tx1, tx_vsizes[1], tx_vsizes[1], med_fee, med_fee); 548 miniminer_info.emplace_back(tx2, tx_vsizes[2], tx_vsizes[2], high_fee, high_fee); 549 miniminer_info.emplace_back(tx3, tx_vsizes[3], tx_vsizes[0]+tx_vsizes[1]+tx_vsizes[2]+tx_vsizes[3], high_fee, low_fee+med_fee+2*high_fee); 550 miniminer_info.emplace_back(tx4, tx_vsizes[4], tx_vsizes[4], high_fee, high_fee); 551 miniminer_info.emplace_back(tx5, tx_vsizes[5], tx_vsizes[4]+tx_vsizes[5], low_fee, low_fee + high_fee); 552 miniminer_info.emplace_back(tx6, tx_vsizes[6], tx_vsizes[4]+tx_vsizes[5]+tx_vsizes[6], med_fee, high_fee+low_fee+med_fee); 553 miniminer_info.emplace_back(tx7, tx_vsizes[7], tx_vsizes[4]+tx_vsizes[5]+tx_vsizes[7], high_fee, high_fee+low_fee+high_fee); 554 555 std::map<Txid, std::set<Txid>> descendant_caches; 556 descendant_caches.emplace(tx0->GetHash(), std::set<Txid>{tx0->GetHash(), tx3->GetHash()}); 557 descendant_caches.emplace(tx1->GetHash(), std::set<Txid>{tx1->GetHash(), tx3->GetHash()}); 558 descendant_caches.emplace(tx2->GetHash(), std::set<Txid>{tx2->GetHash(), tx3->GetHash()}); 559 descendant_caches.emplace(tx3->GetHash(), std::set<Txid>{tx3->GetHash()}); 560 descendant_caches.emplace(tx4->GetHash(), std::set<Txid>{tx4->GetHash(), tx5->GetHash(), tx6->GetHash(), tx7->GetHash()}); 561 descendant_caches.emplace(tx5->GetHash(), std::set<Txid>{tx5->GetHash(), tx6->GetHash(), tx7->GetHash()}); 562 descendant_caches.emplace(tx6->GetHash(), std::set<Txid>{tx6->GetHash()}); 563 descendant_caches.emplace(tx7->GetHash(), std::set<Txid>{tx7->GetHash()}); 564 565 node::MiniMiner miniminer_manual(miniminer_info, descendant_caches); 566 // Use unspent outpoints to avoid entries being omitted. 567 node::MiniMiner miniminer_pool(pool, all_unspent_outpoints); 568 BOOST_CHECK(miniminer_manual.IsReadyToCalculate()); 569 BOOST_CHECK(miniminer_pool.IsReadyToCalculate()); 570 for (const auto& sequences : {miniminer_manual.Linearize(), miniminer_pool.Linearize()}) { 571 // tx2 and tx4 selected first: high feerate with nothing to bump 572 BOOST_CHECK_EQUAL(Find(sequences, tx4->GetHash()), 0); 573 BOOST_CHECK_EQUAL(Find(sequences, tx2->GetHash()), 1); 574 575 // tx5 + tx7 CPFP 576 BOOST_CHECK_EQUAL(Find(sequences, tx5->GetHash()), 2); 577 BOOST_CHECK_EQUAL(Find(sequences, tx7->GetHash()), 2); 578 579 // tx0 and tx1 CPFP'd by tx3 580 BOOST_CHECK_EQUAL(Find(sequences, tx0->GetHash()), 3); 581 BOOST_CHECK_EQUAL(Find(sequences, tx1->GetHash()), 3); 582 BOOST_CHECK_EQUAL(Find(sequences, tx3->GetHash()), 3); 583 584 // tx6 at medium feerate 585 BOOST_CHECK_EQUAL(Find(sequences, tx6->GetHash()), 4); 586 } 587 } 588 BOOST_FIXTURE_TEST_CASE(calculate_cluster, TestChain100Setup) 589 { 590 CTxMemPool& pool = *Assert(m_node.mempool); 591 LOCK2(cs_main, pool.cs); 592 593 // TODO this can be removed once the mempool interface uses Txid, Wtxid 594 auto convert_to_uint256_vec = [](const std::vector<Txid>& vec) -> std::vector<uint256> { 595 std::vector<uint256> out; 596 std::transform(vec.begin(), vec.end(), std::back_inserter(out), 597 [](const Txid& txid) { return txid.ToUint256(); }); 598 return out; 599 }; 600 601 // Add chain of size 500 602 TestMemPoolEntryHelper entry; 603 std::vector<Txid> chain_txids; 604 auto& lasttx = m_coinbase_txns[0]; 605 for (auto i{0}; i < 500; ++i) { 606 const auto tx = make_tx({COutPoint{lasttx->GetHash(), 0}}, /*num_outputs=*/1); 607 pool.addUnchecked(entry.Fee(CENT).FromTx(tx)); 608 chain_txids.push_back(tx->GetHash()); 609 lasttx = tx; 610 } 611 const auto cluster_500tx = pool.GatherClusters({lasttx->GetHash()}); 612 CTxMemPool::setEntries cluster_500tx_set{cluster_500tx.begin(), cluster_500tx.end()}; 613 BOOST_CHECK_EQUAL(cluster_500tx.size(), cluster_500tx_set.size()); 614 const auto vec_iters_500 = pool.GetIterVec(convert_to_uint256_vec(chain_txids)); 615 for (const auto& iter : vec_iters_500) BOOST_CHECK(cluster_500tx_set.count(iter)); 616 617 // GatherClusters stops at 500 transactions. 618 const auto tx_501 = make_tx({COutPoint{lasttx->GetHash(), 0}}, /*num_outputs=*/1); 619 pool.addUnchecked(entry.Fee(CENT).FromTx(tx_501)); 620 const auto cluster_501 = pool.GatherClusters({tx_501->GetHash()}); 621 BOOST_CHECK_EQUAL(cluster_501.size(), 0); 622 623 /* Zig Zag cluster: 624 * txp0 txp1 txp2 ... txp48 txp49 625 * \ / \ / \ \ / 626 * txc0 txc1 txc2 ... txc48 627 * Note that each transaction's ancestor size is 1 or 3, and each descendant size is 1, 2 or 3. 628 * However, all of these transactions are in the same cluster. */ 629 std::vector<Txid> zigzag_txids; 630 for (auto p{0}; p < 50; ++p) { 631 const auto txp = make_tx({COutPoint{Txid::FromUint256(GetRandHash()), 0}}, /*num_outputs=*/2); 632 pool.addUnchecked(entry.Fee(CENT).FromTx(txp)); 633 zigzag_txids.push_back(txp->GetHash()); 634 } 635 for (auto c{0}; c < 49; ++c) { 636 const auto txc = make_tx({COutPoint{zigzag_txids[c], 1}, COutPoint{zigzag_txids[c+1], 0}}, /*num_outputs=*/1); 637 pool.addUnchecked(entry.Fee(CENT).FromTx(txc)); 638 zigzag_txids.push_back(txc->GetHash()); 639 } 640 const auto vec_iters_zigzag = pool.GetIterVec(convert_to_uint256_vec(zigzag_txids)); 641 // It doesn't matter which tx we calculate cluster for, everybody is in it. 642 const std::vector<size_t> indices{0, 22, 72, zigzag_txids.size() - 1}; 643 for (const auto index : indices) { 644 const auto cluster = pool.GatherClusters({zigzag_txids[index]}); 645 BOOST_CHECK_EQUAL(cluster.size(), zigzag_txids.size()); 646 CTxMemPool::setEntries clusterset{cluster.begin(), cluster.end()}; 647 BOOST_CHECK_EQUAL(cluster.size(), clusterset.size()); 648 for (const auto& iter : vec_iters_zigzag) BOOST_CHECK(clusterset.count(iter)); 649 } 650 } 651 652 BOOST_FIXTURE_TEST_CASE(manual_ctor, TestChain100Setup) 653 { 654 CTxMemPool& pool = *Assert(m_node.mempool); 655 LOCK2(cs_main, pool.cs); 656 { 657 // 3 pairs of grandparent + fee-bumping parent, plus 1 low-feerate child. 658 // 0 fee + high fee 659 auto grandparent_zero_fee = make_tx({{m_coinbase_txns.at(0)->GetHash(), 0}}, 1); 660 auto parent_high_feerate = make_tx({{grandparent_zero_fee->GetHash(), 0}}, 1); 661 // double low fee + med fee 662 auto grandparent_double_low_feerate = make_tx({{m_coinbase_txns.at(2)->GetHash(), 0}}, 1); 663 auto parent_med_feerate = make_tx({{grandparent_double_low_feerate->GetHash(), 0}}, 1); 664 // low fee + double low fee 665 auto grandparent_low_feerate = make_tx({{m_coinbase_txns.at(1)->GetHash(), 0}}, 1); 666 auto parent_double_low_feerate = make_tx({{grandparent_low_feerate->GetHash(), 0}}, 1); 667 // child is below the cpfp package feerates because it is larger than everything else 668 auto child = make_tx({{parent_high_feerate->GetHash(), 0}, {parent_double_low_feerate->GetHash(), 0}, {parent_med_feerate->GetHash(), 0}}, 1); 669 670 // We artificially record each transaction (except the child) with a uniform vsize of 100vB. 671 const int64_t tx_vsize{100}; 672 const int64_t child_vsize{1000}; 673 674 std::vector<node::MiniMinerMempoolEntry> miniminer_info; 675 miniminer_info.emplace_back(grandparent_zero_fee, /*vsize_self=*/tx_vsize,/*vsize_ancestor=*/tx_vsize, /*fee_self=*/0,/*fee_ancestor=*/0); 676 miniminer_info.emplace_back(parent_high_feerate, tx_vsize, 2*tx_vsize, high_fee, high_fee); 677 miniminer_info.emplace_back(grandparent_double_low_feerate, tx_vsize, tx_vsize, 2*low_fee, 2*low_fee); 678 miniminer_info.emplace_back(parent_med_feerate, tx_vsize, 2*tx_vsize, med_fee, 2*low_fee+med_fee); 679 miniminer_info.emplace_back(grandparent_low_feerate, tx_vsize, tx_vsize, low_fee, low_fee); 680 miniminer_info.emplace_back(parent_double_low_feerate, tx_vsize, 2*tx_vsize, 2*low_fee, 3*low_fee); 681 miniminer_info.emplace_back(child, child_vsize, 6*tx_vsize+child_vsize, low_fee, high_fee+med_fee+6*low_fee); 682 std::map<Txid, std::set<Txid>> descendant_caches; 683 descendant_caches.emplace(grandparent_zero_fee->GetHash(), std::set<Txid>{grandparent_zero_fee->GetHash(), parent_high_feerate->GetHash(), child->GetHash()}); 684 descendant_caches.emplace(grandparent_low_feerate->GetHash(), std::set<Txid>{grandparent_low_feerate->GetHash(), parent_double_low_feerate->GetHash(), child->GetHash()}); 685 descendant_caches.emplace(grandparent_double_low_feerate->GetHash(), std::set<Txid>{grandparent_double_low_feerate->GetHash(), parent_med_feerate->GetHash(), child->GetHash()}); 686 descendant_caches.emplace(parent_high_feerate->GetHash(), std::set<Txid>{parent_high_feerate->GetHash(), child->GetHash()}); 687 descendant_caches.emplace(parent_med_feerate->GetHash(), std::set<Txid>{parent_med_feerate->GetHash(), child->GetHash()}); 688 descendant_caches.emplace(parent_double_low_feerate->GetHash(), std::set<Txid>{parent_double_low_feerate->GetHash(), child->GetHash()}); 689 descendant_caches.emplace(child->GetHash(), std::set<Txid>{child->GetHash()}); 690 691 node::MiniMiner miniminer_manual(miniminer_info, descendant_caches); 692 BOOST_CHECK(miniminer_manual.IsReadyToCalculate()); 693 const auto sequences{miniminer_manual.Linearize()}; 694 695 // CPFP zero + high 696 BOOST_CHECK_EQUAL(sequences.at(grandparent_zero_fee->GetHash()), 0); 697 BOOST_CHECK_EQUAL(sequences.at(parent_high_feerate->GetHash()), 0); 698 699 // CPFP double low + med 700 BOOST_CHECK_EQUAL(sequences.at(grandparent_double_low_feerate->GetHash()), 1); 701 BOOST_CHECK_EQUAL(sequences.at(parent_med_feerate->GetHash()), 1); 702 703 // CPFP low + double low 704 BOOST_CHECK_EQUAL(sequences.at(grandparent_low_feerate->GetHash()), 2); 705 BOOST_CHECK_EQUAL(sequences.at(parent_double_low_feerate->GetHash()), 2); 706 707 // Child at the end 708 BOOST_CHECK_EQUAL(sequences.at(child->GetHash()), 3); 709 } 710 } 711 712 BOOST_AUTO_TEST_SUITE_END()