2019-03-01 09:21:58 -08:00
|
|
|
//! The `Poh` module provhashes an object for generating a Proof of History.
|
2018-09-20 15:02:24 -07:00
|
|
|
//! It records Hashes items on behalf of its users.
|
2018-11-16 08:04:46 -08:00
|
|
|
use solana_sdk::hash::{hash, hashv, Hash};
|
2018-09-20 15:02:24 -07:00
|
|
|
|
|
|
|
pub struct Poh {
|
2019-03-01 09:21:58 -08:00
|
|
|
pub hash: Hash,
|
2018-09-20 15:02:24 -07:00
|
|
|
num_hashes: u64,
|
2018-10-18 22:57:48 -07:00
|
|
|
pub tick_height: u64,
|
2018-09-20 15:02:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct PohEntry {
|
2018-12-10 20:03:04 -08:00
|
|
|
pub tick_height: u64,
|
2018-09-20 15:02:24 -07:00
|
|
|
pub num_hashes: u64,
|
2019-03-01 09:21:58 -08:00
|
|
|
pub hash: Hash,
|
2018-09-20 15:02:24 -07:00
|
|
|
pub mixin: Option<Hash>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Poh {
|
2019-03-01 09:21:58 -08:00
|
|
|
pub fn new(hash: Hash, tick_height: u64) -> Self {
|
2018-09-20 15:02:24 -07:00
|
|
|
Poh {
|
|
|
|
num_hashes: 0,
|
2019-03-01 09:21:58 -08:00
|
|
|
hash,
|
2018-10-18 22:57:48 -07:00
|
|
|
tick_height,
|
2018-09-20 15:02:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn hash(&mut self) {
|
2019-03-01 09:21:58 -08:00
|
|
|
self.hash = hash(&self.hash.as_ref());
|
2018-09-20 15:02:24 -07:00
|
|
|
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()]);
|
2018-11-12 17:03:23 -08:00
|
|
|
|
|
|
|
let num_hashes = self.num_hashes + 1;
|
2018-09-25 15:01:51 -07:00
|
|
|
self.num_hashes = 0;
|
2018-11-12 17:03:23 -08:00
|
|
|
|
2018-09-20 15:02:24 -07:00
|
|
|
PohEntry {
|
2018-12-10 20:03:04 -08:00
|
|
|
tick_height: self.tick_height,
|
2018-09-20 15:02:24 -07:00
|
|
|
num_hashes,
|
2019-03-01 09:21:58 -08:00
|
|
|
hash: self.hash,
|
2018-09-20 15:02:24 -07:00
|
|
|
mixin: Some(mixin),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// emissions of Ticks (i.e. PohEntries without a mixin) allows
|
2019-03-01 09:21:58 -08:00
|
|
|
// valhashators to parallelize the work of catching up
|
2018-09-25 15:01:51 -07:00
|
|
|
pub fn tick(&mut self) -> PohEntry {
|
|
|
|
self.hash();
|
|
|
|
|
|
|
|
let num_hashes = self.num_hashes;
|
|
|
|
self.num_hashes = 0;
|
2018-10-18 22:57:48 -07:00
|
|
|
self.tick_height += 1;
|
2018-09-25 15:01:51 -07:00
|
|
|
|
|
|
|
PohEntry {
|
2018-12-14 02:25:50 -08:00
|
|
|
tick_height: self.tick_height,
|
2018-09-25 15:01:51 -07:00
|
|
|
num_hashes,
|
2019-03-01 09:21:58 -08:00
|
|
|
hash: self.hash,
|
2018-09-25 15:01:51 -07:00
|
|
|
mixin: None,
|
2018-09-20 15:02:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-25 15:01:51 -07:00
|
|
|
#[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;
|
2018-09-20 15:02:24 -07:00
|
|
|
|
|
|
|
for entry in entries {
|
|
|
|
assert!(entry.num_hashes != 0);
|
2018-11-12 17:03:23 -08:00
|
|
|
|
2018-09-20 15:02:24 -07:00
|
|
|
for _ in 1..entry.num_hashes {
|
2019-03-01 09:21:58 -08:00
|
|
|
current_hash = hash(¤t_hash.as_ref());
|
2018-09-20 15:02:24 -07:00
|
|
|
}
|
2019-03-01 09:21:58 -08:00
|
|
|
current_hash = match entry.mixin {
|
|
|
|
Some(mixin) => hashv(&[¤t_hash.as_ref(), &mixin.as_ref()]),
|
|
|
|
None => hash(¤t_hash.as_ref()),
|
2018-09-20 15:02:24 -07:00
|
|
|
};
|
2019-03-01 09:21:58 -08:00
|
|
|
if current_hash != entry.hash {
|
2018-09-20 15:02:24 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 14:38:17 -07:00
|
|
|
true
|
2018-09-20 15:02:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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
|
|
|
|
);
|
|
|
|
}
|
2018-09-20 15:02:24 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn test_poh_verify_assert() {
|
2018-12-18 17:27:03 -08:00
|
|
|
verify(
|
2018-09-20 15:02:24 -07:00
|
|
|
Hash::default(),
|
|
|
|
&[PohEntry {
|
2018-12-10 20:03:04 -08:00
|
|
|
tick_height: 0,
|
2018-09-20 15:02:24 -07:00
|
|
|
num_hashes: 0,
|
2019-03-01 09:21:58 -08:00
|
|
|
hash: Hash::default(),
|
2018-09-20 15:02:24 -07:00
|
|
|
mixin: None,
|
|
|
|
}],
|
|
|
|
);
|
|
|
|
}
|
2018-11-12 17:03:23 -08:00
|
|
|
|
2018-09-20 15:02:24 -07:00
|
|
|
}
|