ipfrs-storage 0.2.0

Storage backends and block management for IPFRS content-addressed system
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
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
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
//! Block migration planner: schedules and tracks data movement between storage nodes/tiers.
//!
//! This module provides [`BlockMigrationPlanner`] which registers blocks,
//! creates and executes migration plans, schedules migrations by policy,
//! runs batch migrations, and generates defragmentation plans to balance data
//! across nodes.

use std::collections::{HashMap, VecDeque};

// ─────────────────────────────────────────────────────────────────────────────
// Type aliases
// ─────────────────────────────────────────────────────────────────────────────

/// 32-byte content-addressed block identifier (e.g. SHA-256 digest).
pub type BmpBlockId = [u8; 32];

/// Opaque numeric identifier for a migration plan.
pub type BmpPlanId = u64;

/// Alias for [`BlockMigrationPlanner`] — used in pub re-exports.
pub type BmpBlockMigrationPlanner = BlockMigrationPlanner;

// ─────────────────────────────────────────────────────────────────────────────
// Utility helpers (no external crates)
// ─────────────────────────────────────────────────────────────────────────────

/// Xorshift64 PRNG — not cryptographically secure, but fast and dependency-free.
#[inline]
fn xorshift64(state: &mut u64) -> u64 {
    let mut x = *state;
    x ^= x << 13;
    x ^= x >> 7;
    x ^= x << 17;
    *state = x;
    x
}

/// FNV-1a 64-bit hash — deterministic digest of arbitrary bytes.
#[inline]
fn fnv1a_64(data: &[u8]) -> u64 {
    let mut h: u64 = 14_695_981_039_346_656_037;
    for &b in data {
        h ^= b as u64;
        h = h.wrapping_mul(1_099_511_628_211);
    }
    h
}

/// Monotonic Unix-epoch timestamp approximation (nanoseconds from epoch zero
/// expressed as a plain u64 counter seeded from the system clock seconds
/// value; guaranteed non-zero and strictly increasing within one process).
fn now_ts() -> u64 {
    use std::time::{SystemTime, UNIX_EPOCH};
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_nanos() as u64)
        .unwrap_or(1)
}

// ─────────────────────────────────────────────────────────────────────────────
// Enumerations
// ─────────────────────────────────────────────────────────────────────────────

/// Lifecycle state of a [`BmpMigrationPlan`].
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum BmpPlanStatus {
    /// Waiting to be executed.
    Pending,
    /// Currently being executed.
    InProgress,
    /// Execution finished successfully.
    Completed,
    /// Execution finished with at least one failure.
    Failed,
    /// Plan was cancelled before execution began.
    Cancelled,
}

impl std::fmt::Display for BmpPlanStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Pending => write!(f, "Pending"),
            Self::InProgress => write!(f, "InProgress"),
            Self::Completed => write!(f, "Completed"),
            Self::Failed => write!(f, "Failed"),
            Self::Cancelled => write!(f, "Cancelled"),
        }
    }
}

/// Policy for ordering migration candidates when scheduling.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum BmpPriorityPolicy {
    /// Process plans in the order they were created (FIFO).
    FifoQueue,
    /// Migrate blocks with the highest `access_frequency` first.
    HighFrequencyFirst,
    /// Migrate the largest blocks first (maximise bytes moved per slot).
    LargestFirst,
    /// Migrate the smallest blocks first (minimise latency per block).
    SmallestFirst,
    /// Prefer blocks where the estimated migration cost is lowest (bytes × tier distance).
    CostOptimized,
}

impl std::fmt::Display for BmpPriorityPolicy {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::FifoQueue => write!(f, "FifoQueue"),
            Self::HighFrequencyFirst => write!(f, "HighFrequencyFirst"),
            Self::LargestFirst => write!(f, "LargestFirst"),
            Self::SmallestFirst => write!(f, "SmallestFirst"),
            Self::CostOptimized => write!(f, "CostOptimized"),
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Data structures
// ─────────────────────────────────────────────────────────────────────────────

/// Metadata tracked for each registered block.
#[derive(Clone, Debug)]
pub struct BmpBlockMeta {
    /// 32-byte content-addressed identifier.
    pub id: BmpBlockId,
    /// Compressed or raw byte size of the block.
    pub size_bytes: u64,
    /// Logical node or volume currently holding the block.
    pub src_node: String,
    /// Intended destination node (set once a plan is associated).
    pub dst_node: Option<String>,
    /// Storage tier index (0 = fastest / hottest).
    pub tier: u8,
    /// Exponentially-smoothed access frequency (accesses per unit time).
    pub access_frequency: f64,
    /// Timestamp (nanoseconds) of the most recent access.
    pub last_accessed: u64,
    /// When `true` the block must not be migrated.
    pub is_pinned: bool,
}

impl BmpBlockMeta {
    /// Create a new block record.
    pub fn new(
        id: BmpBlockId,
        size_bytes: u64,
        src_node: impl Into<String>,
        tier: u8,
        access_frequency: f64,
    ) -> Self {
        Self {
            id,
            size_bytes,
            src_node: src_node.into(),
            dst_node: None,
            tier,
            access_frequency,
            last_accessed: now_ts(),
            is_pinned: false,
        }
    }
}

/// A migration plan grouping one or more blocks for movement to a destination node.
#[derive(Clone, Debug)]
pub struct BmpMigrationPlan {
    /// Unique plan identifier.
    pub id: BmpPlanId,
    /// Blocks covered by this plan.
    pub block_ids: Vec<BmpBlockId>,
    /// Node from which the blocks originate.
    pub src_node: String,
    /// Node to which the blocks should be moved.
    pub dst_node: String,
    /// Higher value means higher urgency.
    pub priority: u32,
    /// Total byte size of all blocks in the plan.
    pub estimated_bytes: u64,
    /// Current lifecycle state.
    pub status: BmpPlanStatus,
    /// Creation timestamp (nanoseconds).
    pub created_at: u64,
}

/// Per-block execution record appended to the execution log.
#[derive(Clone, Debug)]
pub struct BmpMigrationRecord {
    /// Execution timestamp (nanoseconds).
    pub ts: u64,
    /// Plan that triggered this record.
    pub plan_id: BmpPlanId,
    /// Block that was (or failed to be) migrated.
    pub block_id: BmpBlockId,
    /// Bytes actually transferred.
    pub bytes_moved: u64,
    /// `true` if the block was moved without error.
    pub success: bool,
    /// Error description when `success` is `false`.
    pub error: Option<String>,
}

/// Planner behaviour configuration.
#[derive(Clone, Debug)]
pub struct BmpPlannerConfig {
    /// Maximum number of plans that can be in `InProgress` simultaneously.
    pub max_concurrent_migrations: usize,
    /// Maximum aggregate bytes per second across all active migrations (0 = unlimited).
    pub bandwidth_limit_kbps: u64,
    /// Default policy used by [`BlockMigrationPlanner::schedule_migrations`].
    pub priority_policy: BmpPriorityPolicy,
    /// When `true`, plans are validated and logged but no data is actually moved.
    pub dry_run: bool,
}

impl Default for BmpPlannerConfig {
    fn default() -> Self {
        Self {
            max_concurrent_migrations: 4,
            bandwidth_limit_kbps: 0,
            priority_policy: BmpPriorityPolicy::FifoQueue,
            dry_run: false,
        }
    }
}

/// Aggregate statistics returned by [`BlockMigrationPlanner::migration_stats`].
#[derive(Clone, Debug, Default)]
pub struct BmpPlannerStats {
    /// Total blocks registered.
    pub total_blocks: usize,
    /// Blocks currently marked as pinned.
    pub pinned_blocks: usize,
    /// Total plans ever created.
    pub total_plans: usize,
    /// Plans in `Pending` state.
    pub pending_plans: usize,
    /// Plans in `InProgress` state.
    pub in_progress_plans: usize,
    /// Plans in `Completed` state.
    pub completed_plans: usize,
    /// Plans in `Failed` state.
    pub failed_plans: usize,
    /// Plans in `Cancelled` state.
    pub cancelled_plans: usize,
    /// Total execution log entries.
    pub total_records: usize,
    /// Total bytes successfully moved across all completed records.
    pub total_bytes_moved: u64,
    /// Number of records where `success == false`.
    pub total_failures: usize,
}

/// Result of executing a single plan.
#[derive(Clone, Debug)]
pub struct BmpExecutionResult {
    /// The plan that was executed.
    pub plan_id: BmpPlanId,
    /// Final plan status after execution.
    pub status: BmpPlanStatus,
    /// Blocks that were successfully migrated.
    pub succeeded_blocks: Vec<BmpBlockId>,
    /// Blocks that failed to migrate, with their error strings.
    pub failed_blocks: Vec<(BmpBlockId, String)>,
    /// Total bytes moved.
    pub bytes_moved: u64,
}

/// Aggregated result of a [`BlockMigrationPlanner::run_batch_migration`] call.
#[derive(Clone, Debug, Default)]
pub struct BmpBatchResult {
    /// How many plans were executed in this batch.
    pub plans_executed: usize,
    /// Plans that completed with all blocks successful.
    pub plans_completed: usize,
    /// Plans that had at least one failed block.
    pub plans_failed: usize,
    /// Total bytes moved across all plans in the batch.
    pub total_bytes_moved: u64,
    /// Per-plan execution results.
    pub results: Vec<BmpExecutionResult>,
}

// ─────────────────────────────────────────────────────────────────────────────
// Error type
// ─────────────────────────────────────────────────────────────────────────────

/// Errors returned by [`BlockMigrationPlanner`] operations.
#[derive(Debug, thiserror::Error)]
pub enum BmpError {
    #[error("block {0} is not registered")]
    BlockNotFound(String),
    #[error("plan {0} does not exist")]
    PlanNotFound(BmpPlanId),
    #[error("plan {0} is not in Pending state (current: {1})")]
    PlanNotPending(BmpPlanId, BmpPlanStatus),
    #[error("plan has no blocks")]
    EmptyPlan,
    #[error("block {0} is pinned and cannot be migrated")]
    BlockPinned(String),
    #[error("destination node is empty")]
    EmptyDstNode,
    #[error("no eligible blocks found for scheduling")]
    NoEligibleBlocks,
}

fn block_id_hex(id: &BmpBlockId) -> String {
    let mut s = String::with_capacity(64);
    for b in id {
        s.push_str(&format!("{:02x}", b));
    }
    s
}

// ─────────────────────────────────────────────────────────────────────────────
// Core planner
// ─────────────────────────────────────────────────────────────────────────────

/// Block migration planner: registers blocks, creates plans, executes them,
/// and provides scheduling and defragmentation helpers.
pub struct BlockMigrationPlanner {
    /// All known blocks, keyed by their 32-byte identifier.
    blocks: HashMap<BmpBlockId, BmpBlockMeta>,
    /// All plans, keyed by plan id.
    plans: HashMap<BmpPlanId, BmpMigrationPlan>,
    /// Bounded execution log (cap 1000 most-recent records).
    log: VecDeque<BmpMigrationRecord>,
    /// Behaviour configuration.
    config: BmpPlannerConfig,
    /// Monotonically-increasing plan id counter.
    next_plan_id: BmpPlanId,
    /// PRNG state (xorshift64 seeded from a mix of time and FNV).
    rng_state: u64,
}

impl BlockMigrationPlanner {
    // ── Construction ──────────────────────────────────────────────────────────

