ipfrs-network 0.2.0

Peer-to-peer networking layer with libp2p and QUIC for IPFRS
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
//! Streaming block transfer management with chunked, resumable transfers.
//!
//! Provides [`StreamingBlockTransfer`] for managing chunked, resumable block
//! transfers between peers with progress tracking and checksum verification.

use std::collections::HashMap;

/// Direction of a block transfer.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum TransferDirection {
    /// Uploading data to a remote peer.
    Upload,
    /// Downloading data from a remote peer.
    Download,
}

/// A single chunk of data in a block transfer.
#[derive(Clone, Debug)]
pub struct TransferChunk {
    /// Zero-based index of this chunk in the overall transfer.
    pub chunk_index: u32,
    /// Raw bytes of this chunk.
    pub data: Vec<u8>,
    /// Simple checksum: XOR of all bytes cast to u32.
    pub checksum: u32,
}

impl TransferChunk {
    /// Compute the expected checksum for the contained data.
    pub fn compute_checksum(data: &[u8]) -> u32 {
        data.iter().fold(0u32, |acc, &b| acc ^ u32::from(b))
    }

    /// Return `true` if the stored checksum matches the data.
    pub fn is_valid(&self) -> bool {
        self.checksum == Self::compute_checksum(&self.data)
    }
}

/// Lifecycle state of a [`BlockTransfer`].
#[derive(Clone, Debug, PartialEq)]
pub enum TransferState {
    /// Transfer has been created but not yet started.
    Pending,
    /// Transfer is actively exchanging chunks.
    InProgress {
        /// Number of chunks successfully processed so far.
        chunks_done: u32,
        /// Total number of chunks in the transfer.
        total_chunks: u32,
    },
    /// Transfer has been paused mid-way.
    Paused {
        /// Number of chunks processed before pausing.
        chunks_done: u32,
    },
    /// All chunks have been received/sent successfully.
    Completed,
    /// Transfer encountered an unrecoverable error.
    Failed {
        /// Human-readable description of the failure.
        reason: String,
    },
}

/// A single block transfer between the local node and a remote peer.
#[derive(Clone, Debug)]
pub struct BlockTransfer {
    /// Unique identifier for this transfer.
    pub transfer_id: u64,
    /// Content identifier of the block being transferred.
    pub cid: String,
    /// Identifier of the remote peer.
    pub peer_id: String,
    /// Whether this node is uploading or downloading.
    pub direction: TransferDirection,
    /// Total size of the block in bytes.
    pub total_bytes: u64,
    /// Maximum bytes per chunk (default 65536).
    pub chunk_size: u64,
    /// Current lifecycle state of the transfer.
    pub state: TransferState,
    /// Indices of chunks that have been received or sent.
    pub received_chunks: Vec<u32>,
    /// Tick at which the transfer was started.
    pub started_at_tick: u64,
}

impl BlockTransfer {
    /// Return the total number of chunks required for this transfer.
    ///
    /// Uses ceiling division so that a partial last chunk is counted.
    pub fn total_chunks(&self) -> u32 {
        if self.chunk_size == 0 {
            return 0;
        }
        self.total_bytes.div_ceil(self.chunk_size) as u32
    }

    /// Return transfer progress as a fraction in `[0.0, 1.0]`.
    ///
    /// Returns `0.0` when the transfer is [`TransferState::Pending`].
    pub fn progress(&self) -> f64 {
        let total = self.total_chunks();
        if total == 0 {
            return 1.0;
        }
        let done = match &self.state {
            TransferState::Pending => 0,
            TransferState::InProgress { chunks_done, .. } => *chunks_done,
            TransferState::Paused { chunks_done } => *chunks_done,
            TransferState::Completed => total,
            TransferState::Failed { .. } => self.received_chunks.len() as u32,
        };
        f64::from(done) / f64::from(total)
    }

    /// Return `true` if the transfer has reached [`TransferState::Completed`].
    pub fn is_complete(&self) -> bool {
        self.state == TransferState::Completed
    }
}

