Add support for SDK sysvar types (#5876)
This commit is contained in:
4
programs/bpf/rust/sysval/.gitignore
vendored
Normal file
4
programs/bpf/rust/sysval/.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
/target/
|
||||
|
||||
Cargo.lock
|
||||
/farf/
|
26
programs/bpf/rust/sysval/Cargo.toml
Normal file
26
programs/bpf/rust/sysval/Cargo.toml
Normal file
@ -0,0 +1,26 @@
|
||||
|
||||
# Note: This crate must be built using build.sh
|
||||
|
||||
[package]
|
||||
name = "solana-bpf-rust-sysval"
|
||||
version = "0.19.0-pre0"
|
||||
description = "Solana BPF sysvar test"
|
||||
authors = ["Solana Maintainers <maintainers@solana.com>"]
|
||||
repository = "https://github.com/solana-labs/solana"
|
||||
license = "Apache-2.0"
|
||||
homepage = "https://solana.com/"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
solana-sdk = { path = "../../../../sdk/", version = "0.19.0-pre0", default-features = false }
|
||||
|
||||
[features]
|
||||
program = ["solana-sdk/program"]
|
||||
default = ["program"]
|
||||
|
||||
[workspace]
|
||||
members = []
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
name = "solana_bpf_rust_sysval"
|
2
programs/bpf/rust/sysval/Xargo.toml
Normal file
2
programs/bpf/rust/sysval/Xargo.toml
Normal file
@ -0,0 +1,2 @@
|
||||
[target.bpfel-unknown-unknown.dependencies.std]
|
||||
features = []
|
43
programs/bpf/rust/sysval/src/lib.rs
Normal file
43
programs/bpf/rust/sysval/src/lib.rs
Normal 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
|
||||
}
|
Reference in New Issue
Block a user