    /// Create a planner with default configuration.
    pub fn new() -> Self {
        Self::with_config(BmpPlannerConfig::default())
    }

    /// Create a planner with the supplied configuration.
    pub fn with_config(config: BmpPlannerConfig) -> Self {
        let seed_data = b"block_migration_planner_v1";
        let ts = now_ts();
        let base = fnv1a_64(seed_data);
        let mut rng_state = base ^ ts;
        if rng_state == 0 {
            rng_state = 0xdeadbeef_cafebabe;
        }
        Self {
            blocks: HashMap::new(),
            plans: HashMap::new(),
            log: VecDeque::new(),
            config,
            next_plan_id: 1,
            rng_state,
        }
    }

    /// Return a reference to the current configuration.
    pub fn config(&self) -> &BmpPlannerConfig {
        &self.config
    }

    /// Replace the configuration.
    pub fn set_config(&mut self, config: BmpPlannerConfig) {
        self.config = config;
    }

    // ── Block registration ────────────────────────────────────────────────────

    /// Register a new block (or overwrite if it already exists).
    pub fn register_block(
        &mut self,
        id: BmpBlockId,
        size_bytes: u64,
        src_node: impl Into<String>,
        tier: u8,
        access_frequency: f64,
    ) {
        let meta = BmpBlockMeta::new(id, size_bytes, src_node, tier, access_frequency);
        self.blocks.insert(id, meta);
    }

    /// Record an access event for a block, bumping `last_accessed` and applying
    /// a simple exponential-moving-average update to `access_frequency`.
    pub fn update_access(&mut self, id: &BmpBlockId) -> Result<(), BmpError> {
        let meta = self
            .blocks
            .get_mut(id)
            .ok_or_else(|| BmpError::BlockNotFound(block_id_hex(id)))?;
        meta.last_accessed = now_ts();
        // EMA with α = 0.1
        meta.access_frequency = meta.access_frequency * 0.9 + 1.0 * 0.1;
        Ok(())
    }

    /// Pin a block so it will not be selected for automatic migration.
    pub fn pin(&mut self, id: &BmpBlockId) -> Result<(), BmpError> {
        let meta = self
            .blocks
            .get_mut(id)
            .ok_or_else(|| BmpError::BlockNotFound(block_id_hex(id)))?;
        meta.is_pinned = true;
        Ok(())
    }

    /// Unpin a previously pinned block.
    pub fn unpin(&mut self, id: &BmpBlockId) -> Result<(), BmpError> {
        let meta = self
            .blocks
            .get_mut(id)
            .ok_or_else(|| BmpError::BlockNotFound(block_id_hex(id)))?;
        meta.is_pinned = false;
        Ok(())
    }

    /// Return a reference to a block's metadata, or `None` if not registered.
    pub fn block_meta(&self, id: &BmpBlockId) -> Option<&BmpBlockMeta> {
        self.blocks.get(id)
    }

    /// Number of currently registered blocks.
    pub fn block_count(&self) -> usize {
        self.blocks.len()
    }

    // ── Plan management ───────────────────────────────────────────────────────

    /// Create a migration plan for the given blocks.
    ///
    /// The `src_node` is inferred from the first block's current location; if
    /// the list spans multiple source nodes the first one wins (callers should
    /// group by node before creating plans when that matters).
    ///
    /// Returns `Err` if any block is pinned or if any block is not registered.
    pub fn create_plan(
        &mut self,
        block_ids: Vec<BmpBlockId>,
        dst_node: impl Into<String>,
        priority: u32,
    ) -> Result<BmpPlanId, BmpError> {
        if block_ids.is_empty() {
            return Err(BmpError::EmptyPlan);
        }
        let dst_node = dst_node.into();
        if dst_node.is_empty() {
            return Err(BmpError::EmptyDstNode);
        }

        // Validate all blocks exist and are not pinned.
        for bid in &block_ids {
            let meta = self
                .blocks
                .get(bid)
                .ok_or_else(|| BmpError::BlockNotFound(block_id_hex(bid)))?;
            if meta.is_pinned {
                return Err(BmpError::BlockPinned(block_id_hex(bid)));
            }
        }

        // Infer src_node and tally estimated bytes.
        let src_node = self
            .blocks
            .get(&block_ids[0])
            .map(|m| m.src_node.clone())
            .unwrap_or_default();

        let estimated_bytes: u64 = block_ids
            .iter()
            .filter_map(|bid| self.blocks.get(bid).map(|m| m.size_bytes))
            .sum();

        let plan_id = self.next_plan_id;
        self.next_plan_id += 1;

        // Record intended dst_node on each block.
        for bid in &block_ids {
            if let Some(meta) = self.blocks.get_mut(bid) {
                meta.dst_node = Some(dst_node.clone());
            }
        }

        let plan = BmpMigrationPlan {
            id: plan_id,
            block_ids,
            src_node,
            dst_node,
            priority,
            estimated_bytes,
            status: BmpPlanStatus::Pending,
            created_at: now_ts(),
        };
        self.plans.insert(plan_id, plan);
        Ok(plan_id)
    }

