Rename PohEntry.id to PohEntry.hash

This commit is contained in:
Michael Vines 2019-03-01 09:21:58 -08:00
parent c53c351759
commit 558f10c862
3 changed files with 32 additions and 32 deletions

View File

@ -218,9 +218,9 @@ fn next_hash(start_hash: &Hash, num_hashes: u64, transactions: &[Transaction]) -
} }
if transactions.is_empty() { if transactions.is_empty() {
poh.tick().id poh.tick().hash
} else { } else {
poh.record(Transaction::hash(transactions)).id poh.record(Transaction::hash(transactions)).hash
} }
} }

View File

@ -1,9 +1,9 @@
//! The `Poh` module provides an object for generating a Proof of History. //! The `Poh` module provhashes an object for generating a Proof of History.
//! It records Hashes items on behalf of its users. //! It records Hashes items on behalf of its users.
use solana_sdk::hash::{hash, hashv, Hash}; use solana_sdk::hash::{hash, hashv, Hash};
pub struct Poh { pub struct Poh {
pub id: Hash, pub hash: Hash,
num_hashes: u64, num_hashes: u64,
pub tick_height: u64, pub tick_height: u64,
} }
@ -12,26 +12,26 @@ pub struct Poh {
pub struct PohEntry { pub struct PohEntry {
pub tick_height: u64, pub tick_height: u64,
pub num_hashes: u64, pub num_hashes: u64,
pub id: Hash, pub hash: Hash,
pub mixin: Option<Hash>, pub mixin: Option<Hash>,
} }
impl Poh { impl Poh {
pub fn new(id: Hash, tick_height: u64) -> Self { pub fn new(hash: Hash, tick_height: u64) -> Self {
Poh { Poh {
num_hashes: 0, num_hashes: 0,
id, hash,
tick_height, tick_height,
} }
} }
pub fn hash(&mut self) { pub fn hash(&mut self) {
self.id = hash(&self.id.as_ref()); self.hash = hash(&self.hash.as_ref());
self.num_hashes += 1; self.num_hashes += 1;
} }
pub fn record(&mut self, mixin: Hash) -> PohEntry { pub fn record(&mut self, mixin: Hash) -> PohEntry {
self.id = hashv(&[&self.id.as_ref(), &mixin.as_ref()]); self.hash = hashv(&[&self.hash.as_ref(), &mixin.as_ref()]);
let num_hashes = self.num_hashes + 1; let num_hashes = self.num_hashes + 1;
self.num_hashes = 0; self.num_hashes = 0;
@ -39,13 +39,13 @@ impl Poh {
PohEntry { PohEntry {
tick_height: self.tick_height, tick_height: self.tick_height,
num_hashes, num_hashes,
id: self.id, hash: self.hash,
mixin: Some(mixin), mixin: Some(mixin),
} }
} }
// emissions of Ticks (i.e. PohEntries without a mixin) allows // emissions of Ticks (i.e. PohEntries without a mixin) allows
// validators to parallelize the work of catching up // valhashators to parallelize the work of catching up
pub fn tick(&mut self) -> PohEntry { pub fn tick(&mut self) -> PohEntry {
self.hash(); self.hash();
@ -56,27 +56,27 @@ impl Poh {
PohEntry { PohEntry {
tick_height: self.tick_height, tick_height: self.tick_height,
num_hashes, num_hashes,
id: self.id, hash: self.hash,
mixin: None, mixin: None,
} }
} }
} }
#[cfg(test)] #[cfg(test)]
pub fn verify(initial: Hash, entries: &[PohEntry]) -> bool { pub fn verify(initial_hash: Hash, entries: &[PohEntry]) -> bool {
let mut id = initial; let mut current_hash = initial_hash;
for entry in entries { for entry in entries {
assert!(entry.num_hashes != 0); assert!(entry.num_hashes != 0);
for _ in 1..entry.num_hashes { for _ in 1..entry.num_hashes {
id = hash(&id.as_ref()); current_hash = hash(&current_hash.as_ref());
} }
id = match entry.mixin { current_hash = match entry.mixin {
Some(mixin) => hashv(&[&id.as_ref(), &mixin.as_ref()]), Some(mixin) => hashv(&[&current_hash.as_ref(), &mixin.as_ref()]),
None => hash(&id.as_ref()), None => hash(&current_hash.as_ref()),
}; };
if id != entry.id { if current_hash != entry.hash {
return false; return false;
} }
} }
@ -111,7 +111,7 @@ mod tests {
&[PohEntry { &[PohEntry {
tick_height: 0, tick_height: 0,
num_hashes: 1, num_hashes: 1,
id: one, hash: one,
mixin: None, mixin: None,
}], }],
), ),
@ -123,7 +123,7 @@ mod tests {
&[PohEntry { &[PohEntry {
tick_height: 0, tick_height: 0,
num_hashes: 2, num_hashes: 2,
id: two, hash: two,
mixin: None, mixin: None,
}] }]
), ),
@ -136,7 +136,7 @@ mod tests {
&[PohEntry { &[PohEntry {
tick_height: 0, tick_height: 0,
num_hashes: 1, num_hashes: 1,
id: one_with_zero, hash: one_with_zero,
mixin: Some(zero), mixin: Some(zero),
}] }]
), ),
@ -148,7 +148,7 @@ mod tests {
&[PohEntry { &[PohEntry {
tick_height: 0, tick_height: 0,
num_hashes: 1, num_hashes: 1,
id: zero, hash: zero,
mixin: None mixin: None
}] }]
), ),
@ -162,13 +162,13 @@ mod tests {
PohEntry { PohEntry {
tick_height: 0, tick_height: 0,
num_hashes: 1, num_hashes: 1,
id: one_with_zero, hash: one_with_zero,
mixin: Some(zero), mixin: Some(zero),
}, },
PohEntry { PohEntry {
tick_height: 0, tick_height: 0,
num_hashes: 1, num_hashes: 1,
id: hash(&one_with_zero.as_ref()), hash: hash(&one_with_zero.as_ref()),
mixin: None mixin: None
}, },
] ]
@ -185,7 +185,7 @@ mod tests {
&[PohEntry { &[PohEntry {
tick_height: 0, tick_height: 0,
num_hashes: 0, num_hashes: 0,
id: Hash::default(), hash: Hash::default(),
mixin: None, mixin: None,
}], }],
); );

