orphanage_tests.cpp raw

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