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?
0diff --git a/src/test/private_broadcast_tests.cpp b/src/test/private_broadcast_tests.cpp
1--- a/src/test/private_broadcast_tests.cpp (revision a2c339144982836e4f0773604b2d99c46dd143b8)
2+++ b/src/test/private_broadcast_tests.cpp (date 1770811873480)
3@@ -52,21 +52,19 @@
4 BOOST_REQUIRE(tx1->GetWitnessHash() != tx2->GetWitnessHash());
5
6 BOOST_CHECK(pb.Add(tx2));
7-
8- {
9+ const auto find_tx_info{[](auto& infos, const CTransactionRef& tx) -> const PrivateBroadcast::TxBroadcastInfo& {
10+ const auto it{std::ranges::find(infos, tx->GetWitnessHash(), [](const auto& info) { return info.tx->GetWitnessHash(); })};
11+ BOOST_REQUIRE(it != infos.end());
12+ return *it;
13+ }};
14+ const auto check_peer_counts{[&](size_t tx1_peer_count, size_t tx2_peer_count) {
15 const auto infos{pb.GetBroadcastInfo()};
16 BOOST_CHECK_EQUAL(infos.size(), 2);
17+ BOOST_CHECK_EQUAL(find_tx_info(infos, tx1).peers.size(), tx1_peer_count);
18+ BOOST_CHECK_EQUAL(find_tx_info(infos, tx2).peers.size(), tx2_peer_count);
19+ }};
20
21- const auto it1{std::ranges::find_if(infos, [&](const auto& info) { return info.tx->GetWitnessHash() == tx1->GetWitnessHash(); })};
22- const auto it2{std::ranges::find_if(infos, [&](const auto& info) { return info.tx->GetWitnessHash() == tx2->GetWitnessHash(); })};
23- BOOST_REQUIRE(it1 != infos.end());
24- BOOST_REQUIRE(it2 != infos.end());
25- const auto& info1{*it1};
26- const auto& info2{*it2};
27-
28- BOOST_CHECK_EQUAL(info1.peers.size(), 0);
29- BOOST_CHECK_EQUAL(info2.peers.size(), 0);
30- }
31+ check_peer_counts(/*tx1_peer_count=*/0, /*tx2_peer_count=*/0);
32
33 const auto tx_for_recipient1{pb.PickTxForSend(/*will_send_to_nodeid=*/recipient1, /*will_send_to_address=*/addr1).value()};
34 BOOST_CHECK(tx_for_recipient1 == tx1 || tx_for_recipient1 == tx2);
35@@ -78,20 +76,7 @@
36 BOOST_CHECK(tx_for_recipient2 == tx1 || tx_for_recipient2 == tx2);
37 BOOST_CHECK_NE(tx_for_recipient1, tx_for_recipient2);
38
39- {
40- const auto infos{pb.GetBroadcastInfo()};
41- BOOST_CHECK_EQUAL(infos.size(), 2);
42-
43- const auto it1{std::ranges::find_if(infos, [&](const auto& info) { return info.tx->GetWitnessHash() == tx1->GetWitnessHash(); })};
44- const auto it2{std::ranges::find_if(infos, [&](const auto& info) { return info.tx->GetWitnessHash() == tx2->GetWitnessHash(); })};
45- BOOST_REQUIRE(it1 != infos.end());
46- BOOST_REQUIRE(it2 != infos.end());
47- const auto& info1{*it1};
48- const auto& info2{*it2};
49-
50- BOOST_CHECK_EQUAL(info1.peers.size(), 1);
51- BOOST_CHECK_EQUAL(info2.peers.size(), 1);
52- }
53+ check_peer_counts(/*tx1_peer_count=*/1, /*tx2_peer_count=*/1);
54
55 const NodeId nonexistent_recipient{0};
56
57@@ -114,23 +99,19 @@
58 BOOST_CHECK(pb.DidNodeConfirmReception(recipient1));
59 BOOST_CHECK(!pb.DidNodeConfirmReception(recipient2));
60
61- {
62- const auto infos{pb.GetBroadcastInfo()};
63- BOOST_CHECK_EQUAL(infos.size(), 2);
64-
65- const auto it_confirmed{std::ranges::find_if(infos, [&](const auto& info) { return info.tx->GetWitnessHash() == tx_for_recipient1->GetWitnessHash(); })};
66- const auto it_unconfirmed{std::ranges::find_if(infos, [&](const auto& info) { return info.tx->GetWitnessHash() == tx_for_recipient2->GetWitnessHash(); })};
67- BOOST_REQUIRE(it_confirmed != infos.end());
68- BOOST_REQUIRE(it_unconfirmed != infos.end());
69- const auto& confirmed_tx{*it_confirmed};
70- const auto& unconfirmed_tx{*it_unconfirmed};
71-
72- BOOST_CHECK_EQUAL(confirmed_tx.peers.size(), 1);
73- BOOST_CHECK_EQUAL(confirmed_tx.peers[0].address.ToStringAddrPort(), addr1.ToStringAddrPort());
74- BOOST_CHECK_EQUAL(unconfirmed_tx.peers.size(), 1);
75- BOOST_CHECK_EQUAL(unconfirmed_tx.peers[0].address.ToStringAddrPort(), addr2.ToStringAddrPort());
76- BOOST_CHECK(confirmed_tx.peers[0].received.has_value());
77- BOOST_CHECK(!unconfirmed_tx.peers[0].received.has_value());
78+ const auto infos{pb.GetBroadcastInfo()};
79+ BOOST_CHECK_EQUAL(infos.size(), 2);
80+ {
81+ const auto& [tx, peers]{find_tx_info(infos, tx_for_recipient1)};
82+ BOOST_CHECK_EQUAL(peers.size(), 1);
83+ BOOST_CHECK_EQUAL(peers[0].address.ToStringAddrPort(), addr1.ToStringAddrPort());
84+ BOOST_CHECK(peers[0].received.has_value());
85+ }
86+ {
87+ const auto& [tx, peers]{find_tx_info(infos, tx_for_recipient2)};
88+ BOOST_CHECK_EQUAL(peers.size(), 1);
89+ BOOST_CHECK_EQUAL(peers[0].address.ToStringAddrPort(), addr2.ToStringAddrPort());
90+ BOOST_CHECK(!peers[0].received.has_value());
91 }
92
93 BOOST_CHECK_EQUAL(pb.GetStale().size(), 1);