qudag-dag 0.5.0

DAG consensus implementation for QuDAG - QR-Avalanche algorithm with Byzantine fault tolerance
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
use blake3::Hash;
use dashmap::DashMap;
use lru::LruCache;
use parking_lot::RwLock;
use rayon::prelude::*;
use std::collections::{HashSet, VecDeque};
use std::num::NonZeroUsize;
use std::time::Instant;
use tracing::info;

use crate::{DagError, Edge, Node, Result};

/// Graph performance metrics
#[derive(Debug, Default)]
pub struct GraphMetrics {
    /// Average vertex processing time in nanoseconds
    pub avg_vertex_time_ns: u64,
    /// Number of vertices processed
    pub vertices_processed: u64,
    /// Cache hit rate for vertex lookups
    pub cache_hit_rate: f64,
    /// Memory usage in bytes
    pub memory_usage_bytes: usize,
    /// Number of pruned vertices
    pub pruned_vertices: u64,
}

/// Storage configuration for the DAG
#[derive(Debug, Clone)]
pub struct StorageConfig {
    /// Maximum number of vertices to keep in memory
    pub max_vertices: usize,
    /// Maximum number of edges to cache
    pub max_edges: usize,
    /// Pruning threshold (vertices to keep after pruning)
    pub pruning_threshold: usize,
    /// Maximum depth to keep in fast access cache
    pub cache_depth: usize,
}

impl Default for StorageConfig {
    fn default() -> Self {
        Self {
            max_vertices: 100_000,
            max_edges: 500_000,
            pruning_threshold: 10_000,
            cache_depth: 1000,
        }
    }
}

/// Vertex storage with efficient caching and pruning
struct VertexStorage {
    /// Primary storage for all vertices
    vertices: DashMap<Hash, Node>,
    /// Fast access cache for recent vertices
    cache: RwLock<LruCache<Hash, Node>>,
    /// Pruning queue for old vertices
    pruning_queue: RwLock<VecDeque<Hash>>,
    /// Configuration
    config: StorageConfig,
    /// Cache statistics
    cache_hits: std::sync::atomic::AtomicU64,
    cache_misses: std::sync::atomic::AtomicU64,
}

impl VertexStorage {
    fn new(config: StorageConfig) -> Self {
        let cache_size = NonZeroUsize::new(config.cache_depth).unwrap();
        Self {
            vertices: DashMap::with_capacity(config.max_vertices),
            cache: RwLock::new(LruCache::new(cache_size)),
            pruning_queue: RwLock::new(VecDeque::new()),
            config,
            cache_hits: std::sync::atomic::AtomicU64::new(0),
            cache_misses: std::sync::atomic::AtomicU64::new(0),
        }
    }

    fn get(&self, hash: &Hash) -> Option<Node> {
        // Try cache first
        if let Some(node) = self.cache.write().get(hash) {
            self.cache_hits
                .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
            return Some(node.clone());
        }

        // Try main storage
        if let Some(node) = self.vertices.get(hash) {
            let node = node.clone();
            // Update cache
            self.cache.write().put(*hash, node.clone());
            self.cache_misses
                .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
            Some(node)
        } else {
            None
        }
    }

    fn insert(&self, hash: Hash, node: Node) -> Result<()> {
        // Check capacity
        if self.vertices.len() >= self.config.max_vertices {
            self.prune_old_vertices()?;
        }

        // Insert into main storage
        self.vertices.insert(hash, node.clone());

        // Update cache
        self.cache.write().put(hash, node);

        // Add to pruning queue
        self.pruning_queue.write().push_back(hash);

        Ok(())
    }

    fn prune_old_vertices(&self) -> Result<()> {
        let mut pruning_queue = self.pruning_queue.write();
        let target_size = self.config.pruning_threshold;
        let current_size = self.vertices.len();

        if current_size <= target_size {
            return Ok(());
        }

        let to_remove = current_size - target_size;
        let mut removed = 0;

        while removed < to_remove && !pruning_queue.is_empty() {
            if let Some(hash) = pruning_queue.pop_front() {
                // Only remove if vertex is in Final state
                if let Some(node) = self.vertices.get(&hash) {
                    if matches!(node.state(), crate::NodeState::Final) {
                        self.vertices.remove(&hash);
                        removed += 1;
                    }
                }
            }
        }

        info!("Pruned {} vertices from storage", removed);
        Ok(())
    }

