2021-03-30 12:16:21 -07:00
|
|
|
//! @brief Example Rust-based BPF program that tests sysvar use
|
2019-09-10 18:53:02 -07:00
|
|
|
|
2020-10-23 17:22:10 -07:00
|
|
|
extern crate solana_program;
|
2021-07-27 16:34:21 -07:00
|
|
|
#[allow(deprecated)]
|
2021-08-01 17:31:11 -07:00
|
|
|
use solana_program::sysvar::recent_blockhashes::RecentBlockhashes;
|
2020-10-23 17:22:10 -07:00
|
|
|
use solana_program::{
|
2019-09-10 18:53:02 -07:00
|
|
|
account_info::AccountInfo,
|
2020-02-04 12:25:42 -08:00
|
|
|
entrypoint,
|
|
|
|
entrypoint::ProgramResult,
|
2020-11-30 13:28:58 -08:00
|
|
|
msg,
|
2021-04-06 00:08:03 -07:00
|
|
|
program_error::ProgramError,
|
2019-09-10 18:53:02 -07:00
|
|
|
pubkey::Pubkey,
|
|
|
|
sysvar::{
|
2021-07-29 10:48:14 -07:00
|
|
|
self, clock::Clock, epoch_schedule::EpochSchedule, instructions, rent::Rent,
|
2021-07-27 16:34:21 -07:00
|
|
|
slot_hashes::SlotHashes, slot_history::SlotHistory, stake_history::StakeHistory, Sysvar,
|
2019-09-10 18:53:02 -07:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
entrypoint!(process_instruction);
|
2020-12-13 17:26:34 -08:00
|
|
|
#[allow(clippy::unnecessary_wraps)]
|
2021-04-07 01:47:15 -07:00
|
|
|
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],
|
2020-02-04 12:25:42 -08:00
|
|
|
) -> ProgramResult {
|
2019-09-10 18:53:02 -07:00
|
|
|
// Clock
|
2021-04-07 01:47:15 -07:00
|
|
|
{
|
|
|
|
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);
|
2021-04-07 01:47:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
2021-04-07 01:47:15 -07:00
|
|
|
}
|
2019-09-10 18:53:02 -07:00
|
|
|
|
2021-04-06 00:08:03 -07:00
|
|
|
// Instructions
|
|
|
|
msg!("Instructions identifier:");
|
|
|
|
sysvar::instructions::id().log();
|
2021-08-01 17:31:11 -07:00
|
|
|
let index = instructions::load_current_index(&accounts[4].try_borrow_data()?);
|
2021-04-06 00:08:03 -07:00
|
|
|
assert_eq!(0, index);
|
|
|
|
|
2021-04-07 01:47:15 -07:00
|
|
|
// Recent Blockhashes
|
2021-07-27 16:34:21 -07:00
|
|
|
#[allow(deprecated)]
|
2021-04-07 01:47:15 -07:00
|
|
|
{
|
|
|
|
msg!("RecentBlockhashes identifier:");
|
|
|
|
sysvar::recent_blockhashes::id().log();
|
2021-08-01 17:31:11 -07:00
|
|
|
let recent_blockhashes = RecentBlockhashes::from_account_info(&accounts[5]).unwrap();
|
2021-04-07 01:47:15 -07:00
|
|
|
assert_ne!(recent_blockhashes, RecentBlockhashes::default());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rent
|
|
|
|
{
|
|
|
|
msg!("Rent identifier:");
|
|
|
|
sysvar::rent::id().log();
|
2021-08-01 17:31:11 -07:00
|
|
|
let rent = Rent::from_account_info(&accounts[6]).unwrap();
|
2021-04-07 01:47:15 -07:00
|
|
|
assert_eq!(rent, Rent::default());
|
2021-04-12 16:04:57 -07:00
|
|
|
let got_rent = Rent::get()?;
|
|
|
|
assert_eq!(rent, got_rent);
|
2021-04-07 01:47:15 -07:00
|
|
|
}
|
2021-04-06 00:08:03 -07:00
|
|
|
|
2019-09-10 18:53:02 -07:00
|
|
|
// Slot Hashes
|
2020-11-30 13:28:58 -08:00
|
|
|
msg!("SlotHashes identifier:");
|
2019-12-03 20:55:18 -05:00
|
|
|
sysvar::slot_hashes::id().log();
|
2021-04-06 00:08:03 -07:00
|
|
|
assert_eq!(
|
|
|
|
Err(ProgramError::UnsupportedSysvar),
|
2021-08-01 17:31:11 -07:00
|
|
|
SlotHashes::from_account_info(&accounts[7])
|
2021-04-06 00:08:03 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
// Slot History
|
|
|
|
msg!("SlotHistory identifier:");
|
|
|
|
sysvar::slot_history::id().log();
|
|
|
|
assert_eq!(
|
|
|
|
Err(ProgramError::UnsupportedSysvar),
|
2021-08-01 17:31:11 -07:00
|
|
|
SlotHistory::from_account_info(&accounts[8])
|
2021-04-06 00:08:03 -07:00
|
|
|
);
|
2019-09-10 18:53:02 -07:00
|
|
|
|
|
|
|
// Stake History
|
2020-11-30 13:28:58 -08:00
|
|
|
msg!("StakeHistory identifier:");
|
2019-12-03 20:55:18 -05:00
|
|
|
sysvar::stake_history::id().log();
|
2021-08-01 17:31:11 -07:00
|
|
|
let _ = StakeHistory::from_account_info(&accounts[9]).unwrap();
|
2019-09-10 18:53:02 -07:00
|
|
|
|
2020-01-30 09:47:22 -08:00
|
|
|
Ok(())
|
2019-09-10 18:53:02 -07:00
|
|
|
}
|