qudag-protocol 0.5.0

Protocol implementation for QuDAG - Orchestrates crypto, DAG, and network components
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
//! Transaction processing module for QuDAG protocol.

use crate::types::ProtocolError;
use qudag_crypto::{ml_dsa::MlDsa65, signature::{Signature, SignatureError}};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use thiserror::Error;
use tracing::{debug, error, info};

/// Transaction processing errors
#[derive(Debug, Error)]
pub enum TransactionError {
    #[error("Invalid transaction format")]
    InvalidFormat,
    
    #[error("Insufficient balance")]
    InsufficientBalance,
    
    #[error("Invalid signature")]
    InvalidSignature,
    
    #[error("Double spending detected")]
    DoubleSpending,
    
    #[error("Transaction not found")]
    NotFound,
    
    #[error("Validation failed: {0}")]
    ValidationFailed(String),
    
    #[error("Crypto error: {0}")]
    CryptoError(String),
}

/// Transaction input
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct TransactionInput {
    /// Previous transaction hash
    pub prev_tx_hash: [u8; 32],
    /// Output index in previous transaction
    pub prev_output_index: u32,
    /// Signature script
    pub signature_script: Vec<u8>,
}

/// Transaction output
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct TransactionOutput {
    /// Amount to transfer
    pub amount: u64,
    /// Recipient public key hash
    pub recipient: [u8; 32],
    /// Locking script
    pub locking_script: Vec<u8>,
}

/// Transaction structure
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Transaction {
    /// Transaction version
    pub version: u32,
    /// Transaction inputs
    pub inputs: Vec<TransactionInput>,
    /// Transaction outputs
    pub outputs: Vec<TransactionOutput>,
    /// Transaction timestamp
    pub timestamp: u64,
    /// Transaction nonce (for replay protection)
    pub nonce: u64,
    /// Fee amount
    pub fee: u64,
    /// Transaction signature
    pub signature: Vec<u8>,
}

impl Transaction {
    /// Create new transaction
    pub fn new(
        inputs: Vec<TransactionInput>,
        outputs: Vec<TransactionOutput>,
        fee: u64,
    ) -> Self {
        Self {
            version: 1,
            inputs,
            outputs,
            timestamp: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_secs(),
            nonce: 0,
            fee,
            signature: Vec::new(),
        }
    }

    /// Calculate transaction hash
    pub fn hash(&self) -> [u8; 32] {
        use sha3::{Digest, Sha3_256};
        
        let mut hasher = Sha3_256::new();
        
        // Hash version
        hasher.update(self.version.to_le_bytes());
        
        // Hash inputs
        for input in &self.inputs {
            hasher.update(input.prev_tx_hash);
            hasher.update(input.prev_output_index.to_le_bytes());
            hasher.update(&input.signature_script);
        }
        
        // Hash outputs
        for output in &self.outputs {
            hasher.update(output.amount.to_le_bytes());
            hasher.update(output.recipient);
            hasher.update(&output.locking_script);
        }
        
        // Hash timestamp, nonce, and fee
        hasher.update(self.timestamp.to_le_bytes());
        hasher.update(self.nonce.to_le_bytes());
        hasher.update(self.fee.to_le_bytes());
        
        hasher.finalize().into()
    }

    /// Sign transaction
    pub fn sign(&mut self, private_key: &[u8]) -> Result<(), TransactionError> {
        let hash = self.hash();
        
        // Create ML-DSA signature
        let ml_dsa = MlDsa65::new()
            .map_err(|e| TransactionError::CryptoError(e.to_string()))?;
        
        self.signature = ml_dsa.sign(private_key, &hash)
            .map_err(|e| TransactionError::CryptoError(e.to_string()))?;
        
        Ok(())
    }

