stigmerge_peer 0.6.2

stigmerge p2p protocol and agent components
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
use std::{cmp::Ordering, collections::HashMap, io::SeekFrom, ops::Deref, sync::Arc};

use sha2::{Digest, Sha256};
use stigmerge_fileindex::{Index, BLOCK_SIZE_BYTES, PIECE_SIZE_BLOCKS, PIECE_SIZE_BYTES};
use tokio::{
    fs::File,
    io::{AsyncReadExt, AsyncSeekExt},
    sync::{Mutex, RwLock},
};
use tracing::warn;

use crate::{error::Result, types::PieceState};

#[derive(Clone)]
pub struct PieceVerifier {
    inner: Arc<Mutex<PieceVerifierInner>>,
}

impl PieceVerifier {
    pub async fn new(index: Arc<RwLock<Index>>) -> Self {
        Self {
            inner: Arc::new(Mutex::new(PieceVerifierInner::new(index).await)),
        }
    }

    pub async fn subscribe(&self, handler: Box<dyn PieceStatusHandler + Send + Sync>) {
        let mut inner = self.inner.lock().await;
        inner.add_status_handler(handler);
    }

    pub async fn update_piece(&self, piece_state: PieceState) -> Result<()> {
        let mut inner = self.inner.lock().await;
        let status = inner.handle_update(piece_state).await?;
        inner.handlers.piece_status(&status)
    }

    pub async fn is_complete(&self) -> bool {
        let inner = self.inner.lock().await;
        inner.verified_pieces.len() == inner.n_pieces
    }
}

pub trait PieceStatusHandler {
    fn piece_status(&self, status: &PieceStatus) -> Result<()>;
}

pub struct PieceStatusNotifier {
    verified_tx: flume::Sender<PieceStatus>,
}

impl PieceStatusNotifier {
    pub fn new() -> (Self, flume::Receiver<PieceStatus>) {
        let (verified_tx, verified_rx) = flume::unbounded();
        (Self { verified_tx }, verified_rx)
    }
}

impl PieceStatusHandler for PieceStatusNotifier {
    fn piece_status(&self, status: &PieceStatus) -> Result<()> {
        self.verified_tx.send(status.clone())?;
        Ok(())
    }
}

#[derive(Default)]
struct HandlerChain {
    handlers: Vec<Box<dyn PieceStatusHandler + Send + Sync>>,
}

impl HandlerChain {
    fn add(&mut self, handler: Box<dyn PieceStatusHandler + Send + Sync>) {
        self.handlers.push(handler);
    }
}

impl PieceStatusHandler for HandlerChain {
    fn piece_status(&self, status: &PieceStatus) -> Result<()> {
        for handler in self.handlers.iter() {
            handler.piece_status(status)?;
        }
        Ok(())
    }
}

struct PieceVerifierInner {
    index: Arc<RwLock<Index>>,
    pending_pieces: HashMap<(usize, usize), PieceState>,
    verified_pieces: HashMap<(usize, usize), PieceState>,
    n_pieces: usize,
    handlers: HandlerChain,
}

impl PieceVerifierInner {
    async fn new(index: Arc<RwLock<Index>>) -> Self {
        let pending_pieces = Self::empty_pieces(index.read().await.deref());
        let n_pieces = pending_pieces.len();
        Self {
            index,
            pending_pieces,
            verified_pieces: HashMap::new(),
            n_pieces,
            handlers: HandlerChain::default(),
        }
    }

    fn add_status_handler(&mut self, handler: Box<dyn PieceStatusHandler + Send + Sync>) {
        self.handlers.add(handler);
    }