View File

@ -56,7 +56,7 @@ impl PohRecorder {
let mut cache = vec![]; let mut cache = vec![];
info!( info!(
"reset poh from: {},{} to: {},{}", "reset poh from: {},{} to: {},{}",
self.poh.id, self.poh.tick_height, last_id, tick_height, self.poh.hash, self.poh.tick_height, last_id, tick_height,
); );
std::mem::swap(&mut cache, &mut self.tick_cache); std::mem::swap(&mut cache, &mut self.tick_cache);
self.poh = Poh::new(last_id, tick_height); self.poh = Poh::new(last_id, tick_height);
@ -158,17 +158,17 @@ impl PohRecorder {
.working_bank .working_bank
.as_ref() .as_ref()
.ok_or(Error::PohRecorderError(PohRecorderError::MaxHeightReached))?; .ok_or(Error::PohRecorderError(PohRecorderError::MaxHeightReached))?;
let entry = self.poh.record(mixin); let poh_entry = self.poh.record(mixin);
assert!(!txs.is_empty(), "Entries without transactions are used to track real-time passing in the ledger and can only be generated with PohRecorder::tick function"); assert!(!txs.is_empty(), "Entries without transactions are used to track real-time passing in the ledger and can only be generated with PohRecorder::tick function");
let recorded_entry = Entry { let recorded_entry = Entry {
num_hashes: entry.num_hashes, num_hashes: poh_entry.num_hashes,
hash: entry.id, hash: poh_entry.hash,
transactions: txs, transactions: txs,
}; };
trace!("sending entry {}", recorded_entry.is_tick()); trace!("sending entry {}", recorded_entry.is_tick());
working_bank working_bank
.sender .sender
.send(vec![(recorded_entry, entry.tick_height)])?; .send(vec![(recorded_entry, poh_entry.tick_height)])?;
Ok(()) Ok(())
} }
@ -178,7 +178,7 @@ impl PohRecorder {
( (
Entry { Entry {
num_hashes: tick.num_hashes, num_hashes: tick.num_hashes,
hash: tick.id, hash: tick.hash,
transactions: vec![], transactions: vec![],
}, },
tick.tick_height, tick.tick_height,