gemachain_program/sysvar/
mod.rs

1//! named accounts for synthesized data accounts for bank state, etc.
2//!
3use crate::{account_info::AccountInfo, program_error::ProgramError, pubkey::Pubkey};
4
5pub mod clock;
6pub mod epoch_schedule;
7pub mod fees;
8pub mod instructions;
9pub mod recent_blockhashes;
10pub mod rent;
11pub mod rewards;
12pub mod slot_hashes;
13pub mod slot_history;
14pub mod stake_history;
15
16#[allow(deprecated)]
17pub fn is_sysvar_id(id: &Pubkey) -> bool {
18    clock::check_id(id)
19        || epoch_schedule::check_id(id)
20        || fees::check_id(id)
21        || recent_blockhashes::check_id(id)
22        || rent::check_id(id)
23        || rewards::check_id(id)
24        || slot_hashes::check_id(id)
25        || slot_history::check_id(id)
26        || stake_history::check_id(id)
27        || instructions::check_id(id)
28}
29
30#[macro_export]
31macro_rules! declare_sysvar_id(
32    ($name:expr, $type:ty) => (
33        $crate::declare_id!($name);
34
35        impl $crate::sysvar::SysvarId for $type {
36            fn id() -> $crate::pubkey::Pubkey {
37                id()
38            }
39
40            fn check_id(pubkey: &$crate::pubkey::Pubkey) -> bool {
41                check_id(pubkey)
42            }
43        }
44
45        #[cfg(test)]
46        #[test]
47        fn test_sysvar_id() {
48            if !$crate::sysvar::is_sysvar_id(&id()) {
49                panic!("sysvar::is_sysvar_id() doesn't know about {}", $name);
50            }
51        }
52    )
53);
54
55#[macro_export]
56macro_rules! declare_deprecated_sysvar_id(
57    ($name:expr, $type:ty) => (
58        $crate::declare_deprecated_id!($name);
59
60        impl $crate::sysvar::SysvarId for $type {
61            fn id() -> $crate::pubkey::Pubkey {
62                #[allow(deprecated)]
63                id()
64            }
65
66            fn check_id(pubkey: &$crate::pubkey::Pubkey) -> bool {
67                #[allow(deprecated)]
68                check_id(pubkey)
69            }
70        }
71
72        #[cfg(test)]
73        #[test]
74        fn test_sysvar_id() {
75            #[allow(deprecated)]
76            if !$crate::sysvar::is_sysvar_id(&id()) {
77                panic!("sysvar::is_sysvar_id() doesn't know about {}", $name);
78            }
79        }
80    )
81);
82
83// Owner pubkey for sysvar accounts
84crate::declare_id!("Sysvar1111111111111111111111111111111111111");
85
86pub trait SysvarId {
87    fn id() -> Pubkey;
88
89    fn check_id(pubkey: &Pubkey) -> bool;
90}
91
92// Sysvar utilities
93pub trait Sysvar:
94    SysvarId + Default + Sized + serde::Serialize + serde::de::DeserializeOwned
95{
96    fn size_of() -> usize {
97        bincode::serialized_size(&Self::default()).unwrap() as usize
98    }
99    fn from_account_info(account_info: &AccountInfo) -> Result<Self, ProgramError> {
100        if !Self::check_id(account_info.unsigned_key()) {
101            return Err(ProgramError::InvalidArgument);
102        }
103        bincode::deserialize(&account_info.data.borrow()).map_err(|_| ProgramError::InvalidArgument)
104    }
105    fn to_account_info(&self, account_info: &mut AccountInfo) -> Option<()> {
106        bincode::serialize_into(&mut account_info.data.borrow_mut()[..], self).ok()
107    }
108    fn get() -> Result<Self, ProgramError> {
109        Err(ProgramError::UnsupportedSysvar)
110    }
111}
112
113#[macro_export]
114macro_rules! impl_sysvar_get {
115    ($syscall_name:ident) => {
116        fn get() -> Result<Self, ProgramError> {
117            let mut var = Self::default();
118            let var_addr = &mut var as *mut _ as *mut u8;
119
120            #[cfg(target_arch = "bpf")]
121            let result = unsafe {
122                extern "C" {
123                    fn $syscall_name(var_addr: *mut u8) -> u64;
124                }
125                $syscall_name(var_addr)
126            };
127            #[cfg(not(target_arch = "bpf"))]
128            let result = crate::program_stubs::$syscall_name(var_addr);
129
130            match result {
131                crate::entrypoint::SUCCESS => Ok(var),
132                e => Err(e.into()),
133            }
134        }
135    };
136}
137
138#[cfg(test)]
139mod tests {
140    use super::*;
141    use crate::{clock::Epoch, program_error::ProgramError, pubkey::Pubkey};
142    use std::{cell::RefCell, rc::Rc};
143
144    #[repr(C)]
145    #[derive(Serialize, Deserialize, Debug, Default, PartialEq)]
146    struct TestSysvar {
147        something: Pubkey,
148    }
149    crate::declare_id!("TestSysvar111111111111111111111111111111111");
150    impl crate::sysvar::SysvarId for TestSysvar {
151        fn id() -> crate::pubkey::Pubkey {
152            id()
153        }
154
155        fn check_id(pubkey: &crate::pubkey::Pubkey) -> bool {
156            check_id(pubkey)
157        }
158    }
159    impl Sysvar for TestSysvar {}
160
161    #[test]
162    fn test_sysvar_account_info_to_from() {
163        let test_sysvar = TestSysvar::default();
164        let key = crate::sysvar::tests::id();
165        let wrong_key = Pubkey::new_unique();
166        let owner = Pubkey::new_unique();
167        let mut carats = 42;
168        let mut data = vec![0_u8; TestSysvar::size_of()];
169        let mut account_info = AccountInfo::new(
170            &key,
171            false,
172            true,
173            &mut carats,
174            &mut data,
175            &owner,
176            false,
177            Epoch::default(),
178        );
179
180        test_sysvar.to_account_info(&mut account_info).unwrap();
181        let new_test_sysvar = TestSysvar::from_account_info(&account_info).unwrap();
182        assert_eq!(test_sysvar, new_test_sysvar);
183
184        account_info.key = &wrong_key;
185        assert_eq!(
186            TestSysvar::from_account_info(&account_info),
187            Err(ProgramError::InvalidArgument)
188        );
189
190        let mut small_data = vec![];
191        account_info.data = Rc::new(RefCell::new(&mut small_data));
192        assert_eq!(test_sysvar.to_account_info(&mut account_info), None);
193    }
194}