skippydb 0.2.2

A high-performance verifiable key-value store with SHA256 Merkle trees and optional CUDA GPU acceleration, designed for blockchain state storage.
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
use log::debug;
#[cfg(feature = "cuda")]
use log::error;
use aes_gcm::Aes256Gcm;
use rayon;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use std::{fmt, fs, mem, thread};

use super::twig::{sync_mtree, ActiveBits, TwigMT, NULL_NODE_IN_HIGHER_TREE};
use super::twigfile::{TwigFile, TwigFileWriter};
use super::{proof, twigfile};
use super::{recover, twig};
use crate::def::{
    calc_max_level, ENTRIES_PATH, FIRST_LEVEL_ABOVE_TWIG, LEAF_COUNT_IN_TWIG, MAX_TREE_LEVEL,
    MIN_PRUNE_COUNT, NODE_SHARD_COUNT, TWIG_MASK, TWIG_PATH, TWIG_ROOT_LEVEL, TWIG_SHARD_COUNT,
    TWIG_SHIFT,
};
use crate::entryfile::{Entry, EntryBz};
use crate::entryfile::{EntryFile, EntryFileWriter};
use crate::utils::hasher::{self, Hash32};

/*
             ____TwigRoot___                   Level_12
            /               \
           /                 \
1       leftRoot              activeBitsMTL3   Level_11
2       Level_10        2     activeBitsMTL2
4       Level_9         4     activeBitsMTL1
8       Level_8    8*32bytes  activeBits
16      Level_7
32      Level_6
64      Level_5
128     Level_4
256     Level_3
512     Level_2
1024    Level_1
2048    Level_0
*/

/*         1
     2             3
  4     5       6     7
 8 9   a b     c d   e f
*/

#[derive(Copy, Clone, Eq, Hash, PartialEq)]
pub struct NodePos(u64);

impl NodePos {
    pub fn new(pos: u64) -> NodePos {
        NodePos(pos)
    }
    pub fn pos(level: u64, n: u64) -> NodePos {
        NodePos((level << 56) | n)
    }
    pub fn level(&self) -> u64 {
        self.0 >> 56 // extract the high 8 bits
    }
    pub fn nth(&self) -> u64 {
        (self.0 << 8) >> 8 // extract the low 56 bits
    }
    pub fn as_u64(&self) -> u64 {
        self.0
    }
}

impl fmt::Debug for NodePos {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "NodePos: {:?} {{ level: {}, nth: {} }}",
            self.as_u64(),
            self.level(),
            self.nth()
        )
    }
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct EdgeNode {
    pub pos: NodePos,
    pub value: [u8; 32],
}

/// Maximum level (exclusive) that uses dense Vec-based storage.
/// Levels TWIG_ROOT_LEVEL..DENSE_LEVEL_MAX use Vec<Option<[u8;32]>> for O(1)
/// lookups. Levels DENSE_LEVEL_MAX..MAX_TREE_LEVEL use HashMap for sparse storage.
const DENSE_LEVEL_MAX: usize = 21;

/// Per-shard node storage: dense (Vec-indexed) or sparse (HashMap).
/// Dense shards map `nth / NODE_SHARD_COUNT` → hash via direct indexing.
/// Sparse shards use HashMap<NodePos, hash> for levels with few nodes.
#[derive(Clone)]
pub enum NodeShard {
    Dense(Vec<Option<[u8; 32]>>),
    Sparse(HashMap<NodePos, [u8; 32]>),
}

impl NodeShard {
    fn new_dense() -> Self {
        NodeShard::Dense(Vec::new())
    }

    fn new_sparse() -> Self {
        NodeShard::Sparse(HashMap::new())
    }

    pub fn get(&self, pos: &NodePos) -> Option<&[u8; 32]> {
        match self {
            NodeShard::Dense(v) => {
                let idx = pos.nth() as usize / NODE_SHARD_COUNT;
                v.get(idx).and_then(|opt| opt.as_ref())
            }
            NodeShard::Sparse(m) => m.get(pos),
        }
    }

    pub fn insert(&mut self, pos: NodePos, hash: [u8; 32]) {
        match self {
            NodeShard::Dense(v) => {
                let idx = pos.nth() as usize / NODE_SHARD_COUNT;
                if idx >= v.len() {
                    v.resize(idx + 1, None);
                }
                v[idx] = Some(hash);
            }
            NodeShard::Sparse(m) => {
                m.insert(pos, hash);
            }
        }
    }

    pub fn remove(&mut self, pos: &NodePos) {
        match self {
            NodeShard::Dense(v) => {
                let idx = pos.nth() as usize / NODE_SHARD_COUNT;
                if idx < v.len() {
                    v[idx] = None;
                }
            }
            NodeShard::Sparse(m) => {
                m.remove(pos);
            }
        }
    }

    /// Iterate over all (NodePos, hash) entries in this shard.
    pub fn iter(&self) -> NodeShardIter<'_> {
        match self {
            NodeShard::Dense(v) => NodeShardIter::Dense {
                vec: v,
                idx: 0,
                shard_id: 0, // set by caller via iter_with_shard
                level: 0,    // set by caller via iter_with_shard
            },
            NodeShard::Sparse(m) => NodeShardIter::Sparse(m.iter()),
        }
    }

    /// Iterate with known shard_id and level (needed to reconstruct NodePos
    /// for dense entries).
    pub fn iter_with_context(
        &self,
        shard_id: usize,
        level: usize,
    ) -> NodeShardIter<'_> {
        match self {
            NodeShard::Dense(v) => NodeShardIter::Dense {
                vec: v,
                idx: 0,
                shard_id,
                level,
            },
            NodeShard::Sparse(m) => NodeShardIter::Sparse(m.iter()),
        }
    }
}

/// Iterator over entries in a NodeShard.
pub enum NodeShardIter<'a> {
    Dense {
        vec: &'a Vec<Option<[u8; 32]>>,
        idx: usize,
        shard_id: usize,
        level: usize,
    },
    Sparse(std::collections::hash_map::Iter<'a, NodePos, [u8; 32]>),
}

impl<'a> Iterator for NodeShardIter<'a> {
    type Item = (NodePos, &'a [u8; 32]);

    fn next(&mut self) -> Option<Self::Item> {
        match self {
            NodeShardIter::Dense {
                vec,
                idx,
                shard_id,
                level,
            } => {
                while *idx < vec.len() {
                    let i = *idx;
                    *idx += 1;
                    if let Some(ref hash) = vec[i] {
                        let nth = i * NODE_SHARD_COUNT + *shard_id;
                        let pos = NodePos::pos(*level as u64, nth as u64);
                        return Some((pos, hash));
                    }
                }
                None
            }
            NodeShardIter::Sparse(iter) => {
                iter.next().map(|(pos, hash)| (*pos, hash))
            }
        }
    }
}

#[derive(Clone)]
pub struct UpperTree {
    pub my_shard_id: usize,
    // the nodes in high level tree (higher than twigs)
    // this variable can be recovered from saved edge nodes and activeTwigs
    pub nodes: Vec<Vec<NodeShard>>, //MaxUpperLevel*NodeShardCount maps
    // this variable can be recovered from entry file
    pub active_twig_shards: Vec<HashMap<u64, Box<twig::Twig>>>, //TwigShardCount maps
}

impl UpperTree {
    pub fn empty() -> Self {
        Self {
            my_shard_id: 0,
            nodes: Vec::with_capacity(0),
            active_twig_shards: Vec::with_capacity(0),
        }
    }

    pub fn new(my_shard_id: usize) -> Self {
        let nodes: Vec<Vec<NodeShard>> = (0..MAX_TREE_LEVEL)
            .map(|level| {
                if level < DENSE_LEVEL_MAX {
                    vec![NodeShard::new_dense(); NODE_SHARD_COUNT]
                } else {
                    vec![NodeShard::new_sparse(); NODE_SHARD_COUNT]
                }
            })
            .collect();
        let active_twig_shards = vec![HashMap::<u64, Box<twig::Twig>>::new(); TWIG_SHARD_COUNT];

        Self {
            my_shard_id,
            nodes,
            active_twig_shards,
        }
    }

