solana_runtime/
snapshot_archive_info.rs

1//! Information about snapshot archives
2
3use {
4    crate::{
5        snapshot_hash::SnapshotHash,
6        snapshot_utils::{self, ArchiveFormat, Result},
7    },
8    solana_sdk::clock::Slot,
9    std::{cmp::Ordering, path::PathBuf},
10};
11
12/// Trait to query the snapshot archive information
13pub trait SnapshotArchiveInfoGetter {
14    fn snapshot_archive_info(&self) -> &SnapshotArchiveInfo;
15
16    fn path(&self) -> &PathBuf {
17        &self.snapshot_archive_info().path
18    }
19
20    fn slot(&self) -> Slot {
21        self.snapshot_archive_info().slot
22    }
23
24    fn hash(&self) -> &SnapshotHash {
25        &self.snapshot_archive_info().hash
26    }
27
28    fn archive_format(&self) -> ArchiveFormat {
29        self.snapshot_archive_info().archive_format
30    }
31
32    fn is_remote(&self) -> bool {
33        self.snapshot_archive_info()
34            .path
35            .parent()
36            .map_or(false, |p| {
37                p.ends_with(snapshot_utils::SNAPSHOT_ARCHIVE_DOWNLOAD_DIR)
38            })
39    }
40}
41
42/// Common information about a snapshot archive
43#[derive(PartialEq, Eq, Debug, Clone)]
44pub struct SnapshotArchiveInfo {
45    /// Path to the snapshot archive file
46    pub path: PathBuf,
47
48    /// Slot that the snapshot was made
49    pub slot: Slot,
50
51    /// Hash for the snapshot
52    pub hash: SnapshotHash,
53
54    /// Archive format for the snapshot file
55    pub archive_format: ArchiveFormat,
56}
57
58/// Information about a full snapshot archive: its path, slot, hash, and archive format
59#[derive(PartialEq, Eq, Debug, Clone)]
60pub struct FullSnapshotArchiveInfo(SnapshotArchiveInfo);
61
62impl FullSnapshotArchiveInfo {
63    /// Parse the path to a full snapshot archive and return a new `FullSnapshotArchiveInfo`
64    pub fn new_from_path(path: PathBuf) -> Result<Self> {
65        let filename = snapshot_utils::path_to_file_name_str(path.as_path())?;
66        let (slot, hash, archive_format) =
67            snapshot_utils::parse_full_snapshot_archive_filename(filename)?;
68
69        Ok(Self::new(SnapshotArchiveInfo {
70            path,
71            slot,
72            hash,
73            archive_format,
74        }))
75    }
76
77    pub(crate) fn new(snapshot_archive_info: SnapshotArchiveInfo) -> Self {
78        Self(snapshot_archive_info)
79    }
80}
81
82impl SnapshotArchiveInfoGetter for FullSnapshotArchiveInfo {
83    fn snapshot_archive_info(&self) -> &SnapshotArchiveInfo {
84        &self.0
85    }
86}
87
88impl PartialOrd for FullSnapshotArchiveInfo {
89    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
90        Some(self.cmp(other))
91    }
92}
93
94// Order `FullSnapshotArchiveInfo` by slot (ascending), which practically is sorting chronologically
95impl Ord for FullSnapshotArchiveInfo {
96    fn cmp(&self, other: &Self) -> Ordering {
97        self.slot().cmp(&other.slot())
98    }
99}
100
101/// Information about an incremental snapshot archive: its path, slot, base slot, hash, and archive format
102#[derive(PartialEq, Eq, Debug, Clone)]
103pub struct IncrementalSnapshotArchiveInfo {
104    /// The slot that the incremental snapshot was based from.  This is the same as the full
105    /// snapshot slot used when making the incremental snapshot.
106    base_slot: Slot,
107
108    /// Use the `SnapshotArchiveInfo` struct for the common fields: path, slot, hash, and
109    /// archive_format, but as they pertain to the incremental snapshot.
110    inner: SnapshotArchiveInfo,
111}
112
113impl IncrementalSnapshotArchiveInfo {
114    /// Parse the path to an incremental snapshot archive and return a new `IncrementalSnapshotArchiveInfo`
115    pub fn new_from_path(path: PathBuf) -> Result<Self> {
116        let filename = snapshot_utils::path_to_file_name_str(path.as_path())?;
117        let (base_slot, slot, hash, archive_format) =
118            snapshot_utils::parse_incremental_snapshot_archive_filename(filename)?;
119
120        Ok(Self::new(
121            base_slot,
122            SnapshotArchiveInfo {
123                path,
124                slot,
125                hash,
126                archive_format,
127            },
128        ))
129    }
130
131    pub(crate) fn new(base_slot: Slot, snapshot_archive_info: SnapshotArchiveInfo) -> Self {
132        Self {
133            base_slot,
134            inner: snapshot_archive_info,
135        }
136    }
137
138    pub fn base_slot(&self) -> Slot {
139        self.base_slot
140    }
141}
142
143impl SnapshotArchiveInfoGetter for IncrementalSnapshotArchiveInfo {
144    fn snapshot_archive_info(&self) -> &SnapshotArchiveInfo {
145        &self.inner
146    }
147}
148
149impl PartialOrd for IncrementalSnapshotArchiveInfo {
150    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
151        Some(self.cmp(other))
152    }
153}
154
155// Order `IncrementalSnapshotArchiveInfo` by base slot (ascending), then slot (ascending), which
156// practically is sorting chronologically
157impl Ord for IncrementalSnapshotArchiveInfo {
158    fn cmp(&self, other: &Self) -> Ordering {
159        self.base_slot()
160            .cmp(&other.base_slot())
161            .then(self.slot().cmp(&other.slot()))
162    }
163}