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