diff --git a/program-runtime/src/sysvar_cache.rs b/program-runtime/src/sysvar_cache.rs index 143ae5e111..9469b94ab2 100644 --- a/program-runtime/src/sysvar_cache.rs +++ b/program-runtime/src/sysvar_cache.rs @@ -5,7 +5,6 @@ use { solana_sdk::{ account::{AccountSharedData, ReadableAccount}, instruction::InstructionError, - keyed_account::KeyedAccount, pubkey::Pubkey, sysvar::{ clock::Clock, epoch_schedule::EpochSchedule, rent::Rent, slot_hashes::SlotHashes, @@ -182,60 +181,6 @@ impl SysvarCache { pub mod get_sysvar_with_account_check { use super::*; - fn check_sysvar_keyed_account( - keyed_account: &KeyedAccount, - ) -> Result<(), InstructionError> { - if !S::check_id(keyed_account.unsigned_key()) { - return Err(InstructionError::InvalidArgument); - } - Ok(()) - } - - pub fn clock( - keyed_account: &KeyedAccount, - invoke_context: &InvokeContext, - ) -> Result, InstructionError> { - check_sysvar_keyed_account::(keyed_account)?; - invoke_context.get_sysvar_cache().get_clock() - } - - pub fn rent( - keyed_account: &KeyedAccount, - invoke_context: &InvokeContext, - ) -> Result, InstructionError> { - check_sysvar_keyed_account::(keyed_account)?; - invoke_context.get_sysvar_cache().get_rent() - } - - pub fn slot_hashes( - keyed_account: &KeyedAccount, - invoke_context: &InvokeContext, - ) -> Result, InstructionError> { - check_sysvar_keyed_account::(keyed_account)?; - invoke_context.get_sysvar_cache().get_slot_hashes() - } - - #[allow(deprecated)] - pub fn recent_blockhashes( - keyed_account: &KeyedAccount, - invoke_context: &InvokeContext, - ) -> Result, InstructionError> { - check_sysvar_keyed_account::(keyed_account)?; - invoke_context.get_sysvar_cache().get_recent_blockhashes() - } - - pub fn stake_history( - keyed_account: &KeyedAccount, - invoke_context: &InvokeContext, - ) -> Result, InstructionError> { - check_sysvar_keyed_account::(keyed_account)?; - invoke_context.get_sysvar_cache().get_stake_history() - } -} - -pub mod get_sysvar_with_account_check2 { - use super::*; - fn check_sysvar_account( transaction_context: &TransactionContext, instruction_context: &InstructionContext, diff --git a/programs/bpf_loader/src/lib.rs b/programs/bpf_loader/src/lib.rs index f77a524f7c..bf1b5f5390 100644 --- a/programs/bpf_loader/src/lib.rs +++ b/programs/bpf_loader/src/lib.rs @@ -492,20 +492,9 @@ fn process_loader_upgradeable_instruction( keyed_accounts, first_instruction_account.saturating_add(3), )?; - let rent = get_sysvar_with_account_check::rent( - keyed_account_at_index( - keyed_accounts, - first_instruction_account.saturating_add(4), - )?, - invoke_context, - )?; - let clock = get_sysvar_with_account_check::clock( - keyed_account_at_index( - keyed_accounts, - first_instruction_account.saturating_add(5), - )?, - invoke_context, - )?; + let rent = get_sysvar_with_account_check::rent(invoke_context, instruction_context, 4)?; + let clock = + get_sysvar_with_account_check::clock(invoke_context, instruction_context, 5)?; let authority = keyed_account_at_index( keyed_accounts, first_instruction_account.saturating_add(7), @@ -683,20 +672,9 @@ fn process_loader_upgradeable_instruction( keyed_accounts, first_instruction_account.saturating_add(2), )?; - let rent = get_sysvar_with_account_check::rent( - keyed_account_at_index( - keyed_accounts, - first_instruction_account.saturating_add(4), - )?, - invoke_context, - )?; - let clock = get_sysvar_with_account_check::clock( - keyed_account_at_index( - keyed_accounts, - first_instruction_account.saturating_add(5), - )?, - invoke_context, - )?; + let rent = get_sysvar_with_account_check::rent(invoke_context, instruction_context, 4)?; + let clock = + get_sysvar_with_account_check::clock(invoke_context, instruction_context, 5)?; let authority = keyed_account_at_index( keyed_accounts, first_instruction_account.saturating_add(6), diff --git a/programs/stake/src/stake_instruction.rs b/programs/stake/src/stake_instruction.rs index e577bdbab7..29451782f0 100644 --- a/programs/stake/src/stake_instruction.rs +++ b/programs/stake/src/stake_instruction.rs @@ -43,10 +43,7 @@ pub fn process_instruction( let signers = instruction_context.get_signers(transaction_context); match limited_deserialize(data)? { StakeInstruction::Initialize(authorized, lockup) => { - let rent = get_sysvar_with_account_check::rent( - keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?, - invoke_context, - )?; + let rent = get_sysvar_with_account_check::rent(invoke_context, instruction_context, 1)?; me.initialize(&authorized, &lockup, &rent) } StakeInstruction::Authorize(authorized_pubkey, stake_authorize) => { @@ -55,10 +52,8 @@ pub fn process_instruction( .is_active(&feature_set::require_custodian_for_locked_stake_authorize::id()); if require_custodian_for_locked_stake_authorize { - let clock = get_sysvar_with_account_check::clock( - keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?, - invoke_context, - )?; + let clock = + get_sysvar_with_account_check::clock(invoke_context, instruction_context, 1)?; let _current_authority = keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?; let custodian = @@ -93,10 +88,8 @@ pub fn process_instruction( .is_active(&feature_set::require_custodian_for_locked_stake_authorize::id()); if require_custodian_for_locked_stake_authorize { - let clock = get_sysvar_with_account_check::clock( - keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?, - invoke_context, - )?; + let clock = + get_sysvar_with_account_check::clock(invoke_context, instruction_context, 2)?; let custodian = keyed_account_at_index(keyed_accounts, first_instruction_account + 3) .ok() @@ -127,13 +120,12 @@ pub fn process_instruction( } StakeInstruction::DelegateStake => { let vote = keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?; - let clock = get_sysvar_with_account_check::clock( - keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?, - invoke_context, - )?; + let clock = + get_sysvar_with_account_check::clock(invoke_context, instruction_context, 2)?; let stake_history = get_sysvar_with_account_check::stake_history( - keyed_account_at_index(keyed_accounts, first_instruction_account + 3)?, invoke_context, + instruction_context, + 3, )?; let config_account = keyed_account_at_index(keyed_accounts, first_instruction_account + 4)?; @@ -152,13 +144,12 @@ pub fn process_instruction( StakeInstruction::Merge => { let source_stake = &keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?; - let clock = get_sysvar_with_account_check::clock( - keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?, - invoke_context, - )?; + let clock = + get_sysvar_with_account_check::clock(invoke_context, instruction_context, 2)?; let stake_history = get_sysvar_with_account_check::stake_history( - keyed_account_at_index(keyed_accounts, first_instruction_account + 3)?, invoke_context, + instruction_context, + 3, )?; me.merge( invoke_context, @@ -170,13 +161,12 @@ pub fn process_instruction( } StakeInstruction::Withdraw(lamports) => { let to = &keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?; - let clock = get_sysvar_with_account_check::clock( - keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?, - invoke_context, - )?; + let clock = + get_sysvar_with_account_check::clock(invoke_context, instruction_context, 2)?; let stake_history = get_sysvar_with_account_check::stake_history( - keyed_account_at_index(keyed_accounts, first_instruction_account + 3)?, invoke_context, + instruction_context, + 3, )?; me.withdraw( lamports, @@ -188,10 +178,8 @@ pub fn process_instruction( ) } StakeInstruction::Deactivate => { - let clock = get_sysvar_with_account_check::clock( - keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?, - invoke_context, - )?; + let clock = + get_sysvar_with_account_check::clock(invoke_context, instruction_context, 1)?; me.deactivate(&clock, &signers) } StakeInstruction::SetLockup(lockup) => { @@ -214,10 +202,8 @@ pub fn process_instruction( .ok_or(InstructionError::MissingRequiredSignature)?, }; - let rent = get_sysvar_with_account_check::rent( - keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?, - invoke_context, - )?; + let rent = + get_sysvar_with_account_check::rent(invoke_context, instruction_context, 1)?; me.initialize(&authorized, &Lockup::default(), &rent) } else { Err(InstructionError::InvalidInstructionData) @@ -228,10 +214,8 @@ pub fn process_instruction( .feature_set .is_active(&feature_set::vote_stake_checked_instructions::id()) { - let clock = get_sysvar_with_account_check::clock( - keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?, - invoke_context, - )?; + let clock = + get_sysvar_with_account_check::clock(invoke_context, instruction_context, 1)?; let _current_authority = keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?; let authorized_pubkey = @@ -262,10 +246,8 @@ pub fn process_instruction( { let authority_base = keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?; - let clock = get_sysvar_with_account_check::clock( - keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?, - invoke_context, - )?; + let clock = + get_sysvar_with_account_check::clock(invoke_context, instruction_context, 2)?; let authorized_pubkey = &keyed_account_at_index(keyed_accounts, first_instruction_account + 3)? .signer_key() diff --git a/programs/vote/src/vote_processor.rs b/programs/vote/src/vote_processor.rs index e170653133..0454e93baa 100644 --- a/programs/vote/src/vote_processor.rs +++ b/programs/vote/src/vote_processor.rs @@ -36,22 +36,15 @@ pub fn process_instruction( let signers = instruction_context.get_signers(transaction_context); match limited_deserialize(data)? { VoteInstruction::InitializeAccount(vote_init) => { - let rent = get_sysvar_with_account_check::rent( - keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?, - invoke_context, - )?; + let rent = get_sysvar_with_account_check::rent(invoke_context, instruction_context, 1)?; verify_rent_exemption(me, &rent)?; - let clock = get_sysvar_with_account_check::clock( - keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?, - invoke_context, - )?; + let clock = + get_sysvar_with_account_check::clock(invoke_context, instruction_context, 2)?; vote_state::initialize_account(me, &vote_init, &signers, &clock) } VoteInstruction::Authorize(voter_pubkey, vote_authorize) => { - let clock = get_sysvar_with_account_check::clock( - keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?, - invoke_context, - )?; + let clock = + get_sysvar_with_account_check::clock(invoke_context, instruction_context, 1)?; vote_state::authorize( me, &voter_pubkey, @@ -71,14 +64,10 @@ pub fn process_instruction( } VoteInstruction::Vote(vote) | VoteInstruction::VoteSwitch(vote, _) => { inc_new_counter_info!("vote-native", 1); - let slot_hashes = get_sysvar_with_account_check::slot_hashes( - keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?, - invoke_context, - )?; - let clock = get_sysvar_with_account_check::clock( - keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?, - invoke_context, - )?; + let slot_hashes = + get_sysvar_with_account_check::slot_hashes(invoke_context, instruction_context, 1)?; + let clock = + get_sysvar_with_account_check::clock(invoke_context, instruction_context, 2)?; vote_state::process_vote( me, &slot_hashes, @@ -147,10 +136,8 @@ pub fn process_instruction( &keyed_account_at_index(keyed_accounts, first_instruction_account + 3)? .signer_key() .ok_or(InstructionError::MissingRequiredSignature)?; - let clock = get_sysvar_with_account_check::clock( - keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?, - invoke_context, - )?; + let clock = + get_sysvar_with_account_check::clock(invoke_context, instruction_context, 1)?; vote_state::authorize( me, voter_pubkey, diff --git a/runtime/src/system_instruction_processor.rs b/runtime/src/system_instruction_processor.rs index 0454e33777..c4c70dadf9 100644 --- a/runtime/src/system_instruction_processor.rs +++ b/runtime/src/system_instruction_processor.rs @@ -356,8 +356,9 @@ pub fn process_instruction( let me = &mut keyed_account_at_index(keyed_accounts, first_instruction_account)?; #[allow(deprecated)] let recent_blockhashes = get_sysvar_with_account_check::recent_blockhashes( - keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?, invoke_context, + instruction_context, + 1, )?; if recent_blockhashes.is_empty() { ic_msg!( @@ -373,21 +374,20 @@ pub fn process_instruction( let to = &mut keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?; #[allow(deprecated)] let _recent_blockhashes = get_sysvar_with_account_check::recent_blockhashes( - keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?, - invoke_context, - )?; - let rent = get_sysvar_with_account_check::rent( - keyed_account_at_index(keyed_accounts, first_instruction_account + 3)?, invoke_context, + instruction_context, + 2, )?; + let rent = get_sysvar_with_account_check::rent(invoke_context, instruction_context, 3)?; withdraw_nonce_account(me, lamports, to, &rent, &signers, invoke_context) } SystemInstruction::InitializeNonceAccount(authorized) => { let me = &mut keyed_account_at_index(keyed_accounts, first_instruction_account)?; #[allow(deprecated)] let recent_blockhashes = get_sysvar_with_account_check::recent_blockhashes( - keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?, invoke_context, + instruction_context, + 1, )?; if recent_blockhashes.is_empty() { ic_msg!( @@ -396,10 +396,7 @@ pub fn process_instruction( ); return Err(NonceError::NoRecentBlockhashes.into()); } - let rent = get_sysvar_with_account_check::rent( - keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?, - invoke_context, - )?; + let rent = get_sysvar_with_account_check::rent(invoke_context, instruction_context, 2)?; initialize_nonce_account(me, &authorized, &rent, invoke_context) } SystemInstruction::AuthorizeNonceAccount(nonce_authority) => {