solana_program/sysvar/
recent_blockhashes.rs

1#![allow(deprecated)]
2#![allow(clippy::integer_arithmetic)]
3use {
4    crate::{
5        declare_deprecated_sysvar_id, fee_calculator::FeeCalculator, hash::Hash, sysvar::Sysvar,
6    },
7    std::{cmp::Ordering, collections::BinaryHeap, iter::FromIterator, ops::Deref},
8};
9
10#[deprecated(
11    since = "1.9.0",
12    note = "Please do not use, will no longer be available in the future"
13)]
14pub const MAX_ENTRIES: usize = 150;
15
16declare_deprecated_sysvar_id!(
17    "SysvarRecentB1ockHashes11111111111111111111",
18    RecentBlockhashes
19);
20
21#[deprecated(
22    since = "1.9.0",
23    note = "Please do not use, will no longer be available in the future"
24)]
25#[repr(C)]
26#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq)]
27pub struct Entry {
28    pub blockhash: Hash,
29    pub fee_calculator: FeeCalculator,
30}
31impl Entry {
32    pub fn new(blockhash: &Hash, lamports_per_signature: u64) -> Self {
33        Self {
34            blockhash: *blockhash,
35            fee_calculator: FeeCalculator::new(lamports_per_signature),
36        }
37    }
38}
39
40#[deprecated(
41    since = "1.9.0",
42    note = "Please do not use, will no longer be available in the future"
43)]
44#[derive(Clone, Debug)]
45pub struct IterItem<'a>(pub u64, pub &'a Hash, pub u64);
46
47impl<'a> Eq for IterItem<'a> {}
48
49impl<'a> PartialEq for IterItem<'a> {
50    fn eq(&self, other: &Self) -> bool {
51        self.0 == other.0
52    }
53}
54
55impl<'a> Ord for IterItem<'a> {
56    fn cmp(&self, other: &Self) -> Ordering {
57        self.0.cmp(&other.0)
58    }
59}
60
61impl<'a> PartialOrd for IterItem<'a> {
62    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
63        Some(self.cmp(other))
64    }
65}
66
67/// Contains recent block hashes and fee calculators.
68///
69/// The entries are ordered by descending block height, so the first entry holds
70/// the most recent block hash, and the last entry holds an old block hash.
71#[deprecated(
72    since = "1.9.0",
73    note = "Please do not use, will no longer be available in the future"
74)]
75#[repr(C)]
76#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
77pub struct RecentBlockhashes(Vec<Entry>);
78
79impl Default for RecentBlockhashes {
80    fn default() -> Self {
81        Self(Vec::with_capacity(MAX_ENTRIES))
82    }
83}
84
85impl<'a> FromIterator<IterItem<'a>> for RecentBlockhashes {
86    fn from_iter<I>(iter: I) -> Self
87    where
88        I: IntoIterator<Item = IterItem<'a>>,
89    {
90        let mut new = Self::default();
91        for i in iter {
92            new.0.push(Entry::new(i.1, i.2))
93        }
94        new
95    }
96}
97
98// This is cherry-picked from HEAD of rust-lang's master (ref1) because it's
99// a nightly-only experimental API.
100// (binary_heap_into_iter_sorted [rustc issue #59278])
101// Remove this and use the standard API once BinaryHeap::into_iter_sorted (ref2)
102// is stabilized.
103// ref1: https://github.com/rust-lang/rust/blob/2f688ac602d50129388bb2a5519942049096cbff/src/liballoc/collections/binary_heap.rs#L1149
104// ref2: https://doc.rust-lang.org/std/collections/struct.BinaryHeap.html#into_iter_sorted.v
105
106#[derive(Clone, Debug)]
107pub struct IntoIterSorted<T> {
108    inner: BinaryHeap<T>,
109}
110impl<T> IntoIterSorted<T> {
111    pub fn new(binary_heap: BinaryHeap<T>) -> Self {
112        Self { inner: binary_heap }
113    }
114}
115
116impl<T: Ord> Iterator for IntoIterSorted<T> {
117    type Item = T;
118
119    #[inline]
120    fn next(&mut self) -> Option<T> {
121        self.inner.pop()
122    }
123
124    #[inline]
125    fn size_hint(&self) -> (usize, Option<usize>) {
126        let exact = self.inner.len();
127        (exact, Some(exact))
128    }
129}
130
131impl Sysvar for RecentBlockhashes {
132    fn size_of() -> usize {
133        // hard-coded so that we don't have to construct an empty
134        6008 // golden, update if MAX_ENTRIES changes
135    }
136}
137
138impl Deref for RecentBlockhashes {
139    type Target = Vec<Entry>;
140    fn deref(&self) -> &Self::Target {
141        &self.0
142    }
143}
144
145#[cfg(test)]
146mod tests {
147    use {super::*, crate::clock::MAX_PROCESSING_AGE};
148
149    #[test]
150    #[allow(clippy::assertions_on_constants)]
151    fn test_sysvar_can_hold_all_active_blockhashes() {
152        // Ensure we can still hold all of the active entries in `BlockhashQueue`
153        assert!(MAX_PROCESSING_AGE <= MAX_ENTRIES);
154    }
155
156    #[test]
157    fn test_size_of() {
158        let entry = Entry::new(&Hash::default(), 0);
159        assert_eq!(
160            bincode::serialized_size(&RecentBlockhashes(vec![entry; MAX_ENTRIES])).unwrap()
161                as usize,
162            RecentBlockhashes::size_of()
163        );
164    }
165}