add check_tick_height (#2144)

This commit is contained in:
Rob Walker 2018-12-12 18:52:11 -08:00 committed by GitHub
parent 59a094cb77
commit 3408ce89a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 26 additions and 25 deletions

View File

@ -29,12 +29,12 @@ impl PohRecorder {
// TODO: amortize the cost of this lock by doing the loop in here for // TODO: amortize the cost of this lock by doing the loop in here for
// some min amount of hashes // some min amount of hashes
let mut poh = self.poh.lock().unwrap(); let mut poh = self.poh.lock().unwrap();
if self.is_max_tick_height_reached(&poh) {
Err(Error::PohRecorderError(PohRecorderError::MaxHeightReached)) self.check_tick_height(&poh)?;
} else {
poh.hash(); poh.hash();
Ok(())
} Ok(())
} }
pub fn tick(&mut self) -> Result<()> { pub fn tick(&mut self) -> Result<()> {
@ -42,24 +42,20 @@ impl PohRecorder {
// hasn't been reached. // hasn't been reached.
// This guarantees PoH order and Entry production and banks LastId queue is the same // This guarantees PoH order and Entry production and banks LastId queue is the same
let mut poh = self.poh.lock().unwrap(); let mut poh = self.poh.lock().unwrap();
if self.is_max_tick_height_reached(&poh) {
Err(Error::PohRecorderError(PohRecorderError::MaxHeightReached)) self.check_tick_height(&poh)?;
} else {
self.register_and_send_tick(&mut *poh)?; self.register_and_send_tick(&mut *poh)
Ok(())
}
} }
pub fn record(&self, mixin: Hash, txs: Vec<Transaction>) -> Result<()> { pub fn record(&self, mixin: Hash, txs: Vec<Transaction>) -> Result<()> {
// Register and send the entry out while holding the lock. // Register and send the entry out while holding the lock.
// This guarantees PoH order and Entry production and banks LastId queue is the same. // This guarantees PoH order and Entry production and banks LastId queue is the same.
let mut poh = self.poh.lock().unwrap(); let mut poh = self.poh.lock().unwrap();
if self.is_max_tick_height_reached(&poh) {
Err(Error::PohRecorderError(PohRecorderError::MaxHeightReached)) self.check_tick_height(&poh)?;
} else {
self.record_and_send_txs(&mut *poh, mixin, txs)?; self.record_and_send_txs(&mut *poh, mixin, txs)
Ok(())
}
} }
/// A recorder to synchronize PoH with the following data structures /// A recorder to synchronize PoH with the following data structures
@ -80,11 +76,12 @@ impl PohRecorder {
} }
} }
fn is_max_tick_height_reached(&self, poh: &Poh) -> bool { fn check_tick_height(&self, poh: &Poh) -> Result<()> {
if let Some(max_tick_height) = self.max_tick_height { match self.max_tick_height {
poh.tick_height >= max_tick_height Some(max_tick_height) if poh.tick_height >= max_tick_height => {
} else { Err(Error::PohRecorderError(PohRecorderError::MaxHeightReached))
false }
_ => Ok(()),
} }
} }
@ -132,12 +129,12 @@ mod tests {
let bank = Arc::new(Bank::new(&mint)); let bank = Arc::new(Bank::new(&mint));
let prev_id = bank.last_id(); let prev_id = bank.last_id();
let (entry_sender, entry_receiver) = channel(); let (entry_sender, entry_receiver) = channel();
let mut poh_recorder = PohRecorder::new(bank, entry_sender, prev_id, None); let mut poh_recorder = PohRecorder::new(bank, entry_sender, prev_id, Some(3));
//send some data //send some data
let h1 = hash(b"hello world!"); let h1 = hash(b"hello world!");
let tx = test_tx(); let tx = test_tx();
assert!(poh_recorder.record(h1, vec![tx]).is_ok()); assert!(poh_recorder.record(h1, vec![tx.clone()]).is_ok());
//get some events //get some events
let e = entry_receiver.recv().unwrap(); let e = entry_receiver.recv().unwrap();
assert_eq!(e[0].tick_height, 1); assert_eq!(e[0].tick_height, 1);
@ -150,6 +147,10 @@ mod tests {
let e = entry_receiver.recv().unwrap(); let e = entry_receiver.recv().unwrap();
assert_eq!(e[0].tick_height, 2); assert_eq!(e[0].tick_height, 2);
// max tick height reached
assert!(poh_recorder.tick().is_err());
assert!(poh_recorder.record(h1, vec![tx]).is_err());
//make sure it handles channel close correctly //make sure it handles channel close correctly
drop(entry_receiver); drop(entry_receiver);
assert!(poh_recorder.tick().is_err()); assert!(poh_recorder.tick().is_err());