    /// Verify transaction signature
    pub fn verify_signature(&self, public_key: &[u8]) -> Result<bool, TransactionError> {
        if self.signature.is_empty() {
            return Ok(false);
        }
        
        let hash = self.hash();
        
        let ml_dsa = MlDsa65::new()
            .map_err(|e| TransactionError::CryptoError(e.to_string()))?;
        
        ml_dsa.verify(public_key, &hash, &self.signature)
            .map_err(|e| TransactionError::CryptoError(e.to_string()))
    }

    /// Get total input amount
    pub fn total_input_amount(&self, utxo_set: &UTXOSet) -> Result<u64, TransactionError> {
        let mut total = 0u64;
        
        for input in &self.inputs {
            if let Some(utxo) = utxo_set.get_utxo(&input.prev_tx_hash, input.prev_output_index) {
                total = total.checked_add(utxo.amount)
                    .ok_or(TransactionError::ValidationFailed("Integer overflow".to_string()))?;
            } else {
                return Err(TransactionError::ValidationFailed("UTXO not found".to_string()));
            }
        }
        
        Ok(total)
    }

    /// Get total output amount
    pub fn total_output_amount(&self) -> Result<u64, TransactionError> {
        let mut total = 0u64;
        
        for output in &self.outputs {
            total = total.checked_add(output.amount)
                .ok_or(TransactionError::ValidationFailed("Integer overflow".to_string()))?;
        }
        
        Ok(total)
    }

    /// Validate transaction
    pub fn validate(&self, utxo_set: &UTXOSet, public_key: &[u8]) -> Result<(), TransactionError> {
        // Check basic format
        if self.inputs.is_empty() {
            return Err(TransactionError::ValidationFailed("No inputs".to_string()));
        }
        
        if self.outputs.is_empty() {
            return Err(TransactionError::ValidationFailed("No outputs".to_string()));
        }

        // Verify signature
        if !self.verify_signature(public_key)? {
            return Err(TransactionError::InvalidSignature);
        }

        // Check balance
        let input_amount = self.total_input_amount(utxo_set)?;
        let output_amount = self.total_output_amount()?;
        
        if input_amount < output_amount + self.fee {
            return Err(TransactionError::InsufficientBalance);
        }

        // Check for double spending
        for input in &self.inputs {
            if utxo_set.is_spent(&input.prev_tx_hash, input.prev_output_index) {
                return Err(TransactionError::DoubleSpending);
            }
        }

        Ok(())
    }
}

/// Unspent Transaction Output (UTXO)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct UTXO {
    /// Transaction hash
    pub tx_hash: [u8; 32],
    /// Output index
    pub output_index: u32,
    /// Output amount
    pub amount: u64,
    /// Recipient
    pub recipient: [u8; 32],
    /// Locking script
    pub locking_script: Vec<u8>,
    /// Block height when created
    pub block_height: u64,
}

/// UTXO Set for tracking unspent outputs
#[derive(Debug, Clone)]
pub struct UTXOSet {
    /// Map of (tx_hash, output_index) -> UTXO
    utxos: HashMap<([u8; 32], u32), UTXO>,
    /// Set of spent UTXOs
    spent: HashMap<([u8; 32], u32), bool>,
}

impl UTXOSet {
    /// Create new UTXO set
    pub fn new() -> Self {
        Self {
            utxos: HashMap::new(),
            spent: HashMap::new(),
        }
    }

    /// Add UTXO to set
    pub fn add_utxo(&mut self, utxo: UTXO) {
        let key = (utxo.tx_hash, utxo.output_index);
        self.utxos.insert(key, utxo);
        self.spent.remove(&key); // Unmark as spent if it was
    }

    /// Get UTXO from set
    pub fn get_utxo(&self, tx_hash: &[u8; 32], output_index: u32) -> Option<&UTXO> {
        let key = (*tx_hash, output_index);
        if self.spent.get(&key).copied().unwrap_or(false) {
            None
        } else {
            self.utxos.get(&key)
        }
    }

