Divorce the runtime from FeeCalculator (#20737)

This commit is contained in:
Jack May
2021-10-22 14:32:40 -07:00
committed by GitHub
parent 613c7b8444
commit bfbbc53dac
24 changed files with 503 additions and 563 deletions

View File

@@ -46,12 +46,10 @@ impl BlockhashQueue {
self.last_hash.expect("no hash has been set")
}
#[deprecated(
since = "1.8.0",
note = "Please do not use, will no longer be available in the future"
)]
pub fn get_fee_calculator(&self, hash: &Hash) -> Option<&FeeCalculator> {
self.ages.get(hash).map(|hash_age| &hash_age.fee_calculator)
pub fn get_lamports_per_signature(&self, hash: &Hash) -> Option<u64> {
self.ages
.get(hash)
.map(|hash_age| hash_age.fee_calculator.lamports_per_signature)
}
/// Check if the age of the hash is within the max_age
@@ -74,11 +72,11 @@ impl BlockhashQueue {
self.ages.get(hash).is_some()
}
pub fn genesis_hash(&mut self, hash: &Hash, fee_calculator: &FeeCalculator) {
pub fn genesis_hash(&mut self, hash: &Hash, lamports_per_signature: u64) {
self.ages.insert(
*hash,
HashAge {
fee_calculator: fee_calculator.clone(),
fee_calculator: FeeCalculator::new(lamports_per_signature),
hash_height: 0,
timestamp: timestamp(),
},
@@ -91,7 +89,7 @@ impl BlockhashQueue {
hash_height - age.hash_height <= max_age as u64
}
pub fn register_hash(&mut self, hash: &Hash, fee_calculator: &FeeCalculator) {
pub fn register_hash(&mut self, hash: &Hash, lamports_per_signature: u64) {
self.hash_height += 1;
let hash_height = self.hash_height;
@@ -105,7 +103,7 @@ impl BlockhashQueue {
self.ages.insert(
*hash,
HashAge {
fee_calculator: fee_calculator.clone(),
fee_calculator: FeeCalculator::new(lamports_per_signature),
hash_height,
timestamp: timestamp(),
},
@@ -130,9 +128,9 @@ impl BlockhashQueue {
)]
#[allow(deprecated)]
pub fn get_recent_blockhashes(&self) -> impl Iterator<Item = recent_blockhashes::IterItem> {
(&self.ages)
.iter()
.map(|(k, v)| recent_blockhashes::IterItem(v.hash_height, k, &v.fee_calculator))
(&self.ages).iter().map(|(k, v)| {
recent_blockhashes::IterItem(v.hash_height, k, v.fee_calculator.lamports_per_signature)
})
}
pub(crate) fn len(&self) -> usize {
@@ -152,7 +150,7 @@ mod tests {
let last_hash = Hash::default();
let mut hash_queue = BlockhashQueue::new(100);
assert!(!hash_queue.check_hash(&last_hash));
hash_queue.register_hash(&last_hash, &FeeCalculator::default());
hash_queue.register_hash(&last_hash, 0);
assert!(hash_queue.check_hash(&last_hash));
assert_eq!(hash_queue.hash_height(), 1);
}
@@ -163,7 +161,7 @@ mod tests {
let last_hash = hash(&serialize(&0).unwrap());
for i in 0..102 {
let last_hash = hash(&serialize(&i).unwrap());
hash_queue.register_hash(&last_hash, &FeeCalculator::default());
hash_queue.register_hash(&last_hash, 0);
}
// Assert we're no longer able to use the oldest hash.
assert!(!hash_queue.check_hash(&last_hash));
@@ -180,7 +178,7 @@ mod tests {
fn test_queue_init_blockhash() {
let last_hash = Hash::default();
let mut hash_queue = BlockhashQueue::new(100);
hash_queue.register_hash(&last_hash, &FeeCalculator::default());
hash_queue.register_hash(&last_hash, 0);
assert_eq!(last_hash, hash_queue.last_hash());
assert_eq!(Some(true), hash_queue.check_hash_age(&last_hash, 0));
}
@@ -194,13 +192,13 @@ mod tests {
assert_eq!(recent_blockhashes.count(), 0);
for i in 0..MAX_RECENT_BLOCKHASHES {
let hash = hash(&serialize(&i).unwrap());
blockhash_queue.register_hash(&hash, &FeeCalculator::default());
blockhash_queue.register_hash(&hash, 0);
}
#[allow(deprecated)]
let recent_blockhashes = blockhash_queue.get_recent_blockhashes();
// Verify that the returned hashes are most recent
#[allow(deprecated)]
for IterItem(_slot, hash, _fee_calc) in recent_blockhashes {
for IterItem(_slot, hash, _lamports_per_signature) in recent_blockhashes {
assert_eq!(
Some(true),
blockhash_queue.check_hash_age(hash, MAX_RECENT_BLOCKHASHES)