solana_runtime/
snapshot_hash.rs

1//! Helper types and functions for handling and dealing with snapshot hashes.
2use {
3    solana_accounts_db::{accounts_hash::AccountsHashKind, epoch_accounts_hash::EpochAccountsHash},
4    solana_sdk::{
5        clock::Slot,
6        hash::{Hash, Hasher},
7    },
8};
9
10/// At startup, when loading from snapshots, the starting snapshot hashes need to be passed to
11/// SnapshotPackagerService, which is in charge of pushing the hashes to CRDS.  This struct wraps
12/// up those values make it easier to pass from bank_forks_utils, through validator, to
13/// SnapshotPackagerService.
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15pub struct StartingSnapshotHashes {
16    pub full: FullSnapshotHash,
17    pub incremental: Option<IncrementalSnapshotHash>,
18}
19
20/// Used by SnapshotPackagerService and SnapshotGossipManager, this struct adds type safety to
21/// ensure a full snapshot hash is pushed to the right CRDS.
22#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23pub struct FullSnapshotHash(pub (Slot, SnapshotHash));
24
25/// Used by SnapshotPackagerService and SnapshotGossipManager, this struct adds type safety to
26/// ensure an incremental snapshot hash is pushed to the right CRDS.
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28pub struct IncrementalSnapshotHash(pub (Slot, SnapshotHash));
29
30/// The hash used for snapshot archives
31#[derive(Debug, PartialEq, Eq, Clone, Copy)]
32pub struct SnapshotHash(pub Hash);
33
34impl SnapshotHash {
35    /// Make a snapshot hash from an accounts hash and epoch accounts hash
36    #[must_use]
37    pub fn new(
38        accounts_hash: &AccountsHashKind,
39        epoch_accounts_hash: Option<&EpochAccountsHash>,
40    ) -> Self {
41        let snapshot_hash = match epoch_accounts_hash {
42            None => *accounts_hash.as_hash(),
43            Some(epoch_accounts_hash) => {
44                let mut hasher = Hasher::default();
45                hasher.hash(accounts_hash.as_hash().as_ref());
46                hasher.hash(epoch_accounts_hash.as_ref().as_ref());
47                hasher.result()
48            }
49        };
50
51        Self(snapshot_hash)
52    }
53}