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;
|
|
|
|
use solana_program::{
|
2019-09-10 18:53:02 -07:00
|
|
|
account_info::AccountInfo,
|
2020-05-14 18:22:47 -07:00
|
|
|
clock::DEFAULT_SLOTS_PER_EPOCH,
|
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,
|
2019-10-30 16:25:12 -07:00
|
|
|
rent,
|
2019-09-10 18:53:02 -07:00
|
|
|
sysvar::{
|
2021-04-06 00:08:03 -07:00
|
|
|
self, clock::Clock, fees::Fees, instructions, rent::Rent, 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)]
|
2020-01-24 13:41:14 -08:00
|
|
|
fn process_instruction(
|
|
|
|
_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
|
2020-11-30 13:28:58 -08:00
|
|
|
msg!("Clock identifier:");
|
2019-12-03 20:55:18 -05:00
|
|
|
sysvar::clock::id().log();
|
2021-04-06 00:08:03 -07:00
|
|
|
let clock = Clock::from_account_info(&accounts[2]).unwrap();
|
2019-09-10 18:53:02 -07:00
|
|
|
assert_eq!(clock.slot, DEFAULT_SLOTS_PER_EPOCH + 1);
|
|
|
|
|
|
|
|
// Fees
|
2020-11-30 13:28:58 -08:00
|
|
|
msg!("Fees identifier:");
|
2019-12-03 20:55:18 -05:00
|
|
|
sysvar::fees::id().log();
|
2021-04-06 00:08:03 -07:00
|
|
|
let fees = Fees::from_account_info(&accounts[3]).unwrap();
|
2020-02-28 13:27:01 -07:00
|
|
|
let fee_calculator = fees.fee_calculator;
|
|
|
|
assert_eq!(fee_calculator.lamports_per_signature, 0);
|
2019-09-10 18:53:02 -07:00
|
|
|
|
2021-04-06 00:08:03 -07:00
|
|
|
// Instructions
|
|
|
|
msg!("Instructions identifier:");
|
|
|
|
sysvar::instructions::id().log();
|
|
|
|
let index = instructions::load_current_index(&accounts[4].try_borrow_data()?);
|
|
|
|
assert_eq!(0, index);
|
|
|
|
msg!(
|
|
|
|
"instruction {:?}",
|
|
|
|
instructions::load_instruction_at(index as usize, &accounts[4].try_borrow_data()?)
|
|
|
|
);
|
|
|
|
|
|
|
|
let due = Rent::from_account_info(&accounts[5]).unwrap().due(
|
|
|
|
rent::DEFAULT_LAMPORTS_PER_BYTE_YEAR * rent::DEFAULT_EXEMPTION_THRESHOLD as u64,
|
|
|
|
1,
|
|
|
|
1.0,
|
|
|
|
);
|
|
|
|
assert_eq!(due, (0, true));
|
|
|
|
|
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),
|
|
|
|
SlotHashes::from_account_info(&accounts[6])
|
|
|
|
);
|
|
|
|
|
|
|
|
// Slot History
|
|
|
|
msg!("SlotHistory identifier:");
|
|
|
|
sysvar::slot_history::id().log();
|
|
|
|
assert_eq!(
|
|
|
|
Err(ProgramError::UnsupportedSysvar),
|
|
|
|
SlotHistory::from_account_info(&accounts[7])
|
|
|
|
);
|
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-04-06 00:08:03 -07:00
|
|
|
let stake_history = StakeHistory::from_account_info(&accounts[8]).unwrap();
|
2019-10-08 22:34:26 -07:00
|
|
|
assert!(stake_history.len() >= 1);
|
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
|
|
|
}
|