From b685d322c9739ca03b9d0bb9fa57aabea1890060 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Thu, 9 Jan 2025 14:22:24 -0500 Subject: [PATCH] txgraph: Add DoWork function (feature) This can be called when the caller has time to spend now, and wants future operations to be fast. --- src/test/fuzz/txgraph.cpp | 4 ++++ src/txgraph.cpp | 22 ++++++++++++++++++++++ src/txgraph.h | 5 +++++ 3 files changed, 31 insertions(+) diff --git a/src/test/fuzz/txgraph.cpp b/src/test/fuzz/txgraph.cpp index fca253cfc8d..250d0554dd9 100644 --- a/src/test/fuzz/txgraph.cpp +++ b/src/test/fuzz/txgraph.cpp @@ -526,6 +526,10 @@ FUZZ_TARGET(txgraph) // these here without making more calls to real, which could affect its internal // state. A full comparison is done at the end. break; + } else if (command-- == 0) { + // DoWork. + real->DoWork(); + break; } } } diff --git a/src/txgraph.cpp b/src/txgraph.cpp index 2d96d2802c4..fe220406677 100644 --- a/src/txgraph.cpp +++ b/src/txgraph.cpp @@ -428,6 +428,8 @@ public: void ApplyDependencies(int level) noexcept; /** Make a specified Cluster have quality ACCEPTABLE or OPTIMAL. */ void MakeAcceptable(Cluster& cluster) noexcept; + /** Make all Clusters at the specified level have quality ACCEPTABLE or OPTIMAL. */ + void MakeAllAcceptable(int level) noexcept; // Implementations for the public TxGraph interface. @@ -436,6 +438,8 @@ public: void AddDependency(const Ref& parent, const Ref& child) noexcept final; void SetTransactionFee(const Ref&, int64_t fee) noexcept final; + void DoWork() noexcept final; + void StartStaging() noexcept final; void CommitStaging() noexcept final; void AbortStaging() noexcept final; @@ -1370,6 +1374,17 @@ void TxGraphImpl::MakeAcceptable(Cluster& cluster) noexcept } } +void TxGraphImpl::MakeAllAcceptable(int level) noexcept +{ + ApplyDependencies(level); + auto& clusterset = GetClusterSet(level); + if (clusterset.m_oversized == true) return; + auto& queue = clusterset.m_clusters[int(QualityLevel::NEEDS_RELINEARIZE)]; + while (!queue.empty()) { + MakeAcceptable(*queue.back().get()); + } +} + Cluster::Cluster(TxGraphImpl& graph, const FeePerWeight& feerate, GraphIndex graph_index) noexcept { // Create a new transaction in the DepGraph, and remember its position in m_mapping. @@ -1942,6 +1957,13 @@ void TxGraphImpl::SanityCheck() const } } +void TxGraphImpl::DoWork() noexcept +{ + for (int level = 0; level <= GetTopLevel(); ++level) { + MakeAllAcceptable(level); + } +} + } // namespace TxGraph::Ref::~Ref() diff --git a/src/txgraph.h b/src/txgraph.h index e90d6b3ae46..163aaaa793a 100644 --- a/src/txgraph.h +++ b/src/txgraph.h @@ -91,6 +91,11 @@ public: * effect. */ virtual void SetTransactionFee(const Ref& arg, int64_t fee) noexcept = 0; + /** TxGraph is internally lazy, and will not compute many things until they are needed. + * Calling DoWork will compute everything now, so that future operations are fast. This can be + * invoked while oversized. */ + virtual void DoWork() noexcept = 0; + /** Create a staging graph (which cannot exist already). This acts as if a full copy of * the transaction graph is made, upon which further modifications are made. This copy can * be inspected, and then either discarded, or the main graph can be replaced by it by