2021-07-27 16:34:21 -07:00
|
|
|
#[allow(deprecated)]
|
|
|
|
use solana_sdk::sysvar::recent_blockhashes;
|
2021-12-03 09:00:31 -08:00
|
|
|
use {
|
|
|
|
serde::{Deserialize, Serialize},
|
2022-04-15 09:30:20 -07:00
|
|
|
solana_sdk::{
|
|
|
|
clock::MAX_RECENT_BLOCKHASHES, fee_calculator::FeeCalculator, hash::Hash, timing::timestamp,
|
|
|
|
},
|
2021-12-03 09:00:31 -08:00
|
|
|
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,
|
2022-04-20 20:57:17 -07:00
|
|
|
hash_index: 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
|
2022-04-20 20:57:17 -07:00
|
|
|
#[frozen_abi(digest = "J66ssCYGtWdQu5oyJxFKFeZY86nUjThBdBeXQYuRPDvE")]
|
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 {
|
2022-04-20 20:57:17 -07:00
|
|
|
/// index of last hash to be registered
|
|
|
|
last_hash_index: u64,
|
2019-03-14 11:56:36 -07:00
|
|
|
|
|
|
|
/// 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,
|
|
|
|
}
|
|
|
|
|
2022-04-15 09:30:20 -07:00
|
|
|
impl Default for BlockhashQueue {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new(MAX_RECENT_BLOCKHASHES)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:56:36 -07:00
|
|
|
impl BlockhashQueue {
|
|
|
|
pub fn new(max_age: usize) -> Self {
|
|
|
|
Self {
|
|
|
|
ages: HashMap::new(),
|
2022-04-20 20:57:17 -07:00
|
|
|
last_hash_index: 0,
|
2019-03-14 11:56:36 -07:00
|
|
|
last_hash: None,
|
|
|
|
max_age,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn last_hash(&self) -> Hash {
|
|
|
|
self.last_hash.expect("no hash has been set")
|
|
|
|
}
|
|
|
|
|
2021-10-22 14:32:40 -07:00
|
|
|
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)
|
2019-06-10 22:18:32 -07:00
|
|
|
}
|
|
|
|
|
2022-04-20 20:57:17 -07:00
|
|
|
/// Check if the age of the hash is within the queue's max age
|
|
|
|
pub fn is_hash_valid(&self, hash: &Hash) -> bool {
|
|
|
|
self.ages.get(hash).is_some()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if the age of the hash is within the specified age
|
|
|
|
pub fn is_hash_valid_for_age(&self, hash: &Hash, max_age: usize) -> bool {
|
2020-01-16 23:26:50 -08:00
|
|
|
self.ages
|
|
|
|
.get(hash)
|
2022-04-20 20:57:17 -07:00
|
|
|
.map(|age| Self::is_hash_index_valid(self.last_hash_index, max_age, age.hash_index))
|
|
|
|
.unwrap_or(false)
|
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)
|
2022-04-20 20:57:17 -07:00
|
|
|
.map(|age| self.last_hash_index - age.hash_index)
|
2019-03-14 11:56:36 -07:00
|
|
|
}
|
|
|
|
|
2021-10-22 14:32:40 -07:00
|
|
|
pub fn genesis_hash(&mut self, hash: &Hash, lamports_per_signature: u64) {
|
2019-03-14 11:56:36 -07:00
|
|
|
self.ages.insert(
|
|
|
|
*hash,
|
|
|
|
HashAge {
|
2021-10-22 14:32:40 -07:00
|
|
|
fee_calculator: FeeCalculator::new(lamports_per_signature),
|
2022-04-20 20:57:17 -07:00
|
|
|
hash_index: 0,
|
2019-03-14 11:56:36 -07:00
|
|
|
timestamp: timestamp(),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
self.last_hash = Some(*hash);
|
|
|
|
}
|
|
|
|
|
2022-04-20 20:57:17 -07:00
|
|
|
fn is_hash_index_valid(last_hash_index: u64, max_age: usize, hash_index: u64) -> bool {
|
|
|
|
last_hash_index - hash_index <= max_age as u64
|
2019-03-29 10:03:55 -07:00
|
|
|
}
|
|
|
|
|
2021-10-22 14:32:40 -07:00
|
|
|
pub fn register_hash(&mut self, hash: &Hash, lamports_per_signature: u64) {
|
2022-04-20 20:57:17 -07:00
|
|
|
self.last_hash_index += 1;
|
|
|
|
if self.ages.len() >= self.max_age {
|
|
|
|
self.ages.retain(|_, age| {
|
|
|
|
Self::is_hash_index_valid(self.last_hash_index, self.max_age, age.hash_index)
|
|
|
|
});
|
2019-03-14 11:56:36 -07:00
|
|
|
}
|
2022-04-20 20:57:17 -07:00
|
|
|
|
2019-03-14 11:56:36 -07:00
|
|
|
self.ages.insert(
|
|
|
|
*hash,
|
|
|
|
HashAge {
|
2021-10-22 14:32:40 -07:00
|
|
|
fee_calculator: FeeCalculator::new(lamports_per_signature),
|
2022-04-20 20:57:17 -07:00
|
|
|
hash_index: self.last_hash_index,
|
2019-03-14 11:56:36 -07:00
|
|
|
timestamp: timestamp(),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
self.last_hash = Some(*hash);
|
|
|
|
}
|
|
|
|
|
2021-07-27 16:34:21 -07:00
|
|
|
#[deprecated(
|
2021-10-26 09:06:41 -07:00
|
|
|
since = "1.9.0",
|
2021-07-27 16:34:21 -07:00
|
|
|
note = "Please do not use, will no longer be available in the future"
|
|
|
|
)]
|
|
|
|
#[allow(deprecated)]
|
2020-03-04 11:01:32 -08:00
|
|
|
pub fn get_recent_blockhashes(&self) -> impl Iterator<Item = recent_blockhashes::IterItem> {
|
2022-03-09 13:33:50 -08:00
|
|
|
(self.ages).iter().map(|(k, v)| {
|
2022-04-20 20:57:17 -07:00
|
|
|
recent_blockhashes::IterItem(v.hash_index, k, v.fee_calculator.lamports_per_signature)
|
2021-10-22 14:32:40 -07:00
|
|
|
})
|
2019-11-04 09:51:15 -08:00
|
|
|
}
|
2020-05-26 12:06:21 -07:00
|
|
|
|
2022-04-11 04:36:24 -07:00
|
|
|
pub(crate) fn get_max_age(&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 {
|
2021-07-27 16:34:21 -07:00
|
|
|
#[allow(deprecated)]
|
|
|
|
use solana_sdk::sysvar::recent_blockhashes::IterItem;
|
2021-12-03 09:00:31 -08:00
|
|
|
use {
|
|
|
|
super::*,
|
|
|
|
bincode::serialize,
|
|
|
|
solana_sdk::{clock::MAX_RECENT_BLOCKHASHES, hash::hash},
|
|
|
|
};
|
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);
|
2022-04-20 20:57:17 -07:00
|
|
|
assert!(!hash_queue.is_hash_valid(&last_hash));
|
2021-10-22 14:32:40 -07:00
|
|
|
hash_queue.register_hash(&last_hash, 0);
|
2022-04-20 20:57:17 -07:00
|
|
|
assert!(hash_queue.is_hash_valid(&last_hash));
|
|
|
|
assert_eq!(hash_queue.last_hash_index, 1);
|
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]
|
|
|
|
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());
|
2021-10-22 14:32:40 -07:00
|
|
|
hash_queue.register_hash(&last_hash, 0);
|
2019-03-14 11:56:36 -07:00
|
|
|
}
|
|
|
|
// Assert we're no longer able to use the oldest hash.
|
2022-04-20 20:57:17 -07:00
|
|
|
assert!(!hash_queue.is_hash_valid(&last_hash));
|
|
|
|
assert!(!hash_queue.is_hash_valid_for_age(&last_hash, 0));
|
2020-01-16 23:26:50 -08:00
|
|
|
|
|
|
|
// Assert we are not able to use the oldest remaining hash.
|
|
|
|
let last_valid_hash = hash(&serialize(&1).unwrap());
|
2022-04-20 20:57:17 -07:00
|
|
|
assert!(hash_queue.is_hash_valid(&last_valid_hash));
|
|
|
|
assert!(!hash_queue.is_hash_valid_for_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);
|
2021-10-22 14:32:40 -07:00
|
|
|
hash_queue.register_hash(&last_hash, 0);
|
2019-03-14 11:56:36 -07:00
|
|
|
assert_eq!(last_hash, hash_queue.last_hash());
|
2022-04-20 20:57:17 -07:00
|
|
|
assert!(hash_queue.is_hash_valid_for_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);
|
2021-07-27 16:34:21 -07:00
|
|
|
#[allow(deprecated)]
|
2019-11-04 09:51:15 -08:00
|
|
|
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());
|
2021-10-22 14:32:40 -07:00
|
|
|
blockhash_queue.register_hash(&hash, 0);
|
2019-11-04 09:51:15 -08:00
|
|
|
}
|
2021-07-27 16:34:21 -07:00
|
|
|
#[allow(deprecated)]
|
2019-11-04 09:51:15 -08:00
|
|
|
let recent_blockhashes = blockhash_queue.get_recent_blockhashes();
|
|
|
|
// Verify that the returned hashes are most recent
|
2021-07-27 16:34:21 -07:00
|
|
|
#[allow(deprecated)]
|
2021-10-22 14:32:40 -07:00
|
|
|
for IterItem(_slot, hash, _lamports_per_signature) in recent_blockhashes {
|
2022-04-20 20:57:17 -07:00
|
|
|
assert!(blockhash_queue.is_hash_valid_for_age(hash, MAX_RECENT_BLOCKHASHES));
|
2019-11-04 09:51:15 -08:00
|
|
|
}
|
|
|
|
}
|
2022-04-11 04:36:24 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_len() {
|
|
|
|
const MAX_AGE: usize = 10;
|
|
|
|
let mut hash_queue = BlockhashQueue::new(MAX_AGE);
|
|
|
|
assert_eq!(hash_queue.ages.len(), 0);
|
|
|
|
|
|
|
|
for _ in 0..MAX_AGE {
|
|
|
|
hash_queue.register_hash(&Hash::new_unique(), 0);
|
|
|
|
}
|
|
|
|
assert_eq!(hash_queue.ages.len(), MAX_AGE);
|
|
|
|
|
|
|
|
// Show that the queue actually holds one more entry than the max age.
|
|
|
|
// This is because the most recent hash is considered to have an age of 0,
|
|
|
|
// which is likely the result of an unintentional off-by-one error in the past.
|
|
|
|
hash_queue.register_hash(&Hash::new_unique(), 0);
|
|
|
|
assert_eq!(hash_queue.ages.len(), MAX_AGE + 1);
|
|
|
|
|
|
|
|
// Ensure that no additional entries beyond `MAX_AGE + 1` are added
|
|
|
|
hash_queue.register_hash(&Hash::new_unique(), 0);
|
|
|
|
assert_eq!(hash_queue.ages.len(), MAX_AGE + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_hash_age() {
|
|
|
|
const MAX_AGE: usize = 10;
|
|
|
|
let mut hash_list: Vec<Hash> = Vec::new();
|
|
|
|
hash_list.resize_with(MAX_AGE + 1, Hash::new_unique);
|
|
|
|
|
|
|
|
let mut hash_queue = BlockhashQueue::new(MAX_AGE);
|
|
|
|
for hash in &hash_list {
|
|
|
|
assert!(hash_queue.get_hash_age(hash).is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
for hash in &hash_list {
|
|
|
|
hash_queue.register_hash(hash, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that the "age" of a hash in the queue is 0-indexed. So when processing
|
|
|
|
// transactions in block 50, the hash for block 49 has an age of 0 despite
|
|
|
|
// being one block in the past.
|
|
|
|
for (age, hash) in hash_list.iter().rev().enumerate() {
|
|
|
|
assert_eq!(hash_queue.get_hash_age(hash), Some(age as u64));
|
|
|
|
}
|
|
|
|
|
|
|
|
// When the oldest hash is popped, it should no longer return a hash age
|
|
|
|
hash_queue.register_hash(&Hash::new_unique(), 0);
|
|
|
|
assert!(hash_queue.get_hash_age(&hash_list[0]).is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-04-20 20:57:17 -07:00
|
|
|
fn test_is_hash_valid_for_age() {
|
2022-04-11 04:36:24 -07:00
|
|
|
const MAX_AGE: usize = 10;
|
|
|
|
let mut hash_list: Vec<Hash> = Vec::new();
|
|
|
|
hash_list.resize_with(MAX_AGE + 1, Hash::new_unique);
|
|
|
|
|
|
|
|
let mut hash_queue = BlockhashQueue::new(MAX_AGE);
|
|
|
|
for hash in &hash_list {
|
2022-04-20 20:57:17 -07:00
|
|
|
assert!(!hash_queue.is_hash_valid_for_age(hash, MAX_AGE));
|
2022-04-11 04:36:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for hash in &hash_list {
|
|
|
|
hash_queue.register_hash(hash, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that the "age" of a hash in the queue is 0-indexed. So when checking
|
|
|
|
// the age of a hash is within max age, the hash from 11 slots ago is considered
|
|
|
|
// to be within the max age of 10.
|
|
|
|
for hash in &hash_list {
|
2022-04-20 20:57:17 -07:00
|
|
|
assert!(hash_queue.is_hash_valid_for_age(hash, MAX_AGE));
|
2022-04-11 04:36:24 -07:00
|
|
|
}
|
|
|
|
|
2022-04-20 20:57:17 -07:00
|
|
|
// When max age is 0, only the most recent blockhash is still considered valid
|
|
|
|
assert!(hash_queue.is_hash_valid_for_age(&hash_list[MAX_AGE], 0));
|
|
|
|
assert!(!hash_queue.is_hash_valid_for_age(&hash_list[MAX_AGE - 1], 0));
|
2022-04-11 04:36:24 -07:00
|
|
|
}
|
2019-03-14 11:56:36 -07:00
|
|
|
}
|