tenzro-storage 0.1.0

State storage layer for Tenzro Network — Merkle trees, RocksDB, block storage, snapshots
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
//! State snapshot management for Tenzro Network
//!
//! This module provides functionality for creating, storing, and restoring
//! state snapshots at specific block heights.
//!
//! # Concurrency Model
//!
//! ## For `SnapshotManager<RocksDbStore>`
//!
//! Prefer `create_checkpoint_snapshot()` which uses RocksDB's built-in atomic checkpoint
//! mechanism. The checkpoint creates a point-in-time consistent view of the database
//! without blocking writes.
//!
//! ## For other `KvStore` implementations
//!
//! Use the `state_lock` for coordination:
//!
//! 1. Writers must hold a write lock when modifying state that needs to be
//!    consistent in snapshots.
//! 2. Callers collecting state data for `create_snapshot()` must hold a read lock
//!    while collecting state data to ensure consistency.
//!
//! ```ignore
//! // Example usage:
//! let state_lock = manager.state_lock();
//! let state_data = {
//!     let _guard = state_lock.read();
//!     // Collect state data here while holding read lock
//!     collect_state_data()
//! };
//! manager.create_snapshot(height, state_root, block_hash, state_data).await?;
//! ```

use crate::error::{Result, StorageError};
use crate::kv::{KvStore, RocksDbStore, WriteOp, CF_SNAPSHOTS};
use flate2::read::{GzDecoder, GzEncoder};
use flate2::Compression;
use parking_lot::RwLock;
use rocksdb::checkpoint::Checkpoint;
use serde::{Deserialize, Serialize};
use std::io::Read;
use std::path::Path;
use std::sync::Arc;
use tenzro_types::{BlockHeight, Hash, Timestamp};

/// A state snapshot at a specific block height
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Snapshot {
    /// The block height at which this snapshot was taken
    pub height: BlockHeight,
    /// The state root hash at this height
    pub state_root: Hash,
    /// The block hash at this height
    pub block_hash: Hash,
    /// The timestamp when the snapshot was created
    pub timestamp: Timestamp,
    /// Snapshot metadata
    pub metadata: SnapshotMetadata,
    /// Compressed state data
    pub state_data: Vec<u8>,
}

/// Metadata for a snapshot
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SnapshotMetadata {
    /// Total number of accounts in the snapshot
    pub account_count: u64,
    /// Total state size in bytes (before compression)
    pub state_size: u64,
    /// Compressed size in bytes
    pub compressed_size: u64,
    /// Compression algorithm used
    pub compression: CompressionType,
}

/// Compression types for snapshots
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum CompressionType {
    /// No compression
    None,
    /// LZ4 compression
    Lz4,
    /// Zstandard compression
    Zstd,
    /// Gzip compression (flate2)
    Gzip,
}

/// Compresses raw data using gzip.
///
/// Returns the compressed bytes. If compression fails (should not happen for
/// valid input), returns a `StorageError`.
fn compress_gzip(data: &[u8]) -> Result<Vec<u8>> {
    let mut encoder = GzEncoder::new(data, Compression::default());
    let mut compressed = Vec::new();
    encoder.read_to_end(&mut compressed).map_err(|e| {
        StorageError::InvalidSnapshot(format!("gzip compression failed: {}", e))
    })?;
    Ok(compressed)
}

/// Decompresses gzip-compressed data.
///
/// Returns the original uncompressed bytes. Returns a `StorageError` if the
/// data is not valid gzip.
fn decompress_gzip(data: &[u8]) -> Result<Vec<u8>> {
    let mut decoder = GzDecoder::new(data);
    let mut decompressed = Vec::new();
    decoder.read_to_end(&mut decompressed).map_err(|e| {
        StorageError::InvalidSnapshot(format!("gzip decompression failed: {}", e))
    })?;
    Ok(decompressed)
}

/// Snapshot manager for creating and managing state snapshots
pub struct SnapshotManager<K: KvStore> {
    kv_store: Arc<K>,
    retention_count: Arc<RwLock<u64>>,
    /// Lock to ensure consistent state reads during snapshot creation.
    /// Callers must hold a read lock on this while collecting state data,
    /// and writers must hold a write lock when modifying state.
    state_lock: Arc<RwLock<()>>,
}

impl<K: KvStore> SnapshotManager<K> {
    /// Creates a new snapshot manager
    pub fn new(kv_store: Arc<K>, retention_count: u64) -> Self {
        Self {
            kv_store,
            retention_count: Arc::new(RwLock::new(retention_count)),
            state_lock: Arc::new(RwLock::new(())),
        }
    }

