| Crates.io | qudag-protocol |
| lib.rs | qudag-protocol |
| version | 0.5.0 |
| created_at | 2025-06-20 18:07:04.583469+00 |
| updated_at | 2025-06-23 20:53:53.222512+00 |
| description | Protocol implementation for QuDAG - Orchestrates crypto, DAG, and network components |
| homepage | |
| repository | https://github.com/ruvnet/QuDAG |
| max_upload_size | |
| id | 1719960 |
| size | 588,834 |
Protocol implementation and coordination layer for the QuDAG system.
Add to your Cargo.toml:
[dependencies]
qudag-protocol = "0.1"
use qudag_protocol::{Node, NodeConfig};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = NodeConfig {
node_id: "my-node".to_string(),
listen_port: 8080,
rpc_port: 9090,
data_dir: "./data".into(),
..Default::default()
};
let node = Node::new(config).await?;
node.start().await?;
Ok(())
}
use qudag_protocol::{Message, MessageType, ProtocolVersion};
// Create a protocol message
let message = Message {
version: ProtocolVersion::V1,
message_type: MessageType::Transaction,
payload: b"transaction data".to_vec(),
timestamp: std::time::SystemTime::now(),
};
// Process the message
// protocol.process_message(message).await?;
use qudag_protocol::{RpcServer, RpcTransport};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let server = RpcServer::new("127.0.0.1:9090").await?;
// Start RPC server
server.start().await?;
// Server will handle JSON-RPC requests
Ok(())
}
use qudag_protocol::{StateStore, MemoryStateStore, FileStateStore};
// In-memory storage for testing
let memory_store = MemoryStateStore::new();
// File-based storage for production
let file_store = FileStateStore::new("./state")?;
// Store and retrieve state
memory_store.save_state("key", &data).await?;
let retrieved = memory_store.load_state("key").await?;
┌─────────────────────┐
│ RPC Server │ ← External API
├─────────────────────┤
│ Protocol Layer │ ← Message coordination
├─────────────────────┤
│ DAG Consensus │ ← Consensus algorithm
├─────────────────────┤
│ P2P Network │ ← Peer communication
├─────────────────────┤
│ Cryptography │ ← Quantum-resistant crypto
└─────────────────────┘
use qudag_protocol::{NodeConfig, ProtocolConfig};
let config = NodeConfig {
node_id: "node-1".to_string(),
listen_port: 8080,
rpc_port: 9090,
data_dir: "./data".into(),
max_peers: 50,
protocol_config: ProtocolConfig {
version: ProtocolVersion::V1,
handshake_timeout: Duration::from_secs(30),
message_timeout: Duration::from_secs(60),
..Default::default()
},
..Default::default()
};
The protocol supports various message types:
use qudag_protocol::{MessageType, Message};
// Transaction messages
let tx_message = Message::new(MessageType::Transaction, payload);
// Consensus messages
let consensus_message = Message::new(MessageType::Consensus, payload);
// Network discovery
let discovery_message = Message::new(MessageType::Discovery, payload);
// Administrative commands
let admin_message = Message::new(MessageType::Admin, payload);
The protocol provides a JSON-RPC API:
node.status - Get node status and healthnode.peers - List connected peersnetwork.send_message - Send message to peerdag.get_tips - Get current DAG tipsdag.add_vertex - Add vertex to DAGcurl -X POST http://localhost:9090 \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "node.status",
"id": 1
}'
use qudag_protocol::{ProtocolState, ProtocolStateMachine};
let state_machine = ProtocolStateMachine::new();
// Transition states
state_machine.transition_to(ProtocolState::Connecting).await?;
state_machine.transition_to(ProtocolState::Connected).await?;
// Get current state
let current = state_machine.current_state().await;
Comprehensive error types for protocol operations:
use qudag_protocol::{ProtocolError, MessageError, StateError};
match result {
Err(ProtocolError::MessageError(e)) => {
eprintln!("Message error: {}", e);
}
Err(ProtocolError::StateError(e)) => {
eprintln!("State error: {}", e);
}
Ok(value) => {
// Handle success
}
}
rocksdb: Enable RocksDB backend for state storagecompression: Enable message compressionmetrics: Enable performance metrics collectionThe protocol includes memory tracking and optimization:
use qudag_protocol::{get_memory_usage, MemoryTracker};
// Get current memory usage
let usage = get_memory_usage();
println!("Memory usage: {} bytes", usage);
// Track memory for specific operations
let tracker = MemoryTracker::new();
// ... perform operations ...
let delta = tracker.delta();
Licensed under either MIT or Apache-2.0 at your option.