Refactor: Use SysvarCache in all builtin programs (#22864)

* Replaces from_keyed_account() by SysvarCache in stake instruction.

* Replaces from_keyed_account() by SysvarCache in system instruction processor.

* Removes from_keyed_account().
Moves check_sysvar_keyed_account() into sysvar_cache.rs

* Removes tests which test for incorrectly serialized sysvars.
This commit is contained in:
Alexander Meißner
2022-02-03 13:03:50 +01:00
committed by GitHub
parent 60af1a4cce
commit c16cf9cf8a
4 changed files with 137 additions and 331 deletions

View File

@ -5,11 +5,11 @@ use {
solana_sdk::{ solana_sdk::{
account::{AccountSharedData, ReadableAccount}, account::{AccountSharedData, ReadableAccount},
instruction::InstructionError, instruction::InstructionError,
keyed_account::{check_sysvar_keyed_account, KeyedAccount}, 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,
stake_history::StakeHistory, SysvarId, stake_history::StakeHistory, Sysvar, SysvarId,
}, },
}, },
std::sync::Arc, std::sync::Arc,
@ -181,6 +181,15 @@ 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( pub fn clock(
keyed_account: &KeyedAccount, keyed_account: &KeyedAccount,
invoke_context: &InvokeContext, invoke_context: &InvokeContext,

View File

@ -6,18 +6,20 @@ pub use solana_sdk::stake::instruction::*;
use { use {
crate::{config, stake_state::StakeAccount}, crate::{config, stake_state::StakeAccount},
log::*, log::*,
solana_program_runtime::invoke_context::InvokeContext, solana_program_runtime::{
invoke_context::InvokeContext, sysvar_cache::get_sysvar_with_account_check,
},
solana_sdk::{ solana_sdk::{
feature_set, feature_set,
instruction::InstructionError, instruction::InstructionError,
keyed_account::{from_keyed_account, get_signers, keyed_account_at_index}, keyed_account::{get_signers, keyed_account_at_index},
program_utils::limited_deserialize, program_utils::limited_deserialize,
stake::{ stake::{
instruction::StakeInstruction, instruction::StakeInstruction,
program::id, program::id,
state::{Authorized, Lockup}, state::{Authorized, Lockup},
}, },
sysvar::{clock::Clock, rent::Rent, stake_history::StakeHistory}, sysvar::clock::Clock,
}, },
}; };
@ -38,24 +40,23 @@ pub fn process_instruction(
let signers = get_signers(&keyed_accounts[first_instruction_account..]); let signers = get_signers(&keyed_accounts[first_instruction_account..]);
match limited_deserialize(data)? { match limited_deserialize(data)? {
StakeInstruction::Initialize(authorized, lockup) => me.initialize( StakeInstruction::Initialize(authorized, lockup) => {
&authorized, let rent = get_sysvar_with_account_check::rent(
&lockup, keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?,
&from_keyed_account::<Rent>(keyed_account_at_index( invoke_context,
keyed_accounts, )?;
first_instruction_account + 1, me.initialize(&authorized, &lockup, &rent)
)?)?, }
),
StakeInstruction::Authorize(authorized_pubkey, stake_authorize) => { StakeInstruction::Authorize(authorized_pubkey, stake_authorize) => {
let require_custodian_for_locked_stake_authorize = invoke_context let require_custodian_for_locked_stake_authorize = invoke_context
.feature_set .feature_set
.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 = from_keyed_account::<Clock>(keyed_account_at_index( let clock = get_sysvar_with_account_check::clock(
keyed_accounts, keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?,
first_instruction_account + 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 =
@ -90,10 +91,10 @@ 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 = from_keyed_account::<Clock>(keyed_account_at_index( let clock = get_sysvar_with_account_check::clock(
keyed_accounts, keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?,
first_instruction_account + 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()
@ -124,17 +125,18 @@ 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(
keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?,
invoke_context,
)?;
let stake_history = get_sysvar_with_account_check::stake_history(
keyed_account_at_index(keyed_accounts, first_instruction_account + 3)?,
invoke_context,
)?;
me.delegate( me.delegate(
vote, vote,
&from_keyed_account::<Clock>(keyed_account_at_index( &clock,
keyed_accounts, &stake_history,
first_instruction_account + 2,
)?)?,
&from_keyed_account::<StakeHistory>(keyed_account_at_index(
keyed_accounts,
first_instruction_account + 3,
)?)?,
&config::from_keyed_account(keyed_account_at_index( &config::from_keyed_account(keyed_account_at_index(
keyed_accounts, keyed_accounts,
first_instruction_account + 4, first_instruction_account + 4,
@ -150,44 +152,48 @@ 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(
keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?,
invoke_context,
)?;
let stake_history = get_sysvar_with_account_check::stake_history(
keyed_account_at_index(keyed_accounts, first_instruction_account + 3)?,
invoke_context,
)?;
me.merge( me.merge(
invoke_context, invoke_context,
source_stake, source_stake,
&from_keyed_account::<Clock>(keyed_account_at_index( &clock,
keyed_accounts, &stake_history,
first_instruction_account + 2,
)?)?,
&from_keyed_account::<StakeHistory>(keyed_account_at_index(
keyed_accounts,
first_instruction_account + 3,
)?)?,
&signers, &signers,
) )
} }
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(
keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?,
invoke_context,
)?;
let stake_history = get_sysvar_with_account_check::stake_history(
keyed_account_at_index(keyed_accounts, first_instruction_account + 3)?,
invoke_context,
)?;
me.withdraw( me.withdraw(
lamports, lamports,
to, to,
&from_keyed_account::<Clock>(keyed_account_at_index( &clock,
keyed_accounts, &stake_history,
first_instruction_account + 2,
)?)?,
&from_keyed_account::<StakeHistory>(keyed_account_at_index(
keyed_accounts,
first_instruction_account + 3,
)?)?,
keyed_account_at_index(keyed_accounts, first_instruction_account + 4)?, keyed_account_at_index(keyed_accounts, first_instruction_account + 4)?,
keyed_account_at_index(keyed_accounts, first_instruction_account + 5).ok(), keyed_account_at_index(keyed_accounts, first_instruction_account + 5).ok(),
) )
} }
StakeInstruction::Deactivate => me.deactivate( StakeInstruction::Deactivate => {
&from_keyed_account::<Clock>(keyed_account_at_index( let clock = get_sysvar_with_account_check::clock(
keyed_accounts, keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?,
first_instruction_account + 1, invoke_context,
)?)?, )?;
&signers, me.deactivate(&clock, &signers)
), }
StakeInstruction::SetLockup(lockup) => { StakeInstruction::SetLockup(lockup) => {
let clock = invoke_context.get_sysvar_cache().get_clock()?; let clock = invoke_context.get_sysvar_cache().get_clock()?;
me.set_lockup(&lockup, &signers, &clock) me.set_lockup(&lockup, &signers, &clock)
@ -208,14 +214,11 @@ pub fn process_instruction(
.ok_or(InstructionError::MissingRequiredSignature)?, .ok_or(InstructionError::MissingRequiredSignature)?,
}; };
me.initialize( let rent = get_sysvar_with_account_check::rent(
&authorized, keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?,
&Lockup::default(), invoke_context,
&from_keyed_account::<Rent>(keyed_account_at_index( )?;
keyed_accounts, me.initialize(&authorized, &Lockup::default(), &rent)
first_instruction_account + 1,
)?)?,
)
} else { } else {
Err(InstructionError::InvalidInstructionData) Err(InstructionError::InvalidInstructionData)
} }
@ -225,10 +228,10 @@ 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 = from_keyed_account::<Clock>(keyed_account_at_index( let clock = get_sysvar_with_account_check::clock(
keyed_accounts, keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?,
first_instruction_account + 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 =
@ -259,10 +262,10 @@ 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 = from_keyed_account::<Clock>(keyed_account_at_index( let clock = get_sysvar_with_account_check::clock(
keyed_accounts, keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?,
first_instruction_account + 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()
@ -655,32 +658,6 @@ mod tests {
Err(InstructionError::NotEnoughAccountKeys), Err(InstructionError::NotEnoughAccountKeys),
); );
// rent fails to deserialize
process_instruction(
&serialize(&StakeInstruction::Initialize(
Authorized::default(),
Lockup::default(),
))
.unwrap(),
vec![
(stake_address, stake_account.clone()),
(rent_address, create_default_account()),
],
vec![
AccountMeta {
pubkey: stake_address,
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: rent_address,
is_signer: false,
is_writable: false,
},
],
Err(InstructionError::InvalidArgument),
);
// fails to deserialize stake state // fails to deserialize stake state
process_instruction( process_instruction(
&serialize(&StakeInstruction::Initialize( &serialize(&StakeInstruction::Initialize(

View File

@ -1,13 +1,15 @@
use { use {
crate::nonce_keyed_account::NonceKeyedAccount, crate::nonce_keyed_account::NonceKeyedAccount,
log::*, log::*,
solana_program_runtime::{ic_msg, invoke_context::InvokeContext}, solana_program_runtime::{
ic_msg, invoke_context::InvokeContext, sysvar_cache::get_sysvar_with_account_check,
},
solana_sdk::{ solana_sdk::{
account::{AccountSharedData, ReadableAccount, WritableAccount}, account::{AccountSharedData, ReadableAccount, WritableAccount},
account_utils::StateMut, account_utils::StateMut,
feature_set, feature_set,
instruction::InstructionError, instruction::InstructionError,
keyed_account::{from_keyed_account, get_signers, keyed_account_at_index, KeyedAccount}, keyed_account::{get_signers, keyed_account_at_index, KeyedAccount},
nonce, nonce,
program_utils::limited_deserialize, program_utils::limited_deserialize,
pubkey::Pubkey, pubkey::Pubkey,
@ -15,7 +17,6 @@ use {
NonceError, SystemError, SystemInstruction, MAX_PERMITTED_DATA_LENGTH, NonceError, SystemError, SystemInstruction, MAX_PERMITTED_DATA_LENGTH,
}, },
system_program, system_program,
sysvar::rent::Rent,
}, },
std::collections::HashSet, std::collections::HashSet,
}; };
@ -349,11 +350,11 @@ pub fn process_instruction(
SystemInstruction::AdvanceNonceAccount => { SystemInstruction::AdvanceNonceAccount => {
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)]
if from_keyed_account::<solana_sdk::sysvar::recent_blockhashes::RecentBlockhashes>( let recent_blockhashes = get_sysvar_with_account_check::recent_blockhashes(
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?, keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?,
)? invoke_context,
.is_empty() )?;
{ if recent_blockhashes.is_empty() {
ic_msg!( ic_msg!(
invoke_context, invoke_context,
"Advance nonce account: recent blockhash list is empty", "Advance nonce account: recent blockhash list is empty",
@ -366,42 +367,35 @@ 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)?;
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 _ = from_keyed_account::<solana_sdk::sysvar::recent_blockhashes::RecentBlockhashes>( let _recent_blockhashes = get_sysvar_with_account_check::recent_blockhashes(
keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?, keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?,
)?;
me.withdraw_nonce_account(
lamports,
to,
&from_keyed_account::<Rent>(keyed_account_at_index(
keyed_accounts,
first_instruction_account + 3,
)?)?,
&signers,
invoke_context, invoke_context,
) )?;
let rent = get_sysvar_with_account_check::rent(
keyed_account_at_index(keyed_accounts, first_instruction_account + 3)?,
invoke_context,
)?;
me.withdraw_nonce_account(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)]
if from_keyed_account::<solana_sdk::sysvar::recent_blockhashes::RecentBlockhashes>( let recent_blockhashes = get_sysvar_with_account_check::recent_blockhashes(
keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?, keyed_account_at_index(keyed_accounts, first_instruction_account + 1)?,
)? invoke_context,
.is_empty() )?;
{ if recent_blockhashes.is_empty() {
ic_msg!( ic_msg!(
invoke_context, invoke_context,
"Initialize nonce account: recent blockhash list is empty", "Initialize nonce account: recent blockhash list is empty",
); );
return Err(NonceError::NoRecentBlockhashes.into()); return Err(NonceError::NoRecentBlockhashes.into());
} }
me.initialize_nonce_account( let rent = get_sysvar_with_account_check::rent(
&authorized, keyed_account_at_index(keyed_accounts, first_instruction_account + 2)?,
&from_keyed_account::<Rent>(keyed_account_at_index(
keyed_accounts,
first_instruction_account + 2,
)?)?,
invoke_context, invoke_context,
) )?;
me.initialize_nonce_account(&authorized, &rent, invoke_context)
} }
SystemInstruction::AuthorizeNonceAccount(nonce_authority) => { SystemInstruction::AuthorizeNonceAccount(nonce_authority) => {
let me = &mut keyed_account_at_index(keyed_accounts, first_instruction_account)?; let me = &mut keyed_account_at_index(keyed_accounts, first_instruction_account)?;
@ -485,8 +479,8 @@ mod tests {
message::Message, message::Message,
nonce, nonce_account, recent_blockhashes_account, nonce, nonce_account, recent_blockhashes_account,
signature::{Keypair, Signer}, signature::{Keypair, Signer},
system_instruction, system_program, sysvar, system_instruction, system_program,
sysvar::recent_blockhashes::IterItem, sysvar::{self, recent_blockhashes::IterItem, rent::Rent},
transaction::TransactionError, transaction::TransactionError,
transaction_context::TransactionContext, transaction_context::TransactionContext,
}; };
@ -494,7 +488,9 @@ mod tests {
super::*, super::*,
crate::{bank::Bank, bank_client::BankClient}, crate::{bank::Bank, bank_client::BankClient},
bincode::serialize, bincode::serialize,
solana_program_runtime::invoke_context::{mock_process_instruction, InvokeContext}, solana_program_runtime::invoke_context::{
mock_process_instruction, InvokeContext, ProcessInstructionWithContext,
},
std::{cell::RefCell, sync::Arc}, std::{cell::RefCell, sync::Arc},
}; };
@ -512,6 +508,7 @@ mod tests {
transaction_accounts: Vec<(Pubkey, AccountSharedData)>, transaction_accounts: Vec<(Pubkey, AccountSharedData)>,
instruction_accounts: Vec<AccountMeta>, instruction_accounts: Vec<AccountMeta>,
expected_result: Result<(), InstructionError>, expected_result: Result<(), InstructionError>,
process_instruction: ProcessInstructionWithContext,
) -> Vec<AccountSharedData> { ) -> Vec<AccountSharedData> {
mock_process_instruction( mock_process_instruction(
&system_program::id(), &system_program::id(),
@ -520,7 +517,7 @@ mod tests {
transaction_accounts, transaction_accounts,
instruction_accounts, instruction_accounts,
expected_result, expected_result,
super::process_instruction, process_instruction,
) )
} }
@ -567,6 +564,7 @@ mod tests {
}, },
], ],
Ok(()), Ok(()),
super::process_instruction,
); );
assert_eq!(accounts[0].lamports(), 50); assert_eq!(accounts[0].lamports(), 50);
assert_eq!(accounts[1].lamports(), 50); assert_eq!(accounts[1].lamports(), 50);
@ -606,6 +604,7 @@ mod tests {
}, },
], ],
Ok(()), Ok(()),
super::process_instruction,
); );
assert_eq!(accounts[0].lamports(), 50); assert_eq!(accounts[0].lamports(), 50);
assert_eq!(accounts[1].lamports(), 50); assert_eq!(accounts[1].lamports(), 50);
@ -652,6 +651,7 @@ mod tests {
}, },
], ],
Ok(()), Ok(()),
super::process_instruction,
); );
assert_eq!(accounts[0].lamports(), 50); assert_eq!(accounts[0].lamports(), 50);
assert_eq!(accounts[1].lamports(), 50); assert_eq!(accounts[1].lamports(), 50);
@ -1079,6 +1079,7 @@ mod tests {
is_writable: false, is_writable: false,
}], }],
Ok(()), Ok(()),
super::process_instruction,
); );
} }
@ -1114,6 +1115,7 @@ mod tests {
Vec::new(), Vec::new(),
Vec::new(), Vec::new(),
Err(InstructionError::NotEnoughAccountKeys), Err(InstructionError::NotEnoughAccountKeys),
super::process_instruction,
); );
// Attempt to transfer with no destination // Attempt to transfer with no destination
@ -1130,6 +1132,7 @@ mod tests {
is_writable: false, is_writable: false,
}], }],
Err(InstructionError::NotEnoughAccountKeys), Err(InstructionError::NotEnoughAccountKeys),
super::process_instruction,
); );
} }
@ -1474,6 +1477,7 @@ mod tests {
transaction_accounts, transaction_accounts,
instruction.accounts, instruction.accounts,
expected_result, expected_result,
super::process_instruction,
) )
} }
@ -1493,6 +1497,7 @@ mod tests {
Vec::new(), Vec::new(),
Vec::new(), Vec::new(),
Err(InstructionError::NotEnoughAccountKeys), Err(InstructionError::NotEnoughAccountKeys),
super::process_instruction,
); );
} }
@ -1508,33 +1513,7 @@ mod tests {
is_writable: true, is_writable: true,
}], }],
Err(InstructionError::NotEnoughAccountKeys), Err(InstructionError::NotEnoughAccountKeys),
); super::process_instruction,
}
#[test]
fn test_process_nonce_ix_bad_recent_blockhash_state_fail() {
let pubkey = Pubkey::new_unique();
#[allow(deprecated)]
let blockhash_id = sysvar::recent_blockhashes::id();
process_instruction(
&serialize(&SystemInstruction::AdvanceNonceAccount).unwrap(),
vec![
(pubkey, create_default_account()),
(blockhash_id, create_default_account()),
],
vec![
AccountMeta {
pubkey,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: blockhash_id,
is_signer: false,
is_writable: false,
},
],
Err(InstructionError::InvalidArgument),
); );
} }
@ -1569,6 +1548,7 @@ mod tests {
}, },
], ],
Ok(()), Ok(()),
super::process_instruction,
); );
let blockhash = hash(&serialize(&0).unwrap()); let blockhash = hash(&serialize(&0).unwrap());
#[allow(deprecated)] #[allow(deprecated)]
@ -1577,9 +1557,7 @@ mod tests {
vec![IterItem(0u64, &blockhash, 0); sysvar::recent_blockhashes::MAX_ENTRIES] vec![IterItem(0u64, &blockhash, 0); sysvar::recent_blockhashes::MAX_ENTRIES]
.into_iter(), .into_iter(),
); );
mock_process_instruction( process_instruction(
&system_program::id(),
Vec::new(),
&serialize(&SystemInstruction::AdvanceNonceAccount).unwrap(), &serialize(&SystemInstruction::AdvanceNonceAccount).unwrap(),
vec![ vec![
(nonce_address, accounts[0].clone()), (nonce_address, accounts[0].clone()),
@ -1632,6 +1610,7 @@ mod tests {
Vec::new(), Vec::new(),
Vec::new(), Vec::new(),
Err(InstructionError::NotEnoughAccountKeys), Err(InstructionError::NotEnoughAccountKeys),
super::process_instruction,
); );
} }
@ -1647,81 +1626,7 @@ mod tests {
is_writable: true, is_writable: true,
}], }],
Err(InstructionError::NotEnoughAccountKeys), Err(InstructionError::NotEnoughAccountKeys),
); super::process_instruction,
}
#[test]
fn test_process_withdraw_ix_bad_recent_blockhash_state_fail() {
let nonce_address = Pubkey::new_unique();
let pubkey = Pubkey::new_unique();
#[allow(deprecated)]
let blockhash_id = sysvar::recent_blockhashes::id();
process_instruction(
&serialize(&SystemInstruction::WithdrawNonceAccount(42)).unwrap(),
vec![
(nonce_address, create_default_account()),
(pubkey, create_default_account()),
(blockhash_id, create_default_account()),
],
vec![
AccountMeta {
pubkey: nonce_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey,
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: blockhash_id,
is_signer: false,
is_writable: false,
},
],
Err(InstructionError::InvalidArgument),
);
}
#[test]
fn test_process_withdraw_ix_bad_rent_state_fail() {
let nonce_address = Pubkey::new_unique();
let nonce_account = nonce_account::create_account(1_000_000).into_inner();
let pubkey = Pubkey::new_unique();
#[allow(deprecated)]
let blockhash_id = sysvar::recent_blockhashes::id();
process_instruction(
&serialize(&SystemInstruction::WithdrawNonceAccount(42)).unwrap(),
vec![
(nonce_address, nonce_account),
(pubkey, create_default_account()),
(blockhash_id, create_default_recent_blockhashes_account()),
(sysvar::rent::id(), create_default_account()),
],
vec![
AccountMeta {
pubkey: nonce_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey,
is_signer: true,
is_writable: false,
},
AccountMeta {
pubkey: blockhash_id,
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: sysvar::rent::id(),
is_signer: false,
is_writable: false,
},
],
Err(InstructionError::InvalidArgument),
); );
} }
@ -1763,6 +1668,7 @@ mod tests {
}, },
], ],
Ok(()), Ok(()),
super::process_instruction,
); );
} }
@ -1773,6 +1679,7 @@ mod tests {
Vec::new(), Vec::new(),
Vec::new(), Vec::new(),
Err(InstructionError::NotEnoughAccountKeys), Err(InstructionError::NotEnoughAccountKeys),
super::process_instruction,
); );
} }
@ -1789,68 +1696,7 @@ mod tests {
is_writable: true, is_writable: true,
}], }],
Err(InstructionError::NotEnoughAccountKeys), Err(InstructionError::NotEnoughAccountKeys),
); super::process_instruction,
}
#[test]
fn test_process_initialize_bad_recent_blockhash_state_fail() {
let nonce_address = Pubkey::new_unique();
let nonce_account = nonce_account::create_account(1_000_000).into_inner();
#[allow(deprecated)]
let blockhash_id = sysvar::recent_blockhashes::id();
process_instruction(
&serialize(&SystemInstruction::InitializeNonceAccount(nonce_address)).unwrap(),
vec![
(nonce_address, nonce_account),
(blockhash_id, create_default_account()),
],
vec![
AccountMeta {
pubkey: nonce_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: blockhash_id,
is_signer: false,
is_writable: false,
},
],
Err(InstructionError::InvalidArgument),
);
}
#[test]
fn test_process_initialize_ix_bad_rent_state_fail() {
let nonce_address = Pubkey::new_unique();
let nonce_account = nonce_account::create_account(1_000_000).into_inner();
#[allow(deprecated)]
let blockhash_id = sysvar::recent_blockhashes::id();
process_instruction(
&serialize(&SystemInstruction::InitializeNonceAccount(nonce_address)).unwrap(),
vec![
(nonce_address, nonce_account),
(blockhash_id, create_default_recent_blockhashes_account()),
(sysvar::rent::id(), create_default_account()),
],
vec![
AccountMeta {
pubkey: nonce_address,
is_signer: true,
is_writable: true,
},
AccountMeta {
pubkey: blockhash_id,
is_signer: false,
is_writable: false,
},
AccountMeta {
pubkey: sysvar::rent::id(),
is_signer: false,
is_writable: false,
},
],
Err(InstructionError::InvalidArgument),
); );
} }
@ -1885,6 +1731,7 @@ mod tests {
}, },
], ],
Ok(()), Ok(()),
super::process_instruction,
); );
} }
@ -1919,6 +1766,7 @@ mod tests {
}, },
], ],
Ok(()), Ok(()),
super::process_instruction,
); );
process_instruction( process_instruction(
&serialize(&SystemInstruction::AuthorizeNonceAccount(nonce_address)).unwrap(), &serialize(&SystemInstruction::AuthorizeNonceAccount(nonce_address)).unwrap(),
@ -1929,6 +1777,7 @@ mod tests {
is_writable: true, is_writable: true,
}], }],
Ok(()), Ok(()),
super::process_instruction,
); );
} }
@ -2034,6 +1883,7 @@ mod tests {
}, },
], ],
Err(NonceError::NoRecentBlockhashes.into()), Err(NonceError::NoRecentBlockhashes.into()),
super::process_instruction,
); );
} }
@ -2068,15 +1918,14 @@ mod tests {
}, },
], ],
Ok(()), Ok(()),
super::process_instruction,
); );
#[allow(deprecated)] #[allow(deprecated)]
let new_recent_blockhashes_account = let new_recent_blockhashes_account =
solana_sdk::recent_blockhashes_account::create_account_with_data_for_test( solana_sdk::recent_blockhashes_account::create_account_with_data_for_test(
vec![].into_iter(), vec![].into_iter(),
); );
mock_process_instruction( process_instruction(
&system_program::id(),
Vec::new(),
&serialize(&SystemInstruction::AdvanceNonceAccount).unwrap(), &serialize(&SystemInstruction::AdvanceNonceAccount).unwrap(),
vec![ vec![
(nonce_address, accounts[0].clone()), (nonce_address, accounts[0].clone()),

View File

@ -1,13 +1,12 @@
use { use {
crate::{ crate::{
account::{from_account, AccountSharedData, ReadableAccount}, account::{AccountSharedData, ReadableAccount},
account_utils::{State, StateMut}, account_utils::{State, StateMut},
}, },
solana_program::{clock::Epoch, instruction::InstructionError, pubkey::Pubkey, sysvar::Sysvar}, solana_program::{clock::Epoch, instruction::InstructionError, pubkey::Pubkey},
std::{ std::{
cell::{Ref, RefCell, RefMut}, cell::{Ref, RefCell, RefMut},
iter::FromIterator, iter::FromIterator,
ops::Deref,
rc::Rc, rc::Rc,
}, },
}; };
@ -249,31 +248,15 @@ where
} }
} }
pub fn check_sysvar_keyed_account<'a, S: Sysvar>(
keyed_account: &'a crate::keyed_account::KeyedAccount<'_>,
) -> Result<impl Deref<Target = AccountSharedData> + 'a, InstructionError> {
if !S::check_id(keyed_account.unsigned_key()) {
return Err(InstructionError::InvalidArgument);
}
keyed_account.try_account_ref()
}
pub fn from_keyed_account<S: Sysvar>(
keyed_account: &crate::keyed_account::KeyedAccount,
) -> Result<S, InstructionError> {
let sysvar_account = check_sysvar_keyed_account::<S>(keyed_account)?;
from_account::<S, AccountSharedData>(&*sysvar_account).ok_or(InstructionError::InvalidArgument)
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use { use {
super::*, super::*,
crate::{ crate::{
account::{create_account_for_test, to_account}, account::{create_account_for_test, from_account, to_account},
pubkey::Pubkey, pubkey::Pubkey,
sysvar::Sysvar,
}, },
std::cell::RefCell,
}; };
#[repr(C)] #[repr(C)]
@ -296,7 +279,6 @@ mod tests {
fn test_sysvar_keyed_account_to_from() { fn test_sysvar_keyed_account_to_from() {
let test_sysvar = TestSysvar::default(); let test_sysvar = TestSysvar::default();
let key = crate::keyed_account::tests::id(); let key = crate::keyed_account::tests::id();
let wrong_key = Pubkey::new_unique();
let account = create_account_for_test(&test_sysvar); let account = create_account_for_test(&test_sysvar);
let test_sysvar = from_account::<TestSysvar, _>(&account).unwrap(); let test_sysvar = from_account::<TestSysvar, _>(&account).unwrap();
@ -306,16 +288,5 @@ mod tests {
to_account(&test_sysvar, &mut account).unwrap(); to_account(&test_sysvar, &mut account).unwrap();
let test_sysvar = from_account::<TestSysvar, _>(&account).unwrap(); let test_sysvar = from_account::<TestSysvar, _>(&account).unwrap();
assert_eq!(test_sysvar, TestSysvar::default()); assert_eq!(test_sysvar, TestSysvar::default());
let account = RefCell::new(account);
let keyed_account = KeyedAccount::new(&key, false, &account);
let new_test_sysvar = from_keyed_account::<TestSysvar>(&keyed_account).unwrap();
assert_eq!(test_sysvar, new_test_sysvar);
let keyed_account = KeyedAccount::new(&wrong_key, false, &account);
assert_eq!(
from_keyed_account::<TestSysvar>(&keyed_account),
Err(InstructionError::InvalidArgument)
);
} }
} }