    /// Returns the state lock for coordinating snapshot consistency.
    ///
    /// Hold a read lock while collecting state data for `create_snapshot()`.
    /// Writers should hold a write lock when modifying state that needs
    /// to be consistent in snapshots.
    ///
    /// # Example
    ///
    /// ```ignore
    /// // Collect state data with read lock held
    /// let state_lock = manager.state_lock();
    /// let state_data = {
    ///     let _guard = state_lock.read();
    ///     collect_state_data()
    /// };
    /// manager.create_snapshot(height, state_root, block_hash, state_data).await?;
    /// ```
    pub fn state_lock(&self) -> Arc<RwLock<()>> {
        self.state_lock.clone()
    }

    /// Creates a consistent snapshot by collecting state data under the lock.
    ///
    /// This method atomically:
    /// 1. Acquires the state write lock (prevents all concurrent modifications)
    /// 2. Calls the provided closure to collect state data
    /// 3. Stores the snapshot while the lock is held
    ///
    /// This eliminates the race condition where state could change between
    /// data collection and snapshot creation.
    ///
    /// # Example
    ///
    /// ```ignore
    /// manager.create_consistent_snapshot(height, state_root, block_hash, || {
    ///     collect_state_data_from_vm()
    /// }).await?;
    /// ```
    // The parking_lot::RwLock guard is intentionally held across async calls here.
    // The called methods (store_snapshot, prune_snapshots) perform only synchronous
    // RocksDB I/O and never yield to the executor, so no deadlock is possible.
    #[allow(clippy::await_holding_lock)]
    pub async fn create_consistent_snapshot<F>(
        &self,
        height: BlockHeight,
        state_root: Hash,
        block_hash: Hash,
        collect_state: F,
    ) -> Result<Snapshot>
    where
        F: FnOnce() -> Vec<u8>,
    {
        // Acquire WRITE lock to prevent any concurrent state modifications
        // during both state collection AND snapshot serialization
        let _guard = self.state_lock.write();

        // Collect state data while holding the lock — no torn reads possible
        let state_data = collect_state();

        let state_size = state_data.len() as u64;
        let compressed_data = compress_gzip(&state_data)?;
        let compressed_size = compressed_data.len() as u64;

        let snapshot = Snapshot {
            height,
            state_root,
            block_hash,
            timestamp: Timestamp::now(),
            metadata: SnapshotMetadata {
                account_count: 0,
                state_size,
                compressed_size,
                compression: CompressionType::Gzip,
            },
            state_data: compressed_data,
        };

        // Store the snapshot while still holding the lock
        self.store_snapshot(&snapshot).await?;

        // Prune old snapshots
        self.prune_snapshots(height).await?;

        Ok(snapshot)
    }

    /// Creates a snapshot at the given height
    ///
    /// # Consistency
    ///
    /// The caller is responsible for ensuring `state_data` is consistent by
    /// holding a read lock on `state_lock()` while collecting the data.
    /// Prefer `create_consistent_snapshot()` which handles locking automatically.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let state_lock = manager.state_lock();
    /// let state_data = {
    ///     let _guard = state_lock.read();
    ///     // Collect state data here
    ///     collect_state_data()
    /// };
    /// manager.create_snapshot(height, state_root, block_hash, state_data).await?;
    /// ```
    // See create_consistent_snapshot for rationale on #[allow(clippy::await_holding_lock)]
    #[allow(clippy::await_holding_lock)]
    pub async fn create_snapshot(
        &self,
        height: BlockHeight,
        state_root: Hash,
        block_hash: Hash,
        state_data: Vec<u8>,
    ) -> Result<Snapshot> {
        // Acquire read lock to prevent state modifications during snapshot serialization
        let _guard = self.state_lock.read();

        let state_size = state_data.len() as u64;

        // Compress with gzip
        let compressed_data = compress_gzip(&state_data)?;
        let compressed_size = compressed_data.len() as u64;

        let snapshot = Snapshot {
            height,
            state_root,
            block_hash,
            timestamp: Timestamp::now(),
            metadata: SnapshotMetadata {
                account_count: 0, // Would be calculated from state_data
                state_size,
                compressed_size,
                compression: CompressionType::Gzip,
            },
            state_data: compressed_data,
        };

        // Store the snapshot
        self.store_snapshot(&snapshot).await?;

        // Prune old snapshots
        self.prune_snapshots(height).await?;

        Ok(snapshot)
    }

