fuzz coverage

Coverage Report

Created: 2025-09-17 22:41

/Users/eugenesiegel/btc/bitcoin/src/txgraph.h
Line
Count
Source (jump to first uncovered line)
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 <compare>
6
#include <cstdint>
7
#include <memory>
8
#include <optional>
9
#include <utility>
10
#include <vector>
11
12
#include <util/feefrac.h>
13
14
#ifndef BITCOIN_TXGRAPH_H
15
#define BITCOIN_TXGRAPH_H
16
17
static constexpr unsigned MAX_CLUSTER_COUNT_LIMIT{64};
18
19
/** Data structure to encapsulate fees, sizes, and dependencies for a set of transactions.
20
 *
21
 * Each TxGraph represents one or two such graphs ("main", and optionally "staging"), to allow for
22
 * working with batches of changes that may still be discarded.
23
 *
24
 * The connected components within each transaction graph are called clusters: whenever one
25
 * transaction is reachable from another, through any sequence of is-parent-of or is-child-of
26
 * relations, they belong to the same cluster (so clusters include parents, children, but also
27
 * grandparents, siblings, cousins twice removed, ...).
28
 *
29
 * For each graph, TxGraph implicitly defines an associated total ordering on its transactions
30
 * (its linearization) that respects topology (parents go before their children), aiming for it to
31
 * be close to the optimal order those transactions should be mined in if the goal is fee
32
 * maximization, though this is a best effort only, not a strong guarantee.
33
 *
34
 * For more explanation, see https://delvingbitcoin.org/t/introduction-to-cluster-linearization/1032
35
 *
36
 * This linearization is partitioned into chunks: groups of transactions that according to this
37
 * order would be mined together. Each chunk consists of the highest-feerate prefix of what remains
38
 * of the linearization after removing previous chunks. TxGraph guarantees that the maintained
39
 * linearization always results in chunks consisting of transactions that are connected. A chunk's
40
 * transactions always belong to the same cluster.
41
 *
42
 * The interface is designed to accommodate an implementation that only stores the transitive
43
 * closure of dependencies, so if B spends C, it does not distinguish between "A spending B" and
44
 * "A spending both B and C".
45
 */
