Commit Graph

67 Commits

Author SHA1 Message Date
Pieter Wuille
3efc94d656 clusterlin: replace cluster linearization with SFL (feature)
This replaces the existing LIMO linearization algorithm (which internally uses
ancestor set finding and candidate set finding) with the much more performant
spanning-forest linearization algorithm.

This removes the old candidate-set search algorithm, and several of its tests,
benchmarks, and needed utility code.

The worst case time per cost is similar to the previous algorithm, so
ACCEPTABLE_ITERS is unchanged.
2025-12-18 16:01:31 -05:00
Lőrinc
039307554e refactor: unify container presence checks - trivial counts
The changes made here were:

| From              | To               |
|-------------------|------------------|
| `m.count(k)`      | `m.contains(k)`  |
| `!m.count(k)`     | `!m.contains(k)` |
| `m.count(k) == 0` | `!m.contains(k)` |
| `m.count(k) != 0` | `m.contains(k)`  |
| `m.count(k) > 0`  | `m.contains(k)`  |

The commit contains the trivial, mechanical refactors where it doesn't matter if the container can have multiple elements or not

Co-authored-by: Jan B <608446+janb84@users.noreply.github.com>
2025-12-03 13:36:58 +01:00
Anthony Towns
ade0397f59 txgraph: drop move assignment operator 2025-11-25 07:36:50 -05:00
Lőrinc
2d23820ee1 refactor: remove dead branches in SingletonClusterImpl
`SplitAll()` always calls `ApplyRemovals()` first, for a singleton, it empties the cluster, therefore any `SingletonClusterImpl` passed to `Split()` must be empty.

`TxGraphImpl::ApplyDependencies()` first merges each dependency group and asserts the group has at least one dependency.
Since `parent` != `child`, `TxGraphImpl::Merge()` upgrades the merge target to `GenericClusterImpl`, therefore the `ApplyDependencies()` is never dispatched to `SingletonClusterImpl`.

Found during review: https://github.com/bitcoin/bitcoin/pull/33157#discussion_r2423058928
Coverage evidence:
* https://maflcko.github.io/b-c-cov/fuzz.coverage/src/txgraph.cpp.gcov.html#L1446
* https://storage.googleapis.com/oss-fuzz-coverage/bitcoin-core/reports/20251103/linux/src/bitcoin-core/src/txgraph.cpp.html#L1446
2025-11-03 12:19:05 +01:00
Greg Sanders
9b43428c96 TxGraph: change m_excluded_clusters
Change BlockBuilderImpl's m_excluded_clusters to unordered
set since ordering is not used.

Change the set to a set of sequence numbers for a modest
stability increase under fuzz testing.
2025-10-14 12:44:57 -04:00
Pieter Wuille
023cd5a546 txgraph: add SingletonClusterImpl (mem optimization)
This adds a specialized Cluster implementation for singleton clusters, saving
a significant amount of memory by avoiding the need for m_depgraph, m_mapping,
and m_linearization, and their overheads.
2025-10-11 17:46:43 -04:00
Pieter Wuille
e346250732 txgraph: give Clusters a range of intended tx counts (preparation) 2025-10-11 17:32:35 -04:00
Pieter Wuille
e93b0f09cc txgraph: abstract out creation of empty Clusters (refactor) 2025-10-11 17:32:35 -04:00
Pieter Wuille
6baf12621f txgraph: comment fixes (doc fix) 2025-10-11 17:32:35 -04:00
Pieter Wuille
726b995739 txgraph: make Cluster an abstract class (refactor) 2025-10-11 17:32:32 -04:00
Pieter Wuille
2602d89edd txgraph: avoid accessing other Cluster internals (refactor)
This adds 4 functions to Cluster to help implement Merge() and Split() without
needing access to the internals of the other Cluster. This is a preparation for
a follow-up that will make Clusters a virtual class whose internals are abstracted
away.
2025-10-11 17:26:39 -04:00
Pieter Wuille
04c808ac4c txgraph: expose memory usage estimate function (feature) 2025-10-11 17:25:09 -04:00
Pieter Wuille
7680bb8fd4 txgraph: keep track of Cluster memory usage (preparation) 2025-10-11 17:25:09 -04:00
Pieter Wuille
4ba562e5f4 txgraph: keep data structures compact (mem optimization) 2025-10-11 17:25:09 -04:00
Pieter Wuille
b1637a90de txgraph: avoid holes in DepGraph positions (mem optimization) 2025-10-11 17:25:05 -04:00
Pieter Wuille
2b1d302508 txgraph: move some sanity checks from Cluster to TxGraphImpl (refactor) 2025-10-11 17:16:05 -04:00
Pieter Wuille
d40302fbaf txgraph: Make level of Cluster implicit (optimization)
This reduces per-Cluster memory usage by making Clusters not aware of their
own level. Instead, track it either in calling code, or infer it based on
the transactions in them.
2025-10-11 17:13:50 -04:00
Pieter Wuille
d45f3717d2 txgraph: use enum Level instead of bool main_only 2025-09-10 08:03:17 -04:00
Pieter Wuille
f3c2fc867f txgraph: add work limit to DoWork(), try optimal (feature)
This adds an `iters` parameter to DoWork(), which controls how much work it is
allowed to do right now.

