Fix poh recorder not flushing virtual ticks immediately (#5277)

* Fix poh not flushing virtual ticks immediately

* Fix test_would_be_leader_soon
This commit is contained in:
Sagar Dhawan 2019-07-25 11:08:44 -07:00 committed by GitHub
parent 27bc0a22dd
commit a233a1c822
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 0 deletions

View File

@ -203,6 +203,7 @@ impl PohRecorder {
trace!("new working bank");
assert_eq!(working_bank.bank.ticks_per_slot(), self.ticks_per_slot());
self.working_bank = Some(working_bank);
let _ = self.flush_cache(false);
}
pub fn set_bank(&mut self, bank: &Arc<Bank>) {
let working_bank = WorkingBank {
@ -1218,6 +1219,8 @@ mod tests {
false
);
// Move the bank up a slot (so that max_tick_height > slot 0's tick_height)
let bank = Arc::new(Bank::new_from_parent(&bank, &Pubkey::default(), 1));
// If we set the working bank, the node should be leader within next 2 slots
poh_recorder.set_bank(&bank);
assert_eq!(
@ -1226,4 +1229,37 @@ mod tests {
);
}
}
#[test]
fn test_flush_virtual_ticks() {
let ledger_path = get_tmp_ledger_path!();
{
// test that virtual ticks are flushed into a newly set bank asap
let blocktree =
Blocktree::open(&ledger_path).expect("Expected to be able to open database ledger");
let GenesisBlockInfo { genesis_block, .. } = create_genesis_block(2);
let bank = Arc::new(Bank::new(&genesis_block));
let genesis_blockhash = bank.last_blockhash();
let (mut poh_recorder, _entry_receiver) = PohRecorder::new(
0,
bank.last_blockhash(),
0,
Some(2),
bank.ticks_per_slot(),
&Pubkey::default(),
&Arc::new(blocktree),
&Arc::new(LeaderScheduleCache::new_from_bank(&bank)),
&Arc::new(PohConfig::default()),
);
//create a new bank
let bank = Arc::new(Bank::new_from_parent(&bank, &Pubkey::default(), 2));
//put 2 slots worth of virtual ticks into poh
for _ in 0..(bank.ticks_per_slot() * 2) {
poh_recorder.tick();
}
poh_recorder.set_bank(&bank.clone());
assert!(!bank.check_hash_age(&genesis_blockhash, 1));
}
}
}

View File

@ -876,6 +876,13 @@ impl Bank {
.collect()
}
pub fn check_hash_age(&self, hash: &Hash, max_age: usize) -> bool {
self.blockhash_queue
.read()
.unwrap()
.check_hash_age(hash, max_age)
}
pub fn check_transactions(
&self,
txs: &[Transaction],