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:
committed by
GitHub
parent
60af1a4cce
commit
c16cf9cf8a
@ -1,13 +1,12 @@
|
||||
use {
|
||||
crate::{
|
||||
account::{from_account, AccountSharedData, ReadableAccount},
|
||||
account::{AccountSharedData, ReadableAccount},
|
||||
account_utils::{State, StateMut},
|
||||
},
|
||||
solana_program::{clock::Epoch, instruction::InstructionError, pubkey::Pubkey, sysvar::Sysvar},
|
||||
solana_program::{clock::Epoch, instruction::InstructionError, pubkey::Pubkey},
|
||||
std::{
|
||||
cell::{Ref, RefCell, RefMut},
|
||||
iter::FromIterator,
|
||||
ops::Deref,
|
||||
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)]
|
||||
mod tests {
|
||||
use {
|
||||
super::*,
|
||||
crate::{
|
||||
account::{create_account_for_test, to_account},
|
||||
account::{create_account_for_test, from_account, to_account},
|
||||
pubkey::Pubkey,
|
||||
sysvar::Sysvar,
|
||||
},
|
||||
std::cell::RefCell,
|
||||
};
|
||||
|
||||
#[repr(C)]
|
||||
@ -296,7 +279,6 @@ mod tests {
|
||||
fn test_sysvar_keyed_account_to_from() {
|
||||
let test_sysvar = TestSysvar::default();
|
||||
let key = crate::keyed_account::tests::id();
|
||||
let wrong_key = Pubkey::new_unique();
|
||||
|
||||
let account = create_account_for_test(&test_sysvar);
|
||||
let test_sysvar = from_account::<TestSysvar, _>(&account).unwrap();
|
||||
@ -306,16 +288,5 @@ mod tests {
|
||||
to_account(&test_sysvar, &mut account).unwrap();
|
||||
let test_sysvar = from_account::<TestSysvar, _>(&account).unwrap();
|
||||
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)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user