Bitcoin Core  25.99.0
P2P Digital Currency
miniminer_tests.cpp
Go to the documentation of this file.
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 <txmempool.h>
6 #include <util/time.h>
7 
9 #include <test/util/txmempool.h>
10 
11 #include <boost/test/unit_test.hpp>
12 #include <optional>
13 #include <vector>
14 
15 BOOST_FIXTURE_TEST_SUITE(miniminer_tests, TestingSetup)
16 
17 static inline CTransactionRef make_tx(const std::vector<COutPoint>& inputs, size_t num_outputs)
18 {
20  tx.vin.resize(inputs.size());
21  tx.vout.resize(num_outputs);
22  for (size_t i = 0; i < inputs.size(); ++i) {
23  tx.vin[i].prevout = inputs[i];
24  }
25  for (size_t i = 0; i < num_outputs; ++i) {
26  tx.vout[i].scriptPubKey = CScript() << OP_11 << OP_EQUAL;
27  // The actual input and output values of these transactions don't really
28  // matter, since all accounting will use the entries' cached fees.
29  tx.vout[i].nValue = COIN;
30  }
31  return MakeTransactionRef(tx);
32 }
33 
34 static inline bool sanity_check(const std::vector<CTransactionRef>& transactions,
35  const std::map<COutPoint, CAmount>& bumpfees)
36 {
37  // No negative bumpfees.
38  for (const auto& [outpoint, fee] : bumpfees) {
39  if (fee < 0) return false;
40  if (fee == 0) continue;
41  auto outpoint_ = outpoint; // structured bindings can't be captured in C++17, so we need to use a variable
42  const bool found = std::any_of(transactions.cbegin(), transactions.cend(), [&](const auto& tx) {
43  return outpoint_.hash == tx->GetHash() && outpoint_.n < tx->vout.size();
44  });
45  if (!found) return false;
46  }
47  for (const auto& tx : transactions) {
48  // If tx has multiple outputs, they must all have the same bumpfee (if they exist).
49  if (tx->vout.size() > 1) {
50  std::set<CAmount> distinct_bumpfees;
51  for (size_t i{0}; i < tx->vout.size(); ++i) {
52  const auto bumpfee = bumpfees.find(COutPoint{tx->GetHash(), static_cast<uint32_t>(i)});
53  if (bumpfee != bumpfees.end()) distinct_bumpfees.insert(bumpfee->second);
54  }
55  if (distinct_bumpfees.size() > 1) return false;
56  }
57  }
58  return true;
59 }
60 
61 template <typename Key, typename Value>
62 Value Find(const std::map<Key, Value>& map, const Key& key)
63 {
64  auto it = map.find(key);
65  BOOST_CHECK_MESSAGE(it != map.end(), strprintf("Cannot find %s", key.ToString()));
66  return it->second;
67 }
68 
70 {
71  CTxMemPool& pool = *Assert(m_node.mempool);
72  LOCK2(::cs_main, pool.cs);
74 
75  const CAmount low_fee{CENT/2000};
76  const CAmount normal_fee{CENT/200};
77  const CAmount high_fee{CENT/10};
78 
79  // Create a parent tx1 and child tx2 with normal fees:
80  const auto tx1 = make_tx({COutPoint{m_coinbase_txns[0]->GetHash(), 0}}, /*num_outputs=*/2);
81  pool.addUnchecked(entry.Fee(normal_fee).FromTx(tx1));
82  const auto tx2 = make_tx({COutPoint{tx1->GetHash(), 0}}, /*num_outputs=*/1);
83  pool.addUnchecked(entry.Fee(normal_fee).FromTx(tx2));
84 
85  // Create a low-feerate parent tx3 and high-feerate child tx4 (cpfp)
86  const auto tx3 = make_tx({COutPoint{m_coinbase_txns[1]->GetHash(), 0}}, /*num_outputs=*/2);
87  pool.addUnchecked(entry.Fee(low_fee).FromTx(tx3));
88  const auto tx4 = make_tx({COutPoint{tx3->GetHash(), 0}}, /*num_outputs=*/1);
89  pool.addUnchecked(entry.Fee(high_fee).FromTx(tx4));
90 
91  // Create a parent tx5 and child tx6 where both have low fees
92  const auto tx5 = make_tx({COutPoint{m_coinbase_txns[2]->GetHash(), 0}}, /*num_outputs=*/2);
93  pool.addUnchecked(entry.Fee(low_fee).FromTx(tx5));
94  const auto tx6 = make_tx({COutPoint{tx5->GetHash(), 0}}, /*num_outputs=*/1);
95  pool.addUnchecked(entry.Fee(low_fee).FromTx(tx6));
96  // Make tx6's modified fee much higher than its base fee. This should cause it to pass
97  // the fee-related checks despite being low-feerate.
98  pool.PrioritiseTransaction(tx6->GetHash(), CENT/100);
99 
100  // Create a high-feerate parent tx7, low-feerate child tx8
101  const auto tx7 = make_tx({COutPoint{m_coinbase_txns[3]->GetHash(), 0}}, /*num_outputs=*/2);
102  pool.addUnchecked(entry.Fee(high_fee).FromTx(tx7));
103  const auto tx8 = make_tx({COutPoint{tx7->GetHash(), 0}}, /*num_outputs=*/1);
104  pool.addUnchecked(entry.Fee(low_fee).FromTx(tx8));
105 
106  std::vector<COutPoint> all_unspent_outpoints({
107  COutPoint{tx1->GetHash(), 1},
108  COutPoint{tx2->GetHash(), 0},
109  COutPoint{tx3->GetHash(), 1},
110  COutPoint{tx4->GetHash(), 0},
111  COutPoint{tx5->GetHash(), 1},
112  COutPoint{tx6->GetHash(), 0},
113  COutPoint{tx7->GetHash(), 1},
114  COutPoint{tx8->GetHash(), 0}
115  });
116  for (const auto& outpoint : all_unspent_outpoints) BOOST_CHECK(!pool.isSpent(outpoint));
117 
118  std::vector<COutPoint> all_spent_outpoints({
119  COutPoint{tx1->GetHash(), 0},
120  COutPoint{tx3->GetHash(), 0},
121  COutPoint{tx5->GetHash(), 0},
122  COutPoint{tx7->GetHash(), 0}
123  });
124  for (const auto& outpoint : all_spent_outpoints) BOOST_CHECK(pool.GetConflictTx(outpoint) != nullptr);
125 
126  std::vector<COutPoint> all_parent_outputs({
127  COutPoint{tx1->GetHash(), 0},
128  COutPoint{tx1->GetHash(), 1},
129  COutPoint{tx3->GetHash(), 0},
130  COutPoint{tx3->GetHash(), 1},
131  COutPoint{tx5->GetHash(), 0},
132  COutPoint{tx5->GetHash(), 1},
133  COutPoint{tx7->GetHash(), 0},
134  COutPoint{tx7->GetHash(), 1}
135  });
136 
137 
138  std::vector<CTransactionRef> all_transactions{tx1, tx2, tx3, tx4, tx5, tx6, tx7, tx8};
139  struct TxDimensions {
140  size_t vsize; CAmount mod_fee; CFeeRate feerate;
141  };
142  std::map<uint256, TxDimensions> tx_dims;
143  for (const auto& tx : all_transactions) {
144  const auto it = pool.GetIter(tx->GetHash()).value();
145  tx_dims.emplace(tx->GetHash(), TxDimensions{it->GetTxSize(), it->GetModifiedFee(),
146  CFeeRate(it->GetModifiedFee(), it->GetTxSize())});
147  }
148 
149  const std::vector<CFeeRate> various_normal_feerates({CFeeRate(0), CFeeRate(500), CFeeRate(999),
150  CFeeRate(1000), CFeeRate(2000), CFeeRate(2500),
151  CFeeRate(3333), CFeeRate(7800), CFeeRate(11199),
152  CFeeRate(23330), CFeeRate(50000), CFeeRate(5*CENT)});
153 
154  // All nonexistent entries have a bumpfee of zero, regardless of feerate
155  std::vector<COutPoint> nonexistent_outpoints({ COutPoint{GetRandHash(), 0}, COutPoint{GetRandHash(), 3} });
156  for (const auto& outpoint : nonexistent_outpoints) BOOST_CHECK(!pool.isSpent(outpoint));
157  for (const auto& feerate : various_normal_feerates) {
158  node::MiniMiner mini_miner(pool, nonexistent_outpoints);
159  BOOST_CHECK(mini_miner.IsReadyToCalculate());
160  auto bump_fees = mini_miner.CalculateBumpFees(feerate);
161  BOOST_CHECK(!mini_miner.IsReadyToCalculate());
162  BOOST_CHECK(sanity_check(all_transactions, bump_fees));
163  BOOST_CHECK(bump_fees.size() == nonexistent_outpoints.size());
164  for (const auto& outpoint: nonexistent_outpoints) {
165  auto it = bump_fees.find(outpoint);
166  BOOST_CHECK(it != bump_fees.end());
167  BOOST_CHECK_EQUAL(it->second, 0);
168  }
169  }
170 
171  // Gather bump fees for all available UTXOs.
172  for (const auto& target_feerate : various_normal_feerates) {
173  node::MiniMiner mini_miner(pool, all_unspent_outpoints);
174  BOOST_CHECK(mini_miner.IsReadyToCalculate());
175  auto bump_fees = mini_miner.CalculateBumpFees(target_feerate);
176  BOOST_CHECK(!mini_miner.IsReadyToCalculate());
177  BOOST_CHECK(sanity_check(all_transactions, bump_fees));
178  BOOST_CHECK_EQUAL(bump_fees.size(), all_unspent_outpoints.size());
179 
180  // Check tx1 bumpfee: no other bumper.
181  const TxDimensions& tx1_dimensions = tx_dims.find(tx1->GetHash())->second;
182  CAmount bumpfee1 = Find(bump_fees, COutPoint{tx1->GetHash(), 1});
183  if (target_feerate <= tx1_dimensions.feerate) {
184  BOOST_CHECK_EQUAL(bumpfee1, 0);
185  } else {
186  // Difference is fee to bump tx1 from current to target feerate.
187  BOOST_CHECK_EQUAL(bumpfee1, target_feerate.GetFee(tx1_dimensions.vsize) - tx1_dimensions.mod_fee);
188  }
189 
190  // Check tx3 bumpfee: assisted by tx4.
191  const TxDimensions& tx3_dimensions = tx_dims.find(tx3->GetHash())->second;
192  const TxDimensions& tx4_dimensions = tx_dims.find(tx4->GetHash())->second;
193  const CFeeRate tx3_feerate = CFeeRate(tx3_dimensions.mod_fee + tx4_dimensions.mod_fee, tx3_dimensions.vsize + tx4_dimensions.vsize);
194  CAmount bumpfee3 = Find(bump_fees, COutPoint{tx3->GetHash(), 1});
195  if (target_feerate <= tx3_feerate) {
196  // As long as target feerate is below tx4's ancestor feerate, there is no bump fee.
197  BOOST_CHECK_EQUAL(bumpfee3, 0);
198  } else {
199  // Difference is fee to bump tx3 from current to target feerate, without tx4.
200  BOOST_CHECK_EQUAL(bumpfee3, target_feerate.GetFee(tx3_dimensions.vsize) - tx3_dimensions.mod_fee);
201  }
202 
203  // If tx6’s modified fees are sufficient for tx5 and tx6 to be picked
204  // into the block, our prospective new transaction would not need to
205  // bump tx5 when using tx5’s second output. If however even tx6’s
206  // modified fee (which essentially indicates "effective feerate") is
207  // not sufficient to bump tx5, using the second output of tx5 would
208  // require our transaction to bump tx5 from scratch since we evaluate
209  // transaction packages per ancestor sets and do not consider multiple
210  // children’s fees.
211  const TxDimensions& tx5_dimensions = tx_dims.find(tx5->GetHash())->second;
212  const TxDimensions& tx6_dimensions = tx_dims.find(tx6->GetHash())->second;
213  const CFeeRate tx5_feerate = CFeeRate(tx5_dimensions.mod_fee + tx6_dimensions.mod_fee, tx5_dimensions.vsize + tx6_dimensions.vsize);
214  CAmount bumpfee5 = Find(bump_fees, COutPoint{tx5->GetHash(), 1});
215  if (target_feerate <= tx5_feerate) {
216  // As long as target feerate is below tx6's ancestor feerate, there is no bump fee.
217  BOOST_CHECK_EQUAL(bumpfee5, 0);
218  } else {
219  // Difference is fee to bump tx5 from current to target feerate, without tx6.
220  BOOST_CHECK_EQUAL(bumpfee5, target_feerate.GetFee(tx5_dimensions.vsize) - tx5_dimensions.mod_fee);
221  }
222  }
223  // Spent outpoints should usually not be requested as they would not be
224  // considered available. However, when they are explicitly requested, we
225  // can calculate their bumpfee to facilitate RBF-replacements
226  for (const auto& target_feerate : various_normal_feerates) {
227  node::MiniMiner mini_miner_all_spent(pool, all_spent_outpoints);
228  BOOST_CHECK(mini_miner_all_spent.IsReadyToCalculate());
229  auto bump_fees_all_spent = mini_miner_all_spent.CalculateBumpFees(target_feerate);
230  BOOST_CHECK(!mini_miner_all_spent.IsReadyToCalculate());
231  BOOST_CHECK_EQUAL(bump_fees_all_spent.size(), all_spent_outpoints.size());
232  node::MiniMiner mini_miner_all_parents(pool, all_parent_outputs);
233  BOOST_CHECK(mini_miner_all_parents.IsReadyToCalculate());
234  auto bump_fees_all_parents = mini_miner_all_parents.CalculateBumpFees(target_feerate);
235  BOOST_CHECK(!mini_miner_all_parents.IsReadyToCalculate());
236  BOOST_CHECK_EQUAL(bump_fees_all_parents.size(), all_parent_outputs.size());
237  for (auto& bump_fees : {bump_fees_all_parents, bump_fees_all_spent}) {
238  // For all_parents case, both outputs from the parent should have the same bump fee,
239  // even though only one of them is in a to-be-replaced transaction.
240  BOOST_CHECK(sanity_check(all_transactions, bump_fees));
241 
242  // Check tx1 bumpfee: no other bumper.
243  const TxDimensions& tx1_dimensions = tx_dims.find(tx1->GetHash())->second;
244  CAmount it1_spent = Find(bump_fees, COutPoint{tx1->GetHash(), 0});
245  if (target_feerate <= tx1_dimensions.feerate) {
246  BOOST_CHECK_EQUAL(it1_spent, 0);
247  } else {
248  // Difference is fee to bump tx1 from current to target feerate.
249  BOOST_CHECK_EQUAL(it1_spent, target_feerate.GetFee(tx1_dimensions.vsize) - tx1_dimensions.mod_fee);
250  }
251 
252  // Check tx3 bumpfee: no other bumper, because tx4 is to-be-replaced.
253  const TxDimensions& tx3_dimensions = tx_dims.find(tx3->GetHash())->second;
254  const CFeeRate tx3_feerate_unbumped = tx3_dimensions.feerate;
255  auto it3_spent = Find(bump_fees, COutPoint{tx3->GetHash(), 0});
256  if (target_feerate <= tx3_feerate_unbumped) {
257  BOOST_CHECK_EQUAL(it3_spent, 0);
258  } else {
259  // Difference is fee to bump tx3 from current to target feerate, without tx4.
260  BOOST_CHECK_EQUAL(it3_spent, target_feerate.GetFee(tx3_dimensions.vsize) - tx3_dimensions.mod_fee);
261  }
262 
263  // Check tx5 bumpfee: no other bumper, because tx6 is to-be-replaced.
264  const TxDimensions& tx5_dimensions = tx_dims.find(tx5->GetHash())->second;
265  const CFeeRate tx5_feerate_unbumped = tx5_dimensions.feerate;
266  auto it5_spent = Find(bump_fees, COutPoint{tx5->GetHash(), 0});
267  if (target_feerate <= tx5_feerate_unbumped) {
268  BOOST_CHECK_EQUAL(it5_spent, 0);
269  } else {
270  // Difference is fee to bump tx5 from current to target feerate, without tx6.
271  BOOST_CHECK_EQUAL(it5_spent, target_feerate.GetFee(tx5_dimensions.vsize) - tx5_dimensions.mod_fee);
272  }
273  }
274  }
275 }
276 
278 {
279  CTxMemPool& pool = *Assert(m_node.mempool);
280  LOCK2(::cs_main, pool.cs);
282 
283  const CAmount low_fee{CENT/2000};
284  const CAmount med_fee{CENT/200};
285  const CAmount high_fee{CENT/10};
286 
287  // Create 3 parents of different feerates, and 1 child spending from all 3.
288  const auto tx1 = make_tx({COutPoint{m_coinbase_txns[0]->GetHash(), 0}}, /*num_outputs=*/2);
289  pool.addUnchecked(entry.Fee(low_fee).FromTx(tx1));
290  const auto tx2 = make_tx({COutPoint{m_coinbase_txns[1]->GetHash(), 0}}, /*num_outputs=*/2);
291  pool.addUnchecked(entry.Fee(med_fee).FromTx(tx2));
292  const auto tx3 = make_tx({COutPoint{m_coinbase_txns[2]->GetHash(), 0}}, /*num_outputs=*/2);
293  pool.addUnchecked(entry.Fee(high_fee).FromTx(tx3));
294  const auto tx4 = make_tx({COutPoint{tx1->GetHash(), 0}, COutPoint{tx2->GetHash(), 0}, COutPoint{tx3->GetHash(), 0}}, /*num_outputs=*/3);
295  pool.addUnchecked(entry.Fee(high_fee).FromTx(tx4));
296 
297  // Create 1 grandparent and 1 parent, then 2 children.
298  const auto tx5 = make_tx({COutPoint{m_coinbase_txns[3]->GetHash(), 0}}, /*num_outputs=*/2);
299  pool.addUnchecked(entry.Fee(high_fee).FromTx(tx5));
300  const auto tx6 = make_tx({COutPoint{tx5->GetHash(), 0}}, /*num_outputs=*/3);
301  pool.addUnchecked(entry.Fee(low_fee).FromTx(tx6));
302  const auto tx7 = make_tx({COutPoint{tx6->GetHash(), 0}}, /*num_outputs=*/2);
303  pool.addUnchecked(entry.Fee(med_fee).FromTx(tx7));
304  const auto tx8 = make_tx({COutPoint{tx6->GetHash(), 1}}, /*num_outputs=*/2);
305  pool.addUnchecked(entry.Fee(high_fee).FromTx(tx8));
306 
307  std::vector<CTransactionRef> all_transactions{tx1, tx2, tx3, tx4, tx5, tx6, tx7, tx8};
308  std::vector<int64_t> tx_vsizes;
309  tx_vsizes.reserve(all_transactions.size());
310  for (const auto& tx : all_transactions) tx_vsizes.push_back(GetVirtualTransactionSize(*tx));
311 
312  std::vector<COutPoint> all_unspent_outpoints({
313  COutPoint{tx1->GetHash(), 1},
314  COutPoint{tx2->GetHash(), 1},
315  COutPoint{tx3->GetHash(), 1},
316  COutPoint{tx4->GetHash(), 0},
317  COutPoint{tx4->GetHash(), 1},
318  COutPoint{tx4->GetHash(), 2},
319  COutPoint{tx5->GetHash(), 1},
320  COutPoint{tx6->GetHash(), 2},
321  COutPoint{tx7->GetHash(), 0},
322  COutPoint{tx8->GetHash(), 0}
323  });
324  for (const auto& outpoint : all_unspent_outpoints) BOOST_CHECK(!pool.isSpent(outpoint));
325 
326  const auto tx3_feerate = CFeeRate(high_fee, tx_vsizes[2]);
327  const auto tx4_feerate = CFeeRate(high_fee, tx_vsizes[3]);
328  // tx4's feerate is lower than tx3's. same fee, different weight.
329  BOOST_CHECK(tx3_feerate > tx4_feerate);
330  const auto tx4_anc_feerate = CFeeRate(low_fee + med_fee + high_fee, tx_vsizes[0] + tx_vsizes[1] + tx_vsizes[3]);
331  const auto tx5_feerate = CFeeRate(high_fee, tx_vsizes[4]);
332  const auto tx7_anc_feerate = CFeeRate(low_fee + med_fee, tx_vsizes[5] + tx_vsizes[6]);
333  const auto tx8_anc_feerate = CFeeRate(low_fee + high_fee, tx_vsizes[5] + tx_vsizes[7]);
334  BOOST_CHECK(tx5_feerate > tx7_anc_feerate);
335  BOOST_CHECK(tx5_feerate > tx8_anc_feerate);
336 
337  // Extremely high feerate: everybody's bumpfee is from their full ancestor set.
338  {
339  node::MiniMiner mini_miner(pool, all_unspent_outpoints);
340  const CFeeRate very_high_feerate(COIN);
341  BOOST_CHECK(tx4_anc_feerate < very_high_feerate);
342  BOOST_CHECK(mini_miner.IsReadyToCalculate());
343  auto bump_fees = mini_miner.CalculateBumpFees(very_high_feerate);
344  BOOST_CHECK_EQUAL(bump_fees.size(), all_unspent_outpoints.size());
345  BOOST_CHECK(!mini_miner.IsReadyToCalculate());
346  BOOST_CHECK(sanity_check(all_transactions, bump_fees));
347  const auto tx1_bumpfee = bump_fees.find(COutPoint{tx1->GetHash(), 1});
348  BOOST_CHECK(tx1_bumpfee != bump_fees.end());
349  BOOST_CHECK_EQUAL(tx1_bumpfee->second, very_high_feerate.GetFee(tx_vsizes[0]) - low_fee);
350  const auto tx4_bumpfee = bump_fees.find(COutPoint{tx4->GetHash(), 0});
351  BOOST_CHECK(tx4_bumpfee != bump_fees.end());
352  BOOST_CHECK_EQUAL(tx4_bumpfee->second,
353  very_high_feerate.GetFee(tx_vsizes[0] + tx_vsizes[1] + tx_vsizes[2] + tx_vsizes[3]) - (low_fee + med_fee + high_fee + high_fee));
354  const auto tx7_bumpfee = bump_fees.find(COutPoint{tx7->GetHash(), 0});
355  BOOST_CHECK(tx7_bumpfee != bump_fees.end());
356  BOOST_CHECK_EQUAL(tx7_bumpfee->second,
357  very_high_feerate.GetFee(tx_vsizes[4] + tx_vsizes[5] + tx_vsizes[6]) - (high_fee + low_fee + med_fee));
358  const auto tx8_bumpfee = bump_fees.find(COutPoint{tx8->GetHash(), 0});
359  BOOST_CHECK(tx8_bumpfee != bump_fees.end());
360  BOOST_CHECK_EQUAL(tx8_bumpfee->second,
361  very_high_feerate.GetFee(tx_vsizes[4] + tx_vsizes[5] + tx_vsizes[7]) - (high_fee + low_fee + high_fee));
362  // Total fees: if spending multiple outputs from tx4 don't double-count fees.
363  node::MiniMiner mini_miner_total_tx4(pool, {COutPoint{tx4->GetHash(), 0}, COutPoint{tx4->GetHash(), 1}});
364  BOOST_CHECK(mini_miner_total_tx4.IsReadyToCalculate());
365  const auto tx4_bump_fee = mini_miner_total_tx4.CalculateTotalBumpFees(very_high_feerate);
366  BOOST_CHECK(!mini_miner_total_tx4.IsReadyToCalculate());
367  BOOST_CHECK(tx4_bump_fee.has_value());
368  BOOST_CHECK_EQUAL(tx4_bump_fee.value(),
369  very_high_feerate.GetFee(tx_vsizes[0] + tx_vsizes[1] + tx_vsizes[2] + tx_vsizes[3]) - (low_fee + med_fee + high_fee + high_fee));
370  // Total fees: if spending both tx7 and tx8, don't double-count fees.
371  node::MiniMiner mini_miner_tx7_tx8(pool, {COutPoint{tx7->GetHash(), 0}, COutPoint{tx8->GetHash(), 0}});
372  BOOST_CHECK(mini_miner_tx7_tx8.IsReadyToCalculate());
373  const auto tx7_tx8_bumpfee = mini_miner_tx7_tx8.CalculateTotalBumpFees(very_high_feerate);
374  BOOST_CHECK(!mini_miner_tx7_tx8.IsReadyToCalculate());
375  BOOST_CHECK(tx7_tx8_bumpfee.has_value());
376  BOOST_CHECK_EQUAL(tx7_tx8_bumpfee.value(),
377  very_high_feerate.GetFee(tx_vsizes[4] + tx_vsizes[5] + tx_vsizes[6] + tx_vsizes[7]) - (high_fee + low_fee + med_fee + high_fee));
378  }
379  // Feerate just below tx5: tx7 and tx8 have different bump fees.
380  {
381  const auto just_below_tx5 = CFeeRate(tx5_feerate.GetFeePerK() - 5);
382  node::MiniMiner mini_miner(pool, all_unspent_outpoints);
383  BOOST_CHECK(mini_miner.IsReadyToCalculate());
384  auto bump_fees = mini_miner.CalculateBumpFees(just_below_tx5);
385  BOOST_CHECK(!mini_miner.IsReadyToCalculate());
386  BOOST_CHECK_EQUAL(bump_fees.size(), all_unspent_outpoints.size());
387  BOOST_CHECK(sanity_check(all_transactions, bump_fees));
388  const auto tx7_bumpfee = bump_fees.find(COutPoint{tx7->GetHash(), 0});
389  BOOST_CHECK(tx7_bumpfee != bump_fees.end());
390  BOOST_CHECK_EQUAL(tx7_bumpfee->second, just_below_tx5.GetFee(tx_vsizes[5] + tx_vsizes[6]) - (low_fee + med_fee));
391  const auto tx8_bumpfee = bump_fees.find(COutPoint{tx8->GetHash(), 0});
392  BOOST_CHECK(tx8_bumpfee != bump_fees.end());
393  BOOST_CHECK_EQUAL(tx8_bumpfee->second, just_below_tx5.GetFee(tx_vsizes[5] + tx_vsizes[7]) - (low_fee + high_fee));
394  // Total fees: if spending both tx7 and tx8, don't double-count fees.
395  node::MiniMiner mini_miner_tx7_tx8(pool, {COutPoint{tx7->GetHash(), 0}, COutPoint{tx8->GetHash(), 0}});
396  BOOST_CHECK(mini_miner_tx7_tx8.IsReadyToCalculate());
397  const auto tx7_tx8_bumpfee = mini_miner_tx7_tx8.CalculateTotalBumpFees(just_below_tx5);
398  BOOST_CHECK(!mini_miner_tx7_tx8.IsReadyToCalculate());
399  BOOST_CHECK(tx7_tx8_bumpfee.has_value());
400  BOOST_CHECK_EQUAL(tx7_tx8_bumpfee.value(), just_below_tx5.GetFee(tx_vsizes[5] + tx_vsizes[6]) - (low_fee + med_fee));
401  }
402  // Feerate between tx7 and tx8's ancestor feerates: don't need to bump tx6 because tx8 already does.
403  {
404  const auto just_above_tx7 = CFeeRate(med_fee + 10, tx_vsizes[6]);
405  BOOST_CHECK(just_above_tx7 <= CFeeRate(low_fee + high_fee, tx_vsizes[5] + tx_vsizes[7]));
406  node::MiniMiner mini_miner(pool, all_unspent_outpoints);
407  BOOST_CHECK(mini_miner.IsReadyToCalculate());
408  auto bump_fees = mini_miner.CalculateBumpFees(just_above_tx7);
409  BOOST_CHECK(!mini_miner.IsReadyToCalculate());
410  BOOST_CHECK_EQUAL(bump_fees.size(), all_unspent_outpoints.size());
411  BOOST_CHECK(sanity_check(all_transactions, bump_fees));
412  const auto tx7_bumpfee = bump_fees.find(COutPoint{tx7->GetHash(), 0});
413  BOOST_CHECK(tx7_bumpfee != bump_fees.end());
414  BOOST_CHECK_EQUAL(tx7_bumpfee->second, just_above_tx7.GetFee(tx_vsizes[6]) - (med_fee));
415  const auto tx8_bumpfee = bump_fees.find(COutPoint{tx8->GetHash(), 0});
416  BOOST_CHECK(tx8_bumpfee != bump_fees.end());
417  BOOST_CHECK_EQUAL(tx8_bumpfee->second, 0);
418  }
419 }
421 {
422  CTxMemPool& pool = *Assert(m_node.mempool);
423  LOCK2(cs_main, pool.cs);
424 
425  // Add chain of size 500
427  std::vector<uint256> chain_txids;
428  auto& lasttx = m_coinbase_txns[0];
429  for (auto i{0}; i < 500; ++i) {
430  const auto tx = make_tx({COutPoint{lasttx->GetHash(), 0}}, /*num_outputs=*/1);
431  pool.addUnchecked(entry.Fee(CENT).FromTx(tx));
432  chain_txids.push_back(tx->GetHash());
433  lasttx = tx;
434  }
435  const auto cluster_500tx = pool.GatherClusters({lasttx->GetHash()});
436  CTxMemPool::setEntries cluster_500tx_set{cluster_500tx.begin(), cluster_500tx.end()};
437  BOOST_CHECK_EQUAL(cluster_500tx.size(), cluster_500tx_set.size());
438  const auto vec_iters_500 = pool.GetIterVec(chain_txids);
439  for (const auto& iter : vec_iters_500) BOOST_CHECK(cluster_500tx_set.count(iter));
440 
441  // GatherClusters stops at 500 transactions.
442  const auto tx_501 = make_tx({COutPoint{lasttx->GetHash(), 0}}, /*num_outputs=*/1);
443  pool.addUnchecked(entry.Fee(CENT).FromTx(tx_501));
444  const auto cluster_501 = pool.GatherClusters({tx_501->GetHash()});
445  BOOST_CHECK_EQUAL(cluster_501.size(), 0);
446 
447  // Zig Zag cluster:
448  // txp0 txp1 txp2 ... txp48 txp49
449  // \ / \ / \ \ /
450  // txc0 txc1 txc2 ... txc48
451  // Note that each transaction's ancestor size is 1 or 3, and each descendant size is 1, 2 or 3.
452  // However, all of these transactions are in the same cluster.
453  std::vector<uint256> zigzag_txids;
454  for (auto p{0}; p < 50; ++p) {
455  const auto txp = make_tx({COutPoint{GetRandHash(), 0}}, /*num_outputs=*/2);
456  pool.addUnchecked(entry.Fee(CENT).FromTx(txp));
457  zigzag_txids.push_back(txp->GetHash());
458  }
459  for (auto c{0}; c < 49; ++c) {
460  const auto txc = make_tx({COutPoint{zigzag_txids[c], 1}, COutPoint{zigzag_txids[c+1], 0}}, /*num_outputs=*/1);
461  pool.addUnchecked(entry.Fee(CENT).FromTx(txc));
462  zigzag_txids.push_back(txc->GetHash());
463  }
464  const auto vec_iters_zigzag = pool.GetIterVec(zigzag_txids);
465  // It doesn't matter which tx we calculate cluster for, everybody is in it.
466  const std::vector<size_t> indeces{0, 22, 72, zigzag_txids.size() - 1};
467  for (const auto index : indeces) {
468  const auto cluster = pool.GatherClusters({zigzag_txids[index]});
469  BOOST_CHECK_EQUAL(cluster.size(), zigzag_txids.size());
470  CTxMemPool::setEntries clusterset{cluster.begin(), cluster.end()};
471  BOOST_CHECK_EQUAL(cluster.size(), clusterset.size());
472  for (const auto& iter : vec_iters_zigzag) BOOST_CHECK(clusterset.count(iter));
473  }
474 }
475 
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition: amount.h:15
node::NodeContext m_node
Definition: bitcoin-gui.cpp:37
#define Assert(val)
Identity function.
Definition: check.h:73
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
Definition: feerate.h:33
CAmount GetFee(uint32_t num_bytes) const
Return the fee in satoshis for the given vsize in vbytes.
Definition: feerate.cpp:23
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:36
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:411
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:316
void PrioritiseTransaction(const uint256 &hash, const CAmount &nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
Definition: txmempool.cpp:856
std::vector< txiter > GetIterVec(const std::vector< uint256 > &txids) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Translate a list of hashes into a list of mempool iterators to avoid repeated lookups.
Definition: txmempool.cpp:922
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:405
void check(const CCoinsViewCache &active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(void addUnchecked(const CTxMemPoolEntry &entry, bool validFeeEstimate=true) EXCLUSIVE_LOCKS_REQUIRED(cs
If sanity-checking is turned on, check makes sure the pool is consistent (does not contain two transa...
Definition: txmempool.h:488
std::optional< txiter > GetIter(const uint256 &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns an iterator to the given hash, if found.
Definition: txmempool.cpp:905
std::vector< txiter > GatherClusters(const std::vector< uint256 > &txids) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Collect the entire cluster of connected transactions for each transaction in txids.
Definition: txmempool.cpp:1177
std::set< txiter, CompareIteratorByHash > setEntries
Definition: txmempool.h:411
const CTransaction * GetConflictTx(const COutPoint &prevout) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Get the transaction in the pool that spends the same prevout.
Definition: txmempool.cpp:899
bool isSpent(const COutPoint &outpoint) const
Definition: txmempool.cpp:404
A minimal version of BlockAssembler.
Definition: mini_miner.h:56
std::map< COutPoint, CAmount > CalculateBumpFees(const CFeeRate &target_feerate)
Construct a new block template and, for each outpoint corresponding to a transaction that did not mak...
Definition: mini_miner.cpp:251
bool IsReadyToCalculate() const
Returns true if CalculateBumpFees may be called, false if not.
Definition: mini_miner.h:98
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:8
BOOST_AUTO_TEST_SUITE_END()
static bool sanity_check(const std::vector< CTransactionRef > &transactions, const std::map< COutPoint, CAmount > &bumpfees)
static CTransactionRef make_tx(const std::vector< COutPoint > &inputs, size_t num_outputs)
BOOST_FIXTURE_TEST_CASE(miniminer_1p1c, TestChain100Setup)
Value Find(const std::map< Key, Value > &map, const Key &key)
RPCHelpMan bumpfee()
Definition: spend.cpp:1172
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost, unsigned int bytes_per_sigop)
Compute the virtual transaction size (weight reinterpreted as bytes).
Definition: policy.cpp:295
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:422
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:421
uint256 GetRandHash() noexcept
Definition: random.cpp:570
@ OP_EQUAL
Definition: script.h:142
@ OP_11
Definition: script.h:90
static constexpr CAmount CENT
Definition: setup_common.h:75
A mutable version of CTransaction.
Definition: transaction.h:380
std::vector< CTxOut > vout
Definition: transaction.h:382
std::vector< CTxIn > vin
Definition: transaction.h:381
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition: setup_common.h:129
Definition: txmempool.h:17
CTxMemPoolEntry FromTx(const CMutableTransaction &tx) const
Definition: txmempool.cpp:30
TestMemPoolEntryHelper & Fee(CAmount _fee)
Definition: txmempool.h:30
Testing setup that configures a complete environment.
Definition: setup_common.h:108
std::unique_ptr< CTxMemPool > mempool
Definition: context.h:52
#define LOCK2(cs1, cs2)
Definition: sync.h:259
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1162