Repository: bitcoin/bitcoin
File: src/test/private_broadcast_tests.cpp (C++)

Code before:
    const auto tx1{MakeDummyTx(/*id=*/1, /*num_witness=*/0)};

    BOOST_CHECK(pb.Add(tx1));
    BOOST_CHECK(!pb.Add(tx1));

    // Make another transaction with same txid, different wtxid and add it.
    const auto tx2{MakeDummyTx(/*id=*/1, /*num_witness=*/1)};
    BOOST_REQUIRE(tx1->GetHash() == tx2->GetHash());
    BOOST_REQUIRE(tx1->GetWitnessHash() != tx2->GetWitnessHash());

    BOOST_CHECK(pb.Add(tx2));

    {
        const auto infos{pb.GetBroadcastInfo()};
        BOOST_CHECK_EQUAL(infos.size(), 2);

        const auto it1{std::ranges::find_if(infos, [&](const auto& info) { return info.tx->GetWitnessHash() == tx1->GetWitnessHash(); })};
        const auto it2{std::ranges::find_if(infos, [&](const auto& info) { return info.tx->GetWitnessHash() == tx2->GetWitnessHash(); })};
        BOOST_REQUIRE(it1 != infos.end());
        BOOST_REQUIRE(it2 != infos.end());
        const auto& info1{*it1};
        const auto& info2{*it2};

        BOOST_CHECK_EQUAL(info1.peers.size(), 0);
        BOOST_CHECK_EQUAL(info2.peers.size(), 0);
    }

    const auto tx_for_recipient1{pb.PickTxForSend(/*will_send_to_nodeid=*/recipient1, /*will_send_to_address=*/addr1).value()};
    BOOST_CHECK(tx_for_recipient1 == tx1 || tx_for_recipient1 == tx2);

    // A second pick must return the other transaction.
    const NodeId recipient2{2};
    const CService addr2(ipv4Addr, 2222);
    const auto tx_for_recipient2{pb.PickTxForSend(/*will_send_to_nodeid=*/recipient2, /*will_send_to_address=*/addr2).value()};
    BOOST_CHECK(tx_for_recipient2 == tx1 || tx_for_recipient2 == tx2);
    BOOST_CHECK_NE(tx_for_recipient1, tx_for_recipient2);

    {
        const auto infos{pb.GetBroadcastInfo()};
        BOOST_CHECK_EQUAL(infos.size(), 2);

        const auto it1{std::ranges::find_if(infos, [&](const auto& info) { return info.tx->GetWitnessHash() == tx1->GetWitnessHash(); })};
        const auto it2{std::ranges::find_if(infos, [&](const auto& info) { return info.tx->GetWitnessHash() == tx2->GetWitnessHash(); })};
        BOOST_REQUIRE(it1 != infos.end());
        BOOST_REQUIRE(it2 != infos.end());
        const auto& info1{*it1};
        const auto& info2{*it2};

        BOOST_CHECK_EQUAL(info1.peers.size(), 1);
        BOOST_CHECK_EQUAL(info2.peers.size(), 1);
    }