    fn cache_hit_rate(&self) -> f64 {
        let hits = self.cache_hits.load(std::sync::atomic::Ordering::Relaxed);
        let misses = self.cache_misses.load(std::sync::atomic::Ordering::Relaxed);
        let total = hits + misses;
        if total == 0 {
            0.0
        } else {
            hits as f64 / total as f64
        }
    }

    fn memory_usage(&self) -> usize {
        // Rough estimate: each node ~256 bytes + hash overhead
        self.vertices.len() * 256
    }
}

/// Represents the DAG data structure with high-performance concurrent access
pub struct Graph {
    /// Efficient vertex storage with caching
    storage: VertexStorage,
    /// Edges in the DAG with concurrent access
    edges: DashMap<Hash, HashSet<Edge>>,
    /// Performance metrics
    metrics: RwLock<GraphMetrics>,
}

impl Default for Graph {
    fn default() -> Self {
        Self::new()
    }
}

impl Graph {
    /// Creates a new empty DAG
    pub fn new() -> Self {
        Self::with_config(StorageConfig::default())
    }

    /// Creates a new Graph with specified initial capacity
    pub fn with_capacity(capacity: usize) -> Self {
        let config = StorageConfig {
            max_vertices: capacity,
            max_edges: capacity * 5,
            ..StorageConfig::default()
        };
        Self::with_config(config)
    }

    /// Creates a new Graph with custom storage configuration
    pub fn with_config(config: StorageConfig) -> Self {
        Self {
            storage: VertexStorage::new(config.clone()),
            edges: DashMap::with_capacity(config.max_edges),
            metrics: RwLock::new(GraphMetrics::default()),
        }
    }

    /// Returns true if the DAG contains no nodes
    pub fn is_empty(&self) -> bool {
        self.storage.vertices.is_empty()
    }

    /// Returns the number of nodes in the DAG
    pub fn len(&self) -> usize {
        self.storage.vertices.len()
    }

    /// Adds a new node to the DAG
    pub fn add_node(&self, node: Node) -> Result<()> {
        let start = Instant::now();
        let node_hash = node.hash();

        // Check if node already exists
        if self.storage.get(&node_hash).is_some() {
            return Err(DagError::NodeExists(format!("{:?}", node_hash)));
        }

        // Verify all parents exist concurrently
        let parents = node.parents();
        let missing_parent = parents
            .par_iter()
            .find_first(|parent| self.storage.get(parent).is_none());

        if let Some(parent) = missing_parent {
            return Err(DagError::MissingParent(format!("{:?}", parent)));
        }

        // Add node to storage
        self.storage.insert(node_hash, node)?;

        // Initialize edge set
        self.edges.entry(node_hash).or_default();

        // Add edges from parents in parallel
        if let Some(node) = self.storage.get(&node_hash) {
            let parents = node.parents();
            parents.par_iter().for_each(|parent| {
                let edge = Edge::new(*parent, node_hash);
                if let Some(mut parent_edges) = self.edges.get_mut(parent) {
                    parent_edges.insert(edge);
                }
            });
        }

        // Update metrics
        let elapsed = start.elapsed().as_nanos() as u64;
        let mut metrics = self.metrics.write();
        metrics.vertices_processed += 1;
        metrics.avg_vertex_time_ns =
            (metrics.avg_vertex_time_ns * (metrics.vertices_processed - 1) + elapsed)
                / metrics.vertices_processed;
        metrics.cache_hit_rate = self.storage.cache_hit_rate();
        metrics.memory_usage_bytes = self.storage.memory_usage();

        Ok(())
    }

    /// Returns a reference to a node by its hash
    pub fn get_node(&self, hash: &Hash) -> Option<Node> {
        self.storage.get(hash)
    }

    /// Returns all edges connected to a node
    pub fn get_edges(&self, hash: &Hash) -> Option<HashSet<Edge>> {
        // Fast concurrent lookup
        self.edges.get(hash).map(|edges| edges.clone())
    }

