From e30e4cc603a143dc82be6a2e2d808ea8227390bd Mon Sep 17 00:00:00 2001 From: Michael Vines Date: Fri, 1 Mar 2019 13:13:32 -0800 Subject: [PATCH] Remove get_confirmation_timestamp() from HashQueue --- runtime/src/bank.rs | 26 +++++++++++++++++++++++--- runtime/src/hash_queue.rs | 26 +------------------------- src/leader_confirmation_service.rs | 14 +++----------- 3 files changed, 27 insertions(+), 39 deletions(-) diff --git a/runtime/src/bank.rs b/runtime/src/bank.rs index 41462347b6..1e59e18b28 100644 --- a/runtime/src/bank.rs +++ b/runtime/src/bank.rs @@ -319,11 +319,31 @@ impl Bank { /// tick that has achieved confirmation pub fn get_confirmation_timestamp( &self, - ticks_and_stakes: &mut [(u64, u64)], + mut slots_and_stakes: Vec<(u64, u64)>, supermajority_stake: u64, ) -> Option { - let hash_queue = self.tick_hash_queue.read().unwrap(); - hash_queue.get_confirmation_timestamp(ticks_and_stakes, supermajority_stake) + // Sort by slot height + slots_and_stakes.sort_by(|a, b| a.0.cmp(&b.0)); + + let max_slot = self.slot_height(); + let min_slot = + max_slot.saturating_sub(MAX_RECENT_TICK_HASHES as u64 / self.ticks_per_slot()); + + let mut total_stake = 0; + for (slot, stake) in slots_and_stakes.iter() { + if *slot >= min_slot && *slot <= max_slot { + total_stake += stake; + if total_stake > supermajority_stake { + return self + .tick_hash_queue + .read() + .unwrap() + .hash_height_to_timestamp(slot * self.ticks_per_slot()); + } + } + } + + None } /// Tell the bank which Entry IDs exist on the ledger. This function diff --git a/runtime/src/hash_queue.rs b/runtime/src/hash_queue.rs index c1cb531321..b30bb85159 100644 --- a/runtime/src/hash_queue.rs +++ b/runtime/src/hash_queue.rs @@ -88,32 +88,8 @@ impl HashQueue { self.last_hash = Some(*hash); } - /// Looks through a list of hash heights and stakes, and finds the latest - /// hash that has achieved confirmation - pub fn get_confirmation_timestamp( - &self, - hashes_and_stakes: &mut [(u64, u64)], - supermajority_stake: u64, - ) -> Option { - // Sort by hash height - hashes_and_stakes.sort_by(|a, b| a.0.cmp(&b.0)); - let current_hash_height = self.hash_height; - let mut total = 0; - for (hash_height, stake) in hashes_and_stakes.iter() { - if current_hash_height >= *hash_height - && ((current_hash_height - hash_height) as usize) < MAX_RECENT_TICK_HASHES - { - total += stake; - if total > supermajority_stake { - return self.hash_height_to_timestamp(*hash_height); - } - } - } - None - } - /// Maps a hash height to a timestamp - fn hash_height_to_timestamp(&self, hash_height: u64) -> Option { + pub fn hash_height_to_timestamp(&self, hash_height: u64) -> Option { for entry in self.entries.values() { if entry.hash_height == hash_height { return Some(entry.timestamp); diff --git a/src/leader_confirmation_service.rs b/src/leader_confirmation_service.rs index 6ca2f90e2d..57d1a0af39 100644 --- a/src/leader_confirmation_service.rs +++ b/src/leader_confirmation_service.rs @@ -37,30 +37,22 @@ impl LeaderConfirmationService { // the vote states let vote_states = bank.vote_states(|_, vote_state| leader_id != vote_state.delegate_id); - let mut ticks_and_stakes: Vec<(u64, u64)> = vote_states + let slots_and_stakes: Vec<(u64, u64)> = vote_states .iter() .filter_map(|(_, vote_state)| { let validator_stake = bank.get_balance(&vote_state.delegate_id); total_stake += validator_stake; - // Filter out any validators that don't have at least one vote - // by returning None vote_state .votes .back() - // A vote for a slot is like a vote for the last tick in that slot - .map(|vote| { - ( - (vote.slot_height + 1) * bank.ticks_per_slot() - 1, - validator_stake, - ) - }) + .map(|vote| (vote.slot_height, validator_stake)) }) .collect(); let super_majority_stake = (2 * total_stake) / 3; if let Some(last_valid_validator_timestamp) = - bank.get_confirmation_timestamp(&mut ticks_and_stakes, super_majority_stake) + bank.get_confirmation_timestamp(slots_and_stakes, super_majority_stake) { return Ok(last_valid_validator_timestamp); }