Review comment (refactor):
there's a lot of repetition here alone and parts of this code block are repeated a few more times here - can we add a local helper lambdas to dedup these?
```patch
diff --git a/src/test/private_broadcast_tests.cpp b/src/test/private_broadcast_tests.cpp
--- a/src/test/private_broadcast_tests.cpp	(revision a2c339144982836e4f0773604b2d99c46dd143b8)
+++ b/src/test/private_broadcast_tests.cpp	(date 1770811873480)
@@ -52,21 +52,19 @@
     BOOST_REQUIRE(tx1->GetWitnessHash() != tx2->GetWitnessHash());
 
     BOOST_CHECK(pb.Add(tx2));
-
-    {
+    const auto find_tx_info{[](auto& infos, const CTransactionRef& tx) -> const PrivateBroadcast::TxBroadcastInfo& {
+        const auto it{std::ranges::find(infos, tx->GetWitnessHash(), [](const auto& info) { return info.tx->GetWitnessHash(); })};
+        BOOST_REQUIRE(it != infos.end());
+        return *it;
+    }};
+    const auto check_peer_counts{[&](size_t tx1_peer_count, size_t tx2_peer_count) {
         const auto infos{pb.GetBroadcastInfo()};
         BOOST_CHECK_EQUAL(infos.size(), 2);
+        BOOST_CHECK_EQUAL(find_tx_info(infos, tx1).peers.size(), tx1_peer_count);
+        BOOST_CHECK_EQUAL(find_tx_info(infos, tx2).peers.size(), tx2_peer_count);
+    }};
 
-        const auto it1{std::ranges::find_if(infos, [&](const auto& info) { return info.tx->GetWitnessHash() == tx1->GetWitnessHash(); })};
-        const auto it2{std::ranges::find_if(infos, [&](const auto& info) { return info.tx->GetWitnessHash() == tx2->GetWitnessHash(); })};
-        BOOST_REQUIRE(it1 != infos.end());
-        BOOST_REQUIRE(it2 != infos.end());
-        const auto& info1{*it1};
-        const auto& info2{*it2};
-
-        BOOST_CHECK_EQUAL(info1.peers.size(), 0);
-        BOOST_CHECK_EQUAL(info2.peers.size(), 0);
-    }
+    check_peer_counts(/*tx1_peer_count=*/0, /*tx2_peer_count=*/0);
 
     const auto tx_for_recipient1{pb.PickTxForSend(/*will_send_to_nodeid=*/recipient1, /*will_send_to_address=*/addr1).value()};
     BOOST_CHECK(tx_for_recipient1 == tx1 || tx_for_recipient1 == tx2);
@@ -78,20 +76,7 @@
     BOOST_CHECK(tx_for_recipient2 == tx1 || tx_for_recipient2 == tx2);
     BOOST_CHECK_NE(tx_for_recipient1, tx_for_recipient2);
 
-    {
-        const auto infos{pb.GetBroadcastInfo()};
-        BOOST_CHECK_EQUAL(infos.size(), 2);
-
-        const auto it1{std::ranges::find_if(infos, [&](const auto& info) { return info.tx->GetWitnessHash() == tx1->GetWitnessHash(); })};
-        const auto it2{std::ranges::find_if(infos, [&](const auto& info) { return info.tx->GetWitnessHash() == tx2->GetWitnessHash(); })};
-        BOOST_REQUIRE(it1 != infos.end());
-        BOOST_REQUIRE(it2 != infos.end());
-        const auto& info1{*it1};
-        const auto& info2{*it2};
-
-        BOOST_CHECK_EQUAL(info1.peers.size(), 1);
-        BOOST_CHECK_EQUAL(info2.peers.size(), 1);
-    }
+    check_peer_counts(/*tx1_peer_count=*/1, /*tx2_peer_count=*/1);
 
     const NodeId nonexistent_recipient{0};
 
@@ -114,23 +99,19 @@
     BOOST_CHECK(pb.DidNodeConfirmReception(recipient1));
     BOOST_CHECK(!pb.DidNodeConfirmReception(recipient2));
 
-    {
-        const auto infos{pb.GetBroadcastInfo()};
-        BOOST_CHECK_EQUAL(infos.size(), 2);
-
-        const auto it_confirmed{std::ranges::find_if(infos, [&](const auto& info) { return info.tx->GetWitnessHash() == tx_for_recipient1->GetWitnessHash(); })};
-        const auto it_unconfirmed{std::ranges::find_if(infos, [&](const auto& info) { return info.tx->GetWitnessHash() == tx_for_recipient2->GetWitnessHash(); })};
-        BOOST_REQUIRE(it_confirmed != infos.end());
-        BOOST_REQUIRE(it_unconfirmed != infos.end());
-        const auto& confirmed_tx{*it_confirmed};
-        const auto& unconfirmed_tx{*it_unconfirmed};
-
-        BOOST_CHECK_EQUAL(confirmed_tx.peers.size(), 1);
-        BOOST_CHECK_EQUAL(confirmed_tx.peers[0].address.ToStringAddrPort(), addr1.ToStringAddrPort());
-        BOOST_CHECK_EQUAL(unconfirmed_tx.peers.size(), 1);
-        BOOST_CHECK_EQUAL(unconfirmed_tx.peers[0].address.ToStringAddrPort(), addr2.ToStringAddrPort());
-        BOOST_CHECK(confirmed_tx.peers[0].received.has_value());
-        BOOST_CHECK(!unconfirmed_tx.peers[0].received.has_value());
+    const auto infos{pb.GetBroadcastInfo()};
+    BOOST_CHECK_EQUAL(infos.size(), 2);
+    {
+        const auto& [tx, peers]{find_tx_info(infos, tx_for_recipient1)};
+        BOOST_CHECK_EQUAL(peers.size(), 1);
+        BOOST_CHECK_EQUAL(peers[0].address.ToStringAddrPort(), addr1.ToStringAddrPort());
+        BOOST_CHECK(peers[0].received.has_value());
+    }
+    {
+        const auto& [tx, peers]{find_tx_info(infos, tx_for_recipient2)};
+        BOOST_CHECK_EQUAL(peers.size(), 1);
+        BOOST_CHECK_EQUAL(peers[0].address.ToStringAddrPort(), addr2.ToStringAddrPort());
+        BOOST_CHECK(!peers[0].received.has_value());
     }
 
     BOOST_CHECK_EQUAL(pb.GetStale().size(), 1);
```