Additionally, DoWork() won't stop at just getting everything ACCEPTABLE, but if
there is work budget left, will also attempt to get every cluster linearized
optimally.
2025-07-14 10:28:54 -04:00
Pieter Wuille
e96b00d99e txgraph: make number of acceptable iterations configurable (feature) 2025-07-14 09:42:58 -04:00
Pieter Wuille
cfe9958852 txgraph: track amount of work done in linearization (preparation) 2025-07-14 09:41:17 -04:00
Pieter Wuille
6ba316eaa0 txgraph: 1-or-2-tx split-off clusters are optimal (optimization) 2025-07-14 09:30:24 -04:00
Pieter Wuille
fad0eb091e txgraph: reset quality when merging clusters (bugfix) 2025-07-14 09:30:24 -04:00
Pieter Wuille
1632fc104b txgraph: Track multiple potential would-be clusters in Trim (improvement)
In the existing Trim function, as soon as the set of accepted transactions
would exceed the max cluster size or count limit, the acceptance loop is
stopped, removing all later transactions. However, it is possible that by
excluding some of those transactions the would-be cluster splits apart into
multiple would-clusters. And those clusters may well permit far more
transactions before their limits are reached.

Take this into account by using a union-find structure inside TrimTxData to
keep track of the count/size of all would-be clusters that would be formed
at any point, and only reject transactions which would cause these resulting
partitions to exceed their limits.

This is not an optimization in terms of CPU usage or memory; it just
improves the quality of the transactions removed by Trim().
2025-07-02 16:01:57 -04:00
Pieter Wuille
a04e205ab0 txgraph: Add ability to trim oversized clusters (feature)
During reorganisations, it is possible that dependencies get add which
result in clusters that violate limits (count, size), when linking the
new from-block transactions to the old from-mempool transactions.

Unlike RBF scenarios, we cannot simply reject these policy violations
when they are due to received blocks. To accomodate this, add a Trim()
function to TxGraph, which removes transactions (including descendants)
in order to make all resulting clusters satisfy the limits.

In the initial version of the function added here, the following approach
is used:
- Lazily compute a naive linearization for the to-be-merged cluster (using
  an O(n log n) algorithm, optimized for far larger groups of transactions
  than the normal linearization code).
- Initialize a set of accepted transactions to {}
- Iterate over the transactions in this cluster one by one:
  - If adding the transaction to the set makes it exceed the max cluster size
    or count limit, stop.
  - Add the transaction to the set.
- Remove all transactions from the cluster that were not included in the set
  (note that this necessarily includes all descendants too, because they
  appear later in the naive linearization).

Co-authored-by: Greg Sanders <gsanders87@gmail.com>
2025-07-02 14:52:54 -04:00
Greg Sanders
eabcd0eb6f txgraph: remove unnecessary m_group_oversized (simplification) 2025-07-02 14:52:54 -04:00
Pieter Wuille
19b14e61ea txgraph: Permit transactions that exceed cluster size limit (feature)
This removes the restriction added in the previous commit that individual
transactions do not exceed the max cluster size limit.

With this change, the responsibility for enforcing cluster size limits can
be localized purely in TxGraph, without callers (and in particular, tests)
needing to duplicate the enforcement for individual transactions.
2025-07-02 14:52:54 -04:00
Pieter Wuille
c4287b9b71 txgraph: Add ability to configure maximum cluster size/weight (feature)
This is integrated with the oversized property: the graph is oversized when
any connected component within it contains more than the cluster count limit
many transactions, or when their combined size/weight exceeds the cluster size
limit.

It becomes disallowed to call AddTransaction with a size larger than this limit,
though this limit will be lifted in the next commit.

