waffles_solana_program/sysvar/
recent_blockhashes.rs1#![allow(deprecated)]
20#![allow(clippy::integer_arithmetic)]
21use {
22 crate::{
23 declare_deprecated_sysvar_id, fee_calculator::FeeCalculator, hash::Hash, sysvar::Sysvar,
24 },
25 std::{cmp::Ordering, collections::BinaryHeap, iter::FromIterator, ops::Deref},
26};
27
28#[deprecated(
29 since = "1.9.0",
30 note = "Please do not use, will no longer be available in the future"
31)]
32pub const MAX_ENTRIES: usize = 150;
33
34declare_deprecated_sysvar_id!(
35 "SysvarRecentB1ockHashes11111111111111111111",
36 RecentBlockhashes
37);
38
39#[deprecated(
40 since = "1.9.0",
41 note = "Please do not use, will no longer be available in the future"
42)]
43#[repr(C)]
44#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq)]
45pub struct Entry {
46 pub blockhash: Hash,
47 pub fee_calculator: FeeCalculator,
48}
49impl Entry {
50 pub fn new(blockhash: &Hash, lamports_per_signature: u64) -> Self {
51 Self {
52 blockhash: *blockhash,
53 fee_calculator: FeeCalculator::new(lamports_per_signature),
54 }
55 }
56}
57
58#[deprecated(
59 since = "1.9.0",
60 note = "Please do not use, will no longer be available in the future"
61)]
62#[derive(Clone, Debug)]
63pub struct IterItem<'a>(pub u64, pub &'a Hash, pub u64);
64
65impl<'a> Eq for IterItem<'a> {}
66
67impl<'a> PartialEq for IterItem<'a> {
68 fn eq(&self, other: &Self) -> bool {
69 self.0 == other.0
70 }
71}
72
73impl<'a> Ord for IterItem<'a> {
74 fn cmp(&self, other: &Self) -> Ordering {
75 self.0.cmp(&other.0)
76 }
77}
78
79impl<'a> PartialOrd for IterItem<'a> {
80 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
81 Some(self.cmp(other))
82 }
83}
84
85#[deprecated(
90 since = "1.9.0",
91 note = "Please do not use, will no longer be available in the future"
92)]
93#[repr(C)]
94#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
95pub struct RecentBlockhashes(Vec<Entry>);
96
97impl Default for RecentBlockhashes {
98 fn default() -> Self {
99 Self(Vec::with_capacity(MAX_ENTRIES))
100 }
101}
102
103impl<'a> FromIterator<IterItem<'a>> for RecentBlockhashes {
104 fn from_iter<I>(iter: I) -> Self
105 where
106 I: IntoIterator<Item = IterItem<'a>>,
107 {
108 let mut new = Self::default();
109 for i in iter {
110 new.0.push(Entry::new(i.1, i.2))
111 }
112 new
113 }
114}
115
116#[derive(Clone, Debug)]
125pub struct IntoIterSorted<T> {
126 inner: BinaryHeap<T>,
127}
128impl<T> IntoIterSorted<T> {
129 pub fn new(binary_heap: BinaryHeap<T>) -> Self {
130 Self { inner: binary_heap }
131 }
132}
133
134impl<T: Ord> Iterator for IntoIterSorted<T> {
135 type Item = T;
136
137 #[inline]
138 fn next(&mut self) -> Option<T> {
139 self.inner.pop()
140 }
141
142 #[inline]
143 fn size_hint(&self) -> (usize, Option<usize>) {
144 let exact = self.inner.len();
145 (exact, Some(exact))
146 }
147}
148
149impl Sysvar for RecentBlockhashes {
150 fn size_of() -> usize {
151 6008 }
154}
155
156impl Deref for RecentBlockhashes {
157 type Target = Vec<Entry>;
158 fn deref(&self) -> &Self::Target {
159 &self.0
160 }
161}
162
163#[cfg(test)]
164mod tests {
165 use {super::*, crate::clock::MAX_PROCESSING_AGE};
166
167 #[test]
168 #[allow(clippy::assertions_on_constants)]
169 fn test_sysvar_can_hold_all_active_blockhashes() {
170 assert!(MAX_PROCESSING_AGE <= MAX_ENTRIES);
172 }
173
174 #[test]
175 fn test_size_of() {
176 let entry = Entry::new(&Hash::default(), 0);
177 assert_eq!(
178 bincode::serialized_size(&RecentBlockhashes(vec![entry; MAX_ENTRIES])).unwrap()
179 as usize,
180 RecentBlockhashes::size_of()
181 );
182 }
183}