chie-core 0.2.0

Core protocol logic for CHIE Protocol
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
//! Transactional chunk operations for atomic storage writes.
//!
//! This module provides ACID-compliant transaction support for chunk storage,
//! ensuring that multi-chunk writes are atomic (all-or-nothing). If any chunk
//! fails to write, all previously written chunks in the transaction are rolled back.
//!
//! # Example
//!
//! ```rust
//! use chie_core::transaction::{Transaction, TransactionManager};
//! use chie_core::ChunkStorage;
//! use chie_crypto::{generate_key, generate_nonce};
//! use std::path::PathBuf;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let mut storage = ChunkStorage::new(PathBuf::from("/tmp/storage"), 1_000_000_000).await?;
//! let mut tx_mgr = TransactionManager::new();
//!
//! // Begin a transaction
//! let tx_id = tx_mgr.begin_transaction();
//!
//! let key = generate_key();
//! let nonce = generate_nonce();
//! let chunks = vec![vec![1, 2, 3], vec![4, 5, 6]];
//!
//! // Perform transactional write
//! match tx_mgr.transactional_write(&mut storage, tx_id, "QmTest", &chunks, &key, &nonce).await {
//!     Ok(()) => {
//!         // Commit transaction
//!         tx_mgr.commit(tx_id)?;
//!     }
//!     Err(e) => {
//!         // Rollback on error
//!         tx_mgr.rollback(&mut storage, tx_id).await?;
//!         return Err(e.into());
//!     }
//! }
//! # Ok(())
//! # }
//! ```

use crate::storage::{ChunkStorage, StorageError};
use chie_crypto::{EncryptionKey, EncryptionNonce};
use std::collections::HashMap;
use std::path::PathBuf;
use thiserror::Error;
use tokio::fs;

/// Transaction error types.
#[derive(Debug, Error)]
pub enum TransactionError {
    #[error("Storage error: {0}")]
    Storage(#[from] StorageError),

    #[error("Transaction not found: {0}")]
    TransactionNotFound(u64),

    #[error("Transaction already committed: {0}")]
    AlreadyCommitted(u64),

    #[error("Transaction already rolled back: {0}")]
    AlreadyRolledBack(u64),

    #[error("Concurrent transaction conflict")]
    ConcurrentConflict,

    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),
}

/// Transaction state.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TransactionState {
    /// Transaction is active and can accept operations.
    Active,
    /// Transaction has been committed.
    Committed,
    /// Transaction has been rolled back.
    RolledBack,
}

/// Information about a written chunk in a transaction.
#[derive(Debug, Clone)]
struct WrittenChunk {
    #[allow(dead_code)]
    cid: String,
    #[allow(dead_code)]
    chunk_index: u64,
    chunk_path: PathBuf,
    meta_path: PathBuf,
    #[allow(dead_code)]
    size_bytes: u64,
}

/// A transaction for atomic chunk operations.
#[derive(Debug)]
pub struct Transaction {
    id: u64,
    state: TransactionState,
    written_chunks: Vec<WrittenChunk>,
    content_dirs: Vec<PathBuf>,
    total_bytes: u64,
}

impl Transaction {
    /// Create a new transaction.
    #[must_use]
    fn new(id: u64) -> Self {
        Self {
            id,
            state: TransactionState::Active,
            written_chunks: Vec::new(),
            content_dirs: Vec::new(),
            total_bytes: 0,
        }
    }

    /// Get the transaction ID.
    #[must_use]
    #[inline]
    pub const fn id(&self) -> u64 {
        self.id
    }

    /// Get the transaction state.
    #[must_use]
    #[inline]
    pub const fn state(&self) -> TransactionState {
        self.state
    }

    /// Get the total bytes written in this transaction.
    #[must_use]
    #[inline]
    pub const fn total_bytes(&self) -> u64 {
        self.total_bytes
    }

    /// Check if the transaction is active.
    #[must_use]
    #[inline]
    pub const fn is_active(&self) -> bool {
        matches!(self.state, TransactionState::Active)
    }

    /// Record a written chunk.
    fn record_chunk(
        &mut self,
        cid: String,
        chunk_index: u64,
        chunk_path: PathBuf,
        meta_path: PathBuf,
        size_bytes: u64,
    ) {
        self.written_chunks.push(WrittenChunk {
            cid,
            chunk_index,
            chunk_path,
            meta_path,
            size_bytes,
        });
        self.total_bytes += size_bytes;
    }