    /// Stores a snapshot
    async fn store_snapshot(&self, snapshot: &Snapshot) -> Result<()> {
        let key = Self::snapshot_key(snapshot.height);
        let data = bincode::serialize(snapshot)?;
        self.kv_store.put(CF_SNAPSHOTS, &key, &data)?;

        // Also store in the index
        let index_key = Self::snapshot_index_key();
        let mut index = self.load_snapshot_index().await?;
        index.push(snapshot.height);
        index.sort_by_key(|h| h.0);
        let index_data = bincode::serialize(&index)?;
        self.kv_store.put(CF_SNAPSHOTS, &index_key, &index_data)?;

        Ok(())
    }

    /// Loads a snapshot at the given height
    pub async fn load_snapshot(&self, height: BlockHeight) -> Result<Option<Snapshot>> {
        let key = Self::snapshot_key(height);
        if let Some(data) = self.kv_store.get(CF_SNAPSHOTS, &key)? {
            let snapshot: Snapshot = bincode::deserialize(&data)?;
            Ok(Some(snapshot))
        } else {
            Ok(None)
        }
    }

    /// Deletes a snapshot at the given height
    pub async fn delete_snapshot(&self, height: BlockHeight) -> Result<()> {
        let key = Self::snapshot_key(height);
        self.kv_store.delete(CF_SNAPSHOTS, &key)?;

        // Remove from index
        let index_key = Self::snapshot_index_key();
        let mut index = self.load_snapshot_index().await?;
        index.retain(|h| *h != height);
        let index_data = bincode::serialize(&index)?;
        self.kv_store.put(CF_SNAPSHOTS, &index_key, &index_data)?;

        Ok(())
    }

    /// Lists all available snapshots
    pub async fn list_snapshots(&self) -> Result<Vec<BlockHeight>> {
        self.load_snapshot_index().await
    }

    /// Gets the latest snapshot
    pub async fn latest_snapshot(&self) -> Result<Option<Snapshot>> {
        let index = self.load_snapshot_index().await?;
        if let Some(height) = index.last() {
            self.load_snapshot(*height).await
        } else {
            Ok(None)
        }
    }

    /// Prunes old snapshots based on retention policy
    async fn prune_snapshots(&self, _current_height: BlockHeight) -> Result<()> {
        let retention = *self.retention_count.read();
        let index = self.load_snapshot_index().await?;

        if index.len() as u64 <= retention {
            return Ok(());
        }

        // Calculate how many to delete
        let to_delete = index.len() as u64 - retention;
        let mut ops = Vec::new();

        for height in index.iter().take(to_delete as usize) {
            let key = Self::snapshot_key(*height);
            ops.push(WriteOp::Delete {
                cf: CF_SNAPSHOTS.to_string(),
                key,
            });
        }

        // Update index
        let new_index: Vec<BlockHeight> = index.into_iter().skip(to_delete as usize).collect();
        let index_key = Self::snapshot_index_key();
        let index_data = bincode::serialize(&new_index)?;
        ops.push(WriteOp::Put {
            cf: CF_SNAPSHOTS.to_string(),
            key: index_key,
            value: index_data,
        });

        self.kv_store.write_batch(ops)?;

        Ok(())
    }

    /// Loads the snapshot index
    async fn load_snapshot_index(&self) -> Result<Vec<BlockHeight>> {
        let key = Self::snapshot_index_key();
        if let Some(data) = self.kv_store.get(CF_SNAPSHOTS, &key)? {
            let index: Vec<BlockHeight> = bincode::deserialize(&data)?;
            Ok(index)
        } else {
            Ok(Vec::new())
        }
    }

    /// Generates a key for storing a snapshot
    fn snapshot_key(height: BlockHeight) -> Vec<u8> {
        let mut key = b"snapshot:".to_vec();
        key.extend_from_slice(&height.0.to_be_bytes());
        key
    }

    /// Generates the key for the snapshot index
    fn snapshot_index_key() -> Vec<u8> {
        b"snapshot_index".to_vec()
    }

    /// Sets the retention count
    pub fn set_retention_count(&self, count: u64) {
        *self.retention_count.write() = count;
    }

    /// Gets the retention count
    pub fn retention_count(&self) -> u64 {
        *self.retention_count.read()
    }
}

