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