Skip to content

Commit

Permalink
Replaces KeyedAccount by BorrowedAccount in nonce_keyed_account. (
Browse files Browse the repository at this point in the history
solana-labs#23214)

* Adds get_sysvar_with_account_check2 for ABIv2.

* Replaces get_signers() and get_sysvar_with_account_check() in system_instruction_processor.

* Replaces KeyedAccount by BorrowedAccount in nonce_keyed_account.
  • Loading branch information
Lichtso authored Feb 17, 2022
1 parent da00b39 commit 1a68f81
Show file tree
Hide file tree
Showing 4 changed files with 1,441 additions and 963 deletions.
84 changes: 84 additions & 0 deletions program-runtime/src/sysvar_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use {
clock::Clock, epoch_schedule::EpochSchedule, rent::Rent, slot_hashes::SlotHashes,
stake_history::StakeHistory, Sysvar, SysvarId,
},
transaction_context::{InstructionContext, TransactionContext},
},
std::sync::Arc,
};
Expand Down Expand Up @@ -231,3 +232,86 @@ pub mod get_sysvar_with_account_check {
invoke_context.get_sysvar_cache().get_stake_history()
}
}

pub mod get_sysvar_with_account_check2 {
use super::*;

fn check_sysvar_account<S: Sysvar>(
transaction_context: &TransactionContext,
instruction_context: &InstructionContext,
index_in_instruction: usize,
) -> Result<(), InstructionError> {
let index_in_transaction =
instruction_context.get_index_in_transaction(index_in_instruction)?;
if !S::check_id(transaction_context.get_key_of_account_at_index(index_in_transaction)?) {
return Err(InstructionError::InvalidArgument);
}
Ok(())
}

pub fn clock(
invoke_context: &InvokeContext,
instruction_context: &InstructionContext,
index_in_instruction: usize,
) -> Result<Arc<Clock>, InstructionError> {
check_sysvar_account::<Clock>(
invoke_context.transaction_context,
instruction_context,
index_in_instruction,
)?;
invoke_context.get_sysvar_cache().get_clock()
}

pub fn rent(
invoke_context: &InvokeContext,
instruction_context: &InstructionContext,
index_in_instruction: usize,
) -> Result<Arc<Rent>, InstructionError> {
check_sysvar_account::<Rent>(
invoke_context.transaction_context,
instruction_context,
index_in_instruction,
)?;
invoke_context.get_sysvar_cache().get_rent()
}

pub fn slot_hashes(
invoke_context: &InvokeContext,
instruction_context: &InstructionContext,
index_in_instruction: usize,
) -> Result<Arc<SlotHashes>, InstructionError> {
check_sysvar_account::<SlotHashes>(
invoke_context.transaction_context,
instruction_context,
index_in_instruction,
)?;
invoke_context.get_sysvar_cache().get_slot_hashes()
}

#[allow(deprecated)]
pub fn recent_blockhashes(
invoke_context: &InvokeContext,
instruction_context: &InstructionContext,
index_in_instruction: usize,
) -> Result<Arc<RecentBlockhashes>, InstructionError> {
check_sysvar_account::<RecentBlockhashes>(
invoke_context.transaction_context,
instruction_context,
index_in_instruction,
)?;
invoke_context.get_sysvar_cache().get_recent_blockhashes()
}

pub fn stake_history(
invoke_context: &InvokeContext,
instruction_context: &InstructionContext,
index_in_instruction: usize,
) -> Result<Arc<StakeHistory>, InstructionError> {
check_sysvar_account::<StakeHistory>(
invoke_context.transaction_context,
instruction_context,
index_in_instruction,
)?;
invoke_context.get_sysvar_cache().get_stake_history()
}
}
Loading

0 comments on commit 1a68f81

Please sign in to comment.