Branch data Line data Source code
1 : : // Copyright (c) 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 <cluster_linearize.h>
6 : : #include <random.h>
7 : : #include <serialize.h>
8 : : #include <streams.h>
9 : : #include <test/fuzz/FuzzedDataProvider.h>
10 : : #include <test/fuzz/fuzz.h>
11 : : #include <test/util/cluster_linearize.h>
12 : : #include <util/bitset.h>
13 : : #include <util/feefrac.h>
14 : :
15 : : #include <algorithm>
16 : : #include <cstdint>
17 : : #include <utility>
18 : : #include <vector>
19 : :
20 : : /*
21 : : * The tests in this file primarily cover the candidate finder classes and linearization algorithms.
22 : : *
23 : : * <----: An implementation (at the start of the line --) is tested in the test marked with *,
24 : : * possibly by comparison with other implementations (at the end of the line ->).
25 : : * <<---: The right side is implemented using the left side.
26 : : *
27 : : * +---------------------+ +-----------+
28 : : * | SpanningForestState | <<-------------------- | Linearize |
29 : : * +---------------------+ +-----------+
30 : : * | |
31 : : * | | ^^ PRODUCTION CODE
32 : : * | | ||
33 : : * ==============================================================================================
34 : : * | | ||
35 : : * |-clusterlin_sfl* | vv TEST CODE
36 : : * | |
37 : : * \------------------------------------\ |-clusterlin_linearize*
38 : : * | |
39 : : * v v
40 : : * +-----------------------+ +-----------------+
41 : : * | SimpleCandidateFinder | <<-------------------| SimpleLinearize |
42 : : * +-----------------------+ +-----------------+
43 : : * | |
44 : : * |-clusterlin_simple_finder* |-clusterlin_simple_linearize*
45 : : * v v
46 : : * +---------------------------+ +---------------------+
47 : : * | ExhaustiveCandidateFinder | | ExhaustiveLinearize |
48 : : * +---------------------------+ +---------------------+
49 : : *
50 : : * More tests are included for lower-level and related functions and classes:
51 : : * - DepGraph tests:
52 : : * - clusterlin_depgraph_sim
53 : : * - clusterlin_depgraph_serialization
54 : : * - clusterlin_components
55 : : * - ChunkLinearization and ChunkLinearizationInfo tests:
56 : : * - clusterlin_chunking
57 : : * - PostLinearize tests:
58 : : * - clusterlin_postlinearize
59 : : * - clusterlin_postlinearize_tree
60 : : * - clusterlin_postlinearize_moved_leaf
61 : : * - MakeConnected tests (a test-only function):
62 : : * - clusterlin_make_connected
63 : : */
64 : :
65 : : using namespace cluster_linearize;
66 : :
67 : : namespace {
68 : :
69 : : /** A simple finder class for candidate sets (topologically-valid subsets with high feerate), only
70 : : * used by SimpleLinearize below. */
71 : : template<typename SetType>
72 : : class SimpleCandidateFinder
73 : : {
74 : : /** Internal dependency graph. */
75 : : const DepGraph<SetType>& m_depgraph;
76 : : /** Which transaction are left to include. */
77 : : SetType m_todo;
78 : :
79 : : public:
80 : : /** Construct an SimpleCandidateFinder for a given graph. */
81 : 1556 : SimpleCandidateFinder(const DepGraph<SetType>& depgraph LIFETIMEBOUND) noexcept :
82 : 1556 : m_depgraph(depgraph), m_todo{depgraph.Positions()} {}
83 : :
84 : : /** Remove a set of transactions from the set of to-be-linearized ones. */
85 : 16033 : void MarkDone(SetType select) noexcept { m_todo -= select; }
86 : :
87 : : /** Determine whether unlinearized transactions remain. */
88 : 2250 : bool AllDone() const noexcept { return m_todo.None(); }
89 : :
90 : : /** Find a candidate set using at most max_iterations iterations, and the number of iterations
91 : : * actually performed. If that number is less than max_iterations, then the result is optimal.
92 : : *
93 : : * Always returns a connected set of transactions.
94 : : *
95 : : * Complexity: O(N * M), where M is the number of connected topological subsets of the cluster.
96 : : * That number is bounded by M <= 2^(N-1).
97 : : */
98 : 16033 : std::pair<SetInfo<SetType>, uint64_t> FindCandidateSet(uint64_t max_iterations) const noexcept
99 : : {
100 : 16033 : uint64_t iterations_left = max_iterations;
101 : : // Queue of work units. Each consists of:
102 : : // - inc: set of transactions definitely included
103 : : // - und: set of transactions that can be added to inc still
104 : 16033 : std::vector<std::pair<SetType, SetType>> queue;
105 : : // Initially we have just one queue element, with the entire graph in und.
106 : 16033 : queue.emplace_back(SetType{}, m_todo);
107 : : // Best solution so far. Initialize with the remaining ancestors of the first remaining
108 : : // transaction.
109 : 16033 : SetInfo best(m_depgraph, m_depgraph.Ancestors(m_todo.First()) & m_todo);
110 : : // Process the queue.
111 [ + + + + ]: 53362628 : while (!queue.empty() && iterations_left) {
112 : : // Pop top element of the queue.
113 : 53346595 : auto [inc, und] = queue.back();
114 : 53346595 : queue.pop_back();
115 : : // Look for a transaction to consider adding/removing.
116 : 53346595 : bool inc_none = inc.None();
117 [ + + ]: 117392999 : for (auto split : und) {
118 : : // If inc is empty, consider any split transaction. Otherwise only consider
119 : : // transactions that share ancestry with inc so far (which means only connected
120 : : // sets will be considered).
121 [ + + + + ]: 90713131 : if (inc_none || inc.Overlaps(m_depgraph.Ancestors(split))) {
122 : 26666727 : --iterations_left;
123 : : // Add a queue entry with split included.
124 : 26666727 : SetInfo new_inc(m_depgraph, inc | (m_todo & m_depgraph.Ancestors(split)));
125 : 26666727 : queue.emplace_back(new_inc.transactions, und - new_inc.transactions);
126 : : // Add a queue entry with split excluded.
127 : 53333454 : queue.emplace_back(inc, und - m_depgraph.Descendants(split));
128 : : // Update statistics to account for the candidate new_inc.
129 [ + + ]: 26666727 : if (new_inc.feerate > best.feerate) best = new_inc;
130 : : break;
131 : : }
132 : : }
133 : : }
134 : 16033 : return {std::move(best), max_iterations - iterations_left};
135 : 16033 : }
136 : : };
137 : :
138 : : /** A very simple finder class for optimal candidate sets, which tries every subset.
139 : : *
140 : : * It is even simpler than SimpleCandidateFinder, and exists just to help test the correctness of
141 : : * SimpleCandidateFinder, so that it can be used in SimpleLinearize, which is then used to test the
142 : : * correctness of Linearize.
143 : : */
144 : : template<typename SetType>
145 : : class ExhaustiveCandidateFinder
146 : : {
147 : : /** Internal dependency graph. */
148 : : const DepGraph<SetType>& m_depgraph;
149 : : /** Which transaction are left to include. */
150 : : SetType m_todo;
151 : :
152 : : public:
153 : : /** Construct an ExhaustiveCandidateFinder for a given graph. */
154 : 220 : ExhaustiveCandidateFinder(const DepGraph<SetType>& depgraph LIFETIMEBOUND) noexcept :
155 : 220 : m_depgraph(depgraph), m_todo{depgraph.Positions()} {}
156 : :
157 : : /** Remove a set of transactions from the set of to-be-linearized ones. */
158 : 2030 : void MarkDone(SetType select) noexcept { m_todo -= select; }
159 : :
160 : : /** Determine whether unlinearized transactions remain. */
161 : 2250 : bool AllDone() const noexcept { return m_todo.None(); }
162 : :
163 : : /** Find the optimal remaining candidate set.
164 : : *
165 : : * Complexity: O(N * 2^N).
166 : : */
167 : 1262 : SetInfo<SetType> FindCandidateSet() const noexcept
168 : : {
169 : : // Best solution so far.
170 : 1262 : SetInfo<SetType> best{m_todo, m_depgraph.FeeRate(m_todo)};
171 : : // The number of combinations to try.
172 : 1262 : uint64_t limit = (uint64_t{1} << m_todo.Count()) - 1;
173 : : // Try the transitive closure of every non-empty subset of m_todo.
174 [ + + ]: 576878 : for (uint64_t x = 1; x < limit; ++x) {
175 : : // If bit number b is set in x, then the remaining ancestors of the b'th remaining
176 : : // transaction in m_todo are included.
177 : 575616 : SetType txn;
178 : 575616 : auto x_shifted{x};
179 [ + - + + ]: 7402708 : for (auto i : m_todo) {
180 [ + + ]: 6251476 : if (x_shifted & 1) txn |= m_depgraph.Ancestors(i);
181 : 6251476 : x_shifted >>= 1;
182 : : }
183 : 575616 : SetInfo cur(m_depgraph, txn & m_todo);
184 [ + + ]: 575616 : if (cur.feerate > best.feerate) best = cur;
185 : : }
186 : 1262 : return best;
187 : : }
188 : : };
189 : :
190 : : /** A simple linearization algorithm.
191 : : *
192 : : * This matches Linearize() in interface and behavior, though with fewer optimizations, lacking
193 : : * the ability to pass in an existing linearization, and linearizing by simply finding the
194 : : * consecutive remaining highest-feerate topological subset using SimpleCandidateFinder.
195 : : */
196 : : template<typename SetType>
197 : 1336 : std::pair<std::vector<DepGraphIndex>, bool> SimpleLinearize(const DepGraph<SetType>& depgraph, uint64_t max_iterations)
198 : : {
199 : 1336 : std::vector<DepGraphIndex> linearization;
200 : 1336 : SimpleCandidateFinder finder(depgraph);
201 : 1336 : SetType todo = depgraph.Positions();
202 : 1336 : bool optimal = true;
203 [ + + ]: 15339 : while (todo.Any()) {
204 [ + + ]: 14003 : auto [candidate, iterations_done] = finder.FindCandidateSet(max_iterations);
205 [ + + ]: 14003 : if (iterations_done == max_iterations) optimal = false;
206 : 14003 : depgraph.AppendTopo(linearization, candidate.transactions);
207 : 14003 : todo -= candidate.transactions;
208 : 14003 : finder.MarkDone(candidate.transactions);
209 : 14003 : max_iterations -= iterations_done;
210 : : }
211 : 1336 : return {std::move(linearization), optimal};
212 : 1336 : }
213 : :
214 : : /** An even simpler linearization algorithm that tries all permutations.
215 : : *
216 : : * This roughly matches SimpleLinearize() (and Linearize) in interface and behavior, but always
217 : : * tries all topologically-valid transaction orderings, has no way to bound how much work it does,
218 : : * and always finds the optimal. With an O(n!) complexity, it should only be used for small
219 : : * clusters.
220 : : */
221 : : template<typename SetType>
222 : 145 : std::vector<DepGraphIndex> ExhaustiveLinearize(const DepGraph<SetType>& depgraph)
223 : : {
224 : : // The best linearization so far, and its chunking.
225 : 145 : std::vector<DepGraphIndex> linearization;
226 : 145 : std::vector<FeeFrac> chunking;
227 : :
228 [ + + ]: 145 : std::vector<DepGraphIndex> perm_linearization;
229 : : // Initialize with the lexicographically-first linearization.
230 [ + + + - : 990 : for (DepGraphIndex i : depgraph.Positions()) perm_linearization.push_back(i);
+ + ]
231 : : // Iterate over all valid permutations.
232 : : do {
233 : : /** What prefix of perm_linearization is topological. */
234 : 609325 : DepGraphIndex topo_length{0};
235 : 609325 : TestBitSet perm_done;
236 [ - + + + ]: 5219530 : while (topo_length < perm_linearization.size()) {
237 : 4684106 : auto i = perm_linearization[topo_length];
238 [ + + ]: 4684106 : perm_done.Set(i);
239 [ + + ]: 4684106 : if (!depgraph.Ancestors(i).IsSubsetOf(perm_done)) break;
240 : 4610205 : ++topo_length;
241 : : }
242 [ - + + + ]: 609325 : if (topo_length == perm_linearization.size()) {
243 : : // If all of perm_linearization is topological, check if it is perhaps our best
244 : : // linearization so far.
245 [ - + ]: 535424 : auto perm_chunking = ChunkLinearization(depgraph, perm_linearization);
246 [ - + + - ]: 535424 : auto cmp = CompareChunks(perm_chunking, chunking);
247 : : // If the diagram is better, or if it is equal but with more chunks (because we
248 : : // prefer minimal chunks), consider this better.
249 [ + + + + : 591300 : if (linearization.empty() || cmp > 0 || (cmp == 0 && perm_chunking.size() > chunking.size())) {
+ + - + +
+ ]
250 [ + - ]: 1710 : linearization = perm_linearization;
251 [ + - ]: 1710 : chunking = perm_chunking;
252 : : }
253 : 535424 : } else {
254 : : // Otherwise, fast forward to the last permutation with the same non-topological
255 : : // prefix.
256 : 73901 : auto first_non_topo = perm_linearization.begin() + topo_length;
257 [ - + ]: 73901 : assert(std::is_sorted(first_non_topo + 1, perm_linearization.end()));
258 : 73901 : std::reverse(first_non_topo + 1, perm_linearization.end());
259 : : }
260 [ + + ]: 609325 : } while(std::next_permutation(perm_linearization.begin(), perm_linearization.end()));
261 : :
262 : 145 : return linearization;
263 : 145 : }
264 : :
265 : :
266 : : /** Stitch connected components together in a DepGraph, guaranteeing its corresponding cluster is connected. */
267 : : template<typename BS>
268 : 1166 : void MakeConnected(DepGraph<BS>& depgraph)
269 : : {
270 : 1166 : auto todo = depgraph.Positions();
271 : 1166 : auto comp = depgraph.FindConnectedComponent(todo);
272 [ - + ]: 1166 : Assume(depgraph.IsConnected(comp));
273 : 1166 : todo -= comp;
274 [ + + ]: 11492 : while (todo.Any()) {
275 : 10326 : auto nextcomp = depgraph.FindConnectedComponent(todo);
276 [ - + ]: 10326 : Assume(depgraph.IsConnected(nextcomp));
277 : 10326 : depgraph.AddDependencies(BS::Singleton(comp.Last()), nextcomp.First());
278 : 10326 : todo -= nextcomp;
279 : 10326 : comp = nextcomp;
280 : : }
281 : 1166 : }
282 : :
283 : : /** Given a dependency graph, and a todo set, read a topological subset of todo from reader. */
284 : : template<typename SetType>
285 : 4019 : SetType ReadTopologicalSet(const DepGraph<SetType>& depgraph, const SetType& todo, SpanReader& reader, bool non_empty)
286 : : {
287 : : // Read a bitmask from the fuzzing input. Add 1 if non_empty, so the mask is definitely not
288 : : // zero in that case.
289 [ + + ]: 4019 : uint64_t mask{0};
290 : : try {
291 [ + + ]: 4019 : reader >> VARINT(mask);
292 [ - + ]: 3097 : } catch(const std::ios_base::failure&) {}
293 [ + + ]: 4019 : if (mask != uint64_t(-1)) mask += non_empty;
294 : :
295 : 4019 : SetType ret;
296 [ + - + + ]: 53658 : for (auto i : todo) {
297 [ + + ]: 45620 : if (!ret[i]) {
298 [ + + ]: 43365 : if (mask & 1) ret |= depgraph.Ancestors(i);
299 : 43365 : mask >>= 1;
300 : : }
301 : : }
302 : 4019 : ret &= todo;
303 : :
304 : : // While mask starts off non-zero if non_empty is true, it is still possible that all its low
305 : : // bits are 0, and ret ends up being empty. As a last resort, use the in-todo ancestry of the
306 : : // first todo position.
307 [ + - + + ]: 4019 : if (non_empty && ret.None()) {
308 [ - + ]: 174 : Assume(todo.Any());
309 [ - + ]: 174 : ret = depgraph.Ancestors(todo.First()) & todo;
310 [ - + ]: 174 : Assume(ret.Any());
311 : : }
312 : 4019 : return ret;
313 : : }
314 : :
315 : : /** Given a dependency graph, construct any valid linearization for it, reading from a SpanReader. */
316 : : template<typename BS>
317 : 8558 : std::vector<DepGraphIndex> ReadLinearization(const DepGraph<BS>& depgraph, SpanReader& reader, bool topological=true)
318 : : {
319 : 8558 : std::vector<DepGraphIndex> linearization;
320 : 8558 : TestBitSet todo = depgraph.Positions();
321 : : // In every iteration one transaction is appended to linearization.
322 [ + + ]: 214203 : while (todo.Any()) {
323 : : // Compute the set of transactions to select from.
324 : 197087 : TestBitSet potential_next;
325 [ + + ]: 197087 : if (topological) {
326 : : // Find all transactions with no not-yet-included ancestors.
327 [ + + ]: 2993316 : for (auto j : todo) {
328 [ + + ]: 4073789 : if ((depgraph.Ancestors(j) & todo) == TestBitSet::Singleton(j)) {
329 : 1274580 : potential_next.Set(j);
330 : : }
331 : : }
332 : : } else {
333 : : // Allow any element to be selected next, regardless of topology.
334 : 2980 : potential_next = todo;
335 : : }
336 : : // There must always be one (otherwise there is a cycle in the graph).
337 [ - + ]: 197087 : assert(potential_next.Any());
338 : : // Read a number from reader, and interpret it as index into potential_next.
339 [ + + ]: 197087 : uint64_t idx{0};
340 : : try {
341 [ + + + - ]: 394174 : reader >> VARINT(idx);
342 [ - + ]: 186489 : } catch (const std::ios_base::failure&) {}
343 : 197087 : idx %= potential_next.Count();
344 : : // Find out which transaction that corresponds to.
345 [ + - + - ]: 423624 : for (auto j : potential_next) {
346 [ + + ]: 226537 : if (idx == 0) {
347 : : // When found, add it to linearization and remove it from todo.
348 [ + - ]: 197087 : linearization.push_back(j);
349 [ - + ]: 197087 : assert(todo[j]);
350 : 197087 : todo.Reset(j);
351 : 197087 : break;
352 : : }
353 : 29450 : --idx;
354 : : }
355 : : }
356 : 8558 : return linearization;
357 : 0 : }
358 : :
359 : : /** Given a dependency graph, construct a tree-structured graph.
360 : : *
361 : : * Copies the nodes from the depgraph, but only keeps the first parent (even direction)
362 : : * or the first child (odd direction) for each transaction.
363 : : */
364 : : template<typename BS>
365 : 577 : DepGraph<BS> BuildTreeGraph(const DepGraph<BS>& depgraph, uint8_t direction)
366 : : {
367 : 577 : DepGraph<BS> depgraph_tree;
368 [ - + + + ]: 16959 : for (DepGraphIndex i = 0; i < depgraph.PositionRange(); ++i) {
369 [ + + ]: 16382 : if (depgraph.Positions()[i]) {
370 : 12906 : depgraph_tree.AddTransaction(depgraph.FeeRate(i));
371 : : } else {
372 : : // For holes, add a dummy transaction which is deleted below, so that non-hole
373 : : // transactions retain their position.
374 : 3476 : depgraph_tree.AddTransaction(FeeFrac{});
375 : : }
376 : : }
377 : 577 : depgraph_tree.RemoveTransactions(BS::Fill(depgraph.PositionRange()) - depgraph.Positions());
378 : :
379 [ + + ]: 577 : if (direction & 1) {
380 [ + + + + ]: 8279 : for (DepGraphIndex i : depgraph.Positions()) {
381 [ + + ]: 7634 : auto children = depgraph.GetReducedChildren(i);
382 [ + + ]: 7634 : if (children.Any()) {
383 : 4250 : depgraph_tree.AddDependencies(BS::Singleton(i), children.First());
384 : : }
385 : : }
386 : : } else {
387 [ + + + + ]: 5768 : for (DepGraphIndex i : depgraph.Positions()) {
388 [ + + ]: 5272 : auto parents = depgraph.GetReducedParents(i);
389 [ + + ]: 5272 : if (parents.Any()) {
390 : 2523 : depgraph_tree.AddDependencies(BS::Singleton(parents.First()), i);
391 : : }
392 : : }
393 : : }
394 : :
395 : 577 : return depgraph_tree;
396 : : }
397 : :
398 : : } // namespace
399 : :
400 [ + - ]: 881 : FUZZ_TARGET(clusterlin_depgraph_sim)
401 : : {
402 : : // Simulation test to verify the full behavior of DepGraph.
403 : :
404 : 431 : FuzzedDataProvider provider(buffer.data(), buffer.size());
405 : :
406 : : /** Real DepGraph being tested. */
407 : 431 : DepGraph<TestBitSet> real;
408 : : /** Simulated DepGraph (sim[i] is std::nullopt if position i does not exist; otherwise,
409 : : * sim[i]->first is its individual feerate, and sim[i]->second is its set of ancestors. */
410 : 431 : std::array<std::optional<std::pair<FeeFrac, TestBitSet>>, TestBitSet::Size()> sim;
411 : : /** The number of non-nullopt position in sim. */
412 : 431 : DepGraphIndex num_tx_sim{0};
413 : :
414 : : /** Read a valid index of a transaction from the provider. */
415 : 26963 : auto idx_fn = [&]() {
416 : 26532 : auto offset = provider.ConsumeIntegralInRange<DepGraphIndex>(0, num_tx_sim - 1);
417 [ + - ]: 171500 : for (DepGraphIndex i = 0; i < sim.size(); ++i) {
418 [ + + ]: 171500 : if (!sim[i].has_value()) continue;
419 [ + + ]: 149275 : if (offset == 0) return i;
420 : 122743 : --offset;
421 : : }
422 : 0 : assert(false);
423 : : return DepGraphIndex(-1);
424 : 431 : };
425 : :
426 : : /** Read a valid subset of the transactions from the provider. */
427 : 26963 : auto subset_fn = [&]() {
428 : 26532 : auto range = (uint64_t{1} << num_tx_sim) - 1;
429 : 26532 : const auto mask = provider.ConsumeIntegralInRange<uint64_t>(0, range);
430 : 26532 : auto mask_shifted = mask;
431 : 26532 : TestBitSet subset;
432 [ + + ]: 875556 : for (DepGraphIndex i = 0; i < sim.size(); ++i) {
433 [ + + ]: 849024 : if (!sim[i].has_value()) continue;
434 [ + + ]: 527274 : if (mask_shifted & 1) {
435 : 136723 : subset.Set(i);
436 : : }
437 : 527274 : mask_shifted >>= 1;
438 : : }
439 [ - + ]: 26532 : assert(mask_shifted == 0);
440 : 26532 : return subset;
441 : 431 : };
442 : :
443 : : /** Read any set of transactions from the provider (including unused positions). */
444 : 13155 : auto set_fn = [&]() {
445 : 12724 : auto range = (uint64_t{1} << sim.size()) - 1;
446 : 12724 : const auto mask = provider.ConsumeIntegralInRange<uint64_t>(0, range);
447 : 12724 : TestBitSet set;
448 [ + + ]: 419892 : for (DepGraphIndex i = 0; i < sim.size(); ++i) {
449 [ + + ]: 407168 : if ((mask >> i) & 1) {
450 : 151572 : set.Set(i);
451 : : }
452 : : }
453 : 12724 : return set;
454 : 431 : };
455 : :
456 : : /** Propagate ancestor information in sim. */
457 : 13586 : auto anc_update_fn = [&]() {
458 : 15256 : while (true) {
459 : 15256 : bool updates{false};
460 [ + + ]: 503448 : for (DepGraphIndex chl = 0; chl < sim.size(); ++chl) {
461 [ + + ]: 488192 : if (!sim[chl].has_value()) continue;
462 [ + - + + ]: 1056821 : for (auto par : sim[chl]->second) {
463 [ + + ]: 633345 : if (!sim[chl]->second.IsSupersetOf(sim[par]->second)) {
464 : 8446 : sim[chl]->second |= sim[par]->second;
465 : 8446 : updates = true;
466 : : }
467 : : }
468 : : }
469 [ + + ]: 15256 : if (!updates) break;
470 : : }
471 : 13586 : };
472 : :
473 : : /** Compare the state of transaction i in the simulation with the real one. */
474 : 165795 : auto check_fn = [&](DepGraphIndex i) {
475 : : // Compare used positions.
476 [ - + ]: 165364 : assert(real.Positions()[i] == sim[i].has_value());
477 [ + + ]: 165364 : if (sim[i].has_value()) {
478 : : // Compare feerate.
479 [ + - ]: 34251 : assert(real.FeeRate(i) == sim[i]->first);
480 : : // Compare ancestors (note that SanityCheck verifies correspondence between ancestors
481 : : // and descendants, so we can restrict ourselves to ancestors here).
482 [ - + ]: 34251 : assert(real.Ancestors(i) == sim[i]->second);
483 : : }
484 : 165795 : };
485 : :
486 : 431 : auto last_compaction_pos{real.PositionRange()};
487 : :
488 [ + + + + ]: 108965 : LIMITED_WHILE(provider.remaining_bytes() > 0, 1000) {
489 : 108534 : int command = provider.ConsumeIntegral<uint8_t>() % 4;
490 : 114160 : while (true) {
491 : : // Iterate decreasing command until an applicable branch is found.
492 [ + + + + ]: 114160 : if (num_tx_sim < TestBitSet::Size() && command-- == 0) {
493 : : // AddTransaction.
494 : 34251 : auto fee = provider.ConsumeIntegralInRange<int64_t>(-0x8000000000000, 0x7ffffffffffff);
495 : 34251 : auto size = provider.ConsumeIntegralInRange<int32_t>(1, 0x3fffff);
496 : 34251 : FeeFrac feerate{fee, size};
497 : : // Apply to DepGraph.
498 : 34251 : auto idx = real.AddTransaction(feerate);
499 : : // Verify that the returned index is correct.
500 [ - + ]: 34251 : assert(!sim[idx].has_value());
501 [ + - ]: 441768 : for (DepGraphIndex i = 0; i < TestBitSet::Size(); ++i) {
502 [ + + ]: 441768 : if (!sim[i].has_value()) {
503 [ - + ]: 34251 : assert(idx == i);
504 : : break;
505 : : }
506 : : }
507 : : // Update sim.
508 [ - + ]: 34251 : sim[idx] = {feerate, TestBitSet::Singleton(idx)};
509 : 34251 : ++num_tx_sim;
510 : 34251 : break;
511 [ + + + + ]: 79909 : } else if (num_tx_sim > 0 && command-- == 0) {
512 : : // AddDependencies.
513 : 26532 : DepGraphIndex child = idx_fn();
514 : 26532 : auto parents = subset_fn();
515 : : // Apply to DepGraph.
516 : 26532 : real.AddDependencies(parents, child);
517 : : // Apply to sim.
518 : 26532 : sim[child]->second |= parents;
519 : 26532 : break;
520 [ + + + + ]: 53377 : } else if (num_tx_sim > 0 && command-- == 0) {
521 : : // Remove transactions.
522 : 12724 : auto del = set_fn();
523 : : // Propagate all ancestry information before deleting anything in the simulation (as
524 : : // intermediary transactions may be deleted which impact connectivity).
525 : 12724 : anc_update_fn();
526 : : // Compare the state of the transactions being deleted.
527 [ + + + + ]: 175722 : for (auto i : del) check_fn(i);
528 : : // Apply to DepGraph.
529 : 12724 : real.RemoveTransactions(del);
530 : : // Apply to sim.
531 [ + + ]: 419892 : for (DepGraphIndex i = 0; i < sim.size(); ++i) {
532 [ + + ]: 407168 : if (sim[i].has_value()) {
533 [ + + ]: 150516 : if (del[i]) {
534 : 28336 : --num_tx_sim;
535 [ + - ]: 435504 : sim[i] = std::nullopt;
536 : : } else {
537 : 122180 : sim[i]->second -= del;
538 : : }
539 : : }
540 : : }
541 : : break;
542 [ + + ]: 40653 : } else if (command-- == 0) {
543 : : // Compact.
544 [ - + ]: 35027 : const size_t mem_before{real.DynamicMemoryUsage()};
545 : 35027 : real.Compact();
546 [ - + ]: 35027 : const size_t mem_after{real.DynamicMemoryUsage()};
547 [ - + + + : 35027 : assert(real.PositionRange() < last_compaction_pos ? mem_after < mem_before : mem_after <= mem_before);
- + ]
548 : : last_compaction_pos = real.PositionRange();
549 : : break;
550 : : }
551 : : }
552 : : }
553 : :
554 : : // Compare the real obtained depgraph against the simulation.
555 : 431 : anc_update_fn();
556 [ + + ]: 14223 : for (DepGraphIndex i = 0; i < sim.size(); ++i) check_fn(i);
557 [ - + ]: 431 : assert(real.TxCount() == num_tx_sim);
558 : : // Sanity check the result (which includes round-tripping serialization, if applicable).
559 [ + - ]: 431 : SanityCheck(real);
560 : 431 : }
561 : :
562 [ + - ]: 714 : FUZZ_TARGET(clusterlin_depgraph_serialization)
563 : : {
564 : : // Verify that any deserialized depgraph is acyclic and roundtrips to an identical depgraph.
565 : :
566 : : // Construct a graph by deserializing.
567 : 264 : SpanReader reader(buffer);
568 : 264 : DepGraph<TestBitSet> depgraph;
569 : 264 : DepGraphIndex par_code{0}, chl_code{0};
570 : 264 : try {
571 [ + - + + : 264 : reader >> Using<DepGraphFormatter>(depgraph) >> VARINT(par_code) >> VARINT(chl_code);
+ + ]
572 [ - + ]: 229 : } catch (const std::ios_base::failure&) {}
573 [ + - ]: 264 : SanityCheck(depgraph);
574 : :
575 : : // Verify the graph is a DAG.
576 [ - + ]: 264 : assert(depgraph.IsAcyclic());
577 : :
578 : : // Introduce a cycle, and then test that IsAcyclic returns false.
579 [ + + ]: 264 : if (depgraph.TxCount() < 2) return;
580 : 243 : DepGraphIndex par(0), chl(0);
581 : : // Pick any transaction of depgraph as parent.
582 [ + - ]: 243 : par_code %= depgraph.TxCount();
583 [ + - + - ]: 812 : for (auto i : depgraph.Positions()) {
584 [ + + ]: 569 : if (par_code == 0) {
585 : : par = i;
586 : : break;
587 : : }
588 : 326 : --par_code;
589 : : }
590 : : // Pick any ancestor of par (excluding itself) as child, if any.
591 [ + + ]: 243 : auto ancestors = depgraph.Ancestors(par) - TestBitSet::Singleton(par);
592 [ + + ]: 243 : if (ancestors.None()) return;
593 : 130 : chl_code %= ancestors.Count();
594 [ + - ]: 246 : for (auto i : ancestors) {
595 [ + + ]: 246 : if (chl_code == 0) {
596 : : chl = i;
597 : : break;
598 : : }
599 : 116 : --chl_code;
600 : : }
601 : : // Add the cycle-introducing dependency.
602 : 130 : depgraph.AddDependencies(TestBitSet::Singleton(par), chl);
603 : : // Check that we now detect a cycle.
604 [ - + ]: 130 : assert(!depgraph.IsAcyclic());
605 : 264 : }
606 : :
607 [ + - ]: 583 : FUZZ_TARGET(clusterlin_components)
608 : : {
609 : : // Verify the behavior of DepGraphs's FindConnectedComponent and IsConnected functions.
610 : :
611 : : // Construct a depgraph.
612 : 133 : SpanReader reader(buffer);
613 : 133 : DepGraph<TestBitSet> depgraph;
614 : 133 : std::vector<DepGraphIndex> linearization;
615 : 133 : try {
616 [ + - ]: 133 : reader >> Using<DepGraphFormatter>(depgraph);
617 [ - - ]: 0 : } catch (const std::ios_base::failure&) {}
618 : :
619 : 133 : TestBitSet todo = depgraph.Positions();
620 [ + + ]: 1540 : while (todo.Any()) {
621 : : // Pick a transaction in todo, or nothing.
622 : 1407 : std::optional<DepGraphIndex> picked;
623 : 1407 : {
624 : 1407 : uint64_t picked_num{0};
625 : 1407 : try {
626 [ + + ]: 1407 : reader >> VARINT(picked_num);
627 [ - + ]: 975 : } catch (const std::ios_base::failure&) {}
628 [ + + + + ]: 1407 : if (picked_num < todo.Size() && todo[picked_num]) {
629 : 245 : picked = picked_num;
630 : : }
631 : : }
632 : :
633 : : // Find a connected component inside todo, including picked if any.
634 [ + + ]: 1407 : auto component = picked ? depgraph.GetConnectedComponent(todo, *picked)
635 : 1162 : : depgraph.FindConnectedComponent(todo);
636 : :
637 : : // The component must be a subset of todo and non-empty.
638 [ - + ]: 1407 : assert(component.IsSubsetOf(todo));
639 [ - + ]: 1407 : assert(component.Any());
640 : :
641 : : // If picked was provided, the component must include it.
642 [ + + - + ]: 1407 : if (picked) assert(component[*picked]);
643 : :
644 : : // If todo is the entire graph, and the entire graph is connected, then the component must
645 : : // be the entire graph.
646 [ + + ]: 1407 : if (todo == depgraph.Positions()) {
647 [ + + - + ]: 207 : assert((component == todo) == depgraph.IsConnected());
648 : : }
649 : :
650 : : // If subset is connected, then component must match subset.
651 [ + + - + ]: 2414 : assert((component == todo) == depgraph.IsConnected(todo));
652 : :
653 : : // The component cannot have any ancestors or descendants outside of component but in todo.
654 [ + - + + ]: 10454 : for (auto i : component) {
655 [ - + ]: 7640 : assert((depgraph.Ancestors(i) & todo).IsSubsetOf(component));
656 [ - + ]: 7640 : assert((depgraph.Descendants(i) & todo).IsSubsetOf(component));
657 : : }
658 : :
659 : : // Starting from any component element, we must be able to reach every element.
660 [ + - + + ]: 10454 : for (auto i : component) {
661 : : // Start with just i as reachable.
662 : 7640 : TestBitSet reachable = TestBitSet::Singleton(i);
663 : : // Add in-todo descendants and ancestors to reachable until it does not change anymore.
664 : 47600 : while (true) {
665 : 27620 : TestBitSet new_reachable = reachable;
666 [ + - + + ]: 323261 : for (auto j : new_reachable) {
667 : 268021 : new_reachable |= depgraph.Ancestors(j) & todo;
668 : 268021 : new_reachable |= depgraph.Descendants(j) & todo;
669 : : }
670 [ + + ]: 27620 : if (new_reachable == reachable) break;
671 : 19980 : reachable = new_reachable;
672 : 19980 : }
673 : : // Verify that the result is the entire component.
674 [ - + ]: 7640 : assert(component == reachable);
675 : : }
676 : :
677 : : // Construct an arbitrary subset of todo.
678 : 1407 : uint64_t subset_bits{0};
679 : 1407 : try {
680 [ + + ]: 1407 : reader >> VARINT(subset_bits);
681 [ - + ]: 992 : } catch (const std::ios_base::failure&) {}
682 : 1407 : TestBitSet subset;
683 [ + - + + ]: 34077 : for (DepGraphIndex i : depgraph.Positions()) {
684 [ + + ]: 31263 : if (todo[i]) {
685 [ + + ]: 16358 : if (subset_bits & 1) subset.Set(i);
686 : 16358 : subset_bits >>= 1;
687 : : }
688 : : }
689 : : // Which must be non-empty.
690 [ + + ]: 1407 : if (subset.None()) subset = TestBitSet::Singleton(todo.First());
691 : : // Remove it from todo.
692 : 1407 : todo -= subset;
693 : : }
694 : :
695 : : // No components can be found in an empty subset.
696 [ - + ]: 133 : assert(depgraph.FindConnectedComponent(todo).None());
697 : 133 : }
698 : :
699 [ + - ]: 693 : FUZZ_TARGET(clusterlin_make_connected)
700 : : {
701 : : // Verify that MakeConnected makes graphs connected.
702 : :
703 : 243 : SpanReader reader(buffer);
704 : 243 : DepGraph<TestBitSet> depgraph;
705 : 243 : try {
706 [ + - ]: 243 : reader >> Using<DepGraphFormatter>(depgraph);
707 [ - - ]: 0 : } catch (const std::ios_base::failure&) {}
708 [ + - ]: 243 : MakeConnected(depgraph);
709 [ + - ]: 243 : SanityCheck(depgraph);
710 [ - + ]: 243 : assert(depgraph.IsConnected());
711 : 243 : }
712 : :
713 [ + - ]: 582 : FUZZ_TARGET(clusterlin_chunking)
714 : : {
715 : : // Verify the correctness of the ChunkLinearization function.
716 : :
717 : : // Construct a graph by deserializing.
718 : 132 : SpanReader reader(buffer);
719 : 132 : DepGraph<TestBitSet> depgraph;
720 : 132 : try {
721 [ + - ]: 132 : reader >> Using<DepGraphFormatter>(depgraph);
722 [ - - ]: 0 : } catch (const std::ios_base::failure&) {}
723 : :
724 : : // Read a valid linearization for depgraph.
725 [ + - ]: 132 : auto linearization = ReadLinearization(depgraph, reader);
726 : :
727 : : // Invoke the chunking functions.
728 [ - + ]: 132 : auto chunking = ChunkLinearization(depgraph, linearization);
729 [ - + ]: 132 : auto chunking_info = ChunkLinearizationInfo(depgraph, linearization);
730 : :
731 : : // Verify consistency between the two functions.
732 [ - + - + : 132 : assert(chunking.size() == chunking_info.size());
- + ]
733 [ - + + + ]: 591 : for (size_t i = 0; i < chunking.size(); ++i) {
734 [ + - ]: 459 : assert(chunking[i] == chunking_info[i].feerate);
735 [ + - ]: 918 : assert(SetInfo(depgraph, chunking_info[i].transactions) == chunking_info[i]);
736 : : }
737 : :
738 : : // Verify that chunk feerates are monotonically non-increasing.
739 [ + + ]: 467 : for (size_t i = 1; i < chunking.size(); ++i) {
740 [ - + ]: 335 : assert(!(chunking[i] >> chunking[i - 1]));
741 : : }
742 : :
743 : : // Naively recompute the chunks (each is the highest-feerate prefix of what remains).
744 : 132 : auto todo = depgraph.Positions();
745 [ + + ]: 591 : for (const auto& [chunk_set, chunk_feerate] : chunking_info) {
746 [ - + ]: 459 : assert(todo.Any());
747 : 459 : SetInfo<TestBitSet> accumulator, best;
748 [ + + ]: 9167 : for (DepGraphIndex idx : linearization) {
749 [ + + ]: 8708 : if (todo[idx]) {
750 : 4719 : accumulator.Set(depgraph, idx);
751 [ + + + + ]: 4719 : if (best.feerate.IsEmpty() || accumulator.feerate >> best.feerate) {
752 : 1081 : best = accumulator;
753 : : }
754 : : }
755 : : }
756 [ + - ]: 459 : assert(chunk_feerate == best.feerate);
757 [ - + ]: 459 : assert(chunk_set == best.transactions);
758 [ - + ]: 459 : assert(best.transactions.IsSubsetOf(todo));
759 : 459 : todo -= best.transactions;
760 : : }
761 [ - + ]: 132 : assert(todo.None());
762 : 132 : }
763 : :
764 : : static constexpr auto MAX_SIMPLE_ITERATIONS = 300000;
765 : :
766 [ + - ]: 670 : FUZZ_TARGET(clusterlin_simple_finder)
767 : : {
768 : : // Verify that SimpleCandidateFinder works as expected by sanity checking the results
769 : : // and comparing them (if claimed to be optimal) against the sets found by
770 : : // ExhaustiveCandidateFinder.
771 : : //
772 : : // Note that SimpleCandidateFinder is only used in tests; the purpose of this fuzz test is to
773 : : // establish confidence in SimpleCandidateFinder, so that it can be used in SimpleLinearize,
774 : : // which is then used to test Linearize below.
775 : :
776 : : // Retrieve a depgraph from the fuzz input.
777 : 220 : SpanReader reader(buffer);
778 : 220 : DepGraph<TestBitSet> depgraph;
779 : 220 : try {
780 [ + - ]: 220 : reader >> Using<DepGraphFormatter>(depgraph);
781 [ - - ]: 0 : } catch (const std::ios_base::failure&) {}
782 : :
783 : : // Instantiate the SimpleCandidateFinder to be tested, and the ExhaustiveCandidateFinder it is
784 : : // being tested against.
785 : 220 : SimpleCandidateFinder smp_finder(depgraph);
786 : 220 : ExhaustiveCandidateFinder exh_finder(depgraph);
787 : :
788 : 220 : auto todo = depgraph.Positions();
789 [ + + ]: 2250 : while (todo.Any()) {
790 [ - + ]: 2030 : assert(!smp_finder.AllDone());
791 [ - + ]: 2030 : assert(!exh_finder.AllDone());
792 : :
793 : : // Call SimpleCandidateFinder.
794 [ - + ]: 2030 : auto [found, iterations_done] = smp_finder.FindCandidateSet(MAX_SIMPLE_ITERATIONS);
795 : 2030 : bool optimal = (iterations_done != MAX_SIMPLE_ITERATIONS);
796 : :
797 : : // Sanity check the result.
798 [ - + ]: 2030 : assert(iterations_done <= MAX_SIMPLE_ITERATIONS);
799 [ - + ]: 2030 : assert(found.transactions.Any());
800 [ - + ]: 2030 : assert(found.transactions.IsSubsetOf(todo));
801 [ + - ]: 2030 : assert(depgraph.FeeRate(found.transactions) == found.feerate);
802 : : // Check that it is topologically valid.
803 [ + - + + ]: 8042 : for (auto i : found.transactions) {
804 [ - + ]: 3982 : assert(found.transactions.IsSupersetOf(depgraph.Ancestors(i) & todo));
805 : : }
806 : :
807 : : // At most 2^(N-1) iterations can be required: the number of non-empty connected subsets a
808 : : // graph with N transactions can have. If MAX_SIMPLE_ITERATIONS exceeds this number, the
809 : : // result is necessarily optimal.
810 [ - + ]: 2030 : assert(iterations_done <= (uint64_t{1} << (todo.Count() - 1)));
811 [ + + - + ]: 2030 : if (MAX_SIMPLE_ITERATIONS > (uint64_t{1} << (todo.Count() - 1))) assert(optimal);
812 : :
813 : : // SimpleCandidateFinder only finds connected sets.
814 [ - + ]: 2030 : assert(depgraph.IsConnected(found.transactions));
815 : :
816 : : // Perform further quality checks only if SimpleCandidateFinder claims an optimal result.
817 [ + + ]: 2030 : if (optimal) {
818 [ + + ]: 1989 : if (todo.Count() <= 12) {
819 : : // Compare with ExhaustiveCandidateFinder. This quickly gets computationally
820 : : // expensive for large clusters (O(2^n)), so only do it for sufficiently small ones.
821 : 1262 : auto exhaustive = exh_finder.FindCandidateSet();
822 [ + - ]: 1262 : assert(exhaustive.feerate == found.feerate);
823 : : }
824 : :
825 : : // Compare with a non-empty topological set read from the fuzz input (comparing with an
826 : : // empty set is not interesting).
827 [ + - ]: 1989 : auto read_topo = ReadTopologicalSet(depgraph, todo, reader, /*non_empty=*/true);
828 [ - + ]: 1989 : assert(found.feerate >= depgraph.FeeRate(read_topo));
829 : : }
830 : :
831 : : // Find a non-empty topologically valid subset of transactions to remove from the graph.
832 : : // Using an empty set would mean the next iteration is identical to the current one, and
833 : : // could cause an infinite loop.
834 [ + - ]: 2030 : auto del_set = ReadTopologicalSet(depgraph, todo, reader, /*non_empty=*/true);
835 : 2030 : todo -= del_set;
836 : 2030 : smp_finder.MarkDone(del_set);
837 : 2030 : exh_finder.MarkDone(del_set);
838 : : }
839 : :
840 [ - + ]: 220 : assert(smp_finder.AllDone());
841 [ - + ]: 220 : assert(exh_finder.AllDone());
842 : 220 : }
843 : :
844 [ + - ]: 792 : FUZZ_TARGET(clusterlin_simple_linearize)
845 : : {
846 : : // Verify the behavior of SimpleLinearize(). Note that SimpleLinearize is only used in tests;
847 : : // the purpose of this fuzz test is to establish confidence in SimpleLinearize, so that it can
848 : : // be used to test the real Linearize function in the fuzz test below.
849 : :
850 : : // Retrieve an iteration count and a depgraph from the fuzz input.
851 : 342 : SpanReader reader(buffer);
852 : 342 : uint64_t iter_count{0};
853 : 342 : DepGraph<TestBitSet> depgraph;
854 : 342 : try {
855 [ + + + - ]: 342 : reader >> VARINT(iter_count) >> Using<DepGraphFormatter>(depgraph);
856 [ - + ]: 5 : } catch (const std::ios_base::failure&) {}
857 : 342 : iter_count %= MAX_SIMPLE_ITERATIONS;
858 : :
859 : : // Invoke SimpleLinearize().
860 [ - + ]: 342 : auto [linearization, optimal] = SimpleLinearize(depgraph, iter_count);
861 [ - + ]: 342 : SanityCheck(depgraph, linearization);
862 [ - + ]: 342 : auto simple_chunking = ChunkLinearization(depgraph, linearization);
863 : :
864 : : // If the iteration count is sufficiently high, an optimal linearization must be found.
865 : : // SimpleLinearize on k transactions can take up to 2^(k-1) iterations (one per non-empty
866 : : // connected topologically valid subset), which sums over k=1..n to (2^n)-1.
867 [ + - ]: 342 : const uint64_t n = depgraph.TxCount();
868 [ + - + + ]: 342 : if (n <= 63 && (iter_count >> n)) {
869 [ - + ]: 80 : assert(optimal);
870 : : }
871 : :
872 : : // If SimpleLinearize claims optimal result, and the cluster is sufficiently small (there are
873 : : // n! linearizations), test that the result is as good as every valid linearization.
874 [ + + + + ]: 342 : if (optimal && depgraph.TxCount() <= 8) {
875 [ + - ]: 145 : auto exh_linearization = ExhaustiveLinearize(depgraph);
876 [ - + ]: 145 : auto exh_chunking = ChunkLinearization(depgraph, exh_linearization);
877 [ - + - + : 145 : auto cmp = CompareChunks(simple_chunking, exh_chunking);
+ - ]
878 [ - + ]: 145 : assert(cmp == 0);
879 [ - + - + : 145 : assert(simple_chunking.size() == exh_chunking.size());
- + ]
880 : 145 : }
881 : :
882 [ + + ]: 342 : if (optimal) {
883 : : // Compare with a linearization read from the fuzz input.
884 [ + - ]: 261 : auto read = ReadLinearization(depgraph, reader);
885 [ - + ]: 261 : auto read_chunking = ChunkLinearization(depgraph, read);
886 [ - + - + : 261 : auto cmp = CompareChunks(simple_chunking, read_chunking);
+ - ]
887 [ - + ]: 261 : assert(cmp >= 0);
888 : 261 : }
889 : 342 : }
890 : :
891 [ + - ]: 1049 : FUZZ_TARGET(clusterlin_sfl)
892 : : {
893 : : // Verify the individual steps of the SFL algorithm.
894 : :
895 : 599 : SpanReader reader(buffer);
896 : 599 : DepGraph<TestBitSet> depgraph;
897 : 599 : uint8_t flags{1};
898 : 599 : uint64_t rng_seed{0};
899 : 599 : try {
900 [ + + + + : 599 : reader >> rng_seed >> flags >> Using<DepGraphFormatter>(depgraph);
+ - ]
901 [ - + ]: 2 : } catch (const std::ios_base::failure&) {}
902 [ + + ]: 599 : if (depgraph.TxCount() <= 1) return;
903 : 588 : InsecureRandomContext rng(rng_seed);
904 : : /** Whether to make the depgraph connected. */
905 : 588 : const bool make_connected = flags & 1;
906 : : /** Whether to load some input linearization into the state. */
907 : 588 : const bool load_linearization = flags & 2;
908 : : /** Whether that input linearization is topological. */
909 [ + + + + ]: 588 : const bool load_topological = load_linearization && (flags & 4);
910 : :
911 : : // Initialize SFL state.
912 [ + + + - ]: 588 : if (make_connected) MakeConnected(depgraph);
913 : 588 : SpanningForestState sfl(depgraph, rng.rand64());
914 : :
915 : : // Function to test the state.
916 : 588 : std::vector<FeeFrac> last_diagram;
917 : 588 : bool was_optimal{false};
918 : 25736 : auto test_fn = [&](bool is_optimal = false, bool is_minimal = false) {
919 [ + + ]: 25148 : if (rng.randbits(4) == 0) {
920 : : // Perform sanity checks from time to time (too computationally expensive to do after
921 : : // every step).
922 : 2540 : sfl.SanityCheck(depgraph);
923 : : }
924 : 25148 : auto diagram = sfl.GetDiagram();
925 [ + + ]: 25148 : if (rng.randbits(4) == 0) {
926 : : // Verify that the diagram of GetLinearization() is at least as good as GetDiagram(),
927 : : // from time to time.
928 : 2222 : auto lin = sfl.GetLinearization();
929 [ - + ]: 2222 : auto lin_diagram = ChunkLinearization(depgraph, lin);
930 [ - + - + : 2222 : auto cmp_lin = CompareChunks(lin_diagram, diagram);
+ - ]
931 [ - + ]: 2222 : assert(cmp_lin >= 0);
932 : : // If we're in an allegedly optimal state, they must match.
933 [ + + - + ]: 2222 : if (is_optimal) assert(cmp_lin == 0);
934 : : // If we're in an allegedly minimal state, they must also have the same number of
935 : : // segments.
936 [ + + - + : 2222 : if (is_minimal) assert(diagram.size() == lin_diagram.size());
- + - + ]
937 : 2222 : }
938 : : // Verify that subsequent calls to GetDiagram() never get worse/incomparable.
939 [ + + ]: 25148 : if (!last_diagram.empty()) {
940 [ - + - + : 24670 : auto cmp = CompareChunks(diagram, last_diagram);
+ - ]
941 [ - + ]: 24670 : assert(cmp >= 0);
942 : : // If the last diagram was already optimal, the new one cannot be better.
943 [ + + - + ]: 24670 : if (was_optimal) assert(cmp == 0);
944 : : // Also, if the diagram was already optimal, the number of segments can only increase.
945 [ + + - + : 24670 : if (was_optimal) assert(diagram.size() >= last_diagram.size());
- + - + ]
946 : : }
947 : 25148 : last_diagram = std::move(diagram);
948 : 25148 : was_optimal = is_optimal;
949 : 25736 : };
950 : :
951 [ + + ]: 588 : if (load_linearization) {
952 [ + - ]: 211 : auto input_lin = ReadLinearization(depgraph, reader, load_topological);
953 [ - + ]: 211 : sfl.LoadLinearization(input_lin);
954 [ + + ]: 211 : if (load_topological) {
955 : : // The diagram of the loaded linearization forms an initial lower bound on future
956 : : // diagrams.
957 [ - + ]: 110 : last_diagram = ChunkLinearization(depgraph, input_lin);
958 : : } else {
959 : : // The input linearization may have been non-topological, so invoke MakeTopological to
960 : : // fix it still.
961 : 101 : sfl.MakeTopological();
962 : : }
963 : 211 : } else {
964 : : // Invoke MakeTopological to create an initial from-scratch topological state.
965 : 377 : sfl.MakeTopological();
966 : : }
967 : :
968 : : // Loop until optimal.
969 [ + - ]: 588 : test_fn();
970 : 588 : sfl.StartOptimizing();
971 : 13627 : while (true) {
972 [ + - ]: 13627 : test_fn();
973 [ + + ]: 13627 : if (!sfl.OptimizeStep()) break;
974 : : }
975 : :
976 : : // Loop until minimal.
977 [ + - ]: 588 : test_fn(/*is_optimal=*/true);
978 : 588 : sfl.StartMinimizing();
979 : 9757 : while (true) {
980 [ + - ]: 9757 : test_fn(/*is_optimal=*/true);
981 [ + + ]: 9757 : if (!sfl.MinimizeStep()) break;
982 : : }
983 [ + - ]: 588 : test_fn(/*is_optimal=*/true, /*is_minimal=*/true);
984 : :
985 : : // Verify that optimality is reached within an expected amount of work. This protects against
986 : : // hypothetical bugs that hugely increase the amount of work needed to reach optimality.
987 [ - + ]: 588 : assert(sfl.GetCost() <= MaxOptimalLinearizationIters(depgraph.TxCount()));
988 : :
989 : : // The result must be as good as SimpleLinearize.
990 [ - + ]: 588 : auto [simple_linearization, simple_optimal] = SimpleLinearize(depgraph, MAX_SIMPLE_ITERATIONS / 10);
991 [ - + ]: 588 : auto simple_diagram = ChunkLinearization(depgraph, simple_linearization);
992 [ - + - + : 588 : auto simple_cmp = CompareChunks(last_diagram, simple_diagram);
+ - ]
993 [ - + ]: 588 : assert(simple_cmp >= 0);
994 [ + + - + ]: 588 : if (simple_optimal) assert(simple_cmp == 0);
995 : : // If the diagram matches, we must also have at least as many segments (because the SFL state
996 : : // and its produced diagram are minimal);
997 [ + + - + : 588 : if (simple_cmp == 0) assert(last_diagram.size() >= simple_diagram.size());
- + + - ]
998 : :
999 : : // We can compare with any arbitrary linearization, and the diagram must be at least as good as
1000 : : // each.
1001 [ + + ]: 6468 : for (int i = 0; i < 10; ++i) {
1002 [ + - ]: 5880 : auto read_lin = ReadLinearization(depgraph, reader);
1003 [ - + ]: 5880 : auto read_diagram = ChunkLinearization(depgraph, read_lin);
1004 [ - + - + : 5880 : auto cmp = CompareChunks(last_diagram, read_diagram);
+ - ]
1005 [ - + ]: 5880 : assert(cmp >= 0);
1006 [ + + - + : 5880 : if (cmp == 0) assert(last_diagram.size() >= read_diagram.size());
- + - + ]
1007 : 5880 : }
1008 : 599 : }
1009 : :
1010 [ + - ]: 1164 : FUZZ_TARGET(clusterlin_linearize)
1011 : : {
1012 : : // Verify the behavior of Linearize().
1013 : :
1014 : : // Retrieve an RNG seed, an iteration count, a depgraph, and whether to make it connected from
1015 : : // the fuzz input.
1016 : 714 : SpanReader reader(buffer);
1017 : 714 : DepGraph<TestBitSet> depgraph;
1018 : 714 : uint64_t rng_seed{0};
1019 : 714 : uint64_t iter_count{0};
1020 : 714 : uint8_t flags{7};
1021 : 714 : try {
1022 [ + + + - : 714 : reader >> VARINT(iter_count) >> Using<DepGraphFormatter>(depgraph) >> rng_seed >> flags;
+ + + + ]
1023 [ - + ]: 551 : } catch (const std::ios_base::failure&) {}
1024 : 714 : bool make_connected = flags & 1;
1025 : : // The following 3 booleans have 4 combinations:
1026 : : // - (flags & 6) == 0: do not provide input linearization.
1027 : : // - (flags & 6) == 2: provide potentially non-topological input.
1028 : : // - (flags & 6) == 4: provide topological input linearization, but do not claim it is
1029 : : // topological.
1030 : : // - (flags & 6) == 6: provide topological input linearization, and claim it is topological.
1031 : 714 : bool provide_input = flags & 6;
1032 : 714 : bool provide_topological_input = flags & 4;
1033 : 714 : bool claim_topological_input = (flags & 6) == 6;
1034 : : // The most complicated graphs are connected ones (other ones just split up). Optionally force
1035 : : // the graph to be connected.
1036 [ + + + - ]: 714 : if (make_connected) MakeConnected(depgraph);
1037 : :
1038 : : // Optionally construct an old linearization for it.
1039 : 714 : std::vector<DepGraphIndex> old_linearization;
1040 [ + + ]: 714 : if (provide_input) {
1041 [ + - ]: 1294 : old_linearization = ReadLinearization(depgraph, reader, /*topological=*/provide_topological_input);
1042 [ + + - + ]: 647 : if (provide_topological_input) SanityCheck(depgraph, old_linearization);
1043 : : }
1044 : :
1045 : : // Invoke Linearize().
1046 : 714 : iter_count &= 0x7ffff;
1047 [ - + - + ]: 714 : auto [linearization, optimal, cost] = Linearize(depgraph, iter_count, rng_seed, old_linearization, /*is_topological=*/claim_topological_input);
1048 [ - + ]: 714 : SanityCheck(depgraph, linearization);
1049 [ - + ]: 714 : auto chunking = ChunkLinearization(depgraph, linearization);
1050 : :
1051 : : // Linearization must always be as good as the old one, if provided and topological (even when
1052 : : // not claimed to be topological).
1053 [ + + ]: 714 : if (provide_topological_input) {
1054 [ - + ]: 618 : auto old_chunking = ChunkLinearization(depgraph, old_linearization);
1055 [ - + - + : 618 : auto cmp = CompareChunks(chunking, old_chunking);
+ - ]
1056 [ - + ]: 618 : assert(cmp >= 0);
1057 : 618 : }
1058 : :
1059 : : // If the iteration count is sufficiently high, an optimal linearization must be found.
1060 [ + + ]: 714 : if (iter_count > MaxOptimalLinearizationIters(depgraph.TxCount())) {
1061 [ - + ]: 172 : assert(optimal);
1062 : : }
1063 : :
1064 : : // If Linearize claims optimal result, run quality tests.
1065 [ + + ]: 714 : if (optimal) {
1066 : : // It must be as good as SimpleLinearize.
1067 [ - + ]: 406 : auto [simple_linearization, simple_optimal] = SimpleLinearize(depgraph, MAX_SIMPLE_ITERATIONS);
1068 [ - + ]: 406 : SanityCheck(depgraph, simple_linearization);
1069 [ - + ]: 406 : auto simple_chunking = ChunkLinearization(depgraph, simple_linearization);
1070 [ - + - + : 406 : auto cmp = CompareChunks(chunking, simple_chunking);
+ - ]
1071 [ - + ]: 406 : assert(cmp >= 0);
1072 : : // If SimpleLinearize finds the optimal result too, they must be equal (if not,
1073 : : // SimpleLinearize is broken).
1074 [ + + - + ]: 406 : if (simple_optimal) assert(cmp == 0);
1075 : :
1076 : : // If simple_chunking is diagram-optimal, it cannot have more chunks than chunking (as
1077 : : // chunking is claimed to be optimal, which implies minimal chunks).
1078 [ + + - + : 406 : if (cmp == 0) assert(chunking.size() >= simple_chunking.size());
- + - + ]
1079 : :
1080 : : // Compare with a linearization read from the fuzz input.
1081 [ + - ]: 406 : auto read = ReadLinearization(depgraph, reader);
1082 [ - + ]: 406 : auto read_chunking = ChunkLinearization(depgraph, read);
1083 [ - + - + : 406 : auto cmp_read = CompareChunks(chunking, read_chunking);
+ - ]
1084 [ - + ]: 406 : assert(cmp_read >= 0);
1085 : 406 : }
1086 : 714 : }
1087 : :
1088 [ + - ]: 602 : FUZZ_TARGET(clusterlin_postlinearize)
1089 : : {
1090 : : // Verify expected properties of PostLinearize() on arbitrary linearizations.
1091 : :
1092 : : // Retrieve a depgraph from the fuzz input.
1093 : 152 : SpanReader reader(buffer);
1094 : 152 : DepGraph<TestBitSet> depgraph;
1095 : 152 : try {
1096 [ + - ]: 152 : reader >> Using<DepGraphFormatter>(depgraph);
1097 [ - - ]: 0 : } catch (const std::ios_base::failure&) {}
1098 : :
1099 : : // Retrieve a linearization from the fuzz input.
1100 : 152 : std::vector<DepGraphIndex> linearization;
1101 [ + - ]: 304 : linearization = ReadLinearization(depgraph, reader);
1102 [ - + ]: 152 : SanityCheck(depgraph, linearization);
1103 : :
1104 : : // Produce a post-processed version.
1105 [ + - ]: 152 : auto post_linearization = linearization;
1106 [ - + + - ]: 152 : PostLinearize(depgraph, post_linearization);
1107 [ - + ]: 152 : SanityCheck(depgraph, post_linearization);
1108 : :
1109 : : // Compare diagrams: post-linearization cannot worsen anywhere.
1110 [ - + ]: 152 : auto chunking = ChunkLinearization(depgraph, linearization);
1111 [ - + ]: 152 : auto post_chunking = ChunkLinearization(depgraph, post_linearization);
1112 [ - + - + : 152 : auto cmp = CompareChunks(post_chunking, chunking);
+ - ]
1113 [ - + ]: 152 : assert(cmp >= 0);
1114 : :
1115 : : // Run again, things can keep improving (and never get worse)
1116 [ + - ]: 152 : auto post_post_linearization = post_linearization;
1117 [ - + + - ]: 152 : PostLinearize(depgraph, post_post_linearization);
1118 [ - + ]: 152 : SanityCheck(depgraph, post_post_linearization);
1119 [ - + ]: 152 : auto post_post_chunking = ChunkLinearization(depgraph, post_post_linearization);
1120 [ - + - + : 152 : cmp = CompareChunks(post_post_chunking, post_chunking);
+ - ]
1121 [ - + ]: 152 : assert(cmp >= 0);
1122 : :
1123 : : // The chunks that come out of postlinearizing are always connected.
1124 [ - + ]: 152 : auto linchunking = ChunkLinearizationInfo(depgraph, post_linearization);
1125 [ + + ]: 1292 : for (const auto& [chunk_set, _chunk_feerate] : linchunking) {
1126 [ - + ]: 1140 : assert(depgraph.IsConnected(chunk_set));
1127 : : }
1128 : 152 : }
1129 : :
1130 [ + - ]: 1027 : FUZZ_TARGET(clusterlin_postlinearize_tree)
1131 : : {
1132 : : // Verify expected properties of PostLinearize() on linearizations of graphs that form either
1133 : : // an upright or reverse tree structure.
1134 : :
1135 : : // Construct a direction, RNG seed, and an arbitrary graph from the fuzz input.
1136 : 577 : SpanReader reader(buffer);
1137 : 577 : uint64_t rng_seed{0};
1138 : 577 : DepGraph<TestBitSet> depgraph_gen;
1139 : 577 : uint8_t direction{0};
1140 : 577 : try {
1141 [ + - + + : 577 : reader >> direction >> rng_seed >> Using<DepGraphFormatter>(depgraph_gen);
+ - ]
1142 [ - + ]: 3 : } catch (const std::ios_base::failure&) {}
1143 : :
1144 : 577 : auto depgraph_tree = BuildTreeGraph(depgraph_gen, direction);
1145 : :
1146 : : // Retrieve a linearization from the fuzz input.
1147 : 577 : std::vector<DepGraphIndex> linearization;
1148 [ + - ]: 1154 : linearization = ReadLinearization(depgraph_tree, reader);
1149 [ - + ]: 577 : SanityCheck(depgraph_tree, linearization);
1150 : :
1151 : : // Produce a postlinearized version.
1152 [ + - ]: 577 : auto post_linearization = linearization;
1153 [ - + + - ]: 577 : PostLinearize(depgraph_tree, post_linearization);
1154 [ - + ]: 577 : SanityCheck(depgraph_tree, post_linearization);
1155 : :
1156 : : // Compare diagrams.
1157 [ - + ]: 577 : auto chunking = ChunkLinearization(depgraph_tree, linearization);
1158 [ - + ]: 577 : auto post_chunking = ChunkLinearization(depgraph_tree, post_linearization);
1159 [ - + - + : 577 : auto cmp = CompareChunks(post_chunking, chunking);
+ - ]
1160 [ - + ]: 577 : assert(cmp >= 0);
1161 : :
1162 : : // Verify that post-linearizing again does not change the diagram. The result must be identical
1163 : : // as post_linearization ought to be optimal already with a tree-structured graph.
1164 [ + - ]: 577 : auto post_post_linearization = post_linearization;
1165 [ - + + - ]: 577 : PostLinearize(depgraph_tree, post_post_linearization);
1166 [ - + ]: 577 : SanityCheck(depgraph_tree, post_post_linearization);
1167 [ - + ]: 577 : auto post_post_chunking = ChunkLinearization(depgraph_tree, post_post_linearization);
1168 [ - + - + : 577 : auto cmp_post = CompareChunks(post_post_chunking, post_chunking);
+ - ]
1169 [ - + ]: 577 : assert(cmp_post == 0);
1170 : :
1171 : : // Try to find an even better linearization directly. This must not change the diagram for the
1172 : : // same reason.
1173 [ - + - + ]: 577 : auto [opt_linearization, _optimal, _cost] = Linearize(depgraph_tree, 100000, rng_seed, post_linearization);
1174 [ - + ]: 577 : auto opt_chunking = ChunkLinearization(depgraph_tree, opt_linearization);
1175 [ - + - + : 577 : auto cmp_opt = CompareChunks(opt_chunking, post_chunking);
+ - ]
1176 [ - + ]: 577 : assert(cmp_opt == 0);
1177 : 577 : }
1178 : :
1179 [ + - ]: 609 : FUZZ_TARGET(clusterlin_postlinearize_moved_leaf)
1180 : : {
1181 : : // Verify that taking an existing linearization, and moving a leaf to the back, potentially
1182 : : // increasing its fee, and then post-linearizing, results in something as good as the
1183 : : // original. This guarantees that in an RBF that replaces a transaction with one of the same
1184 : : // size but higher fee, applying the "remove conflicts, append new transaction, postlinearize"
1185 : : // process will never worsen linearization quality.
1186 : :
1187 : : // Construct an arbitrary graph and a fee from the fuzz input.
1188 : 159 : SpanReader reader(buffer);
1189 : 159 : DepGraph<TestBitSet> depgraph;
1190 : 159 : int32_t fee_inc{0};
1191 : 159 : try {
1192 : 159 : uint64_t fee_inc_code;
1193 [ + - + + ]: 159 : reader >> Using<DepGraphFormatter>(depgraph) >> VARINT(fee_inc_code);
1194 : 53 : fee_inc = fee_inc_code & 0x3ffff;
1195 [ - + ]: 106 : } catch (const std::ios_base::failure&) {}
1196 [ + + ]: 159 : if (depgraph.TxCount() == 0) return;
1197 : :
1198 : : // Retrieve two linearizations from the fuzz input.
1199 [ + - ]: 146 : auto lin = ReadLinearization(depgraph, reader);
1200 [ + - ]: 146 : auto lin_leaf = ReadLinearization(depgraph, reader);
1201 : :
1202 : : // Construct a linearization identical to lin, but with the tail end of lin_leaf moved to the
1203 : : // back.
1204 : 146 : std::vector<DepGraphIndex> lin_moved;
1205 [ + + ]: 1786 : for (auto i : lin) {
1206 [ + + + - ]: 1640 : if (i != lin_leaf.back()) lin_moved.push_back(i);
1207 : : }
1208 [ + - ]: 146 : lin_moved.push_back(lin_leaf.back());
1209 : :
1210 : : // Postlinearize lin_moved.
1211 [ - + + - ]: 146 : PostLinearize(depgraph, lin_moved);
1212 [ - + ]: 146 : SanityCheck(depgraph, lin_moved);
1213 : :
1214 : : // Compare diagrams (applying the fee delta after computing the old one).
1215 [ - + ]: 146 : auto old_chunking = ChunkLinearization(depgraph, lin);
1216 [ - + ]: 146 : depgraph.FeeRate(lin_leaf.back()).fee += fee_inc;
1217 [ - + ]: 146 : auto new_chunking = ChunkLinearization(depgraph, lin_moved);
1218 [ - + - + : 146 : auto cmp = CompareChunks(new_chunking, old_chunking);
+ - ]
1219 [ - + ]: 146 : assert(cmp >= 0);
1220 : 159 : }
|