    pub fn is_empty(&self) -> bool {
        self.nodes.len() == 0
    }

    pub fn add_twigs(&mut self, twig_map: HashMap<u64, Box<twig::Twig>>) {
        for (twig_id, twig) in twig_map {
            let (shard_idx, key) = get_shard_idx_and_key(twig_id);
            self.active_twig_shards[shard_idx].insert(key, twig);
        }
    }

    pub fn get_twig(&mut self, twig_id: u64) -> Option<&mut Box<twig::Twig>> {
        let (shard_idx, key) = get_shard_idx_and_key(twig_id);
        self.active_twig_shards[shard_idx].get_mut(&key)
    }

    pub fn get_twig_root(&self, n: u64) -> Option<&[u8; 32]> {
        let (shard_idx, key) = get_shard_idx_and_key(n);
        let twig_option = self.active_twig_shards[shard_idx].get(&key);
        match twig_option {
            Some(v) => Some(&v.twig_root),
            None => {
                // the twig has been evicted
                let pos = NodePos::pos(TWIG_ROOT_LEVEL as u64, n);
                self.get_node(pos)
            }
        }
    }

    pub fn set_node_copy(&mut self, pos: NodePos, node: &[u8; 32]) {
        let mut n = [0; 32];
        n.copy_from_slice(node);
        // self.nodes[pos.level() as usize][pos.nth() as usize % NODE_SHARD_COUNT].insert(pos, n);
        self.set_node(pos, n);
    }

    pub fn set_node(&mut self, pos: NodePos, node: [u8; 32]) {
        self.nodes[pos.level() as usize][pos.nth() as usize % NODE_SHARD_COUNT]
            .insert(pos, node);
    }

    pub fn get_node(&self, pos: NodePos) -> Option<&[u8; 32]> {
        self.nodes[pos.level() as usize][pos.nth() as usize % NODE_SHARD_COUNT]
            .get(&pos)
    }

    fn delete_node(&mut self, pos: NodePos) {
        self.nodes[pos.level() as usize][pos.nth() as usize % NODE_SHARD_COUNT]
            .remove(&pos);
    }

    pub fn prune_nodes(&mut self, start: u64, end: u64, youngest_twig_id: u64) -> Vec<u8> {
        let max_level = calc_max_level(youngest_twig_id);
        self.remove_useless_nodes(start, end, max_level);
        recover::edge_nodes_to_bytes(&self.get_edge_nodes(end, max_level))
    }

    fn remove_useless_nodes(&mut self, start: u64, end: u64, max_level: i64) {
        let mut cur_start = start;
        let mut cur_end = end;
        for level in TWIG_ROOT_LEVEL..=max_level {
            let mut end_back = cur_end;
            if cur_end % 2 != 0 && level != TWIG_ROOT_LEVEL {
                end_back -= 1;
            }

            let mut start_back = cur_start;
            start_back = start_back.saturating_sub(1);
            for i in start_back..end_back {
                let pos = NodePos::pos(level as u64, i);
                self.delete_node(pos);
            }
            cur_start >>= 1;
            cur_end >>= 1;
        }
    }

    fn get_edge_nodes(&self, end: u64, max_level: i64) -> Vec<EdgeNode> {
        let mut cur_end = end;
        let mut new_edge_nodes = Vec::new();
        for level in TWIG_ROOT_LEVEL..=max_level {
            let mut end_back = cur_end;
            if cur_end % 2 != 0 && level != TWIG_ROOT_LEVEL {
                end_back -= 1;
            }
            let pos = NodePos::pos(level as u64, end_back);
            if let Some(v) = self.get_node(pos) {
                new_edge_nodes.push(EdgeNode { pos, value: *v });
            } else {
                panic!(
                    "What? can not find shard_id={} max_level={} level={} end={} cur_end={}",
                    self.my_shard_id, max_level, level, end, cur_end
                );
            }
            cur_end >>= 1;
        }
        new_edge_nodes
    }

    pub fn sync_nodes_by_level(
        &mut self,
        level: i64,
        n_list: Vec<u64>,
        youngest_twig_id: u64,
    ) -> Vec<u64> {
        let max_n = max_n_at_level(youngest_twig_id, level);
        let pos = NodePos::pos(level as u64, max_n);
        self.set_node_copy(pos, &NULL_NODE_IN_HIGHER_TREE[level as usize]);
        let pos = NodePos::pos(level as u64, max_n + 1);
        self.set_node_copy(pos, &NULL_NODE_IN_HIGHER_TREE[level as usize]);
        // take written_nodes out from self.nodes
        self.nodes.push(Vec::new()); // push a placeholder that will be removed
        let mut written_nodes = self.nodes.swap_remove(level as usize);

        // Pre-partition n_list by node shard to avoid 75% wasted iteration
        let mut shard_lists: [Vec<u64>; NODE_SHARD_COUNT] = Default::default();
        for &i in &n_list {
            shard_lists[i as usize % NODE_SHARD_COUNT].push(i);
        }

        let mut new_list = Vec::with_capacity(n_list.len());
        rayon::scope(|s| {
            // run hashing in parallel across node shards
            for (shard_id, (nodes, shard_list)) in written_nodes
                .iter_mut()
                .zip(shard_lists.iter())
                .enumerate()
            {
                let upper_tree = &*self; // change a mutable borrow to an immutable borrow
                s.spawn(move |_| do_sync_job(upper_tree, nodes, level, shard_id, shard_list));
            }
            for &i in n_list.iter() {
                if new_list.is_empty() || *new_list.last().expect("last() after non-empty check") != i / 2 {
                    new_list.push(i / 2);
                }
            }
        });

        // return written_nodes back to self.nodes
        self.nodes.push(written_nodes);
        self.nodes.swap_remove(level as usize); // the placeholder is removed
        new_list
    }

    pub fn sync_upper_nodes(
        &mut self,
        mut n_list: Vec<u64>,
        youngest_twig_id: u64,
    ) -> (Vec<u64>, [u8; 32]) {
        let max_level = calc_max_level(youngest_twig_id);
        if !n_list.is_empty() {
            for level in FIRST_LEVEL_ABOVE_TWIG..=max_level {
                n_list = self.sync_nodes_by_level(level, n_list, youngest_twig_id);
            }
        }
        let root = *self.get_node(NodePos::pos(max_level as u64, 0)).expect("root node missing at max_level");
        (n_list, root)
    }

    pub fn evict_twigs(
        &mut self,
        n_list: Vec<u64>,
        twig_evict_start: u64,
        twig_evict_end: u64,
    ) -> Vec<u64> {
        let new_list = self.sync_mt_for_active_bits_phase2(n_list);
        // run the pending twig-eviction jobs
        // they were not evicted earlier because sync_mt_for_active_bits_phase2 needs their content
        for twig_id in twig_evict_start..twig_evict_end {
            // evict the twig and store its twigRoot in nodes
            let pos = NodePos::pos(TWIG_ROOT_LEVEL as u64, twig_id);
            let twig_root = self.get_twig(twig_id).expect("twig not found during eviction").twig_root;
            self.set_node_copy(pos, &twig_root);
            let (shard_idx, key) = get_shard_idx_and_key(twig_id);
            self.active_twig_shards[shard_idx].remove(&key);
        }
        new_list
    }