impl SnapshotManager<RocksDbStore> {
    /// Creates a consistent snapshot using RocksDB checkpoint
    /// This ensures atomicity and consistency by using RocksDB's built-in checkpoint mechanism.
    /// The checkpoint creates a point-in-time consistent view of the database without blocking writes.
    pub async fn create_checkpoint_snapshot(
        &self,
        height: BlockHeight,
        state_root: Hash,
        block_hash: Hash,
        checkpoint_path: &Path,
    ) -> Result<Snapshot> {
        // Create a RocksDB checkpoint for consistent point-in-time snapshot
        let db = self.kv_store.db();
        let checkpoint = Checkpoint::new(db)
            .map_err(|e| StorageError::InvalidSnapshot(format!("Failed to create checkpoint: {}", e)))?;

        checkpoint.create_checkpoint(checkpoint_path)
            .map_err(|e| StorageError::InvalidSnapshot(format!("Failed to create checkpoint at path: {}", e)))?;

        // The checkpoint is now a consistent snapshot of the entire database
        // For the metadata, we need to read the checkpoint to get the actual size
        let checkpoint_size = calculate_directory_size(checkpoint_path)?;

        let snapshot = Snapshot {
            height,
            state_root,
            block_hash,
            timestamp: Timestamp::now(),
            metadata: SnapshotMetadata {
                account_count: 0, // Would need to scan checkpoint to calculate
                state_size: checkpoint_size,
                compressed_size: checkpoint_size,
                compression: CompressionType::None,
            },
            state_data: checkpoint_path.to_string_lossy().as_bytes().to_vec(),
        };

        // Store the snapshot using the base implementation
        let key = Self::snapshot_key(snapshot.height);
        let data = bincode::serialize(&snapshot)?;
        self.kv_store.put(CF_SNAPSHOTS, &key, &data)?;

        // Update the index
        let index_key = Self::snapshot_index_key();
        let index_data_opt = self.kv_store.get(CF_SNAPSHOTS, &index_key)?;
        let mut index: Vec<BlockHeight> = if let Some(data) = index_data_opt {
            bincode::deserialize(&data)?
        } else {
            Vec::new()
        };
        index.push(snapshot.height);
        index.sort_by_key(|h| h.0);
        let index_data = bincode::serialize(&index)?;
        self.kv_store.put(CF_SNAPSHOTS, &index_key, &index_data)?;

        // Prune old snapshots
        let retention = *self.retention_count.read();
        if index.len() as u64 > retention {
            let to_delete = index.len() as u64 - retention;
            let mut ops = Vec::new();

            for height in index.iter().take(to_delete as usize) {
                let key = Self::snapshot_key(*height);
                ops.push(WriteOp::Delete {
                    cf: CF_SNAPSHOTS.to_string(),
                    key,
                });
            }

            // Update index
            let new_index: Vec<BlockHeight> = index.into_iter().skip(to_delete as usize).collect();
            let index_data = bincode::serialize(&new_index)?;
            ops.push(WriteOp::Put {
                cf: CF_SNAPSHOTS.to_string(),
                key: index_key,
                value: index_data,
            });

            self.kv_store.write_batch(ops)?;
        }

        Ok(snapshot)
    }
}

/// Helper function to calculate the total size of a directory recursively
fn calculate_directory_size(path: &Path) -> Result<u64> {
    let mut total_size = 0u64;

    if path.is_file() {
        return Ok(std::fs::metadata(path)?.len());
    }

    if path.is_dir() {
        for entry in std::fs::read_dir(path)? {
            let entry = entry?;
            let metadata = entry.metadata()?;
            if metadata.is_file() {
                total_size += metadata.len();
            } else if metadata.is_dir() {
                total_size += calculate_directory_size(&entry.path())?;
            }
        }
    }

    Ok(total_size)
}

/// A single key-value entry within a snapshot's serialized state data.
///
/// `state_data` in a `Snapshot` is a `bincode`-serialized `Vec<SnapshotEntry>`.
/// This allows `SnapshotRestorer::restore_from_snapshot` to write each entry
/// back into the correct column family of any `KvStore` implementation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SnapshotEntry {
    /// Column family name (e.g. CF_STATE, CF_ACCOUNTS)
    pub cf: String,
    /// Key bytes
    pub key: Vec<u8>,
    /// Value bytes
    pub value: Vec<u8>,
}

/// Serializes a collection of column-family key-value pairs into the wire
/// format expected by `Snapshot::state_data`.
///
/// # Example
///
/// ```ignore
/// let entries = vec![
///     SnapshotEntry { cf: CF_STATE.to_string(), key: b"k1".to_vec(), value: b"v1".to_vec() },
/// ];
/// let bytes = serialize_snapshot_entries(&entries)?;
/// manager.create_snapshot(height, state_root, block_hash, bytes).await?;
/// ```
pub fn serialize_snapshot_entries(entries: &[SnapshotEntry]) -> Result<Vec<u8>> {
    bincode::serialize(entries).map_err(|e| {
        StorageError::SerializationError(format!("Failed to serialize snapshot entries: {}", e))
    })
}

