Branch data Line data Source code
1 : : // Copyright (c) 2011-present 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 : :
5 : : #include <common/system.h>
6 : : #include <policy/policy.h>
7 : : #include <test/util/time.h>
8 : : #include <test/util/txmempool.h>
9 : : #include <txmempool.h>
10 : : #include <util/time.h>
11 : :
12 : : #include <test/util/setup_common.h>
13 : :
14 : : #include <boost/test/unit_test.hpp>
15 : : #include <vector>
16 : :
17 : : BOOST_FIXTURE_TEST_SUITE(mempool_tests, TestingSetup)
18 : :
19 : : static constexpr auto REMOVAL_REASON_DUMMY = MemPoolRemovalReason::REPLACED;
20 : :
21 : : class MemPoolTest final : public CTxMemPool
22 : : {
23 : : public:
24 : : using CTxMemPool::GetMinFee;
25 : : };
26 : :
27 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(MempoolLookupTest)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
28 : : {
29 : 1 : auto& pool = static_cast<MemPoolTest&>(*Assert(m_node.mempool));
30 [ + - ]: 1 : LOCK2(cs_main, pool.cs);
31 : 1 : TestMemPoolEntryHelper entry;
32 : :
33 [ + - ]: 1 : CMutableTransaction tx = CMutableTransaction();
34 : 1 : tx.vin = {
35 [ + - ]: 2 : CTxIn{COutPoint{}, CScript{} << OP_1},
36 [ + - + + : 3 : };
- - ]
37 : 1 : tx.vout = {
38 [ + - + - ]: 2 : CTxOut{10 * COIN, CScript{} << OP_1 << OP_EQUAL},
39 [ + - + + : 3 : };
- - ]
40 : :
41 : : // Not in the mempool, so can't find it by txid or wtxid
42 [ + - + - : 2 : BOOST_CHECK(!pool.get(tx.GetHash()));
+ - + - -
+ + - ]
43 [ + - + - : 3 : BOOST_CHECK(!pool.get(CTransaction(tx).GetWitnessHash()));
+ - + - -
+ + - ]
44 : :
45 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(1000LL).FromTx(tx));
46 : :
47 : : // Lookup by Txid
48 [ + - + - : 3 : BOOST_CHECK(pool.get(tx.GetHash()));
+ - + - +
- + - ]
49 : :
50 : : // Lookup by Wtxid
51 [ + - + - : 4 : BOOST_CHECK(pool.get(CTransaction(tx).GetWitnessHash()));
+ - + - +
- ]
52 [ + - + - : 6 : }
+ - + - +
- ]
53 : :
54 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(MempoolRemoveTest)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
55 : : {
56 : : // Test CTxMemPool::remove functionality
57 : :
58 : 1 : TestMemPoolEntryHelper entry;
59 : : // Parent transaction with three children,
60 : : // and three grand-children:
61 : 1 : CMutableTransaction txParent;
62 : 1 : txParent.vin = {
63 [ + - ]: 2 : CTxIn{COutPoint{}, CScript{} << OP_11},
64 [ + - + + : 3 : };
- - ]
65 : 1 : txParent.vout = {
66 [ + - + - ]: 2 : CTxOut{33000LL, CScript{} << OP_11 << OP_EQUAL},
67 [ + - + - ]: 2 : CTxOut{33000LL, CScript{} << OP_11 << OP_EQUAL},
68 [ + - + - ]: 2 : CTxOut{33000LL, CScript{} << OP_11 << OP_EQUAL},
69 [ + - + + : 5 : };
- - ]
70 [ + - + + : 10 : CMutableTransaction txChild[3];
- - ]
71 [ + + ]: 4 : for (uint32_t i = 0; i < 3; i++)
72 : : {
73 : 3 : txChild[i].vin = {
74 [ + - + - ]: 6 : CTxIn{COutPoint{txParent.GetHash(), i}, CScript{} << OP_11},
75 [ + - + + : 9 : };
- - ]
76 : 3 : txChild[i].vout = {
77 [ + - + - ]: 6 : CTxOut{11000LL, CScript{} << OP_11 << OP_EQUAL},
78 [ + - + + : 9 : };
- - ]
79 : : }
80 [ + - + + : 11 : CMutableTransaction txGrandChild[3];
- - ]
81 [ + + ]: 4 : for (int i = 0; i < 3; i++)
82 : : {
83 : 3 : txGrandChild[i].vin = {
84 [ + - + - ]: 6 : CTxIn{COutPoint{txChild[i].GetHash(), 0}, CScript{} << OP_11},
85 [ + - + + : 9 : };
- - ]
86 : 3 : txGrandChild[i].vout = {
87 [ + - + - ]: 6 : CTxOut{11000LL, CScript{} << OP_11 << OP_EQUAL},
88 [ + - + + : 9 : };
- - ]
89 : : }
90 : :
91 : :
92 [ + - + - ]: 1 : CTxMemPool& testPool = *Assert(m_node.mempool);
93 [ + - + - ]: 1 : LOCK2(::cs_main, testPool.cs);
94 : :
95 : : // Nothing in pool, remove should do nothing:
96 [ + - ]: 1 : unsigned int poolSize = testPool.size();
97 [ + - + - ]: 1 : testPool.removeRecursive(CTransaction(txParent), REMOVAL_REASON_DUMMY);
98 [ + - + - : 1 : BOOST_CHECK_EQUAL(testPool.size(), poolSize);
+ - ]
99 : :
100 : : // Just the parent:
101 [ + - + - ]: 1 : TryAddToMempool(testPool, entry.FromTx(txParent));
102 [ + - ]: 1 : poolSize = testPool.size();
103 [ + - + - ]: 1 : testPool.removeRecursive(CTransaction(txParent), REMOVAL_REASON_DUMMY);
104 [ + - + - : 1 : BOOST_CHECK_EQUAL(testPool.size(), poolSize - 1);
+ - ]
105 : :
106 : : // Parent, children, grandchildren:
107 [ + - + - ]: 1 : TryAddToMempool(testPool, entry.FromTx(txParent));
108 [ + + ]: 4 : for (int i = 0; i < 3; i++)
109 : : {
110 [ + - + - ]: 3 : TryAddToMempool(testPool, entry.FromTx(txChild[i]));
111 [ + - + - ]: 3 : TryAddToMempool(testPool, entry.FromTx(txGrandChild[i]));
112 : : }
113 : : // Remove Child[0], GrandChild[0] should be removed:
114 [ + - ]: 1 : poolSize = testPool.size();
115 [ + - + - ]: 1 : testPool.removeRecursive(CTransaction(txChild[0]), REMOVAL_REASON_DUMMY);
116 [ + - + - : 1 : BOOST_CHECK_EQUAL(testPool.size(), poolSize - 2);
+ - ]
117 : : // ... make sure grandchild and child are gone:
118 [ + - ]: 1 : poolSize = testPool.size();
119 [ + - + - ]: 1 : testPool.removeRecursive(CTransaction(txGrandChild[0]), REMOVAL_REASON_DUMMY);
120 [ + - + - : 1 : BOOST_CHECK_EQUAL(testPool.size(), poolSize);
+ - ]
121 [ + - ]: 1 : poolSize = testPool.size();
122 [ + - + - ]: 1 : testPool.removeRecursive(CTransaction(txChild[0]), REMOVAL_REASON_DUMMY);
123 [ + - + - : 1 : BOOST_CHECK_EQUAL(testPool.size(), poolSize);
+ - ]
124 : : // Remove parent, all children/grandchildren should go:
125 [ + - ]: 1 : poolSize = testPool.size();
126 [ + - + - ]: 1 : testPool.removeRecursive(CTransaction(txParent), REMOVAL_REASON_DUMMY);
127 [ + - + - : 1 : BOOST_CHECK_EQUAL(testPool.size(), poolSize - 5);
+ - ]
128 [ + - + - : 1 : BOOST_CHECK_EQUAL(testPool.size(), 0U);
+ - ]
129 : :
130 : : // Add children and grandchildren, but NOT the parent (simulate the parent being in a block)
131 [ + + ]: 4 : for (int i = 0; i < 3; i++)
132 : : {
133 [ + - + - ]: 3 : TryAddToMempool(testPool, entry.FromTx(txChild[i]));
134 [ + - + - ]: 3 : TryAddToMempool(testPool, entry.FromTx(txGrandChild[i]));
135 : : }
136 : : // Now remove the parent, as might happen if a block-re-org occurs but the parent cannot be
137 : : // put into the mempool (maybe because it is non-standard):
138 [ + - ]: 1 : poolSize = testPool.size();
139 [ + - + - ]: 1 : testPool.removeRecursive(CTransaction(txParent), REMOVAL_REASON_DUMMY);
140 [ + - + - : 1 : BOOST_CHECK_EQUAL(testPool.size(), poolSize - 6);
+ - ]
141 [ + - + - : 1 : BOOST_CHECK_EQUAL(testPool.size(), 0U);
+ - + - ]
142 [ + - + - : 27 : }
+ - + - +
- + - + -
+ - + - +
- + + + +
- - - - -
- ]
143 : :
144 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(MempoolSizeLimitTest)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
145 : : {
146 : 1 : auto& pool = static_cast<MemPoolTest&>(*Assert(m_node.mempool));
147 [ + - ]: 1 : LOCK2(cs_main, pool.cs);
148 : 1 : TestMemPoolEntryHelper entry;
149 : :
150 [ + - ]: 1 : CMutableTransaction tx1 = CMutableTransaction();
151 : 1 : tx1.vin = {
152 [ + - ]: 2 : CTxIn{COutPoint{}, CScript{} << OP_1},
153 [ + - + + : 3 : };
- - ]
154 : 1 : tx1.vout = {
155 [ + - + - ]: 2 : CTxOut{10 * COIN, CScript{} << OP_1 << OP_EQUAL},
156 [ + - + + : 3 : };
- - ]
157 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(1000LL).FromTx(tx1));
158 : :
159 [ + - ]: 1 : CMutableTransaction tx2 = CMutableTransaction();
160 : 1 : tx2.vin = {
161 [ + - ]: 2 : CTxIn{COutPoint{}, CScript{} << OP_2},
162 [ + - + + : 3 : };
- - ]
163 : 1 : tx2.vout = {
164 [ + - + - ]: 2 : CTxOut{10 * COIN, CScript{} << OP_2 << OP_EQUAL},
165 [ + - + + : 3 : };
- - ]
166 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(500LL).FromTx(tx2));
167 : :
168 [ + - + - ]: 1 : pool.TrimToSize(pool.DynamicMemoryUsage()); // should do nothing
169 [ + - + - : 2 : BOOST_CHECK(pool.exists(tx1.GetHash()));
+ - + - +
- ]
170 [ + - + - : 2 : BOOST_CHECK(pool.exists(tx2.GetHash()));
+ - + - +
- ]
171 : :
172 [ + - + - ]: 1 : pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4); // should remove the lower-feerate transaction
173 [ + - + - : 2 : BOOST_CHECK(pool.exists(tx1.GetHash()));
+ - + - +
- ]
174 [ + - + - : 2 : BOOST_CHECK(!pool.exists(tx2.GetHash()));
+ - + - +
- ]
175 : :
176 [ + - + - ]: 1 : TryAddToMempool(pool, entry.FromTx(tx2));
177 [ + - ]: 1 : CMutableTransaction tx3 = CMutableTransaction();
178 : 1 : tx3.vin = {
179 [ + - + - ]: 2 : CTxIn{COutPoint{tx2.GetHash(), 0}, CScript{} << OP_2},
180 [ + - + + : 3 : };
- - ]
181 : 1 : tx3.vout = {
182 [ + - + - ]: 2 : CTxOut{10 * COIN, CScript{} << OP_3 << OP_EQUAL},
183 [ + - + + : 3 : };
- - ]
184 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(2000LL).FromTx(tx3));
185 : :
186 [ + - + - ]: 1 : pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4); // tx3 should pay for tx2 (CPFP)
187 [ + - + - : 2 : BOOST_CHECK(!pool.exists(tx1.GetHash()));
+ - + - +
- ]
188 [ + - + - : 2 : BOOST_CHECK(pool.exists(tx2.GetHash()));
+ - + - +
- ]
189 [ + - + - : 2 : BOOST_CHECK(pool.exists(tx3.GetHash()));
+ - + - +
- ]
190 : :
191 [ + - + - ]: 2 : pool.TrimToSize(GetVirtualTransactionSize(CTransaction(tx1))); // mempool is limited to tx1's size in memory usage, so nothing fits
192 [ + - + - : 2 : BOOST_CHECK(!pool.exists(tx1.GetHash()));
+ - + - +
- ]
193 [ + - + - : 2 : BOOST_CHECK(!pool.exists(tx2.GetHash()));
+ - + - +
- ]
194 [ + - + - : 2 : BOOST_CHECK(!pool.exists(tx3.GetHash()));
+ - + - +
- ]
195 : :
196 [ + - + - : 4 : CFeeRate maxFeeRateRemoved(2500, GetVirtualTransactionSize(CTransaction(tx3)) + GetVirtualTransactionSize(CTransaction(tx2)));
+ - ]
197 [ + - + - : 1 : BOOST_CHECK_EQUAL(pool.GetMinFee(1).GetFeePerK(), maxFeeRateRemoved.GetFeePerK() + DEFAULT_INCREMENTAL_RELAY_FEE);
+ - + - +
- ]
198 : :
199 [ + - ]: 1 : CMutableTransaction tx4 = CMutableTransaction();
200 : 1 : tx4.vin = {
201 [ + - ]: 2 : CTxIn{COutPoint{}, CScript{} << OP_4},
202 [ + - ]: 2 : CTxIn{COutPoint{}, CScript{} << OP_4},
203 [ + - + + : 4 : };
- - ]
204 : 1 : tx4.vout = {
205 [ + - + - ]: 2 : CTxOut{10 * COIN, CScript{} << OP_4 << OP_EQUAL},
206 [ + - + - ]: 2 : CTxOut{10 * COIN, CScript{} << OP_4 << OP_EQUAL},
207 [ + - + + : 4 : };
- - ]
208 : :
209 [ + - ]: 1 : CMutableTransaction tx5 = CMutableTransaction();
210 : 1 : tx5.vin = {
211 [ + - + - ]: 2 : CTxIn{COutPoint{tx4.GetHash(), 0}, CScript{} << OP_4},
212 [ + - ]: 2 : CTxIn{COutPoint{}, CScript{} << OP_5},
213 [ + - + + : 4 : };
- - ]
214 : 1 : tx5.vout = {
215 [ + - + - ]: 2 : CTxOut{10 * COIN, CScript{} << OP_5 << OP_EQUAL},
216 [ + - + - ]: 2 : CTxOut{10 * COIN, CScript{} << OP_5 << OP_EQUAL},
217 [ + - + + : 4 : };
- - ]
218 : :
219 [ + - ]: 1 : CMutableTransaction tx6 = CMutableTransaction();
220 : 1 : tx6.vin = {
221 [ + - + - ]: 2 : CTxIn{COutPoint{tx4.GetHash(), 1}, CScript{} << OP_4},
222 [ + - ]: 2 : CTxIn{COutPoint{}, CScript{} << OP_6},
223 [ + - + + : 4 : };
- - ]
224 : 1 : tx6.vout = {
225 [ + - + - ]: 2 : CTxOut{10 * COIN, CScript{} << OP_6 << OP_EQUAL},
226 [ + - + - ]: 2 : CTxOut{10 * COIN, CScript{} << OP_6 << OP_EQUAL},
227 [ + - + + : 4 : };
- - ]
228 : :
229 [ + - ]: 1 : CMutableTransaction tx7 = CMutableTransaction();
230 : 1 : tx7.vin = {
231 [ + - + - ]: 2 : CTxIn{COutPoint{tx5.GetHash(), 0}, CScript{} << OP_5},
232 [ + - + - ]: 2 : CTxIn{COutPoint{tx6.GetHash(), 0}, CScript{} << OP_6},
233 [ + - + + : 4 : };
- - ]
234 : 1 : tx7.vout = {
235 [ + - + - ]: 2 : CTxOut{10 * COIN, CScript{} << OP_7 << OP_EQUAL},
236 [ + - + - ]: 2 : CTxOut{10 * COIN, CScript{} << OP_7 << OP_EQUAL},
237 [ + - + + : 4 : };
- - ]
238 : :
239 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(700LL).FromTx(tx4));
240 [ + - ]: 1 : auto usage_with_tx4_only = pool.DynamicMemoryUsage();
241 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(100LL).FromTx(tx5));
242 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(110LL).FromTx(tx6));
243 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(900LL).FromTx(tx7));
244 : :
245 : : // From the topology above, tx7 must be sorted last, so it should
246 : : // definitely evicted first if we must trim. tx4 should definitely remain
247 : : // in the mempool since it has a higher feerate than its descendants and
248 : : // should be in its own chunk.
249 [ + - + - ]: 1 : pool.TrimToSize(pool.DynamicMemoryUsage() - 1);
250 [ + - + - : 2 : BOOST_CHECK(pool.exists(tx4.GetHash()));
+ - + - +
- ]
251 [ + - + - : 2 : BOOST_CHECK(!pool.exists(tx7.GetHash()));
+ - + - +
- ]
252 : :
253 : : // Tx5 and Tx6 may be removed as well because they're in the same chunk as
254 : : // tx7, but this behavior need not be guaranteed.
255 : :
256 [ + - + - : 1 : if (!pool.exists(tx5.GetHash()))
+ - ]
257 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(100LL).FromTx(tx5));
258 [ + - + - : 1 : if (!pool.exists(tx6.GetHash()))
+ - ]
259 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(110LL).FromTx(tx6));
260 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(900LL).FromTx(tx7));
261 : :
262 : : // If we trim sufficiently, everything but tx4 should be removed.
263 [ + - ]: 1 : pool.TrimToSize(usage_with_tx4_only + 1);
264 [ + - + - : 2 : BOOST_CHECK(pool.exists(tx4.GetHash()));
+ - + - +
- ]
265 [ + - + - : 2 : BOOST_CHECK(!pool.exists(tx5.GetHash()));
+ - + - +
- ]
266 [ + - + - : 2 : BOOST_CHECK(!pool.exists(tx6.GetHash()));
+ - + - +
- ]
267 [ + - + - : 2 : BOOST_CHECK(!pool.exists(tx7.GetHash()));
+ - + - +
- ]
268 : :
269 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(100LL).FromTx(tx5));
270 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(110LL).FromTx(tx6));
271 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(900LL).FromTx(tx7));
272 : :
273 : 1 : std::vector<CTransactionRef> vtx;
274 [ + - ]: 1 : FakeNodeClock clock{42s};
275 : 1 : constexpr std::chrono::seconds HALFLIFE{CTxMemPool::ROLLING_FEE_HALFLIFE};
276 [ + - ]: 1 : clock += HALFLIFE;
277 [ + - + - : 1 : BOOST_CHECK_EQUAL(pool.GetMinFee(1).GetFeePerK(), maxFeeRateRemoved.GetFeePerK() + DEFAULT_INCREMENTAL_RELAY_FEE);
+ - + - +
- ]
278 : : // ... we should keep the same min fee until we get a block
279 [ + - ]: 1 : pool.removeForBlock(vtx);
280 [ + - ]: 1 : clock += HALFLIFE;
281 [ + - + - : 1 : BOOST_CHECK_EQUAL(pool.GetMinFee(1).GetFeePerK(), llround((maxFeeRateRemoved.GetFeePerK() + DEFAULT_INCREMENTAL_RELAY_FEE)/2.0));
+ - + - +
- ]
282 : : // ... then feerate should drop 1/2 each halflife
283 : :
284 [ + - ]: 1 : clock += HALFLIFE / 2;
285 [ + - + - : 1 : BOOST_CHECK_EQUAL(pool.GetMinFee(pool.DynamicMemoryUsage() * 5 / 2).GetFeePerK(), llround((maxFeeRateRemoved.GetFeePerK() + DEFAULT_INCREMENTAL_RELAY_FEE)/4.0));
+ - + - +
- + - ]
286 : : // ... with a 1/2 halflife when mempool is < 1/2 its target size
287 : :
288 [ + - ]: 1 : clock += HALFLIFE / 4;
289 [ + - + - : 1 : BOOST_CHECK_EQUAL(pool.GetMinFee(pool.DynamicMemoryUsage() * 9 / 2).GetFeePerK(), llround((maxFeeRateRemoved.GetFeePerK() + DEFAULT_INCREMENTAL_RELAY_FEE)/8.0));
+ - + - +
- + - ]
290 : : // ... with a 1/4 halflife when mempool is < 1/4 its target size
291 : :
292 [ + - ]: 1 : clock += 5 * HALFLIFE;
293 [ + - + - : 1 : BOOST_CHECK_EQUAL(pool.GetMinFee(1).GetFeePerK(), DEFAULT_INCREMENTAL_RELAY_FEE);
+ - + - ]
294 : : // ... but feerate should never drop below DEFAULT_INCREMENTAL_RELAY_FEE
295 : :
296 [ + - ]: 1 : clock += HALFLIFE;
297 [ + - + - : 1 : BOOST_CHECK_EQUAL(pool.GetMinFee(1).GetFeePerK(), 0);
+ - + - ]
298 : : // ... unless it has gone all the way to 0 (after getting past DEFAULT_INCREMENTAL_RELAY_FEE/2)
299 [ + - + - : 35 : }
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- - - - -
- - - - -
- - - - -
- - ]
300 : :
301 : 15 : inline CTransactionRef make_tx(std::vector<CAmount>&& output_values, std::vector<CTransactionRef>&& inputs=std::vector<CTransactionRef>(), std::vector<uint32_t>&& input_indices=std::vector<uint32_t>())
302 : : {
303 : 15 : CMutableTransaction tx = CMutableTransaction();
304 [ - + + + ]: 28 : for (size_t i = 0; i < inputs.size(); ++i) {
305 [ - + + + : 13 : tx.vin.emplace_back(COutPoint{inputs[i]->GetHash(), input_indices.size() > i ? input_indices[i] : 0});
+ - ]
306 : : }
307 [ - + + + ]: 34 : for (size_t i = 0; i < output_values.size(); ++i) {
308 [ + - + - : 38 : tx.vout.emplace_back(output_values[i], CScript{} << OP_11 << OP_EQUAL);
+ - ]
309 : : }
310 [ + - ]: 30 : return MakeTransactionRef(tx);
311 : 15 : }
312 : :
313 : :
314 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(MempoolAncestryTests)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
315 : : {
316 : 1 : size_t ancestors, clustersize;
317 : :
318 : 1 : CTxMemPool& pool = *Assert(m_node.mempool);
319 [ + - ]: 1 : LOCK2(cs_main, pool.cs);
320 : 1 : TestMemPoolEntryHelper entry;
321 : :
322 : : /* Base transaction */
323 : : //
324 : : // [tx1]
325 : : //
326 [ + - + - ]: 2 : CTransactionRef tx1 = make_tx(/*output_values=*/{10 * COIN});
327 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(10000LL).FromTx(tx1));
328 : :
329 : : // Ancestors / clustersize should be 1 / 1 (itself / itself)
330 [ + - ]: 1 : pool.GetTransactionAncestry(tx1->GetHash(), ancestors, clustersize);
331 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ancestors, 1ULL);
332 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(clustersize, 1ULL);
333 : :
334 : : /* Child transaction */
335 : : //
336 : : // [tx1].0 <- [tx2]
337 : : //
338 [ + - + - : 3 : CTransactionRef tx2 = make_tx(/*output_values=*/{495 * CENT, 5 * COIN}, /*inputs=*/{tx1});
+ + + - -
- - - ]
339 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(10000LL).FromTx(tx2));
340 : :
341 : : // Ancestors / clustersize should be:
342 : : // transaction ancestors clustersize
343 : : // ============ =========== ===========
344 : : // tx1 1 (tx1) 2 (tx1,2)
345 : : // tx2 2 (tx1,2) 2 (tx1,2)
346 [ + - ]: 1 : pool.GetTransactionAncestry(tx1->GetHash(), ancestors, clustersize);
347 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ancestors, 1ULL);
348 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(clustersize, 2ULL);
349 [ + - ]: 1 : pool.GetTransactionAncestry(tx2->GetHash(), ancestors, clustersize);
350 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ancestors, 2ULL);
351 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(clustersize, 2ULL);
352 : :
353 : : /* Grand-child 1 */
354 : : //
355 : : // [tx1].0 <- [tx2].0 <- [tx3]
356 : : //
357 [ + - + - : 3 : CTransactionRef tx3 = make_tx(/*output_values=*/{290 * CENT, 200 * CENT}, /*inputs=*/{tx2});
+ + + - -
- - - ]
358 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(10000LL).FromTx(tx3));
359 : :
360 : : // Ancestors / clustersize should be:
361 : : // transaction ancestors clustersize
362 : : // ============ =========== ===========
363 : : // tx1 1 (tx1) 3 (tx1,2,3)
364 : : // tx2 2 (tx1,2) 3 (tx1,2,3)
365 : : // tx3 3 (tx1,2,3) 3 (tx1,2,3)
366 [ + - ]: 1 : pool.GetTransactionAncestry(tx1->GetHash(), ancestors, clustersize);
367 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ancestors, 1ULL);
368 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(clustersize, 3ULL);
369 [ + - ]: 1 : pool.GetTransactionAncestry(tx2->GetHash(), ancestors, clustersize);
370 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ancestors, 2ULL);
371 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(clustersize, 3ULL);
372 [ + - ]: 1 : pool.GetTransactionAncestry(tx3->GetHash(), ancestors, clustersize);
373 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ancestors, 3ULL);
374 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(clustersize, 3ULL);
375 : :
376 : : /* Grand-child 2 */
377 : : //
378 : : // [tx1].0 <- [tx2].0 <- [tx3]
379 : : // |
380 : : // \---1 <- [tx4]
381 : : //
382 [ + - + - : 5 : CTransactionRef tx4 = make_tx(/*output_values=*/{290 * CENT, 250 * CENT}, /*inputs=*/{tx2}, /*input_indices=*/{1});
+ - + + +
- + - - -
- - ]
383 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(10000LL).FromTx(tx4));
384 : :
385 : : // Ancestors / clustersize should be:
386 : : // transaction ancestors clustersize
387 : : // ============ =========== ===========
388 : : // tx1 1 (tx1) 4 (tx1,2,3,4)
389 : : // tx2 2 (tx1,2) 4 (tx1,2,3,4)
390 : : // tx3 3 (tx1,2,3) 4 (tx1,2,3,4)
391 : : // tx4 3 (tx1,2,4) 4 (tx1,2,3,4)
392 [ + - ]: 1 : pool.GetTransactionAncestry(tx1->GetHash(), ancestors, clustersize);
393 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ancestors, 1ULL);
394 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(clustersize, 4ULL);
395 [ + - ]: 1 : pool.GetTransactionAncestry(tx2->GetHash(), ancestors, clustersize);
396 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ancestors, 2ULL);
397 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(clustersize, 4ULL);
398 [ + - ]: 1 : pool.GetTransactionAncestry(tx3->GetHash(), ancestors, clustersize);
399 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ancestors, 3ULL);
400 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(clustersize, 4ULL);
401 [ + - ]: 1 : pool.GetTransactionAncestry(tx4->GetHash(), ancestors, clustersize);
402 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ancestors, 3ULL);
403 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(clustersize, 4ULL);
404 : :
405 : : /* Make an alternate branch that is longer and connect it to tx3 */
406 : : //
407 : : // [ty1].0 <- [ty2].0 <- [ty3].0 <- [ty4].0 <- [ty5].0
408 : : // |
409 : : // [tx1].0 <- [tx2].0 <- [tx3].0 <- [ty6] --->--/
410 : : // |
411 : : // \---1 <- [tx4]
412 : : //
413 : 1 : CTransactionRef ty1, ty2, ty3, ty4, ty5;
414 : 1 : CTransactionRef* ty[5] = {&ty1, &ty2, &ty3, &ty4, &ty5};
415 : 1 : CAmount v = 5 * COIN;
416 [ + + ]: 6 : for (uint64_t i = 0; i < 5; i++) {
417 : 5 : CTransactionRef& tyi = *ty[i];
418 [ + + + - : 22 : tyi = make_tx(/*output_values=*/{v}, /*inputs=*/i > 0 ? std::vector<CTransactionRef>{*ty[i - 1]} : std::vector<CTransactionRef>{});
+ - + - -
+ + + + +
+ - - - -
- - - ]
419 : 5 : v -= 50 * CENT;
420 [ + - + - ]: 5 : TryAddToMempool(pool, entry.Fee(10000LL).FromTx(tyi));
421 [ + - ]: 5 : pool.GetTransactionAncestry(tyi->GetHash(), ancestors, clustersize);
422 [ + - + - ]: 5 : BOOST_CHECK_EQUAL(ancestors, i+1);
423 [ + - + - ]: 5 : BOOST_CHECK_EQUAL(clustersize, i+1);
424 : : }
425 [ + - + - : 4 : CTransactionRef ty6 = make_tx(/*output_values=*/{5 * COIN}, /*inputs=*/{tx3, ty5});
+ + + - -
- - - ]
426 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(10000LL).FromTx(ty6));
427 : :
428 : : // Ancestors / clustersize should be:
429 : : // transaction ancestors clustersize
430 : : // ============ =================== ===========
431 : : // tx1 1 (tx1) 10 (tx1-5, ty1-5)
432 : : // tx2 2 (tx1,2) 10
433 : : // tx3 3 (tx1,2,3) 10
434 : : // tx4 3 (tx1,2,4) 10
435 : : // ty1 1 (ty1) 10
436 : : // ty2 2 (ty1,2) 10
437 : : // ty3 3 (ty1,2,3) 10
438 : : // ty4 4 (y1234) 10
439 : : // ty5 5 (y12345) 10
440 : : // ty6 9 (tx123, ty123456) 10
441 [ + - ]: 1 : pool.GetTransactionAncestry(tx1->GetHash(), ancestors, clustersize);
442 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ancestors, 1ULL);
443 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(clustersize, 10ULL);
444 [ + - ]: 1 : pool.GetTransactionAncestry(tx2->GetHash(), ancestors, clustersize);
445 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ancestors, 2ULL);
446 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(clustersize, 10ULL);
447 [ + - ]: 1 : pool.GetTransactionAncestry(tx3->GetHash(), ancestors, clustersize);
448 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ancestors, 3ULL);
449 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(clustersize, 10ULL);
450 [ + - ]: 1 : pool.GetTransactionAncestry(tx4->GetHash(), ancestors, clustersize);
451 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ancestors, 3ULL);
452 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(clustersize, 10ULL);
453 [ + - ]: 1 : pool.GetTransactionAncestry(ty1->GetHash(), ancestors, clustersize);
454 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ancestors, 1ULL);
455 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(clustersize, 10ULL);
456 [ + - ]: 1 : pool.GetTransactionAncestry(ty2->GetHash(), ancestors, clustersize);
457 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ancestors, 2ULL);
458 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(clustersize, 10ULL);
459 [ + - ]: 1 : pool.GetTransactionAncestry(ty3->GetHash(), ancestors, clustersize);
460 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ancestors, 3ULL);
461 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(clustersize, 10ULL);
462 [ + - ]: 1 : pool.GetTransactionAncestry(ty4->GetHash(), ancestors, clustersize);
463 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ancestors, 4ULL);
464 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(clustersize, 10ULL);
465 [ + - ]: 1 : pool.GetTransactionAncestry(ty5->GetHash(), ancestors, clustersize);
466 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ancestors, 5ULL);
467 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(clustersize, 10ULL);
468 [ + - ]: 1 : pool.GetTransactionAncestry(ty6->GetHash(), ancestors, clustersize);
469 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ancestors, 9ULL);
470 [ + - + - : 1 : BOOST_CHECK_EQUAL(clustersize, 10ULL);
+ - ]
471 [ + - + - : 25 : }
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - ]
472 : :
473 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(MempoolAncestryTestsDiamond)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
474 : : {
475 : 1 : size_t ancestors, descendants;
476 : :
477 : 1 : CTxMemPool& pool = *Assert(m_node.mempool);
478 [ + - ]: 1 : LOCK2(::cs_main, pool.cs);
479 : 1 : TestMemPoolEntryHelper entry;
480 : :
481 : : /* Ancestors represented more than once ("diamond") */
482 : : //
483 : : // [ta].0 <- [tb].0 -----<------- [td].0
484 : : // | |
485 : : // \---1 <- [tc].0 --<--/
486 : : //
487 : 1 : CTransactionRef ta, tb, tc, td;
488 [ + - + - : 2 : ta = make_tx(/*output_values=*/{10 * COIN});
- + ]
489 [ + - + - : 3 : tb = make_tx(/*output_values=*/{5 * COIN, 3 * COIN}, /*inputs=*/ {ta});
- + + + +
- - - -
- ]
490 [ + - + - : 5 : tc = make_tx(/*output_values=*/{2 * COIN}, /*inputs=*/{tb}, /*input_indices=*/{1});
+ - - + +
+ + - + -
- - - - ]
491 [ + - + - : 6 : td = make_tx(/*output_values=*/{6 * COIN}, /*inputs=*/{tb, tc}, /*input_indices=*/{0, 0});
+ - - + +
+ + - + -
- - - - ]
492 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(10000LL).FromTx(ta));
493 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(10000LL).FromTx(tb));
494 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(10000LL).FromTx(tc));
495 [ + - + - ]: 1 : TryAddToMempool(pool, entry.Fee(10000LL).FromTx(td));
496 : :
497 : : // Ancestors / descendants should be:
498 : : // transaction ancestors descendants
499 : : // ============ =================== ===========
500 : : // ta 1 (ta 4 (ta,tb,tc,td)
501 : : // tb 2 (ta,tb) 4 (ta,tb,tc,td)
502 : : // tc 3 (ta,tb,tc) 4 (ta,tb,tc,td)
503 : : // td 4 (ta,tb,tc,td) 4 (ta,tb,tc,td)
504 [ + - ]: 1 : pool.GetTransactionAncestry(ta->GetHash(), ancestors, descendants);
505 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ancestors, 1ULL);
506 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(descendants, 4ULL);
507 [ + - ]: 1 : pool.GetTransactionAncestry(tb->GetHash(), ancestors, descendants);
508 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ancestors, 2ULL);
509 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(descendants, 4ULL);
510 [ + - ]: 1 : pool.GetTransactionAncestry(tc->GetHash(), ancestors, descendants);
511 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ancestors, 3ULL);
512 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(descendants, 4ULL);
513 [ + - ]: 1 : pool.GetTransactionAncestry(td->GetHash(), ancestors, descendants);
514 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(ancestors, 4ULL);
515 [ + - + - : 1 : BOOST_CHECK_EQUAL(descendants, 4ULL);
+ - ]
516 [ + - + - : 13 : }
+ - + - +
- + - + -
+ - + - +
- + - +
- ]
517 : :
518 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(MempoolUnbroadcastMemoryUsage)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
519 : : {
520 : 1 : CTxMemPool& pool{*Assert(m_node.mempool)};
521 [ + - ]: 1 : LOCK2(cs_main, pool.cs);
522 : :
523 [ + - + - ]: 2 : auto tx{make_tx({10 * COIN})};
524 [ + - ]: 1 : auto txid{tx->GetHash()};
525 [ + - + - ]: 1 : TryAddToMempool(pool, TestMemPoolEntryHelper{}.Fee(1000).FromTx(tx));
526 : :
527 [ + - ]: 1 : auto usage_before{pool.DynamicMemoryUsage()};
528 [ + - ]: 1 : pool.AddUnbroadcastTx(txid);
529 [ + - + - : 1 : BOOST_CHECK_GT(pool.DynamicMemoryUsage(), usage_before);
+ - ]
530 : :
531 [ + - ]: 1 : pool.RemoveUnbroadcastTx(txid);
532 [ + - + - : 1 : BOOST_CHECK_EQUAL(pool.DynamicMemoryUsage(), usage_before);
+ - + - ]
533 [ + - + - ]: 3 : }
534 : :
535 : : BOOST_AUTO_TEST_SUITE_END()
|