    pub fn sync_mt_for_active_bits_phase2(&mut self, mut n_list: Vec<u64>) -> Vec<u64> {
        // Pre-partition L2 items by twig shard
        let mut l2_by_shard: [Vec<u64>; TWIG_SHARD_COUNT] = Default::default();
        for &i in &n_list {
            let twig_id = i >> 1;
            let (s, _) = get_shard_idx_and_key(twig_id);
            l2_by_shard[s].push(i);
        }

        let mut new_list = Vec::with_capacity(n_list.len());
        rayon::scope(|s| {
            for (sid, twig_shard) in self.active_twig_shards.iter_mut().enumerate() {
                let shard_items = &l2_by_shard[sid];
                s.spawn(move |_| {
                    for &i in shard_items {
                        let twig_id = i >> 1;
                        let (_, k) = get_shard_idx_and_key(twig_id);
                        twig_shard.get_mut(&k).expect("twig not found for sync_l2").sync_l2((i & 1) as i32);
                    }
                });
            }

            for i in &n_list {
                if new_list.is_empty() || *new_list.last().expect("last() after non-empty check") != i / 2 {
                    new_list.push(i / 2);
                }
            }
        });

        mem::swap(&mut new_list, &mut n_list);
        new_list.clear();

        // Pre-partition L3+top items by twig shard
        let mut l3_by_shard: [Vec<u64>; TWIG_SHARD_COUNT] = Default::default();
        for &twig_id in &n_list {
            let (s, _) = get_shard_idx_and_key(twig_id);
            l3_by_shard[s].push(twig_id);
        }

        rayon::scope(|s| {
            for (sid, twig_shard) in self.active_twig_shards.iter_mut().enumerate() {
                let shard_items = &l3_by_shard[sid];
                s.spawn(move |_| {
                    for &twig_id in shard_items {
                        let (_, k) = get_shard_idx_and_key(twig_id);
                        twig_shard.get_mut(&k).expect("twig not found for sync_l3").sync_l3();
                        twig_shard.get_mut(&k).expect("twig not found for sync_top").sync_top();
                    }
                });
            }

            for i in &n_list {
                if new_list.is_empty() || *new_list.last().expect("last() after non-empty check") != i / 2 {
                    new_list.push(i / 2);
                }
            }
        });

        new_list
    }

    /// GPU-accelerated sync_nodes_by_level.
    /// Batches all node hashes at a given level into a single GPU dispatch.
    #[cfg(feature = "cuda")]
    pub fn sync_nodes_by_level_gpu(
        &mut self,
        gpu: &crate::gpu::GpuHasher,
        level: i64,
        n_list: Vec<u64>,
        youngest_twig_id: u64,
    ) -> Vec<u64> {
        use crate::gpu::NodeHashJob;

        let max_n = max_n_at_level(youngest_twig_id, level);
        let pos = NodePos::pos(level as u64, max_n);
        self.set_node_copy(pos, &NULL_NODE_IN_HIGHER_TREE[level as usize]);
        let pos = NodePos::pos(level as u64, max_n + 1);
        self.set_node_copy(pos, &NULL_NODE_IN_HIGHER_TREE[level as usize]);

        // Collect all hash jobs for this level
        let mut jobs = Vec::with_capacity(n_list.len());
        let mut job_positions: Vec<NodePos> = Vec::with_capacity(n_list.len());

        for &i in &n_list {
            let pos = NodePos::pos(level as u64, i);
            if level == FIRST_LEVEL_ABOVE_TWIG {
                let left = self
                    .get_twig_root(2 * i)
                    .copied()
                    .unwrap_or(twig::NULL_TWIG.twig_root);
                let right = self
                    .get_twig_root(2 * i + 1)
                    .copied()
                    .unwrap_or(twig::NULL_TWIG.twig_root);
                jobs.push(NodeHashJob {
                    level: level as u8 - 1,
                    left,
                    right,
                });
            } else {
                let child_nodes = self.nodes.get((level - 1) as usize).expect("child_nodes level missing in upper tree");
                let node_pos_l = NodePos::pos((level - 1) as u64, 2 * i);
                let node_pos_r = NodePos::pos((level - 1) as u64, 2 * i + 1);
                let sl = node_pos_l.nth() as usize % NODE_SHARD_COUNT;
                let sr = node_pos_r.nth() as usize % NODE_SHARD_COUNT;
                let left = *child_nodes[sl].get(&node_pos_l).expect("left child node missing in GPU sync");
                let right = *child_nodes[sr].get(&node_pos_r).expect("right child node missing in GPU sync");
                jobs.push(NodeHashJob {
                    level: level as u8 - 1,
                    left,
                    right,
                });
            }
            job_positions.push(pos);
        }

        // GPU batch hash
        if !jobs.is_empty() {
            let results = gpu.auto_batch_node_hash(&jobs);
            for (idx, pos) in job_positions.iter().enumerate() {
                self.set_node(*pos, results[idx]);
            }
        }

        // Build next level's n_list
        let mut new_list = Vec::with_capacity(n_list.len());
        for &i in &n_list {
            if new_list.is_empty() || *new_list.last().expect("last() after non-empty check") != i / 2 {
                new_list.push(i / 2);
            }
        }
        new_list
    }

    /// GPU-accelerated sync_upper_nodes.
    #[cfg(feature = "cuda")]
    pub fn sync_upper_nodes_gpu(
        &mut self,
        gpu: &crate::gpu::GpuHasher,
        mut n_list: Vec<u64>,
        youngest_twig_id: u64,
    ) -> (Vec<u64>, [u8; 32]) {
        let max_level = calc_max_level(youngest_twig_id);
        if !n_list.is_empty() {
            for level in FIRST_LEVEL_ABOVE_TWIG..=max_level {
                n_list = self.sync_nodes_by_level_gpu(gpu, level, n_list, youngest_twig_id);
            }
        }
        let root = *self.get_node(NodePos::pos(max_level as u64, 0)).expect("root node missing at max_level in GPU sync");
        (n_list, root)
    }

    /// GPU-resident sync_upper_nodes: entire upper tree computation stays on GPU.
    ///
    /// Instead of per-level H↔D round-trips (69 sync calls), this:
    /// 1. Populates a GPU-resident FlashMap with all relevant nodes + twig roots
    /// 2. Runs all level hashing on GPU (device-to-device via flash-map)
    /// 3. Transfers only the root hash back to CPU (32 bytes)
    /// 4. Updates CPU-side HashMaps with the results for edge node/prune operations
    ///
    /// Reduces sync calls from ~54 to ~4 and eliminates ~368KB of PCIe transfers.
    #[cfg(feature = "cuda")]
    pub fn sync_upper_nodes_gpu_resident(
        &mut self,
        gpu: &crate::gpu::GpuHasher,
        gpu_store: &mut crate::gpu::GpuNodeStore,
        n_list: Vec<u64>,
        youngest_twig_id: u64,
    ) -> (Vec<u64>, [u8; 32]) {
        let max_level = calc_max_level(youngest_twig_id);

        if n_list.is_empty() {
            let root = *self.get_node(NodePos::pos(max_level as u64, 0)).expect("root node missing at max_level in GPU-resident sync");
            return (n_list, root);
        }

        // Phase 1: Populate GPU node store with all nodes needed for this sync.
        // Collect twig roots and existing nodes at levels below what we need.
        let mut populate_pairs: Vec<(u64, [u8; 32])> = Vec::new();

        // Add all active twig roots to the GPU store at TWIG_ROOT_LEVEL
        for twig_shard in &self.active_twig_shards {
            for (&key, twig) in twig_shard {
                let pos_val = (TWIG_ROOT_LEVEL as u64) << 56 | key;
                populate_pairs.push((pos_val, twig.twig_root));
            }
        }

        // Add all existing nodes from CPU node storage to GPU store
        for level_idx in 0..MAX_TREE_LEVEL {
            for (shard_id, shard) in self.nodes[level_idx].iter().enumerate() {
                for (pos, hash) in shard.iter_with_context(shard_id, level_idx) {
                    populate_pairs.push((pos.as_u64(), *hash));
                }
            }
        }

        // Batch upload all nodes to GPU store
        if !populate_pairs.is_empty() {
            if let Err(e) = gpu_store.insert_from_host(&populate_pairs) {
                error!("[gpu-resident] Failed to populate store: {e}, falling back to per-level GPU");
                return self.sync_upper_nodes_gpu(gpu, n_list, youngest_twig_id);
            }
        }

        // Also set NULL_NODE sentinel values at max_n boundaries for each level
        {
            let mut boundary_pairs: Vec<(u64, [u8; 32])> = Vec::new();
            let mut current_n_list = n_list.clone();
            for level in FIRST_LEVEL_ABOVE_TWIG..=max_level {
                let max_n = max_n_at_level(youngest_twig_id, level);
                let pos0 = NodePos::pos(level as u64, max_n).as_u64();
                let pos1 = NodePos::pos(level as u64, max_n + 1).as_u64();
                boundary_pairs.push((pos0, NULL_NODE_IN_HIGHER_TREE[level as usize]));
                boundary_pairs.push((pos1, NULL_NODE_IN_HIGHER_TREE[level as usize]));

                // Build next level n_list for boundary calculation
                let mut new_list = Vec::with_capacity(current_n_list.len());
                for &i in &current_n_list {
                    if new_list.is_empty() || *new_list.last().expect("last() after non-empty check") != i / 2 {
                        new_list.push(i / 2);
                    }
                }
                current_n_list = new_list;
            }
            if !boundary_pairs.is_empty() {
                let _ = gpu_store.insert_from_host(&boundary_pairs);
            }
        }

        // Phase 2: Run upper tree sync entirely on GPU
        let result = gpu_store.sync_upper_nodes_on_device(
            gpu,
            n_list.clone(),
            FIRST_LEVEL_ABOVE_TWIG,
            max_level,
        );

        match result {
            Ok((final_n_list, root_hash)) => {
                // Phase 3: Write back results to CPU HashMaps.
                // We need the CPU-side nodes up-to-date for edge node/prune operations.
                // Fetch the computed nodes back from GPU for each level.
                let mut current_list = n_list;
                for level in FIRST_LEVEL_ABOVE_TWIG..=max_level {
                    // Collect positions we computed at this level
                    let positions: Vec<u64> = current_list
                        .iter()
                        .map(|&i| NodePos::pos(level as u64, i).as_u64())
                        .collect();

                    if let Ok(results) = gpu_store.get_to_host(&positions) {
                        for (idx, &i) in current_list.iter().enumerate() {
                            if let Some(hash) = results[idx] {
                                let pos = NodePos::pos(level as u64, i);
                                self.set_node(pos, hash);
                            }
                        }
                    }

                    // Build next level
                    let mut new_list = Vec::with_capacity(current_list.len());
                    for &i in &current_list {
                        if new_list.is_empty() || *new_list.last().expect("last() after non-empty check") != i / 2 {
                            new_list.push(i / 2);
                        }
                    }
                    current_list = new_list;
                }

                (final_n_list, root_hash)
            }
            Err(e) => {
                error!(
                    "[gpu-resident] sync failed: {e}, falling back to per-level GPU"
                );
                self.sync_upper_nodes_gpu(gpu, n_list, youngest_twig_id)
            }
        }
    }