/// Compute a deterministic state-root hash over a set of snapshot entries.
///
/// Replaces the long-standing `Hash::zero()` stub. The root is
/// `SHA-256(domain || sorted_triples)` where each triple is
/// `len_le(cf) || cf || len_le(key) || key || len_le(value) || value`
/// and `domain = b"tenzro/snapshot/state-root/v1"`.
///
/// Determinism: entries are sorted by `(cf, key)` before hashing so any
/// snapshot of the same logical state produces the same root regardless of
/// the order entries were collected in. The length prefixes guarantee no
/// preimage ambiguity across cf/key/value boundaries.
///
/// This is *not* a Merkle-Patricia-Trie root — that's a heavier construct
/// that the consensus state layer maintains separately and threads in via
/// `create_snapshot(state_root, ...)`. This helper exists so callers that
/// don't have an MPT (offline tooling, lightweight snapshots, tests) can
/// still produce and verify a load-bearing root without falling back to
/// `Hash::zero()`.
pub fn compute_state_root(entries: &[SnapshotEntry]) -> Hash {
    use sha2::{Digest, Sha256};
    const DOMAIN: &[u8] = b"tenzro/snapshot/state-root/v1";

    let mut sorted: Vec<&SnapshotEntry> = entries.iter().collect();
    sorted.sort_by(|a, b| {
        a.cf.as_bytes()
            .cmp(b.cf.as_bytes())
            .then_with(|| a.key.cmp(&b.key))
    });

    let mut hasher = Sha256::new();
    hasher.update(DOMAIN);
    for e in sorted {
        let cf = e.cf.as_bytes();
        hasher.update((cf.len() as u32).to_le_bytes());
        hasher.update(cf);
        hasher.update((e.key.len() as u32).to_le_bytes());
        hasher.update(&e.key);
        hasher.update((e.value.len() as u32).to_le_bytes());
        hasher.update(&e.value);
    }
    let digest = hasher.finalize();
    let mut out = [0u8; 32];
    out.copy_from_slice(&digest);
    Hash::new(out)
}

/// Snapshot restoration helper
pub struct SnapshotRestorer;

impl SnapshotRestorer {
    /// Restores state from a snapshot by writing all stored key-value entries
    /// back into `store`.
    ///
    /// The `snapshot.state_data` field must contain a `bincode`-serialized
    /// `Vec<SnapshotEntry>` (produced by `serialize_snapshot_entries`).
    /// Each entry is written to the corresponding column family atomically via
    /// a single `write_batch_sync` call so the restoration is crash-safe.
    ///
    /// For RocksDB checkpoint snapshots (created by
    /// `create_checkpoint_snapshot`), `state_data` holds the checkpoint path
    /// as UTF-8 bytes and cannot be replayed through this method — use a
    /// filesystem-level restore for those.
    ///
    /// Returns the number of key-value entries restored.
    pub async fn restore_from_snapshot(
        snapshot: &Snapshot,
        store: &dyn KvStore,
    ) -> Result<RestoredState> {
        if snapshot.state_data.is_empty() {
            return Err(StorageError::InvalidSnapshot(
                "snapshot contains no state data".to_string(),
            ));
        }

        // Decompress state data if needed
        let raw_data = match snapshot.metadata.compression {
            CompressionType::Gzip => decompress_gzip(&snapshot.state_data)?,
            CompressionType::None => snapshot.state_data.clone(),
            other => {
                return Err(StorageError::InvalidSnapshot(format!(
                    "unsupported compression type: {:?}",
                    other
                )));
            }
        };

        // Attempt to decode as a Vec<SnapshotEntry>. If the data is not in
        // that format (e.g. it is a checkpoint path), return an error rather
        // than silently ignoring the contents.
        let entries: Vec<SnapshotEntry> =
            bincode::deserialize(&raw_data).map_err(|e| {
                StorageError::InvalidSnapshot(format!(
                    "state_data is not a valid SnapshotEntry list \
                     (is this a RocksDB checkpoint snapshot?): {}",
                    e
                ))
            })?;

        let entry_count = entries.len();
        tracing::info!(
            "Restoring snapshot at height {} with {} entries",
            snapshot.height.0,
            entry_count
        );

        // Convert to WriteOps and flush in one atomic fsync'd batch.
        let ops: Vec<WriteOp> = entries
            .iter()
            .map(|e| WriteOp::Put {
                cf: e.cf.clone(),
                key: e.key.clone(),
                value: e.value.clone(),
            })
            .collect();

        if !ops.is_empty() {
            store.write_batch_sync(ops)?;
        }

        tracing::info!(
            "Snapshot at height {} restored successfully ({} entries)",
            snapshot.height.0,
            entry_count
        );

        Ok(RestoredState {
            height: snapshot.height,
            state_root: snapshot.state_root,
            account_count: snapshot.metadata.account_count,
            entries_restored: entry_count as u64,
        })
    }