In addition, SetTransactionFeeRate becomes SetTransactionFee, so that we do not
need to deal with the case that a call to this function might affect the
oversizedness.
2025-07-02 14:52:54 -04:00
fanquake
e50312eab0 doc: fix typos
Co-authored-by: Ragnar <rodiondenmark@gmail.com>
Co-authored-by: VolodymyrBg <aqdrgg19@gmail.com>
2025-06-03 08:09:28 +01:00
Pieter Wuille
8673e8f019 txgraph: Special-case singletons in chunk index (optimization) 2025-05-12 17:07:30 -04:00
Pieter Wuille
abdd9d35a3 txgraph: Skipping end of cluster has no impact (optimization) 2025-05-12 17:07:30 -04:00
Pieter Wuille
604acc2c28 txgraph: Reuse discarded chunkindex entries (optimization) 2025-05-12 17:07:30 -04:00
Pieter Wuille
c734081454 txgraph: Introduce TxGraph::GetWorstMainChunk (feature)
It returns the last chunk that would be suggested for mining by BlockBuilder
objects. This is intended for eviction.
2025-05-12 17:07:30 -04:00
Pieter Wuille
394dbe2142 txgraph: Introduce BlockBuilder interface (feature)
This interface lets one iterate efficiently over the chunks of the main
graph in a TxGraph, in the same order as CompareMainOrder. Each chunk
can be marked as "included" or "skipped" (and in the latter case,
dependent chunks will be skipped).
2025-05-12 17:07:30 -04:00
Pieter Wuille
883df3648e txgraph: Generalize GetClusterRefs to support subsections (preparation)
This is preparation for a next commit which will need a way to extract Refs
for just individual chunks from a cluster.
2025-05-12 17:07:30 -04:00
Pieter Wuille
c28a602e00 txgraph: Introduce TxGraphImpl observer tracking (preparation)
This is preparation for a next commit which will introduce a class whose
objects hold references to internals in TxGraphImpl, which disallows
modifications to the graph while such objects exist.
2025-05-12 17:07:30 -04:00
Pieter Wuille
9095d8ac1c txgraph: Maintain chunk index (preparation)
This is preparation for exposing mining and eviction functionality in
TxGraph.
2025-05-12 17:07:28 -04:00
Pieter Wuille
87e74e1242 txgraph: abstract out transaction ordering (refactor) 2025-05-12 17:00:33 -04:00
Pieter Wuille
2614fea17f txgraph: Add GetMainStagingDiagrams function (feature)
This allows determining whether the changes in a staging diagram unambiguously improve
the graph, through CompareChunks().
2025-05-12 16:00:24 -04:00
Pieter Wuille
2835216ec0 txgraph: make GroupClusters use partition numbers directly (optimization) 2025-04-11 10:43:56 -04:00
Pieter Wuille
c72c8d5d45 txgraph: compare sequence numbers instead of Cluster* (bugfix)
This makes fuzz testing more deterministic, by avoiding the (arbitrary) pointer
value ordering in comparing transactions.
2025-04-11 10:43:34 -04:00
Greg Sanders
a40bd374aa Get*Union: disallow nulltpr Refs 2025-03-31 13:07:37 -04:00
Greg Sanders
57433502e6 CountDistinctClusters: nullptrs disallowed 2025-03-31 13:07:37 -04:00
Greg Sanders
8bca0d325a TxGraphImpl::Compact: m_main_clusterset.m_removed is always empty 2025-03-31 13:07:14 -04:00
Greg Sanders
2c5cf987e9 TxGraphImpl::PullIn: only allowed when staging exists 2025-03-31 13:07:10 -04:00
Pieter Wuille
c7d5dcaa61 clusterlin: fix typos 2025-03-27 12:41:24 -04:00
Pieter Wuille
777179bc27 txgraph: rename group_data in ApplyDependencies 2025-03-27 12:41:24 -04:00
Pieter Wuille
b2ea365648 txgraph: Add Get{Ancestors,Descendants}Union functions (feature)
Like GetAncestors and GetDescendants, but for the union of multiple inputs.
2025-03-24 10:03:06 -04:00
Pieter Wuille
54bceddd3a txgraph: Multiple inputs to Get{Ancestors,Descendant}Refs (preparation)
This is a preparation for the next commit, which adds a feature to request
the Refs to multiple ancestors/descendants at once.
2025-03-24 10:03:06 -04:00
Pieter Wuille
aded047019 txgraph: Add CountDistinctClusters function (feature) 2025-03-24 10:03:06 -04:00