    /// GPU-accelerated evict_twigs.
    #[cfg(feature = "cuda")]
    pub fn evict_twigs_gpu(
        &mut self,
        gpu: &crate::gpu::GpuHasher,
        n_list: Vec<u64>,
        twig_evict_start: u64,
        twig_evict_end: u64,
    ) -> Vec<u64> {
        let new_list = self.sync_mt_for_active_bits_phase2_gpu(gpu, n_list);
        for twig_id in twig_evict_start..twig_evict_end {
            let pos = NodePos::pos(TWIG_ROOT_LEVEL as u64, twig_id);
            let twig_root = self.get_twig(twig_id).expect("twig not found during GPU eviction").twig_root;
            self.set_node_copy(pos, &twig_root);
            let (shard_idx, key) = get_shard_idx_and_key(twig_id);
            self.active_twig_shards[shard_idx].remove(&key);
        }
        new_list
    }

    /// GPU-accelerated active bits phase2 sync.
    /// Batches sync_l2, sync_l3, and sync_top across all touched twigs.
    #[cfg(feature = "cuda")]
    pub fn sync_mt_for_active_bits_phase2_gpu(
        &mut self,
        gpu: &crate::gpu::GpuHasher,
        n_list: Vec<u64>,
    ) -> Vec<u64> {
        // Build deduplicated twig list from L2-level n_list
        let mut twig_list: Vec<u64> = Vec::with_capacity(n_list.len());
        for &i in &n_list {
            let twig_id = i / 2;
            if twig_list.is_empty() || *twig_list.last().expect("last() after non-empty check") != twig_id {
                twig_list.push(twig_id);
            }
        }

        if !twig_list.is_empty() {
            // Gather all 4 L1 values + left_root per twig for the fused kernel
            let n = twig_list.len();
            let mut l1_values: Vec<[u8; 32]> = Vec::with_capacity(n * 4);
            let mut left_roots: Vec<[u8; 32]> = Vec::with_capacity(n);

            for &twig_id in &twig_list {
                let (s, k) = get_shard_idx_and_key(twig_id);
                let twig = self.active_twig_shards[s].get(&k).expect("twig not found for GPU active bits phase2");
                l1_values.push(twig.active_bits_mtl1[0]);
                l1_values.push(twig.active_bits_mtl1[1]);
                l1_values.push(twig.active_bits_mtl1[2]);
                l1_values.push(twig.active_bits_mtl1[3]);
                left_roots.push(twig.left_root);
            }

            // Single GPU dispatch: L2 + L3 + top fused
            let (twig_roots, l2_out, l3_out) =
                gpu.batch_active_bits_fused(&l1_values, &left_roots);

            // Write back all results
            for (idx, &twig_id) in twig_list.iter().enumerate() {
                let (s, k) = get_shard_idx_and_key(twig_id);
                let twig = self.active_twig_shards[s].get_mut(&k).expect("twig not found for GPU active bits writeback");
                twig.active_bits_mtl2[0] = l2_out[idx * 2];
                twig.active_bits_mtl2[1] = l2_out[idx * 2 + 1];
                twig.active_bits_mtl3 = l3_out[idx];
                twig.twig_root = twig_roots[idx];
            }
        }

        // Return next-level n_list
        let mut new_list: Vec<u64> = Vec::with_capacity(twig_list.len());
        for &i in &twig_list {
            if new_list.is_empty() || *new_list.last().expect("last() after non-empty check") != i / 2 {
                new_list.push(i / 2);
            }
        }
        new_list
    }
}

fn do_sync_job(
    upper_tree: &UpperTree,
    nodes: &mut NodeShard,
    level: i64,
    _shard_id: usize,
    n_list: &[u64],
) {
    let child_nodes = upper_tree.nodes.get((level - 1) as usize).expect("child_nodes level missing in do_sync_job");
    for &i in n_list {
        let pos = NodePos::pos(level as u64, i);
        if level == FIRST_LEVEL_ABOVE_TWIG {
            let left_option = upper_tree.get_twig_root(2 * i);
            let left = match left_option {
                Some(v) => v,
                None => panic!("Cannot find left twig root {}", 2 * i),
            };
            let right_option = upper_tree.get_twig_root(2 * i + 1);
            let mut right = [0u8; 32];
            match right_option {
                Some(v) => {
                    right.copy_from_slice(v);
                }
                None => {
                    right.copy_from_slice(&twig::NULL_TWIG.twig_root[..]);
                }
            };
            let mut hash = [0u8; 32];
            hasher::node_hash_inplace(level as u8 - 1, &mut hash, left, &right);
            nodes.insert(pos, hash);
        } else {
            let node_pos_l = NodePos::pos((level - 1) as u64, 2 * i);
            let node_pos_r = NodePos::pos((level - 1) as u64, 2 * i + 1);
            let sl = node_pos_l.nth() as usize % NODE_SHARD_COUNT;
            let sr = node_pos_r.nth() as usize % NODE_SHARD_COUNT;
            let node_l = match child_nodes[sl].get(&node_pos_l) {
                Some(v) => v,
                None => {
                    panic!(
                        "Cannot find left child {}-{} {}-{} {} {:?}",
                        level,
                        i,
                        level - 1,
                        2 * i,
                        2 * i + 1,
                        node_pos_l
                    );
                }
            };

            let node_r = match child_nodes[sr].get(&node_pos_r) {
                Some(v) => v,
                None => {
                    panic!(
                        "Cannot find right child {}-{} {}-{} {} {:?}",
                        level,
                        i,
                        level - 1,
                        2 * i,
                        2 * i + 1,
                        node_pos_r
                    )
                }
            };

            let mut hash = [0u8; 32];
            hasher::node_hash_inplace(level as u8 - 1, &mut hash, node_l, node_r);
            nodes.insert(pos, hash);
        }
    }
}

pub struct Tree {
    pub my_shard_id: usize,

