Bitcoin Core 30.99.0
P2P Digital Currency
txgraph.cpp
Go to the documentation of this file.
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 <txgraph.h>
6
7#include <cluster_linearize.h>
8#include <random.h>
9#include <util/bitset.h>
10#include <util/check.h>
11#include <util/feefrac.h>
12#include <util/vector.h>
13
14#include <compare>
15#include <functional>
16#include <memory>
17#include <set>
18#include <span>
19#include <unordered_set>
20#include <utility>
21
22namespace {
23
24using namespace cluster_linearize;
25
27static constexpr int MAX_LEVELS{2};
28
29// Forward declare the TxGraph implementation class.
30class TxGraphImpl;
31
33using LinearizationIndex = uint32_t;
35using ClusterSetIndex = uint32_t;
36
38enum class QualityLevel
39{
42 OVERSIZED_SINGLETON,
44 NEEDS_SPLIT_FIX,
46 NEEDS_SPLIT,
48 NEEDS_FIX,
50 NEEDS_RELINEARIZE,
52 ACCEPTABLE,
54 OPTIMAL,
57 NONE,
58};
59
61struct TrimTxData
62{
63 // Fields populated by Cluster::AppendTrimData(). These are immutable after TrimTxData
64 // construction.
66 FeePerWeight m_chunk_feerate;
68 TxGraph::GraphIndex m_index;
70 uint32_t m_tx_size;
71
72 // Fields only used internally by TxGraphImpl::Trim():
74 uint32_t m_deps_left;
76 uint32_t m_parent_count;
78 uint32_t m_parent_offset;
80 uint32_t m_children_count;
82 uint32_t m_children_offset;
83
84 // Fields only used internally by TxGraphImpl::Trim()'s union-find implementation, and only for
85 // transactions that are definitely included or definitely rejected.
86 //
87 // As transactions get processed, they get organized into trees which form partitions
88 // representing the would-be clusters up to that point. The root of each tree is a
89 // representative for that partition. See
90 // https://en.wikipedia.org/wiki/Disjoint-set_data_structure.
91 //
94 TrimTxData* m_uf_parent;
96 uint32_t m_uf_count;
98 uint64_t m_uf_size;
99};
100
102class Cluster
103{
104 friend class TxGraphImpl;
105 friend class BlockBuilderImpl;
106
107protected:
108 using GraphIndex = TxGraph::GraphIndex;
109 using SetType = BitSet<MAX_CLUSTER_COUNT_LIMIT>;
111 QualityLevel m_quality{QualityLevel::NONE};
113 ClusterSetIndex m_setindex{ClusterSetIndex(-1)};
116 uint64_t m_sequence;
117
118 explicit Cluster(uint64_t sequence) noexcept : m_sequence(sequence) {}
119
120public:
121 // Provide virtual destructor, for safe polymorphic usage inside std::unique_ptr.
122 virtual ~Cluster() = default;
123
124 // Cannot move or copy (would invalidate Cluster* in Locator and ClusterSet). */
125 Cluster(const Cluster&) = delete;
126 Cluster& operator=(const Cluster&) = delete;
127 Cluster(Cluster&&) = delete;
128 Cluster& operator=(Cluster&&) = delete;
129
130 // Generic helper functions.
131
133 bool IsAcceptable() const noexcept
134 {
135 return m_quality == QualityLevel::ACCEPTABLE || m_quality == QualityLevel::OPTIMAL;
136 }
138 bool IsTopological() const noexcept
139 {
140 return m_quality != QualityLevel::NEEDS_FIX && m_quality != QualityLevel::NEEDS_SPLIT_FIX;
141 }
143 bool IsOptimal() const noexcept
144 {
145 return m_quality == QualityLevel::OPTIMAL;
146 }
150 bool IsOversized() const noexcept { return m_quality == QualityLevel::OVERSIZED_SINGLETON; }
152 bool NeedsSplitting() const noexcept
153 {
154 return m_quality == QualityLevel::NEEDS_SPLIT || m_quality == QualityLevel::NEEDS_SPLIT_FIX;
155 }
156
158 virtual DepGraphIndex GetMinIntendedTxCount() const noexcept = 0;
160 virtual DepGraphIndex GetMaxTxCount() const noexcept = 0;
163 virtual size_t TotalMemoryUsage() const noexcept = 0;
165 virtual DepGraphIndex GetDepGraphIndexRange() const noexcept = 0;
167 virtual LinearizationIndex GetTxCount() const noexcept = 0;
169 virtual uint64_t GetTotalTxSize() const noexcept = 0;
171 virtual GraphIndex GetClusterEntry(DepGraphIndex index) const noexcept = 0;
174 virtual DepGraphIndex AppendTransaction(GraphIndex graph_idx, FeePerWeight feerate) noexcept = 0;
176 virtual void AddDependencies(SetType parents, DepGraphIndex child) noexcept = 0;
179 virtual void ExtractTransactions(const std::function<void (DepGraphIndex, GraphIndex, FeePerWeight)>& visit1_fn, const std::function<void (DepGraphIndex, GraphIndex, SetType)>& visit2_fn) noexcept = 0;
182 virtual int GetLevel(const TxGraphImpl& graph) const noexcept = 0;
184 virtual void UpdateMapping(DepGraphIndex cluster_idx, GraphIndex graph_idx) noexcept = 0;
186 virtual void Updated(TxGraphImpl& graph, int level) noexcept = 0;
188 virtual Cluster* CopyToStaging(TxGraphImpl& graph) const noexcept = 0;
190 virtual void GetConflicts(const TxGraphImpl& graph, std::vector<Cluster*>& out) const noexcept = 0;
193 virtual void MakeStagingTransactionsMissing(TxGraphImpl& graph) noexcept = 0;
195 virtual void Clear(TxGraphImpl& graph, int level) noexcept = 0;
197 virtual void MoveToMain(TxGraphImpl& graph) noexcept = 0;
199 virtual void Compact() noexcept = 0;
200
201 // Functions that implement the Cluster-specific side of internal TxGraphImpl mutations.
202
205 virtual void ApplyRemovals(TxGraphImpl& graph, int level, std::span<GraphIndex>& to_remove) noexcept = 0;
208 [[nodiscard]] virtual bool Split(TxGraphImpl& graph, int level) noexcept = 0;
210 virtual void Merge(TxGraphImpl& graph, int level, Cluster& cluster) noexcept = 0;
212 virtual void ApplyDependencies(TxGraphImpl& graph, int level, std::span<std::pair<GraphIndex, GraphIndex>> to_apply) noexcept = 0;
215 virtual std::pair<uint64_t, bool> Relinearize(TxGraphImpl& graph, int level, uint64_t max_iters) noexcept = 0;
217 virtual void AppendChunkFeerates(std::vector<FeeFrac>& ret) const noexcept = 0;
221 virtual uint64_t AppendTrimData(std::vector<TrimTxData>& ret, std::vector<std::pair<GraphIndex, GraphIndex>>& deps) const noexcept = 0;
222
223 // Functions that implement the Cluster-specific side of public TxGraph functions.
224
227 virtual void GetAncestorRefs(const TxGraphImpl& graph, std::span<std::pair<Cluster*, DepGraphIndex>>& args, std::vector<TxGraph::Ref*>& output) noexcept = 0;
230 virtual void GetDescendantRefs(const TxGraphImpl& graph, std::span<std::pair<Cluster*, DepGraphIndex>>& args, std::vector<TxGraph::Ref*>& output) noexcept = 0;
234 virtual bool GetClusterRefs(TxGraphImpl& graph, std::span<TxGraph::Ref*> range, LinearizationIndex start_pos) noexcept = 0;
236 virtual FeePerWeight GetIndividualFeerate(DepGraphIndex idx) noexcept = 0;
238 virtual void SetFee(TxGraphImpl& graph, int level, DepGraphIndex idx, int64_t fee) noexcept = 0;
239
240 // Debugging functions.
241
242 virtual void SanityCheck(const TxGraphImpl& graph, int level) const = 0;
243};
244
247class GenericClusterImpl final : public Cluster
248{
249 friend class TxGraphImpl;
251 DepGraph<SetType> m_depgraph;
255 std::vector<GraphIndex> m_mapping;
258 std::vector<DepGraphIndex> m_linearization;
259
260public:
262 static constexpr DepGraphIndex MIN_INTENDED_TX_COUNT{2};
264 static constexpr DepGraphIndex MAX_TX_COUNT{SetType::Size()};
265
266 GenericClusterImpl() noexcept = delete;
268 explicit GenericClusterImpl(uint64_t sequence) noexcept;
269
270 size_t TotalMemoryUsage() const noexcept final;
271 constexpr DepGraphIndex GetMinIntendedTxCount() const noexcept final { return MIN_INTENDED_TX_COUNT; }
272 constexpr DepGraphIndex GetMaxTxCount() const noexcept final { return MAX_TX_COUNT; }
273 DepGraphIndex GetDepGraphIndexRange() const noexcept final { return m_depgraph.PositionRange(); }
274 LinearizationIndex GetTxCount() const noexcept final { return m_linearization.size(); }
275 uint64_t GetTotalTxSize() const noexcept final;
276 GraphIndex GetClusterEntry(DepGraphIndex index) const noexcept final { return m_mapping[index]; }
277 DepGraphIndex AppendTransaction(GraphIndex graph_idx, FeePerWeight feerate) noexcept final;
278 void AddDependencies(SetType parents, DepGraphIndex child) noexcept final;
279 void ExtractTransactions(const std::function<void (DepGraphIndex, GraphIndex, FeePerWeight)>& visit1_fn, const std::function<void (DepGraphIndex, GraphIndex, SetType)>& visit2_fn) noexcept final;
280 int GetLevel(const TxGraphImpl& graph) const noexcept final;
281 void UpdateMapping(DepGraphIndex cluster_idx, GraphIndex graph_idx) noexcept final { m_mapping[cluster_idx] = graph_idx; }
282 void Updated(TxGraphImpl& graph, int level) noexcept final;
283 Cluster* CopyToStaging(TxGraphImpl& graph) const noexcept final;
284 void GetConflicts(const TxGraphImpl& graph, std::vector<Cluster*>& out) const noexcept final;
285 void MakeStagingTransactionsMissing(TxGraphImpl& graph) noexcept final;
286 void Clear(TxGraphImpl& graph, int level) noexcept final;
287 void MoveToMain(TxGraphImpl& graph) noexcept final;
288 void Compact() noexcept final;
289 void ApplyRemovals(TxGraphImpl& graph, int level, std::span<GraphIndex>& to_remove) noexcept final;
290 [[nodiscard]] bool Split(TxGraphImpl& graph, int level) noexcept final;
291 void Merge(TxGraphImpl& graph, int level, Cluster& cluster) noexcept final;
292 void ApplyDependencies(TxGraphImpl& graph, int level, std::span<std::pair<GraphIndex, GraphIndex>> to_apply) noexcept final;
293 std::pair<uint64_t, bool> Relinearize(TxGraphImpl& graph, int level, uint64_t max_iters) noexcept final;
294 void AppendChunkFeerates(std::vector<FeeFrac>& ret) const noexcept final;
295 uint64_t AppendTrimData(std::vector<TrimTxData>& ret, std::vector<std::pair<GraphIndex, GraphIndex>>& deps) const noexcept final;
296 void GetAncestorRefs(const TxGraphImpl& graph, std::span<std::pair<Cluster*, DepGraphIndex>>& args, std::vector<TxGraph::Ref*>& output) noexcept final;
297 void GetDescendantRefs(const TxGraphImpl& graph, std::span<std::pair<Cluster*, DepGraphIndex>>& args, std::vector<TxGraph::Ref*>& output) noexcept final;
298 bool GetClusterRefs(TxGraphImpl& graph, std::span<TxGraph::Ref*> range, LinearizationIndex start_pos) noexcept final;
299 FeePerWeight GetIndividualFeerate(DepGraphIndex idx) noexcept final;
300 void SetFee(TxGraphImpl& graph, int level, DepGraphIndex idx, int64_t fee) noexcept final;
301 void SanityCheck(const TxGraphImpl& graph, int level) const final;
302};
303
305class SingletonClusterImpl final : public Cluster
306{
307 friend class TxGraphImpl;
308
310 FeePerWeight m_feerate;
312 static constexpr auto NO_GRAPH_INDEX = GraphIndex(-1);
314 GraphIndex m_graph_index = NO_GRAPH_INDEX;
315
316public:
318 static constexpr DepGraphIndex MIN_INTENDED_TX_COUNT{1};
320 static constexpr DepGraphIndex MAX_TX_COUNT{1};
321
322 SingletonClusterImpl() noexcept = delete;
324 explicit SingletonClusterImpl(uint64_t sequence) noexcept : Cluster(sequence) {}
325
326 size_t TotalMemoryUsage() const noexcept final;
327 constexpr DepGraphIndex GetMinIntendedTxCount() const noexcept final { return MIN_INTENDED_TX_COUNT; }
328 constexpr DepGraphIndex GetMaxTxCount() const noexcept final { return MAX_TX_COUNT; }
329 LinearizationIndex GetTxCount() const noexcept final { return m_graph_index != NO_GRAPH_INDEX; }
330 DepGraphIndex GetDepGraphIndexRange() const noexcept final { return GetTxCount(); }
331 uint64_t GetTotalTxSize() const noexcept final { return GetTxCount() ? m_feerate.size : 0; }
332 GraphIndex GetClusterEntry(DepGraphIndex index) const noexcept final { Assume(index == 0); Assume(GetTxCount()); return m_graph_index; }
333 DepGraphIndex AppendTransaction(GraphIndex graph_idx, FeePerWeight feerate) noexcept final;
334 void AddDependencies(SetType parents, DepGraphIndex child) noexcept final;
335 void ExtractTransactions(const std::function<void (DepGraphIndex, GraphIndex, FeePerWeight)>& visit1_fn, const std::function<void (DepGraphIndex, GraphIndex, SetType)>& visit2_fn) noexcept final;
336 int GetLevel(const TxGraphImpl& graph) const noexcept final;
337 void UpdateMapping(DepGraphIndex cluster_idx, GraphIndex graph_idx) noexcept final { Assume(cluster_idx == 0); m_graph_index = graph_idx; }
338 void Updated(TxGraphImpl& graph, int level) noexcept final;
339 Cluster* CopyToStaging(TxGraphImpl& graph) const noexcept final;
340 void GetConflicts(const TxGraphImpl& graph, std::vector<Cluster*>& out) const noexcept final;
341 void MakeStagingTransactionsMissing(TxGraphImpl& graph) noexcept final;
342 void Clear(TxGraphImpl& graph, int level) noexcept final;
343 void MoveToMain(TxGraphImpl& graph) noexcept final;
344 void Compact() noexcept final;
345 void ApplyRemovals(TxGraphImpl& graph, int level, std::span<GraphIndex>& to_remove) noexcept final;
346 [[nodiscard]] bool Split(TxGraphImpl& graph, int level) noexcept final;
347 void Merge(TxGraphImpl& graph, int level, Cluster& cluster) noexcept final;
348 void ApplyDependencies(TxGraphImpl& graph, int level, std::span<std::pair<GraphIndex, GraphIndex>> to_apply) noexcept final;
349 std::pair<uint64_t, bool> Relinearize(TxGraphImpl& graph, int level, uint64_t max_iters) noexcept final;
350 void AppendChunkFeerates(std::vector<FeeFrac>& ret) const noexcept final;
351 uint64_t AppendTrimData(std::vector<TrimTxData>& ret, std::vector<std::pair<GraphIndex, GraphIndex>>& deps) const noexcept final;
352 void GetAncestorRefs(const TxGraphImpl& graph, std::span<std::pair<Cluster*, DepGraphIndex>>& args, std::vector<TxGraph::Ref*>& output) noexcept final;
353 void GetDescendantRefs(const TxGraphImpl& graph, std::span<std::pair<Cluster*, DepGraphIndex>>& args, std::vector<TxGraph::Ref*>& output) noexcept final;
354 bool GetClusterRefs(TxGraphImpl& graph, std::span<TxGraph::Ref*> range, LinearizationIndex start_pos) noexcept final;
355 FeePerWeight GetIndividualFeerate(DepGraphIndex idx) noexcept final;
356 void SetFee(TxGraphImpl& graph, int level, DepGraphIndex idx, int64_t fee) noexcept final;
357 void SanityCheck(const TxGraphImpl& graph, int level) const final;
358};
359
383class TxGraphImpl final : public TxGraph
384{
385 friend class Cluster;
386 friend class SingletonClusterImpl;
387 friend class GenericClusterImpl;
388 friend class BlockBuilderImpl;
389private:
391 FastRandomContext m_rng;
393 const DepGraphIndex m_max_cluster_count;
395 const uint64_t m_max_cluster_size;
398 const uint64_t m_acceptable_iters;
399
401 struct GroupEntry
402 {
404 uint32_t m_cluster_offset;
406 uint32_t m_cluster_count;
408 uint32_t m_deps_offset;
410 uint32_t m_deps_count;
411 };
412
414 struct GroupData
415 {
417 std::vector<GroupEntry> m_groups;
419 std::vector<Cluster*> m_group_clusters;
420 };
421
423 struct ClusterSet
424 {
426 std::array<std::vector<std::unique_ptr<Cluster>>, int(QualityLevel::NONE)> m_clusters;
428 std::vector<GraphIndex> m_to_remove;
431 std::vector<std::pair<GraphIndex, GraphIndex>> m_deps_to_add;
433 std::optional<GroupData> m_group_data = GroupData{};
437 std::vector<GraphIndex> m_removed;
440 GraphIndex m_txcount{0};
442 GraphIndex m_txcount_oversized{0};
444 std::optional<bool> m_oversized{false};
447 size_t m_cluster_usage{0};
448
449 ClusterSet() noexcept = default;
450 };
451
453 ClusterSet m_main_clusterset;
455 std::optional<ClusterSet> m_staging_clusterset;
457 uint64_t m_next_sequence_counter{0};
458
460 struct ChunkData
461 {
463 mutable GraphIndex m_graph_index;
465 LinearizationIndex m_chunk_count;
466
467 ChunkData(GraphIndex graph_index, LinearizationIndex chunk_count) noexcept :
468 m_graph_index{graph_index}, m_chunk_count{chunk_count} {}
469 };
470
472 static std::strong_ordering CompareClusters(Cluster* a, Cluster* b) noexcept
473 {
474 // The nullptr pointer compares before everything else.
475 if (a == nullptr || b == nullptr) {
476 return (a != nullptr) <=> (b != nullptr);
477 }
478 // If neither pointer is nullptr, compare the Clusters' sequence numbers.
479 Assume(a == b || a->m_sequence != b->m_sequence);
480 return a->m_sequence <=> b->m_sequence;
481 }
482
484 std::strong_ordering CompareMainTransactions(GraphIndex a, GraphIndex b) const noexcept
485 {
486 Assume(a < m_entries.size() && b < m_entries.size());
487 const auto& entry_a = m_entries[a];
488 const auto& entry_b = m_entries[b];
489 // Compare chunk feerates, and return result if it differs.
490 auto feerate_cmp = FeeRateCompare(entry_b.m_main_chunk_feerate, entry_a.m_main_chunk_feerate);
491 if (feerate_cmp < 0) return std::strong_ordering::less;
492 if (feerate_cmp > 0) return std::strong_ordering::greater;
493 // Compare Cluster m_sequence as tie-break for equal chunk feerates.
494 const auto& locator_a = entry_a.m_locator[0];
495 const auto& locator_b = entry_b.m_locator[0];
496 Assume(locator_a.IsPresent() && locator_b.IsPresent());
497 if (locator_a.cluster != locator_b.cluster) {
498 return CompareClusters(locator_a.cluster, locator_b.cluster);
499 }
500 // As final tie-break, compare position within cluster linearization.
501 return entry_a.m_main_lin_index <=> entry_b.m_main_lin_index;
502 }
503
505 class ChunkOrder
506 {
507 const TxGraphImpl* const m_graph;
508 public:
509 explicit ChunkOrder(const TxGraphImpl* graph) : m_graph(graph) {}
510
511 bool operator()(const ChunkData& a, const ChunkData& b) const noexcept
512 {
513 return m_graph->CompareMainTransactions(a.m_graph_index, b.m_graph_index) < 0;
514 }
515 };
516
518 using ChunkIndex = std::set<ChunkData, ChunkOrder>;
519
522 ChunkIndex m_main_chunkindex;
524 size_t m_main_chunkindex_observers{0};
526 std::vector<ChunkIndex::node_type> m_main_chunkindex_discarded;
527
558 struct Locator
559 {
561 Cluster* cluster{nullptr};
563 DepGraphIndex index{0};
564
566 void SetMissing() noexcept { cluster = nullptr; index = 0; }
568 void SetRemoved() noexcept { cluster = nullptr; index = DepGraphIndex(-1); }
570 void SetPresent(Cluster* c, DepGraphIndex i) noexcept { cluster = c; index = i; }
572 bool IsMissing() const noexcept { return cluster == nullptr && index == 0; }
574 bool IsRemoved() const noexcept { return cluster == nullptr && index == DepGraphIndex(-1); }
576 bool IsPresent() const noexcept { return cluster != nullptr; }
577 };
578
580 struct Entry
581 {
583 Ref* m_ref{nullptr};
586 ChunkIndex::iterator m_main_chunkindex_iterator;
588 Locator m_locator[MAX_LEVELS];
590 FeePerWeight m_main_chunk_feerate;
592 LinearizationIndex m_main_lin_index;
593 };
594
596 std::vector<Entry> m_entries;
597
599 std::vector<GraphIndex> m_unlinked;
600
601public:
603 explicit TxGraphImpl(DepGraphIndex max_cluster_count, uint64_t max_cluster_size, uint64_t acceptable_iters) noexcept :
604 m_max_cluster_count(max_cluster_count),
605 m_max_cluster_size(max_cluster_size),
606 m_acceptable_iters(acceptable_iters),
607 m_main_chunkindex(ChunkOrder(this))
608 {
609 Assume(max_cluster_count >= 1);
610 Assume(max_cluster_count <= MAX_CLUSTER_COUNT_LIMIT);
611 }
612
614 ~TxGraphImpl() noexcept;
615
616 // Cannot move or copy (would invalidate TxGraphImpl* in Ref, MiningOrder, EvictionOrder).
617 TxGraphImpl(const TxGraphImpl&) = delete;
618 TxGraphImpl& operator=(const TxGraphImpl&) = delete;
619 TxGraphImpl(TxGraphImpl&&) = delete;
620 TxGraphImpl& operator=(TxGraphImpl&&) = delete;
621
622 // Simple helper functions.
623
625 void SwapIndexes(GraphIndex a, GraphIndex b) noexcept;
628 Cluster* FindCluster(GraphIndex idx, int level) const noexcept { return FindClusterAndLevel(idx, level).first; }
630 std::pair<Cluster*, int> FindClusterAndLevel(GraphIndex idx, int level) const noexcept;
632 std::unique_ptr<Cluster> ExtractCluster(int level, QualityLevel quality, ClusterSetIndex setindex) noexcept;
634 void DeleteCluster(Cluster& cluster, int level) noexcept;
636 ClusterSetIndex InsertCluster(int level, std::unique_ptr<Cluster>&& cluster, QualityLevel quality) noexcept;
638 void SetClusterQuality(int level, QualityLevel old_quality, ClusterSetIndex old_index, QualityLevel new_quality) noexcept;
640 int GetTopLevel() const noexcept { return m_staging_clusterset.has_value(); }
642 int GetSpecifiedLevel(Level level) const noexcept { return level == Level::TOP && m_staging_clusterset.has_value(); }
644 ClusterSet& GetClusterSet(int level) noexcept;
645 const ClusterSet& GetClusterSet(int level) const noexcept;
649 void ClearLocator(int level, GraphIndex index, bool oversized_tx) noexcept;
651 std::vector<Cluster*> GetConflicts() const noexcept;
653 void ClearChunkData(Entry& entry) noexcept;
655 void CreateChunkData(GraphIndex idx, LinearizationIndex chunk_count) noexcept;
657 std::unique_ptr<GenericClusterImpl> CreateEmptyGenericCluster() noexcept
658 {
659 return std::make_unique<GenericClusterImpl>(m_next_sequence_counter++);
660 }
662 std::unique_ptr<SingletonClusterImpl> CreateEmptySingletonCluster() noexcept
663 {
664 return std::make_unique<SingletonClusterImpl>(m_next_sequence_counter++);
665 }
668 std::unique_ptr<Cluster> CreateEmptyCluster(DepGraphIndex tx_count) noexcept
669 {
670 if (tx_count >= SingletonClusterImpl::MIN_INTENDED_TX_COUNT && tx_count <= SingletonClusterImpl::MAX_TX_COUNT) {
671 return CreateEmptySingletonCluster();
672 }
673 if (tx_count >= GenericClusterImpl::MIN_INTENDED_TX_COUNT && tx_count <= GenericClusterImpl::MAX_TX_COUNT) {
674 return CreateEmptyGenericCluster();
675 }
676 assert(false);
677 return {};
678 }
679
680 // Functions for handling Refs.
681
683 void UpdateRef(GraphIndex idx, Ref& new_location) noexcept final
684 {
685 auto& entry = m_entries[idx];
686 Assume(entry.m_ref != nullptr);
687 entry.m_ref = &new_location;
688 }
689
691 void UnlinkRef(GraphIndex idx) noexcept final
692 {
693 auto& entry = m_entries[idx];
694 Assume(entry.m_ref != nullptr);
695 Assume(m_main_chunkindex_observers == 0 || !entry.m_locator[0].IsPresent());
696 entry.m_ref = nullptr;
697 // Mark the transaction as to be removed in all levels where it explicitly or implicitly
698 // exists.
699 bool exists_anywhere{false};
700 bool exists{false};
701 for (int level = 0; level <= GetTopLevel(); ++level) {
702 if (entry.m_locator[level].IsPresent()) {
703 exists_anywhere = true;
704 exists = true;
705 } else if (entry.m_locator[level].IsRemoved()) {
706 exists = false;
707 }
708 if (exists) {
709 auto& clusterset = GetClusterSet(level);
710 clusterset.m_to_remove.push_back(idx);
711 // Force recomputation of grouping data.
712 clusterset.m_group_data = std::nullopt;
713 // Do not wipe the oversized state of main if staging exists. The reason for this
714 // is that the alternative would mean that cluster merges may need to be applied to
715 // a formerly-oversized main graph while staging exists (to satisfy chunk feerate
716 // queries into main, for example), and such merges could conflict with pulls of
717 // some of their constituents into staging.
718 if (level == GetTopLevel() && clusterset.m_oversized == true) {
719 clusterset.m_oversized = std::nullopt;
720 }
721 }
722 }
723 m_unlinked.push_back(idx);
724 if (!exists_anywhere) Compact();
725 }
726
727 // Functions related to various normalization/application steps.
731 void Compact() noexcept;
735 Cluster* PullIn(Cluster* cluster, int level) noexcept;
738 void ApplyRemovals(int up_to_level) noexcept;
740 void Split(Cluster& cluster, int level) noexcept;
742 void SplitAll(int up_to_level) noexcept;
744 void GroupClusters(int level) noexcept;
746 void Merge(std::span<Cluster*> to_merge, int level) noexcept;
748 void ApplyDependencies(int level) noexcept;
750 void MakeAcceptable(Cluster& cluster, int level) noexcept;
752 void MakeAllAcceptable(int level) noexcept;
753
754 // Implementations for the public TxGraph interface.
755
756 Ref AddTransaction(const FeePerWeight& feerate) noexcept final;
757 void RemoveTransaction(const Ref& arg) noexcept final;
758 void AddDependency(const Ref& parent, const Ref& child) noexcept final;
759 void SetTransactionFee(const Ref&, int64_t fee) noexcept final;
760
761 bool DoWork(uint64_t iters) noexcept final;
762
763 void StartStaging() noexcept final;
764 void CommitStaging() noexcept final;
765 void AbortStaging() noexcept final;
766 bool HaveStaging() const noexcept final { return m_staging_clusterset.has_value(); }
767
768 bool Exists(const Ref& arg, Level level) noexcept final;
769 FeePerWeight GetMainChunkFeerate(const Ref& arg) noexcept final;
770 FeePerWeight GetIndividualFeerate(const Ref& arg) noexcept final;
771 std::vector<Ref*> GetCluster(const Ref& arg, Level level) noexcept final;
772 std::vector<Ref*> GetAncestors(const Ref& arg, Level level) noexcept final;
773 std::vector<Ref*> GetDescendants(const Ref& arg, Level level) noexcept final;
774 std::vector<Ref*> GetAncestorsUnion(std::span<const Ref* const> args, Level level) noexcept final;
775 std::vector<Ref*> GetDescendantsUnion(std::span<const Ref* const> args, Level level) noexcept final;
776 GraphIndex GetTransactionCount(Level level) noexcept final;
777 bool IsOversized(Level level) noexcept final;
778 std::strong_ordering CompareMainOrder(const Ref& a, const Ref& b) noexcept final;
779 GraphIndex CountDistinctClusters(std::span<const Ref* const> refs, Level level) noexcept final;
780 std::pair<std::vector<FeeFrac>, std::vector<FeeFrac>> GetMainStagingDiagrams() noexcept final;
781 std::vector<Ref*> Trim() noexcept final;
782
783 std::unique_ptr<BlockBuilder> GetBlockBuilder() noexcept final;
784 std::pair<std::vector<Ref*>, FeePerWeight> GetWorstMainChunk() noexcept final;
785
786 size_t GetMainMemoryUsage() noexcept final;
787
788 void SanityCheck() const final;
789};
790
791TxGraphImpl::ClusterSet& TxGraphImpl::GetClusterSet(int level) noexcept
792{
793 if (level == 0) return m_main_clusterset;
794 Assume(level == 1);
795 Assume(m_staging_clusterset.has_value());
796 return *m_staging_clusterset;
797}
798
799const TxGraphImpl::ClusterSet& TxGraphImpl::GetClusterSet(int level) const noexcept
800{
801 if (level == 0) return m_main_clusterset;
802 Assume(level == 1);
803 Assume(m_staging_clusterset.has_value());
804 return *m_staging_clusterset;
805}
806
808class BlockBuilderImpl final : public TxGraph::BlockBuilder
809{
812 TxGraphImpl* const m_graph;
814 std::unordered_set<uint64_t> m_excluded_clusters;
816 TxGraphImpl::ChunkIndex::const_iterator m_cur_iter;
819 Cluster* m_cur_cluster;
821 bool m_known_end_of_cluster;
822
823 // Move m_cur_iter / m_cur_cluster to the next acceptable chunk.
824 void Next() noexcept;
825
826public:
828 BlockBuilderImpl(TxGraphImpl& graph) noexcept;
829
830 // Implement the public interface.
831 ~BlockBuilderImpl() final;
832 std::optional<std::pair<std::vector<TxGraph::Ref*>, FeePerWeight>> GetCurrentChunk() noexcept final;
833 void Include() noexcept final;
834 void Skip() noexcept final;
835};
836
837void TxGraphImpl::ClearChunkData(Entry& entry) noexcept
838{
839 if (entry.m_main_chunkindex_iterator != m_main_chunkindex.end()) {
840 Assume(m_main_chunkindex_observers == 0);
841 // If the Entry has a non-empty m_main_chunkindex_iterator, extract it, and move the handle
842 // to the cache of discarded chunkindex entries.
843 m_main_chunkindex_discarded.emplace_back(m_main_chunkindex.extract(entry.m_main_chunkindex_iterator));
844 entry.m_main_chunkindex_iterator = m_main_chunkindex.end();
845 }
846}
847
848void TxGraphImpl::CreateChunkData(GraphIndex idx, LinearizationIndex chunk_count) noexcept
849{
850 auto& entry = m_entries[idx];
851 if (!m_main_chunkindex_discarded.empty()) {
852 // Reuse an discarded node handle.
853 auto& node = m_main_chunkindex_discarded.back().value();
854 node.m_graph_index = idx;
855 node.m_chunk_count = chunk_count;
856 auto insert_result = m_main_chunkindex.insert(std::move(m_main_chunkindex_discarded.back()));
857 Assume(insert_result.inserted);
858 entry.m_main_chunkindex_iterator = insert_result.position;
859 m_main_chunkindex_discarded.pop_back();
860 } else {
861 // Construct a new entry.
862 auto emplace_result = m_main_chunkindex.emplace(idx, chunk_count);
863 Assume(emplace_result.second);
864 entry.m_main_chunkindex_iterator = emplace_result.first;
865 }
866}
867
868size_t GenericClusterImpl::TotalMemoryUsage() const noexcept
869{
870 return // Dynamic memory allocated in this Cluster.
871 memusage::DynamicUsage(m_mapping) + memusage::DynamicUsage(m_linearization) +
872 // Dynamic memory usage inside m_depgraph.
873 m_depgraph.DynamicMemoryUsage() +
874 // Memory usage of the allocated Cluster itself.
875 memusage::MallocUsage(sizeof(GenericClusterImpl)) +
876 // Memory usage of the ClusterSet::m_clusters entry.
877 sizeof(std::unique_ptr<Cluster>);
878}
879
880size_t SingletonClusterImpl::TotalMemoryUsage() const noexcept
881{
882 return // Memory usage of the allocated SingletonClusterImpl itself.
883 memusage::MallocUsage(sizeof(SingletonClusterImpl)) +
884 // Memory usage of the ClusterSet::m_clusters entry.
885 sizeof(std::unique_ptr<Cluster>);
886}
887
888uint64_t GenericClusterImpl::GetTotalTxSize() const noexcept
889{
890 uint64_t ret{0};
891 for (auto i : m_linearization) {
892 ret += m_depgraph.FeeRate(i).size;
893 }
894 return ret;
895}
896
897DepGraphIndex GenericClusterImpl::AppendTransaction(GraphIndex graph_idx, FeePerWeight feerate) noexcept
898{
899 Assume(graph_idx != GraphIndex(-1));
900 auto ret = m_depgraph.AddTransaction(feerate);
901 m_mapping.push_back(graph_idx);
902 m_linearization.push_back(ret);
903 return ret;
904}
905
906DepGraphIndex SingletonClusterImpl::AppendTransaction(GraphIndex graph_idx, FeePerWeight feerate) noexcept
907{
908 Assume(!GetTxCount());
909 m_graph_index = graph_idx;
910 m_feerate = feerate;
911 return 0;
912}
913
914void GenericClusterImpl::AddDependencies(SetType parents, DepGraphIndex child) noexcept
915{
916 m_depgraph.AddDependencies(parents, child);
917}
918
919void SingletonClusterImpl::AddDependencies(SetType parents, DepGraphIndex child) noexcept
920{
921 // Singletons cannot have any dependencies.
922 Assume(child == 0);
923 Assume(parents == SetType{} || parents == SetType::Fill(0));
924}
925
926void GenericClusterImpl::ExtractTransactions(const std::function<void (DepGraphIndex, GraphIndex, FeePerWeight)>& visit1_fn, const std::function<void (DepGraphIndex, GraphIndex, SetType)>& visit2_fn) noexcept
927{
928 for (auto pos : m_linearization) {
929 visit1_fn(pos, m_mapping[pos], FeePerWeight::FromFeeFrac(m_depgraph.FeeRate(pos)));
930 }
931 for (auto pos : m_linearization) {
932 visit2_fn(pos, m_mapping[pos], m_depgraph.GetReducedParents(pos));
933 }
934 // Purge this Cluster, now that everything has been moved.
935 m_depgraph = DepGraph<SetType>{};
936 m_linearization.clear();
937 m_mapping.clear();
938}
939
940void SingletonClusterImpl::ExtractTransactions(const std::function<void (DepGraphIndex, GraphIndex, FeePerWeight)>& visit1_fn, const std::function<void (DepGraphIndex, GraphIndex, SetType)>& visit2_fn) noexcept
941{
942 if (GetTxCount()) {
943 visit1_fn(0, m_graph_index, m_feerate);
944 visit2_fn(0, m_graph_index, SetType{});
945 m_graph_index = NO_GRAPH_INDEX;
946 }
947}
948
949int GenericClusterImpl::GetLevel(const TxGraphImpl& graph) const noexcept
950{
951 // GetLevel() does not work for empty Clusters.
952 if (!Assume(!m_linearization.empty())) return -1;
953
954 // Pick an arbitrary Entry that occurs in this Cluster.
955 const auto& entry = graph.m_entries[m_mapping[m_linearization.front()]];
956 // See if there is a level whose Locator matches this Cluster, if so return that level.
957 for (int level = 0; level < MAX_LEVELS; ++level) {
958 if (entry.m_locator[level].cluster == this) return level;
959 }
960 // Given that we started with an Entry that occurs in this Cluster, one of its Locators must
961 // point back to it.
962 assert(false);
963 return -1;
964}
965
966int SingletonClusterImpl::GetLevel(const TxGraphImpl& graph) const noexcept
967{
968 // GetLevel() does not work for empty Clusters.
969 if (!Assume(GetTxCount())) return -1;
970
971 // Get the Entry in this Cluster.
972 const auto& entry = graph.m_entries[m_graph_index];
973 // See if there is a level whose Locator matches this Cluster, if so return that level.
974 for (int level = 0; level < MAX_LEVELS; ++level) {
975 if (entry.m_locator[level].cluster == this) return level;
976 }
977 // Given that we started with an Entry that occurs in this Cluster, one of its Locators must
978 // point back to it.
979 assert(false);
980 return -1;
981}
982
983void TxGraphImpl::ClearLocator(int level, GraphIndex idx, bool oversized_tx) noexcept
984{
985 auto& entry = m_entries[idx];
986 auto& clusterset = GetClusterSet(level);
987 Assume(entry.m_locator[level].IsPresent());
988 // Change the locator from Present to Missing or Removed.
989 if (level == 0 || !entry.m_locator[level - 1].IsPresent()) {
990 entry.m_locator[level].SetMissing();
991 } else {
992 entry.m_locator[level].SetRemoved();
993 clusterset.m_removed.push_back(idx);
994 }
995 // Update the transaction count.
996 --clusterset.m_txcount;
997 clusterset.m_txcount_oversized -= oversized_tx;
998 // If clearing main, adjust the status of Locators of this transaction in staging, if it exists.
999 if (level == 0 && GetTopLevel() == 1) {
1000 if (entry.m_locator[1].IsRemoved()) {
1001 entry.m_locator[1].SetMissing();
1002 } else if (!entry.m_locator[1].IsPresent()) {
1003 --m_staging_clusterset->m_txcount;
1004 m_staging_clusterset->m_txcount_oversized -= oversized_tx;
1005 }
1006 }
1007 if (level == 0) ClearChunkData(entry);
1008}
1009
1010void GenericClusterImpl::Updated(TxGraphImpl& graph, int level) noexcept
1011{
1012 // Update all the Locators for this Cluster's Entry objects.
1013 for (DepGraphIndex idx : m_linearization) {
1014 auto& entry = graph.m_entries[m_mapping[idx]];
1015 // Discard any potential ChunkData prior to modifying the Cluster (as that could
1016 // invalidate its ordering).
1017 if (level == 0) graph.ClearChunkData(entry);
1018 entry.m_locator[level].SetPresent(this, idx);
1019 }
1020 // If this is for the main graph (level = 0), and the Cluster's quality is ACCEPTABLE or
1021 // OPTIMAL, compute its chunking and store its information in the Entry's m_main_lin_index
1022 // and m_main_chunk_feerate. These fields are only accessed after making the entire graph
1023 // ACCEPTABLE, so it is pointless to compute these if we haven't reached that quality level
1024 // yet.
1025 if (level == 0 && IsAcceptable()) {
1026 auto chunking = ChunkLinearizationInfo(m_depgraph, m_linearization);
1027 LinearizationIndex lin_idx{0};
1028 // Iterate over the chunks.
1029 for (unsigned chunk_idx = 0; chunk_idx < chunking.size(); ++chunk_idx) {
1030 auto& chunk = chunking[chunk_idx];
1031 auto chunk_count = chunk.transactions.Count();
1032 Assume(chunk_count > 0);
1033 // Iterate over the transactions in the linearization, which must match those in chunk.
1034 while (true) {
1035 DepGraphIndex idx = m_linearization[lin_idx];
1036 GraphIndex graph_idx = m_mapping[idx];
1037 auto& entry = graph.m_entries[graph_idx];
1038 entry.m_main_lin_index = lin_idx++;
1039 entry.m_main_chunk_feerate = FeePerWeight::FromFeeFrac(chunk.feerate);
1040 Assume(chunk.transactions[idx]);
1041 chunk.transactions.Reset(idx);
1042 if (chunk.transactions.None()) {
1043 // Last transaction in the chunk.
1044 if (chunk_count == 1 && chunk_idx + 1 == chunking.size()) {
1045 // If this is the final chunk of the cluster, and it contains just a single
1046 // transaction (which will always be true for the very common singleton
1047 // clusters), store the special value -1 as chunk count.
1048 chunk_count = LinearizationIndex(-1);
1049 }
1050 graph.CreateChunkData(graph_idx, chunk_count);
1051 break;
1052 }
1053 }
1054 }
1055 }
1056}
1057
1058void SingletonClusterImpl::Updated(TxGraphImpl& graph, int level) noexcept
1059{
1060 // Don't do anything if this is empty.
1061 if (GetTxCount() == 0) return;
1062
1063 auto& entry = graph.m_entries[m_graph_index];
1064 // Discard any potential ChunkData prior to modifying the Cluster (as that could
1065 // invalidate its ordering).
1066 if (level == 0) graph.ClearChunkData(entry);
1067 entry.m_locator[level].SetPresent(this, 0);
1068 // If this is for the main graph (level = 0), compute its chunking and store its information in
1069 // the Entry's m_main_lin_index and m_main_chunk_feerate.
1070 if (level == 0 && IsAcceptable()) {
1071 entry.m_main_lin_index = 0;
1072 entry.m_main_chunk_feerate = m_feerate;
1073 // Always use the special LinearizationIndex(-1), indicating singleton chunk at end of
1074 // Cluster, here.
1075 graph.CreateChunkData(m_graph_index, LinearizationIndex(-1));
1076 }
1077}
1078
1079void GenericClusterImpl::GetConflicts(const TxGraphImpl& graph, std::vector<Cluster*>& out) const noexcept
1080{
1081 for (auto i : m_linearization) {
1082 auto& entry = graph.m_entries[m_mapping[i]];
1083 // For every transaction Entry in this Cluster, if it also exists in a lower-level Cluster,
1084 // then that Cluster conflicts.
1085 if (entry.m_locator[0].IsPresent()) {
1086 out.push_back(entry.m_locator[0].cluster);
1087 }
1088 }
1089}
1090
1091void SingletonClusterImpl::GetConflicts(const TxGraphImpl& graph, std::vector<Cluster*>& out) const noexcept
1092{
1093 // Empty clusters have no conflicts.
1094 if (GetTxCount() == 0) return;
1095
1096 auto& entry = graph.m_entries[m_graph_index];
1097 // If the transaction in this Cluster also exists in a lower-level Cluster, then that Cluster
1098 // conflicts.
1099 if (entry.m_locator[0].IsPresent()) {
1100 out.push_back(entry.m_locator[0].cluster);
1101 }
1102}
1103
1104std::vector<Cluster*> TxGraphImpl::GetConflicts() const noexcept
1105{
1106 Assume(GetTopLevel() == 1);
1107 auto& clusterset = GetClusterSet(1);
1108 std::vector<Cluster*> ret;
1109 // All main Clusters containing transactions in m_removed (so (P,R) ones) are conflicts.
1110 for (auto i : clusterset.m_removed) {
1111 auto& entry = m_entries[i];
1112 if (entry.m_locator[0].IsPresent()) {
1113 ret.push_back(entry.m_locator[0].cluster);
1114 }
1115 }
1116 // Then go over all Clusters at this level, and find their conflicts (the (P,P) ones).
1117 for (int quality = 0; quality < int(QualityLevel::NONE); ++quality) {
1118 auto& clusters = clusterset.m_clusters[quality];
1119 for (const auto& cluster : clusters) {
1120 cluster->GetConflicts(*this, ret);
1121 }
1122 }
1123 // Deduplicate the result (the same Cluster may appear multiple times).
1124 std::sort(ret.begin(), ret.end(), [](Cluster* a, Cluster* b) noexcept { return CompareClusters(a, b) < 0; });
1125 ret.erase(std::unique(ret.begin(), ret.end()), ret.end());
1126 return ret;
1127}
1128
1129Cluster* GenericClusterImpl::CopyToStaging(TxGraphImpl& graph) const noexcept
1130{
1131 // Construct an empty Cluster.
1132 auto ret = graph.CreateEmptyGenericCluster();
1133 auto ptr = ret.get();
1134 // Copy depgraph, mapping, and linearization.
1135 ptr->m_depgraph = m_depgraph;
1136 ptr->m_mapping = m_mapping;
1137 ptr->m_linearization = m_linearization;
1138 // Insert the new Cluster into the graph.
1139 graph.InsertCluster(/*level=*/1, std::move(ret), m_quality);
1140 // Update its Locators.
1141 ptr->Updated(graph, /*level=*/1);
1142 // Update memory usage.
1143 graph.GetClusterSet(/*level=*/1).m_cluster_usage += ptr->TotalMemoryUsage();
1144 return ptr;
1145}
1146
1147Cluster* SingletonClusterImpl::CopyToStaging(TxGraphImpl& graph) const noexcept
1148{
1149 // Construct an empty Cluster.
1150 auto ret = graph.CreateEmptySingletonCluster();
1151 auto ptr = ret.get();
1152 // Copy data.
1153 ptr->m_graph_index = m_graph_index;
1154 ptr->m_feerate = m_feerate;
1155 // Insert the new Cluster into the graph.
1156 graph.InsertCluster(/*level=*/1, std::move(ret), m_quality);
1157 // Update its Locators.
1158 ptr->Updated(graph, /*level=*/1);
1159 // Update memory usage.
1160 graph.GetClusterSet(/*level=*/1).m_cluster_usage += ptr->TotalMemoryUsage();
1161 return ptr;
1162}
1163
1164void GenericClusterImpl::ApplyRemovals(TxGraphImpl& graph, int level, std::span<GraphIndex>& to_remove) noexcept
1165{
1166 // Iterate over the prefix of to_remove that applies to this cluster.
1167 Assume(!to_remove.empty());
1168 SetType todo;
1169 graph.GetClusterSet(level).m_cluster_usage -= TotalMemoryUsage();
1170 do {
1171 GraphIndex idx = to_remove.front();
1172 Assume(idx < graph.m_entries.size());
1173 auto& entry = graph.m_entries[idx];
1174 auto& locator = entry.m_locator[level];
1175 // Stop once we hit an entry that applies to another Cluster.
1176 if (locator.cluster != this) break;
1177 // - Remember it in a set of to-remove DepGraphIndexes.
1178 todo.Set(locator.index);
1179 // - Remove from m_mapping. This isn't strictly necessary as unused positions in m_mapping
1180 // are just never accessed, but set it to -1 here to increase the ability to detect a bug
1181 // that causes it to be accessed regardless.
1182 m_mapping[locator.index] = GraphIndex(-1);
1183 // - Remove its linearization index from the Entry (if in main).
1184 if (level == 0) {
1185 entry.m_main_lin_index = LinearizationIndex(-1);
1186 }
1187 // - Mark it as missing/removed in the Entry's locator.
1188 graph.ClearLocator(level, idx, m_quality == QualityLevel::OVERSIZED_SINGLETON);
1189 to_remove = to_remove.subspan(1);
1190 } while(!to_remove.empty());
1191
1192 Assume(todo.Any());
1193 // Wipe from the Cluster's DepGraph (this is O(n) regardless of the number of entries
1194 // removed, so we benefit from batching all the removals).
1195 m_depgraph.RemoveTransactions(todo);
1196 m_mapping.resize(m_depgraph.PositionRange());
1197
1198 // Filter removed transactions out of m_linearization.
1199 m_linearization.erase(std::remove_if(m_linearization.begin(), m_linearization.end(),
1200 [&](auto pos) { return todo[pos]; }),
1201 m_linearization.end());
1202
1203 Compact();
1204 graph.GetClusterSet(level).m_cluster_usage += TotalMemoryUsage();
1205 auto new_quality = IsTopological() ? QualityLevel::NEEDS_SPLIT : QualityLevel::NEEDS_SPLIT_FIX;
1206 graph.SetClusterQuality(level, m_quality, m_setindex, new_quality);
1207 Updated(graph, level);
1208}
1209
1210void SingletonClusterImpl::ApplyRemovals(TxGraphImpl& graph, int level, std::span<GraphIndex>& to_remove) noexcept
1211{
1212 // We can only remove the one transaction this Cluster has.
1213 Assume(!to_remove.empty());
1214 Assume(GetTxCount());
1215 Assume(to_remove.front() == m_graph_index);
1216 // Pop all copies of m_graph_index from the front of to_remove (at least one, but there may be
1217 // multiple).
1218 do {
1219 to_remove = to_remove.subspan(1);
1220 } while (!to_remove.empty() && to_remove.front() == m_graph_index);
1221 // Clear this cluster.
1222 graph.ClearLocator(level, m_graph_index, m_quality == QualityLevel::OVERSIZED_SINGLETON);
1223 m_graph_index = NO_GRAPH_INDEX;
1224 graph.SetClusterQuality(level, m_quality, m_setindex, QualityLevel::NEEDS_SPLIT);
1225 // No need to account for m_cluster_usage changes here, as SingletonClusterImpl has constant
1226 // memory usage.
1227}
1228
1229void GenericClusterImpl::Clear(TxGraphImpl& graph, int level) noexcept
1230{
1231 Assume(GetTxCount());
1232 graph.GetClusterSet(level).m_cluster_usage -= TotalMemoryUsage();
1233 for (auto i : m_linearization) {
1234 graph.ClearLocator(level, m_mapping[i], m_quality == QualityLevel::OVERSIZED_SINGLETON);
1235 }
1236 m_depgraph = {};
1237 m_linearization.clear();
1238 m_mapping.clear();
1239}
1240
1241void SingletonClusterImpl::Clear(TxGraphImpl& graph, int level) noexcept
1242{
1243 Assume(GetTxCount());
1244 graph.GetClusterSet(level).m_cluster_usage -= TotalMemoryUsage();
1245 graph.ClearLocator(level, m_graph_index, m_quality == QualityLevel::OVERSIZED_SINGLETON);
1246 m_graph_index = NO_GRAPH_INDEX;
1247}
1248
1249void GenericClusterImpl::MoveToMain(TxGraphImpl& graph) noexcept
1250{
1251 for (auto i : m_linearization) {
1252 GraphIndex idx = m_mapping[i];
1253 auto& entry = graph.m_entries[idx];
1254 entry.m_locator[1].SetMissing();
1255 }
1256 auto quality = m_quality;
1257 // Subtract memory usage from staging and add it to main.
1258 graph.GetClusterSet(/*level=*/1).m_cluster_usage -= TotalMemoryUsage();
1259 graph.GetClusterSet(/*level=*/0).m_cluster_usage += TotalMemoryUsage();
1260 // Remove cluster itself from staging and add it to main.
1261 auto cluster = graph.ExtractCluster(1, quality, m_setindex);
1262 graph.InsertCluster(/*level=*/0, std::move(cluster), quality);
1263 Updated(graph, /*level=*/0);
1264}
1265
1266void SingletonClusterImpl::MoveToMain(TxGraphImpl& graph) noexcept
1267{
1268 if (GetTxCount()) {
1269 auto& entry = graph.m_entries[m_graph_index];
1270 entry.m_locator[1].SetMissing();
1271 }
1272 auto quality = m_quality;
1273 graph.GetClusterSet(/*level=*/1).m_cluster_usage -= TotalMemoryUsage();
1274 auto cluster = graph.ExtractCluster(/*level=*/1, quality, m_setindex);
1275 graph.InsertCluster(/*level=*/0, std::move(cluster), quality);
1276 graph.GetClusterSet(/*level=*/0).m_cluster_usage += TotalMemoryUsage();
1277 Updated(graph, /*level=*/0);
1278}
1279
1280void GenericClusterImpl::Compact() noexcept
1281{
1282 m_linearization.shrink_to_fit();
1283 m_mapping.shrink_to_fit();
1284 m_depgraph.Compact();
1285}
1286
1287void SingletonClusterImpl::Compact() noexcept
1288{
1289 // Nothing to compact; SingletonClusterImpl is constant size.
1290}
1291
1292void GenericClusterImpl::AppendChunkFeerates(std::vector<FeeFrac>& ret) const noexcept
1293{
1294 auto chunk_feerates = ChunkLinearization(m_depgraph, m_linearization);
1295 ret.reserve(ret.size() + chunk_feerates.size());
1296 ret.insert(ret.end(), chunk_feerates.begin(), chunk_feerates.end());
1297}
1298
1299void SingletonClusterImpl::AppendChunkFeerates(std::vector<FeeFrac>& ret) const noexcept
1300{
1301 if (GetTxCount()) {
1302 ret.push_back(m_feerate);
1303 }
1304}
1305
1306uint64_t GenericClusterImpl::AppendTrimData(std::vector<TrimTxData>& ret, std::vector<std::pair<GraphIndex, GraphIndex>>& deps) const noexcept
1307{
1308 Assume(IsAcceptable());
1309 auto linchunking = ChunkLinearizationInfo(m_depgraph, m_linearization);
1310 LinearizationIndex pos{0};
1311 uint64_t size{0};
1312 auto prev_index = GraphIndex(-1);
1313 // Iterate over the chunks of this cluster's linearization.
1314 for (const auto& [chunk, chunk_feerate] : linchunking) {
1315 // Iterate over the transactions of that chunk, in linearization order.
1316 auto chunk_tx_count = chunk.Count();
1317 for (unsigned j = 0; j < chunk_tx_count; ++j) {
1318 auto cluster_idx = m_linearization[pos];
1319 // The transaction must appear in the chunk.
1320 Assume(chunk[cluster_idx]);
1321 // Construct a new element in ret.
1322 auto& entry = ret.emplace_back();
1323 entry.m_chunk_feerate = FeePerWeight::FromFeeFrac(chunk_feerate);
1324 entry.m_index = m_mapping[cluster_idx];
1325 // If this is not the first transaction of the cluster linearization, it has an
1326 // implicit dependency on its predecessor.
1327 if (pos != 0) deps.emplace_back(prev_index, entry.m_index);
1328 prev_index = entry.m_index;
1329 entry.m_tx_size = m_depgraph.FeeRate(cluster_idx).size;
1330 size += entry.m_tx_size;
1331 ++pos;
1332 }
1333 }
1334 return size;
1335}
1336
1337uint64_t SingletonClusterImpl::AppendTrimData(std::vector<TrimTxData>& ret, std::vector<std::pair<GraphIndex, GraphIndex>>& deps) const noexcept
1338{
1339 if (!GetTxCount()) return 0;
1340 auto& entry = ret.emplace_back();
1341 entry.m_chunk_feerate = m_feerate;
1342 entry.m_index = m_graph_index;
1343 entry.m_tx_size = m_feerate.size;
1344 return m_feerate.size;
1345}
1346
1347bool GenericClusterImpl::Split(TxGraphImpl& graph, int level) noexcept
1348{
1349 // This function can only be called when the Cluster needs splitting.
1350 Assume(NeedsSplitting());
1351 // Determine the new quality the split-off Clusters will have.
1352 QualityLevel new_quality = IsTopological() ? QualityLevel::NEEDS_RELINEARIZE : QualityLevel::NEEDS_FIX;
1354 auto todo = m_depgraph.Positions();
1357 std::vector<std::pair<Cluster*, DepGraphIndex>> remap(m_depgraph.PositionRange());
1358 std::vector<Cluster*> new_clusters;
1359 bool first{true};
1360 // Iterate over the connected components of this Cluster's m_depgraph.
1361 while (todo.Any()) {
1362 auto component = m_depgraph.FindConnectedComponent(todo);
1363 auto component_size = component.Count();
1364 auto split_quality = component_size <= 1 ? QualityLevel::OPTIMAL : new_quality;
1365 if (first && component == todo && SetType::Fill(component_size) == component && component_size >= MIN_INTENDED_TX_COUNT) {
1366 // The existing Cluster is an entire component, without holes. Leave it be, but update
1367 // its quality. If there are holes, we continue, so that the Cluster is reconstructed
1368 // without holes, reducing memory usage. If the component's size is below the intended
1369 // transaction count for this Cluster implementation, continue so that it can get
1370 // converted.
1371 Assume(todo == m_depgraph.Positions());
1372 graph.SetClusterQuality(level, m_quality, m_setindex, split_quality);
1373 // If this made the quality ACCEPTABLE or OPTIMAL, we need to compute and cache its
1374 // chunking.
1375 Updated(graph, level);
1376 return false;
1377 }
1378 first = false;
1379 // Construct a new Cluster to hold the found component.
1380 auto new_cluster = graph.CreateEmptyCluster(component_size);
1381 new_clusters.push_back(new_cluster.get());
1382 // Remember that all the component's transactions go to this new Cluster. The positions
1383 // will be determined below, so use -1 for now.
1384 for (auto i : component) {
1385 remap[i] = {new_cluster.get(), DepGraphIndex(-1)};
1386 }
1387 graph.InsertCluster(level, std::move(new_cluster), split_quality);
1388 todo -= component;
1389 }
1390 // We have to split the Cluster up. Remove accounting for the existing one first.
1391 graph.GetClusterSet(level).m_cluster_usage -= TotalMemoryUsage();
1392 // Redistribute the transactions.
1393 for (auto i : m_linearization) {
1395 Cluster* new_cluster = remap[i].first;
1396 // Copy the transaction to the new cluster's depgraph, and remember the position.
1397 remap[i].second = new_cluster->AppendTransaction(m_mapping[i], FeePerWeight::FromFeeFrac(m_depgraph.FeeRate(i)));
1398 }
1399 // Redistribute the dependencies.
1400 for (auto i : m_linearization) {
1402 Cluster* new_cluster = remap[i].first;
1403 // Copy its parents, translating positions.
1404 SetType new_parents;
1405 for (auto par : m_depgraph.GetReducedParents(i)) new_parents.Set(remap[par].second);
1406 new_cluster->AddDependencies(new_parents, remap[i].second);
1407 }
1408 // Update all the Locators of moved transactions, and memory usage.
1409 for (Cluster* new_cluster : new_clusters) {
1410 new_cluster->Updated(graph, level);
1411 new_cluster->Compact();
1412 graph.GetClusterSet(level).m_cluster_usage += new_cluster->TotalMemoryUsage();
1413 }
1414 // Wipe this Cluster, and return that it needs to be deleted.
1415 m_depgraph = DepGraph<SetType>{};
1416 m_mapping.clear();
1417 m_linearization.clear();
1418 return true;
1419}
1420
1421bool SingletonClusterImpl::Split(TxGraphImpl& graph, int level) noexcept
1422{
1423 Assume(NeedsSplitting());
1424 Assume(!GetTxCount());
1425 graph.GetClusterSet(level).m_cluster_usage -= TotalMemoryUsage();
1426 return true;
1427}
1428
1429void GenericClusterImpl::Merge(TxGraphImpl& graph, int level, Cluster& other) noexcept
1430{
1432 std::vector<DepGraphIndex> remap(other.GetDepGraphIndexRange());
1433 // Iterate over all transactions in the other Cluster (the one being absorbed).
1434 other.ExtractTransactions([&](DepGraphIndex pos, GraphIndex idx, FeePerWeight feerate) noexcept {
1435 // Copy the transaction into this Cluster, and remember its position.
1436 auto new_pos = m_depgraph.AddTransaction(feerate);
1437 // Since this cluster must have been made hole-free before being merged into, all added
1438 // transactions should appear at the end.
1439 Assume(new_pos == m_mapping.size());
1440 remap[pos] = new_pos;
1441 m_mapping.push_back(idx);
1442 m_linearization.push_back(new_pos);
1443 }, [&](DepGraphIndex pos, GraphIndex idx, SetType other_parents) noexcept {
1444 // Copy the transaction's dependencies, translating them using remap.
1445 SetType parents;
1446 for (auto par : other_parents) {
1447 parents.Set(remap[par]);
1448 }
1449 m_depgraph.AddDependencies(parents, remap[pos]);
1450 // Update the transaction's Locator. There is no need to call Updated() to update chunk
1451 // feerates, as Updated() will be invoked by Cluster::ApplyDependencies on the resulting
1452 // merged Cluster later anyway.
1453 auto& entry = graph.m_entries[idx];
1454 // Discard any potential ChunkData prior to modifying the Cluster (as that could
1455 // invalidate its ordering).
1456 if (level == 0) graph.ClearChunkData(entry);
1457 entry.m_locator[level].SetPresent(this, remap[pos]);
1458 });
1459}
1460
1461void SingletonClusterImpl::Merge(TxGraphImpl&, int, Cluster&) noexcept
1462{
1463 // Nothing can be merged into a singleton; it should have been converted to GenericClusterImpl first.
1464 Assume(false);
1465}
1466
1467void GenericClusterImpl::ApplyDependencies(TxGraphImpl& graph, int level, std::span<std::pair<GraphIndex, GraphIndex>> to_apply) noexcept
1468{
1469 // Sort the list of dependencies to apply by child, so those can be applied in batch.
1470 std::sort(to_apply.begin(), to_apply.end(), [](auto& a, auto& b) { return a.second < b.second; });
1471 // Iterate over groups of to-be-added dependencies with the same child.
1472 auto it = to_apply.begin();
1473 while (it != to_apply.end()) {
1474 auto& first_child = graph.m_entries[it->second].m_locator[level];
1475 const auto child_idx = first_child.index;
1476 // Iterate over all to-be-added dependencies within that same child, gather the relevant
1477 // parents.
1478 SetType parents;
1479 while (it != to_apply.end()) {
1480 auto& child = graph.m_entries[it->second].m_locator[level];
1481 auto& parent = graph.m_entries[it->first].m_locator[level];
1482 Assume(child.cluster == this && parent.cluster == this);
1483 if (child.index != child_idx) break;
1484 parents.Set(parent.index);
1485 ++it;
1486 }
1487 // Push all dependencies to the underlying DepGraph. Note that this is O(N) in the size of
1488 // the cluster, regardless of the number of parents being added, so batching them together
1489 // has a performance benefit.
1490 m_depgraph.AddDependencies(parents, child_idx);
1491 }
1492
1493 // Finally set the cluster to NEEDS_FIX, so its linearization is fixed the next time it is
1494 // attempted to be made ACCEPTABLE.
1495 Assume(!NeedsSplitting());
1496 Assume(!IsOversized());
1497 graph.SetClusterQuality(level, m_quality, m_setindex, QualityLevel::NEEDS_FIX);
1498
1499 // Finally push the changes to graph.m_entries.
1500 Updated(graph, level);
1501}
1502
1503void SingletonClusterImpl::ApplyDependencies(TxGraphImpl&, int, std::span<std::pair<GraphIndex, GraphIndex>>) noexcept
1504{
1505 // Nothing can actually be applied.
1506 Assume(false);
1507}
1508
1509TxGraphImpl::~TxGraphImpl() noexcept
1510{
1511 // If Refs outlive the TxGraphImpl they refer to, unlink them, so that their destructor does not
1512 // try to reach into a non-existing TxGraphImpl anymore.
1513 for (auto& entry : m_entries) {
1514 if (entry.m_ref != nullptr) {
1515 GetRefGraph(*entry.m_ref) = nullptr;
1516 }
1517 }
1518}
1519
1520std::unique_ptr<Cluster> TxGraphImpl::ExtractCluster(int level, QualityLevel quality, ClusterSetIndex setindex) noexcept
1521{
1522 Assume(quality != QualityLevel::NONE);
1523
1524 auto& clusterset = GetClusterSet(level);
1525 auto& quality_clusters = clusterset.m_clusters[int(quality)];
1526 Assume(setindex < quality_clusters.size());
1527
1528 // Extract the Cluster-owning unique_ptr.
1529 std::unique_ptr<Cluster> ret = std::move(quality_clusters[setindex]);
1530 ret->m_quality = QualityLevel::NONE;
1531 ret->m_setindex = ClusterSetIndex(-1);
1532
1533 // Clean up space in quality_cluster.
1534 auto max_setindex = quality_clusters.size() - 1;
1535 if (setindex != max_setindex) {
1536 // If the cluster was not the last element of quality_clusters, move that to take its place.
1537 quality_clusters.back()->m_setindex = setindex;
1538 quality_clusters[setindex] = std::move(quality_clusters.back());
1539 }
1540 // The last element of quality_clusters is now unused; drop it.
1541 quality_clusters.pop_back();
1542
1543 return ret;
1544}
1545
1546ClusterSetIndex TxGraphImpl::InsertCluster(int level, std::unique_ptr<Cluster>&& cluster, QualityLevel quality) noexcept
1547{
1548 // Cannot insert with quality level NONE (as that would mean not inserted).
1549 Assume(quality != QualityLevel::NONE);
1550 // The passed-in Cluster must not currently be in the TxGraphImpl.
1551 Assume(cluster->m_quality == QualityLevel::NONE);
1552
1553 // Append it at the end of the relevant TxGraphImpl::m_cluster.
1554 auto& clusterset = GetClusterSet(level);
1555 auto& quality_clusters = clusterset.m_clusters[int(quality)];
1556 ClusterSetIndex ret = quality_clusters.size();
1557 cluster->m_quality = quality;
1558 cluster->m_setindex = ret;
1559 quality_clusters.push_back(std::move(cluster));
1560 return ret;
1561}
1562
1563void TxGraphImpl::SetClusterQuality(int level, QualityLevel old_quality, ClusterSetIndex old_index, QualityLevel new_quality) noexcept
1564{
1565 Assume(new_quality != QualityLevel::NONE);
1566
1567 // Don't do anything if the quality did not change.
1568 if (old_quality == new_quality) return;
1569 // Extract the cluster from where it currently resides.
1570 auto cluster_ptr = ExtractCluster(level, old_quality, old_index);
1571 // And re-insert it where it belongs.
1572 InsertCluster(level, std::move(cluster_ptr), new_quality);
1573}
1574
1575void TxGraphImpl::DeleteCluster(Cluster& cluster, int level) noexcept
1576{
1577 // Extract the cluster from where it currently resides.
1578 auto cluster_ptr = ExtractCluster(level, cluster.m_quality, cluster.m_setindex);
1579 // And throw it away.
1580 cluster_ptr.reset();
1581}
1582
1583std::pair<Cluster*, int> TxGraphImpl::FindClusterAndLevel(GraphIndex idx, int level) const noexcept
1584{
1585 Assume(level >= 0 && level <= GetTopLevel());
1586 auto& entry = m_entries[idx];
1587 // Search the entry's locators from top to bottom.
1588 for (int l = level; l >= 0; --l) {
1589 // If the locator is missing, dig deeper; it may exist at a lower level and therefore be
1590 // implicitly existing at this level too.
1591 if (entry.m_locator[l].IsMissing()) continue;
1592 // If the locator has the entry marked as explicitly removed, stop.
1593 if (entry.m_locator[l].IsRemoved()) break;
1594 // Otherwise, we have found the topmost ClusterSet that contains this entry.
1595 return {entry.m_locator[l].cluster, l};
1596 }
1597 // If no non-empty locator was found, or an explicitly removed was hit, return nothing.
1598 return {nullptr, -1};
1599}
1600
1601Cluster* TxGraphImpl::PullIn(Cluster* cluster, int level) noexcept
1602{
1603 int to_level = GetTopLevel();
1604 Assume(to_level == 1);
1605 Assume(level <= to_level);
1606 // Copy the Cluster from main to staging, if it's not already there.
1607 if (level == 0) {
1608 // Make the Cluster Acceptable before copying. This isn't strictly necessary, but doing it
1609 // now avoids doing double work later.
1610 MakeAcceptable(*cluster, level);
1611 cluster = cluster->CopyToStaging(*this);
1612 }
1613 return cluster;
1614}
1615
1616void TxGraphImpl::ApplyRemovals(int up_to_level) noexcept
1617{
1618 Assume(up_to_level >= 0 && up_to_level <= GetTopLevel());
1619 for (int level = 0; level <= up_to_level; ++level) {
1620 auto& clusterset = GetClusterSet(level);
1621 auto& to_remove = clusterset.m_to_remove;
1622 // Skip if there is nothing to remove in this level.
1623 if (to_remove.empty()) continue;
1624 // Pull in all Clusters that are not in staging.
1625 if (level == 1) {
1626 for (GraphIndex index : to_remove) {
1627 auto [cluster, cluster_level] = FindClusterAndLevel(index, level);
1628 if (cluster != nullptr) PullIn(cluster, cluster_level);
1629 }
1630 }
1631 // Group the set of to-be-removed entries by Cluster::m_sequence.
1632 std::sort(to_remove.begin(), to_remove.end(), [&](GraphIndex a, GraphIndex b) noexcept {
1633 Cluster* cluster_a = m_entries[a].m_locator[level].cluster;
1634 Cluster* cluster_b = m_entries[b].m_locator[level].cluster;
1635 return CompareClusters(cluster_a, cluster_b) < 0;
1636 });
1637 // Process per Cluster.
1638 std::span to_remove_span{to_remove};
1639 while (!to_remove_span.empty()) {
1640 Cluster* cluster = m_entries[to_remove_span.front()].m_locator[level].cluster;
1641 if (cluster != nullptr) {
1642 // If the first to_remove_span entry's Cluster exists, hand to_remove_span to it, so it
1643 // can pop off whatever applies to it.
1644 cluster->ApplyRemovals(*this, level, to_remove_span);
1645 } else {
1646 // Otherwise, skip this already-removed entry. This may happen when
1647 // RemoveTransaction was called twice on the same Ref, for example.
1648 to_remove_span = to_remove_span.subspan(1);
1649 }
1650 }
1651 to_remove.clear();
1652 }
1653 Compact();
1654}
1655
1656void TxGraphImpl::SwapIndexes(GraphIndex a, GraphIndex b) noexcept
1657{
1658 Assume(a < m_entries.size());
1659 Assume(b < m_entries.size());
1660 // Swap the Entry objects.
1661 std::swap(m_entries[a], m_entries[b]);
1662 // Iterate over both objects.
1663 for (int i = 0; i < 2; ++i) {
1664 GraphIndex idx = i ? b : a;
1665 Entry& entry = m_entries[idx];
1666 // Update linked Ref, if any exists.
1667 if (entry.m_ref) GetRefIndex(*entry.m_ref) = idx;
1668 // Update linked chunk index entries, if any exist.
1669 if (entry.m_main_chunkindex_iterator != m_main_chunkindex.end()) {
1670 entry.m_main_chunkindex_iterator->m_graph_index = idx;
1671 }
1672 // Update the locators for both levels. The rest of the Entry information will not change,
1673 // so no need to invoke Cluster::Updated().
1674 for (int level = 0; level < MAX_LEVELS; ++level) {
1675 Locator& locator = entry.m_locator[level];
1676 if (locator.IsPresent()) {
1677 locator.cluster->UpdateMapping(locator.index, idx);
1678 }
1679 }
1680 }
1681}
1682
1683void TxGraphImpl::Compact() noexcept
1684{
1685 // We cannot compact while any to-be-applied operations or staged removals remain as we'd need
1686 // to rewrite them. It is easier to delay the compaction until they have been applied.
1687 if (!m_main_clusterset.m_deps_to_add.empty()) return;
1688 if (!m_main_clusterset.m_to_remove.empty()) return;
1689 Assume(m_main_clusterset.m_removed.empty()); // non-staging m_removed is always empty
1690 if (m_staging_clusterset.has_value()) {
1691 if (!m_staging_clusterset->m_deps_to_add.empty()) return;
1692 if (!m_staging_clusterset->m_to_remove.empty()) return;
1693 if (!m_staging_clusterset->m_removed.empty()) return;
1694 }
1695
1696 // Release memory used by discarded ChunkData index entries.
1697 ClearShrink(m_main_chunkindex_discarded);
1698
1699 // Sort the GraphIndexes that need to be cleaned up. They are sorted in reverse, so the last
1700 // ones get processed first. This means earlier-processed GraphIndexes will not cause moving of
1701 // later-processed ones during the "swap with end of m_entries" step below (which might
1702 // invalidate them).
1703 std::sort(m_unlinked.begin(), m_unlinked.end(), std::greater{});
1704
1705 auto last = GraphIndex(-1);
1706 for (GraphIndex idx : m_unlinked) {
1707 // m_unlinked should never contain the same GraphIndex twice (the code below would fail
1708 // if so, because GraphIndexes get invalidated by removing them).
1709 Assume(idx != last);
1710 last = idx;
1711
1712 // Make sure the entry is unlinked.
1713 Entry& entry = m_entries[idx];
1714 Assume(entry.m_ref == nullptr);
1715 // Make sure the entry does not occur in the graph.
1716 for (int level = 0; level < MAX_LEVELS; ++level) {
1717 Assume(!entry.m_locator[level].IsPresent());
1718 }
1719
1720 // Move the entry to the end.
1721 if (idx != m_entries.size() - 1) SwapIndexes(idx, m_entries.size() - 1);
1722 // Drop the entry for idx, now that it is at the end.
1723 m_entries.pop_back();
1724 }
1725 m_unlinked.clear();
1726}
1727
1728void TxGraphImpl::Split(Cluster& cluster, int level) noexcept
1729{
1730 // To split a Cluster, first make sure all removals are applied (as we might need to split
1731 // again afterwards otherwise).
1732 ApplyRemovals(level);
1733 bool del = cluster.Split(*this, level);
1734 if (del) {
1735 // Cluster::Split reports whether the Cluster is to be deleted.
1736 DeleteCluster(cluster, level);
1737 }
1738}
1739
1740void TxGraphImpl::SplitAll(int up_to_level) noexcept
1741{
1742 Assume(up_to_level >= 0 && up_to_level <= GetTopLevel());
1743 // Before splitting all Cluster, first make sure all removals are applied.
1744 ApplyRemovals(up_to_level);
1745 for (int level = 0; level <= up_to_level; ++level) {
1746 for (auto quality : {QualityLevel::NEEDS_SPLIT_FIX, QualityLevel::NEEDS_SPLIT}) {
1747 auto& queue = GetClusterSet(level).m_clusters[int(quality)];
1748 while (!queue.empty()) {
1749 Split(*queue.back().get(), level);
1750 }
1751 }
1752 }
1753}
1754
1755void TxGraphImpl::GroupClusters(int level) noexcept
1756{
1757 auto& clusterset = GetClusterSet(level);
1758 // If the groupings have been computed already, nothing is left to be done.
1759 if (clusterset.m_group_data.has_value()) return;
1760
1761 // Before computing which Clusters need to be merged together, first apply all removals and
1762 // split the Clusters into connected components. If we would group first, we might end up
1763 // with inefficient and/or oversized Clusters which just end up being split again anyway.
1764 SplitAll(level);
1765
1769 std::vector<std::pair<Cluster*, uint64_t>> an_clusters;
1774 std::vector<std::pair<std::pair<GraphIndex, GraphIndex>, uint64_t>> an_deps;
1775
1776 // Construct an an_clusters entry for every oversized cluster, including ones from levels below,
1777 // as they may be inherited in this one.
1778 for (int level_iter = 0; level_iter <= level; ++level_iter) {
1779 for (auto& cluster : GetClusterSet(level_iter).m_clusters[int(QualityLevel::OVERSIZED_SINGLETON)]) {
1780 auto graph_idx = cluster->GetClusterEntry(0);
1781 auto cur_cluster = FindCluster(graph_idx, level);
1782 if (cur_cluster == nullptr) continue;
1783 an_clusters.emplace_back(cur_cluster, cur_cluster->m_sequence);
1784 }
1785 }
1786
1787 // Construct a an_clusters entry for every parent and child in the to-be-applied dependencies,
1788 // and an an_deps entry for each dependency to be applied.
1789 an_deps.reserve(clusterset.m_deps_to_add.size());
1790 for (const auto& [par, chl] : clusterset.m_deps_to_add) {
1791 auto par_cluster = FindCluster(par, level);
1792 auto chl_cluster = FindCluster(chl, level);
1793 // Skip dependencies for which the parent or child transaction is removed.
1794 if (par_cluster == nullptr || chl_cluster == nullptr) continue;
1795 an_clusters.emplace_back(par_cluster, par_cluster->m_sequence);
1796 // Do not include a duplicate when parent and child are identical, as it'll be removed
1797 // below anyway.
1798 if (chl_cluster != par_cluster) an_clusters.emplace_back(chl_cluster, chl_cluster->m_sequence);
1799 // Add entry to an_deps, using the child sequence number.
1800 an_deps.emplace_back(std::pair{par, chl}, chl_cluster->m_sequence);
1801 }
1802 // Sort and deduplicate an_clusters, so we end up with a sorted list of all involved Clusters
1803 // to which dependencies apply, or which are oversized.
1804 std::sort(an_clusters.begin(), an_clusters.end(), [](auto& a, auto& b) noexcept { return a.second < b.second; });
1805 an_clusters.erase(std::unique(an_clusters.begin(), an_clusters.end()), an_clusters.end());
1806 // Sort an_deps by applying the same order to the involved child cluster.
1807 std::sort(an_deps.begin(), an_deps.end(), [&](auto& a, auto& b) noexcept { return a.second < b.second; });
1808
1809 // Run the union-find algorithm to find partitions of the input Clusters which need to be
1810 // grouped together. See https://en.wikipedia.org/wiki/Disjoint-set_data_structure.
1811 {
1813 struct PartitionData
1814 {
1816 uint64_t sequence;
1821 PartitionData* parent;
1824 unsigned rank;
1825 };
1827 std::vector<PartitionData> partition_data;
1828
1830 auto locate_fn = [&](uint64_t sequence) noexcept -> PartitionData* {
1831 auto it = std::lower_bound(partition_data.begin(), partition_data.end(), sequence,
1832 [](auto& a, uint64_t seq) noexcept { return a.sequence < seq; });
1833 Assume(it != partition_data.end());
1834 Assume(it->sequence == sequence);
1835 return &*it;
1836 };
1837
1839 static constexpr auto find_root_fn = [](PartitionData* data) noexcept -> PartitionData* {
1840 while (data->parent != data) {
1841 // Replace pointers to parents with pointers to grandparents.
1842 // See https://en.wikipedia.org/wiki/Disjoint-set_data_structure#Finding_set_representatives.
1843 auto par = data->parent;
1844 data->parent = par->parent;
1845 data = par;
1846 }
1847 return data;
1848 };
1849
1852 static constexpr auto union_fn = [](PartitionData* arg1, PartitionData* arg2) noexcept {
1853 // Find the roots of the trees, and bail out if they are already equal (which would
1854 // mean they are in the same partition already).
1855 auto rep1 = find_root_fn(arg1);
1856 auto rep2 = find_root_fn(arg2);
1857 if (rep1 == rep2) return rep1;
1858 // Pick the lower-rank root to become a child of the higher-rank one.
1859 // See https://en.wikipedia.org/wiki/Disjoint-set_data_structure#Union_by_rank.
1860 if (rep1->rank < rep2->rank) std::swap(rep1, rep2);
1861 rep2->parent = rep1;
1862 rep1->rank += (rep1->rank == rep2->rank);
1863 return rep1;
1864 };
1865
1866 // Start by initializing every Cluster as its own singleton partition.
1867 partition_data.resize(an_clusters.size());
1868 for (size_t i = 0; i < an_clusters.size(); ++i) {
1869 partition_data[i].sequence = an_clusters[i].first->m_sequence;
1870 partition_data[i].parent = &partition_data[i];
1871 partition_data[i].rank = 0;
1872 }
1873
1874 // Run through all parent/child pairs in an_deps, and union the partitions their Clusters
1875 // are in.
1876 Cluster* last_chl_cluster{nullptr};
1877 PartitionData* last_partition{nullptr};
1878 for (const auto& [dep, _] : an_deps) {
1879 auto [par, chl] = dep;
1880 auto par_cluster = FindCluster(par, level);
1881 auto chl_cluster = FindCluster(chl, level);
1882 Assume(chl_cluster != nullptr && par_cluster != nullptr);
1883 // Nothing to do if parent and child are in the same Cluster.
1884 if (par_cluster == chl_cluster) continue;
1885 Assume(par != chl);
1886 if (chl_cluster == last_chl_cluster) {
1887 // If the child Clusters is the same as the previous iteration, union with the
1888 // tree they were in, avoiding the need for another lookup. Note that an_deps
1889 // is sorted by child Cluster, so batches with the same child are expected.
1890 last_partition = union_fn(locate_fn(par_cluster->m_sequence), last_partition);
1891 } else {
1892 last_chl_cluster = chl_cluster;
1893 last_partition = union_fn(locate_fn(par_cluster->m_sequence), locate_fn(chl_cluster->m_sequence));
1894 }
1895 }
1896
1897 // Update the sequence numbers in an_clusters and an_deps to be those of the partition
1898 // representative.
1899 auto deps_it = an_deps.begin();
1900 for (size_t i = 0; i < partition_data.size(); ++i) {
1901 auto& data = partition_data[i];
1902 // Find the sequence of the representative of the partition Cluster i is in, and store
1903 // it with the Cluster.
1904 auto rep_seq = find_root_fn(&data)->sequence;
1905 an_clusters[i].second = rep_seq;
1906 // Find all dependencies whose child Cluster is Cluster i, and annotate them with rep.
1907 while (deps_it != an_deps.end()) {
1908 auto [par, chl] = deps_it->first;
1909 auto chl_cluster = FindCluster(chl, level);
1910 Assume(chl_cluster != nullptr);
1911 if (chl_cluster->m_sequence > data.sequence) break;
1912 deps_it->second = rep_seq;
1913 ++deps_it;
1914 }
1915 }
1916 }
1917
1918 // Sort both an_clusters and an_deps by sequence number of the representative of the
1919 // partition they are in, grouping all those applying to the same partition together.
1920 std::sort(an_deps.begin(), an_deps.end(), [](auto& a, auto& b) noexcept { return a.second < b.second; });
1921 std::sort(an_clusters.begin(), an_clusters.end(), [](auto& a, auto& b) noexcept { return a.second < b.second; });
1922
1923 // Translate the resulting cluster groups to the m_group_data structure, and the dependencies
1924 // back to m_deps_to_add.
1925 clusterset.m_group_data = GroupData{};
1926 clusterset.m_group_data->m_group_clusters.reserve(an_clusters.size());
1927 clusterset.m_deps_to_add.clear();
1928 clusterset.m_deps_to_add.reserve(an_deps.size());
1929 clusterset.m_oversized = false;
1930 auto an_deps_it = an_deps.begin();
1931 auto an_clusters_it = an_clusters.begin();
1932 while (an_clusters_it != an_clusters.end()) {
1933 // Process all clusters/dependencies belonging to the partition with representative rep.
1934 auto rep = an_clusters_it->second;
1935 // Create and initialize a new GroupData entry for the partition.
1936 auto& new_entry = clusterset.m_group_data->m_groups.emplace_back();
1937 new_entry.m_cluster_offset = clusterset.m_group_data->m_group_clusters.size();
1938 new_entry.m_cluster_count = 0;
1939 new_entry.m_deps_offset = clusterset.m_deps_to_add.size();
1940 new_entry.m_deps_count = 0;
1941 uint32_t total_count{0};
1942 uint64_t total_size{0};
1943 // Add all its clusters to it (copying those from an_clusters to m_group_clusters).
1944 while (an_clusters_it != an_clusters.end() && an_clusters_it->second == rep) {
1945 clusterset.m_group_data->m_group_clusters.push_back(an_clusters_it->first);
1946 total_count += an_clusters_it->first->GetTxCount();
1947 total_size += an_clusters_it->first->GetTotalTxSize();
1948 ++an_clusters_it;
1949 ++new_entry.m_cluster_count;
1950 }
1951 // Add all its dependencies to it (copying those back from an_deps to m_deps_to_add).
1952 while (an_deps_it != an_deps.end() && an_deps_it->second == rep) {
1953 clusterset.m_deps_to_add.push_back(an_deps_it->first);
1954 ++an_deps_it;
1955 ++new_entry.m_deps_count;
1956 }
1957 // Detect oversizedness.
1958 if (total_count > m_max_cluster_count || total_size > m_max_cluster_size) {
1959 clusterset.m_oversized = true;
1960 }
1961 }
1962 Assume(an_deps_it == an_deps.end());
1963 Assume(an_clusters_it == an_clusters.end());
1964 Compact();
1965}
1966
1967void TxGraphImpl::Merge(std::span<Cluster*> to_merge, int level) noexcept
1968{
1969 Assume(!to_merge.empty());
1970 // Nothing to do if a group consists of just a single Cluster.
1971 if (to_merge.size() == 1) return;
1972
1973 // Move the largest Cluster to the front of to_merge. As all transactions in other to-be-merged
1974 // Clusters will be moved to that one, putting the largest one first minimizes the number of
1975 // moves.
1976 size_t max_size_pos{0};
1977 DepGraphIndex max_size = to_merge[max_size_pos]->GetTxCount();
1978 GetClusterSet(level).m_cluster_usage -= to_merge[max_size_pos]->TotalMemoryUsage();
1979 DepGraphIndex total_size = max_size;
1980 for (size_t i = 1; i < to_merge.size(); ++i) {
1981 GetClusterSet(level).m_cluster_usage -= to_merge[i]->TotalMemoryUsage();
1982 DepGraphIndex size = to_merge[i]->GetTxCount();
1983 total_size += size;
1984 if (size > max_size) {
1985 max_size_pos = i;
1986 max_size = size;
1987 }
1988 }
1989 if (max_size_pos != 0) std::swap(to_merge[0], to_merge[max_size_pos]);
1990
1991 size_t start_idx = 1;
1992 Cluster* into_cluster = to_merge[0];
1993 if (total_size > into_cluster->GetMaxTxCount()) {
1994 // The into_merge cluster is too small to fit all transactions being merged. Construct a
1995 // a new Cluster using an implementation that matches the total size, and merge everything
1996 // in there.
1997 auto new_cluster = CreateEmptyCluster(total_size);
1998 into_cluster = new_cluster.get();
1999 InsertCluster(level, std::move(new_cluster), QualityLevel::OPTIMAL);
2000 start_idx = 0;
2001 }
2002
2003 // Merge all further Clusters in the group into the result (first one, or new one), and delete
2004 // them.
2005 for (size_t i = start_idx; i < to_merge.size(); ++i) {
2006 into_cluster->Merge(*this, level, *to_merge[i]);
2007 DeleteCluster(*to_merge[i], level);
2008 }
2009 into_cluster->Compact();
2010 GetClusterSet(level).m_cluster_usage += into_cluster->TotalMemoryUsage();
2011}
2012
2013void TxGraphImpl::ApplyDependencies(int level) noexcept
2014{
2015 auto& clusterset = GetClusterSet(level);
2016 // Do not bother computing groups if we already know the result will be oversized.
2017 if (clusterset.m_oversized == true) return;
2018 // Compute the groups of to-be-merged Clusters (which also applies all removals, and splits).
2019 GroupClusters(level);
2020 Assume(clusterset.m_group_data.has_value());
2021 // Nothing to do if there are no dependencies to be added.
2022 if (clusterset.m_deps_to_add.empty()) return;
2023 // Dependencies cannot be applied if it would result in oversized clusters.
2024 if (clusterset.m_oversized == true) return;
2025
2026 // For each group of to-be-merged Clusters.
2027 for (const auto& group_entry : clusterset.m_group_data->m_groups) {
2028 auto cluster_span = std::span{clusterset.m_group_data->m_group_clusters}
2029 .subspan(group_entry.m_cluster_offset, group_entry.m_cluster_count);
2030 // Pull in all the Clusters that contain dependencies.
2031 if (level == 1) {
2032 for (Cluster*& cluster : cluster_span) {
2033 cluster = PullIn(cluster, cluster->GetLevel(*this));
2034 }
2035 }
2036 // Invoke Merge() to merge them into a single Cluster.
2037 Merge(cluster_span, level);
2038 // Actually apply all to-be-added dependencies (all parents and children from this grouping
2039 // belong to the same Cluster at this point because of the merging above).
2040 auto deps_span = std::span{clusterset.m_deps_to_add}
2041 .subspan(group_entry.m_deps_offset, group_entry.m_deps_count);
2042 Assume(!deps_span.empty());
2043 const auto& loc = m_entries[deps_span[0].second].m_locator[level];
2044 Assume(loc.IsPresent());
2045 loc.cluster->ApplyDependencies(*this, level, deps_span);
2046 }
2047
2048 // Wipe the list of to-be-added dependencies now that they are applied.
2049 clusterset.m_deps_to_add.clear();
2050 Compact();
2051 // Also no further Cluster mergings are needed (note that we clear, but don't set to
2052 // std::nullopt, as that would imply the groupings are unknown).
2053 clusterset.m_group_data = GroupData{};
2054}
2055
2056std::pair<uint64_t, bool> GenericClusterImpl::Relinearize(TxGraphImpl& graph, int level, uint64_t max_iters) noexcept
2057{
2058 // We can only relinearize Clusters that do not need splitting.
2059 Assume(!NeedsSplitting());
2060 // No work is required for Clusters which are already optimally linearized.
2061 if (IsOptimal()) return {0, false};
2062 // Invoke the actual linearization algorithm (passing in the existing one).
2063 uint64_t rng_seed = graph.m_rng.rand64();
2064 auto [linearization, optimal, cost] = Linearize(m_depgraph, max_iters, rng_seed, m_linearization, /*is_topological=*/IsTopological());
2065 // Postlinearize to undo some of the non-determinism caused by randomizing the linearization.
2066 // This also guarantees that all chunks are connected (which is not guaranteed by SFL
2067 // currently, even when optimal).
2068 PostLinearize(m_depgraph, linearization);
2069 // Update the linearization.
2070 m_linearization = std::move(linearization);
2071 // Update the Cluster's quality.
2072 bool improved = false;
2073 if (optimal) {
2074 graph.SetClusterQuality(level, m_quality, m_setindex, QualityLevel::OPTIMAL);
2075 improved = true;
2076 } else if (max_iters >= graph.m_acceptable_iters && !IsAcceptable()) {
2077 graph.SetClusterQuality(level, m_quality, m_setindex, QualityLevel::ACCEPTABLE);
2078 improved = true;
2079 } else if (!IsTopological()) {
2080 graph.SetClusterQuality(level, m_quality, m_setindex, QualityLevel::NEEDS_RELINEARIZE);
2081 improved = true;
2082 }
2083 // Update the Entry objects.
2084 Updated(graph, level);
2085 return {cost, improved};
2086}
2087
2088std::pair<uint64_t, bool> SingletonClusterImpl::Relinearize(TxGraphImpl& graph, int level, uint64_t max_iters) noexcept
2089{
2090 // All singletons are optimal, oversized, or need splitting. Each of these precludes
2091 // Relinearize from being called.
2092 assert(false);
2093 return {0, false};
2094}
2095
2096void TxGraphImpl::MakeAcceptable(Cluster& cluster, int level) noexcept
2097{
2098 // Relinearize the Cluster if needed.
2099 if (!cluster.NeedsSplitting() && !cluster.IsAcceptable() && !cluster.IsOversized()) {
2100 cluster.Relinearize(*this, level, m_acceptable_iters);
2101 }
2102}
2103
2104void TxGraphImpl::MakeAllAcceptable(int level) noexcept
2105{
2106 ApplyDependencies(level);
2107 auto& clusterset = GetClusterSet(level);
2108 if (clusterset.m_oversized == true) return;
2109 for (auto quality : {QualityLevel::NEEDS_FIX, QualityLevel::NEEDS_RELINEARIZE}) {
2110 auto& queue = clusterset.m_clusters[int(quality)];
2111 while (!queue.empty()) {
2112 MakeAcceptable(*queue.back().get(), level);
2113 }
2114 }
2115}
2116
2117GenericClusterImpl::GenericClusterImpl(uint64_t sequence) noexcept : Cluster{sequence} {}
2118
2119TxGraph::Ref TxGraphImpl::AddTransaction(const FeePerWeight& feerate) noexcept
2120{
2121 Assume(m_main_chunkindex_observers == 0 || GetTopLevel() != 0);
2122 Assume(feerate.size > 0);
2123 // Construct a new Ref.
2124 Ref ret;
2125 // Construct a new Entry, and link it with the Ref.
2126 auto idx = m_entries.size();
2127 m_entries.emplace_back();
2128 auto& entry = m_entries.back();
2129 entry.m_main_chunkindex_iterator = m_main_chunkindex.end();
2130 entry.m_ref = &ret;
2131 GetRefGraph(ret) = this;
2132 GetRefIndex(ret) = idx;
2133 // Construct a new singleton Cluster (which is necessarily optimally linearized).
2134 bool oversized = uint64_t(feerate.size) > m_max_cluster_size;
2135 auto cluster = CreateEmptyCluster(1);
2136 cluster->AppendTransaction(idx, feerate);
2137 auto cluster_ptr = cluster.get();
2138 int level = GetTopLevel();
2139 auto& clusterset = GetClusterSet(level);
2140 InsertCluster(level, std::move(cluster), oversized ? QualityLevel::OVERSIZED_SINGLETON : QualityLevel::OPTIMAL);
2141 cluster_ptr->Updated(*this, level);
2142 clusterset.m_cluster_usage += cluster_ptr->TotalMemoryUsage();
2143 ++clusterset.m_txcount;
2144 // Deal with individually oversized transactions.
2145 if (oversized) {
2146 ++clusterset.m_txcount_oversized;
2147 clusterset.m_oversized = true;
2148 clusterset.m_group_data = std::nullopt;
2149 }
2150 // Return the Ref.
2151 return ret;
2152}
2153
2154void TxGraphImpl::RemoveTransaction(const Ref& arg) noexcept
2155{
2156 // Don't do anything if the Ref is empty (which may be indicative of the transaction already
2157 // having been removed).
2158 if (GetRefGraph(arg) == nullptr) return;
2159 Assume(GetRefGraph(arg) == this);
2160 Assume(m_main_chunkindex_observers == 0 || GetTopLevel() != 0);
2161 // Find the Cluster the transaction is in, and stop if it isn't in any.
2162 int level = GetTopLevel();
2163 auto cluster = FindCluster(GetRefIndex(arg), level);
2164 if (cluster == nullptr) return;
2165 // Remember that the transaction is to be removed.
2166 auto& clusterset = GetClusterSet(level);
2167 clusterset.m_to_remove.push_back(GetRefIndex(arg));
2168 // Wipe m_group_data (as it will need to be recomputed).
2169 clusterset.m_group_data.reset();
2170 if (clusterset.m_oversized == true) clusterset.m_oversized = std::nullopt;
2171}
2172
2173void TxGraphImpl::AddDependency(const Ref& parent, const Ref& child) noexcept
2174{
2175 // Don't do anything if either Ref is empty (which may be indicative of it having already been
2176 // removed).
2177 if (GetRefGraph(parent) == nullptr || GetRefGraph(child) == nullptr) return;
2178 Assume(GetRefGraph(parent) == this && GetRefGraph(child) == this);
2179 Assume(m_main_chunkindex_observers == 0 || GetTopLevel() != 0);
2180 // Don't do anything if this is a dependency on self.
2181 if (GetRefIndex(parent) == GetRefIndex(child)) return;
2182 // Find the Cluster the parent and child transaction are in, and stop if either appears to be
2183 // already removed.
2184 int level = GetTopLevel();
2185 auto par_cluster = FindCluster(GetRefIndex(parent), level);
2186 if (par_cluster == nullptr) return;
2187 auto chl_cluster = FindCluster(GetRefIndex(child), level);
2188 if (chl_cluster == nullptr) return;
2189 // Remember that this dependency is to be applied.
2190 auto& clusterset = GetClusterSet(level);
2191 clusterset.m_deps_to_add.emplace_back(GetRefIndex(parent), GetRefIndex(child));
2192 // Wipe m_group_data (as it will need to be recomputed).
2193 clusterset.m_group_data.reset();
2194 if (clusterset.m_oversized == false) clusterset.m_oversized = std::nullopt;
2195}
2196
2197bool TxGraphImpl::Exists(const Ref& arg, Level level_select) noexcept
2198{
2199 if (GetRefGraph(arg) == nullptr) return false;
2200 Assume(GetRefGraph(arg) == this);
2201 size_t level = GetSpecifiedLevel(level_select);
2202 // Make sure the transaction isn't scheduled for removal.
2203 ApplyRemovals(level);
2204 auto cluster = FindCluster(GetRefIndex(arg), level);
2205 return cluster != nullptr;
2206}
2207
2208void GenericClusterImpl::GetAncestorRefs(const TxGraphImpl& graph, std::span<std::pair<Cluster*, DepGraphIndex>>& args, std::vector<TxGraph::Ref*>& output) noexcept
2209{
2211 SetType ancestors_union;
2212 // Process elements from the front of args, as long as they apply.
2213 while (!args.empty()) {
2214 if (args.front().first != this) break;
2215 ancestors_union |= m_depgraph.Ancestors(args.front().second);
2216 args = args.subspan(1);
2217 }
2218 Assume(ancestors_union.Any());
2219 // Translate all ancestors (in arbitrary order) to Refs (if they have any), and return them.
2220 for (auto idx : ancestors_union) {
2221 const auto& entry = graph.m_entries[m_mapping[idx]];
2222 Assume(entry.m_ref != nullptr);
2223 output.push_back(entry.m_ref);
2224 }
2225}
2226
2227void SingletonClusterImpl::GetAncestorRefs(const TxGraphImpl& graph, std::span<std::pair<Cluster*, DepGraphIndex>>& args, std::vector<TxGraph::Ref*>& output) noexcept
2228{
2229 Assume(GetTxCount());
2230 while (!args.empty()) {
2231 if (args.front().first != this) break;
2232 args = args.subspan(1);
2233 }
2234 const auto& entry = graph.m_entries[m_graph_index];
2235 Assume(entry.m_ref != nullptr);
2236 output.push_back(entry.m_ref);
2237}
2238
2239void GenericClusterImpl::GetDescendantRefs(const TxGraphImpl& graph, std::span<std::pair<Cluster*, DepGraphIndex>>& args, std::vector<TxGraph::Ref*>& output) noexcept
2240{
2242 SetType descendants_union;
2243 // Process elements from the front of args, as long as they apply.
2244 while (!args.empty()) {
2245 if (args.front().first != this) break;
2246 descendants_union |= m_depgraph.Descendants(args.front().second);
2247 args = args.subspan(1);
2248 }
2249 Assume(descendants_union.Any());
2250 // Translate all descendants (in arbitrary order) to Refs (if they have any), and return them.
2251 for (auto idx : descendants_union) {
2252 const auto& entry = graph.m_entries[m_mapping[idx]];
2253 Assume(entry.m_ref != nullptr);
2254 output.push_back(entry.m_ref);
2255 }
2256}
2257
2258void SingletonClusterImpl::GetDescendantRefs(const TxGraphImpl& graph, std::span<std::pair<Cluster*, DepGraphIndex>>& args, std::vector<TxGraph::Ref*>& output) noexcept
2259{
2260 // In a singleton cluster, the ancestors or descendants are always just the entire cluster.
2261 GetAncestorRefs(graph, args, output);
2262}
2263
2264bool GenericClusterImpl::GetClusterRefs(TxGraphImpl& graph, std::span<TxGraph::Ref*> range, LinearizationIndex start_pos) noexcept
2265{
2266 // Translate the transactions in the Cluster (in linearization order, starting at start_pos in
2267 // the linearization) to Refs, and fill them in range.
2268 for (auto& ref : range) {
2269 Assume(start_pos < m_linearization.size());
2270 const auto& entry = graph.m_entries[m_mapping[m_linearization[start_pos++]]];
2271 Assume(entry.m_ref != nullptr);
2272 ref = entry.m_ref;
2273 }
2274 // Return whether start_pos has advanced to the end of the Cluster.
2275 return start_pos == m_linearization.size();
2276}
2277
2278bool SingletonClusterImpl::GetClusterRefs(TxGraphImpl& graph, std::span<TxGraph::Ref*> range, LinearizationIndex start_pos) noexcept
2279{
2280 Assume(!range.empty());
2281 Assume(GetTxCount());
2282 Assume(start_pos == 0);
2283 const auto& entry = graph.m_entries[m_graph_index];
2284 Assume(entry.m_ref != nullptr);
2285 range[0] = entry.m_ref;
2286 return true;
2287}
2288
2289FeePerWeight GenericClusterImpl::GetIndividualFeerate(DepGraphIndex idx) noexcept
2290{
2291 return FeePerWeight::FromFeeFrac(m_depgraph.FeeRate(idx));
2292}
2293
2294FeePerWeight SingletonClusterImpl::GetIndividualFeerate(DepGraphIndex idx) noexcept
2295{
2296 Assume(GetTxCount());
2297 Assume(idx == 0);
2298 return m_feerate;
2299}
2300
2301void GenericClusterImpl::MakeStagingTransactionsMissing(TxGraphImpl& graph) noexcept
2302{
2303 // Mark all transactions of a Cluster missing, needed when aborting staging, so that the
2304 // corresponding Locators don't retain references into aborted Clusters.
2305 for (auto ci : m_linearization) {
2306 GraphIndex idx = m_mapping[ci];
2307 auto& entry = graph.m_entries[idx];
2308 entry.m_locator[1].SetMissing();
2309 }
2310}
2311
2312void SingletonClusterImpl::MakeStagingTransactionsMissing(TxGraphImpl& graph) noexcept
2313{
2314 if (GetTxCount()) {
2315 auto& entry = graph.m_entries[m_graph_index];
2316 entry.m_locator[1].SetMissing();
2317 }
2318}
2319
2320std::vector<TxGraph::Ref*> TxGraphImpl::GetAncestors(const Ref& arg, Level level_select) noexcept
2321{
2322 // Return the empty vector if the Ref is empty.
2323 if (GetRefGraph(arg) == nullptr) return {};
2324 Assume(GetRefGraph(arg) == this);
2325 // Apply all removals and dependencies, as the result might be incorrect otherwise.
2326 size_t level = GetSpecifiedLevel(level_select);
2327 ApplyDependencies(level);
2328 // Ancestry cannot be known if unapplied dependencies remain.
2329 Assume(GetClusterSet(level).m_deps_to_add.empty());
2330 // Find the Cluster the argument is in, and return the empty vector if it isn't in any.
2331 auto [cluster, cluster_level] = FindClusterAndLevel(GetRefIndex(arg), level);
2332 if (cluster == nullptr) return {};
2333 // Dispatch to the Cluster.
2334 std::pair<Cluster*, DepGraphIndex> match = {cluster, m_entries[GetRefIndex(arg)].m_locator[cluster_level].index};
2335 auto matches = std::span(&match, 1);
2336 std::vector<TxGraph::Ref*> ret;
2337 cluster->GetAncestorRefs(*this, matches, ret);
2338 return ret;
2339}
2340
2341std::vector<TxGraph::Ref*> TxGraphImpl::GetDescendants(const Ref& arg, Level level_select) noexcept
2342{
2343 // Return the empty vector if the Ref is empty.
2344 if (GetRefGraph(arg) == nullptr) return {};
2345 Assume(GetRefGraph(arg) == this);
2346 // Apply all removals and dependencies, as the result might be incorrect otherwise.
2347 size_t level = GetSpecifiedLevel(level_select);
2348 ApplyDependencies(level);
2349 // Ancestry cannot be known if unapplied dependencies remain.
2350 Assume(GetClusterSet(level).m_deps_to_add.empty());
2351 // Find the Cluster the argument is in, and return the empty vector if it isn't in any.
2352 auto [cluster, cluster_level] = FindClusterAndLevel(GetRefIndex(arg), level);
2353 if (cluster == nullptr) return {};
2354 // Dispatch to the Cluster.
2355 std::pair<Cluster*, DepGraphIndex> match = {cluster, m_entries[GetRefIndex(arg)].m_locator[cluster_level].index};
2356 auto matches = std::span(&match, 1);
2357 std::vector<TxGraph::Ref*> ret;
2358 cluster->GetDescendantRefs(*this, matches, ret);
2359 return ret;
2360}
2361
2362std::vector<TxGraph::Ref*> TxGraphImpl::GetAncestorsUnion(std::span<const Ref* const> args, Level level_select) noexcept
2363{
2364 // Apply all dependencies, as the result might be incorrect otherwise.
2365 size_t level = GetSpecifiedLevel(level_select);
2366 ApplyDependencies(level);
2367 // Ancestry cannot be known if unapplied dependencies remain.
2368 Assume(GetClusterSet(level).m_deps_to_add.empty());
2369
2370 // Translate args to matches.
2371 std::vector<std::pair<Cluster*, DepGraphIndex>> matches;
2372 matches.reserve(args.size());
2373 for (auto arg : args) {
2374 Assume(arg);
2375 // Skip empty Refs.
2376 if (GetRefGraph(*arg) == nullptr) continue;
2377 Assume(GetRefGraph(*arg) == this);
2378 // Find the Cluster the argument is in, and skip if none is found.
2379 auto [cluster, cluster_level] = FindClusterAndLevel(GetRefIndex(*arg), level);
2380 if (cluster == nullptr) continue;
2381 // Append to matches.
2382 matches.emplace_back(cluster, m_entries[GetRefIndex(*arg)].m_locator[cluster_level].index);
2383 }
2384 // Group by Cluster.
2385 std::sort(matches.begin(), matches.end(), [](auto& a, auto& b) noexcept { return CompareClusters(a.first, b.first) < 0; });
2386 // Dispatch to the Clusters.
2387 std::span match_span(matches);
2388 std::vector<TxGraph::Ref*> ret;
2389 while (!match_span.empty()) {
2390 match_span.front().first->GetAncestorRefs(*this, match_span, ret);
2391 }
2392 return ret;
2393}
2394
2395std::vector<TxGraph::Ref*> TxGraphImpl::GetDescendantsUnion(std::span<const Ref* const> args, Level level_select) noexcept
2396{
2397 // Apply all dependencies, as the result might be incorrect otherwise.
2398 size_t level = GetSpecifiedLevel(level_select);
2399 ApplyDependencies(level);
2400 // Ancestry cannot be known if unapplied dependencies remain.
2401 Assume(GetClusterSet(level).m_deps_to_add.empty());
2402
2403 // Translate args to matches.
2404 std::vector<std::pair<Cluster*, DepGraphIndex>> matches;
2405 matches.reserve(args.size());
2406 for (auto arg : args) {
2407 Assume(arg);
2408 // Skip empty Refs.
2409 if (GetRefGraph(*arg) == nullptr) continue;
2410 Assume(GetRefGraph(*arg) == this);
2411 // Find the Cluster the argument is in, and skip if none is found.
2412 auto [cluster, cluster_level] = FindClusterAndLevel(GetRefIndex(*arg), level);
2413 if (cluster == nullptr) continue;
2414 // Append to matches.
2415 matches.emplace_back(cluster, m_entries[GetRefIndex(*arg)].m_locator[cluster_level].index);
2416 }
2417 // Group by Cluster.
2418 std::sort(matches.begin(), matches.end(), [](auto& a, auto& b) noexcept { return CompareClusters(a.first, b.first) < 0; });
2419 // Dispatch to the Clusters.
2420 std::span match_span(matches);
2421 std::vector<TxGraph::Ref*> ret;
2422 while (!match_span.empty()) {
2423 match_span.front().first->GetDescendantRefs(*this, match_span, ret);
2424 }
2425 return ret;
2426}
2427
2428std::vector<TxGraph::Ref*> TxGraphImpl::GetCluster(const Ref& arg, Level level_select) noexcept
2429{
2430 // Return the empty vector if the Ref is empty (which may be indicative of the transaction
2431 // having been removed already.
2432 if (GetRefGraph(arg) == nullptr) return {};
2433 Assume(GetRefGraph(arg) == this);
2434 // Apply all removals and dependencies, as the result might be incorrect otherwise.
2435 size_t level = GetSpecifiedLevel(level_select);
2436 ApplyDependencies(level);
2437 // Cluster linearization cannot be known if unapplied dependencies remain.
2438 Assume(GetClusterSet(level).m_deps_to_add.empty());
2439 // Find the Cluster the argument is in, and return the empty vector if it isn't in any.
2440 auto [cluster, cluster_level] = FindClusterAndLevel(GetRefIndex(arg), level);
2441 if (cluster == nullptr) return {};
2442 // Make sure the Cluster has an acceptable quality level, and then dispatch to it.
2443 MakeAcceptable(*cluster, cluster_level);
2444 std::vector<TxGraph::Ref*> ret(cluster->GetTxCount());
2445 cluster->GetClusterRefs(*this, ret, 0);
2446 return ret;
2447}
2448
2449TxGraph::GraphIndex TxGraphImpl::GetTransactionCount(Level level_select) noexcept
2450{
2451 size_t level = GetSpecifiedLevel(level_select);
2452 ApplyRemovals(level);
2453 return GetClusterSet(level).m_txcount;
2454}
2455
2456FeePerWeight TxGraphImpl::GetIndividualFeerate(const Ref& arg) noexcept
2457{
2458 // Return the empty FeePerWeight if the passed Ref is empty.
2459 if (GetRefGraph(arg) == nullptr) return {};
2460 Assume(GetRefGraph(arg) == this);
2461 // Find the cluster the argument is in (the level does not matter as individual feerates will
2462 // be identical if it occurs in both), and return the empty FeePerWeight if it isn't in any.
2463 Cluster* cluster{nullptr};
2464 int level;
2465 for (level = 0; level <= GetTopLevel(); ++level) {
2466 // Apply removals, so that we can correctly report FeePerWeight{} for non-existing
2467 // transactions.
2468 ApplyRemovals(level);
2469 if (m_entries[GetRefIndex(arg)].m_locator[level].IsPresent()) {
2470 cluster = m_entries[GetRefIndex(arg)].m_locator[level].cluster;
2471 break;
2472 }
2473 }
2474 if (cluster == nullptr) return {};
2475 // Dispatch to the Cluster.
2476 return cluster->GetIndividualFeerate(m_entries[GetRefIndex(arg)].m_locator[level].index);
2477}
2478
2479FeePerWeight TxGraphImpl::GetMainChunkFeerate(const Ref& arg) noexcept
2480{
2481 // Return the empty FeePerWeight if the passed Ref is empty.
2482 if (GetRefGraph(arg) == nullptr) return {};
2483 Assume(GetRefGraph(arg) == this);
2484 // Apply all removals and dependencies, as the result might be inaccurate otherwise.
2485 ApplyDependencies(/*level=*/0);
2486 // Chunk feerates cannot be accurately known if unapplied dependencies remain.
2487 Assume(m_main_clusterset.m_deps_to_add.empty());
2488 // Find the cluster the argument is in, and return the empty FeePerWeight if it isn't in any.
2489 auto [cluster, cluster_level] = FindClusterAndLevel(GetRefIndex(arg), /*level=*/0);
2490 if (cluster == nullptr) return {};
2491 // Make sure the Cluster has an acceptable quality level, and then return the transaction's
2492 // chunk feerate.
2493 MakeAcceptable(*cluster, cluster_level);
2494 const auto& entry = m_entries[GetRefIndex(arg)];
2495 return entry.m_main_chunk_feerate;
2496}
2497
2498bool TxGraphImpl::IsOversized(Level level_select) noexcept
2499{
2500 size_t level = GetSpecifiedLevel(level_select);
2501 auto& clusterset = GetClusterSet(level);
2502 if (clusterset.m_oversized.has_value()) {
2503 // Return cached value if known.
2504 return *clusterset.m_oversized;
2505 }
2506 ApplyRemovals(level);
2507 if (clusterset.m_txcount_oversized > 0) {
2508 clusterset.m_oversized = true;
2509 } else {
2510 // Find which Clusters will need to be merged together, as that is where the oversize
2511 // property is assessed.
2512 GroupClusters(level);
2513 }
2514 Assume(clusterset.m_oversized.has_value());
2515 return *clusterset.m_oversized;
2516}
2517
2518void TxGraphImpl::StartStaging() noexcept
2519{
2520 // Staging cannot already exist.
2521 Assume(!m_staging_clusterset.has_value());
2522 // Apply all remaining dependencies in main before creating a staging graph. Once staging
2523 // exists, we cannot merge Clusters anymore (because of interference with Clusters being
2524 // pulled into staging), so to make sure all inspectors are available (if not oversized), do
2525 // all merging work now. Call SplitAll() first, so that even if ApplyDependencies does not do
2526 // any thing due to knowing the result is oversized, splitting is still performed.
2527 SplitAll(0);
2528 ApplyDependencies(0);
2529 // Construct the staging ClusterSet.
2530 m_staging_clusterset.emplace();
2531 // Copy statistics, precomputed data, and to-be-applied dependencies (only if oversized) to
2532 // the new graph. To-be-applied removals will always be empty at this point.
2533 m_staging_clusterset->m_txcount = m_main_clusterset.m_txcount;
2534 m_staging_clusterset->m_txcount_oversized = m_main_clusterset.m_txcount_oversized;
2535 m_staging_clusterset->m_deps_to_add = m_main_clusterset.m_deps_to_add;
2536 m_staging_clusterset->m_group_data = m_main_clusterset.m_group_data;
2537 m_staging_clusterset->m_oversized = m_main_clusterset.m_oversized;
2538 Assume(m_staging_clusterset->m_oversized.has_value());
2539 m_staging_clusterset->m_cluster_usage = 0;
2540}
2541
2542void TxGraphImpl::AbortStaging() noexcept
2543{
2544 // Staging must exist.
2545 Assume(m_staging_clusterset.has_value());
2546 // Mark all removed transactions as Missing (so the staging locator for these transactions
2547 // can be reused if another staging is created).
2548 for (auto idx : m_staging_clusterset->m_removed) {
2549 m_entries[idx].m_locator[1].SetMissing();
2550 }
2551 // Do the same with the non-removed transactions in staging Clusters.
2552 for (int quality = 0; quality < int(QualityLevel::NONE); ++quality) {
2553 for (auto& cluster : m_staging_clusterset->m_clusters[quality]) {
2554 cluster->MakeStagingTransactionsMissing(*this);
2555 }
2556 }
2557 // Destroy the staging ClusterSet.
2558 m_staging_clusterset.reset();
2559 Compact();
2560 if (!m_main_clusterset.m_group_data.has_value()) {
2561 // In case m_oversized in main was kept after a Ref destruction while staging exists, we
2562 // need to re-evaluate m_oversized now.
2563 if (m_main_clusterset.m_to_remove.empty() && m_main_clusterset.m_txcount_oversized > 0) {
2564 // It is possible that a Ref destruction caused a removal in main while staging existed.
2565 // In this case, m_txcount_oversized may be inaccurate.
2566 m_main_clusterset.m_oversized = true;
2567 } else {
2568 m_main_clusterset.m_oversized = std::nullopt;
2569 }
2570 }
2571}
2572
2573void TxGraphImpl::CommitStaging() noexcept
2574{
2575 // Staging must exist.
2576 Assume(m_staging_clusterset.has_value());
2577 Assume(m_main_chunkindex_observers == 0);
2578 // Delete all conflicting Clusters in main, to make place for moving the staging ones
2579 // there. All of these have been copied to staging in PullIn().
2580 auto conflicts = GetConflicts();
2581 for (Cluster* conflict : conflicts) {
2582 conflict->Clear(*this, /*level=*/0);
2583 DeleteCluster(*conflict, /*level=*/0);
2584 }
2585 // Mark the removed transactions as Missing (so the staging locator for these transactions
2586 // can be reused if another staging is created).
2587 for (auto idx : m_staging_clusterset->m_removed) {
2588 m_entries[idx].m_locator[1].SetMissing();
2589 }
2590 // Then move all Clusters in staging to main.
2591 for (int quality = 0; quality < int(QualityLevel::NONE); ++quality) {
2592 auto& stage_sets = m_staging_clusterset->m_clusters[quality];
2593 while (!stage_sets.empty()) {
2594 stage_sets.back()->MoveToMain(*this);
2595 }
2596 }
2597 // Move all statistics, precomputed data, and to-be-applied removals and dependencies.
2598 m_main_clusterset.m_deps_to_add = std::move(m_staging_clusterset->m_deps_to_add);
2599 m_main_clusterset.m_to_remove = std::move(m_staging_clusterset->m_to_remove);
2600 m_main_clusterset.m_group_data = std::move(m_staging_clusterset->m_group_data);
2601 m_main_clusterset.m_oversized = std::move(m_staging_clusterset->m_oversized);
2602 m_main_clusterset.m_txcount = std::move(m_staging_clusterset->m_txcount);
2603 m_main_clusterset.m_txcount_oversized = std::move(m_staging_clusterset->m_txcount_oversized);
2604 // Delete the old staging graph, after all its information was moved to main.
2605 m_staging_clusterset.reset();
2606 Compact();
2607}
2608
2609void GenericClusterImpl::SetFee(TxGraphImpl& graph, int level, DepGraphIndex idx, int64_t fee) noexcept
2610{
2611 // Make sure the specified DepGraphIndex exists in this Cluster.
2612 Assume(m_depgraph.Positions()[idx]);
2613 // Bail out if the fee isn't actually being changed.
2614 if (m_depgraph.FeeRate(idx).fee == fee) return;
2615 // Update the fee, remember that relinearization will be necessary, and update the Entries
2616 // in the same Cluster.
2617 m_depgraph.FeeRate(idx).fee = fee;
2618 if (m_quality == QualityLevel::OVERSIZED_SINGLETON) {
2619 // Nothing to do.
2620 } else if (IsAcceptable()) {
2621 graph.SetClusterQuality(level, m_quality, m_setindex, QualityLevel::NEEDS_RELINEARIZE);
2622 }
2623 Updated(graph, level);
2624}
2625
2626void SingletonClusterImpl::SetFee(TxGraphImpl& graph, int level, DepGraphIndex idx, int64_t fee) noexcept
2627{
2628 Assume(GetTxCount());
2629 Assume(idx == 0);
2630 m_feerate.fee = fee;
2631 Updated(graph, level);
2632}
2633
2634void TxGraphImpl::SetTransactionFee(const Ref& ref, int64_t fee) noexcept
2635{
2636 // Don't do anything if the passed Ref is empty.
2637 if (GetRefGraph(ref) == nullptr) return;
2638 Assume(GetRefGraph(ref) == this);
2639 Assume(m_main_chunkindex_observers == 0);
2640 // Find the entry, its locator, and inform its Cluster about the new feerate, if any.
2641 auto& entry = m_entries[GetRefIndex(ref)];
2642 for (int level = 0; level < MAX_LEVELS; ++level) {
2643 auto& locator = entry.m_locator[level];
2644 if (locator.IsPresent()) {
2645 locator.cluster->SetFee(*this, level, locator.index, fee);
2646 }
2647 }
2648}
2649
2650std::strong_ordering TxGraphImpl::CompareMainOrder(const Ref& a, const Ref& b) noexcept
2651{
2652 // The references must not be empty.
2653 Assume(GetRefGraph(a) == this);
2654 Assume(GetRefGraph(b) == this);
2655 // Apply dependencies in main.
2656 ApplyDependencies(0);
2657 Assume(m_main_clusterset.m_deps_to_add.empty());
2658 // Make both involved Clusters acceptable, so chunk feerates are relevant.
2659 const auto& entry_a = m_entries[GetRefIndex(a)];
2660 const auto& entry_b = m_entries[GetRefIndex(b)];
2661 const auto& locator_a = entry_a.m_locator[0];
2662 const auto& locator_b = entry_b.m_locator[0];
2663 Assume(locator_a.IsPresent());
2664 Assume(locator_b.IsPresent());
2665 MakeAcceptable(*locator_a.cluster, /*level=*/0);
2666 MakeAcceptable(*locator_b.cluster, /*level=*/0);
2667 // Invoke comparison logic.
2668 return CompareMainTransactions(GetRefIndex(a), GetRefIndex(b));
2669}
2670
2671TxGraph::GraphIndex TxGraphImpl::CountDistinctClusters(std::span<const Ref* const> refs, Level level_select) noexcept
2672{
2673 size_t level = GetSpecifiedLevel(level_select);
2674 ApplyDependencies(level);
2675 auto& clusterset = GetClusterSet(level);
2676 Assume(clusterset.m_deps_to_add.empty());
2677 // Build a vector of Clusters that the specified Refs occur in.
2678 std::vector<Cluster*> clusters;
2679 clusters.reserve(refs.size());
2680 for (const Ref* ref : refs) {
2681 Assume(ref);
2682 if (GetRefGraph(*ref) == nullptr) continue;
2683 Assume(GetRefGraph(*ref) == this);
2684 auto cluster = FindCluster(GetRefIndex(*ref), level);
2685 if (cluster != nullptr) clusters.push_back(cluster);
2686 }
2687 // Count the number of distinct elements in clusters.
2688 std::sort(clusters.begin(), clusters.end(), [](Cluster* a, Cluster* b) noexcept { return CompareClusters(a, b) < 0; });
2689 Cluster* last{nullptr};
2690 GraphIndex ret{0};
2691 for (Cluster* cluster : clusters) {
2692 ret += (cluster != last);
2693 last = cluster;
2694 }
2695 return ret;
2696}
2697
2698std::pair<std::vector<FeeFrac>, std::vector<FeeFrac>> TxGraphImpl::GetMainStagingDiagrams() noexcept
2699{
2700 Assume(m_staging_clusterset.has_value());
2701 MakeAllAcceptable(0);
2702 Assume(m_main_clusterset.m_deps_to_add.empty()); // can only fail if main is oversized
2703 MakeAllAcceptable(1);
2704 Assume(m_staging_clusterset->m_deps_to_add.empty()); // can only fail if staging is oversized
2705 // For all Clusters in main which conflict with Clusters in staging (i.e., all that are removed
2706 // by, or replaced in, staging), gather their chunk feerates.
2707 auto main_clusters = GetConflicts();
2708 std::vector<FeeFrac> main_feerates, staging_feerates;
2709 for (Cluster* cluster : main_clusters) {
2710 cluster->AppendChunkFeerates(main_feerates);
2711 }
2712 // Do the same for the Clusters in staging themselves.
2713 for (int quality = 0; quality < int(QualityLevel::NONE); ++quality) {
2714 for (const auto& cluster : m_staging_clusterset->m_clusters[quality]) {
2715 cluster->AppendChunkFeerates(staging_feerates);
2716 }
2717 }
2718 // Sort both by decreasing feerate to obtain diagrams, and return them.
2719 std::sort(main_feerates.begin(), main_feerates.end(), [](auto& a, auto& b) { return a > b; });
2720 std::sort(staging_feerates.begin(), staging_feerates.end(), [](auto& a, auto& b) { return a > b; });
2721 return std::make_pair(std::move(main_feerates), std::move(staging_feerates));
2722}
2723
2724void GenericClusterImpl::SanityCheck(const TxGraphImpl& graph, int level) const
2725{
2726 // There must be an m_mapping for each m_depgraph position (including holes).
2727 assert(m_depgraph.PositionRange() == m_mapping.size());
2728 // The linearization for this Cluster must contain every transaction once.
2729 assert(m_depgraph.TxCount() == m_linearization.size());
2730 // Unless a split is to be applied, the cluster cannot have any holes.
2731 if (!NeedsSplitting()) {
2732 assert(m_depgraph.Positions() == SetType::Fill(m_depgraph.TxCount()));
2733 }
2734
2735 // Compute the chunking of m_linearization.
2736 auto linchunking = ChunkLinearizationInfo(m_depgraph, m_linearization);
2737 unsigned chunk_num{0};
2738
2739 // Verify m_linearization.
2740 SetType m_done;
2741 LinearizationIndex linindex{0};
2742 DepGraphIndex chunk_pos{0};
2743 assert(m_depgraph.IsAcyclic());
2744 for (auto lin_pos : m_linearization) {
2745 assert(lin_pos < m_mapping.size());
2746 const auto& entry = graph.m_entries[m_mapping[lin_pos]];
2747 // Check that the linearization is topological.
2748 m_done.Set(lin_pos);
2749 if (IsTopological()) {
2750 assert(m_done.IsSupersetOf(m_depgraph.Ancestors(lin_pos)));
2751 }
2752 // Check that the Entry has a locator pointing back to this Cluster & position within it.
2753 assert(entry.m_locator[level].cluster == this);
2754 assert(entry.m_locator[level].index == lin_pos);
2755 // For main-level entries, check linearization position and chunk feerate.
2756 if (level == 0 && IsAcceptable()) {
2757 assert(entry.m_main_lin_index == linindex);
2758 ++linindex;
2759 if (!linchunking[chunk_num].transactions[lin_pos]) {
2760 ++chunk_num;
2761 chunk_pos = 0;
2762 }
2763 assert(entry.m_main_chunk_feerate == linchunking[chunk_num].feerate);
2764 // Verify that an entry in the chunk index exists for every chunk-ending transaction.
2765 ++chunk_pos;
2766 bool is_chunk_end = (chunk_pos == linchunking[chunk_num].transactions.Count());
2767 assert((entry.m_main_chunkindex_iterator != graph.m_main_chunkindex.end()) == is_chunk_end);
2768 if (is_chunk_end) {
2769 auto& chunk_data = *entry.m_main_chunkindex_iterator;
2770 if (m_done == m_depgraph.Positions() && chunk_pos == 1) {
2771 assert(chunk_data.m_chunk_count == LinearizationIndex(-1));
2772 } else {
2773 assert(chunk_data.m_chunk_count == chunk_pos);
2774 }
2775 }
2776 // If this Cluster has an acceptable quality level, its chunks must be connected.
2777 assert(m_depgraph.IsConnected(linchunking[chunk_num].transactions));
2778 }
2779 }
2780 // Verify that each element of m_depgraph occurred in m_linearization.
2781 assert(m_done == m_depgraph.Positions());
2782}
2783
2784void SingletonClusterImpl::SanityCheck(const TxGraphImpl& graph, int level) const
2785{
2786 // All singletons are optimal, oversized, or need splitting.
2787 Assume(IsOptimal() || IsOversized() || NeedsSplitting());
2788 if (GetTxCount()) {
2789 const auto& entry = graph.m_entries[m_graph_index];
2790 // Check that the Entry has a locator pointing back to this Cluster & position within it.
2791 assert(entry.m_locator[level].cluster == this);
2792 assert(entry.m_locator[level].index == 0);
2793 // For main-level entries, check linearization position and chunk feerate.
2794 if (level == 0 && IsAcceptable()) {
2795 assert(entry.m_main_lin_index == 0);
2796 assert(entry.m_main_chunk_feerate == m_feerate);
2797 assert(entry.m_main_chunkindex_iterator != graph.m_main_chunkindex.end());
2798 auto& chunk_data = *entry.m_main_chunkindex_iterator;
2799 assert(chunk_data.m_chunk_count == LinearizationIndex(-1));
2800 }
2801 }
2802}
2803
2804void TxGraphImpl::SanityCheck() const
2805{
2807 std::set<GraphIndex> expected_unlinked;
2809 std::set<const Cluster*> expected_clusters[MAX_LEVELS];
2811 std::set<GraphIndex> expected_removed[MAX_LEVELS];
2813 std::set<uint64_t> sequences;
2815 std::set<GraphIndex> expected_chunkindex;
2817 bool compact_possible{true};
2818
2819 // Go over all Entry objects in m_entries.
2820 for (GraphIndex idx = 0; idx < m_entries.size(); ++idx) {
2821 const auto& entry = m_entries[idx];
2822 if (entry.m_ref == nullptr) {
2823 // Unlinked Entry must have indexes appear in m_unlinked.
2824 expected_unlinked.insert(idx);
2825 } else {
2826 // Every non-unlinked Entry must have a Ref that points back to it.
2827 assert(GetRefGraph(*entry.m_ref) == this);
2828 assert(GetRefIndex(*entry.m_ref) == idx);
2829 }
2830 if (entry.m_main_chunkindex_iterator != m_main_chunkindex.end()) {
2831 // Remember which entries we see a chunkindex entry for.
2832 assert(entry.m_locator[0].IsPresent());
2833 expected_chunkindex.insert(idx);
2834 }
2835 // Verify the Entry m_locators.
2836 bool was_present{false}, was_removed{false};
2837 for (int level = 0; level < MAX_LEVELS; ++level) {
2838 const auto& locator = entry.m_locator[level];
2839 // Every Locator must be in exactly one of these 3 states.
2840 assert(locator.IsMissing() + locator.IsRemoved() + locator.IsPresent() == 1);
2841 if (locator.IsPresent()) {
2842 // Once removed, a transaction cannot be revived.
2843 assert(!was_removed);
2844 // Verify that the Cluster agrees with where the Locator claims the transaction is.
2845 assert(locator.cluster->GetClusterEntry(locator.index) == idx);
2846 // Remember that we expect said Cluster to appear in the ClusterSet::m_clusters.
2847 expected_clusters[level].insert(locator.cluster);
2848 was_present = true;
2849 } else if (locator.IsRemoved()) {
2850 // Level 0 (main) cannot have IsRemoved locators (IsMissing there means non-existing).
2851 assert(level > 0);
2852 // A Locator can only be IsRemoved if it was IsPresent before, and only once.
2853 assert(was_present && !was_removed);
2854 // Remember that we expect this GraphIndex to occur in the ClusterSet::m_removed.
2855 expected_removed[level].insert(idx);
2856 was_removed = true;
2857 }
2858 }
2859 }
2860
2861 // For all levels (0 = main, 1 = staged)...
2862 for (int level = 0; level <= GetTopLevel(); ++level) {
2863 assert(level < MAX_LEVELS);
2864 auto& clusterset = GetClusterSet(level);
2865 std::set<const Cluster*> actual_clusters;
2866 size_t recomputed_cluster_usage{0};
2867
2868 // For all quality levels...
2869 for (int qual = 0; qual < int(QualityLevel::NONE); ++qual) {
2870 QualityLevel quality{qual};
2871 const auto& quality_clusters = clusterset.m_clusters[qual];
2872 // ... for all clusters in them ...
2873 for (ClusterSetIndex setindex = 0; setindex < quality_clusters.size(); ++setindex) {
2874 const auto& cluster = *quality_clusters[setindex];
2875 // The number of transactions in a Cluster cannot exceed m_max_cluster_count.
2876 assert(cluster.GetTxCount() <= m_max_cluster_count);
2877 // The level must match the Cluster's own idea of what level it is in (but GetLevel
2878 // can only be called for non-empty Clusters).
2879 assert(cluster.GetTxCount() == 0 || level == cluster.GetLevel(*this));
2880 // The sum of their sizes cannot exceed m_max_cluster_size, unless it is an
2881 // individually oversized transaction singleton. Note that groups of to-be-merged
2882 // clusters which would exceed this limit are marked oversized, which means they
2883 // are never applied.
2884 assert(cluster.IsOversized() || cluster.GetTotalTxSize() <= m_max_cluster_size);
2885 // OVERSIZED clusters are singletons.
2886 assert(!cluster.IsOversized() || cluster.GetTxCount() == 1);
2887 // Transaction counts cannot exceed the Cluster implementation's maximum
2888 // supported transactions count.
2889 assert(cluster.GetTxCount() <= cluster.GetMaxTxCount());
2890 // Unless a Split is yet to be applied, the number of transactions must not be
2891 // below the Cluster implementation's intended transaction count.
2892 if (!cluster.NeedsSplitting()) {
2893 assert(cluster.GetTxCount() >= cluster.GetMinIntendedTxCount());
2894 }
2895
2896 // Check the sequence number.
2897 assert(cluster.m_sequence < m_next_sequence_counter);
2898 assert(!sequences.contains(cluster.m_sequence));
2899 sequences.insert(cluster.m_sequence);
2900 // Remember we saw this Cluster (only if it is non-empty; empty Clusters aren't
2901 // expected to be referenced by the Entry vector).
2902 if (cluster.GetTxCount() != 0) {
2903 actual_clusters.insert(&cluster);
2904 }
2905 // Sanity check the cluster, according to the Cluster's internal rules.
2906 cluster.SanityCheck(*this, level);
2907 // Check that the cluster's quality and setindex matches its position in the quality list.
2908 assert(cluster.m_quality == quality);
2909 assert(cluster.m_setindex == setindex);
2910 // Count memory usage.
2911 recomputed_cluster_usage += cluster.TotalMemoryUsage();
2912 }
2913 }
2914
2915 // Verify memory usage.
2916 assert(clusterset.m_cluster_usage == recomputed_cluster_usage);
2917
2918 // Verify that all to-be-removed transactions have valid identifiers.
2919 for (GraphIndex idx : clusterset.m_to_remove) {
2920 assert(idx < m_entries.size());
2921 // We cannot assert that all m_to_remove transactions are still present: ~Ref on a
2922 // (P,M) transaction (present in main, inherited in staging) will cause an m_to_remove
2923 // addition in both main and staging, but a subsequence ApplyRemovals in main will
2924 // cause it to disappear from staging too, leaving the m_to_remove in place.
2925 }
2926
2927 // Verify that all to-be-added dependencies have valid identifiers.
2928 for (auto [par_idx, chl_idx] : clusterset.m_deps_to_add) {
2929 assert(par_idx != chl_idx);
2930 assert(par_idx < m_entries.size());
2931 assert(chl_idx < m_entries.size());
2932 }
2933
2934 // Verify that the actually encountered clusters match the ones occurring in Entry vector.
2935 assert(actual_clusters == expected_clusters[level]);
2936
2937 // Verify that the contents of m_removed matches what was expected based on the Entry vector.
2938 std::set<GraphIndex> actual_removed(clusterset.m_removed.begin(), clusterset.m_removed.end());
2939 for (auto i : expected_unlinked) {
2940 // If a transaction exists in both main and staging, and is removed from staging (adding
2941 // it to m_removed there), and consequently destroyed (wiping the locator completely),
2942 // it can remain in m_removed despite not having an IsRemoved() locator. Exclude those
2943 // transactions from the comparison here.
2944 actual_removed.erase(i);
2945 expected_removed[level].erase(i);
2946 }
2947
2948 assert(actual_removed == expected_removed[level]);
2949
2950 // If any GraphIndex entries remain in this ClusterSet, compact is not possible.
2951 if (!clusterset.m_deps_to_add.empty()) compact_possible = false;
2952 if (!clusterset.m_to_remove.empty()) compact_possible = false;
2953 if (!clusterset.m_removed.empty()) compact_possible = false;
2954
2955 // For non-top levels, m_oversized must be known (as it cannot change until the level
2956 // on top is gone).
2957 if (level < GetTopLevel()) assert(clusterset.m_oversized.has_value());
2958 }
2959
2960 // Verify that the contents of m_unlinked matches what was expected based on the Entry vector.
2961 std::set<GraphIndex> actual_unlinked(m_unlinked.begin(), m_unlinked.end());
2962 assert(actual_unlinked == expected_unlinked);
2963
2964 // If compaction was possible, it should have been performed already, and m_unlinked must be
2965 // empty (to prevent memory leaks due to an ever-growing m_entries vector).
2966 if (compact_possible) {
2967 assert(actual_unlinked.empty());
2968 }
2969
2970 // Finally, check the chunk index.
2971 std::set<GraphIndex> actual_chunkindex;
2972 FeeFrac last_chunk_feerate;
2973 for (const auto& chunk : m_main_chunkindex) {
2974 GraphIndex idx = chunk.m_graph_index;
2975 actual_chunkindex.insert(idx);
2976 auto chunk_feerate = m_entries[idx].m_main_chunk_feerate;
2977 if (!last_chunk_feerate.IsEmpty()) {
2978 assert(FeeRateCompare(last_chunk_feerate, chunk_feerate) >= 0);
2979 }
2980 last_chunk_feerate = chunk_feerate;
2981 }
2982 assert(actual_chunkindex == expected_chunkindex);
2983}
2984
2985bool TxGraphImpl::DoWork(uint64_t iters) noexcept
2986{
2987 uint64_t iters_done{0};
2988 // First linearize everything in NEEDS_RELINEARIZE to an acceptable level. If more budget
2989 // remains after that, try to make everything optimal.
2990 for (QualityLevel quality : {QualityLevel::NEEDS_FIX, QualityLevel::NEEDS_RELINEARIZE, QualityLevel::ACCEPTABLE}) {
2991 // First linearize staging, if it exists, then main.
2992 for (int level = GetTopLevel(); level >= 0; --level) {
2993 // Do not modify main if it has any observers.
2994 if (level == 0 && m_main_chunkindex_observers != 0) continue;
2995 ApplyDependencies(level);
2996 auto& clusterset = GetClusterSet(level);
2997 // Do not modify oversized levels.
2998 if (clusterset.m_oversized == true) continue;
2999 auto& queue = clusterset.m_clusters[int(quality)];
3000 while (!queue.empty()) {
3001 if (iters_done >= iters) return false;
3002 // Randomize the order in which we process, so that if the first cluster somehow
3003 // needs more work than what iters allows, we don't keep spending it on the same
3004 // one.
3005 auto pos = m_rng.randrange<size_t>(queue.size());
3006 auto iters_now = iters - iters_done;
3007 if (quality == QualityLevel::NEEDS_FIX || quality == QualityLevel::NEEDS_RELINEARIZE) {
3008 // If we're working with clusters that need relinearization still, only perform
3009 // up to m_acceptable_iters iterations. If they become ACCEPTABLE, and we still
3010 // have budget after all other clusters are ACCEPTABLE too, we'll spend the
3011 // remaining budget on trying to make them OPTIMAL.
3012 iters_now = std::min(iters_now, m_acceptable_iters);
3013 }
3014 auto [cost, improved] = queue[pos].get()->Relinearize(*this, level, iters_now);
3015 iters_done += cost;
3016 // If no improvement was made to the Cluster, it means we've essentially run out of
3017 // budget. Even though it may be the case that iters_done < iters still, the
3018 // linearizer decided there wasn't enough budget left to attempt anything with.
3019 // To avoid an infinite loop that keeps trying clusters with minuscule budgets,
3020 // stop here too.
3021 if (!improved) return false;
3022 }
3023 }
3024 }
3025 // All possible work has been performed, so we can return true. Note that this does *not* mean
3026 // that all clusters are optimally linearized now. It may be that there is nothing to do left
3027 // because all non-optimal clusters are in oversized and/or observer-bearing levels.
3028 return true;
3029}
3030
3031void BlockBuilderImpl::Next() noexcept
3032{
3033 // Don't do anything if we're already done.
3034 if (m_cur_iter == m_graph->m_main_chunkindex.end()) return;
3035 while (true) {
3036 // Advance the pointer, and stop if we reach the end.
3037 ++m_cur_iter;
3038 m_cur_cluster = nullptr;
3039 if (m_cur_iter == m_graph->m_main_chunkindex.end()) break;
3040 // Find the cluster pointed to by m_cur_iter.
3041 const auto& chunk_data = *m_cur_iter;
3042 const auto& chunk_end_entry = m_graph->m_entries[chunk_data.m_graph_index];
3043 m_cur_cluster = chunk_end_entry.m_locator[0].cluster;
3044 m_known_end_of_cluster = false;
3045 // If we previously skipped a chunk from this cluster we cannot include more from it.
3046 if (!m_excluded_clusters.contains(m_cur_cluster->m_sequence)) break;
3047 }
3048}
3049
3050std::optional<std::pair<std::vector<TxGraph::Ref*>, FeePerWeight>> BlockBuilderImpl::GetCurrentChunk() noexcept
3051{
3052 std::optional<std::pair<std::vector<TxGraph::Ref*>, FeePerWeight>> ret;
3053 // Populate the return value if we are not done.
3054 if (m_cur_iter != m_graph->m_main_chunkindex.end()) {
3055 ret.emplace();
3056 const auto& chunk_data = *m_cur_iter;
3057 const auto& chunk_end_entry = m_graph->m_entries[chunk_data.m_graph_index];
3058 if (chunk_data.m_chunk_count == LinearizationIndex(-1)) {
3059 // Special case in case just a single transaction remains, avoiding the need to
3060 // dispatch to and dereference Cluster.
3061 ret->first.resize(1);
3062 Assume(chunk_end_entry.m_ref != nullptr);
3063 ret->first[0] = chunk_end_entry.m_ref;
3064 m_known_end_of_cluster = true;
3065 } else {
3066 Assume(m_cur_cluster);
3067 ret->first.resize(chunk_data.m_chunk_count);
3068 auto start_pos = chunk_end_entry.m_main_lin_index + 1 - chunk_data.m_chunk_count;
3069 m_known_end_of_cluster = m_cur_cluster->GetClusterRefs(*m_graph, ret->first, start_pos);
3070 // If the chunk size was 1 and at end of cluster, then the special case above should
3071 // have been used.
3072 Assume(!m_known_end_of_cluster || chunk_data.m_chunk_count > 1);
3073 }
3074 ret->second = chunk_end_entry.m_main_chunk_feerate;
3075 }
3076 return ret;
3077}
3078
3079BlockBuilderImpl::BlockBuilderImpl(TxGraphImpl& graph) noexcept : m_graph(&graph)
3080{
3081 // Make sure all clusters in main are up to date, and acceptable.
3082 m_graph->MakeAllAcceptable(0);
3083 // There cannot remain any inapplicable dependencies (only possible if main is oversized).
3084 Assume(m_graph->m_main_clusterset.m_deps_to_add.empty());
3085 // Remember that this object is observing the graph's index, so that we can detect concurrent
3086 // modifications.
3087 ++m_graph->m_main_chunkindex_observers;
3088 // Find the first chunk.
3089 m_cur_iter = m_graph->m_main_chunkindex.begin();
3090 m_cur_cluster = nullptr;
3091 if (m_cur_iter != m_graph->m_main_chunkindex.end()) {
3092 // Find the cluster pointed to by m_cur_iter.
3093 const auto& chunk_data = *m_cur_iter;
3094 const auto& chunk_end_entry = m_graph->m_entries[chunk_data.m_graph_index];
3095 m_cur_cluster = chunk_end_entry.m_locator[0].cluster;
3096 }
3097}
3098
3099BlockBuilderImpl::~BlockBuilderImpl()
3100{
3101 Assume(m_graph->m_main_chunkindex_observers > 0);
3102 // Permit modifications to the main graph again after destroying the BlockBuilderImpl.
3103 --m_graph->m_main_chunkindex_observers;
3104}
3105
3106void BlockBuilderImpl::Include() noexcept
3107{
3108 // The actual inclusion of the chunk is done by the calling code. All we have to do is switch
3109 // to the next chunk.
3110 Next();
3111}
3112
3113void BlockBuilderImpl::Skip() noexcept
3114{
3115 // When skipping a chunk we need to not include anything more of the cluster, as that could make
3116 // the result topologically invalid. However, don't do this if the chunk is known to be the last
3117 // chunk of the cluster. This may significantly reduce the size of m_excluded_clusters,
3118 // especially when many singleton clusters are ignored.
3119 if (m_cur_cluster != nullptr && !m_known_end_of_cluster) {
3120 m_excluded_clusters.insert(m_cur_cluster->m_sequence);
3121 }
3122 Next();
3123}
3124
3125std::unique_ptr<TxGraph::BlockBuilder> TxGraphImpl::GetBlockBuilder() noexcept
3126{
3127 return std::make_unique<BlockBuilderImpl>(*this);
3128}
3129
3130std::pair<std::vector<TxGraph::Ref*>, FeePerWeight> TxGraphImpl::GetWorstMainChunk() noexcept
3131{
3132 std::pair<std::vector<Ref*>, FeePerWeight> ret;
3133 // Make sure all clusters in main are up to date, and acceptable.
3134 MakeAllAcceptable(0);
3135 Assume(m_main_clusterset.m_deps_to_add.empty());
3136 // If the graph is not empty, populate ret.
3137 if (!m_main_chunkindex.empty()) {
3138 const auto& chunk_data = *m_main_chunkindex.rbegin();
3139 const auto& chunk_end_entry = m_entries[chunk_data.m_graph_index];
3140 Cluster* cluster = chunk_end_entry.m_locator[0].cluster;
3141 if (chunk_data.m_chunk_count == LinearizationIndex(-1) || chunk_data.m_chunk_count == 1) {
3142 // Special case for singletons.
3143 ret.first.resize(1);
3144 Assume(chunk_end_entry.m_ref != nullptr);
3145 ret.first[0] = chunk_end_entry.m_ref;
3146 } else {
3147 ret.first.resize(chunk_data.m_chunk_count);
3148 auto start_pos = chunk_end_entry.m_main_lin_index + 1 - chunk_data.m_chunk_count;
3149 cluster->GetClusterRefs(*this, ret.first, start_pos);
3150 std::reverse(ret.first.begin(), ret.first.end());
3151 }
3152 ret.second = chunk_end_entry.m_main_chunk_feerate;
3153 }
3154 return ret;
3155}
3156
3157std::vector<TxGraph::Ref*> TxGraphImpl::Trim() noexcept
3158{
3159 int level = GetTopLevel();
3160 Assume(m_main_chunkindex_observers == 0 || level != 0);
3161 std::vector<TxGraph::Ref*> ret;
3162
3163 // Compute the groups of to-be-merged Clusters (which also applies all removals, and splits).
3164 auto& clusterset = GetClusterSet(level);
3165 if (clusterset.m_oversized == false) return ret;
3166 GroupClusters(level);
3167 Assume(clusterset.m_group_data.has_value());
3168 // Nothing to do if not oversized.
3169 Assume(clusterset.m_oversized.has_value());
3170 if (clusterset.m_oversized == false) return ret;
3171
3172 // In this function, would-be clusters (as precomputed in m_group_data by GroupClusters) are
3173 // trimmed by removing transactions in them such that the resulting clusters satisfy the size
3174 // and count limits.
3175 //
3176 // It works by defining for each would-be cluster a rudimentary linearization: at every point
3177 // the highest-chunk-feerate remaining transaction is picked among those with no unmet
3178 // dependencies. "Dependency" here means either a to-be-added dependency (m_deps_to_add), or
3179 // an implicit dependency added between any two consecutive transaction in their current
3180 // cluster linearization. So it can be seen as a "merge sort" of the chunks of the clusters,
3181 // but respecting the dependencies being added.
3182 //
3183 // This rudimentary linearization is computed lazily, by putting all eligible (no unmet
3184 // dependencies) transactions in a heap, and popping the highest-feerate one from it. Along the
3185 // way, the counts and sizes of the would-be clusters up to that point are tracked (by
3186 // partitioning the involved transactions using a union-find structure). Any transaction whose
3187 // addition would cause a violation is removed, along with all their descendants.
3188 //
3189 // A next invocation of GroupClusters (after applying the removals) will compute the new
3190 // resulting clusters, and none of them will violate the limits.
3191
3194 std::vector<std::pair<GraphIndex, GraphIndex>> deps_by_parent;
3196 std::vector<std::pair<GraphIndex, GraphIndex>> deps_by_child;
3200 std::vector<TrimTxData> trim_data;
3203 std::vector<std::vector<TrimTxData>::iterator> trim_heap;
3205 std::vector<TrimTxData*> current_deps;
3206
3208 static constexpr auto cmp_fn = [](auto a, auto b) noexcept {
3209 // Sort by increasing chunk feerate, and then by decreasing size.
3210 // We do not need to sort by cluster or within clusters, because due to the implicit
3211 // dependency between consecutive linearization elements, no two transactions from the
3212 // same Cluster will ever simultaneously be in the heap.
3213 return a->m_chunk_feerate < b->m_chunk_feerate;
3214 };
3215
3217 static constexpr auto find_fn = [](TrimTxData* arg) noexcept {
3218 while (arg != arg->m_uf_parent) {
3219 // Replace pointer to parent with pointer to grandparent (path splitting).
3220 // See https://en.wikipedia.org/wiki/Disjoint-set_data_structure#Finding_set_representatives.
3221 auto par = arg->m_uf_parent;
3222 arg->m_uf_parent = par->m_uf_parent;
3223 arg = par;
3224 }
3225 return arg;
3226 };
3227
3230 static constexpr auto union_fn = [](TrimTxData* arg1, TrimTxData* arg2) noexcept {
3231 // Replace arg1 and arg2 by their representatives.
3232 auto rep1 = find_fn(arg1);
3233 auto rep2 = find_fn(arg2);
3234 // Bail out if both representatives are the same, because that means arg1 and arg2 are in
3235 // the same partition already.
3236 if (rep1 == rep2) return rep1;
3237 // Pick the lower-count root to become a child of the higher-count one.
3238 // See https://en.wikipedia.org/wiki/Disjoint-set_data_structure#Union_by_size.
3239 if (rep1->m_uf_count < rep2->m_uf_count) std::swap(rep1, rep2);
3240 rep2->m_uf_parent = rep1;
3241 // Add the statistics of arg2 (which is no longer a representative) to those of arg1 (which
3242 // is now the representative for both).
3243 rep1->m_uf_size += rep2->m_uf_size;
3244 rep1->m_uf_count += rep2->m_uf_count;
3245 return rep1;
3246 };
3247
3249 auto locate_fn = [&](GraphIndex index) noexcept {
3250 auto it = std::lower_bound(trim_data.begin(), trim_data.end(), index, [](TrimTxData& elem, GraphIndex idx) noexcept {
3251 return elem.m_index < idx;
3252 });
3253 Assume(it != trim_data.end() && it->m_index == index);
3254 return it;
3255 };
3256
3257 // For each group of to-be-merged Clusters.
3258 for (const auto& group_data : clusterset.m_group_data->m_groups) {
3259 trim_data.clear();
3260 trim_heap.clear();
3261 deps_by_child.clear();
3262 deps_by_parent.clear();
3263
3264 // Gather trim data and implicit dependency data from all involved Clusters.
3265 auto cluster_span = std::span{clusterset.m_group_data->m_group_clusters}
3266 .subspan(group_data.m_cluster_offset, group_data.m_cluster_count);
3267 uint64_t size{0};
3268 for (Cluster* cluster : cluster_span) {
3269 MakeAcceptable(*cluster, cluster->GetLevel(*this));
3270 size += cluster->AppendTrimData(trim_data, deps_by_child);
3271 }
3272 // If this group of Clusters does not violate any limits, continue to the next group.
3273 if (trim_data.size() <= m_max_cluster_count && size <= m_max_cluster_size) continue;
3274 // Sort the trim data by GraphIndex. In what follows, we will treat this sorted vector as
3275 // a map from GraphIndex to TrimTxData via locate_fn, and its ordering will not change
3276 // anymore.
3277 std::sort(trim_data.begin(), trim_data.end(), [](auto& a, auto& b) noexcept { return a.m_index < b.m_index; });
3278
3279 // Add the explicitly added dependencies to deps_by_child.
3280 deps_by_child.insert(deps_by_child.end(),
3281 clusterset.m_deps_to_add.begin() + group_data.m_deps_offset,
3282 clusterset.m_deps_to_add.begin() + group_data.m_deps_offset + group_data.m_deps_count);
3283
3284 // Sort deps_by_child by child transaction GraphIndex. The order will not be changed
3285 // anymore after this.
3286 std::sort(deps_by_child.begin(), deps_by_child.end(), [](auto& a, auto& b) noexcept { return a.second < b.second; });
3287 // Fill m_parents_count and m_parents_offset in trim_data, as well as m_deps_left, and
3288 // initially populate trim_heap. Because of the sort above, all dependencies involving the
3289 // same child are grouped together, so a single linear scan suffices.
3290 auto deps_it = deps_by_child.begin();
3291 for (auto trim_it = trim_data.begin(); trim_it != trim_data.end(); ++trim_it) {
3292 trim_it->m_parent_offset = deps_it - deps_by_child.begin();
3293 trim_it->m_deps_left = 0;
3294 while (deps_it != deps_by_child.end() && deps_it->second == trim_it->m_index) {
3295 ++trim_it->m_deps_left;
3296 ++deps_it;
3297 }
3298 trim_it->m_parent_count = trim_it->m_deps_left;
3299 // If this transaction has no unmet dependencies, and is not oversized, add it to the
3300 // heap (just append for now, the heapification happens below).
3301 if (trim_it->m_deps_left == 0 && trim_it->m_tx_size <= m_max_cluster_size) {
3302 trim_heap.push_back(trim_it);
3303 }
3304 }
3305 Assume(deps_it == deps_by_child.end());
3306
3307 // Construct deps_by_parent, sorted by parent transaction GraphIndex. The order will not be
3308 // changed anymore after this.
3309 deps_by_parent = deps_by_child;
3310 std::sort(deps_by_parent.begin(), deps_by_parent.end(), [](auto& a, auto& b) noexcept { return a.first < b.first; });
3311 // Fill m_children_offset and m_children_count in trim_data. Because of the sort above, all
3312 // dependencies involving the same parent are grouped together, so a single linear scan
3313 // suffices.
3314 deps_it = deps_by_parent.begin();
3315 for (auto& trim_entry : trim_data) {
3316 trim_entry.m_children_count = 0;
3317 trim_entry.m_children_offset = deps_it - deps_by_parent.begin();
3318 while (deps_it != deps_by_parent.end() && deps_it->first == trim_entry.m_index) {
3319 ++trim_entry.m_children_count;
3320 ++deps_it;
3321 }
3322 }
3323 Assume(deps_it == deps_by_parent.end());
3324
3325 // Build a heap of all transactions with 0 unmet dependencies.
3326 std::make_heap(trim_heap.begin(), trim_heap.end(), cmp_fn);
3327
3328 // Iterate over to-be-included transactions, and convert them to included transactions, or
3329 // skip them if adding them would violate resource limits of the would-be cluster.
3330 //
3331 // It is possible that the heap empties without ever hitting either cluster limit, in case
3332 // the implied graph (to be added dependencies plus implicit dependency between each
3333 // original transaction and its predecessor in the linearization it came from) contains
3334 // cycles. Such cycles will be removed entirely, because each of the transactions in the
3335 // cycle permanently have unmet dependencies. However, this cannot occur in real scenarios
3336 // where Trim() is called to deal with reorganizations that would violate cluster limits,
3337 // as all added dependencies are in the same direction (from old mempool transactions to
3338 // new from-block transactions); cycles require dependencies in both directions to be
3339 // added.
3340 while (!trim_heap.empty()) {
3341 // Move the best remaining transaction to the end of trim_heap.
3342 std::pop_heap(trim_heap.begin(), trim_heap.end(), cmp_fn);
3343 // Pop it, and find its TrimTxData.
3344 auto& entry = *trim_heap.back();
3345 trim_heap.pop_back();
3346
3347 // Initialize it as a singleton partition.
3348 entry.m_uf_parent = &entry;
3349 entry.m_uf_count = 1;
3350 entry.m_uf_size = entry.m_tx_size;
3351
3352 // Find the distinct transaction partitions this entry depends on.
3353 current_deps.clear();
3354 for (auto& [par, chl] : std::span{deps_by_child}.subspan(entry.m_parent_offset, entry.m_parent_count)) {
3355 Assume(chl == entry.m_index);
3356 current_deps.push_back(find_fn(&*locate_fn(par)));
3357 }
3358 std::sort(current_deps.begin(), current_deps.end());
3359 current_deps.erase(std::unique(current_deps.begin(), current_deps.end()), current_deps.end());
3360
3361 // Compute resource counts.
3362 uint32_t new_count = 1;
3363 uint64_t new_size = entry.m_tx_size;
3364 for (TrimTxData* ptr : current_deps) {
3365 new_count += ptr->m_uf_count;
3366 new_size += ptr->m_uf_size;
3367 }
3368 // Skip the entry if this would violate any limit.
3369 if (new_count > m_max_cluster_count || new_size > m_max_cluster_size) continue;
3370
3371 // Union the partitions this transaction and all its dependencies are in together.
3372 auto rep = &entry;
3373 for (TrimTxData* ptr : current_deps) rep = union_fn(ptr, rep);
3374 // Mark the entry as included (so the loop below will not remove the transaction).
3375 entry.m_deps_left = uint32_t(-1);
3376 // Mark each to-be-added dependency involving this transaction as parent satisfied.
3377 for (auto& [par, chl] : std::span{deps_by_parent}.subspan(entry.m_children_offset, entry.m_children_count)) {
3378 Assume(par == entry.m_index);
3379 auto chl_it = locate_fn(chl);
3380 // Reduce the number of unmet dependencies of chl_it, and if that brings the number
3381 // to zero, add it to the heap of includable transactions.
3382 Assume(chl_it->m_deps_left > 0);
3383 if (--chl_it->m_deps_left == 0) {
3384 trim_heap.push_back(chl_it);
3385 std::push_heap(trim_heap.begin(), trim_heap.end(), cmp_fn);
3386 }
3387 }
3388 }
3389
3390 // Remove all the transactions that were not processed above. Because nothing gets
3391 // processed until/unless all its dependencies are met, this automatically guarantees
3392 // that if a transaction is removed, all its descendants, or would-be descendants, are
3393 // removed as well.
3394 for (const auto& trim_entry : trim_data) {
3395 if (trim_entry.m_deps_left != uint32_t(-1)) {
3396 ret.push_back(m_entries[trim_entry.m_index].m_ref);
3397 clusterset.m_to_remove.push_back(trim_entry.m_index);
3398 }
3399 }
3400 }
3401 clusterset.m_group_data.reset();
3402 clusterset.m_oversized = false;
3403 Assume(!ret.empty());
3404 return ret;
3405}
3406
3407size_t TxGraphImpl::GetMainMemoryUsage() noexcept
3408{
3409 // Make sure splits/merges are applied, as memory usage may not be representative otherwise.
3410 SplitAll(/*up_to_level=*/0);
3411 ApplyDependencies(/*level=*/0);
3412 // Compute memory usage
3413 size_t usage = /* From clusters */
3414 m_main_clusterset.m_cluster_usage +
3415 /* From Entry objects. */
3416 sizeof(Entry) * m_main_clusterset.m_txcount +
3417 /* From the chunk index. */
3418 memusage::DynamicUsage(m_main_chunkindex);
3419 return usage;
3420}
3421
3422} // namespace
3423
3425{
3426 if (m_graph) {
3427 // Inform the TxGraph about the Ref being destroyed.
3428 m_graph->UnlinkRef(m_index);
3429 m_graph = nullptr;
3430 }
3431}
3432
3433TxGraph::Ref::Ref(Ref&& other) noexcept
3434{
3435 // Inform the TxGraph of other that its Ref is being moved.
3436 if (other.m_graph) other.m_graph->UpdateRef(other.m_index, *this);
3437 // Actually move the contents.
3438 std::swap(m_graph, other.m_graph);
3439 std::swap(m_index, other.m_index);
3440}
3441
3442std::unique_ptr<TxGraph> MakeTxGraph(unsigned max_cluster_count, uint64_t max_cluster_size, uint64_t acceptable_iters) noexcept
3443{
3444 return std::make_unique<TxGraphImpl>(max_cluster_count, max_cluster_size, acceptable_iters);
3445}
int ret
ArgsManager & args
Definition: bitcoind.cpp:277
std::conditional_t<(BITS<=32), bitset_detail::IntBitSet< uint32_t >, std::conditional_t<(BITS<=std::numeric_limits< size_t >::digits), bitset_detail::IntBitSet< size_t >, bitset_detail::MultiIntBitSet< size_t,(BITS+std::numeric_limits< size_t >::digits - 1)/std::numeric_limits< size_t >::digits > > > BitSet
Definition: bitset.h:525
#define Assume(val)
Assume is the identity function.
Definition: check.h:125
Fast randomness source.
Definition: random.h:386
I randrange(I range) noexcept
Generate a random integer in the range [0..range), with range > 0.
Definition: random.h:254
Interface returned by GetBlockBuilder.
Definition: txgraph.h:179
virtual ~Ref()
Destroy this Ref.
Definition: txgraph.cpp:3424
Ref() noexcept=default
Construct an empty Ref.
Data structure to encapsulate fees, sizes, and dependencies for a set of transactions.
Definition: txgraph.h:47
uint32_t GraphIndex
Internal identifier for a transaction within a TxGraph.
Definition: txgraph.h:50
Data structure that holds a transaction graph's preprocessed data (fee, size, ancestors,...
const SetType & Ancestors(DepGraphIndex i) const noexcept
Get the ancestors of a given transaction i.
SetType GetReducedParents(DepGraphIndex i) const noexcept
Compute the (reduced) set of parents of node i in this graph.
const FeeFrac & FeeRate(DepGraphIndex i) const noexcept
Get the feerate of a given transaction i.
bool IsConnected(const SetType &subset) const noexcept
Determine if a subset is connected.
DepGraphIndex PositionRange() const noexcept
Get the range of positions in this DepGraph.
SetType FindConnectedComponent(const SetType &todo) const noexcept
Find some connected component within the subset "todo" of this graph.
DepGraphIndex AddTransaction(const FeeFrac &feefrac) noexcept
Add a new unconnected transaction to this transaction graph (in the first available position),...
void RemoveTransactions(const SetType &del) noexcept
Remove the specified positions from this DepGraph.
auto TxCount() const noexcept
Get the number of transactions in the graph.
const SetType & Descendants(DepGraphIndex i) const noexcept
Get the descendants of a given transaction i.
size_t DynamicMemoryUsage() const noexcept
const SetType & Positions() const noexcept
Get the set of transactions positions in use.
void Compact() noexcept
Reduce memory usage if possible.
void AddDependencies(const SetType &parents, DepGraphIndex child) noexcept
Modify this transaction graph, adding multiple parents to a specified child.
bool IsAcyclic() const noexcept
Check if this graph is acyclic.
uint64_t fee
uint64_t sequence
Level
Definition: logging.h:121
@ NONE
Definition: logging.h:86
std::tuple< std::vector< DepGraphIndex >, bool, uint64_t > Linearize(const DepGraph< SetType > &depgraph, uint64_t max_iterations, uint64_t rng_seed, std::span< const DepGraphIndex > old_linearization={}, bool is_topological=true) noexcept
Find or improve a linearization for a cluster.
std::vector< FeeFrac > ChunkLinearization(const DepGraph< SetType > &depgraph, std::span< const DepGraphIndex > linearization) noexcept
Compute the feerates of the chunks of linearization.
uint32_t DepGraphIndex
Data type to represent transaction indices in DepGraphs and the clusters they represent.
void PostLinearize(const DepGraph< SetType > &depgraph, std::span< DepGraphIndex > linearization)
Improve a given linearization.
std::vector< SetInfo< SetType > > ChunkLinearizationInfo(const DepGraph< SetType > &depgraph, std::span< const DepGraphIndex > linearization) noexcept
Compute the chunks of linearization as SetInfos.
static size_t DynamicUsage(const int8_t &v)
Dynamic memory usage for built-in types is zero.
Definition: memusage.h:31
static size_t MallocUsage(size_t alloc)
Compute the total memory used by allocating alloc bytes.
Definition: memusage.h:52
Definition: messages.h:21
std::vector< T > Split(const std::span< const char > &sp, std::string_view separators, bool include_sep=false)
Split a string on any char found in separators, returning a vector.
Definition: string.h:115
Data structure storing a fee and size, ordered by increasing fee/size.
Definition: feefrac.h:40
int64_t fee
Definition: feefrac.h:107
int32_t size
Definition: feefrac.h:108
bool IsEmpty() const noexcept
Check if this is empty (size and fee are 0).
Definition: feefrac.h:120
Tagged wrapper around FeeFrac to avoid unit confusion.
Definition: feefrac.h:239
static FeePerUnit FromFeeFrac(const FeeFrac &feefrac) noexcept
Convert a FeeFrac to a FeePerUnit.
Definition: feefrac.h:244
consteval auto _(util::TranslatedLiteral str)
Definition: translation.h:79
std::unique_ptr< TxGraph > MakeTxGraph(unsigned max_cluster_count, uint64_t max_cluster_size, uint64_t acceptable_iters) noexcept
Construct a new TxGraph with the specified limit on the number of transactions within a cluster,...
Definition: txgraph.cpp:3442
static constexpr unsigned MAX_CLUSTER_COUNT_LIMIT
Definition: txgraph.h:17
assert(!tx.IsCoinBase())
void ClearShrink(V &v) noexcept
Clear a vector (or std::deque) and release its allocated memory.
Definition: vector.h:56