    async fn handle_update(&mut self, piece_state: PieceState) -> Result<PieceStatus> {
        // update piece state
        let piece_state = match self.pending_pieces.remove(&piece_state.key()) {
            Some(prior_state) => prior_state.merged(piece_state),
            None => piece_state,
        };

        let status = if piece_state.is_complete() {
            // verify complete ones
            match self
                .verify_piece(piece_state.file_index, piece_state.piece_index)
                .await
            {
                Ok(true) => {
                    self.verified_pieces.insert(piece_state.key(), piece_state);
                    PieceStatus::ValidPiece {
                        file_index: piece_state.file_index,
                        piece_index: piece_state.piece_index,
                        index_complete: self.verified_pieces.len() == self.n_pieces,
                    }
                }
                Ok(false) => {
                    // invalid piece, still pending
                    self.pending_pieces.insert(piece_state.key(), piece_state);
                    PieceStatus::InvalidPiece {
                        file_index: piece_state.file_index,
                        piece_index: piece_state.piece_index,
                    }
                }
                Err(err) => {
                    // error verifying piece in local file, back to pending
                    self.pending_pieces.insert(piece_state.key(), piece_state);
                    warn!(?err, "verifying piece");
                    return Err(err);
                }
            }
        } else {
            // indicate incomplete ones
            self.pending_pieces.insert(piece_state.key(), piece_state);
            PieceStatus::IncompletePiece {
                file_index: piece_state.file_index,
                piece_index: piece_state.piece_index,
            }
        };

        Ok(status)
    }

    async fn verify_piece(&self, file_index: usize, piece_index: usize) -> Result<bool> {
        let index = self.index.read().await;

        let file_spec = &index.files()[file_index];
        let mut fh = File::open(index.root().join(file_spec.path())).await?;
        let piece_spec = &index.payload().pieces()[piece_index];

        // FIXME: this is wrong for multi-file!
        // We'd need to seek relative to the file's payload slice
        fh.seek(SeekFrom::Start((piece_index * PIECE_SIZE_BYTES) as u64))
            .await?;
        let mut buf = [0u8; BLOCK_SIZE_BYTES];
        let mut digest = Sha256::new();
        for _ in 0..PIECE_SIZE_BLOCKS {
            let rd = fh.read(&mut buf[..]).await?;
            if rd == 0 {
                break;
            }
            digest.update(&buf[..rd]);
        }
        let expected_digest = piece_spec.digest();
        let actual_digest: [u8; 32] = digest.finalize().into();
        Ok(expected_digest.cmp(&actual_digest[..]) == Ordering::Equal)
    }

    fn empty_pieces(index: &Index) -> HashMap<(usize, usize), PieceState> {
        let mut result = HashMap::new();
        for (file_index, file) in index.files().iter().enumerate() {
            let n_pieces = (file.contents().length() / PIECE_SIZE_BYTES)
                + if file.contents().length() % PIECE_SIZE_BYTES > 0 {
                    1
                } else {
                    0
                };
            let starting_piece = file.contents().starting_piece();
            for piece_index in starting_piece..starting_piece + n_pieces {
                result.insert(
                    (file_index, piece_index),
                    PieceState::new(
                        file_index,
                        piece_index,
                        0,
                        index.payload().pieces()[piece_index].block_count(),
                        0,
                    ),
                );
            }
        }
        result
    }
}

#[derive(Clone, Debug, PartialEq)]
pub enum PieceStatus {
    IncompletePiece {
        file_index: usize,
        piece_index: usize,
    },
    ValidPiece {
        file_index: usize,
        piece_index: usize,
        index_complete: bool,
    },
    InvalidPiece {
        file_index: usize,
        piece_index: usize,
    },
}

impl PieceStatus {
    pub fn piece_index(&self) -> usize {
        match self {
            PieceStatus::IncompletePiece { piece_index, .. } => *piece_index,
            PieceStatus::ValidPiece { piece_index, .. } => *piece_index,
            PieceStatus::InvalidPiece { piece_index, .. } => *piece_index,
        }
    }

    pub fn index_complete(&self) -> bool {
        match self {
            PieceStatus::ValidPiece { index_complete, .. } => *index_complete,
            _ => false,
        }
    }
}

