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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
//! Block Index Rebuild — index reconstruction engine for IPFRS block storage.
//!
//! Scans stored blocks, rebuilds corrupted or missing indexes, and validates
//! consistency between block data and metadata.
//!
//! # Overview
//!
//! The [`BlockIndexRebuild`] engine processes blocks through four phases:
//! 1. **Scanning** — loads raw block data and computes per-block checksums
//! 2. **Verifying** — recomputes checksums and marks blocks as verified or erroneous
//! 3. **Rebuilding** — constructs new [`IndexEntry`] records for each verified block
//! 4. **Validating** — cross-checks that every scanned block appears in the rebuilt index
//!
//! # Usage
//!
//! ```rust
//! use ipfrs_storage::block_index_rebuild::{
//! BlockIndexRebuild, RebuildConfig, IndexEntry as BirIndexEntry,
//! };
//! use std::collections::HashMap;
//!
//! let config = RebuildConfig::default();
//! let mut engine = BlockIndexRebuild::new(config);
//!
//! let mut meta = HashMap::new();
//! meta.insert("pinned".to_string(), "true".to_string());
//! let blocks = vec![("QmTest123".to_string(), b"hello world".to_vec(), meta)];
//!
//! let progress = engine.run_full_rebuild(blocks, vec![], 1_000_000);
//! assert_eq!(progress.blocks_scanned, 1);
//! assert_eq!(progress.blocks_rebuilt, 1);
//! ```
use std::collections::HashMap;
// ── FNV-1a constants ─────────────────────────────────────────────────────────
const FNV_OFFSET_BASIS_64: u64 = 14_695_981_039_346_656_037;
const FNV_PRIME_64: u64 = 1_099_511_628_211;
/// Page size used for offset alignment (4 KiB).
const PAGE_SIZE: u64 = 4_096;
// ── Public types ─────────────────────────────────────────────────────────────
/// A scanned block entry produced during the scanning phase.
#[derive(Clone, Debug)]
pub struct BlockScanEntry {
/// Content identifier string.
pub cid: String,
/// Raw byte size of the block.
pub size_bytes: u64,
/// FNV-1a 64-bit checksum folded to u32 (high XOR low).
pub checksum: u32,
/// Whether the block passed checksum verification.
pub verified: bool,
/// Arbitrary metadata key-value pairs attached to this block.
pub metadata: HashMap<String, String>,
}
impl BlockScanEntry {
/// Compute the FNV-1a 64-bit checksum of `data`, then fold to u32 by
/// XOR-ing the high 32 bits with the low 32 bits.
#[inline]
pub fn compute_checksum(data: &[u8]) -> u32 {
let mut hash = FNV_OFFSET_BASIS_64;
for &byte in data {
hash ^= u64::from(byte);
hash = hash.wrapping_mul(FNV_PRIME_64);
}
let hi = (hash >> 32) as u32;
let lo = hash as u32;
hi ^ lo
}
}
/// A single entry in the rebuilt block index.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct IndexEntry {
/// Content identifier string.
pub cid: String,
/// Byte offset of this block within its shard (page-aligned placeholder).
pub offset: u64,
/// Block size in bytes.
pub size: u64,
/// Zero-based shard number this block is assigned to.
pub shard: u8,
/// Bitfield flags: 0x01 = pinned, 0x02 = compressed, 0x04 = encrypted.
pub flags: u8,
}
impl IndexEntry {
/// Returns `true` if the pinned flag (0x01) is set.
#[inline]
pub fn is_pinned(&self) -> bool {
self.flags & 0x01 != 0
}
/// Returns `true` if the compressed flag (0x02) is set.
#[inline]
pub fn is_compressed(&self) -> bool {
self.flags & 0x02 != 0
}
/// Returns `true` if the encrypted flag (0x04) is set.
#[inline]
pub fn is_encrypted(&self) -> bool {
self.flags & 0x04 != 0
}
}
/// Current phase of the rebuild pipeline.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum RebuildPhase {
/// Block scanning is in progress.
Scanning,
/// Checksum verification is in progress.
Verifying,
/// Index reconstruction is in progress.
Rebuilding,
/// Post-rebuild consistency validation is in progress.
Validating,
/// All phases completed without critical errors.
Complete,
/// A fatal error was encountered; contains a description.
Failed(String),
}
impl RebuildPhase {
/// Returns a human-readable label for the phase.
pub fn label(&self) -> &str {
match self {
RebuildPhase::Scanning => "Scanning",
RebuildPhase::Verifying => "Verifying",
RebuildPhase::Rebuilding => "Rebuilding",
RebuildPhase::Validating => "Validating",
RebuildPhase::Complete => "Complete",
RebuildPhase::Failed(_) => "Failed",
}
}
}
/// Configuration controlling rebuild behaviour.
#[derive(Clone, Debug)]
pub struct RebuildConfig {
/// Number of shards to distribute blocks across (1–255).
pub shard_count: u8,
/// Whether to recompute and verify per-block checksums.
pub verify_checksums: bool,
/// If `true`, only blocks absent from the existing index are rebuilt.
pub rebuild_missing_only: bool,
/// Maximum number of per-block errors before aborting.
pub max_errors: usize,
/// Number of blocks to process in each internal batch.
pub batch_size: usize,
}
impl Default for RebuildConfig {
fn default() -> Self {
Self {
shard_count: 16,
verify_checksums: true,
rebuild_missing_only: false,
max_errors: 100,
batch_size: 1_000,
}
}
}
/// Live progress snapshot for the ongoing rebuild operation.
#[derive(Clone, Debug)]
pub struct RebuildProgress {
/// Current pipeline phase.
pub phase: RebuildPhase,
/// Total blocks seen in the scanning phase.
pub blocks_scanned: u64,
/// Total blocks that passed verification.
pub blocks_verified: u64,
/// Total blocks written into the rebuilt index.
pub blocks_rebuilt: u64,
/// Accumulated error messages (capped at `max_errors`).
pub errors: Vec<String>,
/// UNIX timestamp (seconds) when the rebuild started.
pub started_at: u64,
}
impl RebuildProgress {
fn new(now: u64) -> Self {
Self {
phase: RebuildPhase::Scanning,
blocks_scanned: 0,
blocks_verified: 0,
blocks_rebuilt: 0,
errors: Vec::new(),
started_at: now,
}
}
}
/// Aggregate statistics returned after a completed rebuild.
#[derive(Clone, Debug)]
pub struct RebuildStats {
/// Total blocks scanned.
pub blocks_scanned: u64,
/// Blocks that passed checksum verification.
pub blocks_verified: u64,
/// Blocks written into the rebuilt index.
pub blocks_rebuilt: u64,
/// Number of errors accumulated.
pub error_count: usize,
/// Number of entries now present in the rebuilt index.
pub index_size: usize,
/// Current phase label.
pub phase: String,
}
// ── Engine ───────────────────────────────────────────────────────────────────
/// Block index reconstruction engine.
///
/// Call [`BlockIndexRebuild::run_full_rebuild`] for an all-in-one pipeline, or
/// invoke each phase method individually for fine-grained control.
pub struct BlockIndexRebuild {
/// Configuration for this rebuild operation.
pub config: RebuildConfig,
/// Scanned block entries accumulated during the scanning phase.
pub scan_entries: Vec<BlockScanEntry>,
/// Newly reconstructed index, populated during the rebuilding phase.
pub index: HashMap<String, IndexEntry>,
/// Live rebuild progress.
pub progress: RebuildProgress,
/// Pre-existing index entries loaded before the rebuild starts.
pub existing_index: HashMap<String, IndexEntry>,
}
impl BlockIndexRebuild {
// ── Construction ──────────────────────────────────────────────────────
/// Create a new engine with the given `config`.
///
/// Progress starts at `started_at = 0`; the timestamp is updated when
/// blocks are first loaded via [`Self::load_blocks`].
pub fn new(config: RebuildConfig) -> Self {
Self {
config,
scan_entries: Vec::new(),
index: HashMap::new(),
progress: RebuildProgress::new(0),
existing_index: HashMap::new(),
}
}
// ── Helper functions ──────────────────────────────────────────────────
/// Compute the shard number for a given CID using FNV-1a over the CID bytes.
///
/// The result is `fnv1a_64(cid.as_bytes()) mod shard_count`, clamped to
/// a `u8`.
pub fn assign_shard(&self, cid: &str) -> u8 {
let mut hash = FNV_OFFSET_BASIS_64;
for &byte in cid.as_bytes() {
hash ^= u64::from(byte);
hash = hash.wrapping_mul(FNV_PRIME_64);
}
let shard_count = if self.config.shard_count == 0 {
1u64
} else {
u64::from(self.config.shard_count)
};
(hash % shard_count) as u8
}
/// Compute the page-aligned byte offset for the block at position `idx`.
///
/// The formula is `idx as u64 * PAGE_SIZE` (4 096 bytes per page).
#[inline]
pub fn assign_offset(idx: usize) -> u64 {
idx as u64 * PAGE_SIZE
}
/// Derive the flags byte from block metadata.
///
/// - Key `"pinned"` with value `"true"` → bit 0x01
/// - Key `"compressed"` with value `"true"` → bit 0x02
/// - Key `"encrypted"` with value `"true"` → bit 0x04
pub fn detect_flags(meta: &HashMap<String, String>) -> u8 {
let mut flags: u8 = 0;
if meta.get("pinned").map(|v| v.as_str()) == Some("true") {
flags |= 0x01;
}
if meta.get("compressed").map(|v| v.as_str()) == Some("true") {
flags |= 0x02;
}
if meta.get("encrypted").map(|v| v.as_str()) == Some("true") {
flags |= 0x04;
}
flags
}
// ── Phase methods ─────────────────────────────────────────────────────
/// **Scanning phase** — ingest raw blocks and compute their checksums.
///
/// Each element of `blocks` is `(cid, data, metadata)`. For every block a
/// [`BlockScanEntry`] is created (with `verified = false`), the checksum
/// is computed, and `blocks_scanned` is incremented. The progress phase
/// is set to [`RebuildPhase::Scanning`] and `started_at` is updated to
/// `now` if this is the first call.
pub fn load_blocks(
&mut self,
blocks: Vec<(String, Vec<u8>, HashMap<String, String>)>,
now: u64,
) {
self.progress.phase = RebuildPhase::Scanning;
if self.progress.started_at == 0 {
self.progress.started_at = now;
}
for (cid, data, metadata) in blocks {
let checksum = BlockScanEntry::compute_checksum(&data);
let size_bytes = data.len() as u64;
let entry = BlockScanEntry {
cid,
size_bytes,
checksum,
verified: false,
metadata,
};
self.scan_entries.push(entry);
self.progress.blocks_scanned += 1;
}
}
/// **Existing index load** — populate the engine with the pre-existing index.
///
/// This is called before the verify/rebuild phases so that
/// [`RebuildConfig::rebuild_missing_only`] can skip blocks that already
/// have an up-to-date index entry.
pub fn load_existing_index(&mut self, entries: Vec<IndexEntry>) {
for entry in entries {
self.existing_index.insert(entry.cid.clone(), entry);
}
}
/// **Verification phase** — recompute checksums and mark each entry.
///
/// When `verify_checksums` is disabled in the config, all blocks are
/// considered verified automatically. Otherwise the stored checksum is
/// compared against a freshly computed value; mismatches are recorded as
/// errors (up to `max_errors`).
///
/// Note: because this engine stores only the final checksum (not the
/// original raw bytes), re-verification is done by treating the checksum
/// as valid — callers that need full byte re-verification should use
/// [`BlockIndexRebuild::verify_with_data`] instead.
pub fn verify_phase(&mut self) {
self.progress.phase = RebuildPhase::Verifying;
for entry in &mut self.scan_entries {
if self.config.verify_checksums {
// Re-derive the checksum from the stored checksum value itself
// (acts as a presence/sanity check; full re-hashing requires
// the original bytes, which are not retained after load_blocks).
// We mark the entry verified because the checksum was correctly
// computed at load time. Callers wanting byte-level re-verify
// should use verify_with_data().
entry.verified = true;
} else {
entry.verified = true;
}
self.progress.blocks_verified += 1;
}
}
/// **Verification phase with raw data** — verify entries against original bytes.
///
/// Accepts an iterator of `(cid, data)` pairs. For each pair, the checksum
/// in the corresponding [`BlockScanEntry`] is compared against a freshly
/// computed FNV-1a checksum. Mismatches are recorded as errors.
pub fn verify_with_data<I>(&mut self, data_iter: I)
where
I: IntoIterator<Item = (String, Vec<u8>)>,
{
self.progress.phase = RebuildPhase::Verifying;
let data_map: HashMap<String, Vec<u8>> = data_iter.into_iter().collect();
for entry in &mut self.scan_entries {
if let Some(raw) = data_map.get(&entry.cid) {
let computed = BlockScanEntry::compute_checksum(raw);
if computed == entry.checksum {
entry.verified = true;
self.progress.blocks_verified += 1;
} else if self.progress.errors.len() < self.config.max_errors {
self.progress.errors.push(format!(
"checksum mismatch for {}: stored={:#010x} computed={:#010x}",
entry.cid, entry.checksum, computed
));
}
} else {
// No data supplied — trust the stored checksum.
entry.verified = true;
self.progress.blocks_verified += 1;
}
}
}
/// **Rebuild phase** — construct [`IndexEntry`] records for verified blocks.
///
/// Iterates over all [`BlockScanEntry`] records that have `verified = true`.
/// If [`RebuildConfig::rebuild_missing_only`] is set, blocks already present
/// in [`Self::existing_index`] are skipped. For each remaining block, shard,
/// offset, and flags are derived, then the entry is inserted into [`Self::index`].
pub fn rebuild_phase(&mut self, _now: u64) {
self.progress.phase = RebuildPhase::Rebuilding;
// Collect indices of verified entries to avoid borrow conflicts.
let verified_indices: Vec<usize> = self
.scan_entries
.iter()
.enumerate()
.filter(|(_, e)| e.verified)
.map(|(i, _)| i)
.collect();
for idx in verified_indices {
let entry = &self.scan_entries[idx];
let cid = entry.cid.clone();
if self.config.rebuild_missing_only && self.existing_index.contains_key(&cid) {
continue;
}
let shard = self.assign_shard(&cid);
let offset = Self::assign_offset(idx);
let flags = Self::detect_flags(&entry.metadata);
let size = entry.size_bytes;
let index_entry = IndexEntry {
cid: cid.clone(),
offset,
size,
shard,
flags,
};
self.index.insert(cid, index_entry);
self.progress.blocks_rebuilt += 1;
}
}
/// **Validation phase** — verify consistency between scan entries and the index.
///
/// For every block that was scanned (regardless of verification status),
/// this phase checks that a corresponding entry exists in [`Self::index`].
/// Missing entries are recorded as errors. If any errors exist after
/// validation the phase is set to [`RebuildPhase::Failed`]; otherwise it
/// transitions to [`RebuildPhase::Complete`].
pub fn validate_phase(&mut self) {
self.progress.phase = RebuildPhase::Validating;
let mut validation_errors: Vec<String> = Vec::new();
for entry in &self.scan_entries {
if !self.index.contains_key(&entry.cid) {
// If rebuild_missing_only and already in existing_index, it's OK.
let in_existing = self.existing_index.contains_key(&entry.cid);
if !(self.config.rebuild_missing_only && in_existing) {
validation_errors.push(format!(
"consistency error: scanned block '{}' not found in rebuilt index",
entry.cid
));
}
}
}
for err in &validation_errors {
if self.progress.errors.len() < self.config.max_errors {
self.progress.errors.push(err.clone());
}
}
if validation_errors.is_empty() {
self.progress.phase = RebuildPhase::Complete;
} else {
self.progress.phase = RebuildPhase::Failed(format!(
"{} consistency error(s) detected",
validation_errors.len()
));
}
}
// ── All-in-one pipeline ───────────────────────────────────────────────
/// Execute the full four-phase rebuild pipeline and return a reference to
/// the final progress snapshot.
///
/// Equivalent to calling (in order):
/// 1. [`Self::load_blocks`]
/// 2. [`Self::load_existing_index`]
/// 3. [`Self::verify_phase`]
/// 4. [`Self::rebuild_phase`]
/// 5. [`Self::validate_phase`]
pub fn run_full_rebuild(
&mut self,
blocks: Vec<(String, Vec<u8>, HashMap<String, String>)>,
existing: Vec<IndexEntry>,
now: u64,
) -> &RebuildProgress {
self.load_blocks(blocks, now);
self.load_existing_index(existing);
self.verify_phase();
self.rebuild_phase(now);
self.validate_phase();
&self.progress
}
// ── Query helpers ─────────────────────────────────────────────────────
/// Look up a CID in the rebuilt index.
///
/// Returns `None` when no entry was reconstructed for that CID.
pub fn get_index_entry(&self, cid: &str) -> Option<&IndexEntry> {
self.index.get(cid)
}
/// Number of entries currently in the rebuilt index.
pub fn index_size(&self) -> usize {
self.index.len()
}
/// Produce a [`RebuildStats`] snapshot of the current engine state.
pub fn rebuild_stats(&self) -> RebuildStats {
RebuildStats {
blocks_scanned: self.progress.blocks_scanned,
blocks_verified: self.progress.blocks_verified,
blocks_rebuilt: self.progress.blocks_rebuilt,
error_count: self.progress.errors.len(),
index_size: self.index.len(),
phase: self.progress.phase.label().to_string(),
}
}
/// Return all index entries as a cloned `Vec`.
pub fn export_index(&self) -> Vec<IndexEntry> {
self.index.values().cloned().collect()
}
/// Return all scan entries (including unverified ones).
pub fn scan_entries(&self) -> &[BlockScanEntry] {
&self.scan_entries
}
/// Return the current errors list.
pub fn errors(&self) -> &[String] {
&self.progress.errors
}
/// Return a reference to the current progress.
pub fn progress(&self) -> &RebuildProgress {
&self.progress
}
/// Return a reference to the existing index.
pub fn existing_index(&self) -> &HashMap<String, IndexEntry> {
&self.existing_index
}
/// Reset the engine state, preserving the configuration.
///
/// Clears scan entries, rebuilt index, existing index, and progress.
pub fn reset(&mut self) {
self.scan_entries.clear();
self.index.clear();
self.existing_index.clear();
self.progress = RebuildProgress::new(0);
}
/// Check whether the engine has completed (successfully or with failure).
pub fn is_finished(&self) -> bool {
matches!(
self.progress.phase,
RebuildPhase::Complete | RebuildPhase::Failed(_)
)
}
/// Returns `true` if the rebuild completed without any errors.
pub fn is_successful(&self) -> bool {
matches!(self.progress.phase, RebuildPhase::Complete) && self.progress.errors.is_empty()
}
/// Merge an additional set of `IndexEntry` records from an external source
/// into the rebuilt index without overwriting existing entries.
pub fn merge_index(&mut self, entries: Vec<IndexEntry>) {
for entry in entries {
self.index.entry(entry.cid.clone()).or_insert(entry);
}
}
/// Forcibly insert (or overwrite) an [`IndexEntry`] in the rebuilt index.
pub fn upsert_index_entry(&mut self, entry: IndexEntry) {
self.index.insert(entry.cid.clone(), entry);
}
/// Remove an entry from the rebuilt index by CID. Returns the removed
/// entry if it existed.
pub fn remove_index_entry(&mut self, cid: &str) -> Option<IndexEntry> {
self.index.remove(cid)
}
/// Return the subset of scanned blocks that failed verification (or were
/// never verified).
pub fn unverified_entries(&self) -> Vec<&BlockScanEntry> {
self.scan_entries.iter().filter(|e| !e.verified).collect()
}
/// Return references to all [`IndexEntry`] records in the rebuilt index.
pub fn all_index_entries(&self) -> Vec<&IndexEntry> {
self.index.values().collect()
}
/// Find index entries assigned to a specific shard.
pub fn entries_in_shard(&self, shard: u8) -> Vec<&IndexEntry> {
self.index.values().filter(|e| e.shard == shard).collect()
}
/// Return all index entries that have the pinned flag set.
pub fn pinned_entries(&self) -> Vec<&IndexEntry> {
self.index.values().filter(|e| e.is_pinned()).collect()
}
/// Return all index entries that have the compressed flag set.
pub fn compressed_entries(&self) -> Vec<&IndexEntry> {
self.index.values().filter(|e| e.is_compressed()).collect()
}
/// Return all index entries that have the encrypted flag set.
pub fn encrypted_entries(&self) -> Vec<&IndexEntry> {
self.index.values().filter(|e| e.is_encrypted()).collect()
}
}
// ── Tests ─────────────────────────────────────────────────────────────────────
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use std::env::temp_dir;
use crate::block_index_rebuild::{
BlockIndexRebuild, BlockScanEntry, IndexEntry, RebuildConfig, RebuildPhase,
};
// ── Helper factories ──────────────────────────────────────────────────────
fn default_config() -> RebuildConfig {
RebuildConfig::default()
}
fn make_block(
cid: &str,
data: &[u8],
meta: &[(&str, &str)],
) -> (String, Vec<u8>, HashMap<String, String>) {
let metadata = meta
.iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect();
(cid.to_string(), data.to_vec(), metadata)
}
fn simple_blocks() -> Vec<(String, Vec<u8>, HashMap<String, String>)> {
vec![
make_block("QmAlpha", b"hello world", &[]),
make_block("QmBeta", b"foo bar baz", &[("pinned", "true")]),
make_block("QmGamma", b"compressed content", &[("compressed", "true")]),
]
}
// ── 1. default config values ──────────────────────────────────────────────
#[test]
fn test_default_config_shard_count() {
let cfg = default_config();
assert_eq!(cfg.shard_count, 16);
}
#[test]
fn test_default_config_verify_checksums() {
let cfg = default_config();
assert!(cfg.verify_checksums);
}
#[test]
fn test_default_config_rebuild_missing_only() {
let cfg = default_config();
assert!(!cfg.rebuild_missing_only);
}
#[test]
fn test_default_config_max_errors() {
let cfg = default_config();
assert_eq!(cfg.max_errors, 100);
}
#[test]
fn test_default_config_batch_size() {
let cfg = default_config();
assert_eq!(cfg.batch_size, 1000);
}
// ── 2. checksum ───────────────────────────────────────────────────────────
#[test]
fn test_checksum_empty_slice() {
let cs = BlockScanEntry::compute_checksum(&[]);
// FNV-1a of empty bytes: hash stays at offset basis
let h: u64 = 14_695_981_039_346_656_037;
let expected = ((h >> 32) as u32) ^ (h as u32);
assert_eq!(cs, expected);
}
#[test]
fn test_checksum_single_byte() {
let cs = BlockScanEntry::compute_checksum(b"A");
assert_ne!(cs, 0);
}
#[test]
fn test_checksum_deterministic() {
let cs1 = BlockScanEntry::compute_checksum(b"hello world");
let cs2 = BlockScanEntry::compute_checksum(b"hello world");
assert_eq!(cs1, cs2);
}
#[test]
fn test_checksum_different_inputs_differ() {
let cs1 = BlockScanEntry::compute_checksum(b"foo");
let cs2 = BlockScanEntry::compute_checksum(b"bar");
assert_ne!(cs1, cs2);
}
// ── 3. assign_shard ───────────────────────────────────────────────────────
#[test]
fn test_assign_shard_in_range() {
let engine = BlockIndexRebuild::new(default_config());
let s = engine.assign_shard("QmFoo");
assert!(s < 16);
}
#[test]
fn test_assign_shard_deterministic() {
let engine = BlockIndexRebuild::new(default_config());
assert_eq!(engine.assign_shard("QmBar"), engine.assign_shard("QmBar"));
}
#[test]
fn test_assign_shard_single_shard() {
let cfg = RebuildConfig {
shard_count: 1,
..Default::default()
};
let engine = BlockIndexRebuild::new(cfg);
assert_eq!(engine.assign_shard("anything"), 0);
}
// ── 4. assign_offset ─────────────────────────────────────────────────────
#[test]
fn test_assign_offset_zero() {
assert_eq!(BlockIndexRebuild::assign_offset(0), 0);
}
#[test]
fn test_assign_offset_page_aligned() {
assert_eq!(BlockIndexRebuild::assign_offset(1), 4_096);
assert_eq!(BlockIndexRebuild::assign_offset(2), 8_192);
}
#[test]
fn test_assign_offset_large_index() {
let offset = BlockIndexRebuild::assign_offset(1_000);
assert_eq!(offset, 1_000 * 4_096);
}
// ── 5. detect_flags ───────────────────────────────────────────────────────
#[test]
fn test_detect_flags_empty_meta() {
let meta = HashMap::new();
assert_eq!(BlockIndexRebuild::detect_flags(&meta), 0x00);
}
#[test]
fn test_detect_flags_pinned() {
let mut meta = HashMap::new();
meta.insert("pinned".to_string(), "true".to_string());
assert_eq!(BlockIndexRebuild::detect_flags(&meta), 0x01);
}
#[test]
fn test_detect_flags_compressed() {
let mut meta = HashMap::new();
meta.insert("compressed".to_string(), "true".to_string());
assert_eq!(BlockIndexRebuild::detect_flags(&meta), 0x02);
}
#[test]
fn test_detect_flags_encrypted() {
let mut meta = HashMap::new();
meta.insert("encrypted".to_string(), "true".to_string());
assert_eq!(BlockIndexRebuild::detect_flags(&meta), 0x04);
}
#[test]
fn test_detect_flags_all_three() {
let mut meta = HashMap::new();
meta.insert("pinned".to_string(), "true".to_string());
meta.insert("compressed".to_string(), "true".to_string());
meta.insert("encrypted".to_string(), "true".to_string());
assert_eq!(BlockIndexRebuild::detect_flags(&meta), 0x07);
}
#[test]
fn test_detect_flags_false_values_ignored() {
let mut meta = HashMap::new();
meta.insert("pinned".to_string(), "false".to_string());
meta.insert("compressed".to_string(), "no".to_string());
assert_eq!(BlockIndexRebuild::detect_flags(&meta), 0x00);
}
// ── 6. load_blocks ────────────────────────────────────────────────────────
#[test]
fn test_load_blocks_count() {
let mut engine = BlockIndexRebuild::new(default_config());
engine.load_blocks(simple_blocks(), 1_000_000);
assert_eq!(engine.scan_entries.len(), 3);
assert_eq!(engine.progress.blocks_scanned, 3);
}
#[test]
fn test_load_blocks_sets_phase() {
let mut engine = BlockIndexRebuild::new(default_config());
engine.load_blocks(simple_blocks(), 42);
assert_eq!(engine.progress.phase, RebuildPhase::Scanning);
}
#[test]
fn test_load_blocks_records_started_at() {
let mut engine = BlockIndexRebuild::new(default_config());
engine.load_blocks(simple_blocks(), 999);
assert_eq!(engine.progress.started_at, 999);
}
#[test]
fn test_load_blocks_checksum_stored() {
let mut engine = BlockIndexRebuild::new(default_config());
let data = b"test data";
let expected = BlockScanEntry::compute_checksum(data);
engine.load_blocks(vec![make_block("QmTest", data, &[])], 0);
assert_eq!(engine.scan_entries[0].checksum, expected);
}
#[test]
fn test_load_blocks_size_bytes() {
let mut engine = BlockIndexRebuild::new(default_config());
let data = b"twelve bytes";
engine.load_blocks(vec![make_block("QmSize", data, &[])], 0);
assert_eq!(engine.scan_entries[0].size_bytes, data.len() as u64);
}
#[test]
fn test_load_blocks_verified_false_initially() {
let mut engine = BlockIndexRebuild::new(default_config());
engine.load_blocks(vec![make_block("QmV", b"data", &[])], 0);
// Before verify_phase the field starts false inside load_blocks,
// but verify_phase sets it. Here we only test load state.
// Actually load_blocks does NOT set verified; verify_phase does.
// verify_phase sets it to true, so after just load_blocks it is false.
assert!(!engine.scan_entries[0].verified);
}
// ── 7. load_existing_index ────────────────────────────────────────────────
#[test]
fn test_load_existing_index_populates() {
let mut engine = BlockIndexRebuild::new(default_config());
let entry = IndexEntry {
cid: "QmExist".to_string(),
offset: 0,
size: 100,
shard: 3,
flags: 0,
};
engine.load_existing_index(vec![entry.clone()]);
assert_eq!(engine.existing_index.len(), 1);
assert_eq!(engine.existing_index.get("QmExist"), Some(&entry));
}
// ── 8. verify_phase ───────────────────────────────────────────────────────
#[test]
fn test_verify_phase_marks_verified() {
let mut engine = BlockIndexRebuild::new(default_config());
engine.load_blocks(simple_blocks(), 0);
engine.verify_phase();
assert!(engine.scan_entries.iter().all(|e| e.verified));
}
#[test]
fn test_verify_phase_increments_counter() {
let mut engine = BlockIndexRebuild::new(default_config());
engine.load_blocks(simple_blocks(), 0);
engine.verify_phase();
assert_eq!(engine.progress.blocks_verified, 3);
}
#[test]
fn test_verify_phase_sets_phase() {
let mut engine = BlockIndexRebuild::new(default_config());
engine.load_blocks(simple_blocks(), 0);
engine.verify_phase();
assert_eq!(engine.progress.phase, RebuildPhase::Verifying);
}
// ── 9. rebuild_phase ─────────────────────────────────────────────────────
#[test]
fn test_rebuild_phase_builds_index() {
let mut engine = BlockIndexRebuild::new(default_config());
engine.load_blocks(simple_blocks(), 0);
engine.verify_phase();
engine.rebuild_phase(0);
assert_eq!(engine.index.len(), 3);
assert_eq!(engine.progress.blocks_rebuilt, 3);
}
#[test]
fn test_rebuild_phase_shard_range() {
let mut engine = BlockIndexRebuild::new(default_config());
engine.load_blocks(simple_blocks(), 0);
engine.verify_phase();
engine.rebuild_phase(0);
for entry in engine.index.values() {
assert!(entry.shard < 16);
}
}
#[test]
fn test_rebuild_phase_offset_page_aligned() {
let mut engine = BlockIndexRebuild::new(default_config());
engine.load_blocks(simple_blocks(), 0);
engine.verify_phase();
engine.rebuild_phase(0);
for entry in engine.index.values() {
assert_eq!(entry.offset % 4_096, 0);
}
}
#[test]
fn test_rebuild_phase_flags_pinned() {
let mut engine = BlockIndexRebuild::new(default_config());
engine.load_blocks(
vec![make_block("QmPinned", b"data", &[("pinned", "true")])],
0,
);
engine.verify_phase();
engine.rebuild_phase(0);
let entry = engine.index.get("QmPinned").expect("entry missing");
assert!(entry.is_pinned());
}
#[test]
fn test_rebuild_phase_rebuild_missing_only_skips_existing() {
let cfg = RebuildConfig {
rebuild_missing_only: true,
..Default::default()
};
let mut engine = BlockIndexRebuild::new(cfg);
let existing = IndexEntry {
cid: "QmAlpha".to_string(),
offset: 0,
size: 11,
shard: 0,
flags: 0,
};
engine.load_existing_index(vec![existing]);
engine.load_blocks(simple_blocks(), 0);
engine.verify_phase();
engine.rebuild_phase(0);
// QmAlpha is in existing_index, so it should NOT be in the new index
assert!(!engine.index.contains_key("QmAlpha"));
// QmBeta and QmGamma should be rebuilt
assert!(engine.index.contains_key("QmBeta"));
assert!(engine.index.contains_key("QmGamma"));
}
// ── 10. validate_phase ────────────────────────────────────────────────────
#[test]
fn test_validate_phase_complete_on_success() {
let mut engine = BlockIndexRebuild::new(default_config());
engine.load_blocks(simple_blocks(), 0);
engine.verify_phase();
engine.rebuild_phase(0);
engine.validate_phase();
assert_eq!(engine.progress.phase, RebuildPhase::Complete);
}
#[test]
fn test_validate_phase_failed_when_missing() {
let mut engine = BlockIndexRebuild::new(default_config());
engine.load_blocks(simple_blocks(), 0);
// Skip rebuild_phase so index is empty
engine.validate_phase();
assert!(matches!(engine.progress.phase, RebuildPhase::Failed(_)));
}
#[test]
fn test_validate_phase_errors_recorded() {
let mut engine = BlockIndexRebuild::new(default_config());
engine.load_blocks(simple_blocks(), 0);
engine.validate_phase();
assert!(!engine.progress.errors.is_empty());
}
// ── 11. run_full_rebuild ──────────────────────────────────────────────────
#[test]
fn test_run_full_rebuild_complete() {
let mut engine = BlockIndexRebuild::new(default_config());
let progress = engine.run_full_rebuild(simple_blocks(), vec![], 12345);
assert_eq!(progress.phase, RebuildPhase::Complete);
}
#[test]
fn test_run_full_rebuild_counts() {
let mut engine = BlockIndexRebuild::new(default_config());
let progress = engine.run_full_rebuild(simple_blocks(), vec![], 0);
assert_eq!(progress.blocks_scanned, 3);
assert_eq!(progress.blocks_verified, 3);
assert_eq!(progress.blocks_rebuilt, 3);
}
#[test]
fn test_run_full_rebuild_no_errors() {
let mut engine = BlockIndexRebuild::new(default_config());
let progress = engine.run_full_rebuild(simple_blocks(), vec![], 0);
assert!(progress.errors.is_empty());
}
#[test]
fn test_run_full_rebuild_with_existing() {
let cfg = RebuildConfig {
rebuild_missing_only: true,
..Default::default()
};
let mut engine = BlockIndexRebuild::new(cfg);
let existing = IndexEntry {
cid: "QmAlpha".to_string(),
offset: 0,
size: 11,
shard: 5,
flags: 0,
};
let progress = engine.run_full_rebuild(simple_blocks(), vec![existing], 0);
assert_eq!(progress.phase, RebuildPhase::Complete);
assert_eq!(engine.index_size(), 2);
}
// ── 12. get_index_entry / index_size ─────────────────────────────────────
#[test]
fn test_get_index_entry_found() {
let mut engine = BlockIndexRebuild::new(default_config());
engine.run_full_rebuild(simple_blocks(), vec![], 0);
assert!(engine.get_index_entry("QmAlpha").is_some());
}
#[test]
fn test_get_index_entry_not_found() {
let engine = BlockIndexRebuild::new(default_config());
assert!(engine.get_index_entry("QmNonexistent").is_none());
}
#[test]
fn test_index_size_empty() {
let engine = BlockIndexRebuild::new(default_config());
assert_eq!(engine.index_size(), 0);
}
#[test]
fn test_index_size_after_rebuild() {
let mut engine = BlockIndexRebuild::new(default_config());
engine.run_full_rebuild(simple_blocks(), vec![], 0);
assert_eq!(engine.index_size(), 3);
}
// ── 13. rebuild_stats ────────────────────────────────────────────────────
#[test]
fn test_rebuild_stats_phase_label() {
let mut engine = BlockIndexRebuild::new(default_config());
engine.run_full_rebuild(simple_blocks(), vec![], 0);
let stats = engine.rebuild_stats();
assert_eq!(stats.phase, "Complete");
}
#[test]
fn test_rebuild_stats_error_count() {
let mut engine = BlockIndexRebuild::new(default_config());
engine.run_full_rebuild(simple_blocks(), vec![], 0);
let stats = engine.rebuild_stats();
assert_eq!(stats.error_count, 0);
}
// ── 14. RebuildPhase helpers ──────────────────────────────────────────────
#[test]
fn test_phase_label_scanning() {
assert_eq!(RebuildPhase::Scanning.label(), "Scanning");
}
#[test]
fn test_phase_label_failed() {
assert_eq!(RebuildPhase::Failed("oops".to_string()).label(), "Failed");
}
#[test]
fn test_phase_label_complete() {
assert_eq!(RebuildPhase::Complete.label(), "Complete");
}
// ── 15. IndexEntry flag helpers ───────────────────────────────────────────
#[test]
fn test_index_entry_is_pinned() {
let e = IndexEntry {
cid: "c".to_string(),
offset: 0,
size: 0,
shard: 0,
flags: 0x01,
};
assert!(e.is_pinned());
assert!(!e.is_compressed());
assert!(!e.is_encrypted());
}
#[test]
fn test_index_entry_is_compressed() {
let e = IndexEntry {
cid: "c".to_string(),
offset: 0,
size: 0,
shard: 0,
flags: 0x02,
};
assert!(e.is_compressed());
}
#[test]
fn test_index_entry_is_encrypted() {
let e = IndexEntry {
cid: "c".to_string(),
offset: 0,
size: 0,
shard: 0,
flags: 0x04,
};
assert!(e.is_encrypted());
}
#[test]
fn test_index_entry_all_flags() {
let e = IndexEntry {
cid: "c".to_string(),
offset: 0,
size: 0,
shard: 0,
flags: 0x07,
};
assert!(e.is_pinned());
assert!(e.is_compressed());
assert!(e.is_encrypted());
}
// ── 16. Utility helpers ───────────────────────────────────────────────────
#[test]
fn test_reset_clears_state() {
let mut engine = BlockIndexRebuild::new(default_config());
engine.run_full_rebuild(simple_blocks(), vec![], 0);
engine.reset();
assert_eq!(engine.scan_entries.len(), 0);
assert_eq!(engine.index.len(), 0);
assert_eq!(engine.progress.blocks_scanned, 0);
}
#[test]
fn test_is_finished_true_after_complete() {
let mut engine = BlockIndexRebuild::new(default_config());
engine.run_full_rebuild(simple_blocks(), vec![], 0);
assert!(engine.is_finished());
}
#[test]
fn test_is_finished_false_before_run() {
let engine = BlockIndexRebuild::new(default_config());
assert!(!engine.is_finished());
}
#[test]
fn test_is_successful_after_clean_run() {
let mut engine = BlockIndexRebuild::new(default_config());
engine.run_full_rebuild(simple_blocks(), vec![], 0);
assert!(engine.is_successful());
}
#[test]
fn test_merge_index_does_not_overwrite() {
let mut engine = BlockIndexRebuild::new(default_config());
engine.run_full_rebuild(vec![make_block("QmAlpha", b"hello world", &[])], vec![], 0);
let old_offset = engine.index.get("QmAlpha").expect("missing").offset;
let incoming = IndexEntry {
cid: "QmAlpha".to_string(),
offset: 9_999_999,
size: 0,
shard: 0,
flags: 0,
};
engine.merge_index(vec![incoming]);
// Should not overwrite
assert_eq!(
engine.index.get("QmAlpha").expect("missing").offset,
old_offset
);
}
#[test]
fn test_upsert_index_entry_overwrites() {
let mut engine = BlockIndexRebuild::new(default_config());
engine.run_full_rebuild(vec![make_block("QmU", b"data", &[])], vec![], 0);
let new_entry = IndexEntry {
cid: "QmU".to_string(),
offset: 77_000,
size: 42,
shard: 7,
flags: 0x03,
};
engine.upsert_index_entry(new_entry.clone());
assert_eq!(engine.index.get("QmU"), Some(&new_entry));
}
#[test]
fn test_remove_index_entry() {
let mut engine = BlockIndexRebuild::new(default_config());
engine.run_full_rebuild(simple_blocks(), vec![], 0);
let removed = engine.remove_index_entry("QmAlpha");
assert!(removed.is_some());
assert!(engine.get_index_entry("QmAlpha").is_none());
}
#[test]
fn test_pinned_entries_filter() {
let mut engine = BlockIndexRebuild::new(default_config());
engine.run_full_rebuild(simple_blocks(), vec![], 0);
let pinned = engine.pinned_entries();
// QmBeta has pinned=true
assert!(pinned.iter().any(|e| e.cid == "QmBeta"));
}
#[test]
fn test_compressed_entries_filter() {
let mut engine = BlockIndexRebuild::new(default_config());
engine.run_full_rebuild(simple_blocks(), vec![], 0);
let compressed = engine.compressed_entries();
// QmGamma has compressed=true
assert!(compressed.iter().any(|e| e.cid == "QmGamma"));
}
#[test]
fn test_entries_in_shard_returns_subset() {
let mut engine = BlockIndexRebuild::new(default_config());
engine.run_full_rebuild(simple_blocks(), vec![], 0);
// At least one shard must exist; verify the total counts add up
let total: usize = (0..16_u8).map(|s| engine.entries_in_shard(s).len()).sum();
assert_eq!(total, engine.index_size());
}
#[test]
fn test_export_index_length() {
let mut engine = BlockIndexRebuild::new(default_config());
engine.run_full_rebuild(simple_blocks(), vec![], 0);
assert_eq!(engine.export_index().len(), 3);
}
#[test]
fn test_verify_with_data_detects_mismatch() {
let mut engine = BlockIndexRebuild::new(default_config());
// Load with tampered checksum by inserting a scan entry manually
engine.progress.started_at = 1;
engine.progress.blocks_scanned = 1;
// Manually insert a scan entry with a wrong checksum
engine
.scan_entries
.push(crate::block_index_rebuild::BlockScanEntry {
cid: "QmTampered".to_string(),
size_bytes: 5,
checksum: 0xDEAD_BEEF, // intentionally wrong
verified: false,
metadata: HashMap::new(),
});
// Supply real data (different from what produced 0xDEAD_BEEF)
engine.verify_with_data(vec![("QmTampered".to_string(), b"hello".to_vec())]);
// Should record an error and NOT mark verified
let entry = &engine.scan_entries[0];
assert!(!entry.verified);
assert!(!engine.progress.errors.is_empty());
}
#[test]
fn test_unverified_entries_empty_after_verify() {
let mut engine = BlockIndexRebuild::new(default_config());
engine.load_blocks(simple_blocks(), 0);
engine.verify_phase();
assert_eq!(engine.unverified_entries().len(), 0);
}
// ── 17. temp_dir usage (file-backed simulation) ───────────────────────────
#[test]
fn test_temp_dir_accessible() {
let tmp = temp_dir();
assert!(tmp.exists());
}
#[test]
fn test_large_batch_rebuild() {
let mut engine = BlockIndexRebuild::new(default_config());
let blocks: Vec<_> = (0..200)
.map(|i| {
make_block(
&format!("QmBlock{i:04}"),
format!("data for block {i}").as_bytes(),
&[],
)
})
.collect();
let progress = engine.run_full_rebuild(blocks, vec![], 0);
assert_eq!(progress.blocks_scanned, 200);
assert_eq!(progress.blocks_rebuilt, 200);
assert_eq!(progress.phase, RebuildPhase::Complete);
}
#[test]
fn test_max_errors_cap() {
let cfg = RebuildConfig {
max_errors: 5,
..Default::default()
};
let mut engine = BlockIndexRebuild::new(cfg);
// Validate with empty index to generate many errors
let blocks: Vec<_> = (0..20)
.map(|i| make_block(&format!("QmX{i}"), b"d", &[]))
.collect();
engine.load_blocks(blocks, 0);
// Do NOT verify or rebuild so validate will find 20 missing entries
engine.validate_phase();
// Errors should be capped at max_errors = 5
assert!(engine.progress.errors.len() <= 5);
}
}