Remove get_confirmation_timestamp() from HashQueue
This commit is contained in:
parent
fdc31e99df
commit
e30e4cc603
|
@ -319,11 +319,31 @@ impl Bank {
|
||||||
/// tick that has achieved confirmation
|
/// tick that has achieved confirmation
|
||||||
pub fn get_confirmation_timestamp(
|
pub fn get_confirmation_timestamp(
|
||||||
&self,
|
&self,
|
||||||
ticks_and_stakes: &mut [(u64, u64)],
|
mut slots_and_stakes: Vec<(u64, u64)>,
|
||||||
supermajority_stake: u64,
|
supermajority_stake: u64,
|
||||||
) -> Option<u64> {
|
) -> Option<u64> {
|
||||||
let hash_queue = self.tick_hash_queue.read().unwrap();
|
// Sort by slot height
|
||||||
hash_queue.get_confirmation_timestamp(ticks_and_stakes, supermajority_stake)
|
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
|
/// Tell the bank which Entry IDs exist on the ledger. This function
|
||||||
|
|
|
@ -88,32 +88,8 @@ impl HashQueue {
|
||||||
self.last_hash = Some(*hash);
|
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<u64> {
|
|
||||||
// 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
|
/// Maps a hash height to a timestamp
|
||||||
fn hash_height_to_timestamp(&self, hash_height: u64) -> Option<u64> {
|
pub fn hash_height_to_timestamp(&self, hash_height: u64) -> Option<u64> {
|
||||||
for entry in self.entries.values() {
|
for entry in self.entries.values() {
|
||||||
if entry.hash_height == hash_height {
|
if entry.hash_height == hash_height {
|
||||||
return Some(entry.timestamp);
|
return Some(entry.timestamp);
|
||||||
|
|
|
@ -37,30 +37,22 @@ impl LeaderConfirmationService {
|
||||||
// the vote states
|
// the vote states
|
||||||
let vote_states = bank.vote_states(|_, vote_state| leader_id != vote_state.delegate_id);
|
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()
|
.iter()
|
||||||
.filter_map(|(_, vote_state)| {
|
.filter_map(|(_, vote_state)| {
|
||||||
let validator_stake = bank.get_balance(&vote_state.delegate_id);
|
let validator_stake = bank.get_balance(&vote_state.delegate_id);
|
||||||
total_stake += validator_stake;
|
total_stake += validator_stake;
|
||||||
// Filter out any validators that don't have at least one vote
|
|
||||||
// by returning None
|
|
||||||
vote_state
|
vote_state
|
||||||
.votes
|
.votes
|
||||||
.back()
|
.back()
|
||||||
// A vote for a slot is like a vote for the last tick in that slot
|
.map(|vote| (vote.slot_height, validator_stake))
|
||||||
.map(|vote| {
|
|
||||||
(
|
|
||||||
(vote.slot_height + 1) * bank.ticks_per_slot() - 1,
|
|
||||||
validator_stake,
|
|
||||||
)
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let super_majority_stake = (2 * total_stake) / 3;
|
let super_majority_stake = (2 * total_stake) / 3;
|
||||||
|
|
||||||
if let Some(last_valid_validator_timestamp) =
|
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);
|
return Ok(last_valid_validator_timestamp);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue