Skip to content

Commit

Permalink
fix sbf sysvar test (solana-labs#32803)
Browse files Browse the repository at this point in the history
* fix sbf sysvar test

* typo

---------

Co-authored-by: HaoranYi <[email protected]>
  • Loading branch information
HaoranYi and HaoranYi authored Aug 21, 2023
1 parent c698918 commit 910b0f5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
7 changes: 6 additions & 1 deletion program-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use {
runtime_config::RuntimeConfig,
},
solana_sdk::{
account::{Account, AccountSharedData},
account::{create_account_shared_data_for_test, Account, AccountSharedData},
account_info::AccountInfo,
clock::Slot,
entrypoint::{deserialize, ProgramResult, SUCCESS},
Expand Down Expand Up @@ -591,6 +591,11 @@ impl ProgramTest {
);
}

pub fn add_sysvar_account<S: Sysvar>(&mut self, address: Pubkey, sysvar: &S) {
let account = create_account_shared_data_for_test(sysvar);
self.add_account(address, account.into());
}

/// Add a SBF program to the test environment.
///
/// `program_name` will also be used to locate the SBF shared object in the current or fixtures
Expand Down
10 changes: 3 additions & 7 deletions programs/sbf/rust/sysvar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,9 @@ pub fn process_instruction(
{
msg!("EpochRewards identifier:");
sysvar::epoch_rewards::id().log();
let epoch_rewards = EpochRewards::from_account_info(&accounts[11]);
// epoch_rewards sysvar should only be valid during epoch reward period. In this test case,
// the test bank is outside reward period. Therefore, we expect that the epoch_rewards
// sysvar doesn't exist.
assert!(epoch_rewards.is_err());
let got_epoch_rewards = EpochRewards::get();
assert!(got_epoch_rewards.is_err());
let epoch_rewards = EpochRewards::from_account_info(&accounts[11]).unwrap();
let got_epoch_rewards = EpochRewards::get()?;
assert_eq!(epoch_rewards, got_epoch_rewards);
}

Ok(())
Expand Down
10 changes: 9 additions & 1 deletion programs/sbf/rust/sysvar/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,18 @@ use {
async fn test_sysvars() {
let program_id = Pubkey::new_unique();

let program_test = ProgramTest::new(
let mut program_test = ProgramTest::new(
"solana_sbf_rust_sysvar",
program_id,
processor!(process_instruction),
);

let epoch_rewards = epoch_rewards::EpochRewards {
total_rewards: 100,
distributed_rewards: 50,
distribution_complete_block_height: 42,
};
program_test.add_sysvar_account(epoch_rewards::id(), &epoch_rewards);
let (mut banks_client, payer, recent_blockhash) = program_test.start().await;

let mut transaction = Transaction::new_with_payer(
Expand Down Expand Up @@ -59,6 +66,7 @@ async fn test_sysvars() {
processor!(process_instruction),
);
program_test.deactivate_feature(disable_fees_sysvar::id());
program_test.add_sysvar_account(epoch_rewards::id(), &epoch_rewards);
let (mut banks_client, payer, recent_blockhash) = program_test.start().await;

let mut transaction = Transaction::new_with_payer(
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3541,7 +3541,7 @@ impl Bank {
}
}

/// Create EpochRewards syavar with calculated rewards
/// Create EpochRewards sysvar with calculated rewards
fn create_epoch_rewards_sysvar(
&self,
total_rewards: u64,
Expand Down

0 comments on commit 910b0f5

Please sign in to comment.