Skip to main content

solana_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 [`crate::Sysvar::get`] method always returns [`ProgramError::UnsupportedSysvar`]
6//! because this sysvar account is too large to process on-chain. Thus this
7//! sysvar cannot be accessed on chain, though one can still use the
8//! [`SysvarId::id`], [`SysvarId::check_id`] and [`SIZE`] in an on-chain program,
9//! and it can be accessed off-chain through RPC.
10//!
11//! [`SysvarId::id`]: https://docs.rs/solana-sysvar-id/latest/solana_sysvar_id/trait.SysvarId.html#tymethod.id
12//! [`SysvarId::check_id`]: https://docs.rs/solana-sysvar-id/latest/solana_sysvar_id/trait.SysvarId.html#tymethod.check_id
13//!
14//! # Examples
15//!
16//! Calling via the RPC client:
17//!
18//! ```
19//! # use solana_example_mocks::solana_account;
20//! # use solana_example_mocks::solana_rpc_client;
21//! # use solana_rpc_client::rpc_client::RpcClient;
22//! # use solana_account::Account;
23//! # use solana_slot_history::SlotHistory;
24//! # use solana_sdk_ids::sysvar::slot_history;
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_ids::system_program::ID,
34//! #       executable: false,
35//! #   });
36//! #
37//!     let slot_history = client.get_account(&slot_history::ID)?;
38//!     let data: SlotHistory = bincode::deserialize(&slot_history.data)?;
39//!
40//!     Ok(())
41//! }
42//! #
43//! # let client = RpcClient::new(String::new());
44//! # print_sysvar_slot_history(&client)?;
45//! #
46//! # Ok::<(), anyhow::Error>(())
47//! ```
48
49#[cfg(feature = "bincode")]
50#[allow(deprecated)]
51use crate::SysvarSerialize;
52pub use {
53    solana_account_info::AccountInfo,
54    solana_program_error::ProgramError,
55    solana_sdk_ids::sysvar::slot_history::{check_id, id, ID},
56    solana_slot_history::{SlotHistory, SIZE},
57};
58
59#[cfg(feature = "bincode")]
60#[allow(deprecated)]
61impl SysvarSerialize for SlotHistory {
62    // override
63    fn size_of() -> usize {
64        SIZE
65    }
66    fn from_account_info(_account_info: &AccountInfo) -> Result<Self, ProgramError> {
67        // This sysvar is too large to bincode::deserialize in-program
68        Err(ProgramError::UnsupportedSysvar)
69    }
70}
71
72#[cfg(test)]
73mod tests {
74    use super::*;
75    #[test]
76    #[allow(deprecated)]
77    fn test_size_of() {
78        assert_eq!(
79            SlotHistory::size_of(),
80            bincode::serialized_size(&SlotHistory::default()).unwrap() as usize
81        );
82    }
83}