miraland_runtime/
snapshot_hash.rs

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