Rename last_id to last_hash within HashQueue

This commit is contained in:
Michael Vines 2019-03-01 09:55:13 -08:00
parent 224b705f8d
commit 46fb0b1b94
3 changed files with 38 additions and 67 deletions

View File

@ -243,7 +243,7 @@ impl Bank {
self.tick_hash_queue self.tick_hash_queue
.write() .write()
.unwrap() .unwrap()
.genesis_last_id(&genesis_block.hash()); .genesis_hash(&genesis_block.hash());
self.ticks_per_slot = genesis_block.ticks_per_slot; self.ticks_per_slot = genesis_block.ticks_per_slot;
self.slots_per_epoch = genesis_block.slots_per_epoch; self.slots_per_epoch = genesis_block.slots_per_epoch;
@ -266,7 +266,7 @@ impl Bank {
/// Return the last entry ID registered. /// Return the last entry ID registered.
pub fn last_id(&self) -> Hash { 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. /// Forget all signatures. Useful for benchmarking.
@ -304,8 +304,8 @@ impl Bank {
/// Tell the bank which Entry IDs exist on the ledger. This function /// Tell the bank which Entry IDs exist on the ledger. This function
/// assumes subsequent calls correspond to later entries, and will boot /// assumes subsequent calls correspond to later entries, and will boot
/// the oldest ones once its internal cache is full. Once boot, the /// the oldest ones once its internal cache is full. Once boot, the
/// bank will reject transactions using that `last_id`. /// bank will reject transactions using that `hash`.
pub fn register_tick(&self, last_id: &Hash) { pub fn register_tick(&self, hash: &Hash) {
if self.is_frozen() { if self.is_frozen() {
warn!("=========== FIXME: register_tick() working on a frozen bank! ================"); warn!("=========== FIXME: register_tick() working on a frozen bank! ================");
} }
@ -315,11 +315,11 @@ impl Bank {
//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_tick(last_id); tick_hash_queue.register_hash(hash);
tick_hash_queue.tick_height() tick_hash_queue.tick_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(last_id); self.status_cache.write().unwrap().new_cache(hash);
} }
} }
@ -1026,13 +1026,8 @@ mod tests {
let key1 = Keypair::new(); let key1 = Keypair::new();
let key2 = Keypair::new(); let key2 = Keypair::new();
let tx = SystemTransaction::new_move( let tx =
&mint_keypair, SystemTransaction::new_move(&mint_keypair, key1.pubkey(), 2, genesis_block.hash(), 3);
key1.pubkey(),
2,
genesis_block.hash(),
3,
);
let initial_balance = bank.get_balance(&leader); let initial_balance = bank.get_balance(&leader);
assert_eq!(bank.process_transaction(&tx), Ok(())); assert_eq!(bank.process_transaction(&tx), Ok(()));
assert_eq!(bank.get_balance(&leader), initial_balance + 3); assert_eq!(bank.get_balance(&leader), initial_balance + 3);
@ -1272,13 +1267,8 @@ mod tests {
let key1 = Keypair::new(); let key1 = Keypair::new();
let parent = Arc::new(Bank::new(&genesis_block)); let parent = Arc::new(Bank::new(&genesis_block));
let tx = SystemTransaction::new_move( let tx =
&mint_keypair, SystemTransaction::new_move(&mint_keypair, key1.pubkey(), 1, genesis_block.hash(), 0);
key1.pubkey(),
1,
genesis_block.hash(),
0,
);
assert_eq!(parent.process_transaction(&tx), Ok(())); assert_eq!(parent.process_transaction(&tx), Ok(()));
let bank = Bank::new_from_parent(&parent); let bank = Bank::new_from_parent(&parent);
assert_eq!( assert_eq!(
@ -1295,13 +1285,8 @@ mod tests {
let key2 = Keypair::new(); let key2 = Keypair::new();
let parent = Arc::new(Bank::new(&genesis_block)); let parent = Arc::new(Bank::new(&genesis_block));
let tx = SystemTransaction::new_move( let tx =
&mint_keypair, SystemTransaction::new_move(&mint_keypair, key1.pubkey(), 1, genesis_block.hash(), 0);
key1.pubkey(),
1,
genesis_block.hash(),
0,
);
assert_eq!(parent.process_transaction(&tx), Ok(())); assert_eq!(parent.process_transaction(&tx), Ok(()));
let bank = Bank::new_from_parent(&parent); let bank = Bank::new_from_parent(&parent);
let tx = SystemTransaction::new_move(&key1, key2.pubkey(), 1, genesis_block.hash(), 0); let tx = SystemTransaction::new_move(&key1, key2.pubkey(), 1, genesis_block.hash(), 0);
@ -1364,13 +1349,8 @@ mod tests {
let key2 = Keypair::new(); let key2 = Keypair::new();
let parent = Arc::new(Bank::new(&genesis_block)); let parent = Arc::new(Bank::new(&genesis_block));
let tx_move_mint_to_1 = SystemTransaction::new_move( let tx_move_mint_to_1 =
&mint_keypair, SystemTransaction::new_move(&mint_keypair, key1.pubkey(), 1, genesis_block.hash(), 0);
key1.pubkey(),
1,
genesis_block.hash(),
0,
);
assert_eq!(parent.process_transaction(&tx_move_mint_to_1), Ok(())); assert_eq!(parent.process_transaction(&tx_move_mint_to_1), Ok(()));
assert_eq!(parent.transaction_count(), 1); assert_eq!(parent.transaction_count(), 1);

View File

@ -14,8 +14,8 @@ pub struct HashQueue {
/// updated whenever an id is registered, at each tick ;) /// updated whenever an id is registered, at each tick ;)
tick_height: u64, tick_height: u64,
/// last tick to be registered /// last hash to be registered
last_id: Option<Hash>, last_hash: Option<Hash>,
entries: HashMap<Hash, HashQueueEntry>, entries: HashMap<Hash, HashQueueEntry>,
} }
@ -24,7 +24,7 @@ impl Default for HashQueue {
Self { Self {
entries: HashMap::new(), entries: HashMap::new(),
tick_height: 0, tick_height: 0,
last_id: None, last_hash: None,
} }
} }
} }
@ -34,8 +34,8 @@ impl HashQueue {
self.tick_height self.tick_height
} }
pub fn last_id(&self) -> Hash { pub fn last_hash(&self) -> Hash {
self.last_id.expect("no last_id has been set") self.last_hash.expect("no hash has been set")
} }
/// Check if the age of the entry_id is within the max_age /// 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() 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( self.entries.insert(
*last_id, *hash,
HashQueueEntry { HashQueueEntry {
tick_height: 0, tick_height: 0,
timestamp: timestamp(), 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 pub fn register_hash(&mut self, hash: &Hash) {
/// 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) {
self.tick_height += 1; self.tick_height += 1;
let tick_height = self.tick_height; let tick_height = self.tick_height;
@ -81,14 +77,14 @@ impl HashQueue {
} }
self.entries.insert( self.entries.insert(
*last_id, *hash,
HashQueueEntry { HashQueueEntry {
tick_height, tick_height,
timestamp: timestamp(), 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 /// Looks through a list of tick heights and stakes, and finds the latest
@ -129,7 +125,7 @@ impl HashQueue {
pub fn clear(&mut self) { pub fn clear(&mut self) {
self.entries = HashMap::new(); self.entries = HashMap::new();
self.tick_height = 0; self.tick_height = 0;
self.last_id = None; self.last_hash = None;
} }
} }
#[cfg(test)] #[cfg(test)]
@ -139,22 +135,22 @@ mod tests {
use solana_sdk::hash::hash; use solana_sdk::hash::hash;
#[test] #[test]
fn test_register_tick() { fn test_register_hash() {
let last_id = Hash::default(); let last_hash = Hash::default();
let mut entry_queue = HashQueue::default(); let mut entry_queue = HashQueue::default();
assert!(!entry_queue.check_entry(last_id)); assert!(!entry_queue.check_entry(last_hash));
entry_queue.register_tick(&last_id); entry_queue.register_hash(&last_hash);
assert!(entry_queue.check_entry(last_id)); assert!(entry_queue.check_entry(last_hash));
} }
#[test] #[test]
fn test_reject_old_last_id() { fn test_reject_old_last_hash() {
let last_id = Hash::default(); let last_hash = Hash::default();
let mut entry_queue = HashQueue::default(); let mut entry_queue = HashQueue::default();
for i in 0..MAX_ENTRY_IDS { for i in 0..MAX_ENTRY_IDS {
let last_id = hash(&serialize(&i).unwrap()); // Unique hash let last_hash = hash(&serialize(&i).unwrap()); // Unique hash
entry_queue.register_tick(&last_id); entry_queue.register_hash(&last_hash);
} }
// Assert we're no longer able to use the oldest entry ID. // 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));
} }
} }

View File

@ -160,13 +160,8 @@ mod tests {
let bank_voter = Keypair::new(); let bank_voter = Keypair::new();
// Give the validator some stake but don't setup a staking account // Give the validator some stake but don't setup a staking account
bank.transfer( bank.transfer(1, &mint_keypair, validator.pubkey(), genesis_block.hash())
1, .unwrap();
&mint_keypair,
validator.pubkey(),
genesis_block.hash(),
)
.unwrap();
// Validator has no token staked, so they get filtered out. Only the bootstrap leader // Validator has no token staked, so they get filtered out. Only the bootstrap leader
// created by the genesis block will get included // created by the genesis block will get included