    pub upper_tree: UpperTree,
    pub new_twig_map: HashMap<u64, Box<twig::Twig>>,

    pub entry_file_wr: EntryFileWriter,
    pub twig_file_wr: TwigFileWriter,
    pub dir_name: String,

    // these variables can be recovered from entry file
    pub youngest_twig_id: u64,
    // pub active_bit_shards: [HashMap<u64, [u8; 256]>; TWIG_SHARD_COUNT],
    pub active_bit_shards: Vec<HashMap<u64, ActiveBits>>,
    pub mtree_for_youngest_twig: Box<TwigMT>,

    // The following variables are only used during the execution of one block
    pub mtree_for_yt_change_start: i32,
    pub mtree_for_yt_change_end: i32,
    touched_pos_of_512b: HashSet<u64>,
}

impl Tree {
    pub fn new_blank(
        shard_id: usize,
        buffer_size: usize,
        segment_size: i64,
        dir_name: String,
        suffix: String,
        with_twig_file: bool,
        cipher: Option<Aes256Gcm>,
    ) -> Self {
        let dir_entry = format!("{}/{}{}", dir_name, ENTRIES_PATH, suffix);
        let _ = fs::create_dir_all(&dir_entry);
        let twig_file = if with_twig_file {
            let dir_twig = format!("{}/{}{}", dir_name, TWIG_PATH, suffix);
            let _ = fs::create_dir_all(&dir_twig);
            TwigFile::new(buffer_size, segment_size, dir_twig)
        } else {
            TwigFile::empty()
        };
        let twig_arc = Arc::new(twig_file);
        let directio = cfg!(feature = "directio");
        let ef = EntryFile::new(buffer_size, segment_size, dir_entry, directio, cipher);

        Self {
            my_shard_id: shard_id,
            upper_tree: UpperTree::new(shard_id),
            new_twig_map: HashMap::new(),
            entry_file_wr: EntryFileWriter::new(Arc::new(ef), buffer_size),
            twig_file_wr: TwigFileWriter::new(twig_arc, buffer_size),
            dir_name,
            youngest_twig_id: 0,
            active_bit_shards: vec![HashMap::new(); TWIG_SHARD_COUNT],
            mtree_for_youngest_twig: twig::NULL_MT_FOR_TWIG.clone(),
            mtree_for_yt_change_start: -1,
            mtree_for_yt_change_end: -1,
            touched_pos_of_512b: HashSet::new(),
        }
    }

    pub fn new(
        shard_id: usize,
        buffer_size: usize,
        segment_size: i64,
        dir_name: String,
        suffix: String,
        with_twig_file: bool,
        cipher: Option<Aes256Gcm>,
    ) -> Self {
        let mut tree = Self::new_blank(
            shard_id,
            buffer_size,
            segment_size,
            dir_name,
            suffix,
            with_twig_file,
            cipher,
        );

        tree.new_twig_map.insert(0, twig::NULL_TWIG.clone());
        tree.upper_tree
            .set_node(NodePos::pos(FIRST_LEVEL_ABOVE_TWIG as u64, 0), [0; 32]);
        tree.upper_tree.active_twig_shards[0].insert(0, twig::NULL_TWIG.clone());
        tree.active_bit_shards[0].insert(0, twig::NULL_ACTIVE_BITS.clone());

        tree
    }

    pub fn close(&mut self) {
        // Close files
        self.entry_file_wr.entry_file.close();
        self.twig_file_wr.twig_file.close();
    }

    pub fn get_file_sizes(&self) -> (i64, i64) {
        (
            self.entry_file_wr.entry_file.size(),
            self.twig_file_wr.twig_file.hp_file.size(),
        )
    }

    pub fn truncate_files(&self, entry_file_size: i64, twig_file_size: i64) {
        self.entry_file_wr
            .entry_file
            .truncate(entry_file_size)
            .expect("I/O failed: truncate entry file");
        self.twig_file_wr.twig_file.truncate(twig_file_size);
    }

    pub fn get_active_bits(&self, twig_id: u64) -> &ActiveBits {
        let (shard_idx, key) = get_shard_idx_and_key(twig_id);
        match self.active_bit_shards[shard_idx].get(&key) {
            Some(v) => v,
            None => panic!("cannot find twig {}", twig_id),
        }
    }

    fn get_active_bits_mut(&mut self, twig_id: u64) -> &mut ActiveBits {
        let (shard_idx, key) = get_shard_idx_and_key(twig_id);
        self.active_bit_shards[shard_idx].get_mut(&key).expect("active_bits not found for twig")
    }

    pub fn get_active_bit(&self, sn: u64) -> bool {
        let twig_id = sn >> TWIG_SHIFT;
        let pos = sn as u32 & TWIG_MASK;
        self.get_active_bits(twig_id).get_bit(pos)
    }

    pub fn set_entry_activiation(&mut self, sn: u64, active: bool) {
        let twig_id = sn >> TWIG_SHIFT;
        let pos = sn as u32 & TWIG_MASK;
        let active_bits = self.get_active_bits_mut(twig_id);
        if active {
            active_bits.set_bit(pos);
        } else {
            active_bits.clear_bit(pos);
        }
        self.touch_pos(sn);
    }

    pub fn touch_pos(&mut self, sn: u64) {
        self.touched_pos_of_512b.insert(sn / 512);
    }

    pub fn clear_touched_pos(&mut self) {
        self.touched_pos_of_512b.clear();
    }

    pub fn active_entry(&mut self, sn: u64) {
        self.set_entry_activiation(sn, true);
    }

    pub fn deactive_entry(&mut self, sn: u64) {
        self.set_entry_activiation(sn, false);
    }

    pub fn append_entry(&mut self, entry_bz: &EntryBz) -> Result<i64, std::io::Error> {
        let sn = entry_bz.serial_number();
        self.active_entry(sn);

        let twig_id = sn >> TWIG_SHIFT;
        self.youngest_twig_id = twig_id;
        // record change_start/change_end for endblock sync
        let position = sn as u32 & TWIG_MASK;
        if self.mtree_for_yt_change_start == -1 {
            self.mtree_for_yt_change_start = position as i32;
        } else if self.mtree_for_yt_change_end + 1 != position as i32 {
            panic!("non-increasing position!");
        }
        self.mtree_for_yt_change_end = position as i32;

        let pos = self.entry_file_wr.append(entry_bz)?;
        self.mtree_for_youngest_twig[(LEAF_COUNT_IN_TWIG + position) as usize]
            .copy_from_slice(entry_bz.hash().as_slice());

        if position == TWIG_MASK {
            // when this is the last entry of current twig
            // write the merkle tree of youngest twig to twig_file
            self.sync_mt_for_youngest_twig(false);
            self.twig_file_wr.append_twig(
                &self.mtree_for_youngest_twig[..],
                pos + entry_bz.len() as i64,
            );
            // allocate new twig as youngest twig
            self.youngest_twig_id += 1;
            let (s, i) = get_shard_idx_and_key(self.youngest_twig_id);
            self.new_twig_map
                .insert(self.youngest_twig_id, twig::NULL_TWIG.clone());
            self.active_bit_shards[s].insert(i, twig::NULL_ACTIVE_BITS.clone());

            self.mtree_for_youngest_twig
                .copy_from_slice(&twig::NULL_MT_FOR_TWIG[..]);
            self.touch_pos(sn + 1)
        }
        Ok(pos)
    }

    pub fn prune_twigs(&mut self, start_id: u64, end_id: u64, entry_file_size: i64) {
        if end_id - start_id < MIN_PRUNE_COUNT {
            panic!(
                "The count of pruned twigs is too small: {}",
                end_id - start_id
            );
        }

        self.entry_file_wr
            .entry_file
            .prune_head(entry_file_size)
            .expect("I/O failed: prune_head entry file");
        self.twig_file_wr
            .twig_file
            .prune_head((end_id * twigfile::TWIG_SIZE) as i64);
    }