    /// Cancel a pending plan.  Plans that are already `InProgress`, `Completed`,
    /// `Failed`, or `Cancelled` cannot be cancelled again.
    pub fn cancel_plan(&mut self, plan_id: BmpPlanId) -> Result<(), BmpError> {
        let plan = self
            .plans
            .get_mut(&plan_id)
            .ok_or(BmpError::PlanNotFound(plan_id))?;
        if plan.status != BmpPlanStatus::Pending {
            return Err(BmpError::PlanNotPending(plan_id, plan.status));
        }
        plan.status = BmpPlanStatus::Cancelled;
        Ok(())
    }

    /// Execute a migration plan.
    ///
    /// Each block is migrated in sequence; a simulated success/failure decision
    /// is made via xorshift64 (roughly 90 % success when `!dry_run`).
    /// In `dry_run` mode all blocks succeed without moving.
    pub fn execute_plan(&mut self, plan_id: BmpPlanId) -> Result<BmpExecutionResult, BmpError> {
        // Verify the plan exists and is pending.
        {
            let plan = self
                .plans
                .get(&plan_id)
                .ok_or(BmpError::PlanNotFound(plan_id))?;
            if plan.status != BmpPlanStatus::Pending {
                return Err(BmpError::PlanNotPending(plan_id, plan.status));
            }
        }

        // Mark in-progress.
        if let Some(plan) = self.plans.get_mut(&plan_id) {
            plan.status = BmpPlanStatus::InProgress;
        }

        let block_ids: Vec<BmpBlockId> = self
            .plans
            .get(&plan_id)
            .map(|p| p.block_ids.clone())
            .unwrap_or_default();

        let dst_node: String = self
            .plans
            .get(&plan_id)
            .map(|p| p.dst_node.clone())
            .unwrap_or_default();

        let dry_run = self.config.dry_run;

        let mut succeeded_blocks: Vec<BmpBlockId> = Vec::new();
        let mut failed_blocks: Vec<(BmpBlockId, String)> = Vec::new();
        let mut bytes_moved: u64 = 0;

        for bid in &block_ids {
            let size = self.blocks.get(bid).map(|m| m.size_bytes).unwrap_or(0);

            let (success, error_msg): (bool, Option<String>) = if dry_run {
                (true, None)
            } else {
                // xorshift64: success if high nibble is not 0xF (~93.75% success).
                let roll = xorshift64(&mut self.rng_state);
                if (roll >> 60) != 0xF {
                    (true, None)
                } else {
                    (
                        false,
                        Some(format!("simulated I/O error (roll=0x{:016x})", roll)),
                    )
                }
            };

            if success {
                bytes_moved += size;
                succeeded_blocks.push(*bid);
                // Update block metadata: move it to dst_node.
                if let Some(meta) = self.blocks.get_mut(bid) {
                    meta.src_node = dst_node.clone();
                    meta.dst_node = None;
                }
            } else {
                failed_blocks.push((*bid, error_msg.clone().unwrap_or_default()));
            }

            let record = BmpMigrationRecord {
                ts: now_ts(),
                plan_id,
                block_id: *bid,
                bytes_moved: if success { size } else { 0 },
                success,
                error: error_msg,
            };
            self.append_log(record);
        }

        let final_status = if failed_blocks.is_empty() {
            BmpPlanStatus::Completed
        } else {
            BmpPlanStatus::Failed
        };

        if let Some(plan) = self.plans.get_mut(&plan_id) {
            plan.status = final_status;
        }

        Ok(BmpExecutionResult {
            plan_id,
            status: final_status,
            succeeded_blocks,
            failed_blocks,
            bytes_moved,
        })
    }

    // ── Scheduling ────────────────────────────────────────────────────────────

    /// Automatically select blocks that need migration and create plans for them.
    ///
    /// "Needing migration" means the block has a `dst_node` already set, or
    /// is unpinned and in a non-zero tier (tier > 0 → candidate for promotion).
    /// Up to `max_plans` plans are created, each containing a single block,
    /// ordered according to `policy`.
    pub fn schedule_migrations(
        &mut self,
        policy: BmpPriorityPolicy,
        max_plans: usize,
    ) -> Result<Vec<BmpPlanId>, BmpError> {
        if max_plans == 0 {
            return Ok(Vec::new());
        }

        // Collect candidate block ids.
        let mut candidates: Vec<BmpBlockId> = self
            .blocks
            .values()
            .filter(|m| !m.is_pinned && m.dst_node.is_some())
            .map(|m| m.id)
            .collect();

        if candidates.is_empty() {
            // Fall back: pick unpinned blocks on tier > 0.
            candidates = self
                .blocks
                .values()
                .filter(|m| !m.is_pinned && m.tier > 0)
                .map(|m| m.id)
                .collect();
        }

        if candidates.is_empty() {
            return Err(BmpError::NoEligibleBlocks);
        }

        // Sort by policy.
        self.sort_candidates_by_policy(&mut candidates, policy);

        candidates.truncate(max_plans);

        let mut plan_ids = Vec::with_capacity(candidates.len());
        for bid in candidates {
            let dst = self
                .blocks
                .get(&bid)
                .and_then(|m| m.dst_node.clone())
                .unwrap_or_else(|| "node-0".to_string());

            match self.create_plan(vec![bid], dst, 0) {
                Ok(pid) => plan_ids.push(pid),
                Err(_) => continue,
            }
        }
        Ok(plan_ids)
    }

    /// Sort a candidate list in-place according to `policy`.
    fn sort_candidates_by_policy(&self, ids: &mut [BmpBlockId], policy: BmpPriorityPolicy) {
        match policy {
            BmpPriorityPolicy::FifoQueue => {
                // Sort by `last_accessed` ascending (oldest first).
                ids.sort_by(|a, b| {
                    let ta = self.blocks.get(a).map_or(0, |m| m.last_accessed);
                    let tb = self.blocks.get(b).map_or(0, |m| m.last_accessed);
                    ta.cmp(&tb)
                });
            }
            BmpPriorityPolicy::HighFrequencyFirst => {
                ids.sort_by(|a, b| {
                    let fa = self.blocks.get(a).map_or(0.0, |m| m.access_frequency);
                    let fb = self.blocks.get(b).map_or(0.0, |m| m.access_frequency);
                    fb.partial_cmp(&fa).unwrap_or(std::cmp::Ordering::Equal)
                });
            }
            BmpPriorityPolicy::LargestFirst => {
                ids.sort_by(|a, b| {
                    let sa = self.blocks.get(a).map_or(0, |m| m.size_bytes);
                    let sb = self.blocks.get(b).map_or(0, |m| m.size_bytes);
                    sb.cmp(&sa)
                });
            }
            BmpPriorityPolicy::SmallestFirst => {
                ids.sort_by(|a, b| {
                    let sa = self.blocks.get(a).map_or(0, |m| m.size_bytes);
                    let sb = self.blocks.get(b).map_or(0, |m| m.size_bytes);
                    sa.cmp(&sb)
                });
            }
            BmpPriorityPolicy::CostOptimized => {
                // Proxy cost = size_bytes * tier (lower = cheaper to migrate).
                ids.sort_by(|a, b| {
                    let ca = self
                        .blocks
                        .get(a)
                        .map_or(0, |m| m.size_bytes.saturating_mul(m.tier as u64));
                    let cb = self
                        .blocks
                        .get(b)
                        .map_or(0, |m| m.size_bytes.saturating_mul(m.tier as u64));
                    ca.cmp(&cb)
                });
            }
        }
    }