#[cfg(test)]
mod tests {
    use std::io::{Seek, Write};
    use stigmerge_fileindex::Indexer;

    use crate::tests::temp_file;

    use super::*;

    #[tokio::test]
    async fn verify_index() {
        const NUM_PIECES: usize = 3;

        // Create a test file with known content
        let tf = temp_file(0xa5u8, PIECE_SIZE_BYTES * NUM_PIECES); // 3 pieces

        // Create index from the file
        let indexer = Indexer::from_file(tf.path()).await.expect("indexer");

        // Set up verifier
        let verifier =
            PieceVerifier::new(Arc::new(RwLock::new(indexer.index().await.expect("index")))).await;
        let (notifier, status_rx) = PieceStatusNotifier::new();
        verifier.subscribe(Box::new(notifier)).await;

        for piece_index in 0..NUM_PIECES {
            // Building up to piece validation
            for block_index in 0..PIECE_SIZE_BLOCKS - 1 {
                let piece_state =
                    PieceState::new(0, piece_index, 0, PIECE_SIZE_BLOCKS, block_index);
                verifier
                    .update_piece(piece_state)
                    .await
                    .expect("update piece");

                let status = status_rx.recv().expect("receive status");
                assert_eq!(
                    status,
                    PieceStatus::IncompletePiece {
                        file_index: 0,
                        piece_index: piece_index
                    }
                );
            }
            // Piece complete & verified
            let piece_state =
                PieceState::new(0, piece_index, 0, PIECE_SIZE_BLOCKS, PIECE_SIZE_BLOCKS - 1);
            verifier
                .update_piece(piece_state)
                .await
                .expect("update piece");

            let status = status_rx.recv().expect("receive status");
            assert_eq!(
                status,
                PieceStatus::ValidPiece {
                    file_index: 0,
                    piece_index: piece_index,
                    index_complete: piece_index == 2, // last piece will complete the index
                }
            );
        }
    }

    #[tokio::test]
    async fn verify_invalid_piece() {
        const CORRUPT_PIECE_INDEX: usize = 0;
        const VALID_PIECE_INDEX: usize = 1;

        // Create a test file with known content
        let mut tf = temp_file(0xa5u8, PIECE_SIZE_BYTES * 2); // 2 pieces

        // Create index from the file
        let indexer = Indexer::from_file(tf.path()).await.expect("indexer");

        // Set up verifier
        let verifier =
            PieceVerifier::new(Arc::new(RwLock::new(indexer.index().await.expect("index")))).await;
        let (notifier, status_rx) = PieceStatusNotifier::new();
        verifier.subscribe(Box::new(notifier)).await;

        // Building up to corrupt piece validation
        for block_index in 0..PIECE_SIZE_BLOCKS - 1 {
            let piece_state =
                PieceState::new(0, CORRUPT_PIECE_INDEX, 0, PIECE_SIZE_BLOCKS, block_index);
            verifier
                .update_piece(piece_state)
                .await
                .expect("update piece");

            let status = status_rx.recv().expect("receive status");
            assert_eq!(
                status,
                PieceStatus::IncompletePiece {
                    file_index: 0,
                    piece_index: CORRUPT_PIECE_INDEX,
                }
            );
        }

        // Corrupt the first piece after indexing
        {
            let f = tf.as_file_mut();
            f.seek(SeekFrom::Start(0)).expect("seek");
            f.write_all(&[0xFFu8; 1024]).expect("write");
            f.flush().expect("flush");
        }

        // Validate corrupted piece
        let piece_state = PieceState::new(
            0,
            CORRUPT_PIECE_INDEX,
            0,
            PIECE_SIZE_BLOCKS,
            PIECE_SIZE_BLOCKS - 1,
        );
        verifier
            .update_piece(piece_state)
            .await
            .expect("update piece");

        let status = status_rx.recv().expect("receive status");
        assert_eq!(
            status,
            PieceStatus::InvalidPiece {
                file_index: 0,
                piece_index: CORRUPT_PIECE_INDEX,
            }
        );

        // Build up to validation of intact piece
        for block_index in 0..PIECE_SIZE_BLOCKS - 1 {
            let piece_state =
                PieceState::new(0, VALID_PIECE_INDEX, 0, PIECE_SIZE_BLOCKS, block_index);
            verifier
                .update_piece(piece_state)
                .await
                .expect("update piece");

            let status = status_rx.recv().expect("receive status");
            assert_eq!(
                status,
                PieceStatus::IncompletePiece {
                    file_index: 0,
                    piece_index: VALID_PIECE_INDEX,
                }
            );
        }

        // Validate other intact piece
        let piece_state = PieceState::new(
            0,
            VALID_PIECE_INDEX,
            0,
            PIECE_SIZE_BLOCKS,
            PIECE_SIZE_BLOCKS - 1,
        );
        verifier
            .update_piece(piece_state)
            .await
            .expect("update piece");

        let status = status_rx.recv().expect("receive status");
        assert_eq!(
            status,
            PieceStatus::ValidPiece {
                file_index: 0,
                piece_index: VALID_PIECE_INDEX,
                // Important: index_complete is false because of the prior corrupt piece
                index_complete: false,
            }
        );
    }

