miraland_program/sysvar/
mod.rs

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