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