/// Aggregated statistics for all transfers managed by a [`StreamingBlockTransfer`].
#[derive(Clone, Debug, Default)]
pub struct TransferManagerStats {
    /// Number of transfers that are currently active (Pending, InProgress, or Paused).
    pub active_transfers: usize,
    /// Total number of transfers that reached [`TransferState::Completed`].
    pub completed_transfers: u64,
    /// Total number of transfers that reached [`TransferState::Failed`].
    pub failed_transfers: u64,
    /// Cumulative bytes transferred across all completed transfers.
    pub total_bytes_transferred: u64,
}

/// Manager for chunked, resumable block transfers between peers.
///
/// Each transfer is identified by a unique `u64` transfer ID returned by
/// [`StreamingBlockTransfer::start_transfer`].
pub struct StreamingBlockTransfer {
    /// All tracked transfers indexed by their transfer ID.
    pub transfers: HashMap<u64, BlockTransfer>,
    /// Counter used to assign the next unique transfer ID.
    pub next_id: u64,
    /// Monotonically increasing logical clock.
    pub current_tick: u64,
}

impl StreamingBlockTransfer {
    /// Create a new, empty transfer manager.
    pub fn new() -> Self {
        Self {
            transfers: HashMap::new(),
            next_id: 1,
            current_tick: 0,
        }
    }

    /// Register a new transfer and return its assigned transfer ID.
    ///
    /// The transfer begins in the [`TransferState::Pending`] state.
    /// If `chunk_size` is `0`, it defaults to `65536`.
    pub fn start_transfer(
        &mut self,
        cid: String,
        peer_id: String,
        direction: TransferDirection,
        total_bytes: u64,
        chunk_size: u64,
    ) -> u64 {
        let id = self.next_id;
        self.next_id += 1;

        let effective_chunk_size = if chunk_size == 0 { 65536 } else { chunk_size };

        let transfer = BlockTransfer {
            transfer_id: id,
            cid,
            peer_id,
            direction,
            total_bytes,
            chunk_size: effective_chunk_size,
            state: TransferState::Pending,
            received_chunks: Vec::new(),
            started_at_tick: self.current_tick,
        };

        self.transfers.insert(id, transfer);
        id
    }

    /// Process an incoming chunk for the given transfer.
    ///
    /// # Errors
    ///
    /// Returns an error string when:
    /// - The transfer ID is unknown.
    /// - The transfer is not in an `InProgress` or `Pending` state.
    /// - The chunk checksum does not match its data.
    /// - The chunk index has already been received.
    pub fn receive_chunk(&mut self, transfer_id: u64, chunk: TransferChunk) -> Result<(), String> {
        let transfer = self
            .transfers
            .get_mut(&transfer_id)
            .ok_or_else(|| format!("unknown transfer id: {transfer_id}"))?;

        // Validate state: only Pending and InProgress accept new chunks.
        match &transfer.state {
            TransferState::Paused { .. } => {
                return Err(format!("transfer {transfer_id} is paused"));
            }
            TransferState::Completed => {
                return Err(format!("transfer {transfer_id} is already completed"));
            }
            TransferState::Failed { reason } => {
                return Err(format!("transfer {transfer_id} has failed: {reason}"));
            }
            TransferState::Pending | TransferState::InProgress { .. } => {}
        }

        // Validate checksum.
        if !chunk.is_valid() {
            return Err(format!(
                "checksum mismatch for chunk {} of transfer {transfer_id}: expected {}, got {}",
                chunk.chunk_index,
                TransferChunk::compute_checksum(&chunk.data),
                chunk.checksum,
            ));
        }

        // Reject duplicates.
        if transfer.received_chunks.contains(&chunk.chunk_index) {
            return Err(format!(
                "chunk {} already received for transfer {transfer_id}",
                chunk.chunk_index
            ));
        }

        transfer.received_chunks.push(chunk.chunk_index);
        let chunks_done = transfer.received_chunks.len() as u32;
        let total = transfer.total_chunks();

        if chunks_done >= total {
            transfer.state = TransferState::Completed;
        } else {
            transfer.state = TransferState::InProgress {
                chunks_done,
                total_chunks: total,
            };
        }

        Ok(())
    }

