Add program-test tests to bpf programs (#16407)

This commit is contained in:
Jack May
2021-04-07 01:47:15 -07:00
committed by GitHub
parent 6cd4bc5e60
commit 476fd40948
9 changed files with 337 additions and 84 deletions

View File

@ -12,8 +12,12 @@ edition = "2018"
[dependencies]
solana-program = { path = "../../../../sdk/program", version = "=1.7.0" }
[dev-dependencies]
solana-program-test = { path = "../../../../program-test", version = "=1.7.0" }
solana-sdk = { path = "../../../../sdk", version = "=1.7.0" }
[lib]
crate-type = ["cdylib"]
crate-type = ["cdylib", "lib"]
[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

View File

@ -3,62 +3,79 @@
extern crate solana_program;
use solana_program::{
account_info::AccountInfo,
clock::DEFAULT_SLOTS_PER_EPOCH,
entrypoint,
entrypoint::ProgramResult,
fee_calculator::FeeCalculator,
msg,
program_error::ProgramError,
pubkey::Pubkey,
rent,
sysvar::{
self, clock::Clock, fees::Fees, instructions, rent::Rent, slot_hashes::SlotHashes,
self, clock::Clock, epoch_schedule::EpochSchedule, fees::Fees, instructions,
recent_blockhashes::RecentBlockhashes, rent::Rent, slot_hashes::SlotHashes,
slot_history::SlotHistory, stake_history::StakeHistory, Sysvar,
},
};
entrypoint!(process_instruction);
#[allow(clippy::unnecessary_wraps)]
fn process_instruction(
pub fn process_instruction(
_program_id: &Pubkey,
accounts: &[AccountInfo],
_instruction_data: &[u8],
) -> ProgramResult {
// Clock
msg!("Clock identifier:");
sysvar::clock::id().log();
let clock = Clock::from_account_info(&accounts[2]).unwrap();
assert_eq!(clock.slot, DEFAULT_SLOTS_PER_EPOCH + 1);
{
msg!("Clock identifier:");
sysvar::clock::id().log();
let clock = Clock::from_account_info(&accounts[2]).unwrap();
assert_ne!(clock, Clock::default());
}
// 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());
}
// Fees
msg!("Fees identifier:");
sysvar::fees::id().log();
let fees = Fees::from_account_info(&accounts[3]).unwrap();
let fee_calculator = fees.fee_calculator;
assert_eq!(fee_calculator.lamports_per_signature, 0);
{
msg!("Fees identifier:");
sysvar::fees::id().log();
let fees = Fees::from_account_info(&accounts[4]).unwrap();
let fee_calculator = fees.fee_calculator;
assert_ne!(fee_calculator, FeeCalculator::default());
}
// Instructions
msg!("Instructions identifier:");
sysvar::instructions::id().log();
let index = instructions::load_current_index(&accounts[4].try_borrow_data()?);
let index = instructions::load_current_index(&accounts[5].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));
// Recent Blockhashes
{
msg!("RecentBlockhashes identifier:");
sysvar::recent_blockhashes::id().log();
let recent_blockhashes = RecentBlockhashes::from_account_info(&accounts[6]).unwrap();
assert_ne!(recent_blockhashes, RecentBlockhashes::default());
}
// Rent
{
msg!("Rent identifier:");
sysvar::rent::id().log();
let rent = Rent::from_account_info(&accounts[7]).unwrap();
assert_eq!(rent, Rent::default());
}
// Slot Hashes
msg!("SlotHashes identifier:");
sysvar::slot_hashes::id().log();
assert_eq!(
Err(ProgramError::UnsupportedSysvar),
SlotHashes::from_account_info(&accounts[6])
SlotHashes::from_account_info(&accounts[8])
);
// Slot History
@ -66,14 +83,13 @@ fn process_instruction(
sysvar::slot_history::id().log();
assert_eq!(
Err(ProgramError::UnsupportedSysvar),
SlotHistory::from_account_info(&accounts[7])
SlotHistory::from_account_info(&accounts[9])
);
// Stake History
msg!("StakeHistory identifier:");
sysvar::stake_history::id().log();
let stake_history = StakeHistory::from_account_info(&accounts[8]).unwrap();
assert!(stake_history.len() >= 1);
let _ = StakeHistory::from_account_info(&accounts[10]).unwrap();
Ok(())
}

View File

@ -0,0 +1,46 @@
use solana_bpf_rust_sysvar::process_instruction;
use solana_program_test::*;
use solana_sdk::{
instruction::{AccountMeta, Instruction},
pubkey::Pubkey,
signature::Signer,
sysvar::{
clock, epoch_schedule, fees, instructions, recent_blockhashes, rent, slot_hashes,
slot_history, stake_history,
},
transaction::Transaction,
};
#[tokio::test]
async fn test_noop() {
let program_id = Pubkey::new_unique();
let program_test = ProgramTest::new(
"solana_bpf_rust_sysvar",
program_id,
processor!(process_instruction),
);
let (mut banks_client, payer, recent_blockhash) = program_test.start().await;
let mut transaction = Transaction::new_with_payer(
&[Instruction::new_with_bincode(
program_id,
&(),
vec![
AccountMeta::new(payer.pubkey(), true),
AccountMeta::new(Pubkey::new_unique(), false),
AccountMeta::new_readonly(clock::id(), false),
AccountMeta::new_readonly(epoch_schedule::id(), false),
AccountMeta::new_readonly(fees::id(), false),
AccountMeta::new_readonly(instructions::id(), false),
AccountMeta::new_readonly(recent_blockhashes::id(), false),
AccountMeta::new_readonly(rent::id(), false),
AccountMeta::new_readonly(slot_hashes::id(), false),
AccountMeta::new_readonly(slot_history::id(), false),
AccountMeta::new_readonly(stake_history::id(), false),
],
)],
Some(&payer.pubkey()),
);
transaction.sign(&[&payer], recent_blockhash);
banks_client.process_transaction(transaction).await.unwrap();
}