refactor: Separate out logic for building a tree-shaped dependency graph
What changed, and why it matters
This commit is a simple code cleanup: it takes a block of code that built a simplified tree-shaped dependency graph inside one fuzz test and moves it into a reusable helper function named BuildTreeGraph. The behavior is unchanged; no security issue is introduced or fixed.
No security action needed; this is a non-functional refactor in test/fuzz code.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change refactors src/test/fuzz/cluster_linearize.cpp by extracting the tree-dependency-graph construction logic from FUZZ_TARGET(clusterlin_postlinearize_tree) into a new templated function BuildTreeGraph. The original inline code is replaced with a single call to that function. Logic, control flow, and data handling are identical; only code organization changed.
Changed components
src/test/fuzz/cluster_linearize.cppInspect captured patch +40 / −29
diff --git a/src/test/fuzz/cluster_linearize.cpp b/src/test/fuzz/cluster_linearize.cpp
index d2286cae..c1ee7802 100644
--- a/src/test/fuzz/cluster_linearize.cpp
+++ b/src/test/fuzz/cluster_linearize.cpp
@@ -364,6 +364,45 @@ std::vector<DepGraphIndex> ReadLinearization(const DepGraph<BS>& depgraph, SpanR
return linearization;
}
+/** Given a dependency graph, construct a tree-structured graph.
+ *
+ * Copies the nodes from the depgraph, but only keeps the first parent (even direction)
+ * or the first child (odd direction) for each transaction.
+ */
+template<typename BS>
+DepGraph<BS> BuildTreeGraph(const DepGraph<BS>& depgraph, uint8_t direction)
+{
+ DepGraph<BS> depgraph_tree;
+ for (DepGraphIndex i = 0; i < depgraph.PositionRange(); ++i) {
+ if (depgraph.Positions()[i]) {
+ depgraph_tree.AddTransaction(depgraph.FeeRate(i));
+ } else {
+ // For holes, add a dummy transaction which is deleted below, so that non-hole
+ // transactions retain their position.
+ depgraph_tree.AddTransaction(FeeFrac{});
+ }
+ }
+ depgraph_tree.RemoveTransactions(BS::Fill(depgraph.PositionRange()) - depgraph.Positions());
+
+ if (direction & 1) {
+ for (DepGraphIndex i : depgraph.Positions()) {
+ auto children = depgraph.GetReducedChildren(i);
+ if (children.Any()) {
+ depgraph_tree.AddDependencies(BS::Singleton(i), children.First());
+ }
+ }
+ } else {
+ for (DepGraphIndex i : depgraph.Positions()) {
+ auto parents = depgraph.GetReducedParents(i);
+ if (parents.Any()) {
+ depgraph_tree.AddDependencies(BS::Singleton(parents.First()), i);
+ }
+ }
+ }
+
+ return depgraph_tree;
+}
+
} // namespace
FUZZ_TARGET(clusterlin_depgraph_sim)
@@ -1260,35 +1299,7 @@ FUZZ_TARGET(clusterlin_postlinearize_tree)
reader >> direction >> rng_seed >> Using<DepGraphFormatter>(depgraph_gen);
} catch (const std::ios_base::failure&) {}
- // Now construct a new graph, copying the nodes, but leaving only the first parent (even
- // direction) or the first child (odd direction).
- DepGraph<TestBitSet> depgraph_tree;
- for (DepGraphIndex i = 0; i < depgraph_gen.PositionRange(); ++i) {
- if (depgraph_gen.Positions()[i]) {
- depgraph_tree.AddTransaction(depgraph_gen.FeeRate(i));
- } else {
- // For holes, add a dummy transaction which is deleted below, so that non-hole
- // transactions retain their position.
- depgraph_tree.AddTransaction(FeeFrac{});
- }
- }
- depgraph_tree.RemoveTransactions(TestBitSet::Fill(depgraph_gen.PositionRange()) - depgraph_gen.Positions());
-
- if (direction & 1) {
- for (DepGraphIndex i : depgraph_gen.Positions()) {
- auto children = depgraph_gen.GetReducedChildren(i);
- if (children.Any()) {
- depgraph_tree.AddDependencies(TestBitSet::Singleton(i), children.First());
- }
- }
- } else {
- for (DepGraphIndex i : depgraph_gen.Positions()) {
- auto parents = depgraph_gen.GetReducedParents(i);
- if (parents.Any()) {
- depgraph_tree.AddDependencies(TestBitSet::Singleton(parents.First()), i);
- }
- }
- }
+ auto depgraph_tree = BuildTreeGraph(depgraph_gen, direction);
// Retrieve a linearization from the fuzz input.
std::vector<DepGraphIndex> linearization;
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.