cbe_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 cbe_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 Cartallum CBE 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 cbe_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 Cartallum CBE [documentation on sysvars][sysvardoc].
81//!
82//! [sysvardoc]: https://docs.cartallum.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_schedule;
91pub mod fees;
92pub mod instructions;
93pub mod recent_blockhashes;
94pub mod rent;
95pub mod rewards;
96pub mod slot_hashes;
97pub mod slot_history;
98pub mod stake_history;
99
100lazy_static! {
101    pub static ref ALL_IDS: Vec<Pubkey> = vec![
102        clock::id(),
103        epoch_schedule::id(),
104        #[allow(deprecated)]
105        fees::id(),
106        #[allow(deprecated)]
107        recent_blockhashes::id(),
108        rent::id(),
109        rewards::id(),
110        slot_hashes::id(),
111        slot_history::id(),
112        stake_history::id(),
113        instructions::id(),
114    ];
115}
116
117/// Returns `true` of the given `Pubkey` is a sysvar account.
118pub fn is_sysvar_id(id: &Pubkey) -> bool {
119    ALL_IDS.iter().any(|key| key == id)
120}
121
122/// Declares an ID that implements [`SysvarId`].
123#[macro_export]
124macro_rules! declare_sysvar_id(
125    ($name:expr, $type:ty) => (
126        $crate::declare_id!($name);
127
128        impl $crate::sysvar::SysvarId for $type {
129            fn id() -> $crate::pubkey::Pubkey {
130                id()
131            }
132
133            fn check_id(pubkey: &$crate::pubkey::Pubkey) -> bool {
134                check_id(pubkey)
135            }
136        }
137
138        #[cfg(test)]
139        #[test]
140        fn test_sysvar_id() {
141            assert!($crate::sysvar::is_sysvar_id(&id()), "sysvar::is_sysvar_id() doesn't know about {}", $name);
142        }
143    )
144);
145
146/// Same as [`declare_sysvar_id`] except that it reports that this ID has been deprecated.
147#[macro_export]
148macro_rules! declare_deprecated_sysvar_id(
149    ($name:expr, $type:ty) => (
150        $crate::declare_deprecated_id!($name);
151
152        impl $crate::sysvar::SysvarId for $type {
153            fn id() -> $crate::pubkey::Pubkey {
154                #[allow(deprecated)]
155                id()
156            }
157
158            fn check_id(pubkey: &$crate::pubkey::Pubkey) -> bool {
159                #[allow(deprecated)]
160                check_id(pubkey)
161            }
162        }
163
164        #[cfg(test)]
165        #[test]
166        fn test_sysvar_id() {
167            assert!($crate::sysvar::is_sysvar_id(&id()), "sysvar::is_sysvar_id() doesn't know about {}", $name);
168        }
169    )
170);
171
172// Owner pubkey for sysvar accounts
173crate::declare_id!("Sysvar1111111111111111111111111111111111111");
174
175/// A type that holds sysvar data and has an associated sysvar `Pubkey`.
176pub trait SysvarId {
177    /// The `Pubkey` of the sysvar.
178    fn id() -> Pubkey;
179
180    /// Returns `true` if the given pubkey is the program ID.
181    fn check_id(pubkey: &Pubkey) -> bool;
182}
183
184/// A type that holds sysvar data.
185pub trait Sysvar:
186    SysvarId + Default + Sized + serde::Serialize + serde::de::DeserializeOwned
187{
188    /// The size in bytes of the sysvar as serialized account data.
189    fn size_of() -> usize {
190        bincode::serialized_size(&Self::default()).unwrap() as usize
191    }
192
193    /// Deserializes the sysvar from its `AccountInfo`.
194    ///
195    /// # Errors
196    ///
197    /// If `account_info` does not have the same ID as the sysvar this function
198    /// returns [`ProgramError::InvalidArgument`].
199    fn from_account_info(account_info: &AccountInfo) -> Result<Self, ProgramError> {
200        if !Self::check_id(account_info.unsigned_key()) {
201            return Err(ProgramError::InvalidArgument);
202        }
203        bincode::deserialize(&account_info.data.borrow()).map_err(|_| ProgramError::InvalidArgument)
204    }
205
206    /// Serializes the sysvar to `AccountInfo`.
207    ///
208    /// # Errors
209    ///
210    /// Returns `None` if serialization failed.
211    fn to_account_info(&self, account_info: &mut AccountInfo) -> Option<()> {
212        bincode::serialize_into(&mut account_info.data.borrow_mut()[..], self).ok()
213    }
214
215    /// Load the sysvar directly from the runtime.
216    ///
217    /// This is the preferred way to load a sysvar. Calling this method does not
218    /// incur any deserialization overhead, and does not require the sysvar
219    /// account to be passed to the program.
220    ///
221    /// Not all sysvars support this method. If not, it returns
222    /// [`ProgramError::UnsupportedSysvar`].
223    fn get() -> Result<Self, ProgramError> {
224        Err(ProgramError::UnsupportedSysvar)
225    }
226}
227
228/// Implements the [`Sysvar::get`] method for both SBF and host targets.
229#[macro_export]
230macro_rules! impl_sysvar_get {
231    ($syscall_name:ident) => {
232        fn get() -> Result<Self, ProgramError> {
233            let mut var = Self::default();
234            let var_addr = &mut var as *mut _ as *mut u8;
235
236            #[cfg(target_os = "cbe")]
237            let result = unsafe { $crate::syscalls::$syscall_name(var_addr) };
238
239            #[cfg(not(target_os = "cbe"))]
240            let result = $crate::program_stubs::$syscall_name(var_addr);
241
242            match result {
243                $crate::entrypoint::SUCCESS => Ok(var),
244                e => Err(e.into()),
245            }
246        }
247    };
248}
249
250#[cfg(test)]
251mod tests {
252    use {
253        super::*,
254        crate::{clock::Epoch, program_error::ProgramError, pubkey::Pubkey},
255        std::{cell::RefCell, rc::Rc},
256    };
257
258    #[repr(C)]
259    #[derive(Serialize, Deserialize, Debug, Default, PartialEq, Eq)]
260    struct TestSysvar {
261        something: Pubkey,
262    }
263    crate::declare_id!("TestSysvar111111111111111111111111111111111");
264    impl crate::sysvar::SysvarId for TestSysvar {
265        fn id() -> crate::pubkey::Pubkey {
266            id()
267        }
268
269        fn check_id(pubkey: &crate::pubkey::Pubkey) -> bool {
270            check_id(pubkey)
271        }
272    }
273    impl Sysvar for TestSysvar {}
274
275    #[test]
276    fn test_sysvar_account_info_to_from() {
277        let test_sysvar = TestSysvar::default();
278        let key = crate::sysvar::tests::id();
279        let wrong_key = Pubkey::new_unique();
280        let owner = Pubkey::new_unique();
281        let mut scoobies = 42;
282        let mut data = vec![0_u8; TestSysvar::size_of()];
283        let mut account_info = AccountInfo::new(
284            &key,
285            false,
286            true,
287            &mut scoobies,
288            &mut data,
289            &owner,
290            false,
291            Epoch::default(),
292        );
293
294        test_sysvar.to_account_info(&mut account_info).unwrap();
295        let new_test_sysvar = TestSysvar::from_account_info(&account_info).unwrap();
296        assert_eq!(test_sysvar, new_test_sysvar);
297
298        account_info.key = &wrong_key;
299        assert_eq!(
300            TestSysvar::from_account_info(&account_info),
301            Err(ProgramError::InvalidArgument)
302        );
303
304        let mut small_data = vec![];
305        account_info.data = Rc::new(RefCell::new(&mut small_data));
306        assert_eq!(test_sysvar.to_account_info(&mut account_info), None);
307    }
308}