    /// Pause an active or pending transfer.
    ///
    /// Returns `true` if the transfer was successfully paused, `false` otherwise
    /// (e.g., the transfer ID is unknown, or the transfer is already paused/done).
    pub fn pause(&mut self, transfer_id: u64) -> bool {
        let Some(transfer) = self.transfers.get_mut(&transfer_id) else {
            return false;
        };

        let chunks_done = match &transfer.state {
            TransferState::Pending => 0,
            TransferState::InProgress { chunks_done, .. } => *chunks_done,
            _ => return false,
        };

        transfer.state = TransferState::Paused { chunks_done };
        true
    }

    /// Resume a paused transfer.
    ///
    /// Returns `true` if the transfer was successfully resumed, `false` otherwise.
    pub fn resume(&mut self, transfer_id: u64) -> bool {
        let Some(transfer) = self.transfers.get_mut(&transfer_id) else {
            return false;
        };

        let chunks_done = match &transfer.state {
            TransferState::Paused { chunks_done } => *chunks_done,
            _ => return false,
        };

        let total_chunks = transfer.total_chunks();
        transfer.state = TransferState::InProgress {
            chunks_done,
            total_chunks,
        };
        true
    }

    /// Mark a transfer as failed with a descriptive reason.
    ///
    /// Returns `true` if the transfer was found and transitioned to
    /// [`TransferState::Failed`], `false` if the ID is unknown or the
    /// transfer has already completed/failed.
    pub fn fail(&mut self, transfer_id: u64, reason: String) -> bool {
        let Some(transfer) = self.transfers.get_mut(&transfer_id) else {
            return false;
        };

        match transfer.state {
            TransferState::Completed | TransferState::Failed { .. } => return false,
            _ => {}
        }

        transfer.state = TransferState::Failed { reason };
        true
    }

    /// Compute aggregated statistics across all managed transfers.
    pub fn stats(&self) -> TransferManagerStats {
        let mut stats = TransferManagerStats::default();

        for transfer in self.transfers.values() {
            match &transfer.state {
                TransferState::Pending
                | TransferState::InProgress { .. }
                | TransferState::Paused { .. } => {
                    stats.active_transfers += 1;
                }
                TransferState::Completed => {
                    stats.completed_transfers += 1;
                    stats.total_bytes_transferred += transfer.total_bytes;
                }
                TransferState::Failed { .. } => {
                    stats.failed_transfers += 1;
                }
            }
        }

        stats
    }

