waffles_solana_program/sysvar/
recent_blockhashes.rs

1//! Information about recent blocks and their fee calculators.
2//!
3//! The _recent blockhashes sysvar_ provides access to the [`RecentBlockhashes`],
4//! which contains recent blockhahes and their [`FeeCalculator`]s.
5//!
6//! [`RecentBlockhashes`] does not implement [`Sysvar::get`].
7//!
8//! This sysvar is deprecated and should not be used. Transaction fees should be
9//! determined with the [`getFeeForMessage`] RPC method. For additional context
10//! see the [Comprehensive Compute Fees proposal][ccf].
11//!
12//! [`getFeeForMessage`]: https://docs.solana.com/developing/clients/jsonrpc-api#getfeeformessage
13//! [ccf]: https://docs.solana.com/proposals/comprehensive-compute-fees
14//!
15//! See also the Solana [documentation on the recent blockhashes sysvar][sdoc].
16//!
17//! [sdoc]: https://docs.solana.com/developing/runtime-facilities/sysvars#recentblockhashes
18
19#![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/// Contains recent block hashes and fee calculators.
86///
87/// The entries are ordered by descending block height, so the first entry holds
88/// the most recent block hash, and the last entry holds an old block hash.
89#[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// This is cherry-picked from HEAD of rust-lang's master (ref1) because it's
117// a nightly-only experimental API.
118// (binary_heap_into_iter_sorted [rustc issue #59278])
119// Remove this and use the standard API once BinaryHeap::into_iter_sorted (ref2)
120// is stabilized.
121// ref1: https://github.com/rust-lang/rust/blob/2f688ac602d50129388bb2a5519942049096cbff/src/liballoc/collections/binary_heap.rs#L1149
122// ref2: https://doc.rust-lang.org/std/collections/struct.BinaryHeap.html#into_iter_sorted.v
123
124#[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        // hard-coded so that we don't have to construct an empty
152        6008 // golden, update if MAX_ENTRIES changes
153    }
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        // Ensure we can still hold all of the active entries in `BlockhashQueue`
171        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}