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