From 910b0f5d1232dfeacce9b27b7867c254d640b79c Mon Sep 17 00:00:00 2001 From: HaoranYi Date: Mon, 21 Aug 2023 10:49:48 -0500 Subject: [PATCH] fix sbf sysvar test (#32803) * fix sbf sysvar test * typo --------- Co-authored-by: HaoranYi --- program-test/src/lib.rs | 7 ++++++- programs/sbf/rust/sysvar/src/lib.rs | 10 +++------- programs/sbf/rust/sysvar/tests/lib.rs | 10 +++++++++- runtime/src/bank.rs | 2 +- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/program-test/src/lib.rs b/program-test/src/lib.rs index 457020c850d466..a4a5207a9d66a2 100644 --- a/program-test/src/lib.rs +++ b/program-test/src/lib.rs @@ -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}, @@ -591,6 +591,11 @@ impl ProgramTest { ); } + pub fn add_sysvar_account(&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 diff --git a/programs/sbf/rust/sysvar/src/lib.rs b/programs/sbf/rust/sysvar/src/lib.rs index c4b51360de68fe..1d0a6c6ac74a53 100644 --- a/programs/sbf/rust/sysvar/src/lib.rs +++ b/programs/sbf/rust/sysvar/src/lib.rs @@ -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(()) diff --git a/programs/sbf/rust/sysvar/tests/lib.rs b/programs/sbf/rust/sysvar/tests/lib.rs index 980171e39c37b4..b99b89e75c9648 100644 --- a/programs/sbf/rust/sysvar/tests/lib.rs +++ b/programs/sbf/rust/sysvar/tests/lib.rs @@ -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( @@ -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( diff --git a/runtime/src/bank.rs b/runtime/src/bank.rs index ca5ba3f5c6532a..cf025b47739a1c 100644 --- a/runtime/src/bank.rs +++ b/runtime/src/bank.rs @@ -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,