    /// Mark UTXO as spent
    pub fn spend_utxo(&mut self, tx_hash: &[u8; 32], output_index: u32) {
        let key = (*tx_hash, output_index);
        self.spent.insert(key, true);
    }

    /// Check if UTXO is spent
    pub fn is_spent(&self, tx_hash: &[u8; 32], output_index: u32) -> bool {
        let key = (*tx_hash, output_index);
        self.spent.get(&key).copied().unwrap_or(false)
    }

    /// Get balance for address
    pub fn get_balance(&self, address: &[u8; 32]) -> u64 {
        self.utxos
            .values()
            .filter(|utxo| !self.is_spent(&utxo.tx_hash, utxo.output_index))
            .filter(|utxo| utxo.recipient == *address)
            .map(|utxo| utxo.amount)
            .sum()
    }

    /// Get UTXOs for address
    pub fn get_utxos_for_address(&self, address: &[u8; 32]) -> Vec<&UTXO> {
        self.utxos
            .values()
            .filter(|utxo| !self.is_spent(&utxo.tx_hash, utxo.output_index))
            .filter(|utxo| utxo.recipient == *address)
            .collect()
    }
}

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

/// Transaction processor
pub struct TransactionProcessor {
    /// UTXO set
    utxo_set: UTXOSet,
    /// Pending transactions
    pending_txs: HashMap<[u8; 32], Transaction>,
    /// Transaction pool
    tx_pool: Vec<Transaction>,
}

impl TransactionProcessor {
    /// Create new transaction processor
    pub fn new() -> Self {
        Self {
            utxo_set: UTXOSet::new(),
            pending_txs: HashMap::new(),
            tx_pool: Vec::new(),
        }
    }

    /// Add transaction to pool
    pub fn add_transaction(&mut self, tx: Transaction, public_key: &[u8]) -> Result<(), TransactionError> {
        // Validate transaction
        tx.validate(&self.utxo_set, public_key)?;
        
        let tx_hash = tx.hash();
        
        // Add to pool
        self.tx_pool.push(tx.clone());
        self.pending_txs.insert(tx_hash, tx);
        
        info!("Added transaction to pool: {:?}", hex::encode(tx_hash));
        Ok(())
    }

    /// Process transaction (commit to UTXO set)
    pub fn process_transaction(&mut self, tx_hash: &[u8; 32]) -> Result<(), TransactionError> {
        let tx = self.pending_txs.remove(tx_hash)
            .ok_or(TransactionError::NotFound)?;

        // Spend inputs
        for input in &tx.inputs {
            self.utxo_set.spend_utxo(&input.prev_tx_hash, input.prev_output_index);
        }

        // Create new UTXOs for outputs
        for (index, output) in tx.outputs.iter().enumerate() {
            let utxo = UTXO {
                tx_hash: *tx_hash,
                output_index: index as u32,
                amount: output.amount,
                recipient: output.recipient,
                locking_script: output.locking_script.clone(),
                block_height: 0, // TODO: Get current block height
            };
            self.utxo_set.add_utxo(utxo);
        }

        // Remove from pool
        self.tx_pool.retain(|pool_tx| pool_tx.hash() != *tx_hash);

        info!("Processed transaction: {:?}", hex::encode(tx_hash));
        Ok(())
    }

    /// Get pending transactions
    pub fn get_pending_transactions(&self) -> Vec<&Transaction> {
        self.tx_pool.iter().collect()
    }

    /// Get UTXO set
    pub fn get_utxo_set(&self) -> &UTXOSet {
        &self.utxo_set
    }