    /// Create up to `batch_size` plans using `policy` and execute them all,
    /// respecting `max_concurrent_migrations`.
    pub fn run_batch_migration(
        &mut self,
        policy: BmpPriorityPolicy,
        batch_size: usize,
    ) -> BmpBatchResult {
        let max = self.config.max_concurrent_migrations.min(batch_size);

        let plan_ids = match self.schedule_migrations(policy, max) {
            Ok(ids) => ids,
            Err(_) => return BmpBatchResult::default(),
        };

        let mut batch = BmpBatchResult {
            plans_executed: plan_ids.len(),
            ..BmpBatchResult::default()
        };

        for pid in plan_ids {
            match self.execute_plan(pid) {
                Ok(result) => {
                    batch.total_bytes_moved += result.bytes_moved;
                    if result.status == BmpPlanStatus::Completed {
                        batch.plans_completed += 1;
                    } else {
                        batch.plans_failed += 1;
                    }
                    batch.results.push(result);
                }
                Err(_) => {
                    batch.plans_failed += 1;
                }
            }
        }
        batch
    }

    // ── Defragmentation ───────────────────────────────────────────────────────

    /// Generate plans to balance blocks evenly across the supplied nodes.
    ///
    /// Blocks that are already on the least-loaded node (or are pinned) are
    /// skipped.  The target is `total_blocks / nodes.len()` per node.
    /// Returns `Vec<BmpPlanId>` of all newly created plans.
    pub fn defragment_plan(&mut self, nodes: &[String]) -> Vec<BmpPlanId> {
        if nodes.is_empty() {
            return Vec::new();
        }

        // Count blocks per node.
        let mut node_counts: HashMap<String, usize> =
            nodes.iter().map(|n| (n.clone(), 0)).collect();
        for meta in self.blocks.values() {
            if let Some(cnt) = node_counts.get_mut(&meta.src_node) {
                *cnt += 1;
            }
        }

        let total: usize = node_counts.values().sum();
        let target = total.div_ceil(nodes.len());

        // Identify over-full and under-full nodes.
        let mut over_nodes: Vec<String> = node_counts
            .iter()
            .filter(|(_, &cnt)| cnt > target)
            .map(|(n, _)| n.clone())
            .collect();
        let mut under_nodes: Vec<String> = node_counts
            .iter()
            .filter(|(_, &cnt)| cnt < target)
            .map(|(n, _)| n.clone())
            .collect();

        if over_nodes.is_empty() || under_nodes.is_empty() {
            return Vec::new();
        }

        // Sort deterministically so behaviour is reproducible.
        over_nodes.sort();
        under_nodes.sort();

        // Build a list of (block_id, src_node, intended_dst_node) moves.
        struct Move {
            bid: BmpBlockId,
            dst: String,
        }
        let mut moves: Vec<Move> = Vec::new();

        // Remaining capacity on each under-loaded node.
        let mut capacity: HashMap<String, usize> = under_nodes
            .iter()
            .map(|n| {
                let cnt = node_counts.get(n).copied().unwrap_or(0);
                (n.clone(), target.saturating_sub(cnt))
            })
            .collect();

        for over in &over_nodes {
            let excess = node_counts
                .get(over)
                .map_or(0, |&cnt| cnt.saturating_sub(target));
            if excess == 0 {
                continue;
            }
            // Collect movable blocks from this node.
            let candidates: Vec<BmpBlockId> = self
                .blocks
                .values()
                .filter(|m| &m.src_node == over && !m.is_pinned && m.dst_node.is_none())
                .map(|m| m.id)
                .collect();

            let mut moved = 0usize;
            'outer: for bid in candidates {
                if moved >= excess {
                    break;
                }
                // Find an under node with remaining capacity.
                for under in &under_nodes {
                    let cap = capacity.get_mut(under);
                    if let Some(c) = cap {
                        if *c > 0 {
                            *c -= 1;
                            moves.push(Move {
                                bid,
                                dst: under.clone(),
                            });
                            moved += 1;
                            continue 'outer;
                        }
                    }
                }
                break; // No space on any under node.
            }
        }

        // Create one plan per move.
        let mut plan_ids = Vec::with_capacity(moves.len());
        for m in moves {
            // Tag the block with its intended dst so create_plan does not reject it.
            if let Some(meta) = self.blocks.get_mut(&m.bid) {
                meta.dst_node = Some(m.dst.clone());
            }
            match self.create_plan(vec![m.bid], m.dst, 0) {
                Ok(pid) => plan_ids.push(pid),
                Err(_) => continue,
            }
        }
        plan_ids
    }

    // ── Statistics ────────────────────────────────────────────────────────────

    /// Compute and return aggregate statistics.
    pub fn migration_stats(&self) -> BmpPlannerStats {
        let pinned_blocks = self.blocks.values().filter(|m| m.is_pinned).count();

        let mut stats = BmpPlannerStats {
            total_blocks: self.blocks.len(),
            pinned_blocks,
            total_plans: self.plans.len(),
            total_records: self.log.len(),
            ..Default::default()
        };

        for plan in self.plans.values() {
            match plan.status {
                BmpPlanStatus::Pending => stats.pending_plans += 1,
                BmpPlanStatus::InProgress => stats.in_progress_plans += 1,
                BmpPlanStatus::Completed => stats.completed_plans += 1,
                BmpPlanStatus::Failed => stats.failed_plans += 1,
                BmpPlanStatus::Cancelled => stats.cancelled_plans += 1,
            }
        }

        for rec in &self.log {
            if rec.success {
                stats.total_bytes_moved += rec.bytes_moved;
            } else {
                stats.total_failures += 1;
            }
        }

        stats
    }

    /// Return the number of plans in the planner.
    pub fn plan_count(&self) -> usize {
        self.plans.len()
    }

    /// Return a reference to a plan, or `None`.
    pub fn plan(&self, plan_id: BmpPlanId) -> Option<&BmpMigrationPlan> {
        self.plans.get(&plan_id)
    }

    /// Iterate over all plans.
    pub fn plans_iter(&self) -> impl Iterator<Item = &BmpMigrationPlan> {
        self.plans.values()
    }

    /// Return the most recent `n` log records.
    pub fn recent_records(&self, n: usize) -> Vec<&BmpMigrationRecord> {
        self.log.iter().rev().take(n).collect()
    }

    /// Total number of records in the execution log.
    pub fn log_len(&self) -> usize {
        self.log.len()
    }

    // ── Internal helpers ──────────────────────────────────────────────────────

    /// Append a record to the execution log, evicting the oldest when full.
    fn append_log(&mut self, record: BmpMigrationRecord) {
        const MAX_LOG: usize = 1000;
        if self.log.len() >= MAX_LOG {
            self.log.pop_front();
        }
        self.log.push_back(record);
    }
}

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

// ─────────────────────────────────────────────────────────────────────────────
// Additional helper: make a BmpBlockId from a byte slice (pads / truncates).
// ─────────────────────────────────────────────────────────────────────────────

/// Build a `BmpBlockId` deterministically from arbitrary bytes using FNV-1a.
///
/// The 32-byte array is filled by splitting the 64-bit FNV hash into 4× u64
/// words (each XOR'd with a rotation of the previous word for diffusion).
pub fn bmp_id_from_bytes(data: &[u8]) -> BmpBlockId {
    let mut id = [0u8; 32];
    let h0 = fnv1a_64(data);
    let h1 = fnv1a_64(&h0.to_le_bytes());
    let h2 = fnv1a_64(&h1.to_le_bytes());
    let h3 = fnv1a_64(&h2.to_le_bytes());
    id[0..8].copy_from_slice(&h0.to_le_bytes());
    id[8..16].copy_from_slice(&h1.to_le_bytes());
    id[16..24].copy_from_slice(&h2.to_le_bytes());
    id[24..32].copy_from_slice(&h3.to_le_bytes());
    id
}

