2019-09-10 18:53:02 -07:00
|
|
|
//! @brief Example Rust-based BPF program that tests sysval use
|
|
|
|
|
|
|
|
extern crate solana_sdk;
|
|
|
|
use solana_sdk::{
|
|
|
|
account_info::AccountInfo,
|
|
|
|
clock::{get_segment_from_slot, DEFAULT_SLOTS_PER_EPOCH, DEFAULT_SLOTS_PER_SEGMENT},
|
|
|
|
entrypoint,
|
|
|
|
entrypoint::SUCCESS,
|
2019-12-03 22:07:19 -05:00
|
|
|
info,
|
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::{
|
2019-12-03 22:07:19 -05:00
|
|
|
self, clock::Clock, fees::Fees, rent::Rent, rewards::Rewards, slot_hashes::SlotHashes,
|
2019-11-04 12:31:24 -08:00
|
|
|
stake_history::StakeHistory, Sysvar,
|
2019-09-10 18:53:02 -07:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
entrypoint!(process_instruction);
|
|
|
|
fn process_instruction(_program_id: &Pubkey, accounts: &mut [AccountInfo], _data: &[u8]) -> u32 {
|
|
|
|
// Clock
|
2019-12-03 22:07:19 -05:00
|
|
|
info!("Clock identifier:");
|
|
|
|
sysvar::clock::id().log();
|
2019-10-08 22:34:26 -07:00
|
|
|
let clock = Clock::from_account_info(&accounts[2]).expect("clock");
|
2019-09-10 18:53:02 -07:00
|
|
|
assert_eq!(clock.slot, DEFAULT_SLOTS_PER_EPOCH + 1);
|
|
|
|
assert_eq!(
|
|
|
|
clock.segment,
|
|
|
|
get_segment_from_slot(clock.slot, DEFAULT_SLOTS_PER_SEGMENT)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Fees
|
2019-12-03 22:07:19 -05:00
|
|
|
info!("Fees identifier:");
|
|
|
|
sysvar::fees::id().log();
|
2019-10-08 22:34:26 -07:00
|
|
|
let fees = Fees::from_account_info(&accounts[3]).expect("fees");
|
2019-09-10 18:53:02 -07:00
|
|
|
let burn = fees.fee_calculator.burn(42);
|
|
|
|
assert_eq!(burn, (21, 21));
|
|
|
|
|
|
|
|
// Rewards
|
2019-12-03 22:07:19 -05:00
|
|
|
info!("Rewards identifier:");
|
|
|
|
sysvar::rewards::id().log();
|
2019-10-08 22:34:26 -07:00
|
|
|
let _rewards = Rewards::from_account_info(&accounts[4]).expect("rewards");
|
2019-09-10 18:53:02 -07:00
|
|
|
|
|
|
|
// Slot Hashes
|
2019-12-03 22:07:19 -05:00
|
|
|
info!("SlotHashes identifier:");
|
|
|
|
sysvar::slot_hashes::id().log();
|
2019-10-08 22:34:26 -07:00
|
|
|
let slot_hashes = SlotHashes::from_account_info(&accounts[5]).expect("slot_hashes");
|
|
|
|
assert!(slot_hashes.len() >= 1);
|
2019-09-10 18:53:02 -07:00
|
|
|
|
|
|
|
// Stake History
|
2019-12-03 22:07:19 -05:00
|
|
|
info!("StakeHistory identifier:");
|
|
|
|
sysvar::stake_history::id().log();
|
2019-10-08 22:34:26 -07:00
|
|
|
let stake_history = StakeHistory::from_account_info(&accounts[6]).expect("stake_history");
|
|
|
|
assert!(stake_history.len() >= 1);
|
2019-09-10 18:53:02 -07:00
|
|
|
|
2019-09-17 17:12:55 +05:30
|
|
|
let rent = Rent::from_account_info(&accounts[7]).unwrap();
|
|
|
|
assert_eq!(
|
2019-10-30 16:25:12 -07:00
|
|
|
rent.due(
|
|
|
|
rent::DEFAULT_LAMPORTS_PER_BYTE_YEAR * rent::DEFAULT_EXEMPTION_THRESHOLD as u64,
|
2019-09-17 17:12:55 +05:30
|
|
|
1,
|
|
|
|
1.0
|
|
|
|
),
|
|
|
|
(0, true)
|
|
|
|
);
|
|
|
|
|
2019-09-10 18:53:02 -07:00
|
|
|
SUCCESS
|
|
|
|
}
|