46
class TxGraph
47
{
48
public:
49
    /** Internal identifier for a transaction within a TxGraph. */
50
    using GraphIndex = uint32_t;
51
52
    /** Data type used to reference transactions within a TxGraph.
53
     *
54
     * Every transaction within a TxGraph has exactly one corresponding TxGraph::Ref, held by users
55
     * of the class. Destroying the TxGraph::Ref removes the corresponding transaction (in both the
56
     * main and staging graphs).
57
     *
58
     * Users of the class can inherit from TxGraph::Ref. If all Refs are inherited this way, the
59
     * Ref* pointers returned by TxGraph functions can be cast to, and used as, this inherited type.
60
     */
61
    class Ref;
62
63
    /** Virtual destructor, so inheriting is safe. */
64
0
    virtual ~TxGraph() = default;
65
    /** Construct a new transaction with the specified feerate, and return a Ref to it.
66
     *  If a staging graph exists, the new transaction is only created there. feerate.size must be
67
     *  strictly positive. In all further calls, only Refs created by AddTransaction() are allowed
68
     *  to be passed to this TxGraph object (or empty Ref objects). Ref objects may outlive the
69
     *  TxGraph they were created for. */
70
    [[nodiscard]] virtual Ref AddTransaction(const FeePerWeight& feerate) noexcept = 0;
71
    /** Remove the specified transaction. If a staging graph exists, the removal only happens
72
     *  there. This is a no-op if the transaction was already removed.
73
     *
74
     * TxGraph may internally reorder transaction removals with dependency additions for
75
     * performance reasons. If together with any transaction removal all its descendants, or all
76
     * its ancestors, are removed as well (which is what always happens in realistic scenarios),
77
     * this reordering will not affect the behavior of TxGraph.
78
     *
79
     * As an example, imagine 3 transactions A,B,C where B depends on A. If a dependency of C on B
80
     * is added, and then B is deleted, C will still depend on A. If the deletion of B is reordered
81
     * before the C->B dependency is added, the dependency adding has no effect. If, together with
82
     * the deletion of B also either A or C is deleted, there is no distinction between the
83
     * original order case and the reordered case.
84
     */
85
    virtual void RemoveTransaction(const Ref& arg) noexcept = 0;
86
    /** Add a dependency between two specified transactions. If a staging graph exists, the
87
     *  dependency is only added there. Parent may not be a descendant of child already (but may
88
     *  be an ancestor of it already, in which case this is a no-op). If either transaction is
89
     *  already removed, this is a no-op. */
90
    virtual void AddDependency(const Ref& parent, const Ref& child) noexcept = 0;
91
    /** Modify the fee of the specified transaction, in both the main graph and the staging
92
     *  graph if it exists. Wherever the transaction does not exist (or was removed), this has no
93
     *  effect. */
94
    virtual void SetTransactionFee(const Ref& arg, int64_t fee) noexcept = 0;
95
96
    /** TxGraph is internally lazy, and will not compute many things until they are needed.
97
     *  Calling DoWork will perform some work now (controlled by iters) so that future operations
98
     *  are fast, if there is any. Returns whether all currently-available work is done. This can
99
     *  be invoked while oversized, but oversized graphs will be skipped by this call. */
100
    virtual bool DoWork(uint64_t iters) noexcept = 0;
101
102
    /** Create a staging graph (which cannot exist already). This acts as if a full copy of
103
     *  the transaction graph is made, upon which further modifications are made. This copy can
104
     *  be inspected, and then either discarded, or the main graph can be replaced by it by
105
     *  committing it. */
106
    virtual void StartStaging() noexcept = 0;
107
    /** Discard the existing active staging graph (which must exist). */
108
    virtual void AbortStaging() noexcept = 0;
109
    /** Replace the main graph with the staging graph (which must exist). */
110
    virtual void CommitStaging() noexcept = 0;
111
    /** Check whether a staging graph exists. */
112
    virtual bool HaveStaging() const noexcept = 0;
113
114
    /** Determine whether the graph is oversized (contains a connected component of more than the
115
     *  configured maximum cluster count). If main_only is false and a staging graph exists, it is
116
     *  queried; otherwise the main graph is queried. Some of the functions below are not available
117
     *  for oversized graphs. The mutators above are always available. Removing a transaction by
118
     *  destroying its Ref while staging exists will not clear main's oversizedness until staging
119
     *  is aborted or committed. */
120
    virtual bool IsOversized(bool main_only = false) noexcept = 0;
121
    /** Determine whether arg exists in the graph (i.e., was not removed). If main_only is false
122
     *  and a staging graph exists, it is queried; otherwise the main graph is queried. This is
123
     *  available even for oversized graphs. */
124
    virtual bool Exists(const Ref& arg, bool main_only = false) noexcept = 0;
125
    /** Get the individual transaction feerate of transaction arg. Returns the empty FeePerWeight
126
     *  if arg does not exist in either main or staging. This is available even for oversized
127
     *  graphs. */
128
    virtual FeePerWeight GetIndividualFeerate(const Ref& arg) noexcept = 0;
129
    /** Get the feerate of the chunk which transaction arg is in, in the main graph. Returns the
130
     *  empty FeePerWeight if arg does not exist in the main graph. The main graph must not be
131
     *  oversized. */
132
    virtual FeePerWeight GetMainChunkFeerate(const Ref& arg) noexcept = 0;
133
    /** Get pointers to all transactions in the cluster which arg is in. The transactions are
134
     *  returned in graph order. If main_only is false and a staging graph exists, it is queried;
135
     *  otherwise the main graph is queried. The queried graph must not be oversized. Returns {} if
136
     *  arg does not exist in the queried graph. */
137
    virtual std::vector<Ref*> GetCluster(const Ref& arg, bool main_only = false) noexcept = 0;
138
    /** Get pointers to all ancestors of the specified transaction (including the transaction
139
     *  itself), in unspecified order. If main_only is false and a staging graph exists, it is
140
     *  queried; otherwise the main graph is queried. The queried graph must not be oversized.
141
     *  Returns {} if arg does not exist in the graph. */
142
    virtual std::vector<Ref*> GetAncestors(const Ref& arg, bool main_only = false) noexcept = 0;
143
    /** Get pointers to all descendants of the specified transaction (including the transaction
144
     *  itself), in unspecified order. If main_only is false and a staging graph exists, it is
145
     *  queried; otherwise the main graph is queried. The queried graph must not be oversized.
146
     *  Returns {} if arg does not exist in the graph. */
147
    virtual std::vector<Ref*> GetDescendants(const Ref& arg, bool main_only = false) noexcept = 0;
148
    /** Like GetAncestors, but return the Refs for all transactions in the union of the provided
149
     *  arguments' ancestors (each transaction is only reported once). Refs that do not exist in
150
     *  the queried graph are ignored. Null refs are not allowed. */
151
    virtual std::vector<Ref*> GetAncestorsUnion(std::span<const Ref* const> args, bool main_only = false) noexcept = 0;
152
    /** Like GetDescendants, but return the Refs for all transactions in the union of the provided
153
     *  arguments' descendants (each transaction is only reported once). Refs that do not exist in
154
     *  the queried graph are ignored. Null refs are not allowed. */
155
    virtual std::vector<Ref*> GetDescendantsUnion(std::span<const Ref* const> args, bool main_only = false) noexcept = 0;
156
    /** Get the total number of transactions in the graph. If main_only is false and a staging
157
     *  graph exists, it is queried; otherwise the main graph is queried. This is available even
158
     *  for oversized graphs. */
159
    virtual GraphIndex GetTransactionCount(bool main_only = false) noexcept = 0;
160
    /** Compare two transactions according to their order in the main graph. Both transactions must
161
     *  be in the main graph. The main graph must not be oversized. */
162
    virtual std::strong_ordering CompareMainOrder(const Ref& a, const Ref& b) noexcept = 0;
163
    /** Count the number of distinct clusters that the specified transactions belong to. If
164
     *  main_only is false and a staging graph exists, staging clusters are counted. Otherwise,
165
     *  main clusters are counted. Refs that do not exist in the queried graph are ignored. Refs
166
     *  can not be null. The queried graph must not be oversized. */
167
    virtual GraphIndex CountDistinctClusters(std::span<const Ref* const>, bool main_only = false) noexcept = 0;
168
    /** For both main and staging (which must both exist and not be oversized), return the combined
169
     *  respective feerate diagrams, including chunks from all clusters, but excluding clusters
170
     *  that appear identically in both. Use FeeFrac rather than FeePerWeight so CompareChunks is
171
     *  usable without type-conversion. */
172
    virtual std::pair<std::vector<FeeFrac>, std::vector<FeeFrac>> GetMainStagingDiagrams() noexcept = 0;
173
    /** Remove transactions (including their own descendants) according to a fast but best-effort
174
     *  strategy such that the TxGraph's cluster and size limits are respected. Applies to staging
175
     *  if it exists, and to main otherwise. Returns the list of all removed transactions in
176
     *  unspecified order. This has no effect unless the relevant graph is oversized. */
177
    virtual std::vector<Ref*> Trim() noexcept = 0;
178
179
    /** Interface returned by GetBlockBuilder. */
180
    class BlockBuilder
181
    {
182
    protected:
183
        /** Make constructor non-public (use TxGraph::GetBlockBuilder()). */
184
0
        BlockBuilder() noexcept = default;
185
    public:
186
        /** Support safe inheritance. */
187
0
        virtual ~BlockBuilder() = default;
188
        /** Get the chunk that is currently suggested to be included, plus its feerate, if any. */
189
        virtual std::optional<std::pair<std::vector<Ref*>, FeePerWeight>> GetCurrentChunk() noexcept = 0;
190
        /** Mark the current chunk as included, and progress to the next one. */
191
        virtual void Include() noexcept = 0;
192
        /** Mark the current chunk as skipped, and progress to the next one. Further chunks from
193
         *  the same cluster as the current one will not be reported anymore. */
194
        virtual void Skip() noexcept = 0;
195
    };
196
197
    /** Construct a block builder, drawing chunks in order, from the main graph, which cannot be
198
     *  oversized. While the returned object exists, no mutators on the main graph are allowed.
199
     *  The BlockBuilder object must not outlive the TxGraph it was created with. */
200
    virtual std::unique_ptr<BlockBuilder> GetBlockBuilder() noexcept = 0;
201
    /** Get the last chunk in the main graph, i.e., the last chunk that would be returned by a
202
     *  BlockBuilder created now, together with its feerate. The chunk is returned in
203
     *  reverse-topological order, so every element is preceded by all its descendants. The main
204
     *  graph must not be oversized. If the graph is empty, {{}, FeePerWeight{}} is returned. */
205
    virtual std::pair<std::vector<Ref*>, FeePerWeight> GetWorstMainChunk() noexcept = 0;
206
207
    /** Perform an internal consistency check on this object. */
208
    virtual void SanityCheck() const = 0;
209
210
protected:
211
    // Allow TxGraph::Ref to call UpdateRef and UnlinkRef.
212
    friend class TxGraph::Ref;
213
    /** Inform the TxGraph implementation that a TxGraph::Ref has moved. */
214
    virtual void UpdateRef(GraphIndex index, Ref& new_location) noexcept = 0;
215
    /** Inform the TxGraph implementation that a TxGraph::Ref was destroyed. */
216
    virtual void UnlinkRef(GraphIndex index) noexcept = 0;
217
    // Allow TxGraph implementations (inheriting from it) to access Ref internals.
218
0
    static TxGraph*& GetRefGraph(Ref& arg) noexcept { return arg.m_graph; }
219
0
    static TxGraph* GetRefGraph(const Ref& arg) noexcept { return arg.m_graph; }
220
0
    static GraphIndex& GetRefIndex(Ref& arg) noexcept { return arg.m_index; }
221
0
    static GraphIndex GetRefIndex(const Ref& arg) noexcept { return arg.m_index; }
222
223
public:
224
    class Ref
225
    {
226
        // Allow TxGraph's GetRefGraph and GetRefIndex to access internals.
227
        friend class TxGraph;
228
        /** Which Graph the Entry lives in. nullptr if this Ref is empty. */
229
        TxGraph* m_graph = nullptr;
230
        /** Index into the Graph's m_entries. Only used if m_graph != nullptr. */
231
        GraphIndex m_index = GraphIndex(-1);
232
    public:
233
        /** Construct an empty Ref. Non-empty Refs can only be created using
234
         *  TxGraph::AddTransaction. */
235
0
        Ref() noexcept = default;
236
        /** Destroy this Ref. If it is not empty, the corresponding transaction is removed (in both
237
         *  main and staging, if it exists). */
238
        virtual ~Ref();
239
        // Support moving a Ref.
240
        Ref& operator=(Ref&& other) noexcept;
241
        Ref(Ref&& other) noexcept;
242
        // Do not permit copy constructing or copy assignment. A TxGraph entry can have at most one
243
        // Ref pointing to it.
244
        Ref& operator=(const Ref&) = delete;
245
        Ref(const Ref&) = delete;
246
    };
247
};
248
249
/** Construct a new TxGraph with the specified limit on the number of transactions within a cluster,
250
 *  and on the sum of transaction sizes within a cluster. max_cluster_count cannot exceed
251
 *  MAX_CLUSTER_COUNT_LIMIT. acceptable_iters controls how many linearization optimization
252
 *  steps will be performed per cluster before they are considered to be of acceptable quality. */
253
std::unique_ptr<TxGraph> MakeTxGraph(unsigned max_cluster_count, uint64_t max_cluster_size, uint64_t acceptable_iters) noexcept;
254
255
#endif // BITCOIN_TXGRAPH_H