trezoa_shred_version/
lib.rs

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