Test epoch rewards sysvar in program test (#32293)

* Add epoch rewards sysvar test to program test

* Add a test to check epoch rewards sysvar inside/outisde reward interval

---------

Co-authored-by: HaoranYi <haoran.yi@solana.com>
This commit is contained in:
HaoranYi 2023-06-28 10:50:11 -05:00 committed by GitHub
parent e60b58258d
commit 906121645c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 86 additions and 5 deletions

View File

@ -362,6 +362,13 @@ impl solana_sdk::program_stubs::SyscallStubs for SyscallStubs {
)
}
fn sol_get_epoch_rewards_sysvar(&self, var_addr: *mut u8) -> u64 {
get_sysvar(
get_invoke_context().get_sysvar_cache().get_epoch_rewards(),
var_addr,
)
}
#[allow(deprecated)]
fn sol_get_fees_sysvar(&self, var_addr: *mut u8) -> u64 {
get_sysvar(get_invoke_context().get_sysvar_cache().get_fees(), var_addr)

View File

@ -2,8 +2,8 @@ use {
solana_program_test::{processor, ProgramTest},
solana_sdk::{
account_info::AccountInfo, clock::Clock, entrypoint::ProgramResult,
epoch_schedule::EpochSchedule, instruction::Instruction, msg, pubkey::Pubkey, rent::Rent,
signature::Signer, sysvar::Sysvar, transaction::Transaction,
epoch_rewards::EpochRewards, epoch_schedule::EpochSchedule, instruction::Instruction, msg,
pubkey::Pubkey, rent::Rent, signature::Signer, sysvar::Sysvar, transaction::Transaction,
},
};
@ -53,3 +53,77 @@ async fn get_sysvar() {
.await
.unwrap();
}
fn epoch_reward_sysvar_getter_process_instruction(
_program_id: &Pubkey,
_accounts: &[AccountInfo],
input: &[u8],
) -> ProgramResult {
msg!("epoch_reward_sysvar_getter");
// input[0] == 0 indicates the bank is not in reward period.
// input[0] == 1 indicates the bank is in reward period.
if input[0] == 0 {
// epoch rewards sysvar should not exist for banks that are not in reward period
let epoch_rewards = EpochRewards::get();
assert!(epoch_rewards.is_err());
} else {
let _epoch_rewards = EpochRewards::get()?;
}
Ok(())
}
#[tokio::test]
async fn get_epoch_rewards_sysvar() {
let program_id = Pubkey::new_unique();
let program_test = ProgramTest::new(
"epoch_reward_sysvar_getter",
program_id,
processor!(epoch_reward_sysvar_getter_process_instruction),
);
let mut context = program_test.start_with_context().await;
// wrap to 1st slot before next epoch (outside reward interval)
let first_normal_slot = context.genesis_config().epoch_schedule.first_normal_slot;
let slots_per_epoch = context.genesis_config().epoch_schedule.slots_per_epoch;
let last_slot_before_new_epoch = first_normal_slot
.saturating_add(slots_per_epoch)
.saturating_sub(1);
context.warp_to_slot(last_slot_before_new_epoch).unwrap();
// outside of reward interval, set input[0] == 0, so that the program assert that epoch_rewards sysvar doesn't exist.
let instructions = vec![Instruction::new_with_bincode(program_id, &[0u8], vec![])];
let transaction = Transaction::new_signed_with_payer(
&instructions,
Some(&context.payer.pubkey()),
&[&context.payer],
context.last_blockhash,
);
context
.banks_client
.process_transaction(transaction)
.await
.unwrap();
// wrap to 1st slot of next epoch (inside reward interval)
let first_slot_in_new_epoch = first_normal_slot.saturating_add(slots_per_epoch);
context.warp_to_slot(first_slot_in_new_epoch).unwrap();
// inside of reward interval, set input[0] == 1, so that the program assert that epoch_rewards sysvar exist.
let instructions = vec![Instruction::new_with_bincode(program_id, &[1u8], vec![])];
let transaction = Transaction::new_signed_with_payer(
&instructions,
Some(&context.payer.pubkey()),
&[&context.payer],
context.last_blockhash,
);
context
.banks_client
.process_transaction(transaction)
.await
.unwrap();
}

View File

@ -46,9 +46,9 @@ pub use solana_program::{
account_info, address_lookup_table_account, alt_bn128, big_mod_exp, blake3, borsh, bpf_loader,
bpf_loader_deprecated, bpf_loader_upgradeable, clock, config, custom_heap_default,
custom_panic_default, debug_account_data, declare_deprecated_sysvar_id, declare_sysvar_id,
decode_error, ed25519_program, epoch_schedule, fee_calculator, impl_sysvar_get, incinerator,
instruction, keccak, lamports, loader_instruction, loader_upgradeable_instruction, loader_v4,
loader_v4_instruction, message, msg, native_token, nonce, program, program_error,
decode_error, ed25519_program, epoch_rewards, epoch_schedule, fee_calculator, impl_sysvar_get,
incinerator, instruction, keccak, lamports, loader_instruction, loader_upgradeable_instruction,
loader_v4, loader_v4_instruction, message, msg, native_token, nonce, program, program_error,
program_memory, program_option, program_pack, rent, sanitize, sdk_ids, secp256k1_program,
secp256k1_recover, serde_varint, serialize_utils, short_vec, slot_hashes, slot_history,
stable_layout, stake, stake_history, syscalls, system_instruction, system_program, sysvar,