Generalize tick_height to hash_height

This commit is contained in:
Michael Vines 2019-03-01 11:36:09 -08:00
parent 31f570a9f4
commit 8ec13d557f
2 changed files with 28 additions and 28 deletions

View File

@ -337,11 +337,11 @@ impl Bank {
// TODO: put this assert back in // TODO: put this assert back in
// assert!(!self.is_frozen()); // assert!(!self.is_frozen());
let current_tick_height = { let current_tick_height = {
//atomic register and read the tick // Atomic register and read the tick
let mut tick_hash_queue = self.tick_hash_queue.write().unwrap(); let mut tick_hash_queue = self.tick_hash_queue.write().unwrap();
inc_new_counter_info!("bank-register_tick-registered", 1); inc_new_counter_info!("bank-register_tick-registered", 1);
tick_hash_queue.register_hash(hash); tick_hash_queue.register_hash(hash);
tick_hash_queue.tick_height() tick_hash_queue.hash_height()
}; };
if current_tick_height % NUM_TICKS_PER_SECOND as u64 == 0 { if current_tick_height % NUM_TICKS_PER_SECOND as u64 == 0 {
self.status_cache.write().unwrap().new_cache(hash); self.status_cache.write().unwrap().new_cache(hash);
@ -722,7 +722,7 @@ impl Bank {
/// Return the number of ticks since genesis. /// Return the number of ticks since genesis.
pub fn tick_height(&self) -> u64 { pub fn tick_height(&self) -> u64 {
self.tick_hash_queue.read().unwrap().tick_height() self.tick_hash_queue.read().unwrap().hash_height()
} }
/// Return the number of ticks since the last slot boundary. /// Return the number of ticks since the last slot boundary.

View File

@ -5,14 +5,14 @@ use solana_sdk::timing::{timestamp, MAX_ENTRY_IDS};
#[derive(Debug, PartialEq, Eq, Clone)] #[derive(Debug, PartialEq, Eq, Clone)]
struct HashQueueEntry { struct HashQueueEntry {
timestamp: u64, timestamp: u64,
tick_height: u64, hash_height: u64,
} }
/// Low memory overhead, so can be cloned for every checkpoint /// Low memory overhead, so can be cloned for every checkpoint
#[derive(Clone)] #[derive(Clone)]
pub struct HashQueue { pub struct HashQueue {
/// updated whenever an id is registered, at each tick ;) /// updated whenever an hash is registered
tick_height: u64, hash_height: u64,
/// last hash to be registered /// last hash to be registered
last_hash: Option<Hash>, last_hash: Option<Hash>,
@ -23,15 +23,15 @@ impl Default for HashQueue {
fn default() -> Self { fn default() -> Self {
Self { Self {
entries: HashMap::new(), entries: HashMap::new(),
tick_height: 0, hash_height: 0,
last_hash: None, last_hash: None,
} }
} }
} }
impl HashQueue { impl HashQueue {
pub fn tick_height(&self) -> u64 { pub fn hash_height(&self) -> u64 {
self.tick_height self.hash_height
} }
pub fn last_hash(&self) -> Hash { pub fn last_hash(&self) -> Hash {
@ -43,7 +43,7 @@ impl HashQueue {
pub fn check_entry_id_age(&self, entry_id: Hash, max_age: usize) -> bool { pub fn check_entry_id_age(&self, entry_id: Hash, max_age: usize) -> bool {
let entry = self.entries.get(&entry_id); let entry = self.entries.get(&entry_id);
match entry { match entry {
Some(entry) => self.tick_height - entry.tick_height < max_age as u64, Some(entry) => self.hash_height - entry.hash_height < max_age as u64,
_ => false, _ => false,
} }
} }
@ -57,7 +57,7 @@ impl HashQueue {
self.entries.insert( self.entries.insert(
*hash, *hash,
HashQueueEntry { HashQueueEntry {
tick_height: 0, hash_height: 0,
timestamp: timestamp(), timestamp: timestamp(),
}, },
); );
@ -66,20 +66,20 @@ impl HashQueue {
} }
pub fn register_hash(&mut self, hash: &Hash) { pub fn register_hash(&mut self, hash: &Hash) {
self.tick_height += 1; self.hash_height += 1;
let tick_height = self.tick_height; let hash_height = self.hash_height;
// this clean up can be deferred until sigs gets larger // this clean up can be deferred until sigs gets larger
// because we verify entry.nth every place we check for validity // because we verify entry.nth every place we check for validity
if self.entries.len() >= MAX_ENTRY_IDS as usize { if self.entries.len() >= MAX_ENTRY_IDS as usize {
self.entries self.entries
.retain(|_, entry| tick_height - entry.tick_height <= MAX_ENTRY_IDS as u64); .retain(|_, entry| hash_height - entry.hash_height <= MAX_ENTRY_IDS as u64);
} }
self.entries.insert( self.entries.insert(
*hash, *hash,
HashQueueEntry { HashQueueEntry {
tick_height, hash_height,
timestamp: timestamp(), timestamp: timestamp(),
}, },
); );
@ -87,34 +87,34 @@ impl HashQueue {
self.last_hash = Some(*hash); self.last_hash = Some(*hash);
} }
/// Looks through a list of tick heights and stakes, and finds the latest /// Looks through a list of hash heights and stakes, and finds the latest
/// tick that has achieved confirmation /// hash that has achieved confirmation
pub fn get_confirmation_timestamp( pub fn get_confirmation_timestamp(
&self, &self,
ticks_and_stakes: &mut [(u64, u64)], hashes_and_stakes: &mut [(u64, u64)],
supermajority_stake: u64, supermajority_stake: u64,
) -> Option<u64> { ) -> Option<u64> {
// Sort by tick height // Sort by hash height
ticks_and_stakes.sort_by(|a, b| a.0.cmp(&b.0)); hashes_and_stakes.sort_by(|a, b| a.0.cmp(&b.0));
let current_tick_height = self.tick_height; let current_hash_height = self.hash_height;
let mut total = 0; let mut total = 0;
for (tick_height, stake) in ticks_and_stakes.iter() { for (hash_height, stake) in hashes_and_stakes.iter() {
if current_tick_height >= *tick_height if current_hash_height >= *hash_height
&& ((current_tick_height - tick_height) as usize) < MAX_ENTRY_IDS && ((current_hash_height - hash_height) as usize) < MAX_ENTRY_IDS
{ {
total += stake; total += stake;
if total > supermajority_stake { if total > supermajority_stake {
return self.tick_height_to_timestamp(*tick_height); return self.hash_height_to_timestamp(*hash_height);
} }
} }
} }
None None
} }
/// Maps a tick height to a timestamp /// Maps a hash height to a timestamp
fn tick_height_to_timestamp(&self, tick_height: u64) -> Option<u64> { 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.tick_height == tick_height { if entry.hash_height == hash_height {
return Some(entry.timestamp); return Some(entry.timestamp);
} }
} }