trezoa_shred_version/
lib.rs1use {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 #[allow(clippy::arithmetic_side_effects)]
19 let version = ((accum[0] as u16) << 8) | accum[1] as u16;
20
21 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}