    pub fn flush_files(&mut self, twig_delete_start: u64, twig_delete_end: u64) -> Vec<u64> {
        let mut entry_file_tmp = self.entry_file_wr.temp_clone();
        let mut twig_file_tmp = self.twig_file_wr.temp_clone();
        mem::swap(&mut entry_file_tmp, &mut self.entry_file_wr);
        mem::swap(&mut twig_file_tmp, &mut self.twig_file_wr);
        let n_list = thread::scope(|s| {
            // run flushing in a threads such that sync_* won't be blocked
            s.spawn(|| {
                entry_file_tmp.flush().expect("I/O failed: flush entry file");
            });
            s.spawn(|| {
                twig_file_tmp.flush();
            });
            self.sync_mt_for_youngest_twig(false);
            let youngest_twig = self.new_twig_map.get(&self.youngest_twig_id).expect("youngest twig not found in new_twig_map");
            let mut twig_map = HashMap::new();
            twig_map.insert(self.youngest_twig_id, youngest_twig.clone());
            mem::swap(&mut self.new_twig_map, &mut twig_map);
            //add new_twig_map's old content to upper_tree
            self.upper_tree.add_twigs(twig_map);
            //now, new_twig_map only contains one member: youngest_twig.clone()

            let n_list = self.sync_mt_for_active_bits_phase1();
            for twig_id in twig_delete_start..twig_delete_end {
                let (shard_idx, key) = get_shard_idx_and_key(twig_id);
                self.active_bit_shards[shard_idx].remove(&key);
            }
            self.touched_pos_of_512b.clear();
            n_list
        });
        mem::swap(&mut entry_file_tmp, &mut self.entry_file_wr);
        mem::swap(&mut twig_file_tmp, &mut self.twig_file_wr);
        n_list
    }

    pub fn sync_mt_for_active_bits_phase1(&mut self) -> Vec<u64> {
        let mut n_list = self
            .touched_pos_of_512b
            .iter()
            .cloned()
            .collect::<Vec<u64>>();
        n_list.sort();

        // Pre-partition by twig shard to avoid 75% wasted iteration
        let mut by_shard: [Vec<u64>; TWIG_SHARD_COUNT] = Default::default();
        for &i in &n_list {
            let twig_id = i >> 2;
            let (s, _) = get_shard_idx_and_key(twig_id);
            by_shard[s].push(i);
        }

        let mut new_list = Vec::with_capacity(n_list.len());
        rayon::scope(|s| {
            for (sid, twig_shard) in self.upper_tree.active_twig_shards.iter_mut().enumerate() {
                let active_bit_shards = &self.active_bit_shards;
                let shard_items = &by_shard[sid];
                s.spawn(move |_| {
                    for &i in shard_items {
                        let twig_id = i >> 2;
                        let (s, k) = get_shard_idx_and_key(twig_id);
                        let active_bits = active_bit_shards[s].get(&k).expect("active_bits not found for sync_l1");
                        twig_shard
                            .get_mut(&k)
                            .expect("twig not found for sync_l1")
                            .sync_l1((i & 3) as i32, active_bits);
                    }
                });
            }
            for i in &n_list {
                if new_list.is_empty() || *new_list.last().expect("last() after non-empty check") != i / 2 {
                    new_list.push(i / 2);
                }
            }
            new_list
        })
    }

    pub fn sync_mt_for_youngest_twig(&mut self, recover_mode: bool) {
        if self.mtree_for_yt_change_start == -1 {
            return;
        }
        sync_mtree(
            &mut self.mtree_for_youngest_twig,
            self.mtree_for_yt_change_start,
            self.mtree_for_yt_change_end,
        );
        self.mtree_for_yt_change_start = -1;
        self.mtree_for_yt_change_end = 0;
        let youngest_twig;
        if recover_mode {
            youngest_twig = self.upper_tree.get_twig(self.youngest_twig_id).expect("youngest twig not found in upper_tree during recover sync");
        } else {
            youngest_twig = self.new_twig_map.get_mut(&self.youngest_twig_id).expect("youngest twig not found in new_twig_map during sync");
        }
        youngest_twig
            .left_root
            .copy_from_slice(&self.mtree_for_youngest_twig[1]);
    }

    pub fn load_mt_for_non_youngest_twig(&mut self, twig_id: u64) {
        if self.mtree_for_yt_change_start == -1 {
            return;
        }
        self.mtree_for_yt_change_start = -1;
        self.mtree_for_yt_change_end = 0;
        let active_twig = self.upper_tree.get_twig(self.youngest_twig_id).expect("youngest twig not found in upper_tree for non-youngest load");
        self.twig_file_wr
            .twig_file
            .get_hash_root(twig_id, &mut active_twig.left_root);
    }

    fn get_upper_path_and_root(&self, twig_id: u64) -> (Vec<proof::ProofNode>, [u8; 32]) {
        let max_level = calc_max_level(self.youngest_twig_id);

        let mut peer_hash = [0u8; 32];
        // use '^ 1' to flip the lowest bit to get sibling
        if let Some(v) = self.upper_tree.get_twig_root(twig_id ^ 1) {
            peer_hash.copy_from_slice(v);
        } else {
            peer_hash.copy_from_slice(&twig::NULL_TWIG.twig_root[..]);
        }

        let mut self_hash = [0u8; 32];
        if let Some(v) = self.upper_tree.get_twig_root(twig_id) {
            self_hash.copy_from_slice(v);
        } else {
            return (Vec::new(), [0; 32]);
        }

        let mut upper_path = Vec::with_capacity((max_level - FIRST_LEVEL_ABOVE_TWIG + 1) as usize);
        upper_path.push(proof::ProofNode {
            self_hash,
            peer_hash,
            peer_at_left: (twig_id & 1) != 0, //twig_id's lowest bit == 1 so the peer is at left
        });

        let mut n = twig_id >> 1;
        for level in FIRST_LEVEL_ABOVE_TWIG..max_level {
            let peer_at_left = (n & 1) != 0;

            let snode = match self.upper_tree.get_node(NodePos::pos(level as u64, n)) {
                Some(v) => *v,
                None => panic!("Cannot find node"),
            };
            let pnode = match self.upper_tree.get_node(NodePos::pos(level as u64, n ^ 1)) {
                Some(v) => *v,
                None => panic!("Cannot find node"),
            };
            upper_path.push(proof::ProofNode {
                self_hash: snode,
                peer_hash: pnode,
                peer_at_left,
            });
            n >>= 1;
        }

        let root_option = self.upper_tree.get_node(NodePos::pos(max_level as u64, 0));
        let root = match root_option {
            Some(v) => *v,
            None => panic!("cannot find node {}-{}", max_level, 0),
        };

        (upper_path, root)
    }

    pub fn get_proof(&self, sn: u64) -> Result<proof::ProofPath, String> {
        let twig_id = sn >> TWIG_SHIFT;
        let mut path = proof::ProofPath::new();
        path.serial_num = sn;

        if twig_id > self.youngest_twig_id {
            return Err("twig_id > self.youngest_twig_id".to_string());
        }

        (path.upper_path, path.root) = self.get_upper_path_and_root(twig_id);
        if path.upper_path.is_empty() {
            return Err("Cannot find upper path".to_string());
        }

        if twig_id == self.youngest_twig_id {
            path.left_of_twig = proof::get_left_path_in_mem(&self.mtree_for_youngest_twig, sn);
        } else {
            let twig_file = &self.twig_file_wr.twig_file;
            if twig_file.is_empty() {
                return Err("twig_file is empty".to_string());
            }
            path.left_of_twig = proof::get_left_path_on_disk(twig_file, twig_id, sn);
        }
        let (s, k) = get_shard_idx_and_key(twig_id);
        let twig = self.upper_tree.active_twig_shards[s]
            .get(&k)
            .unwrap_or(&twig::NULL_TWIG);
        let active_bits = self.active_bit_shards[s]
            .get(&k)
            .unwrap_or(&twig::NULL_ACTIVE_BITS);
        path.right_of_twig = proof::get_right_path(twig, active_bits, sn);

        Ok(path)
    }

    pub fn get_hashes_by_pos_list(&self, pos_list: &Vec<(u8, u64)>) -> Vec<[u8; 32]> {
        let mut hashes = Vec::with_capacity(pos_list.len());
        for (_, hash) in self.hash_iter(pos_list) {
            hashes.push(hash);
        }
        hashes
    }