    /// Validates a snapshot.
    ///
    /// Checks the trivial invariants (state_data non-empty, compressed_size
    /// matches), then — when the payload decodes as a `Vec<SnapshotEntry>` —
    /// recomputes the deterministic state root via [`compute_state_root`] and
    /// requires it to match the snapshot's recorded `state_root`. When the
    /// recorded root is `Hash::zero()`, the recompute check is skipped (legacy
    /// callers that didn't set a real root still validate as long as their
    /// payload is well-formed). Returns `false` on root mismatch so the
    /// snapshot can be rejected before any restore attempt.
    pub fn validate_snapshot(snapshot: &Snapshot) -> Result<bool> {
        if snapshot.state_data.is_empty() {
            return Ok(false);
        }
        if snapshot.metadata.compressed_size != snapshot.state_data.len() as u64 {
            return Ok(false);
        }

        // If the recorded root is non-zero AND the payload decodes as
        // SnapshotEntry list, recompute and compare.
        if snapshot.state_root != Hash::zero() {
            let raw = match snapshot.metadata.compression {
                CompressionType::Gzip => match decompress_gzip(&snapshot.state_data) {
                    Ok(d) => d,
                    Err(_) => return Ok(true), // can't decompress => accept basic checks only
                },
                CompressionType::None => snapshot.state_data.clone(),
                _ => return Ok(true),
            };
            if let Ok(entries) = bincode::deserialize::<Vec<SnapshotEntry>>(&raw) {
                let recomputed = compute_state_root(&entries);
                if recomputed != snapshot.state_root {
                    tracing::warn!(
                        height = snapshot.height.0,
                        recorded = %hex::encode(snapshot.state_root.as_bytes()),
                        recomputed = %hex::encode(recomputed.as_bytes()),
                        "snapshot state_root mismatch"
                    );
                    return Ok(false);
                }
            }
        }
        Ok(true)
    }
}

