Cleanup: InvokeContext accessors (#21574)

* Removes blockhash accessors from InvokeContext.

* Removes lamports_per_signature accessors from InvokeContext.

* Removes return_data accessors from InvokeContext.

* Removes feature_set accessor from InvokeContext.

* Removes instruction_recorders and instruction_index accessors from InvokeContext.

* Moves get_sysvars() into InvokeContext.

* Removes compute_meter parameter from InvokeContext::new().

* Removes InvokeContext::new_mock_with_sysvars_and_features().

* Removes InvokeContext::update_timing().
This commit is contained in:
Alexander Meißner
2021-12-03 12:15:22 +01:00
committed by GitHub
parent dab0e8fdc7
commit a9d5ef2055
14 changed files with 314 additions and 343 deletions

View File

@ -1,7 +1,7 @@
use {
crate::{config, stake_state::StakeAccount},
log::*,
solana_program_runtime::invoke_context::{get_sysvar, InvokeContext},
solana_program_runtime::invoke_context::InvokeContext,
solana_sdk::{
feature_set,
instruction::InstructionError,
@ -48,9 +48,9 @@ pub fn process_instruction(
)?)?,
),
StakeInstruction::Authorize(authorized_pubkey, stake_authorize) => {
let require_custodian_for_locked_stake_authorize = invoke_context.is_feature_active(
&feature_set::require_custodian_for_locked_stake_authorize::id(),
);
let require_custodian_for_locked_stake_authorize = invoke_context
.feature_set
.is_active(&feature_set::require_custodian_for_locked_stake_authorize::id());
if require_custodian_for_locked_stake_authorize {
let clock = from_keyed_account::<Clock>(keyed_account_at_index(
@ -86,9 +86,9 @@ pub fn process_instruction(
StakeInstruction::AuthorizeWithSeed(args) => {
let authority_base =
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?;
let require_custodian_for_locked_stake_authorize = invoke_context.is_feature_active(
&feature_set::require_custodian_for_locked_stake_authorize::id(),
);
let require_custodian_for_locked_stake_authorize = invoke_context
.feature_set
.is_active(&feature_set::require_custodian_for_locked_stake_authorize::id());
if require_custodian_for_locked_stake_authorize {
let clock = from_keyed_account::<Clock>(keyed_account_at_index(
@ -124,8 +124,9 @@ pub fn process_instruction(
}
}
StakeInstruction::DelegateStake => {
let can_reverse_deactivation =
invoke_context.is_feature_active(&feature_set::stake_program_v4::id());
let can_reverse_deactivation = invoke_context
.feature_set
.is_active(&feature_set::stake_program_v4::id());
let vote = keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?;
me.delegate(
@ -154,8 +155,9 @@ pub fn process_instruction(
StakeInstruction::Merge => {
let source_stake =
&keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?;
let can_merge_expired_lockups =
invoke_context.is_feature_active(&feature_set::stake_program_v4::id());
let can_merge_expired_lockups = invoke_context
.feature_set
.is_active(&feature_set::stake_program_v4::id());
me.merge(
invoke_context,
source_stake,
@ -186,7 +188,9 @@ pub fn process_instruction(
)?)?,
keyed_account_at_index(keyed_accounts, first_instruction_account + 4)?,
keyed_account_at_index(keyed_accounts, first_instruction_account + 5).ok(),
invoke_context.is_feature_active(&feature_set::stake_program_v4::id()),
invoke_context
.feature_set
.is_active(&feature_set::stake_program_v4::id()),
)
}
StakeInstruction::Deactivate => me.deactivate(
@ -197,15 +201,20 @@ pub fn process_instruction(
&signers,
),
StakeInstruction::SetLockup(lockup) => {
let clock = if invoke_context.is_feature_active(&feature_set::stake_program_v4::id()) {
Some(get_sysvar::<Clock>(invoke_context, &sysvar::clock::id())?)
let clock = if invoke_context
.feature_set
.is_active(&feature_set::stake_program_v4::id())
{
Some(invoke_context.get_sysvar::<Clock>(&sysvar::clock::id())?)
} else {
None
};
me.set_lockup(&lockup, &signers, clock.as_ref())
}
StakeInstruction::InitializeChecked => {
if invoke_context.is_feature_active(&feature_set::vote_stake_checked_instructions::id())
if invoke_context
.feature_set
.is_active(&feature_set::vote_stake_checked_instructions::id())
{
let authorized = Authorized {
staker: *keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?
@ -231,7 +240,9 @@ pub fn process_instruction(
}
}
StakeInstruction::AuthorizeChecked(stake_authorize) => {
if invoke_context.is_feature_active(&feature_set::vote_stake_checked_instructions::id())
if invoke_context
.feature_set
.is_active(&feature_set::vote_stake_checked_instructions::id())
{
let clock = from_keyed_account::<Clock>(keyed_account_at_index(
keyed_accounts,
@ -261,7 +272,9 @@ pub fn process_instruction(
}
}
StakeInstruction::AuthorizeCheckedWithSeed(args) => {
if invoke_context.is_feature_active(&feature_set::vote_stake_checked_instructions::id())
if invoke_context
.feature_set
.is_active(&feature_set::vote_stake_checked_instructions::id())
{
let authority_base =
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?;
@ -293,7 +306,9 @@ pub fn process_instruction(
}
}
StakeInstruction::SetLockupChecked(lockup_checked) => {
if invoke_context.is_feature_active(&feature_set::vote_stake_checked_instructions::id())
if invoke_context
.feature_set
.is_active(&feature_set::vote_stake_checked_instructions::id())
{
let custodian = if let Ok(custodian) =
keyed_account_at_index(keyed_accounts, first_instruction_account + 2)
@ -312,7 +327,7 @@ pub fn process_instruction(
epoch: lockup_checked.epoch,
custodian,
};
let clock = Some(get_sysvar::<Clock>(invoke_context, &sysvar::clock::id())?);
let clock = Some(invoke_context.get_sysvar::<Clock>(&sysvar::clock::id())?);
me.set_lockup(&lockup, &signers, clock.as_ref())
} else {
Err(InstructionError::InvalidInstructionData)
@ -331,7 +346,6 @@ mod tests {
};
use solana_sdk::{
account::{self, AccountSharedData},
feature_set::FeatureSet,
instruction::{AccountMeta, Instruction},
pubkey::Pubkey,
rent::Rent,
@ -342,7 +356,7 @@ mod tests {
},
sysvar::{stake_history::StakeHistory, Sysvar},
};
use std::{cell::RefCell, rc::Rc, str::FromStr, sync::Arc};
use std::{cell::RefCell, rc::Rc, str::FromStr};
fn create_default_account() -> Rc<RefCell<AccountSharedData>> {
AccountSharedData::new_ref(0, 0, &Pubkey::new_unique())
@ -415,13 +429,9 @@ mod tests {
preparation.accounts.push((id(), processor_account));
let mut data = Vec::with_capacity(sysvar::clock::Clock::size_of());
bincode::serialize_into(&mut data, &sysvar::clock::Clock::default()).unwrap();
let mut invoke_context = InvokeContext::new_mock(&preparation.accounts, &[]);
let sysvars = [(sysvar::clock::id(), data)];
let mut invoke_context = InvokeContext::new_mock_with_sysvars_and_features(
&preparation.accounts,
&[],
&sysvars,
Arc::new(FeatureSet::all_enabled()),
);
invoke_context.sysvars = &sysvars;
invoke_context.push(
&preparation.message,
&preparation.message.instructions[0],
@ -1065,13 +1075,9 @@ mod tests {
preparation.accounts.push((id(), processor_account));
let mut data = Vec::with_capacity(sysvar::clock::Clock::size_of());
bincode::serialize_into(&mut data, &sysvar::clock::Clock::default()).unwrap();
let mut invoke_context = InvokeContext::new_mock(&preparation.accounts, &[]);
let sysvars = [(sysvar::clock::id(), data)];
let mut invoke_context = InvokeContext::new_mock_with_sysvars_and_features(
&preparation.accounts,
&[],
&sysvars,
Arc::new(FeatureSet::all_enabled()),
);
invoke_context.sysvars = &sysvars;
invoke_context
.push(
&preparation.message,

View File

@ -978,7 +978,8 @@ impl MergeKind {
.zip(source.active_stake())
.map(|(stake, source)| {
if invoke_context
.is_feature_active(&stake_merge_with_unmatched_credits_observed::id())
.feature_set
.is_active(&stake_merge_with_unmatched_credits_observed::id())
{
Self::active_delegations_can_merge(
invoke_context,
@ -1038,7 +1039,10 @@ fn merge_delegation_stake_and_credits_observed(
absorbed_lamports: u64,
absorbed_credits_observed: u64,
) -> Result<(), InstructionError> {
if invoke_context.is_feature_active(&stake_merge_with_unmatched_credits_observed::id()) {
if invoke_context
.feature_set
.is_active(&stake_merge_with_unmatched_credits_observed::id())
{
stake.credits_observed =
stake_weighted_credits_observed(stake, absorbed_lamports, absorbed_credits_observed)
.ok_or(InstructionError::ArithmeticOverflow)?;