From 46fb0b1b94cde458f7b603dfcb41c3b8c041279e Mon Sep 17 00:00:00 2001 From: Michael Vines Date: Fri, 1 Mar 2019 09:55:13 -0800 Subject: [PATCH] Rename last_id to last_hash within HashQueue --- runtime/src/bank.rs | 48 ++++++++++++--------------------------- runtime/src/hash_queue.rs | 48 ++++++++++++++++++--------------------- src/staking_utils.rs | 9 ++------ 3 files changed, 38 insertions(+), 67 deletions(-) diff --git a/runtime/src/bank.rs b/runtime/src/bank.rs index b4577d9bc9..f6aca499c0 100644 --- a/runtime/src/bank.rs +++ b/runtime/src/bank.rs @@ -243,7 +243,7 @@ impl Bank { self.tick_hash_queue .write() .unwrap() - .genesis_last_id(&genesis_block.hash()); + .genesis_hash(&genesis_block.hash()); self.ticks_per_slot = genesis_block.ticks_per_slot; self.slots_per_epoch = genesis_block.slots_per_epoch; @@ -266,7 +266,7 @@ impl Bank { /// Return the last entry ID registered. pub fn last_id(&self) -> Hash { - self.tick_hash_queue.read().unwrap().last_id() + self.tick_hash_queue.read().unwrap().last_hash() } /// Forget all signatures. Useful for benchmarking. @@ -304,8 +304,8 @@ impl Bank { /// Tell the bank which Entry IDs exist on the ledger. This function /// assumes subsequent calls correspond to later entries, and will boot /// the oldest ones once its internal cache is full. Once boot, the - /// bank will reject transactions using that `last_id`. - pub fn register_tick(&self, last_id: &Hash) { + /// bank will reject transactions using that `hash`. + pub fn register_tick(&self, hash: &Hash) { if self.is_frozen() { warn!("=========== FIXME: register_tick() working on a frozen bank! ================"); } @@ -315,11 +315,11 @@ impl Bank { //atomic register and read the tick let mut tick_hash_queue = self.tick_hash_queue.write().unwrap(); inc_new_counter_info!("bank-register_tick-registered", 1); - tick_hash_queue.register_tick(last_id); + tick_hash_queue.register_hash(hash); tick_hash_queue.tick_height() }; if current_tick_height % NUM_TICKS_PER_SECOND as u64 == 0 { - self.status_cache.write().unwrap().new_cache(last_id); + self.status_cache.write().unwrap().new_cache(hash); } } @@ -1026,13 +1026,8 @@ mod tests { let key1 = Keypair::new(); let key2 = Keypair::new(); - let tx = SystemTransaction::new_move( - &mint_keypair, - key1.pubkey(), - 2, - genesis_block.hash(), - 3, - ); + let tx = + SystemTransaction::new_move(&mint_keypair, key1.pubkey(), 2, genesis_block.hash(), 3); let initial_balance = bank.get_balance(&leader); assert_eq!(bank.process_transaction(&tx), Ok(())); assert_eq!(bank.get_balance(&leader), initial_balance + 3); @@ -1272,13 +1267,8 @@ mod tests { let key1 = Keypair::new(); let parent = Arc::new(Bank::new(&genesis_block)); - let tx = SystemTransaction::new_move( - &mint_keypair, - key1.pubkey(), - 1, - genesis_block.hash(), - 0, - ); + let tx = + SystemTransaction::new_move(&mint_keypair, key1.pubkey(), 1, genesis_block.hash(), 0); assert_eq!(parent.process_transaction(&tx), Ok(())); let bank = Bank::new_from_parent(&parent); assert_eq!( @@ -1295,13 +1285,8 @@ mod tests { let key2 = Keypair::new(); let parent = Arc::new(Bank::new(&genesis_block)); - let tx = SystemTransaction::new_move( - &mint_keypair, - key1.pubkey(), - 1, - genesis_block.hash(), - 0, - ); + let tx = + SystemTransaction::new_move(&mint_keypair, key1.pubkey(), 1, genesis_block.hash(), 0); assert_eq!(parent.process_transaction(&tx), Ok(())); let bank = Bank::new_from_parent(&parent); let tx = SystemTransaction::new_move(&key1, key2.pubkey(), 1, genesis_block.hash(), 0); @@ -1364,13 +1349,8 @@ mod tests { let key2 = Keypair::new(); let parent = Arc::new(Bank::new(&genesis_block)); - let tx_move_mint_to_1 = SystemTransaction::new_move( - &mint_keypair, - key1.pubkey(), - 1, - genesis_block.hash(), - 0, - ); + let tx_move_mint_to_1 = + SystemTransaction::new_move(&mint_keypair, key1.pubkey(), 1, genesis_block.hash(), 0); assert_eq!(parent.process_transaction(&tx_move_mint_to_1), Ok(())); assert_eq!(parent.transaction_count(), 1); diff --git a/runtime/src/hash_queue.rs b/runtime/src/hash_queue.rs index 9e61f3d920..39445ac47b 100644 --- a/runtime/src/hash_queue.rs +++ b/runtime/src/hash_queue.rs @@ -14,8 +14,8 @@ pub struct HashQueue { /// updated whenever an id is registered, at each tick ;) tick_height: u64, - /// last tick to be registered - last_id: Option, + /// last hash to be registered + last_hash: Option, entries: HashMap, } @@ -24,7 +24,7 @@ impl Default for HashQueue { Self { entries: HashMap::new(), tick_height: 0, - last_id: None, + last_hash: None, } } } @@ -34,8 +34,8 @@ impl HashQueue { self.tick_height } - pub fn last_id(&self) -> Hash { - self.last_id.expect("no last_id has been set") + pub fn last_hash(&self) -> Hash { + self.last_hash.expect("no hash has been set") } /// Check if the age of the entry_id is within the max_age @@ -53,23 +53,19 @@ impl HashQueue { self.entries.get(&entry_id).is_some() } - pub fn genesis_last_id(&mut self, last_id: &Hash) { + pub fn genesis_hash(&mut self, hash: &Hash) { self.entries.insert( - *last_id, + *hash, HashQueueEntry { tick_height: 0, timestamp: timestamp(), }, ); - self.last_id = Some(*last_id); + self.last_hash = Some(*hash); } - /// Tell the bank which Entry IDs exist on the ledger. This function - /// assumes subsequent calls correspond to later entries, and will boot - /// the oldest ones once its internal cache is full. Once boot, the - /// bank will reject transactions using that `last_id`. - pub fn register_tick(&mut self, last_id: &Hash) { + pub fn register_hash(&mut self, hash: &Hash) { self.tick_height += 1; let tick_height = self.tick_height; @@ -81,14 +77,14 @@ impl HashQueue { } self.entries.insert( - *last_id, + *hash, HashQueueEntry { tick_height, timestamp: timestamp(), }, ); - self.last_id = Some(*last_id); + self.last_hash = Some(*hash); } /// Looks through a list of tick heights and stakes, and finds the latest @@ -129,7 +125,7 @@ impl HashQueue { pub fn clear(&mut self) { self.entries = HashMap::new(); self.tick_height = 0; - self.last_id = None; + self.last_hash = None; } } #[cfg(test)] @@ -139,22 +135,22 @@ mod tests { use solana_sdk::hash::hash; #[test] - fn test_register_tick() { - let last_id = Hash::default(); + fn test_register_hash() { + let last_hash = Hash::default(); let mut entry_queue = HashQueue::default(); - assert!(!entry_queue.check_entry(last_id)); - entry_queue.register_tick(&last_id); - assert!(entry_queue.check_entry(last_id)); + assert!(!entry_queue.check_entry(last_hash)); + entry_queue.register_hash(&last_hash); + assert!(entry_queue.check_entry(last_hash)); } #[test] - fn test_reject_old_last_id() { - let last_id = Hash::default(); + fn test_reject_old_last_hash() { + let last_hash = Hash::default(); let mut entry_queue = HashQueue::default(); for i in 0..MAX_ENTRY_IDS { - let last_id = hash(&serialize(&i).unwrap()); // Unique hash - entry_queue.register_tick(&last_id); + let last_hash = hash(&serialize(&i).unwrap()); // Unique hash + entry_queue.register_hash(&last_hash); } // Assert we're no longer able to use the oldest entry ID. - assert!(!entry_queue.check_entry(last_id)); + assert!(!entry_queue.check_entry(last_hash)); } } diff --git a/src/staking_utils.rs b/src/staking_utils.rs index c436e489ed..fe76a86709 100644 --- a/src/staking_utils.rs +++ b/src/staking_utils.rs @@ -160,13 +160,8 @@ mod tests { let bank_voter = Keypair::new(); // Give the validator some stake but don't setup a staking account - bank.transfer( - 1, - &mint_keypair, - validator.pubkey(), - genesis_block.hash(), - ) - .unwrap(); + bank.transfer(1, &mint_keypair, validator.pubkey(), genesis_block.hash()) + .unwrap(); // Validator has no token staked, so they get filtered out. Only the bootstrap leader // created by the genesis block will get included