    pub fn hash_iter<'a>(&'a self, pos_list: &'a Vec<(u8, u64)>) -> HashIterForPosList<'a> {
        HashIterForPosList {
            cache: HashMap::with_capacity(20),
            tree: self,
            pos_list,
            idx: 0,
        }
    }

    fn get_hash_by_node(
        &self,
        level: u8,
        nth: u64,
        cache: &mut HashMap<i64, [u8; 32]>,
    ) -> [u8; 32] {
        let mut twig_id: u64 = 0;
        let mut level_stride: u64 = 0;
        if level <= 12 {
            level_stride = 4096 >> level;
            twig_id = nth / level_stride;
        }

        // left tree of twig
        if level <= 11 && (nth % level_stride) < level_stride / 2 {
            let is_youngest_twig_id = twig_id == self.youngest_twig_id;
            let self_id: u64 = nth % level_stride;
            let idx = level_stride / 2 + self_id;
            if is_youngest_twig_id {
                return self.mtree_for_youngest_twig[idx as usize];
            } else {
                let mut hash = [0u8; 32];
                self.twig_file_wr
                    .twig_file
                    .get_hash_node(twig_id, idx as i64, cache, &mut hash);
                return hash;
            }
        }

        // right tree of twig
        if (8..=11).contains(&level) {
            let (s, k) = get_shard_idx_and_key(twig_id);
            let active_bits = self.active_bit_shards[s]
                .get(&k)
                .unwrap_or(&twig::NULL_ACTIVE_BITS);
            let self_id: u64 = (nth % level_stride) - level_stride / 2;
            if level == 8 {
                let hash = active_bits.get_bits(self_id as usize, 32);
                return hash.try_into().expect("active_bits hash slice must be 32 bytes");
            }
            let twig = self.upper_tree.active_twig_shards[s]
                .get(&k)
                .unwrap_or(&twig::NULL_TWIG);
            if level == 9 {
                return twig.active_bits_mtl1[self_id as usize];
            }
            if level == 10 {
                return twig.active_bits_mtl2[self_id as usize];
            }
            if level == 11 {
                return twig.active_bits_mtl3;
            }
        }

        // upper tree
        if level == 12 {
            return *self
                .upper_tree
                .get_twig_root(twig_id)
                .unwrap_or(&twig::NULL_TWIG.twig_root);
        }
        *self
            .upper_tree
            .get_node(NodePos::pos(level as u64, nth))
            .unwrap_or(&NULL_NODE_IN_HIGHER_TREE[level as usize])
    }

    /// GPU-accelerated flush_files. Same as flush_files but uses GPU batch hashing.
    #[cfg(feature = "cuda")]
    pub fn flush_files_gpu(
        &mut self,
        gpu: &crate::gpu::GpuHasher,
        twig_delete_start: u64,
        twig_delete_end: u64,
    ) -> Vec<u64> {
        let mut entry_file_tmp = self.entry_file_wr.temp_clone();
        let mut twig_file_tmp = self.twig_file_wr.temp_clone();
        mem::swap(&mut entry_file_tmp, &mut self.entry_file_wr);
        mem::swap(&mut twig_file_tmp, &mut self.twig_file_wr);
        let n_list = thread::scope(|s| {
            s.spawn(|| {
                entry_file_tmp.flush().expect("I/O failed: flush entry file in GPU path");
            });
            s.spawn(|| {
                twig_file_tmp.flush();
            });
            self.sync_mt_for_youngest_twig_gpu(gpu);
            let youngest_twig = self.new_twig_map.get(&self.youngest_twig_id).expect("youngest twig not found in new_twig_map during GPU flush");
            let mut twig_map = HashMap::new();
            twig_map.insert(self.youngest_twig_id, youngest_twig.clone());
            mem::swap(&mut self.new_twig_map, &mut twig_map);
            self.upper_tree.add_twigs(twig_map);

            let n_list = self.sync_mt_for_active_bits_phase1_gpu(gpu);
            for twig_id in twig_delete_start..twig_delete_end {
                let (shard_idx, key) = get_shard_idx_and_key(twig_id);
                self.active_bit_shards[shard_idx].remove(&key);
            }
            self.touched_pos_of_512b.clear();
            n_list
        });
        mem::swap(&mut entry_file_tmp, &mut self.entry_file_wr);
        mem::swap(&mut twig_file_tmp, &mut self.twig_file_wr);
        n_list
    }

    /// GPU-accelerated sync for youngest twig Merkle tree.
    #[cfg(feature = "cuda")]
    pub fn sync_mt_for_youngest_twig_gpu(&mut self, gpu: &crate::gpu::GpuHasher) {
        if self.mtree_for_yt_change_start == -1 {
            return;
        }
        let start = self.mtree_for_yt_change_start;
        let end = self.mtree_for_yt_change_end;
        twig::sync_mtrees_gpu(
            gpu,
            &mut [(&mut self.mtree_for_youngest_twig, start, end)],
        );
        self.mtree_for_yt_change_start = -1;
        self.mtree_for_yt_change_end = 0;
        let youngest_twig = self.new_twig_map.get_mut(&self.youngest_twig_id).expect("youngest twig not found in new_twig_map during GPU youngest sync");
        youngest_twig
            .left_root
            .copy_from_slice(&self.mtree_for_youngest_twig[1]);
    }

    /// GPU-accelerated active bits phase1 sync.
    /// Batches all sync_l1 operations into a single GPU dispatch.
    #[cfg(feature = "cuda")]
    pub fn sync_mt_for_active_bits_phase1_gpu(
        &mut self,
        gpu: &crate::gpu::GpuHasher,
    ) -> Vec<u64> {
        use crate::gpu::NodeHashJob;

        let mut n_list = self
            .touched_pos_of_512b
            .iter()
            .cloned()
            .collect::<Vec<u64>>();
        n_list.sort();

        // Collect all sync_l1 jobs
        let mut jobs = Vec::with_capacity(n_list.len());
        let mut targets: Vec<(u64, i32)> = Vec::with_capacity(n_list.len()); // (twig_id, pos)

        for &i in &n_list {
            let twig_id = i >> 2;
            let pos = (i & 3) as i32;
            let (s, k) = get_shard_idx_and_key(twig_id);
            let active_bits = self.active_bit_shards[s].get(&k).expect("active_bits not found for GPU phase1 sync");
            let start = pos as usize * 512;
            // sync_l1 hashes active_bits pages into mtl1
            let left_bits = active_bits.get_bits(start / 256, 32);
            let right_bits = active_bits.get_bits(start / 256 + 1, 32);
            let mut left = [0u8; 32];
            let mut right = [0u8; 32];
            left.copy_from_slice(left_bits);
            right.copy_from_slice(right_bits);
            jobs.push(NodeHashJob {
                level: 8,
                left,
                right,
            });
            targets.push((twig_id, pos));
        }

        if !jobs.is_empty() {
            let results = gpu.auto_batch_node_hash(&jobs);
            for (idx, (twig_id, pos)) in targets.iter().enumerate() {
                let (s, k) = get_shard_idx_and_key(*twig_id);
                let twig = self.upper_tree.active_twig_shards[s]
                    .get_mut(&k)
                    .expect("twig not found for GPU phase1 writeback");
                twig.active_bits_mtl1[*pos as usize] = results[idx];
            }
        }

        let mut new_list = Vec::with_capacity(n_list.len());
        for &i in &n_list {
            if new_list.is_empty() || *new_list.last().expect("last() after non-empty check") != i / 2 {
                new_list.push(i / 2);
            }
        }
        new_list
    }
}

pub struct HashIterForPosList<'a> {
    cache: HashMap<i64, [u8; 32]>,
    tree: &'a Tree,
    pos_list: &'a Vec<(u8, u64)>,
    idx: usize,
}

impl Iterator for HashIterForPosList<'_> {
    type Item = (usize, [u8; 32]);

    fn next(&mut self) -> Option<Self::Item> {
        if self.idx >= self.pos_list.len() {
            return None;
        }
        let (level, nth) = self.pos_list[self.idx];
        let hash = self.tree.get_hash_by_node(level, nth, &mut self.cache);
        let idx = self.idx;
        self.idx += 1;
        Some((idx, hash))
    }
}