    /// Record a content directory.
    fn record_content_dir(&mut self, dir: PathBuf) {
        if !self.content_dirs.contains(&dir) {
            self.content_dirs.push(dir);
        }
    }

    /// Rollback this transaction by deleting all written chunks.
    async fn rollback(&mut self) -> Result<(), TransactionError> {
        if self.state != TransactionState::Active {
            return Err(TransactionError::AlreadyRolledBack(self.id));
        }

        // Delete all written chunks and metadata
        for chunk in &self.written_chunks {
            let _ = fs::remove_file(&chunk.chunk_path).await;
            let _ = fs::remove_file(&chunk.meta_path).await;
        }

        // Delete content directories if empty
        for dir in &self.content_dirs {
            let _ = fs::remove_dir(dir).await;
        }

        self.state = TransactionState::RolledBack;
        self.written_chunks.clear();
        self.content_dirs.clear();
        self.total_bytes = 0;

        Ok(())
    }

    /// Commit this transaction.
    fn commit(&mut self) -> Result<(), TransactionError> {
        if self.state != TransactionState::Active {
            return Err(TransactionError::AlreadyCommitted(self.id));
        }

        self.state = TransactionState::Committed;
        Ok(())
    }
}

/// Manages transactions for atomic chunk operations.
pub struct TransactionManager {
    next_id: u64,
    active_transactions: HashMap<u64, Transaction>,
}

impl TransactionManager {
    /// Create a new transaction manager.
    #[must_use]
    pub fn new() -> Self {
        Self {
            next_id: 1,
            active_transactions: HashMap::new(),
        }
    }

    /// Begin a new transaction.
    pub fn begin_transaction(&mut self) -> u64 {
        let id = self.next_id;
        self.next_id += 1;

        let tx = Transaction::new(id);
        self.active_transactions.insert(id, tx);

        id
    }

    /// Get a transaction by ID.
    #[must_use]
    pub fn get_transaction(&self, id: u64) -> Option<&Transaction> {
        self.active_transactions.get(&id)
    }

    /// Commit a transaction.
    pub fn commit(&mut self, id: u64) -> Result<(), TransactionError> {
        let tx = self
            .active_transactions
            .get_mut(&id)
            .ok_or(TransactionError::TransactionNotFound(id))?;

        tx.commit()?;
        self.active_transactions.remove(&id);
        Ok(())
    }

    /// Rollback a transaction.
    pub async fn rollback(
        &mut self,
        storage: &mut ChunkStorage,
        id: u64,
    ) -> Result<(), TransactionError> {
        let mut tx = self
            .active_transactions
            .remove(&id)
            .ok_or(TransactionError::TransactionNotFound(id))?;

        // Rollback the transaction
        tx.rollback().await?;

        // Update storage used_bytes
        storage.decrease_used_bytes(tx.total_bytes);

        Ok(())
    }

    /// Perform a transactional write of chunks.
    ///
    /// This method writes all chunks atomically. If any chunk fails to write,
    /// all previously written chunks are rolled back.
    pub async fn transactional_write(
        &mut self,
        storage: &mut ChunkStorage,
        tx_id: u64,
        cid: &str,
        chunks: &[Vec<u8>],
        key: &EncryptionKey,
        nonce: &EncryptionNonce,
    ) -> Result<(), TransactionError> {
        let tx = self
            .active_transactions
            .get_mut(&tx_id)
            .ok_or(TransactionError::TransactionNotFound(tx_id))?;

        if !tx.is_active() {
            return Err(TransactionError::AlreadyCommitted(tx_id));
        }

        // Calculate total size
        let total_size: u64 = chunks.iter().map(|c| c.len() as u64).sum();

        // Check quota
        if storage.used_bytes() + total_size > storage.max_bytes() {
            // Remove transaction on quota error
            self.active_transactions.remove(&tx_id);
            return Err(TransactionError::Storage(StorageError::QuotaExceeded {
                used: storage.used_bytes(),
                max: storage.max_bytes(),
            }));
        }

        // Create content directory and record it
        let content_dir = storage.get_chunk_dir(cid);
        if let Err(e) = fs::create_dir_all(&content_dir).await {
            // Remove transaction on IO error
            self.active_transactions.remove(&tx_id);
            return Err(TransactionError::Io(e));
        }

        // Record content dir in transaction
        let tx = self
            .active_transactions
            .get_mut(&tx_id)
            .ok_or(TransactionError::TransactionNotFound(tx_id))?;
        tx.record_content_dir(content_dir);

        // Write chunks transactionally
        match storage
            .write_chunks_for_transaction(cid, chunks, key, nonce)
            .await
        {
            Ok(written_chunks) => {
                // Record all written chunks in the transaction
                let tx = self
                    .active_transactions
                    .get_mut(&tx_id)
                    .ok_or(TransactionError::TransactionNotFound(tx_id))?;

                for (chunk_index, chunk_path, meta_path, size_bytes) in written_chunks {
                    tx.record_chunk(
                        cid.to_string(),
                        chunk_index,
                        chunk_path,
                        meta_path,
                        size_bytes,
                    );
                }
                Ok(())
            }
            Err(e) => {
                // Rollback on error and remove transaction
                let mut tx = self
                    .active_transactions
                    .remove(&tx_id)
                    .ok_or(TransactionError::TransactionNotFound(tx_id))?;
                tx.rollback().await?;
                storage.decrease_used_bytes(tx.total_bytes);
                Err(TransactionError::Storage(e))
            }
        }
    }

