From e8245d24107be03b8049bc18fea67aedf9e9efba Mon Sep 17 00:00:00 2001 From: HaoranYi Date: Fri, 11 Aug 2023 03:40:41 +0000 Subject: [PATCH 1/2] fix sbf sysvar test --- program-test/src/lib.rs | 7 ++++++- programs/sbf/rust/sysvar/src/lib.rs | 10 +++------- programs/sbf/rust/sysvar/tests/lib.rs | 10 +++++++++- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/program-test/src/lib.rs b/program-test/src/lib.rs index e6e731db9c9dc0..5030e3746a1d4d 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}, @@ -587,6 +587,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..254176e096e5e1 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( From e920a84ccde464041ebcea4021ef7a30f69419cf Mon Sep 17 00:00:00 2001 From: HaoranYi Date: Fri, 11 Aug 2023 03:53:20 +0000 Subject: [PATCH 2/2] typo --- programs/sbf/rust/sysvar/tests/lib.rs | 8 ++++---- runtime/src/bank.rs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/programs/sbf/rust/sysvar/tests/lib.rs b/programs/sbf/rust/sysvar/tests/lib.rs index 254176e096e5e1..b99b89e75c9648 100644 --- a/programs/sbf/rust/sysvar/tests/lib.rs +++ b/programs/sbf/rust/sysvar/tests/lib.rs @@ -27,10 +27,10 @@ async fn test_sysvars() { ); let epoch_rewards = epoch_rewards::EpochRewards { - total_rewards: 100, - distributed_rewards: 50, - distribution_complete_block_height: 42, - }; + 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; diff --git a/runtime/src/bank.rs b/runtime/src/bank.rs index c298d104a22ab4..ea277805067aa0 100644 --- a/runtime/src/bank.rs +++ b/runtime/src/bank.rs @@ -3538,7 +3538,7 @@ impl Bank { } } - /// Create EpochRewards syavar with calculated rewards + /// Create EpochRewards sysvar with calculated rewards fn create_epoch_rewards_sysvar( &self, total_rewards: u64,