solana_program/sysvar/
slot_history.rs

1//! A bitvector of slots present over the last epoch.
2//!
3//! The _slot history sysvar_ provides access to the [`SlotHistory`] type.
4//!
5//! The [`Sysvar::from_account_info`] and [`Sysvar::get`] methods always return
6//! [`ProgramError::UnsupportedSysvar`] because this sysvar account is too large
7//! to process on-chain. Thus this sysvar cannot be accessed on chain, though
8//! one can still use the [`SysvarId::id`], [`SysvarId::check_id`] and
9//! [`Sysvar::size_of`] methods in an on-chain program, and it can be accessed
10//! off-chain through RPC.
11//!
12//! [`SysvarId::id`]: https://docs.rs/solana-sysvar-id/latest/solana_sysvar_id/trait.SysvarId.html#tymethod.id
13//! [`SysvarId::check_id`]: https://docs.rs/solana-sysvar-id/latest/solana_sysvar_id/trait.SysvarId.html#tymethod.check_id
14//!
15//! # Examples
16//!
17//! Calling via the RPC client:
18//!
19//! ```
20//! # use solana_program::example_mocks::solana_sdk;
21//! # use solana_program::example_mocks::solana_rpc_client;
22//! # use solana_sdk::account::Account;
23//! # use solana_rpc_client::rpc_client::RpcClient;
24//! # use solana_sdk::sysvar::slot_history::{self, SlotHistory};
25//! # use anyhow::Result;
26//! #
27//! fn print_sysvar_slot_history(client: &RpcClient) -> Result<()> {
28//! #   let slot_history = SlotHistory::default();
29//! #   let data: Vec<u8> = bincode::serialize(&slot_history)?;
30//! #   client.set_get_account_response(slot_history::ID, Account {
31//! #       lamports: 913326000,
32//! #       data,
33//! #       owner: solana_sdk::system_program::ID,
34//! #       executable: false,
35//! #       rent_epoch: 307,
36//! #   });
37//! #
38//!     let slot_history = client.get_account(&slot_history::ID)?;
39//!     let data: SlotHistory = bincode::deserialize(&slot_history.data)?;
40//!
41//!     Ok(())
42//! }
43//! #
44//! # let client = RpcClient::new(String::new());
45//! # print_sysvar_slot_history(&client)?;
46//! #
47//! # Ok::<(), anyhow::Error>(())
48//! ```
49
50use crate::sysvar::Sysvar;
51pub use {
52    crate::{account_info::AccountInfo, program_error::ProgramError},
53    solana_slot_history::{
54        sysvar::{check_id, id, ID},
55        SlotHistory,
56    },
57};
58
59impl Sysvar for SlotHistory {
60    // override
61    fn size_of() -> usize {
62        // hard-coded so that we don't have to construct an empty
63        131_097 // golden, update if MAX_ENTRIES changes
64    }
65    fn from_account_info(_account_info: &AccountInfo) -> Result<Self, ProgramError> {
66        // This sysvar is too large to bincode::deserialize in-program
67        Err(ProgramError::UnsupportedSysvar)
68    }
69}
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74    #[test]
75    fn test_size_of() {
76        assert_eq!(
77            SlotHistory::size_of(),
78            bincode::serialized_size(&SlotHistory::default()).unwrap() as usize
79        );
80    }
81}