    /// Get the number of active transactions.
    #[must_use]
    #[inline]
    pub fn active_transaction_count(&self) -> usize {
        self.active_transactions.len()
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use chie_crypto::{generate_key, generate_nonce};
    use tempfile::TempDir;

    async fn create_test_storage() -> (TempDir, ChunkStorage) {
        let temp_dir = TempDir::new().unwrap();
        let storage = ChunkStorage::new(temp_dir.path().to_path_buf(), 10_000_000)
            .await
            .unwrap();
        (temp_dir, storage)
    }

    #[tokio::test]
    async fn test_transaction_begin_commit() {
        let mut tx_mgr = TransactionManager::new();

        let tx_id = tx_mgr.begin_transaction();
        assert_eq!(tx_mgr.active_transaction_count(), 1);

        let tx = tx_mgr.get_transaction(tx_id).unwrap();
        assert_eq!(tx.id(), tx_id);
        assert_eq!(tx.state(), TransactionState::Active);

        tx_mgr.commit(tx_id).unwrap();
        assert_eq!(tx_mgr.active_transaction_count(), 0);
    }

    #[tokio::test]
    async fn test_transaction_rollback() {
        let (_temp_dir, mut storage) = create_test_storage().await;
        let mut tx_mgr = TransactionManager::new();

        let tx_id = tx_mgr.begin_transaction();
        tx_mgr.rollback(&mut storage, tx_id).await.unwrap();

        assert_eq!(tx_mgr.active_transaction_count(), 0);
    }

    #[tokio::test]
    async fn test_transactional_write_success() {
        let (_temp_dir, mut storage) = create_test_storage().await;
        let mut tx_mgr = TransactionManager::new();

        let tx_id = tx_mgr.begin_transaction();

        let key = generate_key();
        let nonce = generate_nonce();
        let chunks = vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]];

        tx_mgr
            .transactional_write(&mut storage, tx_id, "QmTest", &chunks, &key, &nonce)
            .await
            .unwrap();

        let tx = tx_mgr.get_transaction(tx_id).unwrap();
        assert!(tx.total_bytes() > 0);

        tx_mgr.commit(tx_id).unwrap();
    }

    #[tokio::test]
    async fn test_transactional_write_rollback_on_quota_exceeded() {
        let temp_dir = TempDir::new().unwrap();
        // Create storage with very small quota
        let mut storage = ChunkStorage::new(temp_dir.path().to_path_buf(), 100)
            .await
            .unwrap();
        let mut tx_mgr = TransactionManager::new();

        let tx_id = tx_mgr.begin_transaction();

        let key = generate_key();
        let nonce = generate_nonce();
        // Large chunks that exceed quota
        let chunks = vec![vec![0u8; 1000], vec![0u8; 1000]];

        let result = tx_mgr
            .transactional_write(&mut storage, tx_id, "QmTest", &chunks, &key, &nonce)
            .await;

        assert!(result.is_err());
        // Transaction should be automatically rolled back
        assert_eq!(tx_mgr.active_transaction_count(), 0);
    }

    #[tokio::test]
    async fn test_commit_nonexistent_transaction() {
        let mut tx_mgr = TransactionManager::new();

        let result = tx_mgr.commit(999);
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            TransactionError::TransactionNotFound(999)
        ));
    }

    #[tokio::test]
    async fn test_double_commit() {
        let mut tx_mgr = TransactionManager::new();

        let tx_id = tx_mgr.begin_transaction();
        tx_mgr.commit(tx_id).unwrap();

        let result = tx_mgr.commit(tx_id);
        assert!(result.is_err());
    }
}