clone_solana_shred_version/
lib.rs

1//! Calculation of [shred] versions.
2//!
3//! [shred]: https://solana.com/docs/terminology#shred
4
5use {
6    clone_solana_hard_forks::HardForks, clone_solana_hash::Hash,
7    clone_solana_sha256_hasher::extend_and_hash,
8};
9
10pub fn version_from_hash(hash: &Hash) -> u16 {
11    let hash = hash.as_ref();
12    let mut accum = [0u8; 2];
13    hash.chunks(2).for_each(|seed| {
14        accum
15            .iter_mut()
16            .zip(seed)
17            .for_each(|(accum, seed)| *accum ^= *seed)
18    });
19    // convert accum into a u16
20    // Because accum[0] is a u8, 8bit left shift of the u16 can never overflow
21    #[allow(clippy::arithmetic_side_effects)]
22    let version = ((accum[0] as u16) << 8) | accum[1] as u16;
23
24    // ensure version is never zero, to avoid looking like an uninitialized version
25    version.saturating_add(1)
26}
27
28pub fn compute_shred_version(genesis_hash: &Hash, hard_forks: Option<&HardForks>) -> u16 {
29    let mut hash = *genesis_hash;
30    if let Some(hard_forks) = hard_forks {
31        for &(slot, count) in hard_forks.iter() {
32            let buf = [slot.to_le_bytes(), (count as u64).to_le_bytes()].concat();
33            hash = extend_and_hash(&hash, &buf);
34        }
35    }
36
37    version_from_hash(&hash)
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43
44    #[test]
45    fn test_compute_shred_version() {
46        assert_eq!(compute_shred_version(&Hash::default(), None), 1);
47        let mut hard_forks = HardForks::default();
48        assert_eq!(
49            compute_shred_version(&Hash::default(), Some(&hard_forks)),
50            1
51        );
52        hard_forks.register(1);
53        assert_eq!(
54            compute_shred_version(&Hash::default(), Some(&hard_forks)),
55            55551
56        );
57        hard_forks.register(1);
58        assert_eq!(
59            compute_shred_version(&Hash::default(), Some(&hard_forks)),
60            46353
61        );
62    }
63}