    /// Advance the logical clock by one tick.
    pub fn advance_tick(&mut self) {
        self.current_tick += 1;
    }
}

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

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn make_chunk(index: u32, data: Vec<u8>) -> TransferChunk {
        let checksum = TransferChunk::compute_checksum(&data);
        TransferChunk {
            chunk_index: index,
            data,
            checksum,
        }
    }

    // 1. start_transfer returns unique, incrementing IDs.
    #[test]
    fn test_start_transfer_unique_ids() {
        let mut mgr = StreamingBlockTransfer::new();
        let id1 = mgr.start_transfer(
            "cid1".into(),
            "peer1".into(),
            TransferDirection::Download,
            1024,
            512,
        );
        let id2 = mgr.start_transfer(
            "cid2".into(),
            "peer2".into(),
            TransferDirection::Upload,
            2048,
            512,
        );
        assert_ne!(id1, id2);
        assert!(id2 > id1);
    }

    // 2. New transfer starts in Pending state.
    #[test]
    fn test_start_transfer_pending_state() {
        let mut mgr = StreamingBlockTransfer::new();
        let id = mgr.start_transfer(
            "cid".into(),
            "peer".into(),
            TransferDirection::Download,
            100,
            64,
        );
        let t = &mgr.transfers[&id];
        assert_eq!(t.state, TransferState::Pending);
    }

    // 3. total_chunks calculation — exact multiple.
    #[test]
    fn test_total_chunks_exact_multiple() {
        let mut mgr = StreamingBlockTransfer::new();
        let id = mgr.start_transfer(
            "c".into(),
            "p".into(),
            TransferDirection::Download,
            65536,
            65536,
        );
        assert_eq!(mgr.transfers[&id].total_chunks(), 1);
    }

    // 4. total_chunks calculation — partial last chunk.
    #[test]
    fn test_total_chunks_partial() {
        let mut mgr = StreamingBlockTransfer::new();
        let id = mgr.start_transfer(
            "c".into(),
            "p".into(),
            TransferDirection::Download,
            65537,
            65536,
        );
        assert_eq!(mgr.transfers[&id].total_chunks(), 2);
    }

    // 5. total_chunks for zero bytes.
    #[test]
    fn test_total_chunks_zero_bytes() {
        let mut mgr = StreamingBlockTransfer::new();
        let id = mgr.start_transfer(
            "c".into(),
            "p".into(),
            TransferDirection::Download,
            0,
            65536,
        );
        assert_eq!(mgr.transfers[&id].total_chunks(), 0);
    }

    // 6. receive_chunk updates state to InProgress.
    #[test]
    fn test_receive_chunk_updates_state() {
        let mut mgr = StreamingBlockTransfer::new();
        // 2 chunks of 10 bytes each = 20 bytes total.
        let id = mgr.start_transfer("c".into(), "p".into(), TransferDirection::Download, 20, 10);
        let chunk = make_chunk(0, vec![1u8; 10]);
        mgr.receive_chunk(id, chunk)
            .expect("receive_chunk should succeed");
        match &mgr.transfers[&id].state {
            TransferState::InProgress {
                chunks_done,
                total_chunks,
            } => {
                assert_eq!(*chunks_done, 1);
                assert_eq!(*total_chunks, 2);
            }
            other => panic!("expected InProgress, got {other:?}"),
        }
    }

    // 7. receive_chunk with bad checksum returns Err.
    #[test]
    fn test_receive_chunk_checksum_mismatch() {
        let mut mgr = StreamingBlockTransfer::new();
        let id = mgr.start_transfer("c".into(), "p".into(), TransferDirection::Download, 10, 10);
        let chunk = TransferChunk {
            chunk_index: 0,
            data: vec![42u8; 10],
            checksum: 0xDEAD_BEEF, // deliberately wrong
        };
        let result = mgr.receive_chunk(id, chunk);
        assert!(result.is_err(), "expected error for checksum mismatch");
        assert!(result.unwrap_err().contains("checksum mismatch"));
    }

    // 8. All chunks received → Completed.
    #[test]
    fn test_all_chunks_received_completes_transfer() {
        let mut mgr = StreamingBlockTransfer::new();
        let id = mgr.start_transfer("c".into(), "p".into(), TransferDirection::Download, 30, 10);
        for i in 0..3u32 {
            let chunk = make_chunk(i, vec![i as u8; 10]);
            mgr.receive_chunk(id, chunk)
                .expect("chunk should be accepted");
        }
        assert_eq!(mgr.transfers[&id].state, TransferState::Completed);
        assert!(mgr.transfers[&id].is_complete());
    }

    // 9. Duplicate chunk rejected.
    #[test]
    fn test_duplicate_chunk_rejected() {
        let mut mgr = StreamingBlockTransfer::new();
        let id = mgr.start_transfer("c".into(), "p".into(), TransferDirection::Download, 20, 10);
        let chunk0 = make_chunk(0, vec![1u8; 10]);
        mgr.receive_chunk(id, chunk0.clone())
            .expect("first receive should succeed");
        let dup = make_chunk(0, vec![1u8; 10]);
        let result = mgr.receive_chunk(id, dup);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("already received"));
    }

    // 10. pause transitions InProgress → Paused.
    #[test]
    fn test_pause_in_progress() {
        let mut mgr = StreamingBlockTransfer::new();
        let id = mgr.start_transfer("c".into(), "p".into(), TransferDirection::Download, 30, 10);
        let chunk = make_chunk(0, vec![0u8; 10]);
        mgr.receive_chunk(id, chunk)
            .expect("test: receive_chunk should accept valid chunk");
        assert!(mgr.pause(id));
        match &mgr.transfers[&id].state {
            TransferState::Paused { chunks_done } => assert_eq!(*chunks_done, 1),
            other => panic!("expected Paused, got {other:?}"),
        }
    }

    // 11. pause on Pending → Paused(0).
    #[test]
    fn test_pause_pending() {
        let mut mgr = StreamingBlockTransfer::new();
        let id = mgr.start_transfer("c".into(), "p".into(), TransferDirection::Download, 100, 10);
        assert!(mgr.pause(id));
        assert_eq!(
            mgr.transfers[&id].state,
            TransferState::Paused { chunks_done: 0 }
        );
    }

    // 12. resume transitions Paused → InProgress.
    #[test]
    fn test_resume_paused() {
        let mut mgr = StreamingBlockTransfer::new();
        let id = mgr.start_transfer("c".into(), "p".into(), TransferDirection::Download, 30, 10);
        let chunk = make_chunk(0, vec![7u8; 10]);
        mgr.receive_chunk(id, chunk)
            .expect("test: receive_chunk should accept valid chunk");
        mgr.pause(id);
        assert!(mgr.resume(id));
        match &mgr.transfers[&id].state {
            TransferState::InProgress {
                chunks_done,
                total_chunks,
            } => {
                assert_eq!(*chunks_done, 1);
                assert_eq!(*total_chunks, 3);
            }
            other => panic!("expected InProgress, got {other:?}"),
        }
    }

    // 13. resume returns false for non-paused transfer.
    #[test]
    fn test_resume_non_paused_returns_false() {
        let mut mgr = StreamingBlockTransfer::new();
        let id = mgr.start_transfer("c".into(), "p".into(), TransferDirection::Download, 10, 10);
        assert!(!mgr.resume(id)); // still Pending
    }

    // 14. fail sets Failed state.
    #[test]
    fn test_fail_sets_failed_state() {
        let mut mgr = StreamingBlockTransfer::new();
        let id = mgr.start_transfer("c".into(), "p".into(), TransferDirection::Download, 100, 10);
        assert!(mgr.fail(id, "network timeout".into()));
        match &mgr.transfers[&id].state {
            TransferState::Failed { reason } => assert_eq!(reason, "network timeout"),
            other => panic!("expected Failed, got {other:?}"),
        }
    }

    // 15. fail on completed transfer returns false.
    #[test]
    fn test_fail_on_completed_returns_false() {
        let mut mgr = StreamingBlockTransfer::new();
        let id = mgr.start_transfer("c".into(), "p".into(), TransferDirection::Download, 10, 10);
        let chunk = make_chunk(0, vec![0u8; 10]);
        mgr.receive_chunk(id, chunk)
            .expect("test: receive_chunk should accept valid chunk");
        assert!(mgr.transfers[&id].is_complete());
        assert!(!mgr.fail(id, "too late".into()));
    }

    // 16. progress before and after receiving chunks.
    #[test]
    fn test_progress_values() {
        let mut mgr = StreamingBlockTransfer::new();
        // 4 chunks of 10 bytes.
        let id = mgr.start_transfer("c".into(), "p".into(), TransferDirection::Download, 40, 10);
        assert_eq!(mgr.transfers[&id].progress(), 0.0);

        let chunk0 = make_chunk(0, vec![1u8; 10]);
        mgr.receive_chunk(id, chunk0)
            .expect("test: receive_chunk should accept chunk 0");
        let p = mgr.transfers[&id].progress();
        assert!((p - 0.25).abs() < f64::EPSILON, "expected 0.25 got {p}");

        let chunk1 = make_chunk(1, vec![2u8; 10]);
        mgr.receive_chunk(id, chunk1)
            .expect("test: receive_chunk should accept chunk 1");
        let p = mgr.transfers[&id].progress();
        assert!((p - 0.5).abs() < f64::EPSILON, "expected 0.5 got {p}");
    }

    // 17. stats counts active, completed, and failed correctly.
    #[test]
    fn test_stats_counts() {
        let mut mgr = StreamingBlockTransfer::new();

        // Two active (Pending).
        mgr.start_transfer(
            "c1".into(),
            "p1".into(),
            TransferDirection::Download,
            10,
            10,
        );
        mgr.start_transfer("c2".into(), "p2".into(), TransferDirection::Upload, 10, 10);

        // One completed.
        let id_done = mgr.start_transfer(
            "c3".into(),
            "p3".into(),
            TransferDirection::Download,
            10,
            10,
        );
        let chunk = make_chunk(0, vec![5u8; 10]);
        mgr.receive_chunk(id_done, chunk)
            .expect("test: receive_chunk should accept valid chunk for completed transfer");

        // One failed.
        let id_fail = mgr.start_transfer(
            "c4".into(),
            "p4".into(),
            TransferDirection::Download,
            10,
            10,
        );
        mgr.fail(id_fail, "oops".into());

        let stats = mgr.stats();
        assert_eq!(stats.active_transfers, 2);
        assert_eq!(stats.completed_transfers, 1);
        assert_eq!(stats.failed_transfers, 1);
        assert_eq!(stats.total_bytes_transferred, 10);
    }

    // 18. advance_tick increments current_tick.
    #[test]
    fn test_advance_tick() {
        let mut mgr = StreamingBlockTransfer::new();
        assert_eq!(mgr.current_tick, 0);
        mgr.advance_tick();
        mgr.advance_tick();
        assert_eq!(mgr.current_tick, 2);
    }

    // 19. started_at_tick is recorded from current_tick.
    #[test]
    fn test_started_at_tick_recorded() {
        let mut mgr = StreamingBlockTransfer::new();
        mgr.advance_tick();
        mgr.advance_tick();
        let id = mgr.start_transfer("c".into(), "p".into(), TransferDirection::Upload, 10, 10);
        assert_eq!(mgr.transfers[&id].started_at_tick, 2);
    }

    // 20. receive_chunk rejected for paused transfer.
    #[test]
    fn test_receive_chunk_rejected_when_paused() {
        let mut mgr = StreamingBlockTransfer::new();
        let id = mgr.start_transfer("c".into(), "p".into(), TransferDirection::Download, 20, 10);
        mgr.pause(id);
        let chunk = make_chunk(0, vec![3u8; 10]);
        let result = mgr.receive_chunk(id, chunk);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("paused"));
    }

    // 21. is_complete returns false for InProgress.
    #[test]
    fn test_is_complete_false_for_in_progress() {
        let mut mgr = StreamingBlockTransfer::new();
        let id = mgr.start_transfer("c".into(), "p".into(), TransferDirection::Download, 20, 10);
        let chunk = make_chunk(0, vec![9u8; 10]);
        mgr.receive_chunk(id, chunk)
            .expect("test: receive_chunk should accept valid chunk");
        assert!(!mgr.transfers[&id].is_complete());
    }

    // 22. Unknown transfer ID returns error from receive_chunk.
    #[test]
    fn test_receive_chunk_unknown_id() {
        let mut mgr = StreamingBlockTransfer::new();
        let chunk = make_chunk(0, vec![1u8]);
        let result = mgr.receive_chunk(9999, chunk);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("unknown transfer id"));
    }
}