Skip to main content

solana_sysvar/
last_restart_slot.rs

1//! Information about the last restart slot (hard fork).
2//!
3//! The _last restart sysvar_ provides access to the last restart slot kept in the
4//! bank fork for the slot on the fork that executes the current transaction.
5//! In case there was no fork it returns _0_.
6//!
7//! [`LastRestartSlot`] implements [`crate::Sysvar::get`] and can be loaded efficiently without
8//! passing the sysvar account ID to the program.
9//!
10//! See also the Solana [SIMD proposal][simd].
11//!
12//! [simd]: https://github.com/solana-foundation/solana-improvement-documents/blob/main/proposals/0047-syscall-and-sysvar-for-last-restart-slot.md
13//!
14//! # Examples
15//!
16//! Accessing via on-chain program directly:
17//!
18//! ```no_run
19//! # use solana_account_info::AccountInfo;
20//! # use solana_msg::msg;
21//! # use solana_sysvar::Sysvar;
22//! # use solana_program_error::ProgramResult;
23//! # use solana_pubkey::Pubkey;
24//! # use solana_last_restart_slot::LastRestartSlot;
25//!
26//! fn process_instruction(
27//!     program_id: &Pubkey,
28//!     accounts: &[AccountInfo],
29//!     instruction_data: &[u8],
30//! ) -> ProgramResult {
31//!
32//!     let last_restart_slot = LastRestartSlot::get();
33//!     msg!("last restart slot: {:?}", last_restart_slot);
34//!
35//!     Ok(())
36//! }
37//! ```
38//!
39#[cfg(feature = "bincode")]
40use crate::SysvarSerialize;
41pub use {
42    solana_last_restart_slot::{LastRestartSlot, SIZE},
43    solana_sdk_ids::sysvar::last_restart_slot::{check_id, id, ID},
44};
45
46#[cfg(feature = "bincode")]
47impl SysvarSerialize for LastRestartSlot {}
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    #[cfg(feature = "bincode")]
55    fn test_last_restart_slot_size_matches_bincode() {
56        // Prove that LastRestartSlot's in-memory layout matches its bincode serialization.
57        let slot = LastRestartSlot::default();
58        let bincode_size = bincode::serialized_size(&slot).unwrap() as usize;
59
60        assert_eq!(
61            SIZE, bincode_size,
62            "LastRestartSlot SIZE ({SIZE}) must match bincode size ({bincode_size})",
63        );
64    }
65}