solana_sysvar/
lib.rs

1#![cfg_attr(docsrs, feature(doc_auto_cfg))]
2#![cfg_attr(feature = "frozen-abi", feature(min_specialization))]
3//! Access to special accounts with dynamically-updated data.
4//!
5//! Sysvars are special accounts that contain dynamically-updated data about the
6//! network cluster, the blockchain history, and the executing transaction. Each
7//! sysvar is defined in its own submodule within this module. The [`clock`],
8//! [`epoch_schedule`], and [`rent`] sysvars are most useful to on-chain
9//! programs.
10//!
11//! Simple sysvars implement the [`Sysvar::get`] method, which loads a sysvar
12//! directly from the runtime, as in this example that logs the `clock` sysvar:
13//!
14//! ```
15//! use solana_account_info::AccountInfo;
16//! use solana_msg::msg;
17//! use solana_sysvar::Sysvar;
18//! use solana_program_error::ProgramResult;
19//! use solana_pubkey::Pubkey;
20//!
21//! fn process_instruction(
22//!     program_id: &Pubkey,
23//!     accounts: &[AccountInfo],
24//!     instruction_data: &[u8],
25//! ) -> ProgramResult {
26//!     let clock = solana_clock::Clock::get()?;
27//!     msg!("clock: {:#?}", clock);
28//!     Ok(())
29//! }
30//! ```
31//!
32//! Since Solana sysvars are accounts, if the `AccountInfo` is provided to the
33//! program, then the program can deserialize the sysvar with
34//! [`SysvarSerialize::from_account_info`] to access its data, as in this example that
35//! again logs the [`clock`] sysvar.
36//!
37//! ```
38//! use solana_account_info::{AccountInfo, next_account_info};
39//! use solana_msg::msg;
40//! use solana_sysvar::{Sysvar, SysvarSerialize};
41//! use solana_program_error::ProgramResult;
42//! use solana_pubkey::Pubkey;
43//!
44//! fn process_instruction(
45//!     program_id: &Pubkey,
46//!     accounts: &[AccountInfo],
47//!     instruction_data: &[u8],
48//! ) -> ProgramResult {
49//!     let account_info_iter = &mut accounts.iter();
50//!     let clock_account = next_account_info(account_info_iter)?;
51//!     let clock = solana_clock::Clock::from_account_info(&clock_account)?;
52//!     msg!("clock: {:#?}", clock);
53//!     Ok(())
54//! }
55//! ```
56//!
57//! When possible, programs should prefer to call `Sysvar::get` instead of
58//! deserializing with `Sysvar::from_account_info`, as the latter imposes extra
59//! overhead of deserialization while also requiring the sysvar account address
60//! be passed to the program, wasting the limited space available to
61//! transactions. Deserializing sysvars that can instead be retrieved with
62//! `Sysvar::get` should be only be considered for compatibility with older
63//! programs that pass around sysvar accounts.
64//!
65//! Some sysvars are too large to deserialize within a program, and
66//! `Sysvar::from_account_info` returns an error, or the serialization attempt
67//! will exhaust the program's compute budget. Some sysvars do not implement
68//! `Sysvar::get` and return an error. Some sysvars have custom deserializers
69//! that do not implement the `Sysvar` trait. These cases are documented in the
70//! modules for individual sysvars.
71//!
72//! All sysvar accounts are owned by the account identified by [`sysvar::ID`].
73//!
74//! [`sysvar::ID`]: https://docs.rs/solana-sdk-ids/latest/solana_sdk_ids/sysvar/constant.ID.html
75//!
76//! For more details see the Solana [documentation on sysvars][sysvardoc].
77//!
78//! [sysvardoc]: https://docs.solanalabs.com/runtime/sysvars
79
80// hidden re-exports to make macros work
81pub mod __private {
82    #[cfg(target_os = "solana")]
83    pub use solana_define_syscall::definitions;
84    pub use {solana_program_entrypoint::SUCCESS, solana_program_error::ProgramError};
85}
86#[cfg(feature = "bincode")]
87use {solana_account_info::AccountInfo, solana_sysvar_id::SysvarId};
88use {solana_program_error::ProgramError, solana_pubkey::Pubkey};
89
90pub mod clock;
91pub mod epoch_rewards;
92pub mod epoch_schedule;
93pub mod fees;
94pub mod last_restart_slot;
95pub mod program_stubs;
96pub mod recent_blockhashes;
97pub mod rent;
98pub mod rewards;
99pub mod slot_hashes;
100pub mod slot_history;
101
102/// Return value indicating that the  `offset + length` is greater than the length of
103/// the sysvar data.
104//
105// Defined in the bpf loader as [`OFFSET_LENGTH_EXCEEDS_SYSVAR`](https://github.com/anza-xyz/agave/blob/master/programs/bpf_loader/src/syscalls/sysvar.rs#L172).
106const OFFSET_LENGTH_EXCEEDS_SYSVAR: u64 = 1;
107
108/// Return value indicating that the sysvar was not found.
109//
110// Defined in the bpf loader as [`SYSVAR_NOT_FOUND`](https://github.com/anza-xyz/agave/blob/master/programs/bpf_loader/src/syscalls/sysvar.rs#L171).
111const SYSVAR_NOT_FOUND: u64 = 2;
112
113/// Interface for loading a sysvar.
114pub trait Sysvar: Default + Sized {
115    /// Load the sysvar directly from the runtime.
116    ///
117    /// This is the preferred way to load a sysvar. Calling this method does not
118    /// incur any deserialization overhead, and does not require the sysvar
119    /// account to be passed to the program.
120    ///
121    /// Not all sysvars support this method. If not, it returns
122    /// [`ProgramError::UnsupportedSysvar`].
123    fn get() -> Result<Self, ProgramError> {
124        Err(ProgramError::UnsupportedSysvar)
125    }
126}
127
128#[cfg(feature = "bincode")]
129/// A type that holds sysvar data.
130pub trait SysvarSerialize:
131    Sysvar + SysvarId + serde::Serialize + serde::de::DeserializeOwned
132{
133    /// The size in bytes of the sysvar as serialized account data.
134    fn size_of() -> usize {
135        bincode::serialized_size(&Self::default()).unwrap() as usize
136    }
137
138    /// Deserializes the sysvar from its `AccountInfo`.
139    ///
140    /// # Errors
141    ///
142    /// If `account_info` does not have the same ID as the sysvar this function
143    /// returns [`ProgramError::InvalidArgument`].
144    fn from_account_info(account_info: &AccountInfo) -> Result<Self, ProgramError> {
145        if !Self::check_id(account_info.unsigned_key()) {
146            return Err(ProgramError::InvalidArgument);
147        }
148        bincode::deserialize(&account_info.data.borrow()).map_err(|_| ProgramError::InvalidArgument)
149    }
150
151    /// Serializes the sysvar to `AccountInfo`.
152    ///
153    /// # Errors
154    ///
155    /// Returns `None` if serialization failed.
156    fn to_account_info(&self, account_info: &mut AccountInfo) -> Option<()> {
157        bincode::serialize_into(&mut account_info.data.borrow_mut()[..], self).ok()
158    }
159}
160
161/// Implements the [`Sysvar::get`] method for both SBF and host targets.
162#[macro_export]
163macro_rules! impl_sysvar_get {
164    ($syscall_name:ident) => {
165        fn get() -> Result<Self, $crate::__private::ProgramError> {
166            let mut var = Self::default();
167            let var_addr = &mut var as *mut _ as *mut u8;
168
169            #[cfg(target_os = "solana")]
170            let result = unsafe { $crate::__private::definitions::$syscall_name(var_addr) };
171
172            #[cfg(not(target_os = "solana"))]
173            let result = $crate::program_stubs::$syscall_name(var_addr);
174
175            match result {
176                $crate::__private::SUCCESS => Ok(var),
177                // Unexpected errors are folded into `UnsupportedSysvar`.
178                _ => Err($crate::__private::ProgramError::UnsupportedSysvar),
179            }
180        }
181    };
182}
183
184/// Handler for retrieving a slice of sysvar data from the `sol_get_sysvar`
185/// syscall.
186pub fn get_sysvar(
187    dst: &mut [u8],
188    sysvar_id: &Pubkey,
189    offset: u64,
190    length: u64,
191) -> Result<(), solana_program_error::ProgramError> {
192    // Check that the provided destination buffer is large enough to hold the
193    // requested data.
194    if dst.len() < length as usize {
195        return Err(solana_program_error::ProgramError::InvalidArgument);
196    }
197
198    let sysvar_id = sysvar_id as *const _ as *const u8;
199    let var_addr = dst as *mut _ as *mut u8;
200
201    #[cfg(target_os = "solana")]
202    let result = unsafe {
203        solana_define_syscall::definitions::sol_get_sysvar(sysvar_id, var_addr, offset, length)
204    };
205
206    #[cfg(not(target_os = "solana"))]
207    let result = crate::program_stubs::sol_get_sysvar(sysvar_id, var_addr, offset, length);
208
209    match result {
210        solana_program_entrypoint::SUCCESS => Ok(()),
211        OFFSET_LENGTH_EXCEEDS_SYSVAR => Err(solana_program_error::ProgramError::InvalidArgument),
212        SYSVAR_NOT_FOUND => Err(solana_program_error::ProgramError::UnsupportedSysvar),
213        // Unexpected errors are folded into `UnsupportedSysvar`.
214        _ => Err(solana_program_error::ProgramError::UnsupportedSysvar),
215    }
216}
217
218#[cfg(test)]
219mod tests {
220    use {
221        super::*,
222        crate::program_stubs::{set_syscall_stubs, SyscallStubs},
223        serde_derive::{Deserialize, Serialize},
224        solana_program_entrypoint::SUCCESS,
225        solana_program_error::ProgramError,
226        solana_pubkey::Pubkey,
227        std::{cell::RefCell, rc::Rc},
228    };
229
230    #[repr(C)]
231    #[derive(Serialize, Deserialize, Debug, Default, PartialEq, Eq)]
232    struct TestSysvar {
233        something: Pubkey,
234    }
235    solana_pubkey::declare_id!("TestSysvar111111111111111111111111111111111");
236    impl solana_sysvar_id::SysvarId for TestSysvar {
237        fn id() -> solana_pubkey::Pubkey {
238            id()
239        }
240
241        fn check_id(pubkey: &solana_pubkey::Pubkey) -> bool {
242            check_id(pubkey)
243        }
244    }
245    impl Sysvar for TestSysvar {}
246    impl SysvarSerialize for TestSysvar {}
247
248    // NOTE tests that use this mock MUST carry the #[serial] attribute
249    struct MockGetSysvarSyscall {
250        data: Vec<u8>,
251    }
252    impl SyscallStubs for MockGetSysvarSyscall {
253        #[allow(clippy::arithmetic_side_effects)]
254        fn sol_get_sysvar(
255            &self,
256            _sysvar_id_addr: *const u8,
257            var_addr: *mut u8,
258            offset: u64,
259            length: u64,
260        ) -> u64 {
261            let slice = unsafe { std::slice::from_raw_parts_mut(var_addr, length as usize) };
262            slice.copy_from_slice(&self.data[offset as usize..(offset + length) as usize]);
263            SUCCESS
264        }
265    }
266    pub fn mock_get_sysvar_syscall(data: &[u8]) {
267        set_syscall_stubs(Box::new(MockGetSysvarSyscall {
268            data: data.to_vec(),
269        }));
270    }
271
272    #[test]
273    fn test_sysvar_account_info_to_from() {
274        let test_sysvar = TestSysvar::default();
275        let key = id();
276        let wrong_key = Pubkey::new_unique();
277        let owner = Pubkey::new_unique();
278        let mut lamports = 42;
279        let mut data = vec![0_u8; TestSysvar::size_of()];
280        let mut account_info =
281            AccountInfo::new(&key, false, true, &mut lamports, &mut data, &owner, false);
282
283        test_sysvar.to_account_info(&mut account_info).unwrap();
284        let new_test_sysvar = TestSysvar::from_account_info(&account_info).unwrap();
285        assert_eq!(test_sysvar, new_test_sysvar);
286
287        account_info.key = &wrong_key;
288        assert_eq!(
289            TestSysvar::from_account_info(&account_info),
290            Err(ProgramError::InvalidArgument)
291        );
292
293        let mut small_data = vec![];
294        account_info.data = Rc::new(RefCell::new(&mut small_data));
295        assert_eq!(test_sysvar.to_account_info(&mut account_info), None);
296    }
297}