solana_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://solana.com/docs/rpc/http/getfeeformessage
13//! [ccf]: https://docs.solanalabs.com/proposals/comprehensive-compute-fees
14//!
15//! See also the Solana [documentation on the recent blockhashes sysvar][sdoc].
16//!
17//! [sdoc]: https://docs.solanalabs.com/runtime/sysvars#recentblockhashes
18
19#![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/// Contains recent block hashes and fee calculators.
89///
90/// The entries are ordered by descending block height, so the first entry holds
91/// the most recent block hash, and the last entry holds an old block hash.
92#[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// This is cherry-picked from HEAD of rust-lang's master (ref1) because it's
121// a nightly-only experimental API.
122// (binary_heap_into_iter_sorted [rustc issue #59278])
123// Remove this and use the standard API once BinaryHeap::into_iter_sorted (ref2)
124// is stabilized.
125// ref1: https://github.com/rust-lang/rust/blob/2f688ac602d50129388bb2a5519942049096cbff/src/liballoc/collections/binary_heap.rs#L1149
126// ref2: https://doc.rust-lang.org/std/collections/struct.BinaryHeap.html#into_iter_sorted.v
127
128#[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        // hard-coded so that we don't have to construct an empty
157        6008 // golden, update if MAX_ENTRIES changes
158    }
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        // Ensure we can still hold all of the active entries in `BlockhashQueue`
176        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}