Branch data Line data Source code
1 : : // Copyright (c) 2025-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 <primitives/transaction.h>
6 : : #include <private_broadcast.h>
7 : : #include <test/util/setup_common.h>
8 : : #include <test/util/time.h>
9 : : #include <util/check.h>
10 : : #include <util/time.h>
11 : :
12 : : #include <boost/test/unit_test.hpp>
13 : :
14 : : #include <algorithm>
15 : : #include <ostream>
16 : :
17 : 0 : std::ostream& operator<<(std::ostream& os, PrivateBroadcast::AddResult r)
18 : : {
19 [ # # # # ]: 0 : switch (r) {
20 : 0 : case PrivateBroadcast::AddResult::Added: return os << "Added";
21 : 0 : case PrivateBroadcast::AddResult::AlreadyPresent: return os << "AlreadyPresent";
22 : 0 : case PrivateBroadcast::AddResult::QueueFull: return os << "QueueFull";
23 : : } // no default case, so the compiler can warn about missing cases
24 : 0 : AssertUnreachable();
25 : : }
26 : :
27 : : BOOST_FIXTURE_TEST_SUITE(private_broadcast_tests, BasicTestingSetup)
28 : :
29 : 10014 : static CTransactionRef MakeDummyTx(uint32_t id, size_t num_witness)
30 : : {
31 : 10014 : CMutableTransaction mtx;
32 [ + - ]: 10014 : mtx.vin.resize(1);
33 [ + + ]: 10014 : mtx.vin[0].nSequence = id;
34 [ + + ]: 10014 : if (num_witness > 0) {
35 [ + - + - : 1 : mtx.vin[0].scriptWitness = CScriptWitness{std::vector<std::vector<unsigned char>>(num_witness)};
- + - + ]
36 : : }
37 [ + - ]: 20028 : return MakeTransactionRef(mtx);
38 : 10014 : }
39 : :
40 [ + - + - : 10 : BOOST_AUTO_TEST_CASE(basic)
+ - + - -
+ + - + -
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - ]
41 : : {
42 : 1 : FakeNodeClock clock{};
43 : :
44 [ + - ]: 1 : PrivateBroadcast pb;
45 : 1 : const NodeId recipient1{1};
46 : 1 : in_addr ipv4Addr;
47 : 1 : ipv4Addr.s_addr = 0xa0b0c001;
48 [ + - ]: 1 : const CService addr1{ipv4Addr, 1111};
49 : :
50 : : // No transactions initially.
51 [ + - + - : 2 : BOOST_CHECK(!pb.PickTxForSend(/*will_send_to_nodeid=*/recipient1, /*will_send_to_address=*/addr1).has_value());
+ - + - ]
52 [ + - + - : 2 : BOOST_CHECK_EQUAL(pb.GetStale().size(), 0);
+ - ]
53 [ + - + - : 2 : BOOST_CHECK(!pb.HavePendingTransactions());
+ - + - ]
54 [ + - + - : 2 : BOOST_CHECK_EQUAL(pb.GetBroadcastInfo().size(), 0);
+ - ]
55 : :
56 : : // Make a transaction and add it.
57 [ + - ]: 1 : const auto tx1{MakeDummyTx(/*id=*/1, /*num_witness=*/0)};
58 : :
59 [ + - + - : 1 : BOOST_CHECK_EQUAL(pb.Add(tx1), PrivateBroadcast::AddResult::Added);
+ - ]
60 [ + - + - : 1 : BOOST_CHECK_EQUAL(pb.Add(tx1), PrivateBroadcast::AddResult::AlreadyPresent);
+ - ]
61 : :
62 : : // Make another transaction with same txid, different wtxid and add it.
63 [ + - ]: 1 : const auto tx2{MakeDummyTx(/*id=*/1, /*num_witness=*/1)};
64 [ + - - + : 2 : BOOST_REQUIRE(tx1->GetHash() == tx2->GetHash());
+ - + - ]
65 [ + - + - : 3 : BOOST_REQUIRE(tx1->GetWitnessHash() != tx2->GetWitnessHash());
+ - + - ]
66 : :
67 [ + - + - : 1 : BOOST_CHECK_EQUAL(pb.Add(tx2), PrivateBroadcast::AddResult::Added);
+ - ]
68 : 7 : const auto find_tx_info{[](auto& infos, const CTransactionRef& tx) -> const PrivateBroadcast::TxBroadcastInfo& {
69 [ + + ]: 15 : const auto it{std::ranges::find(infos, tx->GetWitnessHash(), [](const auto& info) { return info.tx->GetWitnessHash(); })};
70 [ + - ]: 12 : BOOST_REQUIRE(it != infos.end());
71 : 6 : return *it;
72 : : }};
73 : 3 : const auto check_peer_counts{[&](size_t tx1_peer_count, size_t tx2_peer_count) {
74 : 2 : const auto infos{pb.GetBroadcastInfo()};
75 [ + - - + : 2 : BOOST_CHECK_EQUAL(infos.size(), 2);
+ - ]
76 [ + - + - : 2 : BOOST_CHECK_EQUAL(find_tx_info(infos, tx1).peers.size(), tx1_peer_count);
- + + - ]
77 [ + - + - : 2 : BOOST_CHECK_EQUAL(find_tx_info(infos, tx2).peers.size(), tx2_peer_count);
- + + - ]
78 : 3 : }};
79 : :
80 [ + - ]: 1 : check_peer_counts(/*tx1_peer_count=*/0, /*tx2_peer_count=*/0);
81 : :
82 [ + - ]: 2 : const auto tx_for_recipient1{pb.PickTxForSend(/*will_send_to_nodeid=*/recipient1, /*will_send_to_address=*/addr1).value()};
83 [ + - + - : 3 : BOOST_CHECK(tx_for_recipient1 == tx1 || tx_for_recipient1 == tx2);
+ - + - +
- + - ]
84 : :
85 : : // A second pick must return the other transaction.
86 : 1 : const NodeId recipient2{2};
87 [ + - ]: 1 : const CService addr2{ipv4Addr, 2222};
88 [ + - ]: 2 : const auto tx_for_recipient2{pb.PickTxForSend(/*will_send_to_nodeid=*/recipient2, /*will_send_to_address=*/addr2).value()};
89 [ + - - + : 2 : BOOST_CHECK(tx_for_recipient2 == tx1 || tx_for_recipient2 == tx2);
- - + - +
- ]
90 [ + - + - ]: 1 : BOOST_CHECK_NE(tx_for_recipient1, tx_for_recipient2);
91 : :
92 [ + - ]: 1 : check_peer_counts(/*tx1_peer_count=*/1, /*tx2_peer_count=*/1);
93 : :
94 : 1 : const NodeId nonexistent_recipient{0};
95 : :
96 : : // Confirm transactions <-> recipients mapping is correct.
97 [ + - + - : 2 : BOOST_CHECK(!pb.GetTxForNode(nonexistent_recipient).has_value());
+ - + - ]
98 [ + - + - : 2 : BOOST_CHECK_EQUAL(pb.GetTxForNode(recipient1).value(), tx_for_recipient1);
+ - ]
99 [ + - + - : 2 : BOOST_CHECK_EQUAL(pb.GetTxForNode(recipient2).value(), tx_for_recipient2);
+ - ]
100 : :
101 : : // Confirm none of the transactions' reception have been confirmed.
102 [ + - + + ]: 3 : for (const auto& info : pb.GetBroadcastInfo()) {
103 [ + - + - ]: 6 : BOOST_CHECK(std::ranges::none_of(info.peers, [](const auto& peer) { return peer.received.has_value(); }));
104 : 1 : }
105 : :
106 : : // 1. Freshly added transactions should NOT be stale yet.
107 [ + - + - : 2 : BOOST_CHECK_EQUAL(pb.GetStale().size(), 0);
+ - ]
108 : :
109 : : // 2. Fast-forward the mock clock past the INITIAL_STALE_DURATION.
110 [ + - ]: 1 : clock += PrivateBroadcast::INITIAL_STALE_DURATION + 1min;
111 : :
112 : : // 3. Now that the initial duration has passed, both unconfirmed transactions should be stale.
113 [ + - + - : 2 : BOOST_CHECK_EQUAL(pb.GetStale().size(), 2);
+ - ]
114 : :
115 : : // Confirm reception by recipient1.
116 [ + - ]: 1 : pb.NodeConfirmedReception(nonexistent_recipient); // Dummy call.
117 [ + - ]: 1 : pb.NodeConfirmedReception(recipient1);
118 : :
119 [ + - ]: 1 : const auto infos{pb.GetBroadcastInfo()};
120 [ + - - + : 1 : BOOST_CHECK_EQUAL(infos.size(), 2);
+ - ]
121 : 1 : {
122 [ + - ]: 1 : const auto& peers{find_tx_info(infos, tx_for_recipient1).peers};
123 [ + - - + : 1 : BOOST_CHECK_EQUAL(peers.size(), 1);
+ - ]
124 [ + - + - : 1 : BOOST_CHECK_EQUAL(peers[0].address.ToStringAddrPort(), addr1.ToStringAddrPort());
+ - + - ]
125 [ + - + - : 2 : BOOST_CHECK(peers[0].received.has_value());
+ - ]
126 : : }
127 : 1 : {
128 [ + - ]: 1 : const auto& peers{find_tx_info(infos, tx_for_recipient2).peers};
129 [ + - - + : 1 : BOOST_CHECK_EQUAL(peers.size(), 1);
+ - ]
130 [ + - + - : 1 : BOOST_CHECK_EQUAL(peers[0].address.ToStringAddrPort(), addr2.ToStringAddrPort());
+ - + - ]
131 [ + - + - : 2 : BOOST_CHECK(!peers[0].received.has_value());
+ - ]
132 : : }
133 : :
134 [ + - ]: 1 : const auto stale_state{pb.GetStale()};
135 [ + - - + : 1 : BOOST_CHECK_EQUAL(stale_state.size(), 1);
+ - ]
136 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(stale_state[0], tx_for_recipient2);
137 : :
138 [ + - ]: 1 : clock += 10h;
139 : :
140 [ + - + - : 2 : BOOST_CHECK_EQUAL(pb.GetStale().size(), 2);
+ - ]
141 : :
142 [ + - + - : 2 : BOOST_CHECK_EQUAL(pb.Remove(tx_for_recipient1).value(), 2);
+ - ]
143 [ + - + - : 2 : BOOST_CHECK(!pb.Remove(tx_for_recipient1).has_value());
+ - + - ]
144 [ + - + - : 2 : BOOST_CHECK_EQUAL(pb.Remove(tx_for_recipient2).value(), 2);
+ - ]
145 [ + - + - : 2 : BOOST_CHECK(!pb.Remove(tx_for_recipient2).has_value());
+ - + - ]
146 : :
147 [ + - + - : 2 : BOOST_CHECK_EQUAL(pb.GetBroadcastInfo().size(), 0);
+ - ]
148 [ + - ]: 1 : const CService addr_nonexistent{ipv4Addr, 3333};
149 [ + - + - : 2 : BOOST_CHECK(!pb.PickTxForSend(/*will_send_to_nodeid=*/nonexistent_recipient, /*will_send_to_address=*/addr_nonexistent).has_value());
+ - ]
150 [ + - + - : 5 : }
+ - + - ]
151 : :
152 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(complete_all_initial_sends)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
153 : : {
154 [ + - ]: 1 : PrivateBroadcast pb;
155 [ + - ]: 1 : const auto tx{MakeDummyTx(/*id=*/1, /*num_witness=*/0)};
156 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(pb.Add(tx), PrivateBroadcast::AddResult::Added);
+ - ]
157 : :
158 : 1 : in_addr ipv4_addr;
159 : 1 : ipv4_addr.s_addr = 0xa0b0c001;
160 [ + - ]: 1 : const CService address{ipv4_addr, 1111};
161 : :
162 : : // Pick the first send, then receive the transaction back from the network.
163 [ + - + - : 2 : BOOST_REQUIRE_EQUAL(pb.PickTxForSend(/*will_send_to_nodeid=*/1, address).value(), tx);
+ - ]
164 [ + - + - : 1 : BOOST_CHECK_EQUAL(pb.Add(tx), PrivateBroadcast::AddResult::AlreadyPresent);
+ - ]
165 [ + - + - : 2 : BOOST_REQUIRE(pb.MarkResolved(tx));
+ - + - ]
166 [ + - + - : 1 : BOOST_CHECK_EQUAL(pb.Add(tx), PrivateBroadcast::AddResult::AlreadyPresent);
+ - ]
167 [ + - + - : 2 : BOOST_REQUIRE_EQUAL(pb.GetTxForNode(1).value(), tx);
+ - ]
168 [ + - + - : 2 : BOOST_CHECK(pb.GetBroadcastInfo().empty());
+ - + - ]
169 : :
170 : : // Receipt must not prevent either of the two remaining initial sends.
171 [ + - + - : 2 : BOOST_REQUIRE_EQUAL(pb.PickTxForSend(/*will_send_to_nodeid=*/2, address).value(), tx);
+ - ]
172 [ + - + - : 2 : BOOST_REQUIRE_EQUAL(pb.PickTxForSend(/*will_send_to_nodeid=*/3, address).value(), tx);
+ - ]
173 : :
174 [ + - + - : 1 : BOOST_CHECK_EQUAL(pb.Add(tx), PrivateBroadcast::AddResult::AlreadyPresent);
+ - ]
175 [ + - ]: 1 : pb.NodeDisconnected(/*nodeid=*/1);
176 [ + - + - : 2 : BOOST_CHECK(!pb.PickTxForSend(/*will_send_to_nodeid=*/4, address).has_value());
+ - ]
177 [ + - ]: 2 : }
178 : :
179 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(stale_unpicked_tx)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
180 : : {
181 : 1 : FakeNodeClock clock{};
182 : :
183 [ + - ]: 1 : PrivateBroadcast pb;
184 [ + - ]: 1 : const auto tx{MakeDummyTx(/*id=*/42, /*num_witness=*/0)};
185 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(pb.Add(tx), PrivateBroadcast::AddResult::Added);
+ - ]
186 : :
187 : : // Unpicked transactions use the longer INITIAL_STALE_DURATION.
188 [ + - + - : 2 : BOOST_CHECK_EQUAL(pb.GetStale().size(), 0);
+ - ]
189 [ + - ]: 1 : clock += PrivateBroadcast::INITIAL_STALE_DURATION - 1min;
190 [ + - + - : 2 : BOOST_CHECK_EQUAL(pb.GetStale().size(), 0);
+ - ]
191 [ + - ]: 1 : clock += 2min;
192 [ + - ]: 1 : const auto stale_state{pb.GetStale()};
193 [ + - - + : 1 : BOOST_REQUIRE_EQUAL(stale_state.size(), 1);
+ - ]
194 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(stale_state[0], tx);
195 [ + - ]: 2 : }
196 : :
197 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(send_attempt_limit)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
198 : : {
199 : 1 : FakeNodeClock clock{};
200 : :
201 : 1 : constexpr size_t max_attempts{5};
202 [ + - ]: 1 : PrivateBroadcast pb{PrivateBroadcast::MAX_TRANSACTIONS, max_attempts};
203 [ + - ]: 1 : const auto tx{MakeDummyTx(/*id=*/1, /*num_witness=*/0)};
204 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(pb.Add(tx), PrivateBroadcast::AddResult::Added);
+ - ]
205 : :
206 : 1 : in_addr ipv4_addr;
207 : 1 : ipv4_addr.s_addr = 0xa0b0c001;
208 [ + - ]: 1 : const CService address{ipv4_addr, 1111};
209 : :
210 : : NodeId node_id{0};
211 [ + + ]: 6 : for (size_t attempt{0}; attempt < max_attempts; ++attempt) {
212 [ + + ]: 5 : if (attempt >= PrivateBroadcast::INITIAL_CONNECTION_COUNT) {
213 [ + - + - : 4 : BOOST_REQUIRE(pb.TryGrantRetry(tx));
+ - ]
214 : : }
215 [ + - + - : 10 : BOOST_CHECK(pb.HavePendingTransactions());
+ - + - ]
216 [ + - + - : 10 : BOOST_REQUIRE_EQUAL(pb.PickTxForSend(/*will_send_to_nodeid=*/node_id++, address).value(), tx);
+ - ]
217 : : }
218 : :
219 : : // The transaction and its complete send history remain available, but no
220 : : // further connections should be opened for it.
221 [ + - + - : 2 : BOOST_CHECK(!pb.HavePendingTransactions());
+ - + - ]
222 [ + - + - : 2 : BOOST_CHECK(!pb.TryGrantRetry(tx));
+ - + - ]
223 [ + - + - : 2 : BOOST_CHECK(!pb.PickTxForSend(/*will_send_to_nodeid=*/node_id++, address).has_value());
+ - + - ]
224 [ + - ]: 1 : const auto info{pb.GetBroadcastInfo()};
225 [ + - - + : 1 : BOOST_REQUIRE_EQUAL(info.size(), 1);
+ - ]
226 [ + - - + : 1 : BOOST_CHECK_EQUAL(info[0].peers.size(), max_attempts);
+ - ]
227 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(info[0].attempts_remaining, 0);
228 : :
229 [ + - ]: 1 : clock += PrivateBroadcast::INITIAL_STALE_DURATION + 1min;
230 [ + - + - : 2 : BOOST_CHECK(pb.GetStale().empty());
+ - + - ]
231 : :
232 : : // An exhausted transaction does not prevent another transaction from being sent.
233 [ + - ]: 1 : const auto next_tx{MakeDummyTx(/*id=*/2, /*num_witness=*/0)};
234 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(pb.Add(next_tx), PrivateBroadcast::AddResult::Added);
+ - ]
235 [ + - + - : 2 : BOOST_CHECK(pb.HavePendingTransactions());
+ - + - ]
236 [ + - + - : 2 : BOOST_REQUIRE_EQUAL(pb.PickTxForSend(/*will_send_to_nodeid=*/node_id++, address).value(), next_tx);
+ - ]
237 : :
238 : : // Re-adding an exhausted transaction resets its state and starts a fresh
239 : : // initial stale period.
240 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(pb.Add(tx), PrivateBroadcast::AddResult::Added);
+ - ]
241 [ + - + - : 2 : BOOST_CHECK(pb.HavePendingTransactions());
+ - + - ]
242 [ + - + - : 2 : BOOST_CHECK(pb.GetStale().empty());
+ - + - ]
243 [ + - ]: 1 : const auto reset_info{pb.GetBroadcastInfo()};
244 [ + + ]: 3 : const auto reset_tx_info{std::ranges::find(reset_info, tx->GetWitnessHash(), [](const auto& entry) { return entry.tx->GetWitnessHash(); })};
245 [ + - + - : 2 : BOOST_REQUIRE(reset_tx_info != reset_info.end());
+ - ]
246 [ + - + - : 2 : BOOST_CHECK(reset_tx_info->peers.empty());
+ - ]
247 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(reset_tx_info->attempts_remaining, max_attempts);
248 [ + - + - : 2 : BOOST_REQUIRE_EQUAL(pb.PickTxForSend(/*will_send_to_nodeid=*/node_id++, address).value(), tx);
+ - ]
249 [ + - + - ]: 3 : }
250 : :
251 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(reset_with_equivalent_transaction_reference)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
252 : : {
253 [ + - ]: 1 : PrivateBroadcast pb{PrivateBroadcast::MAX_TRANSACTIONS, /*max_send_attempts=*/1};
254 [ + - ]: 1 : const auto tx{MakeDummyTx(/*id=*/1, /*num_witness=*/0)};
255 [ + - ]: 1 : const auto equivalent_tx{MakeDummyTx(/*id=*/1, /*num_witness=*/0)};
256 [ + - + - : 2 : BOOST_REQUIRE(tx != equivalent_tx);
+ - ]
257 [ + - - + : 2 : BOOST_REQUIRE(tx->GetWitnessHash() == equivalent_tx->GetWitnessHash());
+ - + - ]
258 : :
259 : 1 : in_addr ipv4_addr;
260 : 1 : ipv4_addr.s_addr = 0xa0b0c001;
261 [ + - ]: 1 : const CService address{ipv4_addr, 1111};
262 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(pb.Add(tx), PrivateBroadcast::AddResult::Added);
+ - ]
263 [ + - + - : 2 : BOOST_REQUIRE_EQUAL(pb.PickTxForSend(/*will_send_to_nodeid=*/0, address).value(), tx);
+ - ]
264 [ + - ]: 1 : pb.NodeConfirmedReception(/*nodeid=*/0);
265 [ + - ]: 1 : const auto confirmed_info{pb.GetBroadcastInfo()};
266 [ + - - + : 1 : BOOST_REQUIRE_EQUAL(confirmed_info.size(), 1);
+ - ]
267 [ + - - + : 1 : BOOST_REQUIRE_EQUAL(confirmed_info[0].peers.size(), 1);
+ - ]
268 [ + - + - : 2 : BOOST_CHECK(confirmed_info[0].peers[0].received.has_value());
+ - ]
269 [ + - + - : 2 : BOOST_CHECK(!pb.HavePendingTransactions());
+ - + - ]
270 : :
271 : : // A distinct CTransactionRef with the same WTXID must reset the exhausted
272 : : // transaction, including its send and confirmation history.
273 [ + - + - : 1 : BOOST_REQUIRE_EQUAL(pb.Add(equivalent_tx), PrivateBroadcast::AddResult::Added);
+ - ]
274 [ + - + - : 2 : BOOST_CHECK(pb.HavePendingTransactions());
+ - + - ]
275 [ + - + - : 2 : BOOST_CHECK(!pb.GetTxForNode(/*nodeid=*/0).has_value());
+ - + - ]
276 [ + - ]: 1 : const auto info{pb.GetBroadcastInfo()};
277 [ + - - + : 1 : BOOST_REQUIRE_EQUAL(info.size(), 1);
+ - ]
278 [ + - - + : 2 : BOOST_CHECK(info[0].tx->GetWitnessHash() == tx->GetWitnessHash());
+ - + - ]
279 [ + - + - : 2 : BOOST_CHECK(info[0].peers.empty());
+ - ]
280 [ + - + - : 2 : BOOST_CHECK_EQUAL(pb.Remove(equivalent_tx).value(), 1);
+ - ]
281 [ + - + - ]: 3 : }
282 : :
283 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(rejection_at_cap)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
284 : : {
285 [ + - ]: 1 : PrivateBroadcast pb;
286 : 1 : constexpr size_t num_cap{PrivateBroadcast::MAX_TRANSACTIONS};
287 : 1 : constexpr size_t num_over{5};
288 : :
289 : : // Fill the queue exactly to the cap; every distinct Add() succeeds.
290 : 1 : std::vector<CTransactionRef> txs;
291 [ + - ]: 1 : txs.reserve(num_cap);
292 [ + + ]: 10001 : for (size_t i{0}; i < num_cap; ++i) {
293 [ + - ]: 10000 : auto tx{MakeDummyTx(/*id=*/static_cast<uint32_t>(i), /*num_witness=*/0)};
294 [ + - + - : 10000 : BOOST_REQUIRE_EQUAL(pb.Add(tx), PrivateBroadcast::AddResult::Added);
+ - ]
295 [ + - - + ]: 10000 : txs.push_back(std::move(tx));
296 : 10000 : }
297 [ + - + - : 2 : BOOST_CHECK_EQUAL(pb.GetBroadcastInfo().size(), num_cap);
+ - ]
298 : :
299 : : // Further distinct transactions are rejected, and the queue is unchanged.
300 [ + + ]: 6 : for (size_t i{0}; i < num_over; ++i) {
301 [ + - ]: 5 : const auto tx{MakeDummyTx(/*id=*/static_cast<uint32_t>(num_cap + i), /*num_witness=*/0)};
302 [ + - + - : 5 : BOOST_CHECK_EQUAL(pb.Add(tx), PrivateBroadcast::AddResult::QueueFull);
+ - + - ]
303 : 5 : }
304 [ + - + - : 2 : BOOST_CHECK_EQUAL(pb.GetBroadcastInfo().size(), num_cap);
+ - ]
305 : :
306 : : // Nothing was evicted: all originally-added transactions are still present.
307 [ + - ]: 1 : const auto infos{pb.GetBroadcastInfo()};
308 : 1 : std::set<uint256> present_wtxids;
309 [ + + ]: 10001 : for (const auto& info : infos) {
310 [ + - ]: 10000 : present_wtxids.insert(info.tx->GetWitnessHash().ToUint256());
311 : : }
312 [ + - - + : 1 : BOOST_CHECK_EQUAL(present_wtxids.size(), infos.size());
+ - ]
313 [ + + ]: 10001 : for (size_t i{0}; i < num_cap; ++i) {
314 [ + - + - ]: 20000 : BOOST_CHECK_MESSAGE(present_wtxids.contains(txs[i]->GetWitnessHash().ToUint256()),
315 : : "tx index " << i << " should still be present");
316 : : }
317 : :
318 : : // Re-adding an already-present tx is AlreadyPresent even at the cap (not QueueFull).
319 [ + - + - : 1 : BOOST_CHECK_EQUAL(pb.Add(txs[0]), PrivateBroadcast::AddResult::AlreadyPresent);
+ - ]
320 [ + - + - : 2 : BOOST_CHECK_EQUAL(pb.GetBroadcastInfo().size(), num_cap);
+ - ]
321 : :
322 : : // Removing one frees exactly one slot for a new transaction.
323 [ + - + - : 2 : BOOST_REQUIRE(pb.Remove(txs[0]).has_value());
+ - + - ]
324 [ + - + - : 2 : BOOST_CHECK_EQUAL(pb.GetBroadcastInfo().size(), num_cap - 1);
+ - ]
325 [ + - ]: 1 : const auto fresh{MakeDummyTx(/*id=*/0xffffffff, /*num_witness=*/0)};
326 [ + - + - : 1 : BOOST_CHECK_EQUAL(pb.Add(fresh), PrivateBroadcast::AddResult::Added);
+ - ]
327 [ + - + - : 2 : BOOST_CHECK_EQUAL(pb.GetBroadcastInfo().size(), num_cap);
+ - ]
328 : :
329 : : // A previously-removed tx can be added again as a brand-new entry.
330 [ + - + - : 2 : BOOST_REQUIRE(pb.Remove(fresh).has_value());
+ - + - ]
331 [ + - + - : 1 : BOOST_CHECK_EQUAL(pb.Add(fresh), PrivateBroadcast::AddResult::Added);
+ - ]
332 [ + - + - : 2 : BOOST_CHECK_EQUAL(pb.GetBroadcastInfo().size(), num_cap);
+ - + - ]
333 : 1 : }
334 : :
335 : : BOOST_AUTO_TEST_SUITE_END()
|