Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
orphanage_tests.cpp
Go to the documentation of this file.
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 <arith_uint256.h>
7#include <node/txorphanage.h>
8#include <policy/policy.h>
10#include <pubkey.h>
11#include <script/sign.h>
13#include <test/util/common.h>
14#include <test/util/random.h>
16#include <test/util/time.h>
18
19#include <array>
20#include <cstdint>
21
22#include <boost/test/unit_test.hpp>
23
25
27{
28 std::vector<unsigned char> keydata;
29 keydata = rand_ctx.randbytes(32);
30 key.Set(keydata.data(), keydata.data() + keydata.size(), /*fCompressedIn=*/true);
31 assert(key.IsValid());
32}
33
34// Creates a transaction with 2 outputs. Spends all outpoints. If outpoints is empty, spends a random one.
35static CTransactionRef MakeTransactionSpending(const std::vector<COutPoint>& outpoints, FastRandomContext& det_rand)
36{
37 CKey key;
40 // If no outpoints are given, create a random one.
41 if (outpoints.empty()) {
42 tx.vin.emplace_back(Txid::FromUint256(det_rand.rand256()), 0);
43 } else {
44 for (const auto& outpoint : outpoints) {
45 tx.vin.emplace_back(outpoint);
46 }
47 }
48 // Ensure txid != wtxid
49 tx.vin[0].scriptWitness.stack.push_back({1});
50 tx.vout.resize(2);
51 tx.vout[0].nValue = CENT;
52 tx.vout[0].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));
53 tx.vout[1].nValue = 3 * CENT;
54 tx.vout[1].scriptPubKey = GetScriptForDestination(WitnessV0KeyHash(key.GetPubKey()));
55 return MakeTransactionRef(tx);
56}
57
58// Make another (not necessarily valid) tx with the same txid but different wtxid.
60{
62 tx.vin[0].scriptWitness.stack.push_back({5});
64 assert(ptx->GetHash() == mutated_tx->GetHash());
65 return mutated_tx;
66}
67
68static bool EqualTxns(const std::set<CTransactionRef>& set_txns, const std::vector<CTransactionRef>& vec_txns)
69{
70 if (vec_txns.size() != set_txns.size()) return false;
71 for (const auto& tx : vec_txns) {
72 if (!set_txns.contains(tx)) return false;
73 }
74 return true;
75}
76
78{
80
81 // Construct transactions to use. They must all be the same size.
82 static constexpr unsigned int NUM_TXNS_CREATED = 100;
83 static constexpr int64_t TX_SIZE{469};
84 static constexpr int64_t TOTAL_SIZE = NUM_TXNS_CREATED * TX_SIZE;
85
86 std::vector<CTransactionRef> txns;
87 txns.reserve(NUM_TXNS_CREATED);
88 // All transactions are the same size.
89 for (unsigned int i{0}; i < NUM_TXNS_CREATED; ++i) {
91 txns.emplace_back(ptx);
93 }
94
95 // Single peer: eviction is triggered if either limit is hit
96 {
97 // Test announcement limits
98 NodeId peer{8};
99 auto orphanage_low_ann = node::MakeTxOrphanage(/*max_global_latency_score=*/1, /*reserved_peer_usage=*/TX_SIZE * 10);
100 auto orphanage_low_mem = node::MakeTxOrphanage(/*max_global_latency_score=*/10, /*reserved_peer_usage=*/TX_SIZE);
101
102 // Add the first transaction
103 orphanage_low_ann->AddTx(txns.at(0), peer);
104 orphanage_low_mem->AddTx(txns.at(0), peer);
105
106 // Add more. One of the limits is exceeded, so LimitOrphans evicts 1.
107 orphanage_low_ann->AddTx(txns.at(1), peer);
108 orphanage_low_mem->AddTx(txns.at(1), peer);
109
110 // The older transaction is evicted.
111 BOOST_CHECK(!orphanage_low_ann->HaveTx(txns.at(0)->GetWitnessHash()));
112 BOOST_CHECK(!orphanage_low_mem->HaveTx(txns.at(0)->GetWitnessHash()));
113 BOOST_CHECK(orphanage_low_ann->HaveTx(txns.at(1)->GetWitnessHash()));
114 BOOST_CHECK(orphanage_low_mem->HaveTx(txns.at(1)->GetWitnessHash()));
115
116 orphanage_low_ann->SanityCheck();
117 orphanage_low_mem->SanityCheck();
118 }
119
120 // Single peer: latency score includes inputs
121 {
122 // Test latency score limits
123 NodeId peer{10};
124 auto orphanage_low_ann = node::MakeTxOrphanage(/*max_global_latency_score=*/5, /*reserved_peer_usage=*/TX_SIZE * 1000);
125
126 // Add the first transaction
127 orphanage_low_ann->AddTx(txns.at(0), peer);
128
129 // Add 1 more transaction with 45 inputs. Even though there are only 2 announcements, this pushes the orphanage above its maximum latency score.
130 std::vector<COutPoint> outpoints_45;
131 for (unsigned int j{0}; j < 45; ++j) {
132 outpoints_45.emplace_back(Txid::FromUint256(det_rand.rand256()), j);
133 }
135 orphanage_low_ann->AddTx(ptx, peer);
136
137 // The older transaction is evicted.
138 BOOST_CHECK(!orphanage_low_ann->HaveTx(txns.at(0)->GetWitnessHash()));
139 BOOST_CHECK(orphanage_low_ann->HaveTx(ptx->GetWitnessHash()));
140
141 orphanage_low_ann->SanityCheck();
142 }
143
144 // Single peer: eviction order is FIFO on non-reconsiderable, then reconsiderable orphans.
145 {
146 // Construct parent + child pairs
147 std::vector<CTransactionRef> parents;
148 std::vector<CTransactionRef> children;
149 for (unsigned int i{0}; i < 10; ++i) {
151 CTransactionRef child = MakeTransactionSpending({{parent->GetHash(), 0}}, det_rand);
152 parents.emplace_back(parent);
153 children.emplace_back(child);
154 }
155
156 // Test announcement limits
157 NodeId peer{9};
158 auto orphanage = node::MakeTxOrphanage(/*max_global_latency_score=*/3, /*reserved_peer_usage=*/TX_SIZE * 10);
159
160 // First add a tx which will be made reconsiderable.
161 orphanage->AddTx(children.at(0), peer);
162
163 // Then add 2 more orphans... not oversize yet.
164 orphanage->AddTx(children.at(1), peer);
165 orphanage->AddTx(children.at(2), peer);
166
167 // Make child0 ready to reconsider
168 const std::vector<std::pair<Wtxid, NodeId>> expected_set_c0{std::make_pair(children.at(0)->GetWitnessHash(), peer)};
169 BOOST_CHECK(orphanage->AddChildrenToWorkSet(*parents.at(0), det_rand) == expected_set_c0);
170 BOOST_CHECK(orphanage->HaveTxToReconsider(peer));
171
172 // Add 1 more orphan, causing the orphanage to be oversize. child1 is evicted.
173 orphanage->AddTx(children.at(3), peer);
174 BOOST_CHECK(orphanage->HaveTx(children.at(0)->GetWitnessHash()));
175 BOOST_CHECK(!orphanage->HaveTx(children.at(1)->GetWitnessHash()));
176 BOOST_CHECK(orphanage->HaveTx(children.at(2)->GetWitnessHash()));
177 BOOST_CHECK(orphanage->HaveTx(children.at(3)->GetWitnessHash()));
178 orphanage->SanityCheck();
179
180 // Add 1 more... child2 is evicted.
181 orphanage->AddTx(children.at(4), peer);
182 BOOST_CHECK(orphanage->HaveTx(children.at(0)->GetWitnessHash()));
183 BOOST_CHECK(!orphanage->HaveTx(children.at(2)->GetWitnessHash()));
184 BOOST_CHECK(orphanage->HaveTx(children.at(3)->GetWitnessHash()));
185 BOOST_CHECK(orphanage->HaveTx(children.at(4)->GetWitnessHash()));
186
187 // Eviction order is FIFO within the orphans that are read
188 const std::vector<std::pair<Wtxid, NodeId>> expected_set_c4{std::make_pair(children.at(4)->GetWitnessHash(), peer)};
189 BOOST_CHECK(orphanage->AddChildrenToWorkSet(*parents.at(4), det_rand) == expected_set_c4);
190 const std::vector<std::pair<Wtxid, NodeId>> expected_set_c3{std::make_pair(children.at(3)->GetWitnessHash(), peer)};
191 BOOST_CHECK(orphanage->AddChildrenToWorkSet(*parents.at(3), det_rand) == expected_set_c3);
192
193 // child5 is evicted immediately because it is the only non-reconsiderable orphan.
194 orphanage->AddTx(children.at(5), peer);
195 BOOST_CHECK(orphanage->HaveTx(children.at(0)->GetWitnessHash()));
196 BOOST_CHECK(orphanage->HaveTx(children.at(3)->GetWitnessHash()));
197 BOOST_CHECK(orphanage->HaveTx(children.at(4)->GetWitnessHash()));
198 BOOST_CHECK(!orphanage->HaveTx(children.at(5)->GetWitnessHash()));
199
200 // Transactions are marked non-reconsiderable again when returned through GetTxToReconsider
201 BOOST_CHECK_EQUAL(orphanage->GetTxToReconsider(peer), children.at(0));
202 orphanage->AddTx(children.at(6), peer);
203 BOOST_CHECK(!orphanage->HaveTx(children.at(0)->GetWitnessHash()));
204 BOOST_CHECK(orphanage->HaveTx(children.at(3)->GetWitnessHash()));
205 BOOST_CHECK(orphanage->HaveTx(children.at(4)->GetWitnessHash()));
206 BOOST_CHECK(orphanage->HaveTx(children.at(6)->GetWitnessHash()));
207
208 // The first transaction returned from GetTxToReconsider is the older one, not the one that was marked for
209 // reconsideration earlier.
210 BOOST_CHECK_EQUAL(orphanage->GetTxToReconsider(peer), children.at(3));
211 BOOST_CHECK_EQUAL(orphanage->GetTxToReconsider(peer), children.at(4));
212
213 orphanage->SanityCheck();
214 }
215
216 // Multiple peers: when limit is exceeded, we choose the DoSiest peer and evict their oldest transaction.
217 {
218 NodeId peer_dosy{0};
219 NodeId peer1{1};
220 NodeId peer2{2};
221
222 unsigned int max_announcements = 60;
223 // Set a high per-peer reservation so announcement limit is always hit first.
225
226 // No evictions happen before the global limit is reached.
227 for (unsigned int i{0}; i < max_announcements; ++i) {
228 orphanage->AddTx(txns.at(i), peer_dosy);
229 }
230 orphanage->SanityCheck();
231 BOOST_CHECK_EQUAL(orphanage->AnnouncementsFromPeer(peer_dosy), max_announcements);
232 BOOST_CHECK_EQUAL(orphanage->AnnouncementsFromPeer(peer1), 0);
233 BOOST_CHECK_EQUAL(orphanage->AnnouncementsFromPeer(peer2), 0);
234
235 // Add 10 unique transactions from peer1.
236 // LimitOrphans should evict from peer_dosy, because that's the one exceeding announcement limits.
237 unsigned int num_from_peer1 = 10;
238 for (unsigned int i{0}; i < num_from_peer1; ++i) {
239 orphanage->AddTx(txns.at(max_announcements + i), peer1);
240 // The announcement limit per peer has halved, but LimitOrphans does not evict beyond what is necessary to
241 // bring the total announcements within its global limit.
242 BOOST_CHECK(orphanage->AnnouncementsFromPeer(peer_dosy) > orphanage->MaxPeerLatencyScore());
243
244 BOOST_CHECK_EQUAL(orphanage->AnnouncementsFromPeer(peer1), i + 1);
245 BOOST_CHECK_EQUAL(orphanage->AnnouncementsFromPeer(peer_dosy), max_announcements - i - 1);
246
247 // Evictions are FIFO within a peer, so the ith transaction sent by peer_dosy is the one that was evicted.
248 BOOST_CHECK(!orphanage->HaveTx(txns.at(i)->GetWitnessHash()));
249 }
250 // Add 10 transactions that are duplicates of the ones sent by peer_dosy. We need to add 10 because the first 10
251 // were just evicted in the previous block additions.
252 for (unsigned int i{num_from_peer1}; i < num_from_peer1 + 10; ++i) {
253 // Tx has already been sent by peer_dosy
254 BOOST_CHECK(orphanage->HaveTxFromPeer(txns.at(i)->GetWitnessHash(), peer_dosy));
255 orphanage->AddTx(txns.at(i), peer2);
256
257 // peer_dosy is still the only one getting evicted
258 BOOST_CHECK_EQUAL(orphanage->AnnouncementsFromPeer(peer_dosy), max_announcements - i - 1);
259 BOOST_CHECK_EQUAL(orphanage->AnnouncementsFromPeer(peer1), num_from_peer1);
260 BOOST_CHECK_EQUAL(orphanage->AnnouncementsFromPeer(peer2), i + 1 - num_from_peer1);
261
262 // Evictions are FIFO within a peer, so the ith transaction sent by peer_dosy is the one that was evicted.
263 BOOST_CHECK(!orphanage->HaveTxFromPeer(txns.at(i)->GetWitnessHash(), peer_dosy));
264 BOOST_CHECK(orphanage->HaveTx(txns.at(i)->GetWitnessHash()));
265 }
266
267 // With 6 peers, each can add 10, and still only peer_dosy's orphans are evicted.
268 const unsigned int max_per_peer{max_announcements / 6};
269 const unsigned int num_announcements{orphanage->CountAnnouncements()};
270 for (NodeId peer{3}; peer < 6; ++peer) {
271 for (unsigned int i{0}; i < max_per_peer; ++i) {
272 // Each addition causes 1 eviction.
273 orphanage->AddTx(txns.at(peer * max_per_peer + i), peer);
274 BOOST_CHECK_EQUAL(orphanage->CountAnnouncements(), num_announcements);
275 }
276 }
277 for (NodeId peer{0}; peer < 6; ++peer) {
278 BOOST_CHECK_EQUAL(orphanage->AnnouncementsFromPeer(peer), max_per_peer);
279 }
280 orphanage->SanityCheck();
281 }
282
283 // Limits change as more peers are added.
284 {
286 // These stay the same regardless of number of peers
289
290 // These change with number of peers
293
294 // Number of peers = 1
295 orphanage->AddTx(txns.at(0), 0);
300
301 // Number of peers = 2
302 orphanage->AddTx(txns.at(1), 1);
307
308 // Number of peers = 3
309 orphanage->AddTx(txns.at(2), 2);
314
315 // Number of peers didn't change.
316 orphanage->AddTx(txns.at(3), 2);
321
322 // Once a peer has no orphans, it is not considered in the limits.
323 // Number of peers = 2
324 orphanage->EraseForPeer(2);
329
330 // Number of peers = 1
331 orphanage->EraseTx(txns.at(0)->GetWitnessHash());
336
337 orphanage->SanityCheck();
338 }
339
340 // Test eviction of multiple transactions at a time
341 {
342 // Create a large transaction that is 10 times larger than the normal size transaction.
344 tx_large.vin.resize(1);
347
351
353 // One peer sends 10 normal size transactions. The other peer sends 10 normal transactions and 1 very large one
356 for (unsigned int i = 0; i < 20; i++) {
357 orphanage->AddTx(txns.at(i), i < 10 ? peer_normal : peer_large);
358 }
359 BOOST_CHECK(orphanage->TotalLatencyScore() <= orphanage->MaxGlobalLatencyScore());
360 BOOST_CHECK(orphanage->TotalOrphanUsage() <= orphanage->MaxGlobalUsage());
361
362 // Add the large transaction. This should cause evictions of all the previous 10 transactions from that peer.
364
365 // peer_normal should still have 10 transactions, and peer_large should have 1.
366 BOOST_CHECK_EQUAL(orphanage->AnnouncementsFromPeer(peer_normal), 10);
367 BOOST_CHECK_EQUAL(orphanage->AnnouncementsFromPeer(peer_large), 1);
368 BOOST_CHECK(orphanage->HaveTxFromPeer(ptx_large->GetWitnessHash(), peer_large));
369 BOOST_CHECK_EQUAL(orphanage->CountAnnouncements(), 11);
370
371 orphanage->SanityCheck();
372 }
373
374 // Test that latency score includes number of inputs.
375 {
377
378 // Add 10 transactions with 9 inputs each.
379 std::vector<COutPoint> outpoints_9;
380 for (unsigned int j{0}; j < 9; ++j) {
381 outpoints_9.emplace_back(Txid::FromUint256(m_rng.rand256()), j);
382 }
383 for (unsigned int i{0}; i < 10; ++i) {
385 orphanage->AddTx(ptx, 0);
386 }
387 BOOST_CHECK_EQUAL(orphanage->CountAnnouncements(), 10);
388 BOOST_CHECK_EQUAL(orphanage->TotalLatencyScore(), 10);
389
390 // Add 10 transactions with 50 inputs each.
391 std::vector<COutPoint> outpoints_50;
392 for (unsigned int j{0}; j < 50; ++j) {
393 outpoints_50.emplace_back(Txid::FromUint256(m_rng.rand256()), j);
394 }
395
396 for (unsigned int i{0}; i < 10; ++i) {
398 std::shuffle(outpoints_50.begin(), outpoints_50.end(), m_rng);
400 BOOST_CHECK(orphanage->AddTx(ptx, 0));
401 if (i < 5) BOOST_CHECK(!orphanage->AddTx(ptx, 1));
402 }
403 // 10 of the 9-input transactions + 10 of the 50-input transactions + 5 more announcements of the 50-input transactions
404 BOOST_CHECK_EQUAL(orphanage->CountAnnouncements(), 25);
405 // Base of 25 announcements, plus 10 * 5 for the 50-input transactions (counted just once)
406 BOOST_CHECK_EQUAL(orphanage->TotalLatencyScore(), 25 + 50);
407
408 // Peer 0 sent all 20 transactions
409 BOOST_CHECK_EQUAL(orphanage->AnnouncementsFromPeer(0), 20);
410 BOOST_CHECK_EQUAL(orphanage->LatencyScoreFromPeer(0), 20 + 10 * 5);
411
412 // Peer 1 sent 5 of the 10 transactions with many inputs
413 BOOST_CHECK_EQUAL(orphanage->AnnouncementsFromPeer(1), 5);
414 BOOST_CHECK_EQUAL(orphanage->LatencyScoreFromPeer(1), 5 + 5 * 5);
415
416 orphanage->SanityCheck();
417 }
418}
420{
421 // This test had non-deterministic coverage due to
422 // randomly selected seeds.
423 // This seed is chosen so that all branches of the function
424 // ecdsa_signature_parse_der_lax are executed during this test.
425 // Specifically branches that run only when an ECDSA
426 // signature's R and S values have leading zeros.
427 m_rng.Reseed(uint256{33});
428
429 std::unique_ptr<node::TxOrphanage> orphanage{node::MakeTxOrphanage()};
430 CKey key;
433 BOOST_CHECK(keystore.AddKey(key));
434
436
437 std::vector<CTransactionRef> orphans_added;
438
439 // 50 orphan transactions:
440 for (int i = 0; i < 50; i++)
441 {
443 tx.vin.resize(1);
444 tx.vin[0].prevout.n = 0;
445 tx.vin[0].prevout.hash = Txid::FromUint256(m_rng.rand256());
446 tx.vin[0].scriptSig << OP_1;
447 tx.vout.resize(1);
448 tx.vout[0].nValue = i*CENT;
449 tx.vout[0].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));
450
451 auto ptx = MakeTransactionRef(tx);
452 orphanage->AddTx(ptx, i);
453 orphans_added.emplace_back(ptx);
454 }
455
456 // ... and 50 that depend on other orphans:
457 for (int i = 0; i < 50; i++)
458 {
459 const auto& txPrev = orphans_added[m_rng.randrange(orphans_added.size())];
460
462 tx.vin.resize(1);
463 tx.vin[0].prevout.n = 0;
464 tx.vin[0].prevout.hash = txPrev->GetHash();
465 tx.vout.resize(1);
466 tx.vout[0].nValue = i*CENT;
467 tx.vout[0].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));
468 SignatureData empty;
470
471 auto ptx = MakeTransactionRef(tx);
472 orphanage->AddTx(ptx, i);
473 orphans_added.emplace_back(ptx);
474 }
475
476 // This really-big orphan should be ignored:
477 for (int i = 0; i < 10; i++)
478 {
479 const auto& txPrev = orphans_added[m_rng.randrange(orphans_added.size())];
480
482 tx.vout.resize(1);
483 tx.vout[0].nValue = 1*CENT;
484 tx.vout[0].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));
485 tx.vin.resize(2777);
486 for (unsigned int j = 0; j < tx.vin.size(); j++)
487 {
488 tx.vin[j].prevout.n = j;
489 tx.vin[j].prevout.hash = txPrev->GetHash();
490 }
491 SignatureData empty;
493 // Reuse same signature for other inputs
494 // (they don't have to be valid for this test)
495 for (unsigned int j = 1; j < tx.vin.size(); j++)
496 tx.vin[j].scriptSig = tx.vin[0].scriptSig;
497
499 }
500
501 size_t expected_num_orphans = orphanage->CountUniqueOrphans();
502
503 // Non-existent peer; nothing should be deleted
504 orphanage->EraseForPeer(/*peer=*/-1);
505 BOOST_CHECK_EQUAL(orphanage->CountUniqueOrphans(), expected_num_orphans);
506
507 // Each of first three peers stored
508 // two transactions each.
509 for (NodeId i = 0; i < 3; i++)
510 {
511 orphanage->EraseForPeer(i);
513 BOOST_CHECK_EQUAL(orphanage->CountUniqueOrphans(), expected_num_orphans);
514 }
515}
516
518{
520 std::unique_ptr<node::TxOrphanage> orphanage{node::MakeTxOrphanage()};
521 NodeId peer{0};
522
523 std::vector<COutPoint> empty_outpoints;
525
526 // Create children to go into orphanage.
527 auto child_normal = MakeTransactionSpending({{parent->GetHash(), 0}}, det_rand);
529
530 const auto& normal_wtxid = child_normal->GetWitnessHash();
531 const auto& mutated_wtxid = child_mutated->GetWitnessHash();
533
534 BOOST_CHECK(orphanage->AddTx(child_normal, peer));
535 // EraseTx fails as transaction by this wtxid doesn't exist.
540 BOOST_CHECK(orphanage->GetTx(mutated_wtxid) == nullptr);
541
542 // Must succeed. Both transactions should be present in orphanage.
543 BOOST_CHECK(orphanage->AddTx(child_mutated, peer));
546
547 // Outpoints map should track all entries: check that both are returned as children of the parent.
548 std::set<CTransactionRef> expected_children{child_normal, child_mutated};
549 BOOST_CHECK(EqualTxns(expected_children, orphanage->GetChildrenFromSamePeer(parent, peer)));
550
551 // Erase by wtxid: mutated first
555
559}
560
561
563{
565 std::vector<COutPoint> empty_outpoints;
566
569
570 // Make sure these parents have different txids otherwise this test won't make sense.
571 while (parent1->GetHash() == parent2->GetHash()) {
573 }
574
575 // Create children to go into orphanage.
576 auto child_p1n0 = MakeTransactionSpending({{parent1->GetHash(), 0}}, det_rand);
577 auto child_p2n1 = MakeTransactionSpending({{parent2->GetHash(), 1}}, det_rand);
578 // Spends the same tx twice. Should not cause duplicates.
579 auto child_p1n0_p1n1 = MakeTransactionSpending({{parent1->GetHash(), 0}, {parent1->GetHash(), 1}}, det_rand);
580 // Spends the same outpoint as previous tx. Should still be returned; don't assume outpoints are unique.
581 auto child_p1n0_p2n0 = MakeTransactionSpending({{parent1->GetHash(), 0}, {parent2->GetHash(), 0}}, det_rand);
582
583 const NodeId node0{0};
584 const NodeId node1{1};
585 const NodeId node2{2};
586 const NodeId node3{3};
587
588 // All orphans provided by node1
589 {
595
596 // Also add some other announcers for the same transactions
600
601
603 std::vector<CTransactionRef> expected_parent2_children{child_p1n0_p2n0, child_p2n1};
604
605 BOOST_CHECK(expected_parent1_children == orphanage->GetChildrenFromSamePeer(parent1, node1));
606 BOOST_CHECK(expected_parent2_children == orphanage->GetChildrenFromSamePeer(parent2, node1));
607
608 // The peer must match
609 BOOST_CHECK(orphanage->GetChildrenFromSamePeer(parent1, node2).empty());
610 BOOST_CHECK(orphanage->GetChildrenFromSamePeer(parent2, node2).empty());
611
612 // There shouldn't be any children of this tx in the orphanage
613 BOOST_CHECK(orphanage->GetChildrenFromSamePeer(child_p1n0_p2n0, node1).empty());
614 BOOST_CHECK(orphanage->GetChildrenFromSamePeer(child_p1n0_p2n0, node2).empty());
615 }
616
617 // Orphans provided by node1 and node2
618 {
619 std::unique_ptr<node::TxOrphanage> orphanage{node::MakeTxOrphanage()};
624
625 // +----------------+---------------+----------------------------------+
626 // | | sender=node1 | sender=node2 |
627 // +----------------+---------------+----------------------------------+
628 // | spends parent1 | child_p1n0 | child_p1n0_p1n1, child_p1n0_p2n0 |
629 // | spends parent2 | child_p2n1 | child_p1n0_p2n0 |
630 // +----------------+---------------+----------------------------------+
631
632 // Children of parent1 from node1:
633 {
634 std::set<CTransactionRef> expected_parent1_node1{child_p1n0};
635
636 BOOST_CHECK_EQUAL(orphanage->GetChildrenFromSamePeer(parent1, node1).size(), 1);
637 BOOST_CHECK(orphanage->HaveTxFromPeer(child_p1n0->GetWitnessHash(), node1));
639 }
640
641 // Children of parent2 from node1:
642 {
643 std::set<CTransactionRef> expected_parent2_node1{child_p2n1};
644
646 }
647
648 // Children of parent1 from node2: newest returned first.
649 {
650 std::vector<CTransactionRef> expected_parent1_node2{child_p1n0_p2n0, child_p1n0_p1n1};
651 BOOST_CHECK(orphanage->HaveTxFromPeer(child_p1n0_p1n1->GetWitnessHash(), node2));
652 BOOST_CHECK(orphanage->HaveTxFromPeer(child_p1n0_p2n0->GetWitnessHash(), node2));
653 BOOST_CHECK(expected_parent1_node2 == orphanage->GetChildrenFromSamePeer(parent1, node2));
654 }
655
656 // Children of parent2 from node2:
657 {
658 std::set<CTransactionRef> expected_parent2_node2{child_p1n0_p2n0};
659
660 BOOST_CHECK_EQUAL(1, orphanage->GetChildrenFromSamePeer(parent2, node2).size());
661 BOOST_CHECK(orphanage->HaveTxFromPeer(child_p1n0_p2n0->GetWitnessHash(), node2));
663 }
664 }
665}
666
668{
669 std::unique_ptr<node::TxOrphanage> orphanage{node::MakeTxOrphanage()};
671 tx.vin.resize(1);
672
673 // check that txs larger than MAX_STANDARD_TX_WEIGHT are not added to the orphanage
677
678 tx.vout.clear();
682}
683
685{
687 std::unique_ptr<node::TxOrphanage> orphanage{node::MakeTxOrphanage()};
688
689 // Create outpoints that will be spent by transactions in the block
690 std::vector<COutPoint> outpoints;
691 const uint32_t num_outpoints{6};
692 outpoints.reserve(num_outpoints);
693 for (uint32_t i{0}; i < num_outpoints; ++i) {
694 // All the hashes should be different, but change the n just in case.
695 outpoints.emplace_back(Txid::FromUint256(det_rand.rand256()), i);
696 }
697
698 CBlock block;
699 const NodeId node{0};
700
703
704 auto bo_tx_same_txid = MakeTransactionSpending({outpoints.at(0)}, det_rand);
706 block.vtx.emplace_back(bo_tx_same_txid);
707
708 // 2 transactions with the same txid but different witness
710 block.vtx.emplace_back(b_tx_same_txid_diff_witness);
711
714
715 // 2 different transactions that spend the same input.
716 auto b_tx_conflict = MakeTransactionSpending({outpoints.at(2)}, det_rand);
717 block.vtx.emplace_back(b_tx_conflict);
718
719 auto o_tx_conflict = MakeTransactionSpending({outpoints.at(2)}, det_rand);
721
722 // 2 different transactions that have 1 overlapping input.
723 auto b_tx_conflict_partial = MakeTransactionSpending({outpoints.at(3), outpoints.at(4)}, det_rand);
724 block.vtx.emplace_back(b_tx_conflict_partial);
725
726 auto o_tx_conflict_partial_2 = MakeTransactionSpending({outpoints.at(4), outpoints.at(5)}, det_rand);
728
729 orphanage->EraseForBlock(block);
731 const auto& expected_removed_wtxid = expected_removed->GetWitnessHash();
733 }
734 // Only remaining tx is control_tx
735 BOOST_CHECK_EQUAL(orphanage->CountUniqueOrphans(), 1);
736 BOOST_CHECK(orphanage->HaveTx(control_tx->GetWitnessHash()));
737}
738
740{
741 const NodeId node0{0};
742 const NodeId node1{1};
743 const NodeId node2{2};
744 size_t expected_total_count{0};
746 std::unique_ptr<node::TxOrphanage> orphanage{node::MakeTxOrphanage()};
747
748 // Check accounting per peer.
749 // Check that EraseForPeer works with multiple announcers.
750 {
752 const auto& wtxid = ptx->GetWitnessHash();
753 BOOST_CHECK(orphanage->AddTx(ptx, node0));
754 BOOST_CHECK(orphanage->HaveTx(wtxid));
756 BOOST_CHECK_EQUAL(orphanage->CountUniqueOrphans(), expected_total_count);
757
758 // Adding again should do nothing.
759 BOOST_CHECK(!orphanage->AddTx(ptx, node0));
760 BOOST_CHECK_EQUAL(orphanage->CountUniqueOrphans(), expected_total_count);
761
762 // We can add another tx with the same txid but different witness.
765 BOOST_CHECK(orphanage->HaveTx(ptx_mutated->GetWitnessHash()));
767
768 BOOST_CHECK(!orphanage->AddTx(ptx, node0));
769
770 // Adding a new announcer should not change overall accounting.
771 BOOST_CHECK(orphanage->AddAnnouncer(ptx->GetWitnessHash(), node2));
772 BOOST_CHECK_EQUAL(orphanage->CountUniqueOrphans(), expected_total_count);
773
774 // If we already have this announcer, AddAnnouncer returns false.
775 BOOST_CHECK(orphanage->HaveTxFromPeer(ptx->GetWitnessHash(), node2));
776 BOOST_CHECK(!orphanage->AddAnnouncer(ptx->GetWitnessHash(), node2));
777
778 // Same with using AddTx for an existing tx, which is equivalent to using AddAnnouncer
779 BOOST_CHECK(!orphanage->AddTx(ptx, node1));
780 BOOST_CHECK_EQUAL(orphanage->CountUniqueOrphans(), expected_total_count);
781
782 // if EraseForPeer is called for an orphan with multiple announcers, the orphanage should only
783 // erase that peer from the announcers set.
784 orphanage->EraseForPeer(node0);
785 BOOST_CHECK(orphanage->HaveTx(ptx->GetWitnessHash()));
786 BOOST_CHECK(!orphanage->HaveTxFromPeer(ptx->GetWitnessHash(), node0));
787 // node0 is the only one that announced ptx_mutated
788 BOOST_CHECK(!orphanage->HaveTx(ptx_mutated->GetWitnessHash()));
790 BOOST_CHECK_EQUAL(orphanage->CountUniqueOrphans(), expected_total_count);
791
792 // EraseForPeer should delete the orphan if it's the only announcer left.
793 orphanage->EraseForPeer(node1);
794 BOOST_CHECK_EQUAL(orphanage->CountUniqueOrphans(), expected_total_count);
795 BOOST_CHECK(orphanage->HaveTx(ptx->GetWitnessHash()));
796 orphanage->EraseForPeer(node2);
798 BOOST_CHECK_EQUAL(orphanage->CountUniqueOrphans(), expected_total_count);
799 BOOST_CHECK(!orphanage->HaveTx(ptx->GetWitnessHash()));
800 }
801
802 // Check that erasure for blocks removes for all peers.
803 {
804 CBlock block;
806 block.vtx.emplace_back(tx_block);
809
811
812 BOOST_CHECK_EQUAL(orphanage->CountUniqueOrphans(), expected_total_count);
813
814 orphanage->EraseForBlock(block);
815
817
818 BOOST_CHECK_EQUAL(orphanage->CountUniqueOrphans(), expected_total_count);
819 }
820}
822{
823 const NodeId node0{0};
824 const NodeId node1{1};
825 const NodeId node2{2};
827 std::unique_ptr<node::TxOrphanage> orphanage{node::MakeTxOrphanage()};
828 // AddChildrenToWorkSet should pick an announcer randomly
829 {
832 const auto& orphan_wtxid = tx_orphan->GetWitnessHash();
833
834 // All 3 peers are announcers.
837 BOOST_CHECK(orphanage->AddAnnouncer(orphan_wtxid, node2));
838 for (NodeId node = node0; node <= node2; ++node) {
839 BOOST_CHECK(orphanage->HaveTxFromPeer(orphan_wtxid, node));
840 }
841
842 // Parent accepted: child is added to 1 of 3 worksets.
843 auto newly_reconsiderable = orphanage->AddChildrenToWorkSet(*tx_missing_parent, det_rand);
845 int node0_reconsider = orphanage->HaveTxToReconsider(node0);
846 int node1_reconsider = orphanage->HaveTxToReconsider(node1);
847 int node2_reconsider = orphanage->HaveTxToReconsider(node2);
849
851 if (node0_reconsider) {
853 } else if (node1_reconsider) {
855 } else {
858 }
859
860 // EraseForPeer also removes that tx from the workset.
861 orphanage->EraseForPeer(assigned_peer);
862 BOOST_CHECK_EQUAL(orphanage->GetTxToReconsider(node0), nullptr);
863
864 // Delete this tx, clearing the orphanage.
866 BOOST_CHECK_EQUAL(orphanage->CountUniqueOrphans(), 0);
867 for (NodeId node = node0; node <= node2; ++node) {
868 BOOST_CHECK_EQUAL(orphanage->GetTxToReconsider(node), nullptr);
869 BOOST_CHECK(!orphanage->HaveTxFromPeer(orphan_wtxid, node));
870 }
871 }
872}
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition block.h:74
std::vector< CTransactionRef > vtx
Definition block.h:77
An encapsulated private key.
Definition key.h:46
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition key.cpp:184
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition transaction.h:30
The basic transaction that is broadcasted on the network and contained in blocks.
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
Definition time.h:54
Fast randomness source.
Definition random.h:386
Fillable signing provider that keeps keys in an address->secret map.
static transaction_identifier FromUint256(const uint256 &id)
256-bit opaque blob.
Definition uint256.h:196
static int32_t GetTransactionWeight(const CTransaction &tx)
Definition validation.h:139
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
@ SIGHASH_ALL
Definition interpreter.h:32
std::unique_ptr< TxOrphanage > MakeTxOrphanage() noexcept
Create a new TxOrphanage instance.
constexpr unsigned int DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE
Default value for TxOrphanage::m_max_global_latency_score.
Definition txorphanage.h:23
constexpr int64_t DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER
Default value for TxOrphanage::m_reserved_usage_per_peer.
Definition txorphanage.h:20
int64_t NodeId
Definition net.h:108
#define BOOST_CHECK_EQUAL(v1, v2)
Definition object.cpp:17
#define BOOST_CHECK(expr)
Definition object.cpp:16
BOOST_AUTO_TEST_CASE(peer_dos_limits)
static CTransactionRef MakeMutation(const CTransactionRef &ptx)
static void MakeNewKeyWithFastRandomContext(CKey &key, FastRandomContext &rand_ctx)
static CTransactionRef MakeTransactionSpending(const std::vector< COutPoint > &outpoints, FastRandomContext &det_rand)
static bool EqualTxns(const std::set< CTransactionRef > &set_txns, const std::vector< CTransactionRef > &vec_txns)
constexpr int32_t MAX_STANDARD_TX_WEIGHT
The maximum weight for transactions we're willing to relay/mine.
Definition policy.h:38
static CTransactionRef MakeTransactionRef(Tx &&txIn)
std::shared_ptr< const CTransaction > CTransactionRef
@ OP_1
Definition script.h:84
constexpr CAmount CENT
Basic testing setup.
A mutable version of CTransaction.
std::vector< CTxOut > vout
std::vector< CTxIn > vin
void BulkTransaction(CMutableTransaction &tx, int32_t target_weight)
bool SignSignature(const SigningProvider &provider, const CScript &fromPubKey, CMutableTransaction &txTo, unsigned int nIn, const CAmount &amount, int nHashType, SignatureData &sig_data)
Produce a satisfying script (scriptSig or witness).
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82
assert(!tx.IsCoinBase())
FakeNodeClock clock