solana_program/sysvar/
recent_blockhashes.rs1#![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#[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#[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 6008 }
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 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}