Coverage Report

Created: 2026-06-18 19:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/bitcoin/src/test/fuzz/txgraph.cpp
Line
Count
Source
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 <test/fuzz/FuzzedDataProvider.h>
7
#include <test/fuzz/fuzz.h>
8
#include <test/util/cluster_linearize.h>
9
#include <test/util/random.h>
10
#include <txgraph.h>
11
#include <util/bitset.h>
12
#include <util/feefrac.h>
13
14
#include <algorithm>
15
#include <cstdint>
16
#include <iterator>
17
#include <map>
18
#include <memory>
19
#include <ranges>
20
#include <set>
21
#include <utility>
22
23
using namespace cluster_linearize;
24
25
namespace {
26
27
struct SimTxObject : public TxGraph::Ref
28
{
29
    // Use random uint64_t as txids for this simulation (0 = empty object).
30
    const uint64_t m_txid{0};
31
0
    SimTxObject() noexcept = default;
32
0
    explicit SimTxObject(uint64_t txid) noexcept : m_txid(txid) {}
33
};
34
35
/** Data type representing a naive simulated TxGraph, keeping all transactions (even from
36
 *  disconnected components) in a single DepGraph. Unlike the real TxGraph, this only models
37
 *  a single graph, and multiple instances are used to simulate main/staging. */
38
struct SimTxGraph
39
{
40
    /** Maximum number of transactions to support simultaneously. Set this higher than txgraph's
41
     *  cluster count, so we can exercise situations with more transactions than fit in one
42
     *  cluster. */
43
    static constexpr unsigned MAX_TRANSACTIONS = MAX_CLUSTER_COUNT_LIMIT * 2;
44
    /** Set type to use in the simulation. */
45
    using SetType = BitSet<MAX_TRANSACTIONS>;
46
    /** Data type for representing positions within SimTxGraph::graph. */
47
    using Pos = DepGraphIndex;
48
    /** Constant to mean "missing in this graph". */
49
    static constexpr auto MISSING = Pos(-1);
50
51
    /** The dependency graph (for all transactions in the simulation, regardless of
52
     *  connectivity/clustering). */
53
    DepGraph<SetType> graph;
54
    /** For each position in graph, which SimTxObject it corresponds with (if any). Use shared_ptr
55
     *  so that a SimTxGraph can be copied to create a staging one, while sharing Refs with
56
     *  the main graph. */
57
    std::array<std::shared_ptr<SimTxObject>, MAX_TRANSACTIONS> simmap;
58
    /** For each TxGraph::Ref in graph, the position it corresponds with. */
59
    std::map<const TxGraph::Ref*, Pos> simrevmap;
60
    /** The set of SimTxObject entries that have been removed, but not yet destroyed. */
61
    std::vector<std::shared_ptr<SimTxObject>> removed;
62
    /** Whether the graph is oversized (true = yes, false = no, std::nullopt = unknown). */
63
    std::optional<bool> oversized;
64
    /** The configured maximum number of transactions per cluster. */
65
    DepGraphIndex max_cluster_count;
66
    /** Which transactions have been modified in the graph since creation, either directly or by
67
     *  being in a cluster which includes modifications. Only relevant for the staging graph. */
68
    SetType modified;
69
    /** The configured maximum total size of transactions per cluster. */
70
    uint64_t max_cluster_size;
71
    /** Whether the corresponding real graph is known to be optimally linearized. */
72
    bool real_is_optimal{false};
73
74
    /** Construct a new SimTxGraph with the specified maximum cluster count and size. */
75
    explicit SimTxGraph(DepGraphIndex cluster_count, uint64_t cluster_size) :
76
0
        max_cluster_count(cluster_count), max_cluster_size(cluster_size) {}
77
78
    // Permit copying and moving.
79
0
    SimTxGraph(const SimTxGraph&) noexcept = default;
80
    SimTxGraph& operator=(const SimTxGraph&) noexcept = default;
81
0
    SimTxGraph(SimTxGraph&&) noexcept = default;
82
0
    SimTxGraph& operator=(SimTxGraph&&) noexcept = default;
83
84
    /** Get the connected components within this simulated transaction graph. */
85
    std::vector<SetType> GetComponents()
86
0
    {
87
0
        auto todo = graph.Positions();
88
0
        std::vector<SetType> ret;
89
        // Iterate over all connected components of the graph.
90
0
        while (todo.Any()) {
  Branch (90:16): [True: 0, False: 0]
91
0
            auto component = graph.FindConnectedComponent(todo);
92
0
            ret.push_back(component);
93
0
            todo -= component;
94
0
        }
95
0
        return ret;
96
0
    }
97
98
    /** Check whether this graph is oversized (contains a connected component whose number of
99
     *  transactions exceeds max_cluster_count. */
100
    bool IsOversized()
101
0
    {
102
0
        if (!oversized.has_value()) {
  Branch (102:13): [True: 0, False: 0]
103
            // Only recompute when oversized isn't already known.
104
0
            oversized = false;
105
0
            for (auto component : GetComponents()) {
  Branch (105:33): [True: 0, False: 0]
106
0
                if (component.Count() > max_cluster_count) oversized = true;
  Branch (106:21): [True: 0, False: 0]
107
0
                uint64_t component_size{0};
108
0
                for (auto i : component) component_size += graph.FeeRate(i).size;
  Branch (108:29): [True: 0, False: 0]
109
0
                if (component_size > max_cluster_size) oversized = true;
  Branch (109:21): [True: 0, False: 0]
110
0
            }
111
0
        }
112
0
        return *oversized;
113
0
    }
114
115
    void MakeModified(DepGraphIndex index)
116
0
    {
117
0
        modified |= graph.GetConnectedComponent(graph.Positions(), index);
118
0
    }
119
120
    /** Determine the number of (non-removed) transactions in the graph. */
121
0
    DepGraphIndex GetTransactionCount() const { return graph.TxCount(); }
122
123
    /** Get the sum of all fees/sizes in the graph. */
124
    FeePerWeight SumAll() const
125
0
    {
126
0
        FeePerWeight ret;
127
0
        for (auto i : graph.Positions()) {
  Branch (127:21): [True: 0, False: 0]
128
0
            ret += graph.FeeRate(i);
129
0
        }
130
0
        return ret;
131
0
    }
132
133
    /** Get the position where ref occurs in this simulated graph, or -1 if it does not. */
134
    Pos Find(const TxGraph::Ref* ref) const
135
0
    {
136
0
        auto it = simrevmap.find(ref);
137
0
        if (it != simrevmap.end()) return it->second;
  Branch (137:13): [True: 0, False: 0]
138
0
        return MISSING;
139
0
    }
140
141
    /** Given a position in this simulated graph, get the corresponding SimTxObject. */
142
    SimTxObject* GetRef(Pos pos)
143
0
    {
144
0
        assert(graph.Positions()[pos]);
  Branch (144:9): [True: 0, False: 0]
145
0
        assert(simmap[pos]);
  Branch (145:9): [True: 0, False: 0]
146
0
        return simmap[pos].get();
147
0
    }
148
149
    /** Add a new transaction to the simulation and the specified real graph. */
150
    void AddTransaction(TxGraph& txgraph, const FeePerWeight& feerate, uint64_t txid)
151
0
    {
152
0
        assert(graph.TxCount() < MAX_TRANSACTIONS);
  Branch (152:9): [True: 0, False: 0]
153
0
        auto simpos = graph.AddTransaction(feerate);
154
0
        real_is_optimal = false;
155
0
        MakeModified(simpos);
156
0
        assert(graph.Positions()[simpos]);
  Branch (156:9): [True: 0, False: 0]
157
0
        simmap[simpos] = std::make_shared<SimTxObject>(txid);
158
0
        txgraph.AddTransaction(*simmap[simpos], feerate);
159
0
        auto ptr = simmap[simpos].get();
160
0
        simrevmap[ptr] = simpos;
161
        // This may invalidate our cached oversized value.
162
0
        if (oversized.has_value() && !*oversized) oversized = std::nullopt;
  Branch (162:13): [True: 0, False: 0]
  Branch (162:38): [True: 0, False: 0]
163
0
    }
164
165
    /** Add a dependency between two positions in this graph. */
166
    void AddDependency(TxGraph::Ref* parent, TxGraph::Ref* child)
167
0
    {
168
0
        auto par_pos = Find(parent);
169
0
        if (par_pos == MISSING) return;
  Branch (169:13): [True: 0, False: 0]
170
0
        auto chl_pos = Find(child);
171
0
        if (chl_pos == MISSING) return;
  Branch (171:13): [True: 0, False: 0]
172
0
        graph.AddDependencies(SetType::Singleton(par_pos), chl_pos);
173
0
        MakeModified(par_pos);
174
0
        real_is_optimal = false;
175
        // This may invalidate our cached oversized value.
176
0
        if (oversized.has_value() && !*oversized) oversized = std::nullopt;
  Branch (176:13): [True: 0, False: 0]
  Branch (176:38): [True: 0, False: 0]
177
0
    }
178
179
    /** Modify the transaction fee of a ref, if it exists. */
180
    void SetTransactionFee(TxGraph::Ref* ref, int64_t fee)
181
0
    {
182
0
        auto pos = Find(ref);
183
0
        if (pos == MISSING) return;
  Branch (183:13): [True: 0, False: 0]
184
        // No need to invoke MakeModified, because this equally affects main and staging.
185
0
        real_is_optimal = false;
186
0
        graph.FeeRate(pos).fee = fee;
187
0
    }
188
189
    /** Remove the transaction in the specified position from the graph. */
190
    void RemoveTransaction(TxGraph::Ref* ref)
191
0
    {
192
0
        auto pos = Find(ref);
193
0
        if (pos == MISSING) return;
  Branch (193:13): [True: 0, False: 0]
194
0
        MakeModified(pos);
195
0
        real_is_optimal = false;
196
0
        graph.RemoveTransactions(SetType::Singleton(pos));
197
0
        simrevmap.erase(simmap[pos].get());
198
        // Retain the TxGraph::Ref corresponding to this position, so the Ref destruction isn't
199
        // invoked until the simulation explicitly decided to do so.
200
0
        removed.push_back(std::move(simmap[pos]));
201
0
        simmap[pos].reset();
202
        // This may invalidate our cached oversized value.
203
0
        if (oversized.has_value() && *oversized) oversized = std::nullopt;
  Branch (203:13): [True: 0, False: 0]
  Branch (203:38): [True: 0, False: 0]
204
0
    }
205
206
    /** Destroy the transaction from the graph, including from the removed set. This will
207
     *  trigger TxGraph::Ref::~Ref. reset_oversize controls whether the cached oversized
208
     *  value is cleared (destroying does not clear oversizedness in TxGraph of the main
209
     *  graph while staging exists). */
210
    void DestroyTransaction(TxGraph::Ref* ref, bool reset_oversize)
211
0
    {
212
0
        auto pos = Find(ref);
213
0
        if (pos == MISSING) {
  Branch (213:13): [True: 0, False: 0]
214
            // Wipe the ref, if it exists, from the removed vector. Use std::partition rather
215
            // than std::erase because we don't care about the order of the entries that
216
            // remain.
217
0
            auto remove = std::partition(removed.begin(), removed.end(), [&](auto& arg) { return arg.get() != ref; });
218
0
            removed.erase(remove, removed.end());
219
0
        } else {
220
0
            MakeModified(pos);
221
0
            graph.RemoveTransactions(SetType::Singleton(pos));
222
0
            real_is_optimal = false;
223
0
            simrevmap.erase(simmap[pos].get());
224
0
            simmap[pos].reset();
225
            // This may invalidate our cached oversized value.
226
0
            if (reset_oversize && oversized.has_value() && *oversized) {
  Branch (226:17): [True: 0, False: 0]
  Branch (226:35): [True: 0, False: 0]
  Branch (226:60): [True: 0, False: 0]
227
0
                oversized = std::nullopt;
228
0
            }
229
0
        }
230
0
    }
231
232
    /** Construct the set with all positions in this graph corresponding to the specified
233
     *  TxGraph::Refs. All of them must occur in this graph and not be removed. */
234
    SetType MakeSet(std::span<TxGraph::Ref* const> arg)
235
0
    {
236
0
        SetType ret;
237
0
        for (TxGraph::Ref* ptr : arg) {
  Branch (237:32): [True: 0, False: 0]
238
0
            auto pos = Find(ptr);
239
0
            assert(pos != Pos(-1));
  Branch (239:13): [True: 0, False: 0]
240
0
            ret.Set(pos);
241
0
        }
242
0
        return ret;
243
0
    }
244
245
    /** Get the set of ancestors (desc=false) or descendants (desc=true) in this graph. */
246
    SetType GetAncDesc(TxGraph::Ref* arg, bool desc)
247
0
    {
248
0
        auto pos = Find(arg);
249
0
        if (pos == MISSING) return {};
  Branch (249:13): [True: 0, False: 0]
250
0
        return desc ? graph.Descendants(pos) : graph.Ancestors(pos);
  Branch (250:16): [True: 0, False: 0]
251
0
    }
252
253
    /** Given a set of Refs (given as a vector of pointers), expand the set to include all its
254
     *  ancestors (desc=false) or all its descendants (desc=true) in this graph. */
255
    void IncludeAncDesc(std::vector<TxGraph::Ref*>& arg, bool desc)
256
0
    {
257
0
        std::vector<TxGraph::Ref*> ret;
258
0
        for (auto ptr : arg) {
  Branch (258:23): [True: 0, False: 0]
259
0
            auto simpos = Find(ptr);
260
0
            if (simpos != MISSING) {
  Branch (260:17): [True: 0, False: 0]
261
0
                for (auto i : desc ? graph.Descendants(simpos) : graph.Ancestors(simpos)) {
  Branch (261:29): [True: 0, False: 0]
  Branch (261:31): [True: 0, False: 0]
262
0
                    ret.push_back(simmap[i].get());
263
0
                }
264
0
            } else {
265
0
                ret.push_back(ptr);
266
0
            }
267
0
        }
268
        // Construct deduplicated version in input (do not use std::sort/std::unique for
269
        // deduplication as it'd rely on non-deterministic pointer comparison).
270
0
        arg.clear();
271
0
        for (auto ptr : ret) {
  Branch (271:23): [True: 0, False: 0]
272
0
            if (std::find(arg.begin(), arg.end(), ptr) == arg.end()) {
  Branch (272:17): [True: 0, False: 0]
273
0
                arg.push_back(ptr);
274
0
            }
275
0
        }
276
0
    }
277
278
279
    /** Verify that set contains transactions from every oversized cluster, and nothing from
280
     *  non-oversized ones. */
281
    bool MatchesOversizedClusters(const SetType& set)
282
0
    {
283
0
        if (set.Any() && !IsOversized()) return false;
  Branch (283:13): [True: 0, False: 0]
  Branch (283:26): [True: 0, False: 0]
284
285
0
        auto todo = graph.Positions();
286
0
        if (!set.IsSubsetOf(todo)) return false;
  Branch (286:13): [True: 0, False: 0]
287
288
        // Walk all clusters, and make sure all of set doesn't come from non-oversized clusters
289
0
        while (todo.Any()) {
  Branch (289:16): [True: 0, False: 0]
290
0
            auto component = graph.FindConnectedComponent(todo);
291
            // Determine whether component is oversized, due to either the size or count limit.
292
0
            bool is_oversized = component.Count() > max_cluster_count;
293
0
            uint64_t component_size{0};
294
0
            for (auto i : component) component_size += graph.FeeRate(i).size;
  Branch (294:25): [True: 0, False: 0]
295
0
            is_oversized |= component_size > max_cluster_size;
296
            // Check whether overlap with set matches is_oversized.
297
0
            if (is_oversized != set.Overlaps(component)) return false;
  Branch (297:17): [True: 0, False: 0]
298
0
            todo -= component;
299
0
        }
300
0
        return true;
301
0
    }
302
};
303
304
} // namespace
305
306
FUZZ_TARGET(txgraph)
307
0
{
308
    // This is a big simulation test for TxGraph, which performs a fuzz-derived sequence of valid
309
    // operations on a TxGraph instance, as well as on a simpler (mostly) reimplementation (see
310
    // SimTxGraph above), comparing the outcome of functions that return a result, and finally
311
    // performing a full comparison between the two.
312
313
0
    SeedRandomStateForTest(SeedRand::ZEROS);
314
0
    FuzzedDataProvider provider(buffer.data(), buffer.size());
315
316
    /** Internal test RNG, used only for decisions which would require significant amount of data
317
     *  to be read from the provider, without realistically impacting test sensitivity, and for
318
     *  specialized test cases that are hard to perform more generically. */
319
0
    InsecureRandomContext rng(provider.ConsumeIntegral<uint64_t>());
320
321
    /** Variable used whenever an empty SimTxObject is needed. */
322
0
    SimTxObject empty_ref;
323
324
    /** The maximum number of transactions per (non-oversized) cluster we will use in this
325
     *  simulation. */
326
0
    auto max_cluster_count = provider.ConsumeIntegralInRange<DepGraphIndex>(1, MAX_CLUSTER_COUNT_LIMIT);
327
    /** The maximum total size of transactions in a (non-oversized) cluster. */
328
0
    auto max_cluster_size = provider.ConsumeIntegralInRange<uint64_t>(1, 0x3fffff * MAX_CLUSTER_COUNT_LIMIT);
329
    /** The amount of work to consider a cluster acceptably linearized. */
330
0
    auto acceptable_cost = provider.ConsumeIntegralInRange<uint64_t>(0, 10000);
331
332
    /** The set of uint64_t "txid"s that have been assigned before. */
333
0
    std::set<uint64_t> assigned_txids;
334
335
    // Construct a real graph, and a vector of simulated graphs (main, and possibly staging).
336
0
    auto fallback_order = [&](const TxGraph::Ref& a, const TxGraph::Ref& b) noexcept {
337
0
        uint64_t txid_a = static_cast<const SimTxObject&>(a).m_txid;
338
0
        uint64_t txid_b = static_cast<const SimTxObject&>(b).m_txid;
339
0
        assert(assigned_txids.contains(txid_a));
  Branch (339:9): [True: 0, False: 0]
340
0
        assert(assigned_txids.contains(txid_b));
  Branch (340:9): [True: 0, False: 0]
341
0
        return txid_a <=> txid_b;
342
0
    };
343
0
    auto real = MakeTxGraph(
344
0
        /*max_cluster_count=*/max_cluster_count,
345
0
        /*max_cluster_size=*/max_cluster_size,
346
0
        /*acceptable_cost=*/acceptable_cost,
347
0
        /*fallback_order=*/fallback_order);
348
349
0
    std::vector<SimTxGraph> sims;
350
0
    sims.reserve(2);
351
0
    sims.emplace_back(max_cluster_count, max_cluster_size);
352
353
    /** Struct encapsulating information about a BlockBuilder that's currently live. */
354
0
    struct BlockBuilderData
355
0
    {
356
        /** BlockBuilder object from real. */
357
0
        std::unique_ptr<TxGraph::BlockBuilder> builder;
358
        /** The set of transactions marked as included in *builder. */
359
0
        SimTxGraph::SetType included;
360
        /** The set of transactions marked as included or skipped in *builder. */
361
0
        SimTxGraph::SetType done;
362
        /** The last chunk feerate returned by *builder. IsEmpty() if none yet. */
363
0
        FeePerWeight last_feerate;
364
365
0
        BlockBuilderData(std::unique_ptr<TxGraph::BlockBuilder> builder_in) : builder(std::move(builder_in)) {}
366
0
    };
367
368
    /** Currently active block builders. */
369
0
    std::vector<BlockBuilderData> block_builders;
370
371
    /** Function to pick any SimTxObject (for either sim in sims: from sim.simmap or sim.removed, or the
372
     *  empty one). */
373
0
    auto pick_fn = [&]() noexcept -> SimTxObject* {
374
0
        size_t tx_count[2] = {sims[0].GetTransactionCount(), 0};
375
        /** The number of possible choices. */
376
0
        size_t choices = tx_count[0] + sims[0].removed.size() + 1;
377
0
        if (sims.size() == 2) {
  Branch (377:13): [True: 0, False: 0]
378
0
            tx_count[1] = sims[1].GetTransactionCount();
379
0
            choices += tx_count[1] + sims[1].removed.size();
380
0
        }
381
        /** Pick one of them. */
382
0
        auto choice = provider.ConsumeIntegralInRange<size_t>(0, choices - 1);
383
        // Consider both main and (if it exists) staging.
384
0
        for (size_t level = 0; level < sims.size(); ++level) {
  Branch (384:32): [True: 0, False: 0]
385
0
            auto& sim = sims[level];
386
0
            if (choice < tx_count[level]) {
  Branch (386:17): [True: 0, False: 0]
387
                // Return from graph.
388
0
                for (auto i : sim.graph.Positions()) {
  Branch (388:29): [True: 0, False: 0]
389
0
                    if (choice == 0) return sim.GetRef(i);
  Branch (389:25): [True: 0, False: 0]
390
0
                    --choice;
391
0
                }
392
0
                assert(false);
  Branch (392:17): [Folded - Ignored]
393
0
            } else {
394
0
                choice -= tx_count[level];
395
0
            }
396
0
            if (choice < sim.removed.size()) {
  Branch (396:17): [True: 0, False: 0]
397
                // Return from removed.
398
0
                return sim.removed[choice].get();
399
0
            } else {
400
0
                choice -= sim.removed.size();
401
0
            }
402
0
        }
403
        // Return empty.
404
0
        assert(choice == 0);
  Branch (404:9): [True: 0, False: 0]
405
0
        return &empty_ref;
406
0
    };
407
408
    /** Function to construct the correct fee-size diagram a real graph has based on its graph
409
     *  order (as reported by GetCluster(), so it works for both main and staging). */
410
0
    auto get_diagram_fn = [&](TxGraph::Level level_select) -> std::vector<FeeFrac> {
411
0
        int level = level_select == TxGraph::Level::MAIN ? 0 : sims.size() - 1;
  Branch (411:21): [True: 0, False: 0]
412
0
        auto& sim = sims[level];
413
        // For every transaction in the graph, request its cluster, and throw them into a set.
414
0
        std::set<std::vector<TxGraph::Ref*>> clusters;
415
0
        for (auto i : sim.graph.Positions()) {
  Branch (415:21): [True: 0, False: 0]
416
0
            auto ref = sim.GetRef(i);
417
0
            clusters.insert(real->GetCluster(*ref, level_select));
418
0
        }
419
        // Compute the chunkings of each (deduplicated) cluster.
420
0
        size_t num_tx{0};
421
0
        std::vector<FeeFrac> chunk_feerates;
422
0
        for (const auto& cluster : clusters) {
  Branch (422:34): [True: 0, False: 0]
423
0
            num_tx += cluster.size();
424
0
            std::vector<SimTxGraph::Pos> linearization;
425
0
            linearization.reserve(cluster.size());
426
0
            for (auto refptr : cluster) linearization.push_back(sim.Find(refptr));
  Branch (426:30): [True: 0, False: 0]
427
0
            for (const FeeFrac& chunk_feerate : ChunkLinearization(sim.graph, linearization)) {
  Branch (427:47): [True: 0, False: 0]
428
0
                chunk_feerates.push_back(chunk_feerate);
429
0
            }
430
0
        }
431
        // Verify the number of transactions after deduplicating clusters. This implicitly verifies
432
        // that GetCluster on each element of a cluster reports the cluster transactions in the same
433
        // order.
434
0
        assert(num_tx == sim.GetTransactionCount());
  Branch (434:9): [True: 0, False: 0]
435
        // Sort by feerate only, since violating topological constraints within same-feerate
436
        // chunks won't affect diagram comparisons.
437
0
        std::ranges::sort(chunk_feerates, std::greater<ByRatioNegSize<FeeFrac>>{});
438
0
        return chunk_feerates;
439
0
    };
440
441
0
    LIMITED_WHILE(provider.remaining_bytes() > 0, 200) {
442
        // Read a one-byte command.
443
0
        int command = provider.ConsumeIntegral<uint8_t>();
444
0
        int orig_command = command;
445
446
        // Treat the lowest bit of a command as a flag (which selects a variant of some of the
447
        // operations), and the second-lowest bit as a way of selecting main vs. staging, and leave
448
        // the rest of the bits in command.
449
0
        bool alt = command & 1;
450
0
        TxGraph::Level level_select = (command & 2) ? TxGraph::Level::MAIN : TxGraph::Level::TOP;
  Branch (450:39): [True: 0, False: 0]
451
0
        command >>= 2;
452
453
        /** Use the bottom 2 bits of command to select an entry in the block_builders vector (if
454
         *  any). These use the same bits as alt/level_select, so don't use those in actions below
455
         *  where builder_idx is used as well. */
456
0
        int builder_idx = block_builders.empty() ? -1 : int((orig_command & 3) % block_builders.size());
  Branch (456:27): [True: 0, False: 0]
457
458
        // Provide convenient aliases for the top simulated graph (main, or staging if it exists),
459
        // one for the simulated graph selected based on level_select (for operations that can operate
460
        // on both graphs), and one that always refers to the main graph.
461
0
        auto& top_sim = sims.back();
462
0
        auto& sel_sim = level_select == TxGraph::Level::MAIN ? sims[0] : top_sim;
  Branch (462:25): [True: 0, False: 0]
463
0
        auto& main_sim = sims[0];
464
465
        // Keep decrementing command for each applicable operation, until one is hit. Multiple
466
        // iterations may be necessary.
467
0
        while (true) {
  Branch (467:16): [Folded - Ignored]
468
0
            if ((block_builders.empty() || sims.size() > 1) && top_sim.GetTransactionCount() < SimTxGraph::MAX_TRANSACTIONS && command-- == 0) {
  Branch (468:18): [True: 0, False: 0]
  Branch (468:44): [True: 0, False: 0]
  Branch (468:64): [True: 0, False: 0]
  Branch (468:128): [True: 0, False: 0]
469
                // AddTransaction.
470
0
                int64_t fee;
471
0
                int32_t size;
472
0
                if (alt) {
  Branch (472:21): [True: 0, False: 0]
473
                    // If alt is true, pick fee and size from the entire range.
474
0
                    fee = provider.ConsumeIntegralInRange<int64_t>(-0x8000000000000, 0x7ffffffffffff);
475
0
                    size = provider.ConsumeIntegralInRange<int32_t>(1, 0x3fffff);
476
0
                } else {
477
                    // Otherwise, use smaller range which consume fewer fuzz input bytes, as just
478
                    // these are likely sufficient to trigger all interesting code paths already.
479
0
                    fee = provider.ConsumeIntegral<uint8_t>();
480
0
                    size = provider.ConsumeIntegralInRange<uint32_t>(1, 0xff);
481
0
                }
482
0
                FeePerWeight feerate{fee, size};
483
                // Pick a novel txid (and not 0, which is reserved for empty_ref).
484
0
                uint64_t txid;
485
0
                do {
486
0
                    txid = rng.rand64();
487
0
                } while (txid == 0 || assigned_txids.contains(txid));
  Branch (487:26): [True: 0, False: 0]
  Branch (487:39): [True: 0, False: 0]
488
0
                assigned_txids.insert(txid);
489
                // Create the transaction in the simulation and the real graph.
490
0
                top_sim.AddTransaction(*real, feerate, txid);
491
0
                break;
492
0
            } else if ((block_builders.empty() || sims.size() > 1) && top_sim.GetTransactionCount() + top_sim.removed.size() > 1 && command-- == 0) {
  Branch (492:25): [True: 0, False: 0]
  Branch (492:51): [True: 0, False: 0]
  Branch (492:71): [True: 0, False: 0]
  Branch (492:133): [True: 0, False: 0]
493
                // AddDependency.
494
0
                auto par = pick_fn();
495
0
                auto chl = pick_fn();
496
0
                auto pos_par = top_sim.Find(par);
497
0
                auto pos_chl = top_sim.Find(chl);
498
0
                if (pos_par != SimTxGraph::MISSING && pos_chl != SimTxGraph::MISSING) {
  Branch (498:21): [True: 0, False: 0]
  Branch (498:55): [True: 0, False: 0]
499
                    // Determine if adding this would introduce a cycle (not allowed by TxGraph),
500
                    // and if so, skip.
501
0
                    if (top_sim.graph.Ancestors(pos_par)[pos_chl]) break;
  Branch (501:25): [True: 0, False: 0]
502
0
                }
503
0
                top_sim.AddDependency(par, chl);
504
0
                top_sim.real_is_optimal = false;
505
0
                real->AddDependency(*par, *chl);
506
0
                break;
507
0
            } else if ((block_builders.empty() || sims.size() > 1) && top_sim.removed.size() < 100 && command-- == 0) {
  Branch (507:25): [True: 0, False: 0]
  Branch (507:51): [True: 0, False: 0]
  Branch (507:71): [True: 0, False: 0]
  Branch (507:103): [True: 0, False: 0]
508
                // RemoveTransaction. Either all its ancestors or all its descendants are also
509
                // removed (if any), to make sure TxGraph's reordering of removals and dependencies
510
                // has no effect.
511
0
                std::vector<TxGraph::Ref*> to_remove;
512
0
                to_remove.push_back(pick_fn());
513
0
                top_sim.IncludeAncDesc(to_remove, alt);
514
                // The order in which these ancestors/descendants are removed should not matter;
515
                // randomly shuffle them.
516
0
                std::shuffle(to_remove.begin(), to_remove.end(), rng);
517
0
                for (TxGraph::Ref* ptr : to_remove) {
  Branch (517:40): [True: 0, False: 0]
518
0
                    real->RemoveTransaction(*ptr);
519
0
                    top_sim.RemoveTransaction(ptr);
520
0
                }
521
0
                break;
522
0
            } else if (sel_sim.removed.size() > 0 && command-- == 0) {
  Branch (522:24): [True: 0, False: 0]
  Branch (522:54): [True: 0, False: 0]
523
                // ~Ref (of an already-removed transaction). Destroying a TxGraph::Ref has an
524
                // observable effect on the TxGraph it refers to, so this simulation permits doing
525
                // so separately from other actions on TxGraph.
526
527
                // Pick a Ref of sel_sim.removed to destroy. Note that the same Ref may still occur
528
                // in the other graph, and thus not actually trigger ~Ref yet (which is exactly
529
                // what we want, as destroying Refs is only allowed when it does not refer to an
530
                // existing transaction in either graph).
531
0
                auto removed_pos = provider.ConsumeIntegralInRange<size_t>(0, sel_sim.removed.size() - 1);
532
0
                if (removed_pos != sel_sim.removed.size() - 1) {
  Branch (532:21): [True: 0, False: 0]
533
0
                    std::swap(sel_sim.removed[removed_pos], sel_sim.removed.back());
534
0
                }
535
0
                sel_sim.removed.pop_back();
536
0
                break;
537
0
            } else if (block_builders.empty() && command-- == 0) {
  Branch (537:24): [True: 0, False: 0]
  Branch (537:50): [True: 0, False: 0]
538
                // ~Ref (of any transaction).
539
0
                std::vector<TxGraph::Ref*> to_destroy;
540
0
                to_destroy.push_back(pick_fn());
541
0
                while (true) {
  Branch (541:24): [Folded - Ignored]
542
                    // Keep adding either the ancestors or descendants the already picked
543
                    // transactions have in both graphs (main and staging) combined. Destroying
544
                    // will trigger deletions in both, so to have consistent TxGraph behavior, the
545
                    // set must be closed under ancestors, or descendants, in both graphs.
546
0
                    auto old_size = to_destroy.size();
547
0
                    for (auto& sim : sims) sim.IncludeAncDesc(to_destroy, alt);
  Branch (547:36): [True: 0, False: 0]
548
0
                    if (to_destroy.size() == old_size) break;
  Branch (548:25): [True: 0, False: 0]
549
0
                }
550
                // The order in which these ancestors/descendants are destroyed should not matter;
551
                // randomly shuffle them.
552
0
                std::shuffle(to_destroy.begin(), to_destroy.end(), rng);
553
0
                for (TxGraph::Ref* ptr : to_destroy) {
  Branch (553:40): [True: 0, False: 0]
554
0
                    for (size_t level = 0; level < sims.size(); ++level) {
  Branch (554:44): [True: 0, False: 0]
555
0
                        sims[level].DestroyTransaction(ptr, level == sims.size() - 1);
556
0
                    }
557
0
                }
558
0
                break;
559
0
            } else if (block_builders.empty() && command-- == 0) {
  Branch (559:24): [True: 0, False: 0]
  Branch (559:50): [True: 0, False: 0]
560
                // SetTransactionFee.
561
0
                int64_t fee;
562
0
                if (alt) {
  Branch (562:21): [True: 0, False: 0]
563
0
                    fee = provider.ConsumeIntegralInRange<int64_t>(-0x8000000000000, 0x7ffffffffffff);
564
0
                } else {
565
0
                    fee = provider.ConsumeIntegral<uint8_t>();
566
0
                }
567
0
                auto ref = pick_fn();
568
0
                real->SetTransactionFee(*ref, fee);
569
0
                for (auto& sim : sims) {
  Branch (569:32): [True: 0, False: 0]
570
0
                    sim.SetTransactionFee(ref, fee);
571
0
                }
572
0
                break;
573
0
            } else if (command-- == 0) {
  Branch (573:24): [True: 0, False: 0]
574
                // GetTransactionCount.
575
0
                assert(real->GetTransactionCount(level_select) == sel_sim.GetTransactionCount());
  Branch (575:17): [True: 0, False: 0]
576
0
                break;
577
0
            } else if (command-- == 0) {
  Branch (577:24): [True: 0, False: 0]
578
                // Exists.
579
0
                auto ref = pick_fn();
580
0
                bool exists = real->Exists(*ref, level_select);
581
0
                bool should_exist = sel_sim.Find(ref) != SimTxGraph::MISSING;
582
0
                assert(exists == should_exist);
  Branch (582:17): [True: 0, False: 0]
583
0
                break;
584
0
            } else if (command-- == 0) {
  Branch (584:24): [True: 0, False: 0]
585
                // IsOversized.
586
0
                assert(sel_sim.IsOversized() == real->IsOversized(level_select));
  Branch (586:17): [True: 0, False: 0]
587
0
                break;
588
0
            } else if (command-- == 0) {
  Branch (588:24): [True: 0, False: 0]
589
                // GetIndividualFeerate.
590
0
                auto ref = pick_fn();
591
0
                auto feerate = real->GetIndividualFeerate(*ref);
592
0
                bool found{false};
593
0
                for (auto& sim : sims) {
  Branch (593:32): [True: 0, False: 0]
594
0
                    auto simpos = sim.Find(ref);
595
0
                    if (simpos != SimTxGraph::MISSING) {
  Branch (595:25): [True: 0, False: 0]
596
0
                        found = true;
597
0
                        assert(feerate == sim.graph.FeeRate(simpos));
  Branch (597:25): [True: 0, False: 0]
598
0
                    }
599
0
                }
600
0
                if (!found) assert(feerate.IsEmpty());
  Branch (600:21): [True: 0, False: 0]
  Branch (600:29): [True: 0, False: 0]
601
0
                break;
602
0
            } else if (!main_sim.IsOversized() && command-- == 0) {
  Branch (602:24): [True: 0, False: 0]
  Branch (602:51): [True: 0, False: 0]
603
                // GetMainChunkFeerate.
604
0
                auto ref = pick_fn();
605
0
                auto feerate = real->GetMainChunkFeerate(*ref);
606
0
                auto simpos = main_sim.Find(ref);
607
0
                if (simpos == SimTxGraph::MISSING) {
  Branch (607:21): [True: 0, False: 0]
608
0
                    assert(feerate.IsEmpty());
  Branch (608:21): [True: 0, False: 0]
609
0
                } else {
610
                    // Just do some quick checks that the reported value is in range. A full
611
                    // recomputation of expected chunk feerates is done at the end.
612
0
                    assert(feerate.size >= main_sim.graph.FeeRate(simpos).size);
  Branch (612:21): [True: 0, False: 0]
613
0
                    assert(feerate.size <= main_sim.SumAll().size);
  Branch (613:21): [True: 0, False: 0]
614
0
                }
615
0
                break;
616
0
            } else if (!sel_sim.IsOversized() && command-- == 0) {
  Branch (616:24): [True: 0, False: 0]
  Branch (616:50): [True: 0, False: 0]
617
                // GetAncestors/GetDescendants.
618
0
                auto ref = pick_fn();
619
0
                auto result = alt ? real->GetDescendants(*ref, level_select)
  Branch (619:31): [True: 0, False: 0]
620
0
                                  : real->GetAncestors(*ref, level_select);
621
0
                assert(result.size() <= max_cluster_count);
  Branch (621:17): [True: 0, False: 0]
622
0
                auto result_set = sel_sim.MakeSet(result);
623
0
                assert(result.size() == result_set.Count());
  Branch (623:17): [True: 0, False: 0]
624
0
                auto expect_set = sel_sim.GetAncDesc(ref, alt);
625
0
                assert(result_set == expect_set);
  Branch (625:17): [True: 0, False: 0]
626
0
                break;
627
0
            } else if (!sel_sim.IsOversized() && command-- == 0) {
  Branch (627:24): [True: 0, False: 0]
  Branch (627:50): [True: 0, False: 0]
628
                // GetAncestorsUnion/GetDescendantsUnion.
629
0
                std::vector<TxGraph::Ref*> refs;
630
                // Gather a list of up to 15 Ref pointers.
631
0
                auto count = provider.ConsumeIntegralInRange<size_t>(0, 15);
632
0
                refs.resize(count);
633
0
                for (size_t i = 0; i < count; ++i) {
  Branch (633:36): [True: 0, False: 0]
634
0
                    refs[i] = pick_fn();
635
0
                }
636
                // Their order should not matter, shuffle them.
637
0
                std::shuffle(refs.begin(), refs.end(), rng);
638
                // Invoke the real function, and convert to SimPos set.
639
0
                auto result = alt ? real->GetDescendantsUnion(refs, level_select)
  Branch (639:31): [True: 0, False: 0]
640
0
                                  : real->GetAncestorsUnion(refs, level_select);
641
0
                auto result_set = sel_sim.MakeSet(result);
642
0
                assert(result.size() == result_set.Count());
  Branch (642:17): [True: 0, False: 0]
643
                // Compute the expected result.
644
0
                SimTxGraph::SetType expect_set;
645
0
                for (TxGraph::Ref* ref : refs) expect_set |= sel_sim.GetAncDesc(ref, alt);
  Branch (645:40): [True: 0, False: 0]
646
                // Compare.
647
0
                assert(result_set == expect_set);
  Branch (647:17): [True: 0, False: 0]
648
0
                break;
649
0
            } else if (!sel_sim.IsOversized() && command-- == 0) {
  Branch (649:24): [True: 0, False: 0]
  Branch (649:50): [True: 0, False: 0]
650
                // GetCluster.
651
0
                auto ref = pick_fn();
652
0
                auto result = real->GetCluster(*ref, level_select);
653
                // Check cluster count limit.
654
0
                assert(result.size() <= max_cluster_count);
  Branch (654:17): [True: 0, False: 0]
655
                // Require the result to be topologically valid and not contain duplicates.
656
0
                auto left = sel_sim.graph.Positions();
657
0
                uint64_t total_size{0};
658
0
                for (auto refptr : result) {
  Branch (658:34): [True: 0, False: 0]
659
0
                    auto simpos = sel_sim.Find(refptr);
660
0
                    total_size += sel_sim.graph.FeeRate(simpos).size;
661
0
                    assert(simpos != SimTxGraph::MISSING);
  Branch (661:21): [True: 0, False: 0]
662
0
                    assert(left[simpos]);
  Branch (662:21): [True: 0, False: 0]
663
0
                    left.Reset(simpos);
664
0
                    assert(!sel_sim.graph.Ancestors(simpos).Overlaps(left));
  Branch (664:21): [True: 0, False: 0]
665
0
                }
666
                // Check cluster size limit.
667
0
                assert(total_size <= max_cluster_size);
  Branch (667:17): [True: 0, False: 0]
668
                // Require the set to be connected.
669
0
                auto result_set = sel_sim.MakeSet(result);
670
0
                assert(sel_sim.graph.IsConnected(result_set));
  Branch (670:17): [True: 0, False: 0]
671
                // If ref exists, the result must contain it. If not, it must be empty.
672
0
                auto simpos = sel_sim.Find(ref);
673
0
                if (simpos != SimTxGraph::MISSING) {
  Branch (673:21): [True: 0, False: 0]
674
0
                    assert(result_set[simpos]);
  Branch (674:21): [True: 0, False: 0]
675
0
                } else {
676
0
                    assert(result_set.None());
  Branch (676:21): [True: 0, False: 0]
677
0
                }
678
                // Require the set not to have ancestors or descendants outside of it.
679
0
                for (auto i : result_set) {
  Branch (679:29): [True: 0, False: 0]
680
0
                    assert(sel_sim.graph.Ancestors(i).IsSubsetOf(result_set));
  Branch (680:21): [True: 0, False: 0]
681
0
                    assert(sel_sim.graph.Descendants(i).IsSubsetOf(result_set));
  Branch (681:21): [True: 0, False: 0]
682
0
                }
683
0
                break;
684
0
            } else if (command-- == 0) {
  Branch (684:24): [True: 0, False: 0]
685
                // HaveStaging.
686
0
                assert((sims.size() == 2) == real->HaveStaging());
  Branch (686:17): [True: 0, False: 0]
687
0
                break;
688
0
            } else if (sims.size() < 2 && command-- == 0) {
  Branch (688:24): [True: 0, False: 0]
  Branch (688:43): [True: 0, False: 0]
689
                // StartStaging.
690
0
                sims.emplace_back(sims.back());
691
0
                sims.back().modified = SimTxGraph::SetType{};
692
0
                real->StartStaging();
693
0
                break;
694
0
            } else if (block_builders.empty() && sims.size() > 1 && command-- == 0) {
  Branch (694:24): [True: 0, False: 0]
  Branch (694:50): [True: 0, False: 0]
  Branch (694:69): [True: 0, False: 0]
695
                // CommitStaging.
696
0
                real->CommitStaging();
697
                // Resulting main level is only guaranteed to be optimal if all levels are
698
0
                const bool main_optimal = std::all_of(sims.cbegin(), sims.cend(), [](const auto &sim) { return sim.real_is_optimal; });
699
0
                sims.erase(sims.begin());
700
0
                sims.front().real_is_optimal = main_optimal;
701
0
                break;
702
0
            } else if (sims.size() > 1 && command-- == 0) {
  Branch (702:24): [True: 0, False: 0]
  Branch (702:43): [True: 0, False: 0]
703
                // AbortStaging.
704
0
                real->AbortStaging();
705
0
                sims.pop_back();
706
                // Reset the cached oversized value (if TxGraph::Ref destructions triggered
707
                // removals of main transactions while staging was active, then aborting will
708
                // cause it to be re-evaluated in TxGraph).
709
0
                sims.back().oversized = std::nullopt;
710
0
                break;
711
0
            } else if (!main_sim.IsOversized() && command-- == 0) {
  Branch (711:24): [True: 0, False: 0]
  Branch (711:51): [True: 0, False: 0]
712
                // CompareMainOrder.
713
0
                auto ref_a = pick_fn();
714
0
                auto ref_b = pick_fn();
715
0
                auto sim_a = main_sim.Find(ref_a);
716
0
                auto sim_b = main_sim.Find(ref_b);
717
                // Both transactions must exist in the main graph.
718
0
                if (sim_a == SimTxGraph::MISSING || sim_b == SimTxGraph::MISSING) break;
  Branch (718:21): [True: 0, False: 0]
  Branch (718:53): [True: 0, False: 0]
719
0
                auto cmp = real->CompareMainOrder(*ref_a, *ref_b);
720
                // Distinct transactions have distinct places.
721
0
                if (sim_a != sim_b) assert(cmp != 0);
  Branch (721:21): [True: 0, False: 0]
  Branch (721:37): [True: 0, False: 0]
722
                // Ancestors go before descendants.
723
0
                if (main_sim.graph.Ancestors(sim_a)[sim_b]) assert(cmp >= 0);
  Branch (723:21): [True: 0, False: 0]
  Branch (723:61): [True: 0, False: 0]
724
0
                if (main_sim.graph.Descendants(sim_a)[sim_b]) assert(cmp <= 0);
  Branch (724:21): [True: 0, False: 0]
  Branch (724:63): [True: 0, False: 0]
725
                // Do not verify consistency with chunk feerates, as we cannot easily determine
726
                // these here without making more calls to real, which could affect its internal
727
                // state. A full comparison is done at the end.
728
0
                break;
729
0
            } else if (!sel_sim.IsOversized() && command-- == 0) {
  Branch (729:24): [True: 0, False: 0]
  Branch (729:50): [True: 0, False: 0]
730
                // CountDistinctClusters.
731
0
                std::vector<TxGraph::Ref*> refs;
732
                // Gather a list of up to 15 (or up to 255) Ref pointers.
733
0
                auto count = provider.ConsumeIntegralInRange<size_t>(0, alt ? 255 : 15);
  Branch (733:73): [True: 0, False: 0]
734
0
                refs.resize(count);
735
0
                for (size_t i = 0; i < count; ++i) {
  Branch (735:36): [True: 0, False: 0]
736
0
                    refs[i] = pick_fn();
737
0
                }
738
                // Their order should not matter, shuffle them.
739
0
                std::shuffle(refs.begin(), refs.end(), rng);
740
                // Invoke the real function.
741
0
                auto result = real->CountDistinctClusters(refs, level_select);
742
                // Build a set with representatives of the clusters the Refs occur in the
743
                // simulated graph. For each, remember the lowest-index transaction SimPos in the
744
                // cluster.
745
0
                SimTxGraph::SetType sim_reps;
746
0
                for (auto ref : refs) {
  Branch (746:31): [True: 0, False: 0]
747
                    // Skip Refs that do not occur in the simulated graph.
748
0
                    auto simpos = sel_sim.Find(ref);
749
0
                    if (simpos == SimTxGraph::MISSING) continue;
  Branch (749:25): [True: 0, False: 0]
750
                    // Find the component that includes ref.
751
0
                    auto component = sel_sim.graph.GetConnectedComponent(sel_sim.graph.Positions(), simpos);
752
                    // Remember the lowest-index SimPos in component, as a representative for it.
753
0
                    assert(component.Any());
  Branch (753:21): [True: 0, False: 0]
754
0
                    sim_reps.Set(component.First());
755
0
                }
756
                // Compare the number of deduplicated representatives with the value returned by
757
                // the real function.
758
0
                assert(result == sim_reps.Count());
  Branch (758:17): [True: 0, False: 0]
759
0
                break;
760
0
            } else if (command-- == 0) {
  Branch (760:24): [True: 0, False: 0]
761
                // DoWork.
762
0
                uint64_t max_cost = provider.ConsumeIntegralInRange<uint64_t>(0, alt ? 10000 : 255);
  Branch (762:82): [True: 0, False: 0]
763
0
                bool ret = real->DoWork(max_cost);
764
0
                uint64_t cost_for_optimal{0};
765
0
                for (unsigned level = 0; level < sims.size(); ++level) {
  Branch (765:42): [True: 0, False: 0]
766
                    // DoWork() will not optimize oversized levels, or the main level if a builder
767
                    // is present. Note that this impacts the DoWork() return value, as true means
768
                    // that non-optimal clusters may remain within such oversized or builder-having
769
                    // levels.
770
0
                    if (sims[level].IsOversized()) continue;
  Branch (770:25): [True: 0, False: 0]
771
0
                    if (level == 0 && !block_builders.empty()) continue;
  Branch (771:25): [True: 0, False: 0]
  Branch (771:39): [True: 0, False: 0]
772
                    // If neither of the two above conditions holds, and DoWork() returned true,
773
                    // then the level is optimal.
774
0
                    if (ret) {
  Branch (774:25): [True: 0, False: 0]
775
0
                        sims[level].real_is_optimal = true;
776
0
                    }
777
                    // Compute how much work would be needed to make everything optimal.
778
0
                    for (auto component : sims[level].GetComponents()) {
  Branch (778:41): [True: 0, False: 0]
779
0
                        auto cost_opt_this_cluster = MaxOptimalLinearizationCost(component.Count());
780
0
                        if (cost_opt_this_cluster > acceptable_cost) {
  Branch (780:29): [True: 0, False: 0]
781
                            // If the amount of work required to linearize this cluster
782
                            // optimally exceeds acceptable_cost, DoWork() may process it in two
783
                            // stages: once to acceptable, and once to optimal.
784
0
                            cost_for_optimal += cost_opt_this_cluster + acceptable_cost;
785
0
                        } else {
786
0
                            cost_for_optimal += cost_opt_this_cluster;
787
0
                        }
788
0
                    }
789
0
                }
790
0
                if (!ret) {
  Branch (790:21): [True: 0, False: 0]
791
                    // DoWork can only have more work left if the requested amount of work
792
                    // was insufficient to linearize everything optimally within the levels it is
793
                    // allowed to touch.
794
0
                    assert(max_cost <= cost_for_optimal);
  Branch (794:21): [True: 0, False: 0]
795
0
                }
796
0
                break;
797
0
            } else if (sims.size() == 2 && !sims[0].IsOversized() && !sims[1].IsOversized() && command-- == 0) {
  Branch (797:24): [True: 0, False: 0]
  Branch (797:44): [True: 0, False: 0]
  Branch (797:70): [True: 0, False: 0]
  Branch (797:96): [True: 0, False: 0]
798
                // GetMainStagingDiagrams()
799
0
                auto [real_main_diagram, real_staged_diagram] = real->GetMainStagingDiagrams();
800
0
                auto real_sum_main = std::accumulate(real_main_diagram.begin(), real_main_diagram.end(), FeeFrac{});
801
0
                auto real_sum_staged = std::accumulate(real_staged_diagram.begin(), real_staged_diagram.end(), FeeFrac{});
802
0
                auto real_gain = real_sum_staged - real_sum_main;
803
0
                auto sim_gain = sims[1].SumAll() - sims[0].SumAll();
804
                // Just check that the total fee gained/lost and size gained/lost according to the
805
                // diagram matches the difference in these values in the simulated graph. A more
806
                // complete check of the GetMainStagingDiagrams result is performed at the end.
807
0
                assert(sim_gain == real_gain);
  Branch (807:17): [True: 0, False: 0]
808
                // Check that the feerates in each diagram are monotonically decreasing.
809
0
                for (size_t i = 1; i < real_main_diagram.size(); ++i) {
  Branch (809:36): [True: 0, False: 0]
810
0
                    assert(ByRatio{real_main_diagram[i]} <= ByRatio{real_main_diagram[i - 1]});
  Branch (810:21): [True: 0, False: 0]
811
0
                }
812
0
                for (size_t i = 1; i < real_staged_diagram.size(); ++i) {
  Branch (812:36): [True: 0, False: 0]
813
0
                    assert(ByRatio{real_staged_diagram[i]} <= ByRatio{real_staged_diagram[i - 1]});
  Branch (813:21): [True: 0, False: 0]
814
0
                }
815
0
                break;
816
0
            } else if (block_builders.size() < 4 && !main_sim.IsOversized() && command-- == 0) {
  Branch (816:24): [True: 0, False: 0]
  Branch (816:53): [True: 0, False: 0]
  Branch (816:80): [True: 0, False: 0]
817
                // GetBlockBuilder.
818
0
                block_builders.emplace_back(real->GetBlockBuilder());
819
0
                break;
820
0
            } else if (!block_builders.empty() && command-- == 0) {
  Branch (820:24): [True: 0, False: 0]
  Branch (820:51): [True: 0, False: 0]
821
                // ~BlockBuilder.
822
0
                block_builders.erase(block_builders.begin() + builder_idx);
823
0
                break;
824
0
            } else if (!block_builders.empty() && command-- == 0) {
  Branch (824:24): [True: 0, False: 0]
  Branch (824:51): [True: 0, False: 0]
825
                // BlockBuilder::GetCurrentChunk, followed by Include/Skip.
826
0
                auto& builder_data = block_builders[builder_idx];
827
0
                auto new_included = builder_data.included;
828
0
                auto new_done = builder_data.done;
829
0
                auto chunk = builder_data.builder->GetCurrentChunk();
830
0
                if (chunk) {
  Branch (830:21): [True: 0, False: 0]
831
                    // Chunk feerates must be monotonously decreasing.
832
0
                    if (!builder_data.last_feerate.IsEmpty()) {
  Branch (832:25): [True: 0, False: 0]
833
0
                        assert(ByRatio{chunk->second} <= ByRatio{builder_data.last_feerate});
  Branch (833:25): [True: 0, False: 0]
834
0
                    }
835
0
                    builder_data.last_feerate = chunk->second;
836
                    // Verify the contents of GetCurrentChunk.
837
0
                    FeePerWeight sum_feerate;
838
0
                    for (TxGraph::Ref* ref : chunk->first) {
  Branch (838:44): [True: 0, False: 0]
839
                        // Each transaction in the chunk must exist in the main graph.
840
0
                        auto simpos = main_sim.Find(ref);
841
0
                        assert(simpos != SimTxGraph::MISSING);
  Branch (841:25): [True: 0, False: 0]
842
                        // Verify the claimed chunk feerate.
843
0
                        sum_feerate += main_sim.graph.FeeRate(simpos);
844
                        // Make sure no transaction is reported twice.
845
0
                        assert(!new_done[simpos]);
  Branch (845:25): [True: 0, False: 0]
846
0
                        new_done.Set(simpos);
847
                        // The concatenation of all included transactions must be topologically valid.
848
0
                        new_included.Set(simpos);
849
0
                        assert(main_sim.graph.Ancestors(simpos).IsSubsetOf(new_included));
  Branch (849:25): [True: 0, False: 0]
850
0
                    }
851
0
                    assert(sum_feerate == chunk->second);
  Branch (851:21): [True: 0, False: 0]
852
0
                } else {
853
                    // When we reach the end, if nothing was skipped, the entire graph should have
854
                    // been reported.
855
0
                    if (builder_data.done == builder_data.included) {
  Branch (855:25): [True: 0, False: 0]
856
0
                        assert(builder_data.done.Count() == main_sim.GetTransactionCount());
  Branch (856:25): [True: 0, False: 0]
857
0
                    }
858
0
                }
859
                // Possibly invoke GetCurrentChunk() again, which should give the same result.
860
0
                if ((orig_command % 7) >= 5) {
  Branch (860:21): [True: 0, False: 0]
861
0
                    auto chunk2 = builder_data.builder->GetCurrentChunk();
862
0
                    assert(chunk == chunk2);
  Branch (862:21): [True: 0, False: 0]
863
0
                }
864
                // Skip or include.
865
0
                if ((orig_command % 5) >= 3) {
  Branch (865:21): [True: 0, False: 0]
866
                    // Skip.
867
0
                    builder_data.builder->Skip();
868
0
                } else {
869
                    // Include.
870
0
                    builder_data.builder->Include();
871
0
                    builder_data.included = new_included;
872
0
                }
873
0
                builder_data.done = new_done;
874
0
                break;
875
0
            } else if (!main_sim.IsOversized() && command-- == 0) {
  Branch (875:24): [True: 0, False: 0]
  Branch (875:51): [True: 0, False: 0]
876
                // GetWorstMainChunk.
877
0
                auto [worst_chunk, worst_chunk_feerate] = real->GetWorstMainChunk();
878
                // Just do some sanity checks here. Consistency with GetBlockBuilder is checked
879
                // below.
880
0
                if (main_sim.GetTransactionCount() == 0) {
  Branch (880:21): [True: 0, False: 0]
881
0
                    assert(worst_chunk.empty());
  Branch (881:21): [True: 0, False: 0]
882
0
                    assert(worst_chunk_feerate.IsEmpty());
  Branch (882:21): [True: 0, False: 0]
883
0
                } else {
884
0
                    assert(!worst_chunk.empty());
  Branch (884:21): [True: 0, False: 0]
885
0
                    SimTxGraph::SetType done;
886
0
                    FeePerWeight sum;
887
0
                    for (TxGraph::Ref* ref : worst_chunk) {
  Branch (887:44): [True: 0, False: 0]
888
                        // Each transaction in the chunk must exist in the main graph.
889
0
                        auto simpos = main_sim.Find(ref);
890
0
                        assert(simpos != SimTxGraph::MISSING);
  Branch (890:25): [True: 0, False: 0]
891
0
                        sum += main_sim.graph.FeeRate(simpos);
892
                        // Make sure the chunk contains no duplicate transactions.
893
0
                        assert(!done[simpos]);
  Branch (893:25): [True: 0, False: 0]
894
0
                        done.Set(simpos);
895
                        // All elements are preceded by all their descendants.
896
0
                        assert(main_sim.graph.Descendants(simpos).IsSubsetOf(done));
  Branch (896:25): [True: 0, False: 0]
897
0
                    }
898
0
                    assert(sum == worst_chunk_feerate);
  Branch (898:21): [True: 0, False: 0]
899
0
                }
900
0
                break;
901
0
            } else if ((block_builders.empty() || sims.size() > 1) && command-- == 0) {
  Branch (901:25): [True: 0, False: 0]
  Branch (901:51): [True: 0, False: 0]
  Branch (901:71): [True: 0, False: 0]
902
                // Trim.
903
0
                bool was_oversized = top_sim.IsOversized();
904
0
                auto removed = real->Trim();
905
                // Verify that something was removed if and only if there was an oversized cluster.
906
0
                assert(was_oversized == !removed.empty());
  Branch (906:17): [True: 0, False: 0]
907
0
                if (!was_oversized) break;
  Branch (907:21): [True: 0, False: 0]
908
0
                auto removed_set = top_sim.MakeSet(removed);
909
                // The removed set must contain all its own descendants.
910
0
                for (auto simpos : removed_set) {
  Branch (910:34): [True: 0, False: 0]
911
0
                    assert(top_sim.graph.Descendants(simpos).IsSubsetOf(removed_set));
  Branch (911:21): [True: 0, False: 0]
912
0
                }
913
                // Something from every oversized cluster should have been removed, and nothing
914
                // else.
915
0
                assert(top_sim.MatchesOversizedClusters(removed_set));
  Branch (915:17): [True: 0, False: 0]
916
917
                // Apply all removals to the simulation, and verify the result is no longer
918
                // oversized. Don't query the real graph for oversizedness; it is compared
919
                // against the simulation anyway later.
920
0
                for (auto simpos : removed_set) {
  Branch (920:34): [True: 0, False: 0]
921
0
                    top_sim.RemoveTransaction(top_sim.GetRef(simpos));
922
0
                }
923
0
                assert(!top_sim.IsOversized());
  Branch (923:17): [True: 0, False: 0]
924
0
                break;
925
0
            } else if ((block_builders.empty() || sims.size() > 1) &&
  Branch (925:25): [True: 0, False: 0]
  Branch (925:51): [True: 0, False: 0]
926
0
                       top_sim.GetTransactionCount() > max_cluster_count && !top_sim.IsOversized() && command-- == 0) {
  Branch (926:24): [True: 0, False: 0]
  Branch (926:77): [True: 0, False: 0]
  Branch (926:103): [True: 0, False: 0]
927
                // Trim (special case which avoids apparent cycles in the implicit approximate
928
                // dependency graph constructed inside the Trim() implementation). This is worth
929
                // testing separately, because such cycles cannot occur in realistic scenarios,
930
                // but this is hard to replicate in general in this fuzz test.
931
932
                // First, we need to have dependencies applied and linearizations fixed to avoid
933
                // circular dependencies in implied graph; trigger it via whatever means.
934
0
                real->CountDistinctClusters({}, TxGraph::Level::TOP);
935
936
                // Gather the current clusters.
937
0
                auto clusters = top_sim.GetComponents();
938
939
                // Merge clusters randomly until at least one oversized one appears.
940
0
                bool made_oversized = false;
941
0
                auto merges_left = clusters.size() - 1;
942
0
                while (merges_left > 0) {
  Branch (942:24): [True: 0, False: 0]
943
0
                    --merges_left;
944
                    // Find positions of clusters in the clusters vector to merge together.
945
0
                    auto par_cl = rng.randrange(clusters.size());
946
0
                    auto chl_cl = rng.randrange(clusters.size() - 1);
947
0
                    chl_cl += (chl_cl >= par_cl);
948
0
                    Assume(chl_cl != par_cl);
949
                    // Add between 1 and 3 dependencies between them. As all are in the same
950
                    // direction (from the child cluster to parent cluster), no cycles are possible,
951
                    // regardless of what internal topology Trim() uses as approximation within the
952
                    // clusters.
953
0
                    int num_deps = rng.randrange(3) + 1;
954
0
                    for (int i = 0; i < num_deps; ++i) {
  Branch (954:37): [True: 0, False: 0]
955
                        // Find a parent transaction in the parent cluster.
956
0
                        auto par_idx = rng.randrange(clusters[par_cl].Count());
957
0
                        SimTxGraph::Pos par_pos = 0;
958
0
                        for (auto j : clusters[par_cl]) {
  Branch (958:37): [True: 0, False: 0]
959
0
                            if (par_idx == 0) {
  Branch (959:33): [True: 0, False: 0]
960
0
                                par_pos = j;
961
0
                                break;
962
0
                            }
963
0
                            --par_idx;
964
0
                        }
965
                        // Find a child transaction in the child cluster.
966
0
                        auto chl_idx = rng.randrange(clusters[chl_cl].Count());
967
0
                        SimTxGraph::Pos chl_pos = 0;
968
0
                        for (auto j : clusters[chl_cl]) {
  Branch (968:37): [True: 0, False: 0]
969
0
                            if (chl_idx == 0) {
  Branch (969:33): [True: 0, False: 0]
970
0
                                chl_pos = j;
971
0
                                break;
972
0
                            }
973
0
                            --chl_idx;
974
0
                        }
975
                        // Add dependency to both simulation and real TxGraph.
976
0
                        auto par_ref = top_sim.GetRef(par_pos);
977
0
                        auto chl_ref = top_sim.GetRef(chl_pos);
978
0
                        top_sim.AddDependency(par_ref, chl_ref);
979
0
                        real->AddDependency(*par_ref, *chl_ref);
980
0
                    }
981
                    // Compute the combined cluster.
982
0
                    auto par_cluster = clusters[par_cl];
983
0
                    auto chl_cluster = clusters[chl_cl];
984
0
                    auto new_cluster = par_cluster | chl_cluster;
985
                    // Remove the parent and child cluster from clusters.
986
0
                    std::erase_if(clusters, [&](const auto& cl) noexcept { return cl == par_cluster || cl == chl_cluster; });
  Branch (986:83): [True: 0, False: 0]
  Branch (986:104): [True: 0, False: 0]
987
                    // Add the combined cluster.
988
0
                    clusters.push_back(new_cluster);
989
                    // If this is the first merge that causes an oversized cluster to appear, pick
990
                    // a random number of further merges to appear.
991
0
                    if (!made_oversized) {
  Branch (991:25): [True: 0, False: 0]
992
0
                        made_oversized = new_cluster.Count() > max_cluster_count;
993
0
                        if (!made_oversized) {
  Branch (993:29): [True: 0, False: 0]
994
0
                            FeeFrac total;
995
0
                            for (auto i : new_cluster) total += top_sim.graph.FeeRate(i);
  Branch (995:41): [True: 0, False: 0]
996
0
                            if (uint32_t(total.size) > max_cluster_size) made_oversized = true;
  Branch (996:33): [True: 0, False: 0]
997
0
                        }
998
0
                        if (made_oversized) merges_left = rng.randrange(clusters.size());
  Branch (998:29): [True: 0, False: 0]
999
0
                    }
1000
0
                }
1001
1002
                // Determine an upper bound on how many transactions are removed.
1003
0
                uint32_t max_removed = 0;
1004
0
                for (auto& cluster : clusters) {
  Branch (1004:36): [True: 0, False: 0]
1005
                    // Gather all transaction sizes in the to-be-combined cluster.
1006
0
                    std::vector<uint32_t> sizes;
1007
0
                    for (auto i : cluster) sizes.push_back(top_sim.graph.FeeRate(i).size);
  Branch (1007:33): [True: 0, False: 0]
1008
0
                    auto sum_sizes = std::accumulate(sizes.begin(), sizes.end(), uint64_t{0});
1009
                    // Sort from large to small.
1010
0
                    std::ranges::sort(sizes, std::greater{});
1011
                    // In the worst case, only the smallest transactions are removed.
1012
0
                    while (sizes.size() > max_cluster_count || sum_sizes > max_cluster_size) {
  Branch (1012:28): [True: 0, False: 0]
  Branch (1012:64): [True: 0, False: 0]
1013
0
                        sum_sizes -= sizes.back();
1014
0
                        sizes.pop_back();
1015
0
                        ++max_removed;
1016
0
                    }
1017
0
                }
1018
1019
                // Invoke Trim now on the definitely-oversized txgraph.
1020
0
                auto removed = real->Trim();
1021
                // Verify that the number of removals is within range.
1022
0
                assert(removed.size() >= 1);
  Branch (1022:17): [True: 0, False: 0]
1023
0
                assert(removed.size() <= max_removed);
  Branch (1023:17): [True: 0, False: 0]
1024
                // The removed set must contain all its own descendants.
1025
0
                auto removed_set = top_sim.MakeSet(removed);
1026
0
                for (auto simpos : removed_set) {
  Branch (1026:34): [True: 0, False: 0]
1027
0
                    assert(top_sim.graph.Descendants(simpos).IsSubsetOf(removed_set));
  Branch (1027:21): [True: 0, False: 0]
1028
0
                }
1029
                // Something from every oversized cluster should have been removed, and nothing
1030
                // else.
1031
0
                assert(top_sim.MatchesOversizedClusters(removed_set));
  Branch (1031:17): [True: 0, False: 0]
1032
1033
                // Apply all removals to the simulation, and verify the result is no longer
1034
                // oversized. Don't query the real graph for oversizedness; it is compared
1035
                // against the simulation anyway later.
1036
0
                for (auto simpos : removed_set) {
  Branch (1036:34): [True: 0, False: 0]
1037
0
                    top_sim.RemoveTransaction(top_sim.GetRef(simpos));
1038
0
                }
1039
0
                assert(!top_sim.IsOversized());
  Branch (1039:17): [True: 0, False: 0]
1040
0
                break;
1041
0
            } else if (command-- == 0) {
  Branch (1041:24): [True: 0, False: 0]
1042
                // GetMainMemoryUsage().
1043
0
                auto usage = real->GetMainMemoryUsage();
1044
                // Test stability.
1045
0
                if (alt) {
  Branch (1045:21): [True: 0, False: 0]
1046
0
                    auto usage2 = real->GetMainMemoryUsage();
1047
0
                    assert(usage == usage2);
  Branch (1047:21): [True: 0, False: 0]
1048
0
                }
1049
                // Only empty graphs have 0 memory usage.
1050
0
                if (main_sim.GetTransactionCount() == 0) {
  Branch (1050:21): [True: 0, False: 0]
1051
0
                    assert(usage == 0);
  Branch (1051:21): [True: 0, False: 0]
1052
0
                } else {
1053
0
                    assert(usage > 0);
  Branch (1053:21): [True: 0, False: 0]
1054
0
                }
1055
0
                break;
1056
0
            }
1057
0
        }
1058
0
    }
1059
1060
    // After running all modifications, perform an internal sanity check (before invoking
1061
    // inspectors that may modify the internal state).
1062
0
    real->SanityCheck();
1063
1064
0
    if (!sims[0].IsOversized()) {
  Branch (1064:9): [True: 0, False: 0]
1065
        // If the main graph is not oversized, verify the total ordering implied by
1066
        // CompareMainOrder.
1067
        // First construct two distinct randomized permutations of the positions in sims[0].
1068
0
        std::vector<SimTxGraph::Pos> vec1;
1069
0
        for (auto i : sims[0].graph.Positions()) vec1.push_back(i);
  Branch (1069:21): [True: 0, False: 0]
1070
0
        std::shuffle(vec1.begin(), vec1.end(), rng);
1071
0
        auto vec2 = vec1;
1072
0
        std::shuffle(vec2.begin(), vec2.end(), rng);
1073
0
        if (vec1 == vec2) std::next_permutation(vec2.begin(), vec2.end());
  Branch (1073:13): [True: 0, False: 0]
1074
        // Sort both according to CompareMainOrder. By having randomized starting points, the order
1075
        // of CompareMainOrder invocations is somewhat randomized as well.
1076
0
        auto cmp = [&](SimTxGraph::Pos a, SimTxGraph::Pos b) noexcept {
1077
0
            return real->CompareMainOrder(*sims[0].GetRef(a), *sims[0].GetRef(b)) < 0;
1078
0
        };
1079
0
        std::ranges::sort(vec1, cmp);
1080
0
        std::ranges::sort(vec2, cmp);
1081
1082
        // Verify the resulting orderings are identical. This could only fail if the ordering was
1083
        // not total.
1084
0
        assert(vec1 == vec2);
  Branch (1084:9): [True: 0, False: 0]
1085
1086
        // Verify that the ordering is topological.
1087
0
        auto todo = sims[0].graph.Positions();
1088
0
        for (auto i : vec1) {
  Branch (1088:21): [True: 0, False: 0]
1089
0
            todo.Reset(i);
1090
0
            assert(!sims[0].graph.Ancestors(i).Overlaps(todo));
  Branch (1090:13): [True: 0, False: 0]
1091
0
        }
1092
0
        assert(todo.None());
  Branch (1092:9): [True: 0, False: 0]
1093
1094
        // If the real graph claims to be optimal (the last DoWork() call returned true), verify
1095
        // that calling Linearize on it does not improve it further.
1096
0
        if (sims[0].real_is_optimal) {
  Branch (1096:13): [True: 0, False: 0]
1097
0
            auto real_diagram = ChunkLinearization(sims[0].graph, vec1);
1098
0
            auto fallback_order_sim = [&](DepGraphIndex a, DepGraphIndex b) noexcept {
1099
0
                auto txid_a = sims[0].GetRef(a)->m_txid;
1100
0
                auto txid_b = sims[0].GetRef(b)->m_txid;
1101
0
                return txid_a <=> txid_b;
1102
0
            };
1103
0
            auto [sim_lin, sim_optimal, _cost] = Linearize(sims[0].graph, 300000, rng.rand64(), fallback_order_sim, vec1);
1104
0
            PostLinearize(sims[0].graph, sim_lin);
1105
0
            auto sim_diagram = ChunkLinearization(sims[0].graph, sim_lin);
1106
0
            auto cmp = CompareChunks(real_diagram, sim_diagram);
1107
0
            assert(cmp == 0);
  Branch (1107:13): [True: 0, False: 0]
1108
1109
            // Verify consistency of cross-cluster chunk ordering with tie-break (equal-feerate
1110
            // prefix size).
1111
0
            auto real_chunking = ChunkLinearizationInfo(sims[0].graph, vec1);
1112
            /** Map with one entry per component of the sim main graph. Key is the first Pos of the
1113
             *  component. Value is the sum of all chunk sizes from that component seen
1114
             *  already, at the current chunk feerate. */
1115
0
            std::map<SimTxGraph::Pos, int32_t> comp_prefix_sizes;
1116
            /** Current chunk feerate. */
1117
0
            FeeFrac last_chunk_feerate;
1118
            /** Largest seen (equal-feerate chunk prefix size, max txid).  */
1119
0
            std::pair<int32_t, uint64_t> max_chunk_tiebreak{0, 0};
1120
0
            for (const auto& chunk : real_chunking) {
  Branch (1120:36): [True: 0, False: 0]
1121
                // If this is the first chunk with a strictly lower feerate, reset.
1122
0
                if (ByRatio{chunk.feerate} < ByRatio{last_chunk_feerate}) {
  Branch (1122:21): [True: 0, False: 0]
1123
0
                    comp_prefix_sizes.clear();
1124
0
                    max_chunk_tiebreak = {0, 0};
1125
0
                }
1126
0
                last_chunk_feerate = chunk.feerate;
1127
                // Find which sim component this chunk belongs to.
1128
0
                auto component = sims[0].graph.GetConnectedComponent(sims[0].graph.Positions(), chunk.transactions.First());
1129
0
                assert(chunk.transactions.IsSubsetOf(component));
  Branch (1129:17): [True: 0, False: 0]
1130
0
                auto comp_key = component.First();
1131
0
                auto& comp_prefix_size = comp_prefix_sizes[comp_key];
1132
0
                comp_prefix_size += chunk.feerate.size;
1133
                // Determine the chunk's max txid.
1134
0
                uint64_t chunk_max_txid{0};
1135
0
                for (auto tx : chunk.transactions) {
  Branch (1135:30): [True: 0, False: 0]
1136
0
                    auto txid = sims[0].GetRef(tx)->m_txid;
1137
0
                    chunk_max_txid = std::max(txid, chunk_max_txid);
1138
0
                }
1139
                // Verify consistency: within each group of equal-feerate chunks, the
1140
                // (equal-feerate chunk prefix size, max txid) must be increasing.
1141
0
                std::pair<int32_t, uint64_t> chunk_tiebreak{comp_prefix_size, chunk_max_txid};
1142
0
                assert(chunk_tiebreak > max_chunk_tiebreak);
  Branch (1142:17): [True: 0, False: 0]
1143
0
                max_chunk_tiebreak = chunk_tiebreak;
1144
0
            }
1145
1146
            // Verify that within each cluster, the internal ordering matches that of the
1147
            // simulation if that is optimal too, since per-cluster optimal orderings are
1148
            // deterministic. Note that both have been PostLinearize()'ed.
1149
0
            if (sim_optimal) {
  Branch (1149:17): [True: 0, False: 0]
1150
0
                for (const auto& component : sims[0].GetComponents()) {
  Branch (1150:44): [True: 0, False: 0]
1151
0
                    std::vector<DepGraphIndex> sim_chunk_lin, real_chunk_lin;
1152
0
                    for (auto i : sim_lin) {
  Branch (1152:33): [True: 0, False: 0]
1153
0
                        if (component[i]) sim_chunk_lin.push_back(i);
  Branch (1153:29): [True: 0, False: 0]
1154
0
                    }
1155
0
                    for (auto i : vec1) {
  Branch (1155:33): [True: 0, False: 0]
1156
0
                        if (component[i]) real_chunk_lin.push_back(i);
  Branch (1156:29): [True: 0, False: 0]
1157
0
                    }
1158
0
                    assert(sim_chunk_lin == real_chunk_lin);
  Branch (1158:21): [True: 0, False: 0]
1159
0
                }
1160
0
            }
1161
1162
            // Verify that a fresh TxGraph, with the same transactions and txids, but constructed
1163
            // in a different order, and with a different RNG state, recreates the exact same
1164
            // ordering, showing that for optimal graphs, the full mempool ordering is
1165
            // deterministic.
1166
0
            auto real_redo = MakeTxGraph(
1167
0
                /*max_cluster_count=*/max_cluster_count,
1168
0
                /*max_cluster_size=*/max_cluster_size,
1169
0
                /*acceptable_cost=*/acceptable_cost,
1170
0
                /*fallback_order=*/fallback_order);
1171
            /** Vector (indexed by SimTxGraph::Pos) of TxObjects in real_redo). */
1172
0
            std::vector<std::optional<SimTxObject>> txobjects_redo;
1173
0
            txobjects_redo.resize(sims[0].graph.PositionRange());
1174
            // Recreate the graph's transactions with same feerate and txid.
1175
0
            std::vector<DepGraphIndex> positions;
1176
0
            for (auto i : sims[0].graph.Positions()) positions.push_back(i);
  Branch (1176:25): [True: 0, False: 0]
1177
0
            std::shuffle(positions.begin(), positions.end(), rng);
1178
0
            for (auto i : positions) {
  Branch (1178:25): [True: 0, False: 0]
1179
0
                txobjects_redo[i].emplace(sims[0].GetRef(i)->m_txid);
1180
0
                real_redo->AddTransaction(*txobjects_redo[i], FeePerWeight::FromFeeFrac(sims[0].graph.FeeRate(i)));
1181
0
            }
1182
            // Recreate the graph's dependencies.
1183
0
            std::vector<std::pair<DepGraphIndex, DepGraphIndex>> deps;
1184
0
            for (auto i : sims[0].graph.Positions()) {
  Branch (1184:25): [True: 0, False: 0]
1185
0
                for (auto j : sims[0].graph.GetReducedParents(i)) {
  Branch (1185:29): [True: 0, False: 0]
1186
0
                    deps.emplace_back(j, i);
1187
0
                }
1188
0
            }
1189
0
            std::shuffle(deps.begin(), deps.end(), rng);
1190
0
            for (auto [parent, child] : deps) {
  Branch (1190:39): [True: 0, False: 0]
1191
0
                real_redo->AddDependency(*txobjects_redo[parent], *txobjects_redo[child]);
1192
0
            }
1193
            // Do work to reach optimality.
1194
0
            if (real_redo->DoWork(300000)) {
  Branch (1194:17): [True: 0, False: 0]
1195
                // Start from a random permutation.
1196
0
                auto vec_redo = vec1;
1197
0
                std::shuffle(vec_redo.begin(), vec_redo.end(), rng);
1198
0
                if (vec_redo == vec1) std::next_permutation(vec_redo.begin(), vec_redo.end());
  Branch (1198:21): [True: 0, False: 0]
1199
                // Sort it according to the main graph order in real_redo.
1200
0
                auto cmp_redo = [&](SimTxGraph::Pos a, SimTxGraph::Pos b) noexcept {
1201
0
                    return real_redo->CompareMainOrder(*txobjects_redo[a], *txobjects_redo[b]) < 0;
1202
0
                };
1203
0
                std::ranges::sort(vec_redo, cmp_redo);
1204
                // Compare with the ordering we got from real.
1205
0
                assert(vec1 == vec_redo);
  Branch (1205:17): [True: 0, False: 0]
1206
0
            }
1207
0
        }
1208
1209
        // For every transaction in the total ordering, find a random one before it and after it,
1210
        // and compare their chunk feerates, which must be consistent with the ordering.
1211
0
        for (size_t pos = 0; pos < vec1.size(); ++pos) {
  Branch (1211:30): [True: 0, False: 0]
1212
0
            auto pos_feerate = real->GetMainChunkFeerate(*sims[0].GetRef(vec1[pos]));
1213
0
            if (pos > 0) {
  Branch (1213:17): [True: 0, False: 0]
1214
0
                size_t before = rng.randrange<size_t>(pos);
1215
0
                auto before_feerate = real->GetMainChunkFeerate(*sims[0].GetRef(vec1[before]));
1216
0
                assert(ByRatio{before_feerate} >= ByRatio{pos_feerate});
  Branch (1216:17): [True: 0, False: 0]
1217
0
            }
1218
0
            if (pos + 1 < vec1.size()) {
  Branch (1218:17): [True: 0, False: 0]
1219
0
                size_t after = pos + 1 + rng.randrange<size_t>(vec1.size() - 1 - pos);
1220
0
                auto after_feerate = real->GetMainChunkFeerate(*sims[0].GetRef(vec1[after]));
1221
0
                assert(ByRatio{after_feerate} <= ByRatio{pos_feerate});
  Branch (1221:17): [True: 0, False: 0]
1222
0
            }
1223
0
        }
1224
1225
        // The same order should be obtained through a BlockBuilder as implied by CompareMainOrder,
1226
        // if nothing is skipped.
1227
0
        auto builder = real->GetBlockBuilder();
1228
0
        std::vector<SimTxGraph::Pos> vec_builder;
1229
0
        std::vector<TxGraph::Ref*> last_chunk;
1230
0
        FeePerWeight last_chunk_feerate;
1231
0
        while (auto chunk = builder->GetCurrentChunk()) {
  Branch (1231:21): [True: 0, False: 0]
1232
0
            FeePerWeight sum;
1233
0
            for (TxGraph::Ref* ref : chunk->first) {
  Branch (1233:36): [True: 0, False: 0]
1234
                // The reported chunk feerate must match the chunk feerate obtained by asking
1235
                // it for each of the chunk's transactions individually.
1236
0
                assert(real->GetMainChunkFeerate(*ref) == chunk->second);
  Branch (1236:17): [True: 0, False: 0]
1237
                // Verify the chunk feerate matches the sum of the reported individual feerates.
1238
0
                sum += real->GetIndividualFeerate(*ref);
1239
                // Chunks must contain transactions that exist in the graph.
1240
0
                auto simpos = sims[0].Find(ref);
1241
0
                assert(simpos != SimTxGraph::MISSING);
  Branch (1241:17): [True: 0, False: 0]
1242
0
                vec_builder.push_back(simpos);
1243
0
            }
1244
0
            assert(sum == chunk->second);
  Branch (1244:13): [True: 0, False: 0]
1245
0
            last_chunk = std::move(chunk->first);
1246
0
            last_chunk_feerate = chunk->second;
1247
0
            builder->Include();
1248
0
        }
1249
0
        assert(vec_builder == vec1);
  Branch (1249:9): [True: 0, False: 0]
1250
1251
        // The last chunk returned by the BlockBuilder must match GetWorstMainChunk, in reverse.
1252
0
        std::reverse(last_chunk.begin(), last_chunk.end());
1253
0
        auto [worst_chunk, worst_chunk_feerate] = real->GetWorstMainChunk();
1254
0
        assert(last_chunk == worst_chunk);
  Branch (1254:9): [True: 0, False: 0]
1255
0
        assert(last_chunk_feerate == worst_chunk_feerate);
  Branch (1255:9): [True: 0, False: 0]
1256
1257
        // Check that the implied ordering gives rise to a combined diagram that matches the
1258
        // diagram constructed from the individual cluster linearization chunkings.
1259
0
        auto main_real_diagram = get_diagram_fn(TxGraph::Level::MAIN);
1260
0
        auto main_implied_diagram = ChunkLinearization(sims[0].graph, vec1);
1261
0
        assert(CompareChunks(main_real_diagram, main_implied_diagram) == 0);
  Branch (1261:9): [True: 0, False: 0]
1262
1263
0
        if (sims.size() >= 2 && !sims[1].IsOversized()) {
  Branch (1263:13): [True: 0, False: 0]
  Branch (1263:33): [True: 0, False: 0]
1264
            // When the staging graph is not oversized as well, call GetMainStagingDiagrams, and
1265
            // fully verify the result.
1266
0
            auto [main_cmp_diagram, stage_cmp_diagram] = real->GetMainStagingDiagrams();
1267
            // Check that the feerates in each diagram are monotonically decreasing.
1268
0
            for (size_t i = 1; i < main_cmp_diagram.size(); ++i) {
  Branch (1268:32): [True: 0, False: 0]
1269
0
                assert(ByRatio{main_cmp_diagram[i]} <= ByRatio{main_cmp_diagram[i - 1]});
  Branch (1269:17): [True: 0, False: 0]
1270
0
            }
1271
0
            for (size_t i = 1; i < stage_cmp_diagram.size(); ++i) {
  Branch (1271:32): [True: 0, False: 0]
1272
0
                assert(ByRatio{stage_cmp_diagram[i]} <= ByRatio{stage_cmp_diagram[i - 1]});
  Branch (1272:17): [True: 0, False: 0]
1273
0
            }
1274
            // Treat the diagrams as sets of chunk feerates, and sort them in the same way so that
1275
            // std::set_difference can be used on them below. The exact ordering does not matter
1276
            // here, but it has to be consistent with the one used in main_real_diagram and
1277
            // stage_real_diagram).
1278
0
            std::ranges::sort(main_cmp_diagram, std::greater<ByRatioNegSize<FeeFrac>>{});
1279
0
            std::ranges::sort(stage_cmp_diagram, std::greater<ByRatioNegSize<FeeFrac>>{});
1280
            // Find the chunks that appear in main_diagram but are missing from main_cmp_diagram.
1281
            // This is allowed, because GetMainStagingDiagrams omits clusters in main unaffected
1282
            // by staging.
1283
0
            std::vector<FeeFrac> missing_main_cmp;
1284
0
            std::set_difference(main_real_diagram.begin(), main_real_diagram.end(),
1285
0
                                main_cmp_diagram.begin(), main_cmp_diagram.end(),
1286
0
                                std::inserter(missing_main_cmp, missing_main_cmp.end()),
1287
0
                                std::greater<ByRatioNegSize<FeeFrac>>{});
1288
0
            assert(main_cmp_diagram.size() + missing_main_cmp.size() == main_real_diagram.size());
  Branch (1288:13): [True: 0, False: 0]
1289
            // Do the same for chunks in stage_diagram missing from stage_cmp_diagram.
1290
0
            auto stage_real_diagram = get_diagram_fn(TxGraph::Level::TOP);
1291
0
            std::vector<FeeFrac> missing_stage_cmp;
1292
0
            std::set_difference(stage_real_diagram.begin(), stage_real_diagram.end(),
1293
0
                                stage_cmp_diagram.begin(), stage_cmp_diagram.end(),
1294
0
                                std::inserter(missing_stage_cmp, missing_stage_cmp.end()),
1295
0
                                std::greater<ByRatioNegSize<FeeFrac>>{});
1296
0
            assert(stage_cmp_diagram.size() + missing_stage_cmp.size() == stage_real_diagram.size());
  Branch (1296:13): [True: 0, False: 0]
1297
            // The missing chunks must be equal across main & staging (otherwise they couldn't have
1298
            // been omitted).
1299
0
            assert(missing_main_cmp == missing_stage_cmp);
  Branch (1299:13): [True: 0, False: 0]
1300
1301
            // The missing part must include at least all transactions in staging which have not been
1302
            // modified, or been in a cluster together with modified transactions, since they were
1303
            // copied from main. Note that due to the reordering of removals w.r.t. dependency
1304
            // additions, it is possible that the real implementation found more unaffected things.
1305
0
            FeeFrac missing_real;
1306
0
            for (const auto& feerate : missing_main_cmp) missing_real += feerate;
  Branch (1306:38): [True: 0, False: 0]
1307
0
            FeeFrac missing_expected = sims[1].graph.FeeRate(sims[1].graph.Positions() - sims[1].modified);
1308
            // Note that missing_real.fee < missing_expected.fee is possible to due the presence of
1309
            // negative-fee transactions.
1310
0
            assert(missing_real.size >= missing_expected.size);
  Branch (1310:13): [True: 0, False: 0]
1311
0
        }
1312
0
    }
1313
1314
0
    assert(real->HaveStaging() == (sims.size() > 1));
  Branch (1314:5): [True: 0, False: 0]
1315
1316
    // Try to run a full comparison, for both TxGraph::Level::MAIN and TxGraph::Level::TOP in
1317
    // TxGraph inspector functions that support both.
1318
0
    for (auto level : {TxGraph::Level::TOP, TxGraph::Level::MAIN}) {
  Branch (1318:21): [True: 0, False: 0]
1319
0
        auto& sim = level == TxGraph::Level::TOP ? sims.back() : sims.front();
  Branch (1319:21): [True: 0, False: 0]
1320
        // Compare simple properties of the graph with the simulation.
1321
0
        assert(real->IsOversized(level) == sim.IsOversized());
  Branch (1321:9): [True: 0, False: 0]
1322
0
        assert(real->GetTransactionCount(level) == sim.GetTransactionCount());
  Branch (1322:9): [True: 0, False: 0]
1323
        // If the graph (and the simulation) are not oversized, perform a full comparison.
1324
0
        if (!sim.IsOversized()) {
  Branch (1324:13): [True: 0, False: 0]
1325
0
            auto todo = sim.graph.Positions();
1326
            // Iterate over all connected components of the resulting (simulated) graph, each of which
1327
            // should correspond to a cluster in the real one.
1328
0
            while (todo.Any()) {
  Branch (1328:20): [True: 0, False: 0]
1329
0
                auto component = sim.graph.FindConnectedComponent(todo);
1330
0
                todo -= component;
1331
                // Iterate over the transactions in that component.
1332
0
                for (auto i : component) {
  Branch (1332:29): [True: 0, False: 0]
1333
                    // Check its individual feerate against simulation.
1334
0
                    assert(sim.graph.FeeRate(i) == real->GetIndividualFeerate(*sim.GetRef(i)));
  Branch (1334:21): [True: 0, False: 0]
1335
                    // Check its ancestors against simulation.
1336
0
                    auto expect_anc = sim.graph.Ancestors(i);
1337
0
                    auto anc = sim.MakeSet(real->GetAncestors(*sim.GetRef(i), level));
1338
0
                    assert(anc.Count() <= max_cluster_count);
  Branch (1338:21): [True: 0, False: 0]
1339
0
                    assert(anc == expect_anc);
  Branch (1339:21): [True: 0, False: 0]
1340
                    // Check its descendants against simulation.
1341
0
                    auto expect_desc = sim.graph.Descendants(i);
1342
0
                    auto desc = sim.MakeSet(real->GetDescendants(*sim.GetRef(i), level));
1343
0
                    assert(desc.Count() <= max_cluster_count);
  Branch (1343:21): [True: 0, False: 0]
1344
0
                    assert(desc == expect_desc);
  Branch (1344:21): [True: 0, False: 0]
1345
                    // Check the cluster the transaction is part of.
1346
0
                    auto cluster = real->GetCluster(*sim.GetRef(i), level);
1347
0
                    assert(cluster.size() <= max_cluster_count);
  Branch (1347:21): [True: 0, False: 0]
1348
0
                    assert(sim.MakeSet(cluster) == component);
  Branch (1348:21): [True: 0, False: 0]
1349
                    // Check that the cluster is reported in a valid topological order (its
1350
                    // linearization).
1351
0
                    std::vector<DepGraphIndex> simlin;
1352
0
                    SimTxGraph::SetType done;
1353
0
                    uint64_t total_size{0};
1354
0
                    for (TxGraph::Ref* ptr : cluster) {
  Branch (1354:44): [True: 0, False: 0]
1355
0
                        auto simpos = sim.Find(ptr);
1356
0
                        assert(sim.graph.Descendants(simpos).IsSubsetOf(component - done));
  Branch (1356:25): [True: 0, False: 0]
1357
0
                        done.Set(simpos);
1358
0
                        assert(sim.graph.Ancestors(simpos).IsSubsetOf(done));
  Branch (1358:25): [True: 0, False: 0]
1359
0
                        simlin.push_back(simpos);
1360
0
                        total_size += sim.graph.FeeRate(simpos).size;
1361
0
                    }
1362
                    // Check cluster size.
1363
0
                    assert(total_size <= max_cluster_size);
  Branch (1363:21): [True: 0, False: 0]
1364
                    // Construct a chunking object for the simulated graph, using the reported cluster
1365
                    // linearization as ordering, and compare it against the reported chunk feerates.
1366
0
                    if (sims.size() == 1 || level == TxGraph::Level::MAIN) {
  Branch (1366:25): [True: 0, False: 0]
  Branch (1366:45): [True: 0, False: 0]
1367
0
                        auto simlinchunk = ChunkLinearizationInfo(sim.graph, simlin);
1368
0
                        DepGraphIndex idx{0};
1369
0
                        for (auto& chunk : simlinchunk) {
  Branch (1369:42): [True: 0, False: 0]
1370
                            // Require that the chunks of cluster linearizations are connected (this must
1371
                            // be the case as all linearizations inside are PostLinearized).
1372
0
                            assert(sim.graph.IsConnected(chunk.transactions));
  Branch (1372:29): [True: 0, False: 0]
1373
                            // Check the chunk feerates of all transactions in the cluster.
1374
0
                            while (chunk.transactions.Any()) {
  Branch (1374:36): [True: 0, False: 0]
1375
0
                                assert(chunk.transactions[simlin[idx]]);
  Branch (1375:33): [True: 0, False: 0]
1376
0
                                chunk.transactions.Reset(simlin[idx]);
1377
0
                                assert(chunk.feerate == real->GetMainChunkFeerate(*cluster[idx]));
  Branch (1377:33): [True: 0, False: 0]
1378
0
                                ++idx;
1379
0
                            }
1380
0
                        }
1381
0
                    }
1382
0
                }
1383
0
            }
1384
0
        }
1385
0
    }
1386
1387
    // Sanity check again (because invoking inspectors may modify internal unobservable state).
1388
0
    real->SanityCheck();
1389
1390
    // Kill the block builders.
1391
0
    block_builders.clear();
1392
    // Kill the TxGraph object.
1393
0
    real.reset();
1394
    // Kill the simulated graphs, with all remaining Refs in it. If any, this verifies that Refs
1395
    // can outlive the TxGraph that created them.
1396
0
    sims.clear();
1397
0
}