Fetch sysvars from invoke context for vote program (#22444)

This commit is contained in:
Justin Starry
2022-01-13 08:41:48 +08:00
committed by GitHub
parent eaae2f3538
commit b211f839cb
2 changed files with 58 additions and 49 deletions

View File

@ -7,6 +7,7 @@ use {
std::{
cell::{Ref, RefCell, RefMut},
iter::FromIterator,
ops::Deref,
rc::Rc,
},
};
@ -248,14 +249,20 @@ where
}
}
pub fn from_keyed_account<S: Sysvar>(
keyed_account: &crate::keyed_account::KeyedAccount,
) -> Result<S, InstructionError> {
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);
}
from_account::<S, AccountSharedData>(&*keyed_account.try_account_ref()?)
.ok_or(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)]