/ src / test / orphanage_tests.cpp
orphanage_tests.cpp
  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>
  6  #include <consensus/validation.h>
  7  #include <node/txorphanage.h>
  8  #include <policy/policy.h>
  9  #include <primitives/transaction.h>
 10  #include <pubkey.h>
 11  #include <script/sign.h>
 12  #include <script/signingprovider.h>
 13  #include <test/util/common.h>
 14  #include <test/util/random.h>
 15  #include <test/util/setup_common.h>
 16  #include <test/util/transaction_utils.h>
 17  
 18  #include <array>
 19  #include <cstdint>
 20  
 21  #include <boost/test/unit_test.hpp>
 22  
 23  BOOST_FIXTURE_TEST_SUITE(orphanage_tests, TestingSetup)
 24  
 25  static void MakeNewKeyWithFastRandomContext(CKey& key, FastRandomContext& rand_ctx)
 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.
 34  static CTransactionRef MakeTransactionSpending(const std::vector<COutPoint>& outpoints, FastRandomContext& det_rand)
 35  {
 36      CKey key;
 37      MakeNewKeyWithFastRandomContext(key, det_rand);
 38      CMutableTransaction tx;
 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.
 58  static CTransactionRef MakeMutation(const CTransactionRef& ptx)
 59  {
 60      CMutableTransaction tx(*ptx);
 61      tx.vin[0].scriptWitness.stack.push_back({5});
 62      auto mutated_tx = MakeTransactionRef(tx);
 63      assert(ptx->GetHash() == mutated_tx->GetHash());
 64      return mutated_tx;
 65  }
 66  
 67  static 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  
 76  BOOST_AUTO_TEST_CASE(peer_dos_limits)
 77  {
 78      FastRandomContext det_rand{true};
 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) {
 89          auto ptx = MakeTransactionSpending({}, det_rand);
 90          txns.emplace_back(ptx);
 91          BOOST_CHECK_EQUAL(TX_SIZE, GetTransactionWeight(*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          }
133          auto ptx = MakeTransactionSpending(outpoints_45, det_rand);
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) {
149              CTransactionRef parent = MakeTransactionSpending({}, det_rand);
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.
223          auto orphanage = node::MakeTxOrphanage(max_announcements, TOTAL_SIZE * 10);
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      {
284          auto orphanage{node::MakeTxOrphanage()};
285          // These stay the same regardless of number of peers
286          BOOST_CHECK_EQUAL(orphanage->MaxGlobalLatencyScore(), node::DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE);
287          BOOST_CHECK_EQUAL(orphanage->ReservedPeerUsage(), node::DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER);
288  
289          // These change with number of peers
290          BOOST_CHECK_EQUAL(orphanage->MaxGlobalUsage(), node::DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER);
291          BOOST_CHECK_EQUAL(orphanage->MaxPeerLatencyScore(), node::DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE);
292  
293          // Number of peers = 1
294          orphanage->AddTx(txns.at(0), 0);
295          BOOST_CHECK_EQUAL(orphanage->MaxGlobalLatencyScore(), node::DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE);
296          BOOST_CHECK_EQUAL(orphanage->ReservedPeerUsage(), node::DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER);
297          BOOST_CHECK_EQUAL(orphanage->MaxGlobalUsage(), node::DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER);
298          BOOST_CHECK_EQUAL(orphanage->MaxPeerLatencyScore(), node::DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE);
299  
300          // Number of peers = 2
301          orphanage->AddTx(txns.at(1), 1);
302          BOOST_CHECK_EQUAL(orphanage->MaxGlobalLatencyScore(), node::DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE);
303          BOOST_CHECK_EQUAL(orphanage->ReservedPeerUsage(), node::DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER);
304          BOOST_CHECK_EQUAL(orphanage->MaxGlobalUsage(), node::DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER * 2);
305          BOOST_CHECK_EQUAL(orphanage->MaxPeerLatencyScore(), node::DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE / 2);
306  
307          // Number of peers = 3
308          orphanage->AddTx(txns.at(2), 2);
309          BOOST_CHECK_EQUAL(orphanage->MaxGlobalLatencyScore(), node::DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE);
310          BOOST_CHECK_EQUAL(orphanage->ReservedPeerUsage(), node::DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER);
311          BOOST_CHECK_EQUAL(orphanage->MaxGlobalUsage(), node::DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER * 3);
312          BOOST_CHECK_EQUAL(orphanage->MaxPeerLatencyScore(), node::DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE / 3);
313  
314          // Number of peers didn't change.
315          orphanage->AddTx(txns.at(3), 2);
316          BOOST_CHECK_EQUAL(orphanage->MaxGlobalLatencyScore(), node::DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE);
317          BOOST_CHECK_EQUAL(orphanage->ReservedPeerUsage(), node::DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER);
318          BOOST_CHECK_EQUAL(orphanage->MaxGlobalUsage(), node::DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER * 3);
319          BOOST_CHECK_EQUAL(orphanage->MaxPeerLatencyScore(), node::DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE / 3);
320  
321          // Once a peer has no orphans, it is not considered in the limits.
322          // Number of peers = 2
323          orphanage->EraseForPeer(2);
324          BOOST_CHECK_EQUAL(orphanage->MaxGlobalLatencyScore(), node::DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE);
325          BOOST_CHECK_EQUAL(orphanage->ReservedPeerUsage(), node::DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER);
326          BOOST_CHECK_EQUAL(orphanage->MaxGlobalUsage(), node::DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER * 2);
327          BOOST_CHECK_EQUAL(orphanage->MaxPeerLatencyScore(), node::DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE / 2);
328  
329          // Number of peers = 1
330          orphanage->EraseTx(txns.at(0)->GetWitnessHash());
331          BOOST_CHECK_EQUAL(orphanage->MaxGlobalLatencyScore(), node::DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE);
332          BOOST_CHECK_EQUAL(orphanage->ReservedPeerUsage(), node::DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER);
333          BOOST_CHECK_EQUAL(orphanage->MaxGlobalUsage(), node::DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER);
334          BOOST_CHECK_EQUAL(orphanage->MaxPeerLatencyScore(), node::DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE);
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.
342          CMutableTransaction tx_large;
343          tx_large.vin.resize(1);
344          BulkTransaction(tx_large, 10 * TX_SIZE);
345          auto ptx_large = MakeTransactionRef(tx_large);
346  
347          const auto large_tx_size = GetTransactionWeight(*ptx_large);
348          BOOST_CHECK(large_tx_size > 10 * TX_SIZE);
349          BOOST_CHECK(large_tx_size < 11 * TX_SIZE);
350  
351          auto orphanage = node::MakeTxOrphanage(20, large_tx_size);
352          // One peer sends 10 normal size transactions. The other peer sends 10 normal transactions and 1 very large one
353          NodeId peer_normal{0};
354          NodeId peer_large{1};
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.
362          orphanage->AddTx(ptx_large, peer_large);
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      {
375          auto orphanage = node::MakeTxOrphanage();
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) {
383              auto ptx = MakeTransactionSpending(outpoints_9, m_rng);
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) {
396              CMutableTransaction tx;
397              std::shuffle(outpoints_50.begin(), outpoints_50.end(), m_rng);
398              auto ptx = MakeTransactionSpending(outpoints_50, 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  }
418  BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
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;
430      MakeNewKeyWithFastRandomContext(key, m_rng);
431      FillableSigningProvider keystore;
432      BOOST_CHECK(keystore.AddKey(key));
433  
434      // Freeze time for length of test
435      auto now{GetTime<std::chrono::seconds>()};
436      SetMockTime(now);
437  
438      std::vector<CTransactionRef> orphans_added;
439  
440      // 50 orphan transactions:
441      for (int i = 0; i < 50; i++)
442      {
443          CMutableTransaction tx;
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  
462          CMutableTransaction tx;
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;
470          BOOST_CHECK(SignSignature(keystore, *txPrev, tx, 0, SIGHASH_ALL, 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  
482          CMutableTransaction tx;
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;
493          BOOST_CHECK(SignSignature(keystore, *txPrev, tx, 0, SIGHASH_ALL, 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  
499          BOOST_CHECK(!orphanage->AddTx(MakeTransactionRef(tx), i));
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);
513          expected_num_orphans -= 2;
514          BOOST_CHECK_EQUAL(orphanage->CountUniqueOrphans(), expected_num_orphans);
515      }
516  }
517  
518  BOOST_AUTO_TEST_CASE(same_txid_diff_witness)
519  {
520      FastRandomContext det_rand{true};
521      std::unique_ptr<node::TxOrphanage> orphanage{node::MakeTxOrphanage()};
522      NodeId peer{0};
523  
524      std::vector<COutPoint> empty_outpoints;
525      auto parent = MakeTransactionSpending(empty_outpoints, det_rand);
526  
527      // Create children to go into orphanage.
528      auto child_normal = MakeTransactionSpending({{parent->GetHash(), 0}}, det_rand);
529      auto child_mutated = MakeMutation(child_normal);
530  
531      const auto& normal_wtxid = child_normal->GetWitnessHash();
532      const auto& mutated_wtxid = child_mutated->GetWitnessHash();
533      BOOST_CHECK(normal_wtxid != mutated_wtxid);
534  
535      BOOST_CHECK(orphanage->AddTx(child_normal, peer));
536      // EraseTx fails as transaction by this wtxid doesn't exist.
537      BOOST_CHECK_EQUAL(orphanage->EraseTx(mutated_wtxid), 0);
538      BOOST_CHECK(orphanage->HaveTx(normal_wtxid));
539      BOOST_CHECK(orphanage->GetTx(normal_wtxid) == child_normal);
540      BOOST_CHECK(!orphanage->HaveTx(mutated_wtxid));
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));
545      BOOST_CHECK(orphanage->HaveTx(normal_wtxid));
546      BOOST_CHECK(orphanage->HaveTx(mutated_wtxid));
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
553      BOOST_CHECK_EQUAL(orphanage->EraseTx(mutated_wtxid), 1);
554      BOOST_CHECK(orphanage->HaveTx(normal_wtxid));
555      BOOST_CHECK(!orphanage->HaveTx(mutated_wtxid));
556  
557      BOOST_CHECK_EQUAL(orphanage->EraseTx(normal_wtxid), 1);
558      BOOST_CHECK(!orphanage->HaveTx(normal_wtxid));
559      BOOST_CHECK(!orphanage->HaveTx(mutated_wtxid));
560  }
561  
562  
563  BOOST_AUTO_TEST_CASE(get_children)
564  {
565      FastRandomContext det_rand{true};
566      std::vector<COutPoint> empty_outpoints;
567  
568      auto parent1 = MakeTransactionSpending(empty_outpoints, det_rand);
569      auto parent2 = MakeTransactionSpending(empty_outpoints, det_rand);
570  
571      // Make sure these parents have different txids otherwise this test won't make sense.
572      while (parent1->GetHash() == parent2->GetHash()) {
573          parent2 = MakeTransactionSpending(empty_outpoints, det_rand);
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      {
591          auto orphanage{node::MakeTxOrphanage()};
592          BOOST_CHECK(orphanage->AddTx(child_p1n0, node1));
593          BOOST_CHECK(orphanage->AddTx(child_p2n1, node1));
594          BOOST_CHECK(orphanage->AddTx(child_p1n0_p1n1, node1));
595          BOOST_CHECK(orphanage->AddTx(child_p1n0_p2n0, node1));
596  
597          // Also add some other announcers for the same transactions
598          BOOST_CHECK(!orphanage->AddTx(child_p1n0_p1n1, node0));
599          BOOST_CHECK(!orphanage->AddTx(child_p2n1, node0));
600          BOOST_CHECK(!orphanage->AddTx(child_p1n0, node3));
601  
602  
603          std::vector<CTransactionRef> expected_parent1_children{child_p1n0_p2n0, child_p1n0_p1n1, child_p1n0};
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()};
621          BOOST_CHECK(orphanage->AddTx(child_p1n0, node1));
622          BOOST_CHECK(orphanage->AddTx(child_p2n1, node1));
623          BOOST_CHECK(orphanage->AddTx(child_p1n0_p1n1, node2));
624          BOOST_CHECK(orphanage->AddTx(child_p1n0_p2n0, node2));
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));
639              BOOST_CHECK(EqualTxns(expected_parent1_node1, orphanage->GetChildrenFromSamePeer(parent1, node1)));
640          }
641  
642          // Children of parent2 from node1:
643          {
644              std::set<CTransactionRef> expected_parent2_node1{child_p2n1};
645  
646              BOOST_CHECK(EqualTxns(expected_parent2_node1, orphanage->GetChildrenFromSamePeer(parent2, node1)));
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));
663              BOOST_CHECK(EqualTxns(expected_parent2_node2, orphanage->GetChildrenFromSamePeer(parent2, node2)));
664          }
665      }
666  }
667  
668  BOOST_AUTO_TEST_CASE(too_large_orphan_tx)
669  {
670      std::unique_ptr<node::TxOrphanage> orphanage{node::MakeTxOrphanage()};
671      CMutableTransaction tx;
672      tx.vin.resize(1);
673  
674      // check that txs larger than MAX_STANDARD_TX_WEIGHT are not added to the orphanage
675      BulkTransaction(tx, MAX_STANDARD_TX_WEIGHT + 4);
676      BOOST_CHECK_EQUAL(GetTransactionWeight(CTransaction(tx)), MAX_STANDARD_TX_WEIGHT + 4);
677      BOOST_CHECK(!orphanage->AddTx(MakeTransactionRef(tx), 0));
678  
679      tx.vout.clear();
680      BulkTransaction(tx, MAX_STANDARD_TX_WEIGHT);
681      BOOST_CHECK_EQUAL(GetTransactionWeight(CTransaction(tx)), MAX_STANDARD_TX_WEIGHT);
682      BOOST_CHECK(orphanage->AddTx(MakeTransactionRef(tx), 0));
683  }
684  
685  BOOST_AUTO_TEST_CASE(process_block)
686  {
687      FastRandomContext det_rand{true};
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  
702      auto control_tx = MakeTransactionSpending({}, det_rand);
703      BOOST_CHECK(orphanage->AddTx(control_tx, node));
704  
705      auto bo_tx_same_txid = MakeTransactionSpending({outpoints.at(0)}, det_rand);
706      BOOST_CHECK(orphanage->AddTx(bo_tx_same_txid, node));
707      block.vtx.emplace_back(bo_tx_same_txid);
708  
709      // 2 transactions with the same txid but different witness
710      auto b_tx_same_txid_diff_witness = MakeTransactionSpending({outpoints.at(1)}, det_rand);
711      block.vtx.emplace_back(b_tx_same_txid_diff_witness);
712  
713      auto o_tx_same_txid_diff_witness = MakeMutation(b_tx_same_txid_diff_witness);
714      BOOST_CHECK(orphanage->AddTx(o_tx_same_txid_diff_witness, node));
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);
721      BOOST_CHECK(orphanage->AddTx(o_tx_conflict, node));
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);
728      BOOST_CHECK(orphanage->AddTx(o_tx_conflict_partial_2, node));
729  
730      orphanage->EraseForBlock(block);
731      for (const auto& expected_removed : {bo_tx_same_txid, o_tx_same_txid_diff_witness, o_tx_conflict, o_tx_conflict_partial_2}) {
732          const auto& expected_removed_wtxid = expected_removed->GetWitnessHash();
733          BOOST_CHECK(!orphanage->HaveTx(expected_removed_wtxid));
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  
740  BOOST_AUTO_TEST_CASE(multiple_announcers)
741  {
742      const NodeId node0{0};
743      const NodeId node1{1};
744      const NodeId node2{2};
745      size_t expected_total_count{0};
746      FastRandomContext det_rand{true};
747      std::unique_ptr<node::TxOrphanage> orphanage{node::MakeTxOrphanage()};
748  
749      // Check accounting per peer.
750      // Check that EraseForPeer works with multiple announcers.
751      {
752          auto ptx = MakeTransactionSpending({}, det_rand);
753          const auto& wtxid = ptx->GetWitnessHash();
754          BOOST_CHECK(orphanage->AddTx(ptx, node0));
755          BOOST_CHECK(orphanage->HaveTx(wtxid));
756          expected_total_count += 1;
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.
764          auto ptx_mutated{MakeMutation(ptx)};
765          BOOST_CHECK(orphanage->AddTx(ptx_mutated, node0));
766          BOOST_CHECK(orphanage->HaveTx(ptx_mutated->GetWitnessHash()));
767          expected_total_count += 1;
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()));
790          expected_total_count -= 1;
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);
798          expected_total_count -= 1;
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;
806          auto tx_block = MakeTransactionSpending({}, det_rand);
807          block.vtx.emplace_back(tx_block);
808          BOOST_CHECK(orphanage->AddTx(tx_block, node0));
809          BOOST_CHECK(!orphanage->AddTx(tx_block, node1));
810  
811          expected_total_count += 1;
812  
813          BOOST_CHECK_EQUAL(orphanage->CountUniqueOrphans(), expected_total_count);
814  
815          orphanage->EraseForBlock(block);
816  
817          expected_total_count -= 1;
818  
819          BOOST_CHECK_EQUAL(orphanage->CountUniqueOrphans(), expected_total_count);
820      }
821  }
822  BOOST_AUTO_TEST_CASE(peer_worksets)
823  {
824      const NodeId node0{0};
825      const NodeId node1{1};
826      const NodeId node2{2};
827      FastRandomContext det_rand{true};
828      std::unique_ptr<node::TxOrphanage> orphanage{node::MakeTxOrphanage()};
829      // AddChildrenToWorkSet should pick an announcer randomly
830      {
831          auto tx_missing_parent = MakeTransactionSpending({}, det_rand);
832          auto tx_orphan = MakeTransactionSpending({COutPoint{tx_missing_parent->GetHash(), 0}}, det_rand);
833          const auto& orphan_wtxid = tx_orphan->GetWitnessHash();
834  
835          // All 3 peers are announcers.
836          BOOST_CHECK(orphanage->AddTx(tx_orphan, node0));
837          BOOST_CHECK(!orphanage->AddTx(tx_orphan, node1));
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);
845          BOOST_CHECK_EQUAL(newly_reconsiderable.size(), 1);
846          int node0_reconsider = orphanage->HaveTxToReconsider(node0);
847          int node1_reconsider = orphanage->HaveTxToReconsider(node1);
848          int node2_reconsider = orphanage->HaveTxToReconsider(node2);
849          BOOST_CHECK_EQUAL(node0_reconsider + node1_reconsider + node2_reconsider, 1);
850  
851          NodeId assigned_peer;
852          if (node0_reconsider) {
853              assigned_peer = node0;
854          } else if (node1_reconsider) {
855              assigned_peer = node1;
856          } else {
857              BOOST_CHECK(node2_reconsider);
858              assigned_peer = node2;
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.
866          BOOST_CHECK_EQUAL(orphanage->EraseTx(orphan_wtxid), 1);
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  }
874  BOOST_AUTO_TEST_SUITE_END()