Add support for SDK sysvar types (#5876)

This commit is contained in:
Jack May
2019-09-10 18:53:02 -07:00
committed by GitHub
parent 772ee4b29d
commit 1853771930
19 changed files with 124 additions and 75 deletions

View File

@@ -1,28 +0,0 @@
//! @brief Example Rust-based BPF program that prints out the parameters passed to it
extern crate solana_sdk;
use solana_sdk::{
account_info::AccountInfo, entrypoint, entrypoint::SUCCESS, info, pubkey::Pubkey,
sysvar::clock::Clock,
};
entrypoint!(process_instruction);
fn process_instruction(_program_id: &Pubkey, accounts: &mut [AccountInfo], _data: &[u8]) -> u32 {
match Clock::from_account_info(&accounts[2]) {
Some(clock) => {
info!("slot, segment, epoch, stakers_epoch");
info!(
clock.slot,
clock.segment, clock.epoch, clock.stakers_epoch, 0
);
assert_eq!(clock.slot, 42);
}
None => {
info!("Failed to get clock from account 2");
panic!();
}
}
info!("Success");
SUCCESS
}

View File

@@ -2,9 +2,9 @@
# Note: This crate must be built using build.sh
[package]
name = "solana-bpf-rust-clock"
name = "solana-bpf-rust-sysval"
version = "0.19.0-pre0"
description = "Solana BPF clock sysvar test"
description = "Solana BPF sysvar test"
authors = ["Solana Maintainers <maintainers@solana.com>"]
repository = "https://github.com/solana-labs/solana"
license = "Apache-2.0"
@@ -23,4 +23,4 @@ members = []
[lib]
crate-type = ["cdylib"]
name = "solana_bpf_rust_clock"
name = "solana_bpf_rust_sysval"

View File

@@ -0,0 +1,43 @@
//! @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,
pubkey::Pubkey,
sysvar::{
clock::Clock, fees::Fees, rewards::Rewards, slot_hashes::SlotHashes,
stake_history::StakeHistory,
},
};
entrypoint!(process_instruction);
fn process_instruction(_program_id: &Pubkey, accounts: &mut [AccountInfo], _data: &[u8]) -> u32 {
// Clock
let clock = Clock::from_account_info(&accounts[2]).unwrap();
assert_eq!(clock.slot, DEFAULT_SLOTS_PER_EPOCH + 1);
assert_eq!(
clock.segment,
get_segment_from_slot(clock.slot, DEFAULT_SLOTS_PER_SEGMENT)
);
// Fees
let fees = Fees::from_account_info(&accounts[3]).unwrap();
let burn = fees.fee_calculator.burn(42);
assert_eq!(burn, (21, 21));
// Rewards
let _ = Rewards::from_account_info(&accounts[4]).unwrap();
// Slot Hashes
let slot_hashes = SlotHashes::from_account_info(&accounts[5]).unwrap();
assert_eq!(slot_hashes.len(), 1);
// Stake History
let stake_history = StakeHistory::from_account_info(&accounts[6]).unwrap();
assert_eq!(stake_history.len(), 1);
SUCCESS
}