Skip to content

Commit 22c68cd

Browse files
committed
txgraph: Allow Refs to outlive the TxGraph (feature)
1 parent 82fa357 commit 22c68cd

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

src/test/fuzz/txgraph.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,4 +587,10 @@ FUZZ_TARGET(txgraph)
587587

588588
// Sanity check again (because invoking inspectors may modify internal unobservable state).
589589
real->SanityCheck();
590+
591+
// Kill the TxGraph object.
592+
real.reset();
593+
// Kill the simulated graphs, with all remaining Refs in it. If any, this verifies that Refs
594+
// can outlive the TxGraph that created them.
595+
sims.clear();
590596
}

src/txgraph.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,9 @@ class TxGraphImpl final : public TxGraph
320320
Assume(max_cluster_count <= MAX_CLUSTER_COUNT_LIMIT);
321321
}
322322

323+
/** Destructor. */
324+
~TxGraphImpl() noexcept;
325+
323326
// Cannot move or copy (would invalidate TxGraphImpl* in Ref, MiningOrder, EvictionOrder).
324327
TxGraphImpl(const TxGraphImpl&) = delete;
325328
TxGraphImpl& operator=(const TxGraphImpl&) = delete;
@@ -809,6 +812,17 @@ void Cluster::ApplyDependencies(TxGraphImpl& graph, std::span<std::pair<GraphInd
809812
Updated(graph);
810813
}
811814

815+
TxGraphImpl::~TxGraphImpl() noexcept
816+
{
817+
// If Refs outlive the TxGraphImpl they refer to, unlink them, so that their destructor does not
818+
// try to reach into a non-existing TxGraphImpl anymore.
819+
for (auto& entry : m_entries) {
820+
if (entry.m_ref != nullptr) {
821+
GetRefGraph(*entry.m_ref) = nullptr;
822+
}
823+
}
824+
}
825+
812826
std::unique_ptr<Cluster> TxGraphImpl::ExtractCluster(int level, QualityLevel quality, ClusterSetIndex setindex) noexcept
813827
{
814828
Assume(quality != QualityLevel::NONE);

src/txgraph.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ class TxGraph
6363
/** Construct a new transaction with the specified feerate, and return a Ref to it.
6464
* If a staging graph exists, the new transaction is only created there. In all
6565
* further calls, only Refs created by AddTransaction() are allowed to be passed to this
66-
* TxGraph object (or empty Ref objects). */
66+
* TxGraph object (or empty Ref objects). Ref objects may outlive the TxGraph they were
67+
* created for. */
6768
[[nodiscard]] virtual Ref AddTransaction(const FeePerWeight& feerate) noexcept = 0;
6869
/** Remove the specified transaction. If a staging graph exists, the removal only happens
6970
* there. This is a no-op if the transaction was already removed.

0 commit comments

Comments
 (0)