solana_program/sysvar/
recent_blockhashes.rs#![allow(deprecated)]
#![allow(clippy::arithmetic_side_effects)]
use {
crate::{fee_calculator::FeeCalculator, hash::Hash, sysvar::Sysvar},
solana_sysvar_id::declare_deprecated_sysvar_id,
std::{cmp::Ordering, collections::BinaryHeap, iter::FromIterator, ops::Deref},
};
#[deprecated(
since = "1.9.0",
note = "Please do not use, will no longer be available in the future"
)]
pub const MAX_ENTRIES: usize = 150;
declare_deprecated_sysvar_id!(
"SysvarRecentB1ockHashes11111111111111111111",
RecentBlockhashes
);
#[deprecated(
since = "1.9.0",
note = "Please do not use, will no longer be available in the future"
)]
#[repr(C)]
#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq)]
pub struct Entry {
pub blockhash: Hash,
pub fee_calculator: FeeCalculator,
}
impl Entry {
pub fn new(blockhash: &Hash, lamports_per_signature: u64) -> Self {
Self {
blockhash: *blockhash,
fee_calculator: FeeCalculator::new(lamports_per_signature),
}
}
}
#[deprecated(
since = "1.9.0",
note = "Please do not use, will no longer be available in the future"
)]
#[derive(Clone, Debug)]
pub struct IterItem<'a>(pub u64, pub &'a Hash, pub u64);
impl<'a> Eq for IterItem<'a> {}
impl<'a> PartialEq for IterItem<'a> {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl<'a> Ord for IterItem<'a> {
fn cmp(&self, other: &Self) -> Ordering {
self.0.cmp(&other.0)
}
}
impl<'a> PartialOrd for IterItem<'a> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[deprecated(
since = "1.9.0",
note = "Please do not use, will no longer be available in the future"
)]
#[repr(C)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct RecentBlockhashes(Vec<Entry>);
impl Default for RecentBlockhashes {
fn default() -> Self {
Self(Vec::with_capacity(MAX_ENTRIES))
}
}
impl<'a> FromIterator<IterItem<'a>> for RecentBlockhashes {
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = IterItem<'a>>,
{
let mut new = Self::default();
for i in iter {
new.0.push(Entry::new(i.1, i.2))
}
new
}
}
#[derive(Clone, Debug)]
pub struct IntoIterSorted<T> {
inner: BinaryHeap<T>,
}
impl<T> IntoIterSorted<T> {
pub fn new(binary_heap: BinaryHeap<T>) -> Self {
Self { inner: binary_heap }
}
}
impl<T: Ord> Iterator for IntoIterSorted<T> {
type Item = T;
#[inline]
fn next(&mut self) -> Option<T> {
self.inner.pop()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let exact = self.inner.len();
(exact, Some(exact))
}
}
impl Sysvar for RecentBlockhashes {
fn size_of() -> usize {
6008 }
}
impl Deref for RecentBlockhashes {
type Target = Vec<Entry>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[cfg(test)]
mod tests {
use {super::*, solana_clock::MAX_PROCESSING_AGE};
#[test]
#[allow(clippy::assertions_on_constants)]
fn test_sysvar_can_hold_all_active_blockhashes() {
assert!(MAX_PROCESSING_AGE <= MAX_ENTRIES);
}
#[test]
fn test_size_of() {
let entry = Entry::new(&Hash::default(), 0);
assert_eq!(
bincode::serialized_size(&RecentBlockhashes(vec![entry; MAX_ENTRIES])).unwrap()
as usize,
RecentBlockhashes::size_of()
);
}
}