/// Result of restoring from a snapshot
#[derive(Debug, Clone)]
pub struct RestoredState {
    /// The block height of the restored state
    pub height: BlockHeight,
    /// The state root hash
    pub state_root: Hash,
    /// Number of accounts in the snapshot (from metadata)
    pub account_count: u64,
    /// Number of key-value entries actually written back to the store
    pub entries_restored: u64,
}

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

    #[tokio::test]
    async fn test_snapshot_creation() {
        let kv_store = Arc::new(MemoryStore::new());
        let manager = SnapshotManager::new(kv_store, 5);

        let state_data = vec![1, 2, 3, 4, 5];
        let snapshot = manager
            .create_snapshot(
                BlockHeight::new(100),
                Hash::zero(),
                Hash::zero(),
                state_data,
            )
            .await
            .unwrap();

        assert_eq!(snapshot.height, BlockHeight::new(100));
        assert_eq!(snapshot.metadata.state_size, 5);
    }

    #[tokio::test]
    async fn test_snapshot_load() {
        let kv_store = Arc::new(MemoryStore::new());
        let manager = SnapshotManager::new(kv_store, 5);

        let state_data = vec![1, 2, 3, 4, 5];
        manager
            .create_snapshot(
                BlockHeight::new(100),
                Hash::zero(),
                Hash::zero(),
                state_data,
            )
            .await
            .unwrap();

        let loaded = manager
            .load_snapshot(BlockHeight::new(100))
            .await
            .unwrap();
        assert!(loaded.is_some());
        assert_eq!(loaded.unwrap().height, BlockHeight::new(100));
    }

    #[tokio::test]
    async fn test_snapshot_pruning() {
        let kv_store = Arc::new(MemoryStore::new());
        let manager = SnapshotManager::new(kv_store, 3);

        // Create 5 snapshots
        for i in 1..=5 {
            manager
                .create_snapshot(
                    BlockHeight::new(i * 100),
                    Hash::zero(),
                    Hash::zero(),
                    vec![i as u8],
                )
                .await
                .unwrap();
        }

        // Should only have 3 snapshots (retention policy)
        let snapshots = manager.list_snapshots().await.unwrap();
        assert_eq!(snapshots.len(), 3);

        // Should have the latest 3
        assert_eq!(snapshots[0], BlockHeight::new(300));
        assert_eq!(snapshots[1], BlockHeight::new(400));
        assert_eq!(snapshots[2], BlockHeight::new(500));
    }

    #[tokio::test]
    async fn test_latest_snapshot() {
        let kv_store = Arc::new(MemoryStore::new());
        let manager = SnapshotManager::new(kv_store, 5);

        manager
            .create_snapshot(
                BlockHeight::new(100),
                Hash::zero(),
                Hash::zero(),
                vec![1],
            )
            .await
            .unwrap();

        manager
            .create_snapshot(
                BlockHeight::new(200),
                Hash::zero(),
                Hash::zero(),
                vec![2],
            )
            .await
            .unwrap();

        let latest = manager.latest_snapshot().await.unwrap();
        assert!(latest.is_some());
        assert_eq!(latest.unwrap().height, BlockHeight::new(200));
    }

    #[tokio::test]
    async fn test_snapshot_deletion() {
        let kv_store = Arc::new(MemoryStore::new());
        let manager = SnapshotManager::new(kv_store, 5);

        manager
            .create_snapshot(
                BlockHeight::new(100),
                Hash::zero(),
                Hash::zero(),
                vec![1],
            )
            .await
            .unwrap();

        manager.delete_snapshot(BlockHeight::new(100)).await.unwrap();

        let loaded = manager.load_snapshot(BlockHeight::new(100)).await.unwrap();
        assert!(loaded.is_none());
    }

    #[tokio::test]
    async fn test_restore_from_snapshot_writes_entries() {
        use crate::kv::{MemoryStore, CF_STATE, CF_ACCOUNTS};

        let entries = vec![
            SnapshotEntry {
                cf: CF_STATE.to_string(),
                key: b"balance:0xabc".to_vec(),
                value: 1234u128.to_le_bytes().to_vec(),
            },
            SnapshotEntry {
                cf: CF_ACCOUNTS.to_string(),
                key: b"account:0xabc".to_vec(),
                value: b"account_data".to_vec(),
            },
        ];

        let state_data = serialize_snapshot_entries(&entries).unwrap();
        let restore_store = Arc::new(MemoryStore::new());
        let manager = SnapshotManager::new(restore_store.clone(), 5);

        let snapshot = manager
            .create_snapshot(
                BlockHeight::new(42),
                Hash::zero(),
                Hash::zero(),
                state_data,
            )
            .await
            .unwrap();

        // Restore into a fresh store
        let target_store = Arc::new(MemoryStore::new());
        let restored = SnapshotRestorer::restore_from_snapshot(&snapshot, target_store.as_ref())
            .await
            .unwrap();

        assert_eq!(restored.height, BlockHeight::new(42));
        assert_eq!(restored.entries_restored, 2);

        // Verify entries are actually in the target store
        let balance_bytes = target_store
            .get(CF_STATE, b"balance:0xabc")
            .unwrap()
            .expect("balance entry should be present");
        assert_eq!(u128::from_le_bytes(balance_bytes.try_into().unwrap()), 1234u128);

        let account_bytes = target_store
            .get(CF_ACCOUNTS, b"account:0xabc")
            .unwrap()
            .expect("account entry should be present");
        assert_eq!(account_bytes, b"account_data");
    }

    #[tokio::test]
    async fn test_restore_from_snapshot_empty_data_returns_error() {
        let kv_store = Arc::new(MemoryStore::new());
        let target_store = Arc::new(MemoryStore::new());

        // Build a snapshot with empty state_data
        let snapshot = Snapshot {
            height: BlockHeight::new(1),
            state_root: Hash::zero(),
            block_hash: Hash::zero(),
            timestamp: Timestamp::now(),
            metadata: SnapshotMetadata {
                account_count: 0,
                state_size: 0,
                compressed_size: 0,
                compression: CompressionType::None,
            },
            state_data: vec![],
        };

        let result = SnapshotRestorer::restore_from_snapshot(&snapshot, target_store.as_ref()).await;
        assert!(result.is_err(), "empty state_data must return an error");

        // Silence unused variable warning
        let _ = kv_store;
    }

    #[tokio::test]
    async fn test_restore_roundtrip_via_snapshot_manager() {
        use crate::kv::{MemoryStore, CF_STATE};

        // Populate source state
        let source_entries = (0u8..5).map(|i| SnapshotEntry {
            cf: CF_STATE.to_string(),
            key: format!("key:{}", i).into_bytes(),
            value: vec![i * 10],
        }).collect::<Vec<_>>();

        let state_data = serialize_snapshot_entries(&source_entries).unwrap();

        let snap_store = Arc::new(MemoryStore::new());
        let manager = SnapshotManager::new(snap_store, 10);

        let snapshot = manager
            .create_snapshot(BlockHeight::new(77), Hash::zero(), Hash::zero(), state_data)
            .await
            .unwrap();

        // Validate the snapshot first
        assert!(SnapshotRestorer::validate_snapshot(&snapshot).unwrap());

        // Restore into a fresh target
        let target = Arc::new(MemoryStore::new());
        let result = SnapshotRestorer::restore_from_snapshot(&snapshot, target.as_ref())
            .await
            .unwrap();
        assert_eq!(result.entries_restored, 5);

        // Confirm all entries are present
        for i in 0u8..5 {
            let key = format!("key:{}", i).into_bytes();
            let val = target.get(CF_STATE, &key).unwrap().expect("entry missing");
            assert_eq!(val, vec![i * 10]);
        }
    }

    #[test]
    fn compute_state_root_is_deterministic() {
        use crate::kv::CF_STATE;
        let entries_a = vec![
            SnapshotEntry {
                cf: CF_STATE.to_string(),
                key: b"k1".to_vec(),
                value: b"v1".to_vec(),
            },
            SnapshotEntry {
                cf: CF_STATE.to_string(),
                key: b"k2".to_vec(),
                value: b"v2".to_vec(),
            },
        ];
        let entries_b = vec![
            SnapshotEntry {
                cf: CF_STATE.to_string(),
                key: b"k2".to_vec(),
                value: b"v2".to_vec(),
            },
            SnapshotEntry {
                cf: CF_STATE.to_string(),
                key: b"k1".to_vec(),
                value: b"v1".to_vec(),
            },
        ];
        let root_a = compute_state_root(&entries_a);
        let root_b = compute_state_root(&entries_b);
        assert_eq!(root_a, root_b, "root must be order-independent");
        assert_ne!(root_a, Hash::zero(), "root must be non-zero for non-empty payload");
    }

    #[test]
    fn compute_state_root_changes_with_value() {
        use crate::kv::CF_STATE;
        let entries_a = vec![SnapshotEntry {
            cf: CF_STATE.to_string(),
            key: b"k".to_vec(),
            value: b"v1".to_vec(),
        }];
        let entries_b = vec![SnapshotEntry {
            cf: CF_STATE.to_string(),
            key: b"k".to_vec(),
            value: b"v2".to_vec(),
        }];
        assert_ne!(
            compute_state_root(&entries_a),
            compute_state_root(&entries_b)
        );
    }

    #[tokio::test]
    async fn validate_snapshot_accepts_correct_root() {
        use crate::kv::CF_STATE;
        let kv_store = Arc::new(MemoryStore::new());
        let manager = SnapshotManager::new(kv_store, 5);
        let entries = vec![
            SnapshotEntry {
                cf: CF_STATE.to_string(),
                key: b"alpha".to_vec(),
                value: b"1".to_vec(),
            },
            SnapshotEntry {
                cf: CF_STATE.to_string(),
                key: b"beta".to_vec(),
                value: b"2".to_vec(),
            },
        ];
        let real_root = compute_state_root(&entries);
        let payload = serialize_snapshot_entries(&entries).unwrap();
        let snapshot = manager
            .create_snapshot(BlockHeight::new(42), real_root, Hash::zero(), payload)
            .await
            .unwrap();
        assert!(SnapshotRestorer::validate_snapshot(&snapshot).unwrap());
    }

    #[tokio::test]
    async fn validate_snapshot_rejects_wrong_root() {
        use crate::kv::CF_STATE;
        let kv_store = Arc::new(MemoryStore::new());
        let manager = SnapshotManager::new(kv_store, 5);
        let entries = vec![SnapshotEntry {
            cf: CF_STATE.to_string(),
            key: b"alpha".to_vec(),
            value: b"1".to_vec(),
        }];
        let mut wrong_root = [0u8; 32];
        wrong_root[0] = 0x42; // arbitrary non-zero hash
        let payload = serialize_snapshot_entries(&entries).unwrap();
        let snapshot = manager
            .create_snapshot(
                BlockHeight::new(43),
                Hash::new(wrong_root),
                Hash::zero(),
                payload,
            )
            .await
            .unwrap();
        assert!(!SnapshotRestorer::validate_snapshot(&snapshot).unwrap());
    }
}