From 22c68cd153b72f867dffcc7a62a3f65cef9038fb Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Fri, 21 Mar 2025 15:17:42 -0400 Subject: [PATCH] txgraph: Allow Refs to outlive the TxGraph (feature) --- src/test/fuzz/txgraph.cpp | 6 ++++++ src/txgraph.cpp | 14 ++++++++++++++ src/txgraph.h | 3 ++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/test/fuzz/txgraph.cpp b/src/test/fuzz/txgraph.cpp index 976839464b7..d85b81d6402 100644 --- a/src/test/fuzz/txgraph.cpp +++ b/src/test/fuzz/txgraph.cpp @@ -587,4 +587,10 @@ FUZZ_TARGET(txgraph) // Sanity check again (because invoking inspectors may modify internal unobservable state). real->SanityCheck(); + + // Kill the TxGraph object. + real.reset(); + // Kill the simulated graphs, with all remaining Refs in it. If any, this verifies that Refs + // can outlive the TxGraph that created them. + sims.clear(); } diff --git a/src/txgraph.cpp b/src/txgraph.cpp index 7312a402bf3..b90911c5920 100644 --- a/src/txgraph.cpp +++ b/src/txgraph.cpp @@ -320,6 +320,9 @@ public: Assume(max_cluster_count <= MAX_CLUSTER_COUNT_LIMIT); } + /** Destructor. */ + ~TxGraphImpl() noexcept; + // Cannot move or copy (would invalidate TxGraphImpl* in Ref, MiningOrder, EvictionOrder). TxGraphImpl(const TxGraphImpl&) = delete; TxGraphImpl& operator=(const TxGraphImpl&) = delete; @@ -809,6 +812,17 @@ void Cluster::ApplyDependencies(TxGraphImpl& graph, std::span TxGraphImpl::ExtractCluster(int level, QualityLevel quality, ClusterSetIndex setindex) noexcept { Assume(quality != QualityLevel::NONE); diff --git a/src/txgraph.h b/src/txgraph.h index 8f9451921eb..27f6ab21751 100644 --- a/src/txgraph.h +++ b/src/txgraph.h @@ -63,7 +63,8 @@ public: /** Construct a new transaction with the specified feerate, and return a Ref to it. * If a staging graph exists, the new transaction is only created there. In all * further calls, only Refs created by AddTransaction() are allowed to be passed to this - * TxGraph object (or empty Ref objects). */ + * TxGraph object (or empty Ref objects). Ref objects may outlive the TxGraph they were + * created for. */ [[nodiscard]] virtual Ref AddTransaction(const FeePerWeight& feerate) noexcept = 0; /** Remove the specified transaction. If a staging graph exists, the removal only happens * there. This is a no-op if the transaction was already removed.