txgraph: Add DoWork function (feature)

This can be called when the caller has time to spend now, and wants future operations
to be fast.
This commit is contained in:
Pieter Wuille 2025-01-09 14:22:24 -05:00
parent 295a1ca8bb
commit b685d322c9
3 changed files with 31 additions and 0 deletions

View file

@ -526,6 +526,10 @@ FUZZ_TARGET(txgraph)
// these here without making more calls to real, which could affect its internal // these here without making more calls to real, which could affect its internal
// state. A full comparison is done at the end. // state. A full comparison is done at the end.
break; break;
} else if (command-- == 0) {
// DoWork.
real->DoWork();
break;
} }
} }
} }

View file

@ -428,6 +428,8 @@ public:
void ApplyDependencies(int level) noexcept; void ApplyDependencies(int level) noexcept;
/** Make a specified Cluster have quality ACCEPTABLE or OPTIMAL. */ /** Make a specified Cluster have quality ACCEPTABLE or OPTIMAL. */
void MakeAcceptable(Cluster& cluster) noexcept; 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. // Implementations for the public TxGraph interface.
@ -436,6 +438,8 @@ public:
void AddDependency(const Ref& parent, const Ref& child) noexcept final; void AddDependency(const Ref& parent, const Ref& child) noexcept final;
void SetTransactionFee(const Ref&, int64_t fee) noexcept final; void SetTransactionFee(const Ref&, int64_t fee) noexcept final;
void DoWork() noexcept final;
void StartStaging() noexcept final; void StartStaging() noexcept final;
void CommitStaging() noexcept final; void CommitStaging() noexcept final;
void AbortStaging() 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 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. // 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 } // namespace
TxGraph::Ref::~Ref() TxGraph::Ref::~Ref()

View file

@ -91,6 +91,11 @@ public:
* effect. */ * effect. */
virtual void SetTransactionFee(const Ref& arg, int64_t fee) noexcept = 0; 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 /** 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 * 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 * be inspected, and then either discarded, or the main graph can be replaced by it by