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 [`SysvarSerialize::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//! [`SysvarSerialize::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_example_mocks::solana_account;
21//! # use solana_example_mocks::solana_rpc_client;
22//! # use solana_rpc_client::rpc_client::RpcClient;
23//! # use solana_account::Account;
24//! # use solana_slot_history::SlotHistory;
25//! # use solana_sdk_ids::sysvar::slot_history;
26//! # use anyhow::Result;
27//! #
28//! fn print_sysvar_slot_history(client: &RpcClient) -> Result<()> {
29//! #   let slot_history = SlotHistory::default();
30//! #   let data: Vec<u8> = bincode::serialize(&slot_history)?;
31//! #   client.set_get_account_response(slot_history::ID, Account {
32//! #       lamports: 913326000,
33//! #       data,
34//! #       owner: solana_sdk_ids::system_program::ID,
35//! #       executable: false,
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;
51#[cfg(feature = "bincode")]
52use crate::SysvarSerialize;
53pub use {
54    solana_account_info::AccountInfo,
55    solana_program_error::ProgramError,
56    solana_sdk_ids::sysvar::slot_history::{check_id, id, ID},
57    solana_slot_history::SlotHistory,
58};
59impl Sysvar for SlotHistory {}
60#[cfg(feature = "bincode")]
61impl SysvarSerialize for SlotHistory {
62    // override
63    fn size_of() -> usize {
64        // hard-coded so that we don't have to construct an empty
65        131_097 // golden, update if MAX_ENTRIES changes
66    }
67    fn from_account_info(_account_info: &AccountInfo) -> Result<Self, ProgramError> {
68        // This sysvar is too large to bincode::deserialize in-program
69        Err(ProgramError::UnsupportedSysvar)
70    }
71}
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76    #[test]
77    fn test_size_of() {
78        assert_eq!(
79            SlotHistory::size_of(),
80            bincode::serialized_size(&SlotHistory::default()).unwrap() as usize
81        );
82    }
83}