pub fn max_n_at_level(youngest_twig_id: u64, level: i64) -> u64 {
    if level < FIRST_LEVEL_ABOVE_TWIG {
        panic!("level is too small");
    }
    let shift = level - FIRST_LEVEL_ABOVE_TWIG + 1;
    youngest_twig_id >> shift
}

pub fn get_shard_idx_and_key(twig_id: u64) -> (usize, u64) {
    let idx = twig_id as usize % TWIG_SHARD_COUNT;
    let key = twig_id / TWIG_SHARD_COUNT as u64;
    (idx, key)
}

// debug

impl Tree {
    pub fn print(&self) {
        let mut offset: i64 = 0;
        let mut buf = vec![0u8; 2048];
        for twig_id in 0..self.youngest_twig_id {
            for _sn in twig_id * 2048..(twig_id + 1) * 2048 {
                let n = self.entry_file_wr.entry_file.read_entry(offset, &mut buf);
                if n > buf.len() {
                    buf.resize(n, 0);
                    self.entry_file_wr.entry_file.read_entry(offset, &mut buf);
                }
                offset += n as i64;

                let entry_bz = EntryBz { bz: &buf[0..n] };
                let entry = Entry::from_bz(&entry_bz);

                debug!(
                    "[entry] twig: {}, sn: {}, k: {}, v: {}",
                    twig_id,
                    entry.serial_number,
                    hex::encode(entry.key),
                    hex::encode(entry.value)
                );
            }
        }

        let mut cache: HashMap<i64, Hash32> = HashMap::new();
        for twig_id in 0..self.youngest_twig_id {
            for _sn in twig_id * 2048..(twig_id + 1) * 2048 {
                let _h = self.get_hash_by_node(0, _sn, &mut cache);
                debug!(
                    "[hash] level: {}, nth: {}, hash: {}",
                    0,
                    _sn,
                    hex::encode(_h)
                );
            }
        }
    }
}

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

    // ========== ME-4: NodeShard Dense/Sparse Tests ==========

    #[test]
    fn test_node_shard_dense_insert_get_remove() {
        let mut shard = NodeShard::new_dense();
        let hash_a = [0xAA; 32];
        let hash_b = [0xBB; 32];

        // nth values must be multiples of NODE_SHARD_COUNT for shard 0
        let pos_a = NodePos::pos(5, 0);
        let pos_b = NodePos::pos(5, NODE_SHARD_COUNT as u64 * 3);

        shard.insert(pos_a, hash_a);
        shard.insert(pos_b, hash_b);

        assert_eq!(shard.get(&pos_a), Some(&hash_a));
        assert_eq!(shard.get(&pos_b), Some(&hash_b));

        // Remove pos_a
        shard.remove(&pos_a);
        assert_eq!(shard.get(&pos_a), None);
        // pos_b still present
        assert_eq!(shard.get(&pos_b), Some(&hash_b));

        // Remove pos_b
        shard.remove(&pos_b);
        assert_eq!(shard.get(&pos_b), None);
    }

    #[test]
    fn test_node_shard_sparse_insert_get_remove() {
        let mut shard = NodeShard::new_sparse();
        let hash_a = [0x11; 32];
        let hash_b = [0x22; 32];
        let hash_c = [0x33; 32];

        let pos_a = NodePos::pos(30, 100);
        let pos_b = NodePos::pos(30, 200);
        let pos_c = NodePos::pos(31, 50);

        shard.insert(pos_a, hash_a);
        shard.insert(pos_b, hash_b);
        shard.insert(pos_c, hash_c);

        assert_eq!(shard.get(&pos_a), Some(&hash_a));
        assert_eq!(shard.get(&pos_b), Some(&hash_b));
        assert_eq!(shard.get(&pos_c), Some(&hash_c));

        shard.remove(&pos_b);
        assert_eq!(shard.get(&pos_b), None);
        assert_eq!(shard.get(&pos_a), Some(&hash_a));
        assert_eq!(shard.get(&pos_c), Some(&hash_c));
    }

    #[test]
    fn test_node_shard_dense_sparse_equivalence() {
        let mut dense = NodeShard::new_dense();
        let mut sparse = NodeShard::new_sparse();

        let test_data: Vec<(NodePos, [u8; 32])> = (0..20)
            .map(|i| {
                let nth = i as u64 * NODE_SHARD_COUNT as u64;
                let pos = NodePos::pos(5, nth);
                let mut hash = [0u8; 32];
                for k in 0..32 {
                    hash[k] = ((i * 7 + k) & 0xFF) as u8;
                }
                (pos, hash)
            })
            .collect();

        for (pos, hash) in &test_data {
            dense.insert(*pos, *hash);
            sparse.insert(*pos, *hash);
        }

        for (pos, _) in &test_data {
            let d = dense.get(pos);
            let s = sparse.get(pos);
            assert_eq!(
                d, s,
                "dense vs sparse mismatch at level={} nth={}",
                pos.level(),
                pos.nth()
            );
        }

        // Non-existent position returns None in both
        let missing = NodePos::pos(5, 99999 * NODE_SHARD_COUNT as u64);
        assert_eq!(dense.get(&missing), None);
        assert_eq!(sparse.get(&missing), None);
    }

    #[test]
    fn test_node_shard_iter_with_context() {
        let mut shard = NodeShard::new_dense();
        let shard_id = 2;
        let level = 7;

        let mut inserted: HashMap<u64, [u8; 32]> = HashMap::new();
        for i in 0..10 {
            let nth = i * NODE_SHARD_COUNT + shard_id;
            let pos = NodePos::pos(level as u64, nth as u64);
            let mut hash = [0u8; 32];
            hash[0] = i as u8;
            shard.insert(pos, hash);
            inserted.insert(nth as u64, hash);
        }

        let mut recovered: HashMap<u64, [u8; 32]> = HashMap::new();
        for (pos, hash) in shard.iter_with_context(shard_id, level) {
            assert_eq!(pos.level(), level as u64);
            recovered.insert(pos.nth(), *hash);
        }

        assert_eq!(
            inserted.len(),
            recovered.len(),
            "iter count mismatch: {} vs {}",
            inserted.len(),
            recovered.len()
        );
        for (nth, expected_hash) in &inserted {
            let got = recovered.get(nth);
            assert_eq!(
                got,
                Some(expected_hash),
                "missing or wrong hash at nth={}", nth
            );
        }
    }

    #[test]
    fn test_node_shard_dense_auto_resize() {
        let mut shard = NodeShard::new_dense();

        let pos_0 = NodePos::pos(5, 0);
        let pos_far = NodePos::pos(5, 1000 * NODE_SHARD_COUNT as u64);

        let hash_0 = [0x01; 32];
        let hash_far = [0x02; 32];

        shard.insert(pos_0, hash_0);
        assert_eq!(shard.get(&pos_0), Some(&hash_0));

        // Inserting at a much higher index auto-resizes
        shard.insert(pos_far, hash_far);
        assert_eq!(shard.get(&pos_far), Some(&hash_far));

        // Original still accessible
        assert_eq!(shard.get(&pos_0), Some(&hash_0));

        // In-between indices are None
        let pos_mid = NodePos::pos(5, 500 * NODE_SHARD_COUNT as u64);
        assert_eq!(shard.get(&pos_mid), None);
    }

    #[test]
    fn test_node_shard_remove_nonexistent() {
        let mut dense = NodeShard::new_dense();
        let mut sparse = NodeShard::new_sparse();

        let pos = NodePos::pos(5, 0);

        // Removing from empty shards should not panic
        dense.remove(&pos);
        sparse.remove(&pos);

        assert_eq!(dense.get(&pos), None);
        assert_eq!(sparse.get(&pos), None);

        // Insert then remove a different position
        dense.insert(NodePos::pos(5, 0), [0xAA; 32]);
        dense.remove(&NodePos::pos(5, NODE_SHARD_COUNT as u64 * 99));
        assert_eq!(dense.get(&NodePos::pos(5, 0)), Some(&[0xAA; 32]));
    }
}