    /// Get balance for address
    pub fn get_balance(&self, address: &[u8; 32]) -> u64 {
        self.utxo_set.get_balance(address)
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use qudag_crypto::ml_dsa::MlDsa65;

    #[test]
    fn test_transaction_creation() {
        let input = TransactionInput {
            prev_tx_hash: [1; 32],
            prev_output_index: 0,
            signature_script: vec![],
        };

        let output = TransactionOutput {
            amount: 100,
            recipient: [2; 32],
            locking_script: vec![],
        };

        let tx = Transaction::new(vec![input], vec![output], 1);
        
        assert_eq!(tx.version, 1);
        assert_eq!(tx.inputs.len(), 1);
        assert_eq!(tx.outputs.len(), 1);
        assert_eq!(tx.fee, 1);
    }

    #[test]
    fn test_transaction_hash() {
        let input = TransactionInput {
            prev_tx_hash: [1; 32],
            prev_output_index: 0,
            signature_script: vec![],
        };

        let output = TransactionOutput {
            amount: 100,
            recipient: [2; 32],
            locking_script: vec![],
        };

        let tx1 = Transaction::new(vec![input.clone()], vec![output.clone()], 1);
        let tx2 = Transaction::new(vec![input], vec![output], 1);
        
        // Same transactions should have same hash (timestamp will differ)
        // So we'll just test that hash is deterministic for same content
        let hash1 = tx1.hash();
        let hash2 = tx1.hash();
        assert_eq!(hash1, hash2);
    }

    #[tokio::test]
    async fn test_transaction_signing() {
        let ml_dsa = MlDsa65::new().unwrap();
        let (pk, sk) = ml_dsa.keygen().unwrap();

        let input = TransactionInput {
            prev_tx_hash: [1; 32],
            prev_output_index: 0,
            signature_script: vec![],
        };

        let output = TransactionOutput {
            amount: 100,
            recipient: [2; 32],
            locking_script: vec![],
        };

        let mut tx = Transaction::new(vec![input], vec![output], 1);
        
        // Sign transaction
        tx.sign(sk.as_bytes()).unwrap();
        assert!(!tx.signature.is_empty());

        // Verify signature
        assert!(tx.verify_signature(pk.as_bytes()).unwrap());
    }

    #[test]
    fn test_utxo_set() {
        let mut utxo_set = UTXOSet::new();
        
        let utxo = UTXO {
            tx_hash: [1; 32],
            output_index: 0,
            amount: 100,
            recipient: [2; 32],
            locking_script: vec![],
            block_height: 1,
        };

        // Add UTXO
        utxo_set.add_utxo(utxo.clone());
        
        // Check balance
        assert_eq!(utxo_set.get_balance(&[2; 32]), 100);
        
        // Spend UTXO
        utxo_set.spend_utxo(&[1; 32], 0);
        assert_eq!(utxo_set.get_balance(&[2; 32]), 0);
        assert!(utxo_set.is_spent(&[1; 32], 0));
    }

    #[tokio::test]
    async fn test_transaction_processor() {
        let mut processor = TransactionProcessor::new();
        let ml_dsa = MlDsa65::new().unwrap();
        let (pk, sk) = ml_dsa.keygen().unwrap();

        // Create a genesis UTXO
        let genesis_utxo = UTXO {
            tx_hash: [0; 32],
            output_index: 0,
            amount: 1000,
            recipient: [1; 32],
            locking_script: vec![],
            block_height: 0,
        };
        processor.utxo_set.add_utxo(genesis_utxo);

        // Create transaction spending the genesis UTXO
        let input = TransactionInput {
            prev_tx_hash: [0; 32],
            prev_output_index: 0,
            signature_script: vec![],
        };

        let output = TransactionOutput {
            amount: 900,
            recipient: [2; 32],
            locking_script: vec![],
        };

        let mut tx = Transaction::new(vec![input], vec![output], 100);
        tx.sign(sk.as_bytes()).unwrap();

        // Add transaction to processor
        processor.add_transaction(tx.clone(), pk.as_bytes()).unwrap();
        
        // Process transaction
        let tx_hash = tx.hash();
        processor.process_transaction(&tx_hash).unwrap();

        // Check balances
        assert_eq!(processor.get_balance(&[1; 32]), 0); // Original owner
        assert_eq!(processor.get_balance(&[2; 32]), 900); // New owner
    }
}