solana/core/src/poh.rs

195 lines
4.6 KiB
Rust
Raw Normal View History

2019-03-01 09:21:58 -08:00
//! The `Poh` module provhashes an object for generating a Proof of History.
//! It records Hashes items on behalf of its users.
2018-11-16 08:04:46 -08:00
use solana_sdk::hash::{hash, hashv, Hash};
pub struct Poh {
2019-03-01 09:21:58 -08:00
pub hash: Hash,
num_hashes: u64,
pub tick_height: u64,
}
#[derive(Debug)]
pub struct PohEntry {
pub tick_height: u64,
pub num_hashes: u64,
2019-03-01 09:21:58 -08:00
pub hash: Hash,
pub mixin: Option<Hash>,
}
impl Poh {
2019-03-01 09:21:58 -08:00
pub fn new(hash: Hash, tick_height: u64) -> Self {
Poh {
num_hashes: 0,
2019-03-01 09:21:58 -08:00
hash,
tick_height,
}
}
pub fn hash(&mut self) {
2019-03-01 09:21:58 -08:00
self.hash = hash(&self.hash.as_ref());
self.num_hashes += 1;
}
pub fn record(&mut self, mixin: Hash) -> PohEntry {
2019-03-01 09:21:58 -08:00
self.hash = hashv(&[&self.hash.as_ref(), &mixin.as_ref()]);
let num_hashes = self.num_hashes + 1;
self.num_hashes = 0;
PohEntry {
tick_height: self.tick_height,
num_hashes,
2019-03-01 09:21:58 -08:00
hash: self.hash,
mixin: Some(mixin),
}
}
// emissions of Ticks (i.e. PohEntries without a mixin) allows
2019-05-13 15:29:23 -07:00
// validators to parallelize the work of catching up
pub fn tick(&mut self) -> PohEntry {
self.hash();
let num_hashes = self.num_hashes;
self.num_hashes = 0;
self.tick_height += 1;
PohEntry {
tick_height: self.tick_height,
num_hashes,
2019-03-01 09:21:58 -08:00
hash: self.hash,
mixin: None,
}
}
}
#[cfg(test)]
2019-03-01 09:21:58 -08:00
pub fn verify(initial_hash: Hash, entries: &[PohEntry]) -> bool {
let mut current_hash = initial_hash;
for entry in entries {
assert!(entry.num_hashes != 0);
for _ in 1..entry.num_hashes {
2019-03-01 09:21:58 -08:00
current_hash = hash(&current_hash.as_ref());
}
2019-03-01 09:21:58 -08:00
current_hash = match entry.mixin {
Some(mixin) => hashv(&[&current_hash.as_ref(), &mixin.as_ref()]),
None => hash(&current_hash.as_ref()),
};
2019-03-01 09:21:58 -08:00
if current_hash != entry.hash {
return false;
}
}
2018-09-23 14:38:17 -07:00
true
}
#[cfg(test)]
mod tests {
2018-12-18 17:27:03 -08:00
use crate::poh::{verify, Poh, PohEntry};
2018-12-18 16:44:27 -08:00
use solana_sdk::hash::{hash, hashv, Hash};
2018-12-18 17:27:03 -08:00
2018-12-18 16:44:27 -08:00
#[test]
fn test_poh_verify() {
let zero = Hash::default();
let one = hash(&zero.as_ref());
let two = hash(&one.as_ref());
let one_with_zero = hashv(&[&zero.as_ref(), &zero.as_ref()]);
2018-12-18 17:27:03 -08:00
let mut poh = Poh::new(zero, 0);
assert_eq!(
verify(
zero,
&[poh.tick(), poh.record(zero), poh.record(zero), poh.tick(),],
),
true
);
2018-12-18 16:44:27 -08:00
assert_eq!(
2018-12-18 17:27:03 -08:00
verify(
2018-12-18 16:44:27 -08:00
zero,
&[PohEntry {
tick_height: 0,
num_hashes: 1,
2019-03-01 09:21:58 -08:00
hash: one,
2018-12-18 16:44:27 -08:00
mixin: None,
}],
),
true
);
assert_eq!(
2018-12-18 17:27:03 -08:00
verify(
2018-12-18 16:44:27 -08:00
zero,
&[PohEntry {
tick_height: 0,
num_hashes: 2,
2019-03-01 09:21:58 -08:00
hash: two,
2018-12-18 16:44:27 -08:00
mixin: None,
}]
),
true
);
assert_eq!(
2018-12-18 17:27:03 -08:00
verify(
2018-12-18 16:44:27 -08:00
zero,
&[PohEntry {
tick_height: 0,
num_hashes: 1,
2019-03-01 09:21:58 -08:00
hash: one_with_zero,
2018-12-18 16:44:27 -08:00
mixin: Some(zero),
}]
),
true
);
assert_eq!(
2018-12-18 17:27:03 -08:00
verify(
2018-12-18 16:44:27 -08:00
zero,
&[PohEntry {
tick_height: 0,
num_hashes: 1,
2019-03-01 09:21:58 -08:00
hash: zero,
2018-12-18 16:44:27 -08:00
mixin: None
}]
),
false
);
assert_eq!(
2018-12-18 17:27:03 -08:00
verify(
2018-12-18 16:44:27 -08:00
zero,
&[
PohEntry {
tick_height: 0,
num_hashes: 1,
2019-03-01 09:21:58 -08:00
hash: one_with_zero,
2018-12-18 16:44:27 -08:00
mixin: Some(zero),
},
PohEntry {
tick_height: 0,
num_hashes: 1,
2019-03-01 09:21:58 -08:00
hash: hash(&one_with_zero.as_ref()),
2018-12-18 16:44:27 -08:00
mixin: None
},
]
),
true
);
}
#[test]
#[should_panic]
fn test_poh_verify_assert() {
2018-12-18 17:27:03 -08:00
verify(
Hash::default(),
&[PohEntry {
tick_height: 0,
num_hashes: 0,
2019-03-01 09:21:58 -08:00
hash: Hash::default(),
mixin: None,
}],
);
}
}