Refactor: Replaces KeyedAccount in_get_sysvar_with_account_check (#23905)

* Replaces all use sites of get_sysvar_with_account_check by get_sysvar_with_account_check2.

* Removes get_sysvar_with_account_check.

* Renames get_sysvar_with_account_check2 to get_sysvar_with_account_check.
This commit is contained in:
Alexander Meißner
2022-03-24 19:30:42 +01:00
committed by GitHub
parent 37c36ce3fa
commit 140c8dd01f
5 changed files with 50 additions and 161 deletions

View File

@ -5,7 +5,6 @@ use {
solana_sdk::{ solana_sdk::{
account::{AccountSharedData, ReadableAccount}, account::{AccountSharedData, ReadableAccount},
instruction::InstructionError, instruction::InstructionError,
keyed_account::KeyedAccount,
pubkey::Pubkey, pubkey::Pubkey,
sysvar::{ sysvar::{
clock::Clock, epoch_schedule::EpochSchedule, rent::Rent, slot_hashes::SlotHashes, clock::Clock, epoch_schedule::EpochSchedule, rent::Rent, slot_hashes::SlotHashes,
@ -182,60 +181,6 @@ impl SysvarCache {
pub mod get_sysvar_with_account_check { pub mod get_sysvar_with_account_check {
use super::*; use super::*;
fn check_sysvar_keyed_account<S: Sysvar>(
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<Arc<Clock>, InstructionError> {
check_sysvar_keyed_account::<Clock>(keyed_account)?;
invoke_context.get_sysvar_cache().get_clock()
}
pub fn rent(
keyed_account: &KeyedAccount,
invoke_context: &InvokeContext,
) -> Result<Arc<Rent>, InstructionError> {
check_sysvar_keyed_account::<Rent>(keyed_account)?;
invoke_context.get_sysvar_cache().get_rent()
}
pub fn slot_hashes(
keyed_account: &KeyedAccount,
invoke_context: &InvokeContext,
) -> Result<Arc<SlotHashes>, InstructionError> {
check_sysvar_keyed_account::<SlotHashes>(keyed_account)?;
invoke_context.get_sysvar_cache().get_slot_hashes()
}
#[allow(deprecated)]
pub fn recent_blockhashes(
keyed_account: &KeyedAccount,
invoke_context: &InvokeContext,
) -> Result<Arc<RecentBlockhashes>, InstructionError> {
check_sysvar_keyed_account::<RecentBlockhashes>(keyed_account)?;
invoke_context.get_sysvar_cache().get_recent_blockhashes()
}
pub fn stake_history(
keyed_account: &KeyedAccount,
invoke_context: &InvokeContext,
) -> Result<Arc<StakeHistory>, InstructionError> {
check_sysvar_keyed_account::<StakeHistory>(keyed_account)?;
invoke_context.get_sysvar_cache().get_stake_history()
}
}
pub mod get_sysvar_with_account_check2 {
use super::*;
fn check_sysvar_account<S: Sysvar>( fn check_sysvar_account<S: Sysvar>(
transaction_context: &TransactionContext, transaction_context: &TransactionContext,
instruction_context: &InstructionContext, instruction_context: &InstructionContext,

View File

@ -492,20 +492,9 @@ fn process_loader_upgradeable_instruction(
keyed_accounts, keyed_accounts,
first_instruction_account.saturating_add(3), first_instruction_account.saturating_add(3),
)?; )?;
let rent = get_sysvar_with_account_check::rent( let rent = get_sysvar_with_account_check::rent(invoke_context, instruction_context, 4)?;
keyed_account_at_index( let clock =
keyed_accounts, get_sysvar_with_account_check::clock(invoke_context, instruction_context, 5)?;
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 authority = keyed_account_at_index( let authority = keyed_account_at_index(
keyed_accounts, keyed_accounts,
first_instruction_account.saturating_add(7), first_instruction_account.saturating_add(7),
@ -683,20 +672,9 @@ fn process_loader_upgradeable_instruction(
keyed_accounts, keyed_accounts,
first_instruction_account.saturating_add(2), first_instruction_account.saturating_add(2),
)?; )?;
let rent = get_sysvar_with_account_check::rent( let rent = get_sysvar_with_account_check::rent(invoke_context, instruction_context, 4)?;
keyed_account_at_index( let clock =
keyed_accounts, get_sysvar_with_account_check::clock(invoke_context, instruction_context, 5)?;
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 authority = keyed_account_at_index( let authority = keyed_account_at_index(
keyed_accounts, keyed_accounts,
first_instruction_account.saturating_add(6), first_instruction_account.saturating_add(6),

View File

@ -43,10 +43,7 @@ pub fn process_instruction(
let signers = instruction_context.get_signers(transaction_context); let signers = instruction_context.get_signers(transaction_context);
match limited_deserialize(data)? { match limited_deserialize(data)? {
StakeInstruction::Initialize(authorized, lockup) => { StakeInstruction::Initialize(authorized, lockup) => {
let rent = get_sysvar_with_account_check::rent( let rent = get_sysvar_with_account_check::rent(invoke_context, instruction_context, 1)?;
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?,
invoke_context,
)?;
me.initialize(&authorized, &lockup, &rent) me.initialize(&authorized, &lockup, &rent)
} }
StakeInstruction::Authorize(authorized_pubkey, stake_authorize) => { 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()); .is_active(&feature_set::require_custodian_for_locked_stake_authorize::id());
if require_custodian_for_locked_stake_authorize { if require_custodian_for_locked_stake_authorize {
let clock = get_sysvar_with_account_check::clock( let clock =
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?, get_sysvar_with_account_check::clock(invoke_context, instruction_context, 1)?;
invoke_context,
)?;
let _current_authority = let _current_authority =
keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?; keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?;
let custodian = let custodian =
@ -93,10 +88,8 @@ pub fn process_instruction(
.is_active(&feature_set::require_custodian_for_locked_stake_authorize::id()); .is_active(&feature_set::require_custodian_for_locked_stake_authorize::id());
if require_custodian_for_locked_stake_authorize { if require_custodian_for_locked_stake_authorize {
let clock = get_sysvar_with_account_check::clock( let clock =
keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?, get_sysvar_with_account_check::clock(invoke_context, instruction_context, 2)?;
invoke_context,
)?;
let custodian = let custodian =
keyed_account_at_index(keyed_accounts, first_instruction_account + 3) keyed_account_at_index(keyed_accounts, first_instruction_account + 3)
.ok() .ok()
@ -127,13 +120,12 @@ pub fn process_instruction(
} }
StakeInstruction::DelegateStake => { StakeInstruction::DelegateStake => {
let vote = keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?; let vote = keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?;
let clock = get_sysvar_with_account_check::clock( let clock =
keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?, get_sysvar_with_account_check::clock(invoke_context, instruction_context, 2)?;
invoke_context,
)?;
let stake_history = get_sysvar_with_account_check::stake_history( let stake_history = get_sysvar_with_account_check::stake_history(
keyed_account_at_index(keyed_accounts, first_instruction_account + 3)?,
invoke_context, invoke_context,
instruction_context,
3,
)?; )?;
let config_account = let config_account =
keyed_account_at_index(keyed_accounts, first_instruction_account + 4)?; keyed_account_at_index(keyed_accounts, first_instruction_account + 4)?;
@ -152,13 +144,12 @@ pub fn process_instruction(
StakeInstruction::Merge => { StakeInstruction::Merge => {
let source_stake = let source_stake =
&keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?; &keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?;
let clock = get_sysvar_with_account_check::clock( let clock =
keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?, get_sysvar_with_account_check::clock(invoke_context, instruction_context, 2)?;
invoke_context,
)?;
let stake_history = get_sysvar_with_account_check::stake_history( let stake_history = get_sysvar_with_account_check::stake_history(
keyed_account_at_index(keyed_accounts, first_instruction_account + 3)?,
invoke_context, invoke_context,
instruction_context,
3,
)?; )?;
me.merge( me.merge(
invoke_context, invoke_context,
@ -170,13 +161,12 @@ pub fn process_instruction(
} }
StakeInstruction::Withdraw(lamports) => { StakeInstruction::Withdraw(lamports) => {
let to = &keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?; let to = &keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?;
let clock = get_sysvar_with_account_check::clock( let clock =
keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?, get_sysvar_with_account_check::clock(invoke_context, instruction_context, 2)?;
invoke_context,
)?;
let stake_history = get_sysvar_with_account_check::stake_history( let stake_history = get_sysvar_with_account_check::stake_history(
keyed_account_at_index(keyed_accounts, first_instruction_account + 3)?,
invoke_context, invoke_context,
instruction_context,
3,
)?; )?;
me.withdraw( me.withdraw(
lamports, lamports,
@ -188,10 +178,8 @@ pub fn process_instruction(
) )
} }
StakeInstruction::Deactivate => { StakeInstruction::Deactivate => {
let clock = get_sysvar_with_account_check::clock( let clock =
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?, get_sysvar_with_account_check::clock(invoke_context, instruction_context, 1)?;
invoke_context,
)?;
me.deactivate(&clock, &signers) me.deactivate(&clock, &signers)
} }
StakeInstruction::SetLockup(lockup) => { StakeInstruction::SetLockup(lockup) => {
@ -214,10 +202,8 @@ pub fn process_instruction(
.ok_or(InstructionError::MissingRequiredSignature)?, .ok_or(InstructionError::MissingRequiredSignature)?,
}; };
let rent = get_sysvar_with_account_check::rent( let rent =
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?, get_sysvar_with_account_check::rent(invoke_context, instruction_context, 1)?;
invoke_context,
)?;
me.initialize(&authorized, &Lockup::default(), &rent) me.initialize(&authorized, &Lockup::default(), &rent)
} else { } else {
Err(InstructionError::InvalidInstructionData) Err(InstructionError::InvalidInstructionData)
@ -228,10 +214,8 @@ pub fn process_instruction(
.feature_set .feature_set
.is_active(&feature_set::vote_stake_checked_instructions::id()) .is_active(&feature_set::vote_stake_checked_instructions::id())
{ {
let clock = get_sysvar_with_account_check::clock( let clock =
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?, get_sysvar_with_account_check::clock(invoke_context, instruction_context, 1)?;
invoke_context,
)?;
let _current_authority = let _current_authority =
keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?; keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?;
let authorized_pubkey = let authorized_pubkey =
@ -262,10 +246,8 @@ pub fn process_instruction(
{ {
let authority_base = let authority_base =
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?; keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?;
let clock = get_sysvar_with_account_check::clock( let clock =
keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?, get_sysvar_with_account_check::clock(invoke_context, instruction_context, 2)?;
invoke_context,
)?;
let authorized_pubkey = let authorized_pubkey =
&keyed_account_at_index(keyed_accounts, first_instruction_account + 3)? &keyed_account_at_index(keyed_accounts, first_instruction_account + 3)?
.signer_key() .signer_key()

View File

@ -36,22 +36,15 @@ pub fn process_instruction(
let signers = instruction_context.get_signers(transaction_context); let signers = instruction_context.get_signers(transaction_context);
match limited_deserialize(data)? { match limited_deserialize(data)? {
VoteInstruction::InitializeAccount(vote_init) => { VoteInstruction::InitializeAccount(vote_init) => {
let rent = get_sysvar_with_account_check::rent( let rent = get_sysvar_with_account_check::rent(invoke_context, instruction_context, 1)?;
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?,
invoke_context,
)?;
verify_rent_exemption(me, &rent)?; verify_rent_exemption(me, &rent)?;
let clock = get_sysvar_with_account_check::clock( let clock =
keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?, get_sysvar_with_account_check::clock(invoke_context, instruction_context, 2)?;
invoke_context,
)?;
vote_state::initialize_account(me, &vote_init, &signers, &clock) vote_state::initialize_account(me, &vote_init, &signers, &clock)
} }
VoteInstruction::Authorize(voter_pubkey, vote_authorize) => { VoteInstruction::Authorize(voter_pubkey, vote_authorize) => {
let clock = get_sysvar_with_account_check::clock( let clock =
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?, get_sysvar_with_account_check::clock(invoke_context, instruction_context, 1)?;
invoke_context,
)?;
vote_state::authorize( vote_state::authorize(
me, me,
&voter_pubkey, &voter_pubkey,
@ -71,14 +64,10 @@ pub fn process_instruction(
} }
VoteInstruction::Vote(vote) | VoteInstruction::VoteSwitch(vote, _) => { VoteInstruction::Vote(vote) | VoteInstruction::VoteSwitch(vote, _) => {
inc_new_counter_info!("vote-native", 1); inc_new_counter_info!("vote-native", 1);
let slot_hashes = get_sysvar_with_account_check::slot_hashes( let slot_hashes =
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?, get_sysvar_with_account_check::slot_hashes(invoke_context, instruction_context, 1)?;
invoke_context, let clock =
)?; get_sysvar_with_account_check::clock(invoke_context, instruction_context, 2)?;
let clock = get_sysvar_with_account_check::clock(
keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?,
invoke_context,
)?;
vote_state::process_vote( vote_state::process_vote(
me, me,
&slot_hashes, &slot_hashes,
@ -147,10 +136,8 @@ pub fn process_instruction(
&keyed_account_at_index(keyed_accounts, first_instruction_account + 3)? &keyed_account_at_index(keyed_accounts, first_instruction_account + 3)?
.signer_key() .signer_key()
.ok_or(InstructionError::MissingRequiredSignature)?; .ok_or(InstructionError::MissingRequiredSignature)?;
let clock = get_sysvar_with_account_check::clock( let clock =
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?, get_sysvar_with_account_check::clock(invoke_context, instruction_context, 1)?;
invoke_context,
)?;
vote_state::authorize( vote_state::authorize(
me, me,
voter_pubkey, voter_pubkey,

View File

@ -356,8 +356,9 @@ pub fn process_instruction(
let me = &mut keyed_account_at_index(keyed_accounts, first_instruction_account)?; let me = &mut keyed_account_at_index(keyed_accounts, first_instruction_account)?;
#[allow(deprecated)] #[allow(deprecated)]
let recent_blockhashes = get_sysvar_with_account_check::recent_blockhashes( let recent_blockhashes = get_sysvar_with_account_check::recent_blockhashes(
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?,
invoke_context, invoke_context,
instruction_context,
1,
)?; )?;
if recent_blockhashes.is_empty() { if recent_blockhashes.is_empty() {
ic_msg!( ic_msg!(
@ -373,21 +374,20 @@ pub fn process_instruction(
let to = &mut keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?; let to = &mut keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?;
#[allow(deprecated)] #[allow(deprecated)]
let _recent_blockhashes = get_sysvar_with_account_check::recent_blockhashes( 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, 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) withdraw_nonce_account(me, lamports, to, &rent, &signers, invoke_context)
} }
SystemInstruction::InitializeNonceAccount(authorized) => { SystemInstruction::InitializeNonceAccount(authorized) => {
let me = &mut keyed_account_at_index(keyed_accounts, first_instruction_account)?; let me = &mut keyed_account_at_index(keyed_accounts, first_instruction_account)?;
#[allow(deprecated)] #[allow(deprecated)]
let recent_blockhashes = get_sysvar_with_account_check::recent_blockhashes( let recent_blockhashes = get_sysvar_with_account_check::recent_blockhashes(
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?,
invoke_context, invoke_context,
instruction_context,
1,
)?; )?;
if recent_blockhashes.is_empty() { if recent_blockhashes.is_empty() {
ic_msg!( ic_msg!(
@ -396,10 +396,7 @@ pub fn process_instruction(
); );
return Err(NonceError::NoRecentBlockhashes.into()); return Err(NonceError::NoRecentBlockhashes.into());
} }
let rent = get_sysvar_with_account_check::rent( let rent = get_sysvar_with_account_check::rent(invoke_context, instruction_context, 2)?;
keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?,
invoke_context,
)?;
initialize_nonce_account(me, &authorized, &rent, invoke_context) initialize_nonce_account(me, &authorized, &rent, invoke_context)
} }
SystemInstruction::AuthorizeNonceAccount(nonce_authority) => { SystemInstruction::AuthorizeNonceAccount(nonce_authority) => {