2019-05-30 21:31:35 -07:00
|
|
|
use serde::{Deserialize, Serialize};
|
2020-03-04 11:01:32 -08:00
|
|
|
use solana_sdk::{
|
|
|
|
fee_calculator::FeeCalculator, hash::Hash, sysvar::recent_blockhashes, timing::timestamp,
|
|
|
|
};
|
2019-05-30 21:31:35 -07:00
|
|
|
use std::collections::HashMap;
|
2019-03-14 11:56:36 -07:00
|
|
|
|
2020-07-06 04:22:23 -07:00
|
|
|
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, AbiExample)]
|
2019-03-14 11:56:36 -07:00
|
|
|
struct HashAge {
|
2019-06-10 22:18:32 -07:00
|
|
|
fee_calculator: FeeCalculator,
|
2019-03-14 11:56:36 -07:00
|
|
|
hash_height: u64,
|
2019-06-10 22:18:32 -07:00
|
|
|
timestamp: u64,
|
2019-03-14 11:56:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Low memory overhead, so can be cloned for every checkpoint
|
2020-10-22 10:32:35 -07:00
|
|
|
#[frozen_abi(digest = "J1fGiMHyiKEBcWE6mfm7grAEGJgYEaVLzcrNZvd37iA2")]
|
2020-10-08 23:44:41 -07:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, AbiExample)]
|
2019-03-14 11:56:36 -07:00
|
|
|
pub struct BlockhashQueue {
|
|
|
|
/// updated whenever an hash is registered
|
|
|
|
hash_height: u64,
|
|
|
|
|
|
|
|
/// last hash to be registered
|
|
|
|
last_hash: Option<Hash>,
|
|
|
|
|
|
|
|
ages: HashMap<Hash, HashAge>,
|
|
|
|
|
|
|
|
/// hashes older than `max_age` will be dropped from the queue
|
|
|
|
max_age: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BlockhashQueue {
|
|
|
|
pub fn new(max_age: usize) -> Self {
|
|
|
|
Self {
|
|
|
|
ages: HashMap::new(),
|
|
|
|
hash_height: 0,
|
|
|
|
last_hash: None,
|
|
|
|
max_age,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn hash_height(&self) -> u64 {
|
|
|
|
self.hash_height
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn last_hash(&self) -> Hash {
|
|
|
|
self.last_hash.expect("no hash has been set")
|
|
|
|
}
|
|
|
|
|
2019-06-10 22:18:32 -07:00
|
|
|
pub fn get_fee_calculator(&self, hash: &Hash) -> Option<&FeeCalculator> {
|
|
|
|
self.ages.get(hash).map(|hash_age| &hash_age.fee_calculator)
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:56:36 -07:00
|
|
|
/// Check if the age of the hash is within the max_age
|
|
|
|
/// return false for any hashes with an age above max_age
|
2020-01-16 23:26:50 -08:00
|
|
|
/// return None for any hashes that were not found
|
|
|
|
pub fn check_hash_age(&self, hash: &Hash, max_age: usize) -> Option<bool> {
|
|
|
|
self.ages
|
|
|
|
.get(hash)
|
|
|
|
.map(|age| self.hash_height - age.hash_height <= max_age as u64)
|
2019-03-14 11:56:36 -07:00
|
|
|
}
|
2019-06-10 22:18:32 -07:00
|
|
|
|
2020-05-26 12:06:21 -07:00
|
|
|
pub fn get_hash_age(&self, hash: &Hash) -> Option<u64> {
|
|
|
|
self.ages
|
|
|
|
.get(hash)
|
|
|
|
.map(|age| self.hash_height - age.hash_height)
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:56:36 -07:00
|
|
|
/// check if hash is valid
|
|
|
|
#[cfg(test)]
|
|
|
|
pub fn check_hash(&self, hash: Hash) -> bool {
|
|
|
|
self.ages.get(&hash).is_some()
|
|
|
|
}
|
|
|
|
|
2019-06-10 22:18:32 -07:00
|
|
|
pub fn genesis_hash(&mut self, hash: &Hash, fee_calculator: &FeeCalculator) {
|
2019-03-14 11:56:36 -07:00
|
|
|
self.ages.insert(
|
|
|
|
*hash,
|
|
|
|
HashAge {
|
2019-06-10 22:18:32 -07:00
|
|
|
fee_calculator: fee_calculator.clone(),
|
2019-03-14 11:56:36 -07:00
|
|
|
hash_height: 0,
|
|
|
|
timestamp: timestamp(),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
self.last_hash = Some(*hash);
|
|
|
|
}
|
|
|
|
|
2019-03-29 10:03:55 -07:00
|
|
|
fn check_age(hash_height: u64, max_age: usize, age: &HashAge) -> bool {
|
|
|
|
hash_height - age.hash_height <= max_age as u64
|
|
|
|
}
|
|
|
|
|
2019-06-10 22:18:32 -07:00
|
|
|
pub fn register_hash(&mut self, hash: &Hash, fee_calculator: &FeeCalculator) {
|
2019-03-14 11:56:36 -07:00
|
|
|
self.hash_height += 1;
|
|
|
|
let hash_height = self.hash_height;
|
|
|
|
|
|
|
|
// this clean up can be deferred until sigs gets larger
|
|
|
|
// because we verify age.nth every place we check for validity
|
|
|
|
let max_age = self.max_age;
|
|
|
|
if self.ages.len() >= max_age {
|
|
|
|
self.ages
|
2019-03-29 10:03:55 -07:00
|
|
|
.retain(|_, age| Self::check_age(hash_height, max_age, age));
|
2019-03-14 11:56:36 -07:00
|
|
|
}
|
|
|
|
self.ages.insert(
|
|
|
|
*hash,
|
|
|
|
HashAge {
|
2019-06-10 22:18:32 -07:00
|
|
|
fee_calculator: fee_calculator.clone(),
|
2019-03-14 11:56:36 -07:00
|
|
|
hash_height,
|
|
|
|
timestamp: timestamp(),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
self.last_hash = Some(*hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Maps a hash height to a timestamp
|
|
|
|
pub fn hash_height_to_timestamp(&self, hash_height: u64) -> Option<u64> {
|
|
|
|
for age in self.ages.values() {
|
|
|
|
if age.hash_height == hash_height {
|
|
|
|
return Some(age.timestamp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
2019-11-04 09:51:15 -08:00
|
|
|
|
2020-03-04 11:01:32 -08:00
|
|
|
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))
|
2019-11-04 09:51:15 -08:00
|
|
|
}
|
2020-05-26 12:06:21 -07:00
|
|
|
|
2021-06-09 15:01:48 -07:00
|
|
|
pub(crate) fn len(&self) -> usize {
|
2020-05-26 12:06:21 -07:00
|
|
|
self.max_age
|
|
|
|
}
|
2019-03-14 11:56:36 -07:00
|
|
|
}
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use bincode::serialize;
|
2020-03-04 11:01:32 -08:00
|
|
|
use solana_sdk::{
|
|
|
|
clock::MAX_RECENT_BLOCKHASHES, hash::hash, sysvar::recent_blockhashes::IterItem,
|
|
|
|
};
|
2019-03-14 11:56:36 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_register_hash() {
|
|
|
|
let last_hash = Hash::default();
|
|
|
|
let mut hash_queue = BlockhashQueue::new(100);
|
|
|
|
assert!(!hash_queue.check_hash(last_hash));
|
2019-06-10 22:18:32 -07:00
|
|
|
hash_queue.register_hash(&last_hash, &FeeCalculator::default());
|
2019-03-14 11:56:36 -07:00
|
|
|
assert!(hash_queue.check_hash(last_hash));
|
|
|
|
assert_eq!(hash_queue.hash_height(), 1);
|
|
|
|
}
|
2020-01-16 23:26:50 -08:00
|
|
|
|
2019-03-14 11:56:36 -07:00
|
|
|
#[test]
|
|
|
|
fn test_reject_old_last_hash() {
|
|
|
|
let mut hash_queue = BlockhashQueue::new(100);
|
2019-03-29 10:03:55 -07:00
|
|
|
let last_hash = hash(&serialize(&0).unwrap());
|
|
|
|
for i in 0..102 {
|
|
|
|
let last_hash = hash(&serialize(&i).unwrap());
|
2019-06-10 22:18:32 -07:00
|
|
|
hash_queue.register_hash(&last_hash, &FeeCalculator::default());
|
2019-03-14 11:56:36 -07:00
|
|
|
}
|
|
|
|
// Assert we're no longer able to use the oldest hash.
|
|
|
|
assert!(!hash_queue.check_hash(last_hash));
|
2020-01-16 23:26:50 -08:00
|
|
|
assert_eq!(None, hash_queue.check_hash_age(&last_hash, 0));
|
|
|
|
|
|
|
|
// Assert we are not able to use the oldest remaining hash.
|
|
|
|
let last_valid_hash = hash(&serialize(&1).unwrap());
|
|
|
|
assert!(hash_queue.check_hash(last_valid_hash));
|
|
|
|
assert_eq!(Some(false), hash_queue.check_hash_age(&last_valid_hash, 0));
|
2019-03-14 11:56:36 -07:00
|
|
|
}
|
2020-01-16 23:26:50 -08:00
|
|
|
|
2019-03-14 11:56:36 -07:00
|
|
|
/// test that when max age is 0, that a valid last_hash still passes the age check
|
|
|
|
#[test]
|
|
|
|
fn test_queue_init_blockhash() {
|
|
|
|
let last_hash = Hash::default();
|
|
|
|
let mut hash_queue = BlockhashQueue::new(100);
|
2019-06-10 22:18:32 -07:00
|
|
|
hash_queue.register_hash(&last_hash, &FeeCalculator::default());
|
2019-03-14 11:56:36 -07:00
|
|
|
assert_eq!(last_hash, hash_queue.last_hash());
|
2020-01-16 23:26:50 -08:00
|
|
|
assert_eq!(Some(true), hash_queue.check_hash_age(&last_hash, 0));
|
2019-03-14 11:56:36 -07:00
|
|
|
}
|
2019-11-04 09:51:15 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_recent_blockhashes() {
|
|
|
|
let mut blockhash_queue = BlockhashQueue::new(MAX_RECENT_BLOCKHASHES);
|
|
|
|
let recent_blockhashes = blockhash_queue.get_recent_blockhashes();
|
|
|
|
// Sanity-check an empty BlockhashQueue
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
let recent_blockhashes = blockhash_queue.get_recent_blockhashes();
|
|
|
|
// Verify that the returned hashes are most recent
|
2020-03-04 11:01:32 -08:00
|
|
|
for IterItem(_slot, hash, _fee_calc) in recent_blockhashes {
|
2020-01-16 23:26:50 -08:00
|
|
|
assert_eq!(
|
|
|
|
Some(true),
|
|
|
|
blockhash_queue.check_hash_age(hash, MAX_RECENT_BLOCKHASHES)
|
|
|
|
);
|
2019-11-04 09:51:15 -08:00
|
|
|
}
|
|
|
|
}
|
2019-03-14 11:56:36 -07:00
|
|
|
}
|