Code after:
    const auto tx1{MakeDummyTx(/*id=*/1, /*num_witness=*/0)};

    BOOST_CHECK(pb.Add(tx1));
    BOOST_CHECK(!pb.Add(tx1));

    // Make another transaction with same txid, different wtxid and add it.
    const auto tx2{MakeDummyTx(/*id=*/1, /*num_witness=*/1)};
    BOOST_REQUIRE(tx1->GetHash() == tx2->GetHash());
    BOOST_REQUIRE(tx1->GetWitnessHash() != tx2->GetWitnessHash());

    BOOST_CHECK(pb.Add(tx2));
    const auto find_tx_info{[](auto& infos, const CTransactionRef& tx) -> const PrivateBroadcast::TxBroadcastInfo& {
        const auto it{std::ranges::find(infos, tx->GetWitnessHash(), [](const auto& info) { return info.tx->GetWitnessHash(); })};
        BOOST_REQUIRE(it != infos.end());
        return *it;
    }};
    const auto check_peer_counts{[&](size_t tx1_peer_count, size_t tx2_peer_count) {
        const auto infos{pb.GetBroadcastInfo()};
        BOOST_CHECK_EQUAL(infos.size(), 2);
        BOOST_CHECK_EQUAL(find_tx_info(infos, tx1).peers.size(), tx1_peer_count);
        BOOST_CHECK_EQUAL(find_tx_info(infos, tx2).peers.size(), tx2_peer_count);
    }};

    check_peer_counts(/*tx1_peer_count=*/0, /*tx2_peer_count=*/0);

    const auto tx_for_recipient1{pb.PickTxForSend(/*will_send_to_nodeid=*/recipient1, /*will_send_to_address=*/addr1).value()};
    BOOST_CHECK(tx_for_recipient1 == tx1 || tx_for_recipient1 == tx2);

    // A second pick must return the other transaction.
    const NodeId recipient2{2};
    const CService addr2{ipv4Addr, 2222};
    const auto tx_for_recipient2{pb.PickTxForSend(/*will_send_to_nodeid=*/recipient2, /*will_send_to_address=*/addr2).value()};
    BOOST_CHECK(tx_for_recipient2 == tx1 || tx_for_recipient2 == tx2);
    BOOST_CHECK_NE(tx_for_recipient1, tx_for_recipient2);

    check_peer_counts(/*tx1_peer_count=*/1, /*tx2_peer_count=*/1);

    const NodeId nonexistent_recipient{0};

    // Confirm transactions <-> recipients mapping is correct.
    BOOST_CHECK(!pb.GetTxForNode(nonexistent_recipient).has_value());
    BOOST_CHECK_EQUAL(pb.GetTxForNode(recipient1).value(), tx_for_recipient1);
    BOOST_CHECK_EQUAL(pb.GetTxForNode(recipient2).value(), tx_for_recipient2);

    // Confirm none of the transactions' reception have been confirmed.
    BOOST_CHECK(!pb.DidNodeConfirmReception(recipient1));
    BOOST_CHECK(!pb.DidNodeConfirmReception(recipient2));
    BOOST_CHECK(!pb.DidNodeConfirmReception(nonexistent_recipient));

    BOOST_CHECK_EQUAL(pb.GetStale().size(), 2);