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 <random.h>
6 #include <txmempool.h>
7 #include <util/time.h>
8 
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 static inline CTransactionRef make_tx(const std::vector<COutPoint>& inputs, size_t num_outputs)
19 {
21  tx.vin.resize(inputs.size());
22  tx.vout.resize(num_outputs);
23  for (size_t i = 0; i < inputs.size(); ++i) {
24  tx.vin[i].prevout = inputs[i];
25  }
26  for (size_t i = 0; i < num_outputs; ++i) {
27  tx.vout[i].scriptPubKey = CScript() << OP_11 << OP_EQUAL;
28  // The actual input and output values of these transactions don't really
29  // matter, since all accounting will use the entries' cached fees.
30  tx.vout[i].nValue = COIN;
31  }
32  return MakeTransactionRef(tx);
33 }
34 
35 static inline bool sanity_check(const std::vector<CTransactionRef>& transactions,
36  const std::map<COutPoint, CAmount>& bumpfees)
37 {
38  // No negative bumpfees.
39  for (const auto& [outpoint, fee] : bumpfees) {
40  if (fee < 0) return false;
41  if (fee == 0) continue;
42  auto outpoint_ = outpoint; // structured bindings can't be captured in C++17, so we need to use a variable
43  const bool found = std::any_of(transactions.cbegin(), transactions.cend(), [&](const auto& tx) {
44  return outpoint_.hash == tx->GetHash() && outpoint_.n < tx->vout.size();
45  });
46  if (!found) return false;
47  }
48  for (const auto& tx : transactions) {
49  // If tx has multiple outputs, they must all have the same bumpfee (if they exist).
50  if (tx->vout.size() > 1) {
51  std::set<CAmount> distinct_bumpfees;
52  for (size_t i{0}; i < tx->vout.size(); ++i) {
53  const auto bumpfee = bumpfees.find(COutPoint{tx->GetHash(), static_cast<uint32_t>(i)});
54  if (bumpfee != bumpfees.end()) distinct_bumpfees.insert(bumpfee->second);
55  }
56  if (distinct_bumpfees.size() > 1) return false;
57  }
58  }
59  return true;
60 }
61 
62 template <typename Key, typename Value>
63 Value Find(const std::map<Key, Value>& map, const Key& key)
64 {
65  auto it = map.find(key);
66  BOOST_CHECK_MESSAGE(it != map.end(), strprintf("Cannot find %s", key.ToString()));
67  return it->second;
68 }
69 
71 {
72  CTxMemPool& pool = *Assert(m_node.mempool);
73  LOCK2(::cs_main, pool.cs);
75 
76  const CAmount low_fee{CENT/2000};
77  const CAmount normal_fee{CENT/200};
78  const CAmount high_fee{CENT/10};
79 
80  // Create a parent tx0 and child tx1 with normal fees:
81  const auto tx0 = make_tx({COutPoint{m_coinbase_txns[0]->GetHash(), 0}}, /*num_outputs=*/2);
82  pool.addUnchecked(entry.Fee(normal_fee).FromTx(tx0));
83  const auto tx1 = make_tx({COutPoint{tx0->GetHash(), 0}}, /*num_outputs=*/1);
84  pool.addUnchecked(entry.Fee(normal_fee).FromTx(tx1));
85 
86  // Create a low-feerate parent tx2 and high-feerate child tx3 (cpfp)
87  const auto tx2 = make_tx({COutPoint{m_coinbase_txns[1]->GetHash(), 0}}, /*num_outputs=*/2);
88  pool.addUnchecked(entry.Fee(low_fee).FromTx(tx2));
89  const auto tx3 = make_tx({COutPoint{tx2->GetHash(), 0}}, /*num_outputs=*/1);
90  pool.addUnchecked(entry.Fee(high_fee).FromTx(tx3));
91 
92  // Create a parent tx4 and child tx5 where both have low fees
93  const auto tx4 = make_tx({COutPoint{m_coinbase_txns[2]->GetHash(), 0}}, /*num_outputs=*/2);
94  pool.addUnchecked(entry.Fee(low_fee).FromTx(tx4));
95  const auto tx5 = make_tx({COutPoint{tx4->GetHash(), 0}}, /*num_outputs=*/1);
96  pool.addUnchecked(entry.Fee(low_fee).FromTx(tx5));
97  // Make tx5's modified fee much higher than its base fee. This should cause it to pass
98  // the fee-related checks despite being low-feerate.
99  pool.PrioritiseTransaction(tx5->GetHash(), CENT/100);
100 
101  // Create a high-feerate parent tx6, low-feerate child tx7
102  const auto tx6 = make_tx({COutPoint{m_coinbase_txns[3]->GetHash(), 0}}, /*num_outputs=*/2);
103  pool.addUnchecked(entry.Fee(high_fee).FromTx(tx6));
104  const auto tx7 = make_tx({COutPoint{tx6->GetHash(), 0}}, /*num_outputs=*/1);
105  pool.addUnchecked(entry.Fee(low_fee).FromTx(tx7));
106 
107  std::vector<COutPoint> all_unspent_outpoints({
108  COutPoint{tx0->GetHash(), 1},
109  COutPoint{tx1->GetHash(), 0},
110  COutPoint{tx2->GetHash(), 1},
111  COutPoint{tx3->GetHash(), 0},
112  COutPoint{tx4->GetHash(), 1},
113  COutPoint{tx5->GetHash(), 0},
114  COutPoint{tx6->GetHash(), 1},
115  COutPoint{tx7->GetHash(), 0}
116  });
117  for (const auto& outpoint : all_unspent_outpoints) BOOST_CHECK(!pool.isSpent(outpoint));
118 
119  std::vector<COutPoint> all_spent_outpoints({
120  COutPoint{tx0->GetHash(), 0},
121  COutPoint{tx2->GetHash(), 0},
122  COutPoint{tx4->GetHash(), 0},
123  COutPoint{tx6->GetHash(), 0}
124  });
125  for (const auto& outpoint : all_spent_outpoints) BOOST_CHECK(pool.GetConflictTx(outpoint) != nullptr);
126 
127  std::vector<COutPoint> all_parent_outputs({
128  COutPoint{tx0->GetHash(), 0},
129  COutPoint{tx0->GetHash(), 1},
130  COutPoint{tx2->GetHash(), 0},
131  COutPoint{tx2->GetHash(), 1},
132  COutPoint{tx4->GetHash(), 0},
133  COutPoint{tx4->GetHash(), 1},
134  COutPoint{tx6->GetHash(), 0},
135  COutPoint{tx6->GetHash(), 1}
136  });
137 
138 
139  std::vector<CTransactionRef> all_transactions{tx0, tx1, tx2, tx3, tx4, tx5, tx6, tx7};
140  struct TxDimensions {
141  int32_t vsize; CAmount mod_fee; CFeeRate feerate;
142  };
143  std::map<uint256, TxDimensions> tx_dims;
144  for (const auto& tx : all_transactions) {
145  const auto it = pool.GetIter(tx->GetHash()).value();
146  tx_dims.emplace(tx->GetHash(), TxDimensions{it->GetTxSize(), it->GetModifiedFee(),
147  CFeeRate(it->GetModifiedFee(), it->GetTxSize())});
148  }
149 
150  const std::vector<CFeeRate> various_normal_feerates({CFeeRate(0), CFeeRate(500), CFeeRate(999),
151  CFeeRate(1000), CFeeRate(2000), CFeeRate(2500),
152  CFeeRate(3333), CFeeRate(7800), CFeeRate(11199),
153  CFeeRate(23330), CFeeRate(50000), CFeeRate(5*CENT)});
154 
155  // All nonexistent entries have a bumpfee of zero, regardless of feerate
156  std::vector<COutPoint> nonexistent_outpoints({ COutPoint{GetRandHash(), 0}, COutPoint{GetRandHash(), 3} });
157  for (const auto& outpoint : nonexistent_outpoints) BOOST_CHECK(!pool.isSpent(outpoint));
158  for (const auto& feerate : various_normal_feerates) {
159  node::MiniMiner mini_miner(pool, nonexistent_outpoints);
160  BOOST_CHECK(mini_miner.IsReadyToCalculate());
161  auto bump_fees = mini_miner.CalculateBumpFees(feerate);
162  BOOST_CHECK(!mini_miner.IsReadyToCalculate());
163  BOOST_CHECK(sanity_check(all_transactions, bump_fees));
164  BOOST_CHECK(bump_fees.size() == nonexistent_outpoints.size());
165  for (const auto& outpoint: nonexistent_outpoints) {
166  auto it = bump_fees.find(outpoint);
167  BOOST_CHECK(it != bump_fees.end());
168  BOOST_CHECK_EQUAL(it->second, 0);
169  }
170  }
171 
172  // Gather bump fees for all available UTXOs.
173  for (const auto& target_feerate : various_normal_feerates) {
174  node::MiniMiner mini_miner(pool, all_unspent_outpoints);
175  BOOST_CHECK(mini_miner.IsReadyToCalculate());
176  auto bump_fees = mini_miner.CalculateBumpFees(target_feerate);
177  BOOST_CHECK(!mini_miner.IsReadyToCalculate());
178  BOOST_CHECK(sanity_check(all_transactions, bump_fees));
179  BOOST_CHECK_EQUAL(bump_fees.size(), all_unspent_outpoints.size());
180 
181  // Check tx0 bumpfee: no other bumper.
182  const TxDimensions& tx0_dimensions = tx_dims.find(tx0->GetHash())->second;
183  CAmount bumpfee0 = Find(bump_fees, COutPoint{tx0->GetHash(), 1});
184  if (target_feerate <= tx0_dimensions.feerate) {
185  BOOST_CHECK_EQUAL(bumpfee0, 0);
186  } else {
187  // Difference is fee to bump tx0 from current to target feerate.
188  BOOST_CHECK_EQUAL(bumpfee0, target_feerate.GetFee(tx0_dimensions.vsize) - tx0_dimensions.mod_fee);
189  }
190 
191  // Check tx2 bumpfee: assisted by tx3.
192  const TxDimensions& tx2_dimensions = tx_dims.find(tx2->GetHash())->second;
193  const TxDimensions& tx3_dimensions = tx_dims.find(tx3->GetHash())->second;
194  const CFeeRate tx2_feerate = CFeeRate(tx2_dimensions.mod_fee + tx3_dimensions.mod_fee, tx2_dimensions.vsize + tx3_dimensions.vsize);
195  CAmount bumpfee2 = Find(bump_fees, COutPoint{tx2->GetHash(), 1});
196  if (target_feerate <= tx2_feerate) {
197  // As long as target feerate is below tx3's ancestor feerate, there is no bump fee.
198  BOOST_CHECK_EQUAL(bumpfee2, 0);
199  } else {
200  // Difference is fee to bump tx2 from current to target feerate, without tx3.
201  BOOST_CHECK_EQUAL(bumpfee2, target_feerate.GetFee(tx2_dimensions.vsize) - tx2_dimensions.mod_fee);
202  }
203 
204  // If tx5’s modified fees are sufficient for tx4 and tx5 to be picked
205  // into the block, our prospective new transaction would not need to
206  // bump tx4 when using tx4’s second output. If however even tx5’s
207  // modified fee (which essentially indicates "effective feerate") is
208  // not sufficient to bump tx4, using the second output of tx4 would
209  // require our transaction to bump tx4 from scratch since we evaluate
210  // transaction packages per ancestor sets and do not consider multiple
211  // children’s fees.
212  const TxDimensions& tx4_dimensions = tx_dims.find(tx4->GetHash())->second;
213  const TxDimensions& tx5_dimensions = tx_dims.find(tx5->GetHash())->second;
214  const CFeeRate tx4_feerate = CFeeRate(tx4_dimensions.mod_fee + tx5_dimensions.mod_fee, tx4_dimensions.vsize + tx5_dimensions.vsize);
215  CAmount bumpfee4 = Find(bump_fees, COutPoint{tx4->GetHash(), 1});
216  if (target_feerate <= tx4_feerate) {
217  // As long as target feerate is below tx5's ancestor feerate, there is no bump fee.
218  BOOST_CHECK_EQUAL(bumpfee4, 0);
219  } else {
220  // Difference is fee to bump tx4 from current to target feerate, without tx5.
221  BOOST_CHECK_EQUAL(bumpfee4, target_feerate.GetFee(tx4_dimensions.vsize) - tx4_dimensions.mod_fee);
222  }
223  }
224  // Spent outpoints should usually not be requested as they would not be
225  // considered available. However, when they are explicitly requested, we
226  // can calculate their bumpfee to facilitate RBF-replacements
227  for (const auto& target_feerate : various_normal_feerates) {
228  node::MiniMiner mini_miner_all_spent(pool, all_spent_outpoints);
229  BOOST_CHECK(mini_miner_all_spent.IsReadyToCalculate());
230  auto bump_fees_all_spent = mini_miner_all_spent.CalculateBumpFees(target_feerate);
231  BOOST_CHECK(!mini_miner_all_spent.IsReadyToCalculate());
232  BOOST_CHECK_EQUAL(bump_fees_all_spent.size(), all_spent_outpoints.size());
233  node::MiniMiner mini_miner_all_parents(pool, all_parent_outputs);
234  BOOST_CHECK(mini_miner_all_parents.IsReadyToCalculate());
235  auto bump_fees_all_parents = mini_miner_all_parents.CalculateBumpFees(target_feerate);
236  BOOST_CHECK(!mini_miner_all_parents.IsReadyToCalculate());
237  BOOST_CHECK_EQUAL(bump_fees_all_parents.size(), all_parent_outputs.size());
238  for (auto& bump_fees : {bump_fees_all_parents, bump_fees_all_spent}) {
239  // For all_parents case, both outputs from the parent should have the same bump fee,
240  // even though only one of them is in a to-be-replaced transaction.
241  BOOST_CHECK(sanity_check(all_transactions, bump_fees));
242 
243  // Check tx0 bumpfee: no other bumper.
244  const TxDimensions& tx0_dimensions = tx_dims.find(tx0->GetHash())->second;
245  CAmount it0_spent = Find(bump_fees, COutPoint{tx0->GetHash(), 0});
246  if (target_feerate <= tx0_dimensions.feerate) {
247  BOOST_CHECK_EQUAL(it0_spent, 0);
248  } else {
249  // Difference is fee to bump tx0 from current to target feerate.
250  BOOST_CHECK_EQUAL(it0_spent, target_feerate.GetFee(tx0_dimensions.vsize) - tx0_dimensions.mod_fee);
251  }
252 
253  // Check tx2 bumpfee: no other bumper, because tx3 is to-be-replaced.
254  const TxDimensions& tx2_dimensions = tx_dims.find(tx2->GetHash())->second;
255  const CFeeRate tx2_feerate_unbumped = tx2_dimensions.feerate;
256  auto it2_spent = Find(bump_fees, COutPoint{tx2->GetHash(), 0});
257  if (target_feerate <= tx2_feerate_unbumped) {
258  BOOST_CHECK_EQUAL(it2_spent, 0);
259  } else {
260  // Difference is fee to bump tx2 from current to target feerate, without tx3.
261  BOOST_CHECK_EQUAL(it2_spent, target_feerate.GetFee(tx2_dimensions.vsize) - tx2_dimensions.mod_fee);
262  }
263 
264  // Check tx4 bumpfee: no other bumper, because tx5 is to-be-replaced.
265  const TxDimensions& tx4_dimensions = tx_dims.find(tx4->GetHash())->second;
266  const CFeeRate tx4_feerate_unbumped = tx4_dimensions.feerate;
267  auto it4_spent = Find(bump_fees, COutPoint{tx4->GetHash(), 0});
268  if (target_feerate <= tx4_feerate_unbumped) {
269  BOOST_CHECK_EQUAL(it4_spent, 0);
270  } else {
271  // Difference is fee to bump tx4 from current to target feerate, without tx5.
272  BOOST_CHECK_EQUAL(it4_spent, target_feerate.GetFee(tx4_dimensions.vsize) - tx4_dimensions.mod_fee);
273  }
274  }
275  }
276 }
277 
279 {
280 /* Tx graph for `miniminer_overlap` unit test:
281  *
282  * coinbase_tx [mined] ... block-chain
283  * -------------------------------------------------
284  * / | \ \ ... mempool
285  * / | \ |
286  * tx0 tx1 tx2 tx4
287  * [low] [med] [high] [high]
288  * \ | / |
289  * \ | / tx5
290  * \ | / [low]
291  * tx3 / \
292  * [high] tx6 tx7
293  * [med] [high]
294  *
295  * NOTE:
296  * -> "low"/"med"/"high" denote the _absolute_ fee of each tx
297  * -> tx3 has 3 inputs and 3 outputs, all other txs have 1 input and 2 outputs
298  * -> tx3's feerate is lower than tx2's, as tx3 has more weight (due to having more inputs and outputs)
299  *
300  * -> tx2_FR = high / tx2_vsize
301  * -> tx3_FR = high / tx3_vsize
302  * -> tx3_ASFR = (low+med+high+high) / (tx0_vsize + tx1_vsize + tx2_vsize + tx3_vsize)
303  * -> tx4_FR = high / tx4_vsize
304  * -> tx6_ASFR = (high+low+med) / (tx4_vsize + tx5_vsize + tx6_vsize)
305  * -> tx7_ASFR = (high+low+high) / (tx4_vsize + tx5_vsize + tx7_vsize) */
306 
307  CTxMemPool& pool = *Assert(m_node.mempool);
308  LOCK2(::cs_main, pool.cs);
310 
311  const CAmount low_fee{CENT/2000}; // 500 ṩ
312  const CAmount med_fee{CENT/200}; // 5000 ṩ
313  const CAmount high_fee{CENT/10}; // 100_000 ṩ
314 
315  // Create 3 parents of different feerates, and 1 child spending outputs from all 3 parents.
316  const auto tx0 = make_tx({COutPoint{m_coinbase_txns[0]->GetHash(), 0}}, /*num_outputs=*/2);
317  pool.addUnchecked(entry.Fee(low_fee).FromTx(tx0));
318  const auto tx1 = make_tx({COutPoint{m_coinbase_txns[1]->GetHash(), 0}}, /*num_outputs=*/2);
319  pool.addUnchecked(entry.Fee(med_fee).FromTx(tx1));
320  const auto tx2 = make_tx({COutPoint{m_coinbase_txns[2]->GetHash(), 0}}, /*num_outputs=*/2);
321  pool.addUnchecked(entry.Fee(high_fee).FromTx(tx2));
322  const auto tx3 = make_tx({COutPoint{tx0->GetHash(), 0}, COutPoint{tx1->GetHash(), 0}, COutPoint{tx2->GetHash(), 0}}, /*num_outputs=*/3);
323  pool.addUnchecked(entry.Fee(high_fee).FromTx(tx3));
324 
325  // Create 1 grandparent and 1 parent, then 2 children.
326  const auto tx4 = make_tx({COutPoint{m_coinbase_txns[3]->GetHash(), 0}}, /*num_outputs=*/2);
327  pool.addUnchecked(entry.Fee(high_fee).FromTx(tx4));
328  const auto tx5 = make_tx({COutPoint{tx4->GetHash(), 0}}, /*num_outputs=*/3);
329  pool.addUnchecked(entry.Fee(low_fee).FromTx(tx5));
330  const auto tx6 = make_tx({COutPoint{tx5->GetHash(), 0}}, /*num_outputs=*/2);
331  pool.addUnchecked(entry.Fee(med_fee).FromTx(tx6));
332  const auto tx7 = make_tx({COutPoint{tx5->GetHash(), 1}}, /*num_outputs=*/2);
333  pool.addUnchecked(entry.Fee(high_fee).FromTx(tx7));
334 
335  std::vector<CTransactionRef> all_transactions{tx0, tx1, tx2, tx3, tx4, tx5, tx6, tx7};
336  std::vector<int64_t> tx_vsizes;
337  tx_vsizes.reserve(all_transactions.size());
338  for (const auto& tx : all_transactions) tx_vsizes.push_back(GetVirtualTransactionSize(*tx));
339 
340  std::vector<COutPoint> all_unspent_outpoints({
341  COutPoint{tx0->GetHash(), 1},
342  COutPoint{tx1->GetHash(), 1},
343  COutPoint{tx2->GetHash(), 1},
344  COutPoint{tx3->GetHash(), 0},
345  COutPoint{tx3->GetHash(), 1},
346  COutPoint{tx3->GetHash(), 2},
347  COutPoint{tx4->GetHash(), 1},
348  COutPoint{tx5->GetHash(), 2},
349  COutPoint{tx6->GetHash(), 0},
350  COutPoint{tx7->GetHash(), 0}
351  });
352  for (const auto& outpoint : all_unspent_outpoints) BOOST_CHECK(!pool.isSpent(outpoint));
353 
354  const auto tx2_feerate = CFeeRate(high_fee, tx_vsizes[2]);
355  const auto tx3_feerate = CFeeRate(high_fee, tx_vsizes[3]);
356  // tx3's feerate is lower than tx2's. same fee, different weight.
357  BOOST_CHECK(tx2_feerate > tx3_feerate);
358  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]);
359  const auto tx3_iter = pool.GetIter(tx3->GetHash());
360  BOOST_CHECK(tx3_anc_feerate == CFeeRate(tx3_iter.value()->GetModFeesWithAncestors(), tx3_iter.value()->GetSizeWithAncestors()));
361  const auto tx4_feerate = CFeeRate(high_fee, tx_vsizes[4]);
362  const auto tx6_anc_feerate = CFeeRate(high_fee + low_fee + med_fee, tx_vsizes[4] + tx_vsizes[5] + tx_vsizes[6]);
363  const auto tx6_iter = pool.GetIter(tx6->GetHash());
364  BOOST_CHECK(tx6_anc_feerate == CFeeRate(tx6_iter.value()->GetModFeesWithAncestors(), tx6_iter.value()->GetSizeWithAncestors()));
365  const auto tx7_anc_feerate = CFeeRate(high_fee + low_fee + high_fee, tx_vsizes[4] + tx_vsizes[5] + tx_vsizes[7]);
366  const auto tx7_iter = pool.GetIter(tx7->GetHash());
367  BOOST_CHECK(tx7_anc_feerate == CFeeRate(tx7_iter.value()->GetModFeesWithAncestors(), tx7_iter.value()->GetSizeWithAncestors()));
368  BOOST_CHECK(tx4_feerate > tx6_anc_feerate);
369  BOOST_CHECK(tx4_feerate > tx7_anc_feerate);
370 
371  // Extremely high feerate: everybody's bumpfee is from their full ancestor set.
372  {
373  node::MiniMiner mini_miner(pool, all_unspent_outpoints);
374  const CFeeRate very_high_feerate(COIN);
375  BOOST_CHECK(tx3_anc_feerate < very_high_feerate);
376  BOOST_CHECK(mini_miner.IsReadyToCalculate());
377  auto bump_fees = mini_miner.CalculateBumpFees(very_high_feerate);
378  BOOST_CHECK_EQUAL(bump_fees.size(), all_unspent_outpoints.size());
379  BOOST_CHECK(!mini_miner.IsReadyToCalculate());
380  BOOST_CHECK(sanity_check(all_transactions, bump_fees));
381  const auto tx0_bumpfee = bump_fees.find(COutPoint{tx0->GetHash(), 1});
382  BOOST_CHECK(tx0_bumpfee != bump_fees.end());
383  BOOST_CHECK_EQUAL(tx0_bumpfee->second, very_high_feerate.GetFee(tx_vsizes[0]) - low_fee);
384  const auto tx3_bumpfee = bump_fees.find(COutPoint{tx3->GetHash(), 0});
385  BOOST_CHECK(tx3_bumpfee != bump_fees.end());
386  BOOST_CHECK_EQUAL(tx3_bumpfee->second,
387  very_high_feerate.GetFee(tx_vsizes[0] + tx_vsizes[1] + tx_vsizes[2] + tx_vsizes[3]) - (low_fee + med_fee + high_fee + high_fee));
388  const auto tx6_bumpfee = bump_fees.find(COutPoint{tx6->GetHash(), 0});
389  BOOST_CHECK(tx6_bumpfee != bump_fees.end());
390  BOOST_CHECK_EQUAL(tx6_bumpfee->second,
391  very_high_feerate.GetFee(tx_vsizes[4] + tx_vsizes[5] + tx_vsizes[6]) - (high_fee + low_fee + med_fee));
392  const auto tx7_bumpfee = bump_fees.find(COutPoint{tx7->GetHash(), 0});
393  BOOST_CHECK(tx7_bumpfee != bump_fees.end());
394  BOOST_CHECK_EQUAL(tx7_bumpfee->second,
395  very_high_feerate.GetFee(tx_vsizes[4] + tx_vsizes[5] + tx_vsizes[7]) - (high_fee + low_fee + high_fee));
396  // Total fees: if spending multiple outputs from tx3 don't double-count fees.
397  node::MiniMiner mini_miner_total_tx3(pool, {COutPoint{tx3->GetHash(), 0}, COutPoint{tx3->GetHash(), 1}});
398  BOOST_CHECK(mini_miner_total_tx3.IsReadyToCalculate());
399  const auto tx3_bump_fee = mini_miner_total_tx3.CalculateTotalBumpFees(very_high_feerate);
400  BOOST_CHECK(!mini_miner_total_tx3.IsReadyToCalculate());
401  BOOST_CHECK(tx3_bump_fee.has_value());
402  BOOST_CHECK_EQUAL(tx3_bump_fee.value(),
403  very_high_feerate.GetFee(tx_vsizes[0] + tx_vsizes[1] + tx_vsizes[2] + tx_vsizes[3]) - (low_fee + med_fee + high_fee + high_fee));
404  // Total fees: if spending both tx6 and tx7, don't double-count fees.
405  node::MiniMiner mini_miner_tx6_tx7(pool, {COutPoint{tx6->GetHash(), 0}, COutPoint{tx7->GetHash(), 0}});
406  BOOST_CHECK(mini_miner_tx6_tx7.IsReadyToCalculate());
407  const auto tx6_tx7_bumpfee = mini_miner_tx6_tx7.CalculateTotalBumpFees(very_high_feerate);
408  BOOST_CHECK(!mini_miner_tx6_tx7.IsReadyToCalculate());
409  BOOST_CHECK(tx6_tx7_bumpfee.has_value());
410  BOOST_CHECK_EQUAL(tx6_tx7_bumpfee.value(),
411  very_high_feerate.GetFee(tx_vsizes[4] + tx_vsizes[5] + tx_vsizes[6] + tx_vsizes[7]) - (high_fee + low_fee + med_fee + high_fee));
412  }
413  // Feerate just below tx4: tx6 and tx7 have different bump fees.
414  {
415  const auto just_below_tx4 = CFeeRate(tx4_feerate.GetFeePerK() - 5);
416  node::MiniMiner mini_miner(pool, all_unspent_outpoints);
417  BOOST_CHECK(mini_miner.IsReadyToCalculate());
418  auto bump_fees = mini_miner.CalculateBumpFees(just_below_tx4);
419  BOOST_CHECK(!mini_miner.IsReadyToCalculate());
420  BOOST_CHECK_EQUAL(bump_fees.size(), all_unspent_outpoints.size());
421  BOOST_CHECK(sanity_check(all_transactions, bump_fees));
422  const auto tx6_bumpfee = bump_fees.find(COutPoint{tx6->GetHash(), 0});
423  BOOST_CHECK(tx6_bumpfee != bump_fees.end());
424  BOOST_CHECK_EQUAL(tx6_bumpfee->second, just_below_tx4.GetFee(tx_vsizes[5] + tx_vsizes[6]) - (low_fee + med_fee));
425  const auto tx7_bumpfee = bump_fees.find(COutPoint{tx7->GetHash(), 0});
426  BOOST_CHECK(tx7_bumpfee != bump_fees.end());
427  BOOST_CHECK_EQUAL(tx7_bumpfee->second, just_below_tx4.GetFee(tx_vsizes[5] + tx_vsizes[7]) - (low_fee + high_fee));
428  // Total fees: if spending both tx6 and tx7, don't double-count fees.
429  node::MiniMiner mini_miner_tx6_tx7(pool, {COutPoint{tx6->GetHash(), 0}, COutPoint{tx7->GetHash(), 0}});
430  BOOST_CHECK(mini_miner_tx6_tx7.IsReadyToCalculate());
431  const auto tx6_tx7_bumpfee = mini_miner_tx6_tx7.CalculateTotalBumpFees(just_below_tx4);
432  BOOST_CHECK(!mini_miner_tx6_tx7.IsReadyToCalculate());
433  BOOST_CHECK(tx6_tx7_bumpfee.has_value());
434  BOOST_CHECK_EQUAL(tx6_tx7_bumpfee.value(), just_below_tx4.GetFee(tx_vsizes[5] + tx_vsizes[6]) - (low_fee + med_fee));
435  }
436  // Feerate between tx6 and tx7's ancestor feerates: don't need to bump tx5 because tx7 already does.
437  {
438  const auto just_above_tx6 = CFeeRate(med_fee + 10, tx_vsizes[6]);
439  BOOST_CHECK(just_above_tx6 <= CFeeRate(low_fee + high_fee, tx_vsizes[5] + tx_vsizes[7]));
440  node::MiniMiner mini_miner(pool, all_unspent_outpoints);
441  BOOST_CHECK(mini_miner.IsReadyToCalculate());
442  auto bump_fees = mini_miner.CalculateBumpFees(just_above_tx6);
443  BOOST_CHECK(!mini_miner.IsReadyToCalculate());
444  BOOST_CHECK_EQUAL(bump_fees.size(), all_unspent_outpoints.size());
445  BOOST_CHECK(sanity_check(all_transactions, bump_fees));
446  const auto tx6_bumpfee = bump_fees.find(COutPoint{tx6->GetHash(), 0});
447  BOOST_CHECK(tx6_bumpfee != bump_fees.end());
448  BOOST_CHECK_EQUAL(tx6_bumpfee->second, just_above_tx6.GetFee(tx_vsizes[6]) - (med_fee));
449  const auto tx7_bumpfee = bump_fees.find(COutPoint{tx7->GetHash(), 0});
450  BOOST_CHECK(tx7_bumpfee != bump_fees.end());
451  BOOST_CHECK_EQUAL(tx7_bumpfee->second, 0);
452  }
453 }
455 {
456  CTxMemPool& pool = *Assert(m_node.mempool);
457  LOCK2(cs_main, pool.cs);
458 
459  // Add chain of size 500
461  std::vector<uint256> chain_txids;
462  auto& lasttx = m_coinbase_txns[0];
463  for (auto i{0}; i < 500; ++i) {
464  const auto tx = make_tx({COutPoint{lasttx->GetHash(), 0}}, /*num_outputs=*/1);
465  pool.addUnchecked(entry.Fee(CENT).FromTx(tx));
466  chain_txids.push_back(tx->GetHash());
467  lasttx = tx;
468  }
469  const auto cluster_500tx = pool.GatherClusters({lasttx->GetHash()});
470  CTxMemPool::setEntries cluster_500tx_set{cluster_500tx.begin(), cluster_500tx.end()};
471  BOOST_CHECK_EQUAL(cluster_500tx.size(), cluster_500tx_set.size());
472  const auto vec_iters_500 = pool.GetIterVec(chain_txids);
473  for (const auto& iter : vec_iters_500) BOOST_CHECK(cluster_500tx_set.count(iter));
474 
475  // GatherClusters stops at 500 transactions.
476  const auto tx_501 = make_tx({COutPoint{lasttx->GetHash(), 0}}, /*num_outputs=*/1);
477  pool.addUnchecked(entry.Fee(CENT).FromTx(tx_501));
478  const auto cluster_501 = pool.GatherClusters({tx_501->GetHash()});
479  BOOST_CHECK_EQUAL(cluster_501.size(), 0);
480 
481  /* Zig Zag cluster:
482  * txp0 txp1 txp2 ... txp48 txp49
483  * \ / \ / \ \ /
484  * txc0 txc1 txc2 ... txc48
485  * Note that each transaction's ancestor size is 1 or 3, and each descendant size is 1, 2 or 3.
486  * However, all of these transactions are in the same cluster. */
487  std::vector<uint256> zigzag_txids;
488  for (auto p{0}; p < 50; ++p) {
489  const auto txp = make_tx({COutPoint{GetRandHash(), 0}}, /*num_outputs=*/2);
490  pool.addUnchecked(entry.Fee(CENT).FromTx(txp));
491  zigzag_txids.push_back(txp->GetHash());
492  }
493  for (auto c{0}; c < 49; ++c) {
494  const auto txc = make_tx({COutPoint{zigzag_txids[c], 1}, COutPoint{zigzag_txids[c+1], 0}}, /*num_outputs=*/1);
495  pool.addUnchecked(entry.Fee(CENT).FromTx(txc));
496  zigzag_txids.push_back(txc->GetHash());
497  }
498  const auto vec_iters_zigzag = pool.GetIterVec(zigzag_txids);
499  // It doesn't matter which tx we calculate cluster for, everybody is in it.
500  const std::vector<size_t> indices{0, 22, 72, zigzag_txids.size() - 1};
501  for (const auto index : indices) {
502  const auto cluster = pool.GatherClusters({zigzag_txids[index]});
503  BOOST_CHECK_EQUAL(cluster.size(), zigzag_txids.size());
504  CTxMemPool::setEntries clusterset{cluster.begin(), cluster.end()};
505  BOOST_CHECK_EQUAL(cluster.size(), clusterset.size());
506  for (const auto& iter : vec_iters_zigzag) BOOST_CHECK(clusterset.count(iter));
507  }
508 }
509 
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:413
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:302
void PrioritiseTransaction(const uint256 &hash, const CAmount &nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
Definition: txmempool.cpp:882
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:973
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:391
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:474
std::optional< txiter > GetIter(const uint256 &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns an iterator to the given hash, if found.
Definition: txmempool.cpp:956
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:1222
std::set< txiter, CompareIteratorByHash > setEntries
Definition: txmempool.h:397
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:950
bool isSpent(const COutPoint &outpoint) const
Definition: txmempool.cpp:419
A minimal version of BlockAssembler.
Definition: mini_miner.h:61
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:248
bool IsReadyToCalculate() const
Returns true if CalculateBumpFees may be called, false if not.
Definition: mini_miner.h:103
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:1185
#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:573
@ OP_EQUAL
Definition: script.h:144
@ OP_11
Definition: script.h:92
static constexpr CAmount CENT
Definition: setup_common.h:44
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:98
Definition: txmempool.h:17
CTxMemPoolEntry FromTx(const CMutableTransaction &tx) const
Definition: txmempool.cpp:30
TestMemPoolEntryHelper & Fee(CAmount _fee)
Definition: txmempool.h:31
Testing setup that configures a complete environment.
Definition: setup_common.h:77
std::unique_ptr< CTxMemPool > mempool
Definition: context.h:55
#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