    #[tokio::test]
    async fn verify_non_aligned_file() {
        const NUM_PIECES: usize = 2;

        // Create a test file that's not aligned to piece size
        let non_aligned_size = PIECE_SIZE_BYTES + (PIECE_SIZE_BYTES / 2); // 1.5 pieces
        let tf = temp_file(0xa5u8, non_aligned_size);
        let test_path = std::env::temp_dir().join(tf.path());

        // Create index from the file
        let indexer = Indexer::from_file(test_path.as_path())
            .await
            .expect("indexer");

        // Set up verifier
        let verifier =
            PieceVerifier::new(Arc::new(RwLock::new(indexer.index().await.expect("index")))).await;
        let (notifier, status_rx) = PieceStatusNotifier::new();
        verifier.subscribe(Box::new(notifier)).await;

        let piece_index = 0;
        for block_index in 0..PIECE_SIZE_BLOCKS - 1 {
            let piece_state = PieceState::new(0, piece_index, 0, PIECE_SIZE_BLOCKS, block_index);
            verifier
                .update_piece(piece_state)
                .await
                .expect("update piece");

            let status = status_rx.recv().expect("receive status");
            assert_eq!(
                status,
                PieceStatus::IncompletePiece {
                    file_index: 0,
                    piece_index: piece_index,
                }
            );
        }

        // Verify first complete piece
        let piece_state =
            PieceState::new(0, piece_index, 0, PIECE_SIZE_BLOCKS, PIECE_SIZE_BLOCKS - 1);
        verifier
            .update_piece(piece_state)
            .await
            .expect("update piece");

        let status = status_rx.recv().expect("receive status");
        assert_eq!(
            status,
            PieceStatus::ValidPiece {
                file_index: 0,
                piece_index,
                index_complete: piece_index == NUM_PIECES - 1
            }
        );

        let piece_index = 1;
        for block_index in 0..14 {
            let piece_state = PieceState::new(0, piece_index, 0, 16, block_index);
            verifier
                .update_piece(piece_state)
                .await
                .expect("update piece");

            let status = status_rx.recv().expect("receive status");
            assert_eq!(
                status,
                PieceStatus::IncompletePiece {
                    file_index: 0,
                    piece_index: piece_index,
                },
                "{piece_index} {block_index}"
            );
        }

        // Verify second complete piece
        let piece_state = PieceState::new(0, piece_index, 0, 16, 15);
        verifier
            .update_piece(piece_state)
            .await
            .expect("update piece");

        let status = status_rx.recv().expect("receive status");
        assert_eq!(
            status,
            PieceStatus::ValidPiece {
                file_index: 0,
                piece_index,
                index_complete: piece_index == NUM_PIECES - 1
            }
        );
    }
}