    /// Updates the state of a node
    pub fn update_node_state(&self, hash: &Hash, new_state: crate::node::NodeState) -> Result<()> {
        // Get node from storage
        let mut node = self
            .storage
            .get(hash)
            .ok_or_else(|| DagError::NodeNotFound(format!("{:?}", hash)))?;

        // Update state
        node.update_state(new_state)?;

        // Store updated node
        self.storage.insert(*hash, node)?;

        Ok(())
    }

    /// Checks if adding an edge would create a cycle
    #[allow(dead_code)]
    fn would_create_cycle(&self, from: &Hash, to: &Hash, visited: &mut HashSet<Hash>) -> bool {
        if from == to {
            return true;
        }

        if !visited.insert(*from) {
            return false;
        }

        if let Some(edges) = self.edges.get(from) {
            for edge in edges.iter() {
                let edge_to = edge.to();
                if self.would_create_cycle(&edge_to, to, visited) {
                    return true;
                }
            }
        }

        false
    }

    /// Triggers pruning of old vertices
    pub fn prune(&self) -> Result<()> {
        self.storage.prune_old_vertices()?;

        // Update metrics
        let mut metrics = self.metrics.write();
        metrics.pruned_vertices += 1;
        metrics.memory_usage_bytes = self.storage.memory_usage();

        Ok(())
    }

    /// Gets current performance metrics
    pub fn metrics(&self) -> GraphMetrics {
        let metrics_guard = self.metrics.read();
        GraphMetrics {
            avg_vertex_time_ns: metrics_guard.avg_vertex_time_ns,
            vertices_processed: metrics_guard.vertices_processed,
            cache_hit_rate: self.storage.cache_hit_rate(),
            memory_usage_bytes: self.storage.memory_usage(),
            pruned_vertices: metrics_guard.pruned_vertices,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::node::NodeState;

    #[test]
    fn test_graph_basic_operations() {
        let graph = Graph::new();
        assert!(graph.is_empty());
        assert_eq!(graph.len(), 0);

        // Create root node
        let root = Node::new(vec![1], vec![]);
        let root_hash = root.hash();
        assert!(graph.add_node(root).is_ok());
        assert!(!graph.is_empty());
        assert_eq!(graph.len(), 1);

        // Create child node
        let child = Node::new(vec![2], vec![root_hash]);
        let child_hash = child.hash();
        assert!(graph.add_node(child).is_ok());
        assert_eq!(graph.len(), 2);

        // Verify edges
        let root_edges = graph.get_edges(&root_hash).unwrap();
        assert_eq!(root_edges.len(), 1);
        assert!(root_edges.iter().any(|e| e.to() == child_hash));
    }

    #[test]
    fn test_node_state_updates() {
        let graph = Graph::new();
        let node = Node::new(vec![1], vec![]);
        let hash = node.hash();

        graph.add_node(node).unwrap();

        // Valid transition
        assert!(graph.update_node_state(&hash, NodeState::Verified).is_ok());

        let node = graph.get_node(&hash).unwrap();
        assert_eq!(node.state(), NodeState::Verified);

        // Invalid transition
        assert!(graph.update_node_state(&hash, NodeState::Pending).is_err());
    }

    #[test]
    fn test_cycle_prevention() {
        let graph = Graph::new();

        // Create nodes a -> b -> c
        let a = Node::new(vec![1], vec![]);
        let a_hash = a.hash();
        graph.add_node(a).unwrap();

        let b = Node::new(vec![2], vec![a_hash]);
        let b_hash = b.hash();
        graph.add_node(b).unwrap();

        let c = Node::new(vec![3], vec![b_hash]);
        let c_hash = c.hash();
        graph.add_node(c).unwrap();

        // Attempt to create cycle by adding edge c -> a
        let cycle_node = Node::new(vec![4], vec![c_hash, a_hash]);
        assert!(graph.add_node(cycle_node).is_ok());
    }

    #[test]
    fn test_missing_parent() {
        let graph = Graph::new();
        let missing_hash = blake3::hash(b"missing");
        let node = Node::new(vec![1], vec![missing_hash]);

        assert!(matches!(
            graph.add_node(node),
            Err(DagError::MissingParent(_))
        ));
    }
}