// ─────────────────────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────────────────────

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

    // ── Helpers ───────────────────────────────────────────────────────────────

    fn make_id(seed: u8) -> BmpBlockId {
        bmp_id_from_bytes(&[seed; 16])
    }

    fn make_planner() -> BlockMigrationPlanner {
        BlockMigrationPlanner::new()
    }

    fn register_n(planner: &mut BlockMigrationPlanner, n: usize, node: &str) -> Vec<BmpBlockId> {
        let mut ids = Vec::with_capacity(n);
        for i in 0..n {
            let id = bmp_id_from_bytes(format!("block-{}", i).as_bytes());
            planner.register_block(id, (i as u64 + 1) * 512, node, 1, 1.0);
            ids.push(id);
        }
        ids
    }

    // ── xorshift64 / fnv1a_64 ─────────────────────────────────────────────────

    #[test]
    fn test_xorshift64_nonzero() {
        let mut state: u64 = 12345678;
        for _ in 0..100 {
            let v = xorshift64(&mut state);
            assert_ne!(v, 0);
        }
    }

    #[test]
    fn test_xorshift64_changes_state() {
        let mut state: u64 = 1;
        let a = xorshift64(&mut state);
        let b = xorshift64(&mut state);
        assert_ne!(a, b);
    }

    #[test]
    fn test_fnv1a_empty() {
        let h = fnv1a_64(&[]);
        // FNV offset basis
        assert_eq!(h, 14_695_981_039_346_656_037_u64);
    }

    #[test]
    fn test_fnv1a_deterministic() {
        assert_eq!(fnv1a_64(b"hello"), fnv1a_64(b"hello"));
    }

    #[test]
    fn test_fnv1a_differs_on_different_input() {
        assert_ne!(fnv1a_64(b"foo"), fnv1a_64(b"bar"));
    }

    // ── bmp_id_from_bytes ─────────────────────────────────────────────────────

    #[test]
    fn test_bmp_id_from_bytes_length() {
        let id = bmp_id_from_bytes(b"test");
        assert_eq!(id.len(), 32);
    }

    #[test]
    fn test_bmp_id_from_bytes_deterministic() {
        assert_eq!(bmp_id_from_bytes(b"abc"), bmp_id_from_bytes(b"abc"));
    }

    #[test]
    fn test_bmp_id_from_bytes_differs() {
        assert_ne!(bmp_id_from_bytes(b"a"), bmp_id_from_bytes(b"b"));
    }

    // ── BlockMigrationPlanner construction ────────────────────────────────────

    #[test]
    fn test_new_planner_empty() {
        let p = make_planner();
        assert_eq!(p.block_count(), 0);
        assert_eq!(p.plan_count(), 0);
        assert_eq!(p.log_len(), 0);
    }

    #[test]
    fn test_with_config_dry_run() {
        let cfg = BmpPlannerConfig {
            dry_run: true,
            ..Default::default()
        };
        let p = BlockMigrationPlanner::with_config(cfg);
        assert!(p.config().dry_run);
    }

    #[test]
    fn test_set_config() {
        let mut p = make_planner();
        let cfg = BmpPlannerConfig {
            bandwidth_limit_kbps: 1024,
            ..Default::default()
        };
        p.set_config(cfg);
        assert_eq!(p.config().bandwidth_limit_kbps, 1024);
    }

    // ── register_block ────────────────────────────────────────────────────────

    #[test]
    fn test_register_block_increases_count() {
        let mut p = make_planner();
        let id = make_id(1);
        p.register_block(id, 1024, "node-0", 0, 1.0);
        assert_eq!(p.block_count(), 1);
    }

    #[test]
    fn test_register_block_metadata() {
        let mut p = make_planner();
        let id = make_id(2);
        p.register_block(id, 2048, "node-1", 2, 3.5);
        let meta = p.block_meta(&id).expect("block should exist");
        assert_eq!(meta.size_bytes, 2048);
        assert_eq!(meta.src_node, "node-1");
        assert_eq!(meta.tier, 2);
        assert!((meta.access_frequency - 3.5).abs() < 1e-9);
        assert!(!meta.is_pinned);
    }

    #[test]
    fn test_register_block_overwrite() {
        let mut p = make_planner();
        let id = make_id(3);
        p.register_block(id, 100, "node-a", 0, 0.5);
        p.register_block(id, 200, "node-b", 1, 2.0);
        assert_eq!(p.block_count(), 1); // still one block
        let meta = p.block_meta(&id).expect("exists");
        assert_eq!(meta.size_bytes, 200);
        assert_eq!(meta.src_node, "node-b");
    }

    #[test]
    fn test_register_multiple_blocks() {
        let mut p = make_planner();
        for i in 0..10u8 {
            p.register_block(make_id(i), 512, "node-0", 0, 1.0);
        }
        assert_eq!(p.block_count(), 10);
    }

    // ── update_access ─────────────────────────────────────────────────────────

    #[test]
    fn test_update_access_ok() {
        let mut p = make_planner();
        let id = make_id(10);
        p.register_block(id, 512, "node-0", 0, 1.0);
        let old_freq = p.block_meta(&id).map_or(0.0, |m| m.access_frequency);
        p.update_access(&id).expect("ok");
        let new_freq = p.block_meta(&id).map_or(0.0, |m| m.access_frequency);
        // EMA means new_freq = old * 0.9 + 0.1; should differ from initial 1.0
        assert!(new_freq > 0.0);
        let _ = old_freq; // suppress unused warning
    }

    #[test]
    fn test_update_access_unknown_block_errors() {
        let mut p = make_planner();
        let id = make_id(11);
        assert!(p.update_access(&id).is_err());
    }

    #[test]
    fn test_update_access_bumps_last_accessed() {
        let mut p = make_planner();
        let id = make_id(12);
        p.register_block(id, 512, "node-0", 0, 1.0);
        let t0 = p.block_meta(&id).map_or(0, |m| m.last_accessed);
        std::thread::sleep(std::time::Duration::from_millis(1));
        p.update_access(&id).expect("ok");
        let t1 = p.block_meta(&id).map_or(0, |m| m.last_accessed);
        assert!(t1 >= t0);
    }

    // ── pin / unpin ───────────────────────────────────────────────────────────

    #[test]
    fn test_pin_block() {
        let mut p = make_planner();
        let id = make_id(20);
        p.register_block(id, 512, "node-0", 0, 1.0);
        p.pin(&id).expect("ok");
        assert!(p.block_meta(&id).is_some_and(|m| m.is_pinned));
    }

    #[test]
    fn test_unpin_block() {
        let mut p = make_planner();
        let id = make_id(21);
        p.register_block(id, 512, "node-0", 0, 1.0);
        p.pin(&id).expect("ok");
        p.unpin(&id).expect("ok");
        assert!(!p.block_meta(&id).is_none_or(|m| m.is_pinned));
    }

    #[test]
    fn test_pin_unknown_block_errors() {
        let mut p = make_planner();
        assert!(p.pin(&make_id(22)).is_err());
    }

    #[test]
    fn test_unpin_unknown_block_errors() {
        let mut p = make_planner();
        assert!(p.unpin(&make_id(23)).is_err());
    }

    // ── create_plan ───────────────────────────────────────────────────────────

    #[test]
    fn test_create_plan_basic() {
        let mut p = make_planner();
        let id = make_id(30);
        p.register_block(id, 1024, "node-0", 0, 1.0);
        let pid = p.create_plan(vec![id], "node-1", 5).expect("ok");
        assert_eq!(pid, 1);
        let plan = p.plan(pid).expect("plan exists");
        assert_eq!(plan.status, BmpPlanStatus::Pending);
        assert_eq!(plan.estimated_bytes, 1024);
        assert_eq!(plan.dst_node, "node-1");
        assert_eq!(plan.priority, 5);
    }

    #[test]
    fn test_create_plan_empty_blocks_errors() {
        let mut p = make_planner();
        assert!(p.create_plan(vec![], "node-1", 0).is_err());
    }

    #[test]
    fn test_create_plan_empty_dst_errors() {
        let mut p = make_planner();
        let id = make_id(31);
        p.register_block(id, 512, "node-0", 0, 1.0);
        assert!(p.create_plan(vec![id], "", 0).is_err());
    }

    #[test]
    fn test_create_plan_unknown_block_errors() {
        let mut p = make_planner();
        let id = make_id(32);
        assert!(p.create_plan(vec![id], "node-1", 0).is_err());
    }

    #[test]
    fn test_create_plan_pinned_block_errors() {
        let mut p = make_planner();
        let id = make_id(33);
        p.register_block(id, 512, "node-0", 0, 1.0);
        p.pin(&id).expect("ok");
        assert!(p.create_plan(vec![id], "node-1", 0).is_err());
    }

    #[test]
    fn test_create_plan_increments_id() {
        let mut p = make_planner();
        for i in 0..5u8 {
            let id = make_id(40 + i);
            p.register_block(id, 512, "node-0", 0, 1.0);
            let pid = p.create_plan(vec![id], "node-1", 0).expect("ok");
            assert_eq!(pid, i as u64 + 1);
        }
    }

    #[test]
    fn test_create_plan_multi_block_bytes() {
        let mut p = make_planner();
        let ids: Vec<_> = (0..3u8)
            .map(|i| {
                let id = make_id(50 + i);
                p.register_block(id, 1000, "node-0", 0, 1.0);
                id
            })
            .collect();
        let pid = p.create_plan(ids, "node-1", 0).expect("ok");
        let plan = p.plan(pid).expect("plan");
        assert_eq!(plan.estimated_bytes, 3000);
    }

    // ── cancel_plan ───────────────────────────────────────────────────────────

    #[test]
    fn test_cancel_pending_plan() {
        let mut p = make_planner();
        let id = make_id(60);
        p.register_block(id, 512, "node-0", 0, 1.0);
        let pid = p.create_plan(vec![id], "node-1", 0).expect("ok");
        p.cancel_plan(pid).expect("ok");
        assert_eq!(
            p.plan(pid).map(|pl| pl.status),
            Some(BmpPlanStatus::Cancelled)
        );
    }

    #[test]
    fn test_cancel_nonexistent_plan_errors() {
        let mut p = make_planner();
        assert!(p.cancel_plan(999).is_err());
    }

    #[test]
    fn test_cancel_already_cancelled_errors() {
        let mut p = make_planner();
        let id = make_id(61);
        p.register_block(id, 512, "node-0", 0, 1.0);
        let pid = p.create_plan(vec![id], "node-1", 0).expect("ok");
        p.cancel_plan(pid).expect("ok");
        assert!(p.cancel_plan(pid).is_err());
    }

    // ── execute_plan ──────────────────────────────────────────────────────────

    #[test]
    fn test_execute_plan_dry_run_always_succeeds() {
        let cfg = BmpPlannerConfig {
            dry_run: true,
            ..Default::default()
        };
        let mut p = BlockMigrationPlanner::with_config(cfg);
        let id = make_id(70);
        p.register_block(id, 512, "node-0", 0, 1.0);
        let pid = p.create_plan(vec![id], "node-1", 0).expect("ok");
        let result = p.execute_plan(pid).expect("ok");
        assert_eq!(result.status, BmpPlanStatus::Completed);
        assert_eq!(result.succeeded_blocks.len(), 1);
        assert!(result.failed_blocks.is_empty());
        assert_eq!(result.bytes_moved, 512);
    }

    #[test]
    fn test_execute_plan_updates_log() {
        let cfg = BmpPlannerConfig {
            dry_run: true,
            ..Default::default()
        };
        let mut p = BlockMigrationPlanner::with_config(cfg);
        let id = make_id(71);
        p.register_block(id, 256, "node-0", 0, 1.0);
        let pid = p.create_plan(vec![id], "node-1", 0).expect("ok");
        p.execute_plan(pid).expect("ok");
        assert_eq!(p.log_len(), 1);
    }

    #[test]
    fn test_execute_plan_updates_block_src_node() {
        let cfg = BmpPlannerConfig {
            dry_run: true,
            ..Default::default()
        };
        let mut p = BlockMigrationPlanner::with_config(cfg);
        let id = make_id(72);
        p.register_block(id, 512, "node-0", 0, 1.0);
        let pid = p.create_plan(vec![id], "node-2", 0).expect("ok");
        p.execute_plan(pid).expect("ok");
        let meta = p.block_meta(&id).expect("exists");
        assert_eq!(meta.src_node, "node-2");
    }

    #[test]
    fn test_execute_plan_not_found_errors() {
        let mut p = make_planner();
        assert!(p.execute_plan(42).is_err());
    }

    #[test]
    fn test_execute_plan_not_pending_errors() {
        let cfg = BmpPlannerConfig {
            dry_run: true,
            ..Default::default()
        };
        let mut p = BlockMigrationPlanner::with_config(cfg);
        let id = make_id(73);
        p.register_block(id, 512, "node-0", 0, 1.0);
        let pid = p.create_plan(vec![id], "node-1", 0).expect("ok");
        p.execute_plan(pid).expect("ok");
        // Already completed — cannot execute again.
        assert!(p.execute_plan(pid).is_err());
    }

    #[test]
    fn test_execute_plan_multi_block_dry_run() {
        let cfg = BmpPlannerConfig {
            dry_run: true,
            ..Default::default()
        };
        let mut p = BlockMigrationPlanner::with_config(cfg);
        let ids: Vec<_> = (0..5u8)
            .map(|i| {
                let id = make_id(80 + i);
                p.register_block(id, 100, "node-0", 0, 1.0);
                id
            })
            .collect();
        let pid = p.create_plan(ids.clone(), "node-1", 0).expect("ok");
        let res = p.execute_plan(pid).expect("ok");
        assert_eq!(res.succeeded_blocks.len(), 5);
        assert_eq!(res.bytes_moved, 500);
    }

    // ── schedule_migrations ───────────────────────────────────────────────────

    #[test]
    fn test_schedule_migrations_no_eligible_blocks_errors() {
        let mut p = make_planner();
        // Register tier-0 blocks with no dst_node → not eligible.
        let id = make_id(90);
        p.register_block(id, 512, "node-0", 0, 1.0);
        let result = p.schedule_migrations(BmpPriorityPolicy::FifoQueue, 5);
        assert!(result.is_err());
    }

    #[test]
    fn test_schedule_migrations_with_dst_node_set() {
        let mut p = make_planner();
        let id = make_id(91);
        p.register_block(id, 512, "node-0", 1, 1.0);
        // Set dst_node directly.
        if let Some(meta) = p.blocks.get_mut(&id) {
            meta.dst_node = Some("node-1".to_string());
        }
        let ids = p
            .schedule_migrations(BmpPriorityPolicy::FifoQueue, 5)
            .expect("ok");
        assert_eq!(ids.len(), 1);
    }

    #[test]
    fn test_schedule_migrations_respects_max_plans() {
        let mut p = make_planner();
        for i in 0..10u8 {
            let id = make_id(100 + i);
            p.register_block(id, 512, "node-0", 1, 1.0);
            if let Some(meta) = p.blocks.get_mut(&id) {
                meta.dst_node = Some("node-1".to_string());
            }
        }
        let ids = p
            .schedule_migrations(BmpPriorityPolicy::FifoQueue, 3)
            .expect("ok");
        assert_eq!(ids.len(), 3);
    }

    #[test]
    fn test_schedule_migrations_skips_pinned() {
        let mut p = make_planner();
        for i in 0..3u8 {
            let id = make_id(110 + i);
            p.register_block(id, 512, "node-0", 1, 1.0);
            if let Some(meta) = p.blocks.get_mut(&id) {
                meta.dst_node = Some("node-1".to_string());
            }
            if i == 0 {
                p.pin(&id).expect("ok");
            }
        }
        let ids = p
            .schedule_migrations(BmpPriorityPolicy::FifoQueue, 10)
            .expect("ok");
        // Only 2 unpinned blocks should become plans.
        assert_eq!(ids.len(), 2);
    }

    #[test]
    fn test_schedule_migrations_zero_max_returns_empty() {
        let mut p = make_planner();
        let id = make_id(120);
        p.register_block(id, 512, "node-0", 1, 1.0);
        if let Some(meta) = p.blocks.get_mut(&id) {
            meta.dst_node = Some("node-1".to_string());
        }
        let ids = p
            .schedule_migrations(BmpPriorityPolicy::FifoQueue, 0)
            .expect("ok");
        assert!(ids.is_empty());
    }

    #[test]
    fn test_schedule_largest_first_ordering() {
        let mut p = make_planner();
        let sizes = [100u64, 500, 200, 800, 50];
        let mut block_ids = Vec::new();
        for (i, &sz) in sizes.iter().enumerate() {
            let id = make_id(130 + i as u8);
            p.register_block(id, sz, "node-0", 1, 1.0);
            if let Some(meta) = p.blocks.get_mut(&id) {
                meta.dst_node = Some("node-1".to_string());
            }
            block_ids.push(id);
        }
        let pids = p
            .schedule_migrations(BmpPriorityPolicy::LargestFirst, 5)
            .expect("ok");
        // Collect estimated bytes for the first plan to verify ordering.
        let first_bytes = p.plan(pids[0]).map_or(0, |pl| pl.estimated_bytes);
        let last_bytes = p
            .plan(*pids.last().unwrap())
            .map_or(0, |pl| pl.estimated_bytes);
        assert!(
            first_bytes >= last_bytes,
            "largest first violated: {} < {}",
            first_bytes,
            last_bytes
        );
    }

    #[test]
    fn test_schedule_smallest_first_ordering() {
        let mut p = make_planner();
        let sizes = [100u64, 500, 200, 800, 50];
        for (i, &sz) in sizes.iter().enumerate() {
            let id = make_id(140 + i as u8);
            p.register_block(id, sz, "node-0", 1, 1.0);
            if let Some(meta) = p.blocks.get_mut(&id) {
                meta.dst_node = Some("node-1".to_string());
            }
        }
        let pids = p
            .schedule_migrations(BmpPriorityPolicy::SmallestFirst, 5)
            .expect("ok");
        let first_bytes = p.plan(pids[0]).map_or(0, |pl| pl.estimated_bytes);
        let last_bytes = p
            .plan(*pids.last().unwrap())
            .map_or(0, |pl| pl.estimated_bytes);
        assert!(
            first_bytes <= last_bytes,
            "smallest first violated: {} > {}",
            first_bytes,
            last_bytes
        );
    }

    #[test]
    fn test_schedule_high_frequency_first() {
        let mut p = make_planner();
        let freqs = [0.1f64, 5.0, 2.0];
        let mut bid_map: Vec<(BmpBlockId, f64)> = Vec::new();
        for (i, &f) in freqs.iter().enumerate() {
            let id = make_id(150 + i as u8);
            p.register_block(id, 512, "node-0", 1, f);
            if let Some(meta) = p.blocks.get_mut(&id) {
                meta.dst_node = Some("node-1".to_string());
            }
            bid_map.push((id, f));
        }
        let pids = p
            .schedule_migrations(BmpPriorityPolicy::HighFrequencyFirst, 3)
            .expect("ok");
        // The first plan should contain the block with the highest frequency (5.0).
        let first_plan_bid = p.plan(pids[0]).and_then(|pl| pl.block_ids.first().copied());
        if let Some(bid) = first_plan_bid {
            let freq = p.block_meta(&bid).map_or(0.0, |m| m.access_frequency);
            // Highest frequency among scheduled should be 5.0.
            assert!(
                (freq - 5.0).abs() < 1e-6 || freq >= 2.0,
                "unexpected freq {}",
                freq
            );
        }
    }

    // ── run_batch_migration ───────────────────────────────────────────────────

    #[test]
    fn test_run_batch_migration_dry_run() {
        let cfg = BmpPlannerConfig {
            dry_run: true,
            max_concurrent_migrations: 10,
            ..Default::default()
        };
        let mut p = BlockMigrationPlanner::with_config(cfg);
        let _ids = register_n(&mut p, 5, "node-0");
        for id in &_ids {
            if let Some(meta) = p.blocks.get_mut(id) {
                meta.dst_node = Some("node-1".to_string());
                meta.tier = 1;
            }
        }
        let batch = p.run_batch_migration(BmpPriorityPolicy::FifoQueue, 5);
        assert!(batch.plans_executed > 0);
        assert_eq!(batch.plans_failed, 0);
    }

    #[test]
    fn test_run_batch_migration_empty_result_on_no_candidates() {
        let mut p = make_planner();
        // Register only tier-0 blocks (no dst_node).
        let id = make_id(160);
        p.register_block(id, 512, "node-0", 0, 1.0);
        let batch = p.run_batch_migration(BmpPriorityPolicy::FifoQueue, 5);
        assert_eq!(batch.plans_executed, 0);
    }

    #[test]
    fn test_run_batch_migration_respects_max_concurrent() {
        let cfg = BmpPlannerConfig {
            dry_run: true,
            max_concurrent_migrations: 2,
            ..Default::default()
        };
        let mut p = BlockMigrationPlanner::with_config(cfg);
        let _ids = register_n(&mut p, 10, "node-0");
        for id in &_ids {
            if let Some(meta) = p.blocks.get_mut(id) {
                meta.dst_node = Some("node-1".to_string());
                meta.tier = 1;
            }
        }
        let batch = p.run_batch_migration(BmpPriorityPolicy::FifoQueue, 10);
        assert!(batch.plans_executed <= 2);
    }

    #[test]
    fn test_run_batch_total_bytes_moved() {
        let cfg = BmpPlannerConfig {
            dry_run: true,
            max_concurrent_migrations: 5,
            ..Default::default()
        };
        let mut p = BlockMigrationPlanner::with_config(cfg);
        for i in 0..5u8 {
            let id = make_id(170 + i);
            p.register_block(id, 1000, "node-0", 1, 1.0);
            if let Some(meta) = p.blocks.get_mut(&id) {
                meta.dst_node = Some("node-1".to_string());
            }
        }
        let batch = p.run_batch_migration(BmpPriorityPolicy::FifoQueue, 5);
        assert_eq!(
            batch.total_bytes_moved,
            (batch.plans_completed as u64) * 1000
        );
    }

    // ── defragment_plan ───────────────────────────────────────────────────────

    #[test]
    fn test_defragment_plan_empty_nodes() {
        let mut p = make_planner();
        let result = p.defragment_plan(&[]);
        assert!(result.is_empty());
    }

    #[test]
    fn test_defragment_plan_already_balanced() {
        let mut p = make_planner();
        let nodes = vec!["node-0".to_string(), "node-1".to_string()];
        for i in 0..4u8 {
            let node = if i < 2 { "node-0" } else { "node-1" };
            let id = make_id(180 + i);
            p.register_block(id, 512, node, 0, 1.0);
        }
        let plans = p.defragment_plan(&nodes);
        // Already balanced (2 each) — no plans needed.
        assert!(plans.is_empty());
    }

    #[test]
    fn test_defragment_plan_imbalanced() {
        let mut p = make_planner();
        let nodes = vec!["node-0".to_string(), "node-1".to_string()];
        // 4 blocks on node-0, 0 on node-1.
        for i in 0..4u8 {
            let id = make_id(190 + i);
            p.register_block(id, 512, "node-0", 0, 1.0);
        }
        let plans = p.defragment_plan(&nodes);
        assert!(!plans.is_empty());
    }

    #[test]
    fn test_defragment_plan_skips_pinned() {
        let mut p = make_planner();
        let nodes = vec!["node-0".to_string(), "node-1".to_string()];
        for i in 0..4u8 {
            let id = make_id(200 + i);
            p.register_block(id, 512, "node-0", 0, 1.0);
            if i < 2 {
                p.pin(&id).expect("ok");
            }
        }
        let plans = p.defragment_plan(&nodes);
        // Only 2 movable blocks; moving up to 2 plans.
        assert!(plans.len() <= 2);
    }

    #[test]
    fn test_defragment_plan_single_node_no_plans() {
        let mut p = make_planner();
        let nodes = vec!["node-0".to_string()];
        for i in 0..3u8 {
            let id = make_id(210 + i);
            p.register_block(id, 512, "node-0", 0, 1.0);
        }
        // With a single node, over == under logic finds neither.
        let plans = p.defragment_plan(&nodes);
        assert!(plans.is_empty());
    }

    // ── migration_stats ───────────────────────────────────────────────────────

    #[test]
    fn test_migration_stats_empty() {
        let p = make_planner();
        let stats = p.migration_stats();
        assert_eq!(stats.total_blocks, 0);
        assert_eq!(stats.total_plans, 0);
        assert_eq!(stats.total_records, 0);
        assert_eq!(stats.total_bytes_moved, 0);
    }

    #[test]
    fn test_migration_stats_after_register() {
        let mut p = make_planner();
        for i in 0..5u8 {
            p.register_block(make_id(220 + i), 512, "node-0", 0, 1.0);
        }
        let stats = p.migration_stats();
        assert_eq!(stats.total_blocks, 5);
    }

    #[test]
    fn test_migration_stats_pinned_count() {
        let mut p = make_planner();
        for i in 0..4u8 {
            let id = make_id(230 + i);
            p.register_block(id, 512, "node-0", 0, 1.0);
            if i < 2 {
                p.pin(&id).expect("ok");
            }
        }
        let stats = p.migration_stats();
        assert_eq!(stats.pinned_blocks, 2);
    }

    #[test]
    fn test_migration_stats_plan_counts() {
        let cfg = BmpPlannerConfig {
            dry_run: true,
            ..Default::default()
        };
        let mut p = BlockMigrationPlanner::with_config(cfg);
        for i in 0..3u8 {
            let id = make_id(240 + i);
            p.register_block(id, 512, "node-0", 0, 1.0);
            p.create_plan(vec![id], "node-1", 0).expect("ok");
        }
        let stats = p.migration_stats();
        assert_eq!(stats.total_plans, 3);
        assert_eq!(stats.pending_plans, 3);
    }

    #[test]
    fn test_migration_stats_bytes_moved() {
        let cfg = BmpPlannerConfig {
            dry_run: true,
            ..Default::default()
        };
        let mut p = BlockMigrationPlanner::with_config(cfg);
        let id = make_id(250);
        p.register_block(id, 4096, "node-0", 0, 1.0);
        let pid = p.create_plan(vec![id], "node-1", 0).expect("ok");
        p.execute_plan(pid).expect("ok");
        let stats = p.migration_stats();
        assert_eq!(stats.total_bytes_moved, 4096);
    }

    #[test]
    fn test_migration_stats_cancelled_count() {
        let mut p = make_planner();
        for i in 200..203u8 {
            let id = make_id(i);
            p.register_block(id, 512, "node-0", 0, 1.0);
            let pid = p.create_plan(vec![id], "node-1", 0).expect("ok");
            p.cancel_plan(pid).expect("ok");
        }
        let stats = p.migration_stats();
        assert_eq!(stats.cancelled_plans, 3);
    }

    // ── execution log / recent_records ───────────────────────────────────────

    #[test]
    fn test_log_bounded_at_1000() {
        let cfg = BmpPlannerConfig {
            dry_run: true,
            ..Default::default()
        };
        let mut p = BlockMigrationPlanner::with_config(cfg);
        // Create 1100 single-block plans and execute them all.
        for i in 0..1100u16 {
            let id = bmp_id_from_bytes(&i.to_le_bytes());
            // Re-register (overwrite) to avoid needing unique keys.
            p.register_block(id, 1, "node-0", 0, 1.0);
            if let Ok(pid) = p.create_plan(vec![id], "node-1", 0) {
                let _ = p.execute_plan(pid);
            }
        }
        assert_eq!(p.log_len(), 1000);
    }

    #[test]
    fn test_recent_records_returns_n() {
        let cfg = BmpPlannerConfig {
            dry_run: true,
            ..Default::default()
        };
        let mut p = BlockMigrationPlanner::with_config(cfg);
        for i in 0..10u8 {
            let id = make_id(i);
            p.register_block(id, 512, "node-0", 0, 1.0);
            if let Ok(pid) = p.create_plan(vec![id], "node-1", 0) {
                let _ = p.execute_plan(pid);
            }
        }
        assert_eq!(p.recent_records(5).len(), 5);
    }

    #[test]
    fn test_plans_iter() {
        let mut p = make_planner();
        for i in 0..4u8 {
            let id = make_id(i);
            p.register_block(id, 512, "node-0", 0, 1.0);
            p.create_plan(vec![id], "node-1", 0).expect("ok");
        }
        assert_eq!(p.plans_iter().count(), 4);
    }

    // ── BmpPlanStatus Display ─────────────────────────────────────────────────

    #[test]
    fn test_plan_status_display() {
        assert_eq!(BmpPlanStatus::Pending.to_string(), "Pending");
        assert_eq!(BmpPlanStatus::InProgress.to_string(), "InProgress");
        assert_eq!(BmpPlanStatus::Completed.to_string(), "Completed");
        assert_eq!(BmpPlanStatus::Failed.to_string(), "Failed");
        assert_eq!(BmpPlanStatus::Cancelled.to_string(), "Cancelled");
    }

    // ── BmpPriorityPolicy Display ─────────────────────────────────────────────

    #[test]
    fn test_priority_policy_display() {
        assert_eq!(BmpPriorityPolicy::FifoQueue.to_string(), "FifoQueue");
        assert_eq!(
            BmpPriorityPolicy::HighFrequencyFirst.to_string(),
            "HighFrequencyFirst"
        );
        assert_eq!(BmpPriorityPolicy::LargestFirst.to_string(), "LargestFirst");
        assert_eq!(
            BmpPriorityPolicy::SmallestFirst.to_string(),
            "SmallestFirst"
        );
        assert_eq!(
            BmpPriorityPolicy::CostOptimized.to_string(),
            "CostOptimized"
        );
    }

    // ── Default impls ─────────────────────────────────────────────────────────

    #[test]
    fn test_planner_default() {
        let p = BlockMigrationPlanner::default();
        assert_eq!(p.block_count(), 0);
    }

    #[test]
    fn test_config_default() {
        let cfg = BmpPlannerConfig::default();
        assert_eq!(cfg.max_concurrent_migrations, 4);
        assert_eq!(cfg.bandwidth_limit_kbps, 0);
        assert!(!cfg.dry_run);
    }

    #[test]
    fn test_batch_result_default() {
        let b = BmpBatchResult::default();
        assert_eq!(b.plans_executed, 0);
        assert_eq!(b.total_bytes_moved, 0);
    }

    #[test]
    fn test_planner_stats_default() {
        let s = BmpPlannerStats::default();
        assert_eq!(s.total_blocks, 0);
        assert_eq!(s.total_failures, 0);
    }

    // ── cost-optimized scheduling ─────────────────────────────────────────────

    #[test]
    fn test_schedule_cost_optimized() {
        let mut p = make_planner();
        // tier 3, size 100 => cost 300; tier 1, size 500 => cost 500
        let id_a = make_id(20);
        let id_b = make_id(21);
        p.register_block(id_a, 100, "node-0", 3, 1.0);
        p.register_block(id_b, 500, "node-0", 1, 1.0);
        for id in [id_a, id_b] {
            if let Some(m) = p.blocks.get_mut(&id) {
                m.dst_node = Some("node-1".to_string());
            }
        }
        let pids = p
            .schedule_migrations(BmpPriorityPolicy::CostOptimized, 2)
            .expect("ok");
        // First plan should be the cheaper one (id_a, cost 300).
        let first_plan = p.plan(pids[0]).expect("plan");
        let first_bid = first_plan.block_ids[0];
        let first_meta = p.block_meta(&first_bid).expect("meta");
        let first_cost = first_meta.size_bytes * first_meta.tier as u64;
        // Should be <= the other block's cost.
        assert!(
            first_cost <= 500,
            "cost optimized ordering failed: {} > 500",
            first_cost
        );
    }
}