2022-12-22 00:58:06 -08:00
|
|
|
//! Calculation of [shred] versions.
|
|
|
|
//!
|
|
|
|
//! [shred]: https://docs.solana.com/terminology#shred
|
|
|
|
|
2020-10-24 08:39:28 -07:00
|
|
|
#![cfg(feature = "full")]
|
2020-10-19 13:19:24 -07:00
|
|
|
|
2020-02-24 09:18:08 -08:00
|
|
|
use solana_sdk::{
|
|
|
|
hard_forks::HardForks,
|
|
|
|
hash::{extend_and_hash, Hash},
|
|
|
|
};
|
|
|
|
|
|
|
|
pub fn version_from_hash(hash: &Hash) -> u16 {
|
|
|
|
let hash = hash.as_ref();
|
|
|
|
let mut accum = [0u8; 2];
|
|
|
|
hash.chunks(2).for_each(|seed| {
|
|
|
|
accum
|
|
|
|
.iter_mut()
|
|
|
|
.zip(seed)
|
|
|
|
.for_each(|(accum, seed)| *accum ^= *seed)
|
|
|
|
});
|
|
|
|
// convert accum into a u16
|
2021-03-08 17:37:57 -08:00
|
|
|
// Because accum[0] is a u8, 8bit left shift of the u16 can never overflow
|
|
|
|
#[allow(clippy::integer_arithmetic)]
|
2020-02-24 09:18:08 -08:00
|
|
|
let version = ((accum[0] as u16) << 8) | accum[1] as u16;
|
|
|
|
|
|
|
|
// ensure version is never zero, to avoid looking like an uninitialized version
|
|
|
|
version.saturating_add(1)
|
|
|
|
}
|
2020-02-07 08:57:54 -08:00
|
|
|
|
|
|
|
pub fn compute_shred_version(genesis_hash: &Hash, hard_forks: Option<&HardForks>) -> u16 {
|
|
|
|
use byteorder::{ByteOrder, LittleEndian};
|
|
|
|
|
|
|
|
let mut hash = *genesis_hash;
|
|
|
|
if let Some(hard_forks) = hard_forks {
|
|
|
|
for (slot, count) in hard_forks.iter() {
|
|
|
|
let mut buf = [0u8; 16];
|
|
|
|
LittleEndian::write_u64(&mut buf[..8], *slot);
|
|
|
|
LittleEndian::write_u64(&mut buf[8..], *count as u64);
|
|
|
|
hash = extend_and_hash(&hash, &buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-24 09:18:08 -08:00
|
|
|
version_from_hash(&hash)
|
2020-02-07 08:57:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_compute_shred_version() {
|
|
|
|
assert_eq!(compute_shred_version(&Hash::default(), None), 1);
|
|
|
|
let mut hard_forks = HardForks::default();
|
|
|
|
assert_eq!(
|
|
|
|
compute_shred_version(&Hash::default(), Some(&hard_forks)),
|
|
|
|
1
|
|
|
|
);
|
|
|
|
hard_forks.register(1);
|
|
|
|
assert_eq!(
|
|
|
|
compute_shred_version(&Hash::default(), Some(&hard_forks)),
|
|
|
|
55551
|
|
|
|
);
|
|
|
|
hard_forks.register(1);
|
|
|
|
assert_eq!(
|
|
|
|
compute_shred_version(&Hash::default(), Some(&hard_forks)),
|
|
|
|
46353
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|