Files
solana/programs/bpf/rust/sysvar/src/lib.rs

96 lines
2.9 KiB
Rust
Raw Normal View History

2021-03-30 12:16:21 -07:00
//! @brief Example Rust-based BPF program that tests sysvar use
extern crate solana_program;
#[allow(deprecated)]
use solana_program::sysvar::recent_blockhashes::RecentBlockhashes;
use solana_program::{
account_info::AccountInfo,
entrypoint,
entrypoint::ProgramResult,
msg,
program_error::ProgramError,
pubkey::Pubkey,
system_program,
sysvar::{
2021-07-29 10:48:14 -07:00
self, clock::Clock, epoch_schedule::EpochSchedule, instructions, rent::Rent,
slot_hashes::SlotHashes, slot_history::SlotHistory, stake_history::StakeHistory, Sysvar,
},
};
entrypoint!(process_instruction);
2020-12-13 17:26:34 -08:00
#[allow(clippy::unnecessary_wraps)]
pub fn process_instruction(
2020-01-24 13:41:14 -08:00
_program_id: &Pubkey,
2020-01-27 18:27:44 -08:00
accounts: &[AccountInfo],
2020-01-24 13:41:14 -08:00
_instruction_data: &[u8],
) -> ProgramResult {
// Clock
{
msg!("Clock identifier:");
sysvar::clock::id().log();
let clock = Clock::from_account_info(&accounts[2]).unwrap();
assert_ne!(clock, Clock::default());
2021-04-12 16:04:57 -07:00
let got_clock = Clock::get()?;
assert_eq!(clock, got_clock);
}
// Epoch Schedule
{
msg!("EpochSchedule identifier:");
sysvar::epoch_schedule::id().log();
let epoch_schedule = EpochSchedule::from_account_info(&accounts[3]).unwrap();
assert_eq!(epoch_schedule, EpochSchedule::default());
2021-04-12 16:04:57 -07:00
let got_epoch_schedule = EpochSchedule::get()?;
assert_eq!(epoch_schedule, got_epoch_schedule);
}
// Instructions
msg!("Instructions identifier:");
sysvar::instructions::id().log();
assert_eq!(*accounts[4].owner, system_program::id());
let index = instructions::load_current_index(&accounts[4].try_borrow_data()?);
assert_eq!(0, index);
// Recent Blockhashes
#[allow(deprecated)]
{
msg!("RecentBlockhashes identifier:");
sysvar::recent_blockhashes::id().log();
let recent_blockhashes = RecentBlockhashes::from_account_info(&accounts[5]).unwrap();
assert_ne!(recent_blockhashes, RecentBlockhashes::default());
}
// Rent
{
msg!("Rent identifier:");
sysvar::rent::id().log();
let rent = Rent::from_account_info(&accounts[6]).unwrap();
assert_eq!(rent, Rent::default());
2021-04-12 16:04:57 -07:00
let got_rent = Rent::get()?;
assert_eq!(rent, got_rent);
}
// Slot Hashes
msg!("SlotHashes identifier:");
sysvar::slot_hashes::id().log();
assert_eq!(
Err(ProgramError::UnsupportedSysvar),
SlotHashes::from_account_info(&accounts[7])
);
// Slot History
msg!("SlotHistory identifier:");
sysvar::slot_history::id().log();
assert_eq!(
Err(ProgramError::UnsupportedSysvar),
SlotHistory::from_account_info(&accounts[8])
);
// Stake History
msg!("StakeHistory identifier:");
sysvar::stake_history::id().log();
let _ = StakeHistory::from_account_info(&accounts[9]).unwrap();
Ok(())
}