Skip to content

Commit af3e8d2

Browse files
committed
mempool/txgraph: remove unneeded functionality
1 parent a049e38 commit af3e8d2

File tree

6 files changed

+0
-551
lines changed

6 files changed

+0
-551
lines changed

mempool/txgraph/graph.go

Lines changed: 0 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,6 @@ type Config struct {
4646
// mempool eviction policies in the caller.
4747
MaxNodes int
4848

49-
// MaxEdges limits the total number of parent-child relationships. This
50-
// provides defense against attacks that try to create extremely
51-
// connected transaction graphs to degrade performance.
52-
MaxEdges int
53-
54-
// EnableCaching enables memoization of expensive computations like
55-
// ancestor/descendant counts. This trades memory for speed, which is
56-
// beneficial in production but may complicate debugging.
57-
EnableCaching bool
58-
59-
// CacheTimeout defines how long cached computation results remain
60-
// valid. Shorter timeouts trade freshness for computation cost.
61-
CacheTimeout time.Duration
62-
6349
// MaxPackageSize limits the number of transactions in a package.
6450
// Bitcoin Core uses 101 (25 ancestors + 25 descendants + 1 root),
6551
// enforced here to prevent package relay DoS attacks.
@@ -75,9 +61,6 @@ type Config struct {
7561
func DefaultConfig() *Config {
7662
return &Config{
7763
MaxNodes: 100000,
78-
MaxEdges: 200000,
79-
EnableCaching: true,
80-
CacheTimeout: 5 * time.Second,
8164
MaxPackageSize: 101,
8265
}
8366
}
@@ -444,61 +427,6 @@ func (g *TxGraph) HasTransaction(hash chainhash.Hash) bool {
444427
return exists
445428
}
446429

447-
// AddEdge adds an edge between two nodes.
448-
func (g *TxGraph) AddEdge(parentHash, childHash chainhash.Hash) error {
449-
g.mu.Lock()
450-
defer g.mu.Unlock()
451-
452-
parent, parentExists := g.nodes[parentHash]
453-
child, childExists := g.nodes[childHash]
454-
455-
if !parentExists || !childExists {
456-
return ErrNodeNotFound
457-
}
458-
459-
// Check if edge already exists.
460-
if _, exists := parent.Children[childHash]; exists {
461-
return nil // Already connected
462-
}
463-
464-
// Check for cycles.
465-
if g.wouldCreateCycle(parent, child) {
466-
return ErrCycleDetected
467-
}
468-
469-
// Add edge.
470-
parent.Children[childHash] = child
471-
child.Parents[parentHash] = parent
472-
atomic.AddInt32(&g.metrics.edgeCount, 1)
473-
474-
// Update clusters.
475-
g.mergeNodeClusters(parent, child)
476-
477-
return nil
478-
}
479-
480-
// RemoveEdge removes an edge between two nodes.
481-
func (g *TxGraph) RemoveEdge(parentHash, childHash chainhash.Hash) error {
482-
g.mu.Lock()
483-
defer g.mu.Unlock()
484-
485-
parent, parentExists := g.nodes[parentHash]
486-
child, childExists := g.nodes[childHash]
487-
488-
if !parentExists || !childExists {
489-
return ErrNodeNotFound
490-
}
491-
492-
// Remove edge if it exists.
493-
if _, exists := parent.Children[childHash]; exists {
494-
delete(parent.Children, childHash)
495-
delete(child.Parents, parentHash)
496-
atomic.AddInt32(&g.metrics.edgeCount, -1)
497-
}
498-
499-
return nil
500-
}
501-
502430
// GetAncestors returns all ancestors of a transaction up to maxDepth.
503431
func (g *TxGraph) GetAncestors(hash chainhash.Hash,
504432
maxDepth int) map[chainhash.Hash]*TxGraphNode {
@@ -511,13 +439,6 @@ func (g *TxGraph) GetAncestors(hash chainhash.Hash,
511439
return nil
512440
}
513441

514-
// Check cache if enabled.
515-
if g.config.EnableCaching &&
516-
time.Since(node.cachedMetrics.LastUpdated) < g.config.CacheTimeout {
517-
// For now, skip cache and compute directly.
518-
// TODO: Implement proper caching.
519-
}
520-
521442
ancestors := make(map[chainhash.Hash]*TxGraphNode)
522443
visited := make(map[chainhash.Hash]bool)
523444
g.collectAncestorsRecursive(node, ancestors, visited, 0, maxDepth)
@@ -798,8 +719,6 @@ func (g *TxGraph) createNewCluster(node *TxGraphNode) {
798719
}
799720

800721
cluster.Nodes[node.TxHash] = node
801-
cluster.Roots = []*TxGraphNode{node}
802-
cluster.Leaves = []*TxGraphNode{node}
803722

804723
g.indexes.clusters[clusterID] = cluster
805724
g.indexes.nodeToCluster[node.TxHash] = clusterID
@@ -822,9 +741,6 @@ func (g *TxGraph) addToCluster(node *TxGraphNode, clusterID ClusterID) {
822741
g.indexes.nodeToCluster[node.TxHash] = clusterID
823742

824743
node.Metadata.ClusterID = clusterID
825-
826-
// Update roots and leaves.
827-
g.updateClusterBoundaries(cluster)
828744
}
829745

830746
// mergeClusters merges multiple clusters into one.
@@ -878,7 +794,6 @@ func (g *TxGraph) mergeClusters(
878794
}
879795

880796
targetCluster.Size = len(targetCluster.Nodes)
881-
g.updateClusterBoundaries(targetCluster)
882797
}
883798

884799
// mergeNodeClusters merges clusters when adding an edge.
@@ -897,18 +812,3 @@ func (g *TxGraph) mergeNodeClusters(parent, child *TxGraphNode) {
897812
g.mergeClusters(parent, clusters)
898813
}
899814

900-
// updateClusterBoundaries updates roots and leaves of a cluster.
901-
func (g *TxGraph) updateClusterBoundaries(cluster *TxCluster) {
902-
cluster.Roots = nil
903-
cluster.Leaves = nil
904-
905-
for _, node := range cluster.Nodes {
906-
if len(node.Parents) == 0 {
907-
cluster.Roots = append(cluster.Roots, node)
908-
}
909-
if len(node.Children) == 0 {
910-
cluster.Leaves = append(cluster.Leaves, node)
911-
}
912-
}
913-
}
914-

mempool/txgraph/graph_test.go

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -191,16 +191,6 @@ func TestGraphEdges(t *testing.T) {
191191
metrics := g.GetMetrics()
192192
require.Equal(t, 2, metrics.NodeCount)
193193
require.Equal(t, 1, metrics.EdgeCount)
194-
195-
err = g.RemoveEdge(*parent.Hash(), *child.Hash())
196-
require.NoError(t, err)
197-
198-
// Edge removal should update both nodes' relationship maps and
199-
// decrement the edge count metric.
200-
parentNode, _ = g.GetNode(*parent.Hash())
201-
childNode, _ = g.GetNode(*child.Hash())
202-
require.Len(t, parentNode.Children, 0)
203-
require.Len(t, childNode.Parents, 0)
204194
}
205195

206196
// TestGraphAncestorsDescendants verifies that ancestor and descendant
@@ -256,30 +246,6 @@ func TestGraphAncestorsDescendants(t *testing.T) {
256246
require.NotNil(t, descendants[*txs[2].Hash()])
257247
}
258248

259-
// TestCycleDetection verifies that the graph prevents cycles, which would
260-
// violate the DAG property required for transaction dependencies. Cycles
261-
// would make ancestor/descendant queries infinite loop and break topological
262-
// ordering for block template construction.
263-
func TestCycleDetection(t *testing.T) {
264-
g := New(DefaultConfig())
265-
266-
tx1, desc1 := createTestTx(nil, 1)
267-
tx2, desc2 := createTestTx(
268-
[]wire.OutPoint{{Hash: *tx1.Hash(), Index: 0}}, 1,
269-
)
270-
271-
err := g.AddTransaction(tx1, desc1)
272-
require.NoError(t, err)
273-
err = g.AddTransaction(tx2, desc2)
274-
require.NoError(t, err)
275-
276-
// Attempting to add an edge that would create a cycle (tx2 -> tx1
277-
// when tx1 -> tx2 already exists) must be rejected to maintain the
278-
// DAG invariant.
279-
err = g.AddEdge(*tx2.Hash(), *tx1.Hash())
280-
require.ErrorIs(t, err, ErrCycleDetected)
281-
}
282-
283249
// TestClusterManagement verifies that transactions are correctly grouped
284250
// into clusters (connected components) and that clusters merge when a
285251
// transaction bridges two previously separate clusters. This is essential
@@ -393,46 +359,6 @@ func TestGetNodeCount(t *testing.T) {
393359
require.Equal(t, 2, g.GetNodeCount())
394360
}
395361

396-
// TestAddEdgeErrors tests error cases in AddEdge.
397-
func TestAddEdgeErrors(t *testing.T) {
398-
g := New(DefaultConfig())
399-
400-
// Try to add edge between non-existent nodes.
401-
tx1Msg := wire.NewMsgTx(1)
402-
tx2Msg := wire.NewMsgTx(1)
403-
hash1 := tx1Msg.TxHash()
404-
hash2 := tx2Msg.TxHash()
405-
406-
err := g.AddEdge(hash1, hash2)
407-
require.Error(t, err)
408-
require.Equal(t, ErrNodeNotFound, err)
409-
410-
// Add one node and try to add edge.
411-
tx1, desc1 := createTestTx(nil, 1)
412-
require.NoError(t, g.AddTransaction(tx1, desc1))
413-
414-
err = g.AddEdge(*tx1.Hash(), hash2)
415-
require.Error(t, err)
416-
require.Equal(t, ErrNodeNotFound, err)
417-
418-
// Add second node.
419-
tx2, desc2 := createTestTx(nil, 1)
420-
require.NoError(t, g.AddTransaction(tx2, desc2))
421-
422-
// Add valid edge.
423-
err = g.AddEdge(*tx1.Hash(), *tx2.Hash())
424-
require.NoError(t, err)
425-
426-
// Try to add duplicate edge.
427-
err = g.AddEdge(*tx1.Hash(), *tx2.Hash())
428-
require.NoError(t, err)
429-
430-
// Try to create cycle.
431-
err = g.AddEdge(*tx2.Hash(), *tx1.Hash())
432-
require.Error(t, err)
433-
require.Equal(t, ErrCycleDetected, err)
434-
}
435-
436362
// TestRemoveTransactionComplex tests complex removal scenarios.
437363
func TestRemoveTransactionComplex(t *testing.T) {
438364
g := New(